summaryrefslogtreecommitdiffstats
path: root/chrome/app/breakpad_unittest_win.cc
blob: b33bffbb998db4ad1419dfb1bc34ec73d6dcda29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <vector>

#include "base/metrics/field_trial.h"
#include "base/stringprintf.h"
#include "breakpad/src/client/windows/common/ipc_protocol.h"
#include "chrome/app/breakpad_field_trial_win.h"
#include "chrome/app/breakpad_win.h"
#include "chrome/common/child_process_logging.h"
#include "testing/gtest/include/gtest/gtest.h"

using breakpad_win::g_custom_entries;
using breakpad_win::g_experiment_chunks_offset;
using breakpad_win::g_num_of_experiments_offset;

class ChromeAppBreakpadTest : public testing::Test {
 public:
  ChromeAppBreakpadTest() {
    testing::InitCustomInfoEntries();
  }

 protected:
  typedef std::vector<base::FieldTrial::NameGroupId> Experiments;
  void ValidateExperimentChunks(const Experiments& experiments) {
    UpdateExperiments();
    EXPECT_STREQ(base::StringPrintf(L"%d", experiments.size()).c_str(),
                 (*g_custom_entries)[g_num_of_experiments_offset].value);
    // We make a copy of the array that we empty as we find the experiments.
    Experiments experiments_left(experiments);
    for (int i = 0; i < kMaxReportedExperimentChunks; ++i) {
      EXPECT_STREQ(base::StringPrintf(L"experiment-chunk-%i", i + 1).c_str(),
                   (*g_custom_entries)[g_experiment_chunks_offset + i].name);
      if (experiments_left.empty()) {
        // All other slots should be empty.
        EXPECT_STREQ(
            L"", (*g_custom_entries)[g_experiment_chunks_offset + i].value);
      } else {
        // We can't guarantee the order, so we must search for them all.
        Experiments::const_iterator experiment = experiments_left.begin();
        while (experiment != experiments_left.end()) {
          if (wcsstr((*g_custom_entries)[g_experiment_chunks_offset + i].value,
              base::StringPrintf(
                  L"%x-%x", experiment->name, experiment->group).c_str())) {
            experiment = experiments_left.erase(experiment);
          } else {
            ++experiment;
          }
        }
      }
    }
    EXPECT_TRUE(experiments_left.empty());
  }

 private:
  static const wchar_t* kNumExperiments;
  static const size_t kNumExperimentsSize;
};

const wchar_t* ChromeAppBreakpadTest::kNumExperiments = L"num-experiments";
const size_t ChromeAppBreakpadTest::kNumExperimentsSize =
    wcslen(ChromeAppBreakpadTest::kNumExperiments);

TEST_F(ChromeAppBreakpadTest, ExperimentList) {
  base::FieldTrialList field_trial_list("ABCDE");
  base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial("All", "To");
  base::FieldTrial::NameGroupId name_group_id;
  trial->GetNameGroupId(&name_group_id);
  Experiments experiments;
  experiments.push_back(name_group_id);
  ValidateExperimentChunks(experiments);

  trial = base::FieldTrialList::CreateFieldTrial("There", "You Are");
  trial->GetNameGroupId(&name_group_id);
  experiments.push_back(name_group_id);
  ValidateExperimentChunks(experiments);

  trial = base::FieldTrialList::CreateFieldTrial("Peter", "Sellers");
  trial->GetNameGroupId(&name_group_id);
  experiments.push_back(name_group_id);
  ValidateExperimentChunks(experiments);

  trial = base::FieldTrialList::CreateFieldTrial("Eat me", "Drink me");
  trial->GetNameGroupId(&name_group_id);
  experiments.push_back(name_group_id);
  ValidateExperimentChunks(experiments);
}