summaryrefslogtreecommitdiffstats
path: root/chrome/installer/gcapi/gcapi_omaha_experiment_test.cc
blob: 43b7bcaeb72f97c2526a22360d3fc365e66db7b3 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2013 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 "chrome/installer/gcapi/gcapi_omaha_experiment.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/installer/gcapi/gcapi.h"
#include "chrome/installer/gcapi/gcapi_test_registry_overrider.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/google_update_experiment_util.h"
#include "chrome/installer/util/google_update_settings.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::ASCIIToUTF16;

namespace {

const wchar_t kBrand[] = L"ABCD";
const uint16 kUserLevel = GCAPI_INVOKED_STANDARD_SHELL;

const wchar_t kSomeExperiments[] = L"myexp=1|Aug 2;yourexp=2|Sep 5";
const wchar_t kSomeOtherExperiments[] = L"anotherexp=joe|Jun 7 2008";
const wchar_t kSomeMoreExperiments[] = L"moreexp=foo|Jul 31 1999";

class GCAPIOmahaExperimentTest : public ::testing::Test {
 protected:
  GCAPIOmahaExperimentTest()
      : brand_(kBrand),
        reactivation_label_(gcapi_internals::GetGCAPIExperimentLabel(
            kBrand, gcapi_internals::kReactivationLabel)),
        relaunch_label_(gcapi_internals::GetGCAPIExperimentLabel(
            kBrand, gcapi_internals::kRelaunchLabel)) {
  }

  void VerifyExperimentLabels(const base::string16& expected_labels) {
    base::string16 actual_labels;
    EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(false,
                                                           &actual_labels));
    EXPECT_EQ(expected_labels, actual_labels);
  }

  base::string16 brand_;
  base::string16 reactivation_label_;
  base::string16 relaunch_label_;

  const GCAPITestRegistryOverrider gcapi_test_registry_overrider_;
};

TEST_F(GCAPIOmahaExperimentTest, SetReactivationLabelFromEmptyExperiments) {
  ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));
  VerifyExperimentLabels(reactivation_label_);
}

// Test the relaunch label once; all other tests go more in depth, but since
// both labels use the same logic underneath there is no need to test both in
// depth.
TEST_F(GCAPIOmahaExperimentTest, SetRelaunchLabelFromEmptyExperiments) {
  ASSERT_TRUE(SetRelaunchExperimentLabels(kBrand, kUserLevel));
  VerifyExperimentLabels(relaunch_label_);
}

TEST_F(GCAPIOmahaExperimentTest, SetReactivationLabelWithExistingExperiments) {
  GoogleUpdateSettings::SetExperimentLabels(false, kSomeExperiments);

  ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));

  base::string16 expected_labels(kSomeExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(reactivation_label_);
  VerifyExperimentLabels(expected_labels);
}

TEST_F(GCAPIOmahaExperimentTest,
       SetReactivationLabelWithExistingIdenticalExperiment) {
  base::string16 previous_labels(kSomeExperiments);
  previous_labels += google_update::kExperimentLabelSeparator;
  previous_labels.append(reactivation_label_);
  previous_labels += google_update::kExperimentLabelSeparator;
  previous_labels.append(kSomeOtherExperiments);
  GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);

  ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));

  base::string16 expected_labels(kSomeExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(kSomeOtherExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(reactivation_label_);
  VerifyExperimentLabels(expected_labels);
}

TEST_F(GCAPIOmahaExperimentTest,
       SetReactivationLabelWithExistingIdenticalAtBeginning) {
  base::string16 previous_labels(reactivation_label_);
  previous_labels += google_update::kExperimentLabelSeparator;
  previous_labels.append(kSomeExperiments);
  GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);

  ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));

  base::string16 expected_labels(kSomeExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(reactivation_label_);
  VerifyExperimentLabels(expected_labels);
}

TEST_F(GCAPIOmahaExperimentTest,
       SetReactivationLabelWithFakeMatchInAnExperiment) {
  base::string16 previous_labels(kSomeExperiments);
  previous_labels += google_update::kExperimentLabelSeparator;
  previous_labels.append(L"blah_");
  // Shouldn't match deletion criteria.
  previous_labels.append(reactivation_label_);
  previous_labels += google_update::kExperimentLabelSeparator;
  previous_labels.append(kSomeOtherExperiments);
  previous_labels += google_update::kExperimentLabelSeparator;
  // Should match the deletion criteria.
  previous_labels.append(reactivation_label_);
  previous_labels += google_update::kExperimentLabelSeparator;
  previous_labels.append(kSomeMoreExperiments);
  GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);

  ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));

  base::string16 expected_labels(kSomeExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(L"blah_");
  expected_labels.append(reactivation_label_);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(kSomeOtherExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(kSomeMoreExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(reactivation_label_);
  VerifyExperimentLabels(expected_labels);
}

TEST_F(GCAPIOmahaExperimentTest,
       SetReactivationLabelWithFakeMatchInAnExperimentAndNoRealMatch) {
  base::string16 previous_labels(kSomeExperiments);
  previous_labels += google_update::kExperimentLabelSeparator;
  previous_labels.append(L"blah_");
  // Shouldn't match deletion criteria.
  previous_labels.append(reactivation_label_);
  previous_labels += google_update::kExperimentLabelSeparator;
  previous_labels.append(kSomeOtherExperiments);
  GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);

  ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));

  base::string16 expected_labels(kSomeExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(L"blah_");
  expected_labels.append(reactivation_label_);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(kSomeOtherExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(reactivation_label_);
  VerifyExperimentLabels(expected_labels);
}

TEST_F(GCAPIOmahaExperimentTest,
       SetReactivationLabelWithExistingEntryWithLabelAsPrefix) {
  base::string16 previous_labels(kSomeExperiments);
  previous_labels += google_update::kExperimentLabelSeparator;
  // Append prefix matching the label, but not followed by '='.
  previous_labels.append(gcapi_internals::kReactivationLabel);
  // Shouldn't match deletion criteria.
  previous_labels.append(kSomeOtherExperiments);
  GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);

  ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));

  base::string16 expected_labels(kSomeExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(gcapi_internals::kReactivationLabel);
  expected_labels.append(kSomeOtherExperiments);
  expected_labels += google_update::kExperimentLabelSeparator;
  expected_labels.append(reactivation_label_);
  VerifyExperimentLabels(expected_labels);
}

}  // namespace