[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/about_flags.h" |
| 6 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 7 | #include "base/values.h" |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 8 | #include "chrome/common/chrome_switches.h" |
| 9 | #include "chrome/common/pref_names.h" |
| 10 | #include "chrome/test/testing_pref_service.h" |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 11 | #include "grit/chromium_strings.h" |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 14 | const char kFlags1[] = "flag1"; |
| 15 | const char kFlags2[] = "flag2"; |
| 16 | const char kFlags3[] = "flag3"; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 17 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 18 | const char kSwitch1[] = "switch"; |
| 19 | const char kSwitch2[] = "switch2"; |
| 20 | const char kSwitch3[] = "switch3"; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 21 | |
| 22 | namespace about_flags { |
| 23 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 24 | // The experiments that are set for these tests. The first two experiments are |
| 25 | // supported on the current platform, but the last is only supported on a |
| 26 | // platform other than the current. |
| 27 | static Experiment kExperiments[] = { |
| 28 | { |
| 29 | kFlags1, |
| 30 | IDS_PRODUCT_NAME, |
| 31 | IDS_PRODUCT_NAME, |
| 32 | 0, // Ends up being mapped to the current platform. |
| 33 | kSwitch1 |
| 34 | }, |
| 35 | { |
| 36 | kFlags2, |
| 37 | IDS_PRODUCT_NAME, |
| 38 | IDS_PRODUCT_NAME, |
| 39 | 0, // Ends up being mapped to the current platform. |
| 40 | kSwitch2 |
| 41 | }, |
| 42 | { |
| 43 | kFlags3, |
| 44 | IDS_PRODUCT_NAME, |
| 45 | IDS_PRODUCT_NAME, |
| 46 | 0, // This ends up enabling for an OS other than the current. |
| 47 | kSwitch3 |
| 48 | }, |
| 49 | }; |
| 50 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 51 | class AboutFlagsTest : public ::testing::Test { |
| 52 | protected: |
| 53 | AboutFlagsTest() { |
| 54 | prefs_.RegisterListPref(prefs::kEnabledLabsExperiments); |
| 55 | testing::ClearState(); |
| 56 | } |
| 57 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 58 | virtual void SetUp() { |
| 59 | for (size_t i = 0; i < arraysize(kExperiments); ++i) |
| 60 | kExperiments[i].supported_platforms = GetCurrentPlatform(); |
| 61 | |
| 62 | int os_other_than_current = 1; |
| 63 | while (os_other_than_current == GetCurrentPlatform()) |
| 64 | os_other_than_current <<= 1; |
| 65 | kExperiments[2].supported_platforms = os_other_than_current; |
| 66 | |
| 67 | testing::SetExperiments(kExperiments, arraysize(kExperiments)); |
| 68 | } |
| 69 | |
| 70 | virtual void TearDown() { |
| 71 | testing::SetExperiments(NULL, 0); |
| 72 | } |
| 73 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 74 | TestingPrefService prefs_; |
| 75 | }; |
| 76 | |
| 77 | TEST_F(AboutFlagsTest, ChangeNeedsRestart) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 78 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
| 79 | SetExperimentEnabled(&prefs_, kFlags1, true); |
| 80 | EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 81 | } |
| 82 | |
| 83 | TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 84 | // Add two experiments, check they're there. |
| 85 | SetExperimentEnabled(&prefs_, kFlags1, true); |
| 86 | SetExperimentEnabled(&prefs_, kFlags2, true); |
| 87 | |
| 88 | ListValue* experiments_list = prefs_.GetMutableList( |
| 89 | prefs::kEnabledLabsExperiments); |
| 90 | ASSERT_TRUE(experiments_list != NULL); |
| 91 | |
| 92 | ASSERT_EQ(2u, experiments_list->GetSize()); |
| 93 | |
| 94 | std::string s0; |
| 95 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 96 | std::string s1; |
| 97 | ASSERT_TRUE(experiments_list->GetString(1, &s1)); |
| 98 | |
| 99 | EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1); |
| 100 | EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2); |
| 101 | |
| 102 | // Remove one experiment, check the other's still around. |
| 103 | SetExperimentEnabled(&prefs_, kFlags2, false); |
| 104 | |
| 105 | experiments_list = prefs_.GetMutableList(prefs::kEnabledLabsExperiments); |
| 106 | ASSERT_TRUE(experiments_list != NULL); |
| 107 | ASSERT_EQ(1u, experiments_list->GetSize()); |
| 108 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 109 | EXPECT_TRUE(s0 == kFlags1); |
| 110 | } |
| 111 | |
| 112 | TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 113 | // Add two experiments, check the pref exists. |
| 114 | SetExperimentEnabled(&prefs_, kFlags1, true); |
| 115 | SetExperimentEnabled(&prefs_, kFlags2, true); |
| 116 | ListValue* experiments_list = prefs_.GetMutableList( |
| 117 | prefs::kEnabledLabsExperiments); |
| 118 | ASSERT_TRUE(experiments_list != NULL); |
| 119 | |
| 120 | // Remove both, the pref should have been removed completely. |
| 121 | SetExperimentEnabled(&prefs_, kFlags1, false); |
| 122 | SetExperimentEnabled(&prefs_, kFlags2, false); |
| 123 | experiments_list = prefs_.GetMutableList(prefs::kEnabledLabsExperiments); |
| 124 | EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0); |
| 125 | } |
| 126 | |
| 127 | TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 128 | SetExperimentEnabled(&prefs_, kFlags1, true); |
| 129 | |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 130 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 131 | command_line.AppendSwitch("foo"); |
| 132 | |
| 133 | EXPECT_TRUE(command_line.HasSwitch("foo")); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 134 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 135 | |
| 136 | ConvertFlagsToSwitches(&prefs_, &command_line); |
| 137 | |
| 138 | EXPECT_TRUE(command_line.HasSwitch("foo")); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 139 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TEST_F(AboutFlagsTest, RemoveFlagSwitches) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 143 | std::map<std::string, CommandLine::StringType> switch_list; |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 144 | switch_list[kSwitch1] = CommandLine::StringType(); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 145 | switch_list[switches::kFlagSwitchesBegin] = CommandLine::StringType(); |
| 146 | switch_list[switches::kFlagSwitchesEnd] = CommandLine::StringType(); |
| 147 | switch_list["foo"] = CommandLine::StringType(); |
| 148 | |
| 149 | SetExperimentEnabled(&prefs_, kFlags1, true); |
| 150 | |
| 151 | // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called. |
| 152 | RemoveFlagsSwitches(&switch_list); |
| 153 | ASSERT_EQ(4u, switch_list.size()); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 154 | EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end()); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 155 | EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) != |
| 156 | switch_list.end()); |
| 157 | EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) != |
| 158 | switch_list.end()); |
| 159 | EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); |
| 160 | |
| 161 | // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again. |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 162 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 163 | command_line.AppendSwitch("foo"); |
| 164 | ConvertFlagsToSwitches(&prefs_, &command_line); |
| 165 | RemoveFlagsSwitches(&switch_list); |
| 166 | |
| 167 | // Now the about:flags-related switch should have been removed. |
| 168 | ASSERT_EQ(1u, switch_list.size()); |
| 169 | EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); |
| 170 | } |
| 171 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame^] | 172 | // Tests enabling experiments that aren't supported on the current platform. |
| 173 | TEST_F(AboutFlagsTest, PersistAndPrune) { |
| 174 | // Enable exerpiement 1 and experiment 3. |
| 175 | SetExperimentEnabled(&prefs_, kFlags1, true); |
| 176 | SetExperimentEnabled(&prefs_, kFlags3, true); |
| 177 | CommandLine command_line(CommandLine::NO_PROGRAM); |
| 178 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 179 | EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
| 180 | |
| 181 | // Convert the flags to switches. Experiment 3 shouldn't be among the switches |
| 182 | // as it is not applicable to the current platform. |
| 183 | ConvertFlagsToSwitches(&prefs_, &command_line); |
| 184 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
| 185 | EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
| 186 | |
| 187 | // Experiment 3 should show still be persisted in preferences though. |
| 188 | scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_)); |
| 189 | ASSERT_TRUE(switch_prefs.get()); |
| 190 | EXPECT_EQ(2u, switch_prefs->GetSize()); |
| 191 | } |
| 192 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 193 | } // namespace about_flags |