| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/string_number_conversions.h" | 5 #include "base/string_number_conversions.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chrome/browser/about_flags.h" | 7 #include "chrome/browser/about_flags.h" |
| 8 #include "chrome/common/chrome_switches.h" | 8 #include "chrome/common/chrome_switches.h" |
| 9 #include "chrome/common/pref_names.h" | 9 #include "chrome/common/pref_names.h" |
| 10 #include "chrome/test/testing_pref_service.h" | 10 #include "chrome/test/testing_pref_service.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 0, // Ends up being mapped to the current platform. | 70 0, // Ends up being mapped to the current platform. |
| 71 Experiment::MULTI_VALUE, | 71 Experiment::MULTI_VALUE, |
| 72 "", | 72 "", |
| 73 kMultiChoices, | 73 kMultiChoices, |
| 74 arraysize(kMultiChoices) | 74 arraysize(kMultiChoices) |
| 75 }, | 75 }, |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 class AboutFlagsTest : public ::testing::Test { | 78 class AboutFlagsTest : public ::testing::Test { |
| 79 protected: | 79 protected: |
| 80 AboutFlagsTest() { | 80 AboutFlagsTest() |
| 81 prefs_.RegisterListPref(prefs::kEnabledLabsExperiments); | 81 : prefs_(TestingPrefService::CreateTestingPrefService()) { |
| 82 prefs_->RegisterListPref(prefs::kEnabledLabsExperiments); |
| 82 #if defined(OS_CHROMEOS) | 83 #if defined(OS_CHROMEOS) |
| 83 prefs_.RegisterBooleanPref(prefs::kLabsMediaplayerEnabled, false); | 84 prefs_->RegisterBooleanPref(prefs::kLabsMediaplayerEnabled, false); |
| 84 prefs_.RegisterBooleanPref(prefs::kLabsAdvancedFilesystemEnabled, false); | 85 prefs_->RegisterBooleanPref(prefs::kLabsAdvancedFilesystemEnabled, false); |
| 85 prefs_.RegisterBooleanPref(prefs::kUseVerticalTabs, false); | 86 prefs_->RegisterBooleanPref(prefs::kUseVerticalTabs, false); |
| 86 #endif | 87 #endif |
| 87 testing::ClearState(); | 88 testing::ClearState(); |
| 88 } | 89 } |
| 89 | 90 |
| 90 virtual void SetUp() { | 91 virtual void SetUp() { |
| 91 for (size_t i = 0; i < arraysize(kExperiments); ++i) | 92 for (size_t i = 0; i < arraysize(kExperiments); ++i) |
| 92 kExperiments[i].supported_platforms = GetCurrentPlatform(); | 93 kExperiments[i].supported_platforms = GetCurrentPlatform(); |
| 93 | 94 |
| 94 int os_other_than_current = 1; | 95 int os_other_than_current = 1; |
| 95 while (os_other_than_current == GetCurrentPlatform()) | 96 while (os_other_than_current == GetCurrentPlatform()) |
| 96 os_other_than_current <<= 1; | 97 os_other_than_current <<= 1; |
| 97 kExperiments[2].supported_platforms = os_other_than_current; | 98 kExperiments[2].supported_platforms = os_other_than_current; |
| 98 | 99 |
| 99 testing::SetExperiments(kExperiments, arraysize(kExperiments)); | 100 testing::SetExperiments(kExperiments, arraysize(kExperiments)); |
| 100 } | 101 } |
| 101 | 102 |
| 102 virtual void TearDown() { | 103 virtual void TearDown() { |
| 103 testing::SetExperiments(NULL, 0); | 104 testing::SetExperiments(NULL, 0); |
| 104 } | 105 } |
| 105 | 106 |
| 106 TestingPrefService prefs_; | 107 scoped_ptr<TestingPrefService> prefs_; |
| 107 }; | 108 }; |
| 108 | 109 |
| 109 TEST_F(AboutFlagsTest, ChangeNeedsRestart) { | 110 TEST_F(AboutFlagsTest, ChangeNeedsRestart) { |
| 110 EXPECT_FALSE(IsRestartNeededToCommitChanges()); | 111 EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
| 111 SetExperimentEnabled(&prefs_, kFlags1, true); | 112 SetExperimentEnabled(prefs_.get(), kFlags1, true); |
| 112 EXPECT_TRUE(IsRestartNeededToCommitChanges()); | 113 EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 113 } | 114 } |
| 114 | 115 |
| 115 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) { | 116 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) { |
| 116 // Add two experiments, check they're there. | 117 // Add two experiments, check they're there. |
| 117 SetExperimentEnabled(&prefs_, kFlags1, true); | 118 SetExperimentEnabled(prefs_.get(), kFlags1, true); |
| 118 SetExperimentEnabled(&prefs_, kFlags2, true); | 119 SetExperimentEnabled(prefs_.get(), kFlags2, true); |
| 119 | 120 |
| 120 ListValue* experiments_list = prefs_.GetMutableList( | 121 ListValue* experiments_list = prefs_->GetMutableList( |
| 121 prefs::kEnabledLabsExperiments); | 122 prefs::kEnabledLabsExperiments); |
| 122 ASSERT_TRUE(experiments_list != NULL); | 123 ASSERT_TRUE(experiments_list != NULL); |
| 123 | 124 |
| 124 ASSERT_EQ(2u, experiments_list->GetSize()); | 125 ASSERT_EQ(2u, experiments_list->GetSize()); |
| 125 | 126 |
| 126 std::string s0; | 127 std::string s0; |
| 127 ASSERT_TRUE(experiments_list->GetString(0, &s0)); | 128 ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 128 std::string s1; | 129 std::string s1; |
| 129 ASSERT_TRUE(experiments_list->GetString(1, &s1)); | 130 ASSERT_TRUE(experiments_list->GetString(1, &s1)); |
| 130 | 131 |
| 131 EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1); | 132 EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1); |
| 132 EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2); | 133 EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2); |
| 133 | 134 |
| 134 // Remove one experiment, check the other's still around. | 135 // Remove one experiment, check the other's still around. |
| 135 SetExperimentEnabled(&prefs_, kFlags2, false); | 136 SetExperimentEnabled(prefs_.get(), kFlags2, false); |
| 136 | 137 |
| 137 experiments_list = prefs_.GetMutableList(prefs::kEnabledLabsExperiments); | 138 experiments_list = prefs_->GetMutableList(prefs::kEnabledLabsExperiments); |
| 138 ASSERT_TRUE(experiments_list != NULL); | 139 ASSERT_TRUE(experiments_list != NULL); |
| 139 ASSERT_EQ(1u, experiments_list->GetSize()); | 140 ASSERT_EQ(1u, experiments_list->GetSize()); |
| 140 ASSERT_TRUE(experiments_list->GetString(0, &s0)); | 141 ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 141 EXPECT_TRUE(s0 == kFlags1); | 142 EXPECT_TRUE(s0 == kFlags1); |
| 142 } | 143 } |
| 143 | 144 |
| 144 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) { | 145 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) { |
| 145 // Add two experiments, check the pref exists. | 146 // Add two experiments, check the pref exists. |
| 146 SetExperimentEnabled(&prefs_, kFlags1, true); | 147 SetExperimentEnabled(prefs_.get(), kFlags1, true); |
| 147 SetExperimentEnabled(&prefs_, kFlags2, true); | 148 SetExperimentEnabled(prefs_.get(), kFlags2, true); |
| 148 ListValue* experiments_list = prefs_.GetMutableList( | 149 ListValue* experiments_list = prefs_->GetMutableList( |
| 149 prefs::kEnabledLabsExperiments); | 150 prefs::kEnabledLabsExperiments); |
| 150 ASSERT_TRUE(experiments_list != NULL); | 151 ASSERT_TRUE(experiments_list != NULL); |
| 151 | 152 |
| 152 // Remove both, the pref should have been removed completely. | 153 // Remove both, the pref should have been removed completely. |
| 153 SetExperimentEnabled(&prefs_, kFlags1, false); | 154 SetExperimentEnabled(prefs_.get(), kFlags1, false); |
| 154 SetExperimentEnabled(&prefs_, kFlags2, false); | 155 SetExperimentEnabled(prefs_.get(), kFlags2, false); |
| 155 experiments_list = prefs_.GetMutableList(prefs::kEnabledLabsExperiments); | 156 experiments_list = prefs_->GetMutableList(prefs::kEnabledLabsExperiments); |
| 156 EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0); | 157 EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0); |
| 157 } | 158 } |
| 158 | 159 |
| 159 TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) { | 160 TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) { |
| 160 SetExperimentEnabled(&prefs_, kFlags1, true); | 161 SetExperimentEnabled(prefs_.get(), kFlags1, true); |
| 161 | 162 |
| 162 CommandLine command_line(CommandLine::NO_PROGRAM); | 163 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 163 command_line.AppendSwitch("foo"); | 164 command_line.AppendSwitch("foo"); |
| 164 | 165 |
| 165 EXPECT_TRUE(command_line.HasSwitch("foo")); | 166 EXPECT_TRUE(command_line.HasSwitch("foo")); |
| 166 EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); | 167 EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 167 | 168 |
| 168 ConvertFlagsToSwitches(&prefs_, &command_line); | 169 ConvertFlagsToSwitches(prefs_.get(), &command_line); |
| 169 | 170 |
| 170 EXPECT_TRUE(command_line.HasSwitch("foo")); | 171 EXPECT_TRUE(command_line.HasSwitch("foo")); |
| 171 EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); | 172 EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
| 172 } | 173 } |
| 173 | 174 |
| 174 TEST_F(AboutFlagsTest, RemoveFlagSwitches) { | 175 TEST_F(AboutFlagsTest, RemoveFlagSwitches) { |
| 175 std::map<std::string, CommandLine::StringType> switch_list; | 176 std::map<std::string, CommandLine::StringType> switch_list; |
| 176 switch_list[kSwitch1] = CommandLine::StringType(); | 177 switch_list[kSwitch1] = CommandLine::StringType(); |
| 177 switch_list[switches::kFlagSwitchesBegin] = CommandLine::StringType(); | 178 switch_list[switches::kFlagSwitchesBegin] = CommandLine::StringType(); |
| 178 switch_list[switches::kFlagSwitchesEnd] = CommandLine::StringType(); | 179 switch_list[switches::kFlagSwitchesEnd] = CommandLine::StringType(); |
| 179 switch_list["foo"] = CommandLine::StringType(); | 180 switch_list["foo"] = CommandLine::StringType(); |
| 180 | 181 |
| 181 SetExperimentEnabled(&prefs_, kFlags1, true); | 182 SetExperimentEnabled(prefs_.get(), kFlags1, true); |
| 182 | 183 |
| 183 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called. | 184 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called. |
| 184 RemoveFlagsSwitches(&switch_list); | 185 RemoveFlagsSwitches(&switch_list); |
| 185 ASSERT_EQ(4u, switch_list.size()); | 186 ASSERT_EQ(4u, switch_list.size()); |
| 186 EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end()); | 187 EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end()); |
| 187 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) != | 188 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) != |
| 188 switch_list.end()); | 189 switch_list.end()); |
| 189 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) != | 190 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) != |
| 190 switch_list.end()); | 191 switch_list.end()); |
| 191 EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); | 192 EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); |
| 192 | 193 |
| 193 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again. | 194 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again. |
| 194 CommandLine command_line(CommandLine::NO_PROGRAM); | 195 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 195 command_line.AppendSwitch("foo"); | 196 command_line.AppendSwitch("foo"); |
| 196 ConvertFlagsToSwitches(&prefs_, &command_line); | 197 ConvertFlagsToSwitches(prefs_.get(), &command_line); |
| 197 RemoveFlagsSwitches(&switch_list); | 198 RemoveFlagsSwitches(&switch_list); |
| 198 | 199 |
| 199 // Now the about:flags-related switch should have been removed. | 200 // Now the about:flags-related switch should have been removed. |
| 200 ASSERT_EQ(1u, switch_list.size()); | 201 ASSERT_EQ(1u, switch_list.size()); |
| 201 EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); | 202 EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); |
| 202 } | 203 } |
| 203 | 204 |
| 204 // Tests enabling experiments that aren't supported on the current platform. | 205 // Tests enabling experiments that aren't supported on the current platform. |
| 205 TEST_F(AboutFlagsTest, PersistAndPrune) { | 206 TEST_F(AboutFlagsTest, PersistAndPrune) { |
| 206 // Enable experiments 1 and 3. | 207 // Enable experiments 1 and 3. |
| 207 SetExperimentEnabled(&prefs_, kFlags1, true); | 208 SetExperimentEnabled(prefs_.get(), kFlags1, true); |
| 208 SetExperimentEnabled(&prefs_, kFlags3, true); | 209 SetExperimentEnabled(prefs_.get(), kFlags3, true); |
| 209 CommandLine command_line(CommandLine::NO_PROGRAM); | 210 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 210 EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); | 211 EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 211 EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); | 212 EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
| 212 | 213 |
| 213 // Convert the flags to switches. Experiment 3 shouldn't be among the switches | 214 // Convert the flags to switches. Experiment 3 shouldn't be among the switches |
| 214 // as it is not applicable to the current platform. | 215 // as it is not applicable to the current platform. |
| 215 ConvertFlagsToSwitches(&prefs_, &command_line); | 216 ConvertFlagsToSwitches(prefs_.get(), &command_line); |
| 216 EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); | 217 EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
| 217 EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); | 218 EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
| 218 | 219 |
| 219 // Experiment 3 should show still be persisted in preferences though. | 220 // Experiment 3 should show still be persisted in preferences though. |
| 220 scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_)); | 221 scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(prefs_.get())); |
| 221 ASSERT_TRUE(switch_prefs.get()); | 222 ASSERT_TRUE(switch_prefs.get()); |
| 222 EXPECT_EQ(arraysize(kExperiments) - 1, switch_prefs->GetSize()); | 223 EXPECT_EQ(arraysize(kExperiments) - 1, switch_prefs->GetSize()); |
| 223 } | 224 } |
| 224 | 225 |
| 225 // Tests enabling multi-value type experiments. | 226 // Tests enabling multi-value type experiments. |
| 226 TEST_F(AboutFlagsTest, MultiValues) { | 227 TEST_F(AboutFlagsTest, MultiValues) { |
| 227 // Enable the multi value experiment, which should enable the first choice. | 228 // Enable the multi value experiment, which should enable the first choice. |
| 228 SetExperimentEnabled(&prefs_, kFlags4, true); | 229 SetExperimentEnabled(prefs_.get(), kFlags4, true); |
| 229 { | 230 { |
| 230 CommandLine command_line(CommandLine::NO_PROGRAM); | 231 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 231 ConvertFlagsToSwitches(&prefs_, &command_line); | 232 ConvertFlagsToSwitches(prefs_.get(), &command_line); |
| 232 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch1)); | 233 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch1)); |
| 233 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); | 234 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
| 234 } | 235 } |
| 235 | 236 |
| 236 // Enable the 2nd choice of the multi-value, which should disable the first | 237 // Enable the 2nd choice of the multi-value, which should disable the first |
| 237 // choice. | 238 // choice. |
| 238 SetExperimentEnabled(&prefs_, std::string(kFlags4) + | 239 SetExperimentEnabled(prefs_.get(), std::string(kFlags4) + |
| 239 std::string(testing::kMultiSeparator) + | 240 std::string(testing::kMultiSeparator) + |
| 240 base::IntToString(1), true); | 241 base::IntToString(1), true); |
| 241 { | 242 { |
| 242 CommandLine command_line(CommandLine::NO_PROGRAM); | 243 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 243 ConvertFlagsToSwitches(&prefs_, &command_line); | 244 ConvertFlagsToSwitches(prefs_.get(), &command_line); |
| 244 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); | 245 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
| 245 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2)); | 246 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2)); |
| 246 } | 247 } |
| 247 | 248 |
| 248 // Disable the multi-value experiment. | 249 // Disable the multi-value experiment. |
| 249 SetExperimentEnabled(&prefs_, kFlags4, false); | 250 SetExperimentEnabled(prefs_.get(), kFlags4, false); |
| 250 { | 251 { |
| 251 CommandLine command_line(CommandLine::NO_PROGRAM); | 252 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 252 ConvertFlagsToSwitches(&prefs_, &command_line); | 253 ConvertFlagsToSwitches(prefs_.get(), &command_line); |
| 253 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); | 254 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
| 254 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); | 255 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
| 255 } | 256 } |
| 256 } | 257 } |
| 257 | 258 |
| 258 // Makes sure there are no separators in any of the experiment names. | 259 // Makes sure there are no separators in any of the experiment names. |
| 259 TEST_F(AboutFlagsTest, NoSeparators) { | 260 TEST_F(AboutFlagsTest, NoSeparators) { |
| 260 testing::SetExperiments(NULL, 0); | 261 testing::SetExperiments(NULL, 0); |
| 261 size_t count; | 262 size_t count; |
| 262 const Experiment* experiments = testing::GetExperiments(&count); | 263 const Experiment* experiments = testing::GetExperiments(&count); |
| 263 for (size_t i = 0; i < count; ++i) { | 264 for (size_t i = 0; i < count; ++i) { |
| 264 std::string name = experiments->internal_name; | 265 std::string name = experiments->internal_name; |
| 265 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i; | 266 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i; |
| 266 } | 267 } |
| 267 } | 268 } |
| 268 | 269 |
| 269 } // namespace about_flags | 270 } // namespace about_flags |
| OLD | NEW |