blob: cff2597aade89476c032d6c726db880e42258f66 [file] [log] [blame]
[email protected]1bc78422011-03-31 08:41:381// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e2ddbc92010-10-15 20:02:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]8a6ff28d2010-12-02 16:35:195#include "base/string_number_conversions.h"
[email protected]a82744532011-02-11 16:15:536#include "base/utf_string_conversions.h"
[email protected]a314ee5a2010-10-26 21:23:287#include "base/values.h"
[email protected]8a6ff28d2010-12-02 16:35:198#include "chrome/browser/about_flags.h"
[email protected]e2ddbc92010-10-15 20:02:079#include "chrome/common/chrome_switches.h"
10#include "chrome/common/pref_names.h"
[email protected]8ad3636e2011-08-01 22:31:4011#include "chrome/test/base/testing_pref_service.h"
[email protected]a314ee5a2010-10-26 21:23:2812#include "grit/chromium_strings.h"
[email protected]e2ddbc92010-10-15 20:02:0713#include "testing/gtest/include/gtest/gtest.h"
14
[email protected]a314ee5a2010-10-26 21:23:2815const char kFlags1[] = "flag1";
16const char kFlags2[] = "flag2";
17const char kFlags3[] = "flag3";
[email protected]8a6ff28d2010-12-02 16:35:1918const char kFlags4[] = "flag4";
[email protected]e2ddbc92010-10-15 20:02:0719
[email protected]a314ee5a2010-10-26 21:23:2820const char kSwitch1[] = "switch";
21const char kSwitch2[] = "switch2";
22const char kSwitch3[] = "switch3";
[email protected]a82744532011-02-11 16:15:5323const char kValueForSwitch2[] = "value_for_switch2";
[email protected]e2ddbc92010-10-15 20:02:0724
[email protected]8a6ff28d2010-12-02 16:35:1925const char kMultiSwitch1[] = "multi_switch1";
26const char kMultiSwitch2[] = "multi_switch2";
[email protected]a82744532011-02-11 16:15:5327const char kValueForMultiSwitch2[] = "value_for_multi_switch2";
[email protected]8a6ff28d2010-12-02 16:35:1928
[email protected]e2ddbc92010-10-15 20:02:0729namespace about_flags {
30
[email protected]8a6ff28d2010-12-02 16:35:1931const Experiment::Choice kMultiChoices[] = {
[email protected]a82744532011-02-11 16:15:5332 { IDS_PRODUCT_NAME, "", "" },
33 { IDS_PRODUCT_NAME, kMultiSwitch1, "" },
34 { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 },
[email protected]8a6ff28d2010-12-02 16:35:1935};
36
37// The experiments that are set for these tests. The 3rd experiment is not
38// supported on the current platform, all others are.
[email protected]a314ee5a2010-10-26 21:23:2839static Experiment kExperiments[] = {
40 {
41 kFlags1,
42 IDS_PRODUCT_NAME,
43 IDS_PRODUCT_NAME,
44 0, // Ends up being mapped to the current platform.
[email protected]8a6ff28d2010-12-02 16:35:1945 Experiment::SINGLE_VALUE,
46 kSwitch1,
[email protected]a82744532011-02-11 16:15:5347 "",
[email protected]8a6ff28d2010-12-02 16:35:1948 NULL,
49 0
[email protected]a314ee5a2010-10-26 21:23:2850 },
51 {
52 kFlags2,
53 IDS_PRODUCT_NAME,
54 IDS_PRODUCT_NAME,
55 0, // Ends up being mapped to the current platform.
[email protected]8a6ff28d2010-12-02 16:35:1956 Experiment::SINGLE_VALUE,
57 kSwitch2,
[email protected]a82744532011-02-11 16:15:5358 kValueForSwitch2,
[email protected]8a6ff28d2010-12-02 16:35:1959 NULL,
60 0
[email protected]a314ee5a2010-10-26 21:23:2861 },
62 {
63 kFlags3,
64 IDS_PRODUCT_NAME,
65 IDS_PRODUCT_NAME,
66 0, // This ends up enabling for an OS other than the current.
[email protected]8a6ff28d2010-12-02 16:35:1967 Experiment::SINGLE_VALUE,
68 kSwitch3,
[email protected]a82744532011-02-11 16:15:5369 "",
[email protected]8a6ff28d2010-12-02 16:35:1970 NULL,
71 0
72 },
73 {
74 kFlags4,
75 IDS_PRODUCT_NAME,
76 IDS_PRODUCT_NAME,
77 0, // Ends up being mapped to the current platform.
78 Experiment::MULTI_VALUE,
79 "",
[email protected]a82744532011-02-11 16:15:5380 "",
[email protected]8a6ff28d2010-12-02 16:35:1981 kMultiChoices,
82 arraysize(kMultiChoices)
[email protected]a314ee5a2010-10-26 21:23:2883 },
84};
85
[email protected]e2ddbc92010-10-15 20:02:0786class AboutFlagsTest : public ::testing::Test {
87 protected:
88 AboutFlagsTest() {
[email protected]5b199522012-12-22 17:24:4489 prefs_.RegisterListPref(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:0790 testing::ClearState();
91 }
92
[email protected]a314ee5a2010-10-26 21:23:2893 virtual void SetUp() {
94 for (size_t i = 0; i < arraysize(kExperiments); ++i)
95 kExperiments[i].supported_platforms = GetCurrentPlatform();
96
97 int os_other_than_current = 1;
98 while (os_other_than_current == GetCurrentPlatform())
99 os_other_than_current <<= 1;
100 kExperiments[2].supported_platforms = os_other_than_current;
101
102 testing::SetExperiments(kExperiments, arraysize(kExperiments));
103 }
104
105 virtual void TearDown() {
106 testing::SetExperiments(NULL, 0);
107 }
108
[email protected]5b199522012-12-22 17:24:44109 TestingPrefServiceSimple prefs_;
[email protected]e2ddbc92010-10-15 20:02:07110};
111
112TEST_F(AboutFlagsTest, ChangeNeedsRestart) {
[email protected]e2ddbc92010-10-15 20:02:07113 EXPECT_FALSE(IsRestartNeededToCommitChanges());
114 SetExperimentEnabled(&prefs_, kFlags1, true);
115 EXPECT_TRUE(IsRestartNeededToCommitChanges());
116}
117
118TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) {
[email protected]e2ddbc92010-10-15 20:02:07119 // Add two experiments, check they're there.
120 SetExperimentEnabled(&prefs_, kFlags1, true);
121 SetExperimentEnabled(&prefs_, kFlags2, true);
122
[email protected]1bc78422011-03-31 08:41:38123 const ListValue* experiments_list = prefs_.GetList(
[email protected]e2ddbc92010-10-15 20:02:07124 prefs::kEnabledLabsExperiments);
125 ASSERT_TRUE(experiments_list != NULL);
126
127 ASSERT_EQ(2u, experiments_list->GetSize());
128
129 std::string s0;
130 ASSERT_TRUE(experiments_list->GetString(0, &s0));
131 std::string s1;
132 ASSERT_TRUE(experiments_list->GetString(1, &s1));
133
134 EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1);
135 EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2);
136
137 // Remove one experiment, check the other's still around.
138 SetExperimentEnabled(&prefs_, kFlags2, false);
139
[email protected]1bc78422011-03-31 08:41:38140 experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:07141 ASSERT_TRUE(experiments_list != NULL);
142 ASSERT_EQ(1u, experiments_list->GetSize());
143 ASSERT_TRUE(experiments_list->GetString(0, &s0));
144 EXPECT_TRUE(s0 == kFlags1);
145}
146
147TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) {
[email protected]e2ddbc92010-10-15 20:02:07148 // Add two experiments, check the pref exists.
149 SetExperimentEnabled(&prefs_, kFlags1, true);
150 SetExperimentEnabled(&prefs_, kFlags2, true);
[email protected]1bc78422011-03-31 08:41:38151 const ListValue* experiments_list = prefs_.GetList(
[email protected]e2ddbc92010-10-15 20:02:07152 prefs::kEnabledLabsExperiments);
153 ASSERT_TRUE(experiments_list != NULL);
154
155 // Remove both, the pref should have been removed completely.
156 SetExperimentEnabled(&prefs_, kFlags1, false);
157 SetExperimentEnabled(&prefs_, kFlags2, false);
[email protected]1bc78422011-03-31 08:41:38158 experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:07159 EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0);
160}
161
162TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) {
[email protected]e2ddbc92010-10-15 20:02:07163 SetExperimentEnabled(&prefs_, kFlags1, true);
164
[email protected]947446b2010-10-21 03:36:31165 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]e2ddbc92010-10-15 20:02:07166 command_line.AppendSwitch("foo");
167
168 EXPECT_TRUE(command_line.HasSwitch("foo"));
[email protected]a314ee5a2010-10-26 21:23:28169 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
[email protected]e2ddbc92010-10-15 20:02:07170
171 ConvertFlagsToSwitches(&prefs_, &command_line);
172
173 EXPECT_TRUE(command_line.HasSwitch("foo"));
[email protected]cd7fa99f2011-09-07 01:24:55174 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]e2ddbc92010-10-15 20:02:07175}
176
177TEST_F(AboutFlagsTest, RemoveFlagSwitches) {
[email protected]e2ddbc92010-10-15 20:02:07178 std::map<std::string, CommandLine::StringType> switch_list;
[email protected]a314ee5a2010-10-26 21:23:28179 switch_list[kSwitch1] = CommandLine::StringType();
[email protected]e2ddbc92010-10-15 20:02:07180 switch_list[switches::kFlagSwitchesBegin] = CommandLine::StringType();
181 switch_list[switches::kFlagSwitchesEnd] = CommandLine::StringType();
182 switch_list["foo"] = CommandLine::StringType();
183
184 SetExperimentEnabled(&prefs_, kFlags1, true);
185
186 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called.
187 RemoveFlagsSwitches(&switch_list);
188 ASSERT_EQ(4u, switch_list.size());
[email protected]a314ee5a2010-10-26 21:23:28189 EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end());
[email protected]e2ddbc92010-10-15 20:02:07190 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) !=
191 switch_list.end());
192 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) !=
193 switch_list.end());
194 EXPECT_TRUE(switch_list.find("foo") != switch_list.end());
195
196 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again.
[email protected]947446b2010-10-21 03:36:31197 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]e2ddbc92010-10-15 20:02:07198 command_line.AppendSwitch("foo");
199 ConvertFlagsToSwitches(&prefs_, &command_line);
200 RemoveFlagsSwitches(&switch_list);
201
202 // Now the about:flags-related switch should have been removed.
203 ASSERT_EQ(1u, switch_list.size());
204 EXPECT_TRUE(switch_list.find("foo") != switch_list.end());
205}
206
[email protected]a314ee5a2010-10-26 21:23:28207// Tests enabling experiments that aren't supported on the current platform.
208TEST_F(AboutFlagsTest, PersistAndPrune) {
[email protected]8a6ff28d2010-12-02 16:35:19209 // Enable experiments 1 and 3.
[email protected]a314ee5a2010-10-26 21:23:28210 SetExperimentEnabled(&prefs_, kFlags1, true);
211 SetExperimentEnabled(&prefs_, kFlags3, true);
212 CommandLine command_line(CommandLine::NO_PROGRAM);
213 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
214 EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
215
216 // Convert the flags to switches. Experiment 3 shouldn't be among the switches
217 // as it is not applicable to the current platform.
218 ConvertFlagsToSwitches(&prefs_, &command_line);
[email protected]cd7fa99f2011-09-07 01:24:55219 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]a314ee5a2010-10-26 21:23:28220 EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
221
222 // Experiment 3 should show still be persisted in preferences though.
223 scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_));
224 ASSERT_TRUE(switch_prefs.get());
[email protected]cc3e2052011-12-20 01:01:40225 EXPECT_EQ(arraysize(kExperiments), switch_prefs->GetSize());
[email protected]8a6ff28d2010-12-02 16:35:19226}
227
[email protected]a82744532011-02-11 16:15:53228// Tests that switches which should have values get them in the command
229// line.
230TEST_F(AboutFlagsTest, CheckValues) {
231 // Enable experiments 1 and 2.
232 SetExperimentEnabled(&prefs_, kFlags1, true);
233 SetExperimentEnabled(&prefs_, kFlags2, true);
234 CommandLine command_line(CommandLine::NO_PROGRAM);
235 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
236 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
237
238 // Convert the flags to switches.
239 ConvertFlagsToSwitches(&prefs_, &command_line);
[email protected]cd7fa99f2011-09-07 01:24:55240 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]61a4c6f2011-07-20 04:54:52241 EXPECT_EQ(std::string(""), command_line.GetSwitchValueASCII(kSwitch1));
[email protected]cd7fa99f2011-09-07 01:24:55242 EXPECT_TRUE(command_line.HasSwitch(kSwitch2));
243 EXPECT_EQ(std::string(kValueForSwitch2),
[email protected]a82744532011-02-11 16:15:53244 command_line.GetSwitchValueASCII(kSwitch2));
245
246 // Confirm that there is no '=' in the command line for simple switches.
247 std::string switch1_with_equals = std::string("--") +
248 std::string(kSwitch1) +
249 std::string("=");
250#if defined(OS_WIN)
251 EXPECT_EQ(std::wstring::npos,
[email protected]61a4c6f2011-07-20 04:54:52252 command_line.GetCommandLineString().find(
[email protected]a82744532011-02-11 16:15:53253 ASCIIToWide(switch1_with_equals)));
254#else
255 EXPECT_EQ(std::string::npos,
[email protected]61a4c6f2011-07-20 04:54:52256 command_line.GetCommandLineString().find(switch1_with_equals));
[email protected]a82744532011-02-11 16:15:53257#endif
258
259 // And confirm there is a '=' for switches with values.
260 std::string switch2_with_equals = std::string("--") +
261 std::string(kSwitch2) +
262 std::string("=");
[email protected]5b199522012-12-22 17:24:44263#if defined(OS_WIN)
[email protected]cd7fa99f2011-09-07 01:24:55264 EXPECT_NE(std::wstring::npos,
[email protected]61a4c6f2011-07-20 04:54:52265 command_line.GetCommandLineString().find(
[email protected]a82744532011-02-11 16:15:53266 ASCIIToWide(switch2_with_equals)));
267#else
[email protected]cd7fa99f2011-09-07 01:24:55268 EXPECT_NE(std::string::npos,
[email protected]61a4c6f2011-07-20 04:54:52269 command_line.GetCommandLineString().find(switch2_with_equals));
[email protected]a82744532011-02-11 16:15:53270#endif
271
272 // And it should persist
273 scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_));
274 ASSERT_TRUE(switch_prefs.get());
[email protected]cc3e2052011-12-20 01:01:40275 EXPECT_EQ(arraysize(kExperiments), switch_prefs->GetSize());
[email protected]a82744532011-02-11 16:15:53276}
277
[email protected]28e35af2011-02-09 12:56:22278// Tests multi-value type experiments.
[email protected]8a6ff28d2010-12-02 16:35:19279TEST_F(AboutFlagsTest, MultiValues) {
[email protected]28e35af2011-02-09 12:56:22280 // Initially, the first "deactivated" option of the multi experiment should
281 // be set.
[email protected]8a6ff28d2010-12-02 16:35:19282 {
283 CommandLine command_line(CommandLine::NO_PROGRAM);
284 ConvertFlagsToSwitches(&prefs_, &command_line);
[email protected]28e35af2011-02-09 12:56:22285 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
[email protected]8a6ff28d2010-12-02 16:35:19286 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
287 }
288
[email protected]28e35af2011-02-09 12:56:22289 // Enable the 2nd choice of the multi-value.
[email protected]8a6ff28d2010-12-02 16:35:19290 SetExperimentEnabled(&prefs_, std::string(kFlags4) +
291 std::string(testing::kMultiSeparator) +
[email protected]28e35af2011-02-09 12:56:22292 base::IntToString(2), true);
[email protected]8a6ff28d2010-12-02 16:35:19293 {
294 CommandLine command_line(CommandLine::NO_PROGRAM);
295 ConvertFlagsToSwitches(&prefs_, &command_line);
296 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
[email protected]cd7fa99f2011-09-07 01:24:55297 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2));
298 EXPECT_EQ(std::string(kValueForMultiSwitch2),
[email protected]a82744532011-02-11 16:15:53299 command_line.GetSwitchValueASCII(kMultiSwitch2));
[email protected]8a6ff28d2010-12-02 16:35:19300 }
301
302 // Disable the multi-value experiment.
[email protected]28e35af2011-02-09 12:56:22303 SetExperimentEnabled(&prefs_, std::string(kFlags4) +
304 std::string(testing::kMultiSeparator) +
305 base::IntToString(0), true);
[email protected]8a6ff28d2010-12-02 16:35:19306 {
307 CommandLine command_line(CommandLine::NO_PROGRAM);
308 ConvertFlagsToSwitches(&prefs_, &command_line);
309 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
310 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
311 }
312}
313
314// Makes sure there are no separators in any of the experiment names.
315TEST_F(AboutFlagsTest, NoSeparators) {
316 testing::SetExperiments(NULL, 0);
317 size_t count;
318 const Experiment* experiments = testing::GetExperiments(&count);
319 for (size_t i = 0; i < count; ++i) {
320 std::string name = experiments->internal_name;
321 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i;
322 }
[email protected]a314ee5a2010-10-26 21:23:28323}
324
[email protected]e2ddbc92010-10-15 20:02:07325} // namespace about_flags