Chromium Code Reviews| 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 pairs to simulate a restart of cast_shell. Each | |
| 21 // test (ex. FooTest) is accompanied by a test which is guaranteed to run | |
| 22 // before it (ex. PRE_FooTest). PRE_ is a special prefix known to gtest | |
| 23 // which enforces that a given prefixed test runs *before* the unprefixed | |
| 24 // test of the same name. If the 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. BarTest may run before, after, or | |
| 28 // during PRE_FooTest). 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 to test in more than one pair of tests. Because | |
| 33 // the order of execution is not guaranteed between test pairs, this may | |
| 34 // cause the tests to flake, depending on how gtest decides to schedule them. | |
| 35 // For example, FooTest and BarTest should not both try to read and write | |
| 36 // kEnableFoo in the PrefStore. This makes the tests flaky and co-dependent. | |
| 37 | |
| 38 namespace chromecast { | |
| 39 namespace shell { | |
| 40 namespace { | |
| 41 | |
| 42 // For use in TestFeaturesActivateOnBoot only. | |
| 43 const base::Feature kEnableFoo("enable_foo", base::FEATURE_DISABLED_BY_DEFAULT); | |
| 44 const base::Feature kEnableBar("enable_bar", base::FEATURE_ENABLED_BY_DEFAULT); | |
| 45 const base::Feature kEnableBaz("enable_baz", base::FEATURE_DISABLED_BY_DEFAULT); | |
| 46 const base::Feature kEnableBat("enable_bat", base::FEATURE_ENABLED_BY_DEFAULT); | |
|
halliwell
2017/04/27 00:04:23
nit: would it make sense to have a more consistent
slan
2017/04/27 15:36:02
Made it a little more formulaic, added guidance.
| |
| 47 | |
| 48 // For use in TestParamsActivateOnBoot only. | |
| 49 const base::Feature kEnableParams("enable_params", | |
| 50 base::FEATURE_DISABLED_BY_DEFAULT); | |
| 51 | |
| 52 // For use in TestOnlyWellFormedFeaturesPersisted only. | |
| 53 const base::Feature kEnableAlpha("enable_alpha", | |
| 54 base::FEATURE_DISABLED_BY_DEFAULT); | |
| 55 const base::Feature kEnableBravo("enable_bravo", | |
| 56 base::FEATURE_ENABLED_BY_DEFAULT); | |
| 57 const base::Feature kEnableCharlie("enable_charlie", | |
| 58 base::FEATURE_DISABLED_BY_DEFAULT); | |
| 59 const base::Feature kEnableDelta("enable_delta", | |
| 60 base::FEATURE_ENABLED_BY_DEFAULT); | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 class CastFeaturesBrowserTest : public CastBrowserTest { | |
| 65 public: | |
| 66 CastFeaturesBrowserTest() {} | |
| 67 ~CastFeaturesBrowserTest() override {} | |
| 68 | |
| 69 // CastBrowserTest implementation: | |
| 70 void TearDownOnMainThread() override { | |
| 71 CastBrowserTest::TearDownOnMainThread(); | |
| 72 ASSERT_TRUE(features_declared_); | |
| 73 | |
| 74 // If the test failed, scrub any state from prefs. | |
|
halliwell
2017/04/27 00:04:23
What is this block for exactly? does it cover cle
slan
2017/04/27 15:36:02
All really good points. On crash, the device would
| |
| 75 if (HasFailure()) { | |
| 76 ClearDeclaredFeaturesFromPrefs(); | |
| 77 ClearExperimentIds(); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 static PrefService* pref_service() { | |
| 82 return CastBrowserProcess::GetInstance()->pref_service(); | |
| 83 } | |
| 84 | |
| 85 // Write |dcs_features| to the pref store. This method is intended to be | |
| 86 // overridden in internal test to utilize the real production codepath for | |
| 87 // setting features from the server. | |
| 88 virtual void SetFeatures(const base::DictionaryValue& dcs_features) { | |
| 89 auto pref_features = GetOverriddenFeaturesForStorage(dcs_features); | |
| 90 ScopedUserPrefUpdate<base::DictionaryValue, base::Value::Type::DICTIONARY> | |
| 91 dict(pref_service(), prefs::kLatestDCSFeatures); | |
| 92 dict->MergeDictionary(&pref_features); | |
| 93 pref_service()->CommitPendingWrite(); | |
| 94 } | |
| 95 | |
| 96 // Declare the features that will used for this test. This function MUST be | |
| 97 // caled for every test case. | |
| 98 void DeclareFeatures(std::vector<base::Feature> features) { | |
| 99 DCHECK(!features_declared_); | |
|
halliwell
2017/04/27 00:04:23
here and below, should these be asserts running on
slan
2017/04/27 15:36:02
No more DCHECKS after changes above.
| |
| 100 features_declared_ = true; | |
| 101 features_.swap(features); | |
| 102 } | |
| 103 | |
| 104 // Clears only the features applicable to the current test (set in | |
| 105 // DelcareFeatures() above). DeclareFeatures() must be called before this | |
|
halliwell
2017/04/27 00:04:23
DeclareFeatures
slan
2017/04/27 15:36:02
Irrelevant
| |
| 106 // method. | |
| 107 void ClearDeclaredFeaturesFromPrefs() { | |
| 108 DCHECK(features_declared_) << "DeclareFeatures() must be called at the " | |
| 109 << "beginning of the test."; | |
| 110 ScopedUserPrefUpdate<base::DictionaryValue, base::Value::Type::DICTIONARY> | |
| 111 dict(pref_service(), prefs::kLatestDCSFeatures); | |
| 112 for (auto f : features_) | |
| 113 dict->Remove(f.name, nullptr); | |
| 114 pref_service()->CommitPendingWrite(); | |
| 115 } | |
| 116 | |
| 117 // Write |experiment_ids| to the pref store. This method is intended to be | |
| 118 // overridden in internal test to utilize the real production codepath for | |
| 119 // setting features from the server. | |
| 120 virtual void SetExperimentIds( | |
| 121 const std::unordered_set<int32_t>& experiment_ids) { | |
| 122 base::ListValue list; | |
| 123 for (auto id : experiment_ids) | |
| 124 list.AppendInteger(id); | |
| 125 pref_service()->Set(prefs::kActiveDCSExperiments, list); | |
| 126 pref_service()->CommitPendingWrite(); | |
| 127 } | |
| 128 | |
| 129 // Clear the set experiment id's. | |
| 130 void ClearExperimentIds() { | |
| 131 pref_service()->Set(prefs::kActiveDCSExperiments, base::ListValue()); | |
| 132 pref_service()->CommitPendingWrite(); | |
| 133 } | |
| 134 | |
| 135 private: | |
| 136 bool features_declared_ = false; | |
| 137 std::vector<base::Feature> features_; | |
| 138 | |
| 139 DISALLOW_COPY_AND_ASSIGN(CastFeaturesBrowserTest); | |
| 140 }; | |
| 141 | |
| 142 // Test that set features activate on the next boot. Part 1 of 2. | |
| 143 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
| 144 PRE_TestFeaturesActivateOnBoot) { | |
| 145 DeclareFeatures({kEnableFoo, kEnableBar, kEnableBaz, kEnableBat}); | |
| 146 | |
| 147 // Default values should be returned. | |
| 148 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 149 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 150 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBaz)); | |
| 151 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBat)); | |
| 152 | |
| 153 // Set the features to be used on next boot. | |
| 154 base::DictionaryValue features; | |
| 155 features.SetBoolean("enable_foo", true); | |
| 156 features.SetBoolean("enable_bat", false); | |
| 157 SetFeatures(features); | |
| 158 | |
| 159 // Default values should still be returned until next boot. | |
| 160 EXPECT_FALSE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 161 EXPECT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 162 EXPECT_FALSE(base::FeatureList::IsEnabled(kEnableBaz)); | |
| 163 EXPECT_TRUE(base::FeatureList::IsEnabled(kEnableBat)); | |
| 164 } | |
| 165 | |
| 166 // Test that features activate on the next boot. Part 2 of 2. | |
| 167 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestFeaturesActivateOnBoot) { | |
| 168 DeclareFeatures({kEnableFoo, kEnableBar, kEnableBaz, kEnableBat}); | |
| 169 | |
| 170 // Overriden values set in test case above should be set. | |
| 171 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 172 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 173 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBaz)); | |
| 174 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBat)); | |
| 175 | |
| 176 ClearDeclaredFeaturesFromPrefs(); | |
| 177 } | |
| 178 | |
| 179 // Test that features with params activate on boot. Part 1 of 2. | |
| 180 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, PRE_TestParamsActivateOnBoot) { | |
| 181 DeclareFeatures({kEnableParams}); | |
| 182 | |
| 183 // Default value should be returned. | |
| 184 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableParams)); | |
| 185 | |
| 186 // Set the features to be used on next boot. | |
| 187 base::DictionaryValue features; | |
| 188 auto params = base::MakeUnique<base::DictionaryValue>(); | |
| 189 params->SetBoolean("bool_param", true); | |
| 190 params->SetBoolean("bool_param_2", false); | |
| 191 params->SetString("str_param", "foo"); | |
| 192 params->SetDouble("doub_param", 3.14159); | |
| 193 params->SetInteger("int_param", 76543); | |
| 194 features.Set("enable_params", std::move(params)); | |
| 195 SetFeatures(features); | |
| 196 | |
| 197 // Default value should still be returned until next boot. | |
| 198 EXPECT_FALSE(base::FeatureList::IsEnabled(kEnableParams)); | |
| 199 } | |
| 200 | |
| 201 // Test that features with params activate on boot. Part 2 of 2. | |
| 202 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestParamsActivateOnBoot) { | |
| 203 DeclareFeatures({kEnableParams}); | |
| 204 | |
| 205 // Check that the feature is now enabled. | |
| 206 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableParams)); | |
| 207 | |
| 208 // Check that the params are populated and correct. | |
| 209 ASSERT_TRUE(base::GetFieldTrialParamByFeatureAsBool( | |
| 210 kEnableParams, "bool_param", false /* default_value */)); | |
| 211 ASSERT_FALSE(base::GetFieldTrialParamByFeatureAsBool( | |
| 212 kEnableParams, "bool_param_2", true /* default_value */)); | |
| 213 ASSERT_EQ("foo", | |
| 214 base::GetFieldTrialParamValueByFeature(kEnableParams, "str_param")); | |
| 215 ASSERT_EQ(76543, base::GetFieldTrialParamByFeatureAsInt( | |
| 216 kEnableParams, "int_param", 0 /* default_value */)); | |
| 217 ASSERT_EQ(3.14159, base::GetFieldTrialParamByFeatureAsDouble( | |
| 218 kEnableParams, "doub_param", 0.0 /* default_value */)); | |
| 219 | |
| 220 // Check that no extra parameters are set. | |
| 221 std::map<std::string, std::string> params_out; | |
| 222 ASSERT_TRUE(base::GetFieldTrialParamsByFeature(kEnableParams, ¶ms_out)); | |
| 223 ASSERT_EQ(5u, params_out.size()); | |
| 224 | |
| 225 ClearDeclaredFeaturesFromPrefs(); | |
| 226 } | |
| 227 | |
| 228 // Test that only well-formed features are persisted to disk. Part 1 of 2. | |
| 229 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
| 230 PRE_TestOnlyWellFormedFeaturesPersisted) { | |
| 231 DeclareFeatures({kEnableAlpha, kEnableBravo, kEnableCharlie, kEnableDelta}); | |
| 232 | |
| 233 // Default values should be returned. | |
| 234 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableAlpha)); | |
| 235 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBravo)); | |
| 236 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableCharlie)); | |
| 237 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableDelta)); | |
| 238 | |
| 239 // Set both good parameters... | |
| 240 base::DictionaryValue features; | |
| 241 features.SetBoolean("enable_alpha", true); | |
| 242 features.SetBoolean("enable_delta", false); | |
| 243 | |
| 244 // ... and bad parameters. | |
| 245 features.SetString("enable_bravo", "False"); | |
| 246 features.Set("enable_charlie", base::MakeUnique<base::ListValue>()); | |
| 247 | |
| 248 SetFeatures(features); | |
| 249 } | |
| 250 | |
| 251 // Test that only well-formed features are persisted to disk. Part 2 of 2. | |
| 252 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
| 253 TestOnlyWellFormedFeaturesPersisted) { | |
| 254 DeclareFeatures({kEnableAlpha, kEnableBravo, kEnableCharlie, kEnableDelta}); | |
| 255 | |
| 256 // Only the well-formed parameters should be overriden. | |
| 257 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableAlpha)); | |
| 258 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableDelta)); | |
| 259 | |
| 260 // The other should take default values. | |
| 261 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBravo)); | |
| 262 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableCharlie)); | |
| 263 | |
| 264 ClearDeclaredFeaturesFromPrefs(); | |
| 265 } | |
| 266 | |
| 267 // Test that experiment ids are persisted to disk. Part 1 of 2. | |
| 268 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
| 269 PRE_TestExperimentIdsPersisted) { | |
| 270 DeclareFeatures(std::vector<base::Feature>()); | |
| 271 | |
| 272 // No experiments should be set. | |
| 273 ASSERT_TRUE(GetDCSExperimentIds().empty()); | |
| 274 | |
| 275 // Set a test list of experiments. | |
| 276 std::unordered_set<int32_t> ids({1234, 1, 4000}); | |
| 277 SetExperimentIds(ids); | |
| 278 | |
| 279 // No experiments should be set, still. | |
| 280 ASSERT_TRUE(GetDCSExperimentIds().empty()); | |
| 281 } | |
| 282 | |
| 283 // Test that experiment ids are persisted to disk. Part 2 of 2. | |
| 284 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestExperimentIdsPersisted) { | |
| 285 DeclareFeatures(std::vector<base::Feature>()); | |
| 286 | |
| 287 // The experiments set in the last run should be active now. | |
| 288 std::unordered_set<int32_t> ids({1234, 1, 4000}); | |
| 289 ASSERT_EQ(ids, GetDCSExperimentIds()); | |
| 290 | |
| 291 ClearExperimentIds(); | |
| 292 } | |
| 293 | |
| 294 } // namespace shell | |
| 295 } // namespace chromecast | |
| OLD | NEW |