Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: components/flags_ui/flags_state_unittest.cc

Issue 2146903002: Clean-up the code for default variations in chrome://flags. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: iOS test crash fix? #2 Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/flags_ui/flags_state.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/flags_ui/flags_state.h" 5 #include "components/flags_ui/flags_state.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 const char kValueForMultiSwitch2[] = "value_for_multi_switch2"; 55 const char kValueForMultiSwitch2[] = "value_for_multi_switch2";
56 56
57 const char kEnableDisableValue1[] = "value1"; 57 const char kEnableDisableValue1[] = "value1";
58 const char kEnableDisableValue2[] = "value2"; 58 const char kEnableDisableValue2[] = "value2";
59 59
60 const char kEnableFeatures[] = "dummy-enable-features"; 60 const char kEnableFeatures[] = "dummy-enable-features";
61 const char kDisableFeatures[] = "dummy-disable-features"; 61 const char kDisableFeatures[] = "dummy-disable-features";
62 62
63 const char kTestTrial[] = "TestTrial"; 63 const char kTestTrial[] = "TestTrial";
64 const char kTestParam[] = "param"; 64 const char kTestParam[] = "param";
65 const char kTestParamValue1[] = "value1"; 65 const char kTestParamValue[] = "value";
66 const char kTestParamValue2[] = "value2";
67 66
68 const base::Feature kTestFeature1{"FeatureName1", 67 const base::Feature kTestFeature1{"FeatureName1",
69 base::FEATURE_ENABLED_BY_DEFAULT}; 68 base::FEATURE_ENABLED_BY_DEFAULT};
70 const base::Feature kTestFeature2{"FeatureName2", 69 const base::Feature kTestFeature2{"FeatureName2",
71 base::FEATURE_ENABLED_BY_DEFAULT}; 70 base::FEATURE_ENABLED_BY_DEFAULT};
72 71
73 const FeatureEntry::FeatureParam kTestVariationDefault[] = {
74 {kTestParam, kTestParamValue1}};
75
76 const FeatureEntry::FeatureParam kTestVariationOther[] = { 72 const FeatureEntry::FeatureParam kTestVariationOther[] = {
77 {kTestParam, kTestParamValue2}}; 73 {kTestParam, kTestParamValue}};
78 74
79 const FeatureEntry::FeatureVariation kTestVariations[] = { 75 const FeatureEntry::FeatureVariation kTestVariations[] = {
80 {"", kTestVariationDefault, 1},
81 {"dummy description", kTestVariationOther, 1}}; 76 {"dummy description", kTestVariationOther, 1}};
82 77
83 // Those have to be valid ids for the translation system but the value are 78 // Those have to be valid ids for the translation system but the value are
84 // never used, so pick one at random from the current component. 79 // never used, so pick one at random from the current component.
85 const int kDummyNameId = IDS_FLAGS_UI_WARNING_HEADER; 80 const int kDummyNameId = IDS_FLAGS_UI_WARNING_HEADER;
86 const int kDummyDescriptionId = IDS_FLAGS_UI_WARNING_TEXT; 81 const int kDummyDescriptionId = IDS_FLAGS_UI_WARNING_TEXT;
87 82
88 bool SkipFeatureEntry(const FeatureEntry& feature_entry) { 83 bool SkipFeatureEntry(const FeatureEntry& feature_entry) {
89 return false; 84 return false;
90 } 85 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 for (size_t i = 0; i < arraysize(kEntries); ++i) 136 for (size_t i = 0; i < arraysize(kEntries); ++i)
142 kEntries[i].supported_platforms = FlagsState::GetCurrentPlatform(); 137 kEntries[i].supported_platforms = FlagsState::GetCurrentPlatform();
143 138
144 int os_other_than_current = 1; 139 int os_other_than_current = 1;
145 while (os_other_than_current == FlagsState::GetCurrentPlatform()) 140 while (os_other_than_current == FlagsState::GetCurrentPlatform())
146 os_other_than_current <<= 1; 141 os_other_than_current <<= 1;
147 kEntries[2].supported_platforms = os_other_than_current; 142 kEntries[2].supported_platforms = os_other_than_current;
148 flags_state_.reset(new FlagsState(kEntries, arraysize(kEntries))); 143 flags_state_.reset(new FlagsState(kEntries, arraysize(kEntries)));
149 } 144 }
150 145
146 ~FlagsStateTest() override {
147 variations::testing::ClearAllVariationParams();
148 }
149
151 TestingPrefServiceSimple prefs_; 150 TestingPrefServiceSimple prefs_;
152 PrefServiceFlagsStorage flags_storage_; 151 PrefServiceFlagsStorage flags_storage_;
153 std::unique_ptr<FlagsState> flags_state_; 152 std::unique_ptr<FlagsState> flags_state_;
154 base::FieldTrialList trial_list_; 153 base::FieldTrialList trial_list_;
155 }; 154 };
156 155
157 TEST_F(FlagsStateTest, NoChangeNoRestart) { 156 TEST_F(FlagsStateTest, NoChangeNoRestart) {
158 EXPECT_FALSE(flags_state_->IsRestartNeededToCommitChanges()); 157 EXPECT_FALSE(flags_state_->IsRestartNeededToCommitChanges());
159 flags_state_->SetFeatureEntryEnabled(&flags_storage_, kFlags1, false); 158 flags_state_->SetFeatureEntryEnabled(&flags_storage_, kFlags1, false);
160 EXPECT_FALSE(flags_state_->IsRestartNeededToCommitChanges()); 159 EXPECT_FALSE(flags_state_->IsRestartNeededToCommitChanges());
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 kNoSentinels, kEnableFeatures, 260 kNoSentinels, kEnableFeatures,
262 kDisableFeatures); 261 kDisableFeatures);
263 262
264 EXPECT_TRUE(command_line2.HasSwitch(kSwitch1)); 263 EXPECT_TRUE(command_line2.HasSwitch(kSwitch1));
265 EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesBegin)); 264 EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesBegin));
266 EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesEnd)); 265 EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesEnd));
267 } 266 }
268 267
269 TEST_F(FlagsStateTest, RegisterAllFeatureVariationParameters) { 268 TEST_F(FlagsStateTest, RegisterAllFeatureVariationParameters) {
270 const FeatureEntry& entry = kEntries[7]; 269 const FeatureEntry& entry = kEntries[7];
271 // Select the "Disabled" variation. 270 // Select the "Default" variation.
272 flags_state_->SetFeatureEntryEnabled(&flags_storage_, entry.NameForOption(0), 271 flags_state_->SetFeatureEntryEnabled(&flags_storage_, entry.NameForOption(0),
273 true); 272 true);
274 flags_state_->RegisterAllFeatureVariationParameters(&flags_storage_); 273 flags_state_->RegisterAllFeatureVariationParameters(&flags_storage_);
275 // No value should be associated. 274 // No value should be associated.
276 EXPECT_EQ("", variations::GetVariationParamValue(kTestTrial, kTestParam)); 275 EXPECT_EQ("", variations::GetVariationParamValue(kTestTrial, kTestParam));
277 // The trial should not be created. 276 // The trial should not be created.
278 base::FieldTrial* trial = base::FieldTrialList::Find(kTestTrial); 277 base::FieldTrial* trial = base::FieldTrialList::Find(kTestTrial);
279 EXPECT_EQ(nullptr, trial); 278 EXPECT_EQ(nullptr, trial);
280 279
281 // Select the first "Enabled" variation. 280 // Select the default "Enabled" variation.
282 flags_state_->SetFeatureEntryEnabled(&flags_storage_, entry.NameForOption(1), 281 flags_state_->SetFeatureEntryEnabled(&flags_storage_, entry.NameForOption(1),
283 true); 282 true);
284 283
285 flags_state_->RegisterAllFeatureVariationParameters(&flags_storage_); 284 flags_state_->RegisterAllFeatureVariationParameters(&flags_storage_);
286 // The value should be associated. 285 // No value should be associated as this is the default option.
287 EXPECT_EQ(kTestParamValue1, 286 EXPECT_EQ("",
288 variations::GetVariationParamValue(kTestTrial, kTestParam)); 287 variations::GetVariationParamValue(kTestTrial, kTestParam));
289 288
290 // The trial should be created. 289 // The trial should be created.
291 trial = base::FieldTrialList::Find(kTestTrial); 290 trial = base::FieldTrialList::Find(kTestTrial);
292 EXPECT_NE(nullptr, trial); 291 EXPECT_NE(nullptr, trial);
293 // The about:flags group should be selected for the trial. 292 // The about:flags group should be selected for the trial.
294 EXPECT_EQ(internal::kTrialGroupAboutFlags, trial->group_name()); 293 EXPECT_EQ(internal::kTrialGroupAboutFlags, trial->group_name());
295 294
296 // Select the second "Enabled" variation. 295 // Select the only one variation.
297 flags_state_->SetFeatureEntryEnabled(&flags_storage_, entry.NameForOption(2), 296 flags_state_->SetFeatureEntryEnabled(&flags_storage_, entry.NameForOption(2),
298 true); 297 true);
299 flags_state_->RegisterAllFeatureVariationParameters(&flags_storage_); 298 flags_state_->RegisterAllFeatureVariationParameters(&flags_storage_);
300 // Associating for the second time should not change the value. 299 // Associating for the second time should not change the value.
301 EXPECT_EQ(kTestParamValue1, 300 EXPECT_EQ("",
302 variations::GetVariationParamValue(kTestTrial, kTestParam)); 301 variations::GetVariationParamValue(kTestTrial, kTestParam));
303 } 302 }
304 303
304 TEST_F(FlagsStateTest, RegisterAllFeatureVariationParametersNonDefault) {
305 const FeatureEntry& entry = kEntries[7];
306 // Select the only one variation.
307 flags_state_->SetFeatureEntryEnabled(&flags_storage_, entry.NameForOption(2),
308 true);
309 flags_state_->RegisterAllFeatureVariationParameters(&flags_storage_);
310 // The param should have the value predefined in this variation.
311 EXPECT_EQ(kTestParamValue,
312 variations::GetVariationParamValue(kTestTrial, kTestParam));
313 }
314
305 base::CommandLine::StringType CreateSwitch(const std::string& value) { 315 base::CommandLine::StringType CreateSwitch(const std::string& value) {
306 #if defined(OS_WIN) 316 #if defined(OS_WIN)
307 return base::ASCIIToUTF16(value); 317 return base::ASCIIToUTF16(value);
308 #else 318 #else
309 return value; 319 return value;
310 #endif 320 #endif
311 } 321 }
312 322
313 TEST_F(FlagsStateTest, CompareSwitchesToCurrentCommandLine) { 323 TEST_F(FlagsStateTest, CompareSwitchesToCurrentCommandLine) {
314 flags_state_->SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); 324 flags_state_->SetFeatureEntryEnabled(&flags_storage_, kFlags1, true);
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 &supported_entries, &unsupported_entries, 762 &supported_entries, &unsupported_entries,
753 base::Bind(&SkipFeatureEntry)); 763 base::Bind(&SkipFeatureEntry));
754 // All |kEntries| except for |kFlags3| should be supported. 764 // All |kEntries| except for |kFlags3| should be supported.
755 EXPECT_EQ(7u, supported_entries.GetSize()); 765 EXPECT_EQ(7u, supported_entries.GetSize());
756 EXPECT_EQ(1u, unsupported_entries.GetSize()); 766 EXPECT_EQ(1u, unsupported_entries.GetSize());
757 EXPECT_EQ(arraysize(kEntries), 767 EXPECT_EQ(arraysize(kEntries),
758 supported_entries.GetSize() + unsupported_entries.GetSize()); 768 supported_entries.GetSize() + unsupported_entries.GetSize());
759 } 769 }
760 770
761 } // namespace flags_ui 771 } // namespace flags_ui
OLDNEW
« no previous file with comments | « components/flags_ui/flags_state.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698