blob: 9c2245eca68e55981b52e69b34701789412db66a [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]3853a4c2013-02-11 17:15:575#include "base/prefs/pref_registry_simple.h"
[email protected]e5ba874f2013-02-14 17:20:196#include "base/prefs/testing_pref_service.h"
[email protected]3ea1b182013-02-08 22:38:417#include "base/strings/string_number_conversions.h"
[email protected]a82744532011-02-11 16:15:538#include "base/utf_string_conversions.h"
[email protected]a314ee5a2010-10-26 21:23:289#include "base/values.h"
[email protected]8a6ff28d2010-12-02 16:35:1910#include "chrome/browser/about_flags.h"
[email protected]e2ddbc92010-10-15 20:02:0711#include "chrome/common/chrome_switches.h"
12#include "chrome/common/pref_names.h"
[email protected]a314ee5a2010-10-26 21:23:2813#include "grit/chromium_strings.h"
[email protected]e2ddbc92010-10-15 20:02:0714#include "testing/gtest/include/gtest/gtest.h"
15
[email protected]a314ee5a2010-10-26 21:23:2816const char kFlags1[] = "flag1";
17const char kFlags2[] = "flag2";
18const char kFlags3[] = "flag3";
[email protected]8a6ff28d2010-12-02 16:35:1919const char kFlags4[] = "flag4";
[email protected]e2ddbc92010-10-15 20:02:0720
[email protected]a314ee5a2010-10-26 21:23:2821const char kSwitch1[] = "switch";
22const char kSwitch2[] = "switch2";
23const char kSwitch3[] = "switch3";
[email protected]a82744532011-02-11 16:15:5324const char kValueForSwitch2[] = "value_for_switch2";
[email protected]e2ddbc92010-10-15 20:02:0725
[email protected]8a6ff28d2010-12-02 16:35:1926const char kMultiSwitch1[] = "multi_switch1";
27const char kMultiSwitch2[] = "multi_switch2";
[email protected]a82744532011-02-11 16:15:5328const char kValueForMultiSwitch2[] = "value_for_multi_switch2";
[email protected]8a6ff28d2010-12-02 16:35:1929
[email protected]e2ddbc92010-10-15 20:02:0730namespace about_flags {
31
[email protected]8a6ff28d2010-12-02 16:35:1932const Experiment::Choice kMultiChoices[] = {
[email protected]a82744532011-02-11 16:15:5333 { IDS_PRODUCT_NAME, "", "" },
34 { IDS_PRODUCT_NAME, kMultiSwitch1, "" },
35 { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 },
[email protected]8a6ff28d2010-12-02 16:35:1936};
37
38// The experiments that are set for these tests. The 3rd experiment is not
39// supported on the current platform, all others are.
[email protected]a314ee5a2010-10-26 21:23:2840static Experiment kExperiments[] = {
41 {
42 kFlags1,
43 IDS_PRODUCT_NAME,
44 IDS_PRODUCT_NAME,
45 0, // Ends up being mapped to the current platform.
[email protected]8a6ff28d2010-12-02 16:35:1946 Experiment::SINGLE_VALUE,
47 kSwitch1,
[email protected]a82744532011-02-11 16:15:5348 "",
[email protected]8a6ff28d2010-12-02 16:35:1949 NULL,
50 0
[email protected]a314ee5a2010-10-26 21:23:2851 },
52 {
53 kFlags2,
54 IDS_PRODUCT_NAME,
55 IDS_PRODUCT_NAME,
56 0, // Ends up being mapped to the current platform.
[email protected]8a6ff28d2010-12-02 16:35:1957 Experiment::SINGLE_VALUE,
58 kSwitch2,
[email protected]a82744532011-02-11 16:15:5359 kValueForSwitch2,
[email protected]8a6ff28d2010-12-02 16:35:1960 NULL,
61 0
[email protected]a314ee5a2010-10-26 21:23:2862 },
63 {
64 kFlags3,
65 IDS_PRODUCT_NAME,
66 IDS_PRODUCT_NAME,
67 0, // This ends up enabling for an OS other than the current.
[email protected]8a6ff28d2010-12-02 16:35:1968 Experiment::SINGLE_VALUE,
69 kSwitch3,
[email protected]a82744532011-02-11 16:15:5370 "",
[email protected]8a6ff28d2010-12-02 16:35:1971 NULL,
72 0
73 },
74 {
75 kFlags4,
76 IDS_PRODUCT_NAME,
77 IDS_PRODUCT_NAME,
78 0, // Ends up being mapped to the current platform.
79 Experiment::MULTI_VALUE,
80 "",
[email protected]a82744532011-02-11 16:15:5381 "",
[email protected]8a6ff28d2010-12-02 16:35:1982 kMultiChoices,
83 arraysize(kMultiChoices)
[email protected]a314ee5a2010-10-26 21:23:2884 },
85};
86
[email protected]e2ddbc92010-10-15 20:02:0787class AboutFlagsTest : public ::testing::Test {
88 protected:
89 AboutFlagsTest() {
[email protected]b1de2c72013-02-06 02:45:4790 prefs_.registry()->RegisterListPref(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:0791 testing::ClearState();
92 }
93
[email protected]a314ee5a2010-10-26 21:23:2894 virtual void SetUp() {
95 for (size_t i = 0; i < arraysize(kExperiments); ++i)
96 kExperiments[i].supported_platforms = GetCurrentPlatform();
97
98 int os_other_than_current = 1;
99 while (os_other_than_current == GetCurrentPlatform())
100 os_other_than_current <<= 1;
101 kExperiments[2].supported_platforms = os_other_than_current;
102
103 testing::SetExperiments(kExperiments, arraysize(kExperiments));
104 }
105
106 virtual void TearDown() {
107 testing::SetExperiments(NULL, 0);
108 }
109
[email protected]5b199522012-12-22 17:24:44110 TestingPrefServiceSimple prefs_;
[email protected]e2ddbc92010-10-15 20:02:07111};
112
113TEST_F(AboutFlagsTest, ChangeNeedsRestart) {
[email protected]e2ddbc92010-10-15 20:02:07114 EXPECT_FALSE(IsRestartNeededToCommitChanges());
115 SetExperimentEnabled(&prefs_, kFlags1, true);
116 EXPECT_TRUE(IsRestartNeededToCommitChanges());
117}
118
119TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) {
[email protected]e2ddbc92010-10-15 20:02:07120 // Add two experiments, check they're there.
121 SetExperimentEnabled(&prefs_, kFlags1, true);
122 SetExperimentEnabled(&prefs_, kFlags2, true);
123
[email protected]1bc78422011-03-31 08:41:38124 const ListValue* experiments_list = prefs_.GetList(
[email protected]e2ddbc92010-10-15 20:02:07125 prefs::kEnabledLabsExperiments);
126 ASSERT_TRUE(experiments_list != NULL);
127
128 ASSERT_EQ(2u, experiments_list->GetSize());
129
130 std::string s0;
131 ASSERT_TRUE(experiments_list->GetString(0, &s0));
132 std::string s1;
133 ASSERT_TRUE(experiments_list->GetString(1, &s1));
134
135 EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1);
136 EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2);
137
138 // Remove one experiment, check the other's still around.
139 SetExperimentEnabled(&prefs_, kFlags2, false);
140
[email protected]1bc78422011-03-31 08:41:38141 experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:07142 ASSERT_TRUE(experiments_list != NULL);
143 ASSERT_EQ(1u, experiments_list->GetSize());
144 ASSERT_TRUE(experiments_list->GetString(0, &s0));
145 EXPECT_TRUE(s0 == kFlags1);
146}
147
148TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) {
[email protected]e2ddbc92010-10-15 20:02:07149 // Add two experiments, check the pref exists.
150 SetExperimentEnabled(&prefs_, kFlags1, true);
151 SetExperimentEnabled(&prefs_, kFlags2, true);
[email protected]1bc78422011-03-31 08:41:38152 const ListValue* experiments_list = prefs_.GetList(
[email protected]e2ddbc92010-10-15 20:02:07153 prefs::kEnabledLabsExperiments);
154 ASSERT_TRUE(experiments_list != NULL);
155
156 // Remove both, the pref should have been removed completely.
157 SetExperimentEnabled(&prefs_, kFlags1, false);
158 SetExperimentEnabled(&prefs_, kFlags2, false);
[email protected]1bc78422011-03-31 08:41:38159 experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:07160 EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0);
161}
162
163TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) {
[email protected]e2ddbc92010-10-15 20:02:07164 SetExperimentEnabled(&prefs_, kFlags1, true);
165
[email protected]947446b2010-10-21 03:36:31166 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]e2ddbc92010-10-15 20:02:07167 command_line.AppendSwitch("foo");
168
169 EXPECT_TRUE(command_line.HasSwitch("foo"));
[email protected]a314ee5a2010-10-26 21:23:28170 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
[email protected]e2ddbc92010-10-15 20:02:07171
172 ConvertFlagsToSwitches(&prefs_, &command_line);
173
174 EXPECT_TRUE(command_line.HasSwitch("foo"));
[email protected]cd7fa99f2011-09-07 01:24:55175 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]e2ddbc92010-10-15 20:02:07176}
177
178TEST_F(AboutFlagsTest, RemoveFlagSwitches) {
[email protected]e2ddbc92010-10-15 20:02:07179 std::map<std::string, CommandLine::StringType> switch_list;
[email protected]a314ee5a2010-10-26 21:23:28180 switch_list[kSwitch1] = CommandLine::StringType();
[email protected]e2ddbc92010-10-15 20:02:07181 switch_list[switches::kFlagSwitchesBegin] = CommandLine::StringType();
182 switch_list[switches::kFlagSwitchesEnd] = CommandLine::StringType();
183 switch_list["foo"] = CommandLine::StringType();
184
185 SetExperimentEnabled(&prefs_, kFlags1, true);
186
187 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called.
188 RemoveFlagsSwitches(&switch_list);
189 ASSERT_EQ(4u, switch_list.size());
[email protected]a314ee5a2010-10-26 21:23:28190 EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end());
[email protected]e2ddbc92010-10-15 20:02:07191 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) !=
192 switch_list.end());
193 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) !=
194 switch_list.end());
195 EXPECT_TRUE(switch_list.find("foo") != switch_list.end());
196
197 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again.
[email protected]947446b2010-10-21 03:36:31198 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]e2ddbc92010-10-15 20:02:07199 command_line.AppendSwitch("foo");
200 ConvertFlagsToSwitches(&prefs_, &command_line);
201 RemoveFlagsSwitches(&switch_list);
202
203 // Now the about:flags-related switch should have been removed.
204 ASSERT_EQ(1u, switch_list.size());
205 EXPECT_TRUE(switch_list.find("foo") != switch_list.end());
206}
207
[email protected]a314ee5a2010-10-26 21:23:28208// Tests enabling experiments that aren't supported on the current platform.
209TEST_F(AboutFlagsTest, PersistAndPrune) {
[email protected]8a6ff28d2010-12-02 16:35:19210 // Enable experiments 1 and 3.
[email protected]a314ee5a2010-10-26 21:23:28211 SetExperimentEnabled(&prefs_, kFlags1, true);
212 SetExperimentEnabled(&prefs_, kFlags3, true);
213 CommandLine command_line(CommandLine::NO_PROGRAM);
214 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
215 EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
216
217 // Convert the flags to switches. Experiment 3 shouldn't be among the switches
218 // as it is not applicable to the current platform.
219 ConvertFlagsToSwitches(&prefs_, &command_line);
[email protected]cd7fa99f2011-09-07 01:24:55220 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]a314ee5a2010-10-26 21:23:28221 EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
222
223 // Experiment 3 should show still be persisted in preferences though.
224 scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_));
225 ASSERT_TRUE(switch_prefs.get());
[email protected]cc3e2052011-12-20 01:01:40226 EXPECT_EQ(arraysize(kExperiments), switch_prefs->GetSize());
[email protected]8a6ff28d2010-12-02 16:35:19227}
228
[email protected]a82744532011-02-11 16:15:53229// Tests that switches which should have values get them in the command
230// line.
231TEST_F(AboutFlagsTest, CheckValues) {
232 // Enable experiments 1 and 2.
233 SetExperimentEnabled(&prefs_, kFlags1, true);
234 SetExperimentEnabled(&prefs_, kFlags2, true);
235 CommandLine command_line(CommandLine::NO_PROGRAM);
236 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
237 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
238
239 // Convert the flags to switches.
240 ConvertFlagsToSwitches(&prefs_, &command_line);
[email protected]cd7fa99f2011-09-07 01:24:55241 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]61a4c6f2011-07-20 04:54:52242 EXPECT_EQ(std::string(""), command_line.GetSwitchValueASCII(kSwitch1));
[email protected]cd7fa99f2011-09-07 01:24:55243 EXPECT_TRUE(command_line.HasSwitch(kSwitch2));
244 EXPECT_EQ(std::string(kValueForSwitch2),
[email protected]a82744532011-02-11 16:15:53245 command_line.GetSwitchValueASCII(kSwitch2));
246
247 // Confirm that there is no '=' in the command line for simple switches.
248 std::string switch1_with_equals = std::string("--") +
249 std::string(kSwitch1) +
250 std::string("=");
251#if defined(OS_WIN)
252 EXPECT_EQ(std::wstring::npos,
[email protected]61a4c6f2011-07-20 04:54:52253 command_line.GetCommandLineString().find(
[email protected]a82744532011-02-11 16:15:53254 ASCIIToWide(switch1_with_equals)));
255#else
256 EXPECT_EQ(std::string::npos,
[email protected]61a4c6f2011-07-20 04:54:52257 command_line.GetCommandLineString().find(switch1_with_equals));
[email protected]a82744532011-02-11 16:15:53258#endif
259
260 // And confirm there is a '=' for switches with values.
261 std::string switch2_with_equals = std::string("--") +
262 std::string(kSwitch2) +
263 std::string("=");
[email protected]5b199522012-12-22 17:24:44264#if defined(OS_WIN)
[email protected]cd7fa99f2011-09-07 01:24:55265 EXPECT_NE(std::wstring::npos,
[email protected]61a4c6f2011-07-20 04:54:52266 command_line.GetCommandLineString().find(
[email protected]a82744532011-02-11 16:15:53267 ASCIIToWide(switch2_with_equals)));
268#else
[email protected]cd7fa99f2011-09-07 01:24:55269 EXPECT_NE(std::string::npos,
[email protected]61a4c6f2011-07-20 04:54:52270 command_line.GetCommandLineString().find(switch2_with_equals));
[email protected]a82744532011-02-11 16:15:53271#endif
272
273 // And it should persist
274 scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_));
275 ASSERT_TRUE(switch_prefs.get());
[email protected]cc3e2052011-12-20 01:01:40276 EXPECT_EQ(arraysize(kExperiments), switch_prefs->GetSize());
[email protected]a82744532011-02-11 16:15:53277}
278
[email protected]28e35af2011-02-09 12:56:22279// Tests multi-value type experiments.
[email protected]8a6ff28d2010-12-02 16:35:19280TEST_F(AboutFlagsTest, MultiValues) {
[email protected]28e35af2011-02-09 12:56:22281 // Initially, the first "deactivated" option of the multi experiment should
282 // be set.
[email protected]8a6ff28d2010-12-02 16:35:19283 {
284 CommandLine command_line(CommandLine::NO_PROGRAM);
285 ConvertFlagsToSwitches(&prefs_, &command_line);
[email protected]28e35af2011-02-09 12:56:22286 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
[email protected]8a6ff28d2010-12-02 16:35:19287 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
288 }
289
[email protected]28e35af2011-02-09 12:56:22290 // Enable the 2nd choice of the multi-value.
[email protected]8a6ff28d2010-12-02 16:35:19291 SetExperimentEnabled(&prefs_, std::string(kFlags4) +
292 std::string(testing::kMultiSeparator) +
[email protected]28e35af2011-02-09 12:56:22293 base::IntToString(2), true);
[email protected]8a6ff28d2010-12-02 16:35:19294 {
295 CommandLine command_line(CommandLine::NO_PROGRAM);
296 ConvertFlagsToSwitches(&prefs_, &command_line);
297 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
[email protected]cd7fa99f2011-09-07 01:24:55298 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2));
299 EXPECT_EQ(std::string(kValueForMultiSwitch2),
[email protected]a82744532011-02-11 16:15:53300 command_line.GetSwitchValueASCII(kMultiSwitch2));
[email protected]8a6ff28d2010-12-02 16:35:19301 }
302
303 // Disable the multi-value experiment.
[email protected]28e35af2011-02-09 12:56:22304 SetExperimentEnabled(&prefs_, std::string(kFlags4) +
305 std::string(testing::kMultiSeparator) +
306 base::IntToString(0), true);
[email protected]8a6ff28d2010-12-02 16:35:19307 {
308 CommandLine command_line(CommandLine::NO_PROGRAM);
309 ConvertFlagsToSwitches(&prefs_, &command_line);
310 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
311 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
312 }
313}
314
315// Makes sure there are no separators in any of the experiment names.
316TEST_F(AboutFlagsTest, NoSeparators) {
317 testing::SetExperiments(NULL, 0);
318 size_t count;
319 const Experiment* experiments = testing::GetExperiments(&count);
320 for (size_t i = 0; i < count; ++i) {
321 std::string name = experiments->internal_name;
322 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i;
323 }
[email protected]a314ee5a2010-10-26 21:23:28324}
325
[email protected]e2ddbc92010-10-15 20:02:07326} // namespace about_flags