OLD | NEW |
| (Empty) |
1 // Copyright 2017 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 "chromecast/base/cast_features.h" | |
6 | |
7 #include <cstdint> | |
8 #include <unordered_set> | |
9 | |
10 #include "base/feature_list.h" | |
11 #include "base/macros.h" | |
12 #include "base/metrics/field_trial_params.h" | |
13 #include "chromecast/base/pref_names.h" | |
14 #include "chromecast/browser/cast_browser_process.h" | |
15 #include "chromecast/browser/test/cast_browser_test.h" | |
16 #include "components/prefs/pref_service.h" | |
17 #include "components/prefs/scoped_user_pref_update.h" | |
18 | |
19 // PLEASE READ: | |
20 // 1) These tests are run in groups to simulate a restart of cast_shell. Each | |
21 // test (ex. TestFoo) is accompanied by one or more tests which are | |
22 // guaranteed to run before it (ex. PRE_TestFoo, PRE_PRE_TestFoo). PRE_ is a | |
23 // special prefix used by content::BrowserTest to enforce test ordering among | |
24 // tests of the same name. If a prefixed test fails, the unprefixed test | |
25 // will not run. The tests use this behavior to test persisted state between | |
26 // subsequent cast_shell runs. HOWEVER, tests which do not share a common | |
27 // name have NO guarantee of ordering (ex. TestBar may run before, after, or | |
28 // during PRE_TestFoo). Therefore, tests with different names should be | |
29 // entirely independent. | |
30 // | |
31 // 2) Because of the last note in (1), when testing persistent features, do not | |
32 // re-use the same feature in more than one set of tests. Because the order | |
33 // of execution is not guaranteed between test sets, this may cause the tests | |
34 // to flake, depending on how gtest decides to schedule them. | |
35 // For example, TestFoo and TestBar should not both try to read and write | |
36 // kTestFeat1 in the PrefStore. This makes the tests flaky and co-dependent. | |
37 // | |
38 // 3) Generally, the model should look like this: | |
39 // | |
40 // IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, PRE_PRE_TestFoo) { | |
41 // // Reset the state of the features. | |
42 // CleareFeaturesFromPrefs({kFooFeature1, kFooFeature2}); | |
43 // } | |
44 // | |
45 // IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, PRE_TestFoo) { | |
46 // // Test the default state of the features... | |
47 // EXPECT_TRUE(...); | |
48 // | |
49 // // ... then persist overrides to disk. | |
50 // SetFeatures(features_dict); // contains override for kFooFeature1 | |
51 // } | |
52 // | |
53 // IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestFoo) { | |
54 // // Test that the feature override was properly persisted. | |
55 // EXPECT_FALSE(...); | |
56 // } | |
57 // | |
58 // Note that in the example above, kFooFeatureX should not be used in tests | |
59 // other than (PRE_)*FooTest, to ensure test indepdendence. An unrelated | |
60 // BarTest should use features like KBarFeatureX. | |
61 // | |
62 | |
63 namespace chromecast { | |
64 namespace shell { | |
65 namespace { | |
66 | |
67 // Note: When adding new features for existing tests, please extend on the | |
68 // current block of features, by adding another sequentially-numbered feature. | |
69 // When adding a new test, please add a new block of features, the numbering for | |
70 // which is based at the next available multiple of 10. | |
71 | |
72 // For use in TestFeaturesActivateOnBoot only. | |
73 const base::Feature kTestFeat1("test_feat_1", | |
74 base::FEATURE_DISABLED_BY_DEFAULT); | |
75 const base::Feature kTestFeat2("test_feat_2", base::FEATURE_ENABLED_BY_DEFAULT); | |
76 const base::Feature kTestFeat3("test_feat_3", | |
77 base::FEATURE_DISABLED_BY_DEFAULT); | |
78 const base::Feature kTestFeat4("test_feat_4", base::FEATURE_ENABLED_BY_DEFAULT); | |
79 | |
80 // For use in TestParamsActivateOnBoot only. | |
81 const base::Feature kTestFeat11("test_feat_11", | |
82 base::FEATURE_DISABLED_BY_DEFAULT); | |
83 | |
84 // For use in TestOnlyWellFormedFeaturesPersisted only. | |
85 const base::Feature kTestFeat21("test_feat_21", | |
86 base::FEATURE_DISABLED_BY_DEFAULT); | |
87 const base::Feature kTestFeat22("test_feat_22", | |
88 base::FEATURE_ENABLED_BY_DEFAULT); | |
89 const base::Feature kTestFeat23("test_feat_23", | |
90 base::FEATURE_DISABLED_BY_DEFAULT); | |
91 const base::Feature kTestFeat24("test_feat_24", | |
92 base::FEATURE_ENABLED_BY_DEFAULT); | |
93 | |
94 } // namespace | |
95 | |
96 class CastFeaturesBrowserTest : public CastBrowserTest { | |
97 public: | |
98 CastFeaturesBrowserTest() {} | |
99 ~CastFeaturesBrowserTest() override {} | |
100 | |
101 static PrefService* pref_service() { | |
102 return CastBrowserProcess::GetInstance()->pref_service(); | |
103 } | |
104 | |
105 // Write |dcs_features| to the pref store. This method is intended to be | |
106 // overridden in internal test to utilize the real production codepath for | |
107 // setting features from the server. | |
108 virtual void SetFeatures(const base::DictionaryValue& dcs_features) { | |
109 auto pref_features = GetOverriddenFeaturesForStorage(dcs_features); | |
110 ScopedUserPrefUpdate<base::DictionaryValue, base::Value::Type::DICTIONARY> | |
111 dict(pref_service(), prefs::kLatestDCSFeatures); | |
112 dict->MergeDictionary(&pref_features); | |
113 pref_service()->CommitPendingWrite(); | |
114 } | |
115 | |
116 // Clears |features| from the PrefStore. Should be called in a PRE_PRE_* | |
117 // method for any tested feature in a test to ensure consistent state. | |
118 void ClearFeaturesFromPrefs(std::vector<base::Feature> features) { | |
119 ScopedUserPrefUpdate<base::DictionaryValue, base::Value::Type::DICTIONARY> | |
120 dict(pref_service(), prefs::kLatestDCSFeatures); | |
121 for (auto f : features) | |
122 dict->Remove(f.name, nullptr); | |
123 pref_service()->CommitPendingWrite(); | |
124 } | |
125 | |
126 // Write |experiment_ids| to the pref store. This method is intended to be | |
127 // overridden in internal test to utilize the real production codepath for | |
128 // setting features from the server. | |
129 virtual void SetExperimentIds( | |
130 const std::unordered_set<int32_t>& experiment_ids) { | |
131 base::ListValue list; | |
132 for (auto id : experiment_ids) | |
133 list.AppendInteger(id); | |
134 pref_service()->Set(prefs::kActiveDCSExperiments, list); | |
135 pref_service()->CommitPendingWrite(); | |
136 } | |
137 | |
138 // Clear the set experiment id's. | |
139 void ClearExperimentIds() { | |
140 pref_service()->Set(prefs::kActiveDCSExperiments, base::ListValue()); | |
141 pref_service()->CommitPendingWrite(); | |
142 } | |
143 | |
144 private: | |
145 DISALLOW_COPY_AND_ASSIGN(CastFeaturesBrowserTest); | |
146 }; | |
147 | |
148 // Test that set features activate on the next boot. Part 1 of 3. | |
149 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
150 PRE_PRE_TestFeaturesActivateOnBoot) { | |
151 ClearFeaturesFromPrefs({kTestFeat1, kTestFeat2, kTestFeat3, kTestFeat4}); | |
152 } | |
153 | |
154 // Test that set features activate on the next boot. Part 2 of 3. | |
155 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
156 PRE_TestFeaturesActivateOnBoot) { | |
157 // Default values should be returned. | |
158 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat1)); | |
159 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat2)); | |
160 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat3)); | |
161 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat4)); | |
162 | |
163 // Set the features to be used on next boot. | |
164 base::DictionaryValue features; | |
165 features.SetBoolean("test_feat_1", true); | |
166 features.SetBoolean("test_feat_4", false); | |
167 SetFeatures(features); | |
168 | |
169 // Default values should still be returned until next boot. | |
170 EXPECT_FALSE(base::FeatureList::IsEnabled(kTestFeat1)); | |
171 EXPECT_TRUE(base::FeatureList::IsEnabled(kTestFeat2)); | |
172 EXPECT_FALSE(base::FeatureList::IsEnabled(kTestFeat3)); | |
173 EXPECT_TRUE(base::FeatureList::IsEnabled(kTestFeat4)); | |
174 } | |
175 | |
176 // Test that features activate on the next boot. Part 3 of 3. | |
177 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestFeaturesActivateOnBoot) { | |
178 // Overriden values set in test case above should be set. | |
179 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat1)); | |
180 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat2)); | |
181 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat3)); | |
182 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat4)); | |
183 } | |
184 | |
185 // Test that features with params activate on boot. Part 1 of 3. | |
186 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
187 PRE_PRE_TestParamsActivateOnBoot) { | |
188 ClearFeaturesFromPrefs({kTestFeat11}); | |
189 } | |
190 | |
191 // Test that features with params activate on boot. Part 2 of 3. | |
192 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, PRE_TestParamsActivateOnBoot) { | |
193 // Default value should be returned. | |
194 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat11)); | |
195 | |
196 // Set the features to be used on next boot. | |
197 base::DictionaryValue features; | |
198 auto params = base::MakeUnique<base::DictionaryValue>(); | |
199 params->SetBoolean("bool_param", true); | |
200 params->SetBoolean("bool_param_2", false); | |
201 params->SetString("str_param", "foo"); | |
202 params->SetDouble("doub_param", 3.14159); | |
203 params->SetInteger("int_param", 76543); | |
204 features.Set("test_feat_11", std::move(params)); | |
205 SetFeatures(features); | |
206 | |
207 // Default value should still be returned until next boot. | |
208 EXPECT_FALSE(base::FeatureList::IsEnabled(kTestFeat11)); | |
209 } | |
210 | |
211 // Test that features with params activate on boot. Part 3 of 3. | |
212 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestParamsActivateOnBoot) { | |
213 // Check that the feature is now enabled. | |
214 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat11)); | |
215 | |
216 // Check that the params are populated and correct. | |
217 ASSERT_TRUE(base::GetFieldTrialParamByFeatureAsBool( | |
218 kTestFeat11, "bool_param", false /* default_value */)); | |
219 ASSERT_FALSE(base::GetFieldTrialParamByFeatureAsBool( | |
220 kTestFeat11, "bool_param_2", true /* default_value */)); | |
221 ASSERT_EQ("foo", | |
222 base::GetFieldTrialParamValueByFeature(kTestFeat11, "str_param")); | |
223 ASSERT_EQ(76543, base::GetFieldTrialParamByFeatureAsInt( | |
224 kTestFeat11, "int_param", 0 /* default_value */)); | |
225 ASSERT_EQ(3.14159, base::GetFieldTrialParamByFeatureAsDouble( | |
226 kTestFeat11, "doub_param", 0.0 /* default_value */)); | |
227 | |
228 // Check that no extra parameters are set. | |
229 std::map<std::string, std::string> params_out; | |
230 ASSERT_TRUE(base::GetFieldTrialParamsByFeature(kTestFeat11, ¶ms_out)); | |
231 ASSERT_EQ(5u, params_out.size()); | |
232 } | |
233 | |
234 // Test that only well-formed features are persisted to disk. Part 1 of 3. | |
235 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
236 PRE_PRE_TestOnlyWellFormedFeaturesPersisted) { | |
237 ClearFeaturesFromPrefs({kTestFeat21, kTestFeat22, kTestFeat23, kTestFeat24}); | |
238 } | |
239 | |
240 // Test that only well-formed features are persisted to disk. Part 2 of 3. | |
241 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
242 PRE_TestOnlyWellFormedFeaturesPersisted) { | |
243 // Default values should be returned. | |
244 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat21)); | |
245 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat22)); | |
246 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat23)); | |
247 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat24)); | |
248 | |
249 // Set both good parameters... | |
250 base::DictionaryValue features; | |
251 features.SetBoolean("test_feat_21", true); | |
252 features.SetBoolean("test_feat_24", false); | |
253 | |
254 // ... and bad parameters. | |
255 features.SetString("test_feat_22", "False"); | |
256 features.Set("test_feat_23", base::MakeUnique<base::ListValue>()); | |
257 | |
258 SetFeatures(features); | |
259 } | |
260 | |
261 // Test that only well-formed features are persisted to disk. Part 2 of 2. | |
262 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
263 TestOnlyWellFormedFeaturesPersisted) { | |
264 // Only the well-formed parameters should be overriden. | |
265 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat21)); | |
266 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat24)); | |
267 | |
268 // The other should take default values. | |
269 ASSERT_TRUE(base::FeatureList::IsEnabled(kTestFeat22)); | |
270 ASSERT_FALSE(base::FeatureList::IsEnabled(kTestFeat23)); | |
271 } | |
272 | |
273 // Test that experiment ids are persisted to disk. Part 1 of 3. | |
274 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
275 PRE_PRE_TestExperimentIdsPersisted) { | |
276 ClearExperimentIds(); | |
277 } | |
278 | |
279 // Test that experiment ids are persisted to disk. Part 2 of 3. | |
280 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
281 PRE_TestExperimentIdsPersisted) { | |
282 // No experiments should be set. | |
283 ASSERT_TRUE(GetDCSExperimentIds().empty()); | |
284 | |
285 // Set a test list of experiments. | |
286 std::unordered_set<int32_t> ids({1234, 1, 4000}); | |
287 SetExperimentIds(ids); | |
288 | |
289 // No experiments should be set, still. | |
290 ASSERT_TRUE(GetDCSExperimentIds().empty()); | |
291 } | |
292 | |
293 // Test that experiment ids are persisted to disk. Part 3 of 3. | |
294 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestExperimentIdsPersisted) { | |
295 // The experiments set in the last run should be active now. | |
296 std::unordered_set<int32_t> ids({1234, 1, 4000}); | |
297 ASSERT_EQ(ids, GetDCSExperimentIds()); | |
298 } | |
299 | |
300 } // namespace shell | |
301 } // namespace chromecast | |
OLD | NEW |