Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
Alexei Svitkine (slow)
2017/04/21 19:48:01
2017 :)
slan
2017/04/21 21:43:28
Ugh... sloppy. Done.
| |
| 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 "base/feature_list.h" | |
| 6 #include "base/macros.h" | |
| 7 #include "base/metrics/field_trial_params.h" | |
| 8 #include "chromecast/base/cast_features.h" | |
|
Alexei Svitkine (slow)
2017/04/21 19:48:01
Nit: Should be the first include.
slan
2017/04/21 21:43:28
Done.
| |
| 9 #include "chromecast/base/pref_names.h" | |
| 10 #include "chromecast/browser/cast_browser_process.h" | |
| 11 #include "chromecast/browser/test/cast_browser_test.h" | |
| 12 #include "components/prefs/pref_service.h" | |
| 13 | |
| 14 namespace chromecast { | |
| 15 namespace shell { | |
| 16 namespace { | |
| 17 | |
| 18 const base::Feature kEnableFoo("enable_foo", base::FEATURE_DISABLED_BY_DEFAULT); | |
| 19 const base::Feature kEnableBar("enable_bar", base::FEATURE_ENABLED_BY_DEFAULT); | |
| 20 const base::Feature kEnableBaz("enable_baz", base::FEATURE_DISABLED_BY_DEFAULT); | |
| 21 const base::Feature kEnableBat("enable_bat", base::FEATURE_ENABLED_BY_DEFAULT); | |
| 22 | |
| 23 const base::Feature kEnableParams("enable_params", | |
| 24 base::FEATURE_DISABLED_BY_DEFAULT); | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 class CastFeaturesBrowserTest : public CastBrowserTest { | |
| 29 public: | |
| 30 CastFeaturesBrowserTest() {} | |
| 31 ~CastFeaturesBrowserTest() override {} | |
| 32 | |
| 33 // Write |dcs_features| to the pref store. This method is intended to be | |
| 34 // overridden in internal test to utilize the real production codepath for | |
| 35 // setting features from the server. | |
| 36 virtual void SetFeatures(const base::DictionaryValue& dcs_features) { | |
| 37 auto pref_features = GetOverriddenFeaturesForStorage(dcs_features); | |
| 38 CastBrowserProcess::GetInstance()->pref_service()->Set( | |
| 39 prefs::kLatestDCSFeatures, *pref_features); | |
| 40 } | |
| 41 | |
| 42 static void ClearFeaturesInPrefs() { | |
| 43 DCHECK(CastBrowserProcess::GetInstance()); | |
| 44 DCHECK(CastBrowserProcess::GetInstance()->pref_service()); | |
| 45 CastBrowserProcess::GetInstance()->pref_service()->Set( | |
| 46 prefs::kLatestDCSFeatures, base::DictionaryValue()); | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 DISALLOW_COPY_AND_ASSIGN(CastFeaturesBrowserTest); | |
| 51 }; | |
| 52 | |
| 53 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, EmptyTest) { | |
| 54 // Default values should be returned. | |
| 55 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 56 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 57 } | |
| 58 | |
| 59 // Test that set features activate on the next boot. Part 1 of 2. | |
| 60 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
| 61 PRE_TestFeaturesActivateOnBoot) { | |
| 62 // Default values should be returned. | |
| 63 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 64 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 65 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBaz)); | |
| 66 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBat)); | |
| 67 | |
| 68 // Set the features to be used on next boot. | |
| 69 base::DictionaryValue features; | |
| 70 features.SetBoolean("enable_foo", true); | |
| 71 features.SetBoolean("enable_bat", false); | |
| 72 SetFeatures(features); | |
| 73 | |
| 74 // Default values should still be returned until next boot. | |
| 75 EXPECT_FALSE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 76 EXPECT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 77 EXPECT_FALSE(base::FeatureList::IsEnabled(kEnableBaz)); | |
| 78 EXPECT_TRUE(base::FeatureList::IsEnabled(kEnableBat)); | |
| 79 } | |
| 80 | |
| 81 // Test that features activate on the next boot. Part 2 of 2. | |
| 82 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestFeaturesActivateOnBoot) { | |
| 83 // Overriden values set in test case above should be set. | |
| 84 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 85 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 86 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBaz)); | |
| 87 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBat)); | |
| 88 | |
| 89 // Clear the features for other tests. | |
| 90 ClearFeaturesInPrefs(); | |
| 91 } | |
| 92 | |
| 93 // Test that features with params activate on boot. Part 1 of 2. | |
| 94 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, PRE_TestParamsActivateOnBoot) { | |
| 95 // Default value should be returned. | |
| 96 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableParams)); | |
| 97 | |
| 98 // Set the features to be used on next boot. | |
| 99 base::DictionaryValue features; | |
| 100 auto params = base::MakeUnique<base::DictionaryValue>(); | |
| 101 params->SetBoolean("bool_param", true); | |
| 102 params->SetBoolean("bool_param_2", false); | |
| 103 params->SetString("str_param", "foo"); | |
| 104 params->SetDouble("doub_param", 3.14159); | |
| 105 params->SetInteger("int_param", 76543); | |
| 106 features.Set("enable_params", std::move(params)); | |
| 107 SetFeatures(features); | |
| 108 | |
| 109 // Default value should still be returned until next boot. | |
| 110 EXPECT_FALSE(base::FeatureList::IsEnabled(kEnableParams)); | |
| 111 } | |
| 112 | |
| 113 // Test that features with params activate on boot. Part 2 of 2. | |
| 114 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, TestParamsActivateOnBoot) { | |
| 115 // Check that the feature is now enabled. | |
| 116 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableParams)); | |
| 117 | |
| 118 // Check that the params are populated and correct. | |
| 119 ASSERT_TRUE(base::GetFieldTrialParamByFeatureAsBool( | |
| 120 kEnableParams, "bool_param", false /* default_value */)); | |
| 121 ASSERT_FALSE(base::GetFieldTrialParamByFeatureAsBool( | |
| 122 kEnableParams, "bool_param_2", true /* default_value */)); | |
| 123 ASSERT_EQ("foo", | |
| 124 base::GetFieldTrialParamValueByFeature(kEnableParams, "str_param")); | |
| 125 ASSERT_EQ(76543, base::GetFieldTrialParamByFeatureAsInt( | |
| 126 kEnableParams, "int_param", 0 /* default_value */)); | |
| 127 ASSERT_EQ(3.14159, base::GetFieldTrialParamByFeatureAsDouble( | |
| 128 kEnableParams, "doub_param", 0.0 /* default_value */)); | |
| 129 | |
| 130 // Check that no extra parameters are set. | |
| 131 std::map<std::string, std::string> params_out; | |
| 132 ASSERT_TRUE(base::GetFieldTrialParamsByFeature(kEnableParams, ¶ms_out)); | |
| 133 ASSERT_EQ(5u, params_out.size()); | |
| 134 | |
| 135 // Clear the features for other tests. | |
| 136 ClearFeaturesInPrefs(); | |
| 137 } | |
| 138 | |
| 139 // Test that only well-formed features are persisted to disk. Part 1 of 2. | |
| 140 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
| 141 PRE_TestOnlyWellFormedFeaturesPersisted) { | |
| 142 // Default values should be returned. | |
| 143 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 144 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 145 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBaz)); | |
| 146 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBat)); | |
| 147 | |
| 148 // Set both good parameters... | |
| 149 base::DictionaryValue features; | |
| 150 features.SetBoolean("enable_foo", true); | |
| 151 features.SetBoolean("enable_bat", false); | |
| 152 | |
| 153 // ... and bad parameters. | |
| 154 features.SetString("enable_bar", "False"); | |
| 155 features.Set("enable_baz", base::MakeUnique<base::ListValue>()); | |
| 156 | |
| 157 SetFeatures(features); | |
| 158 } | |
| 159 | |
| 160 // Test that only well-formed features are persisted to disk. Part 2 of 2. | |
| 161 IN_PROC_BROWSER_TEST_F(CastFeaturesBrowserTest, | |
| 162 TestOnlyWellFormedFeaturesPersisted) { | |
| 163 // Only the well-formed parameters should be overriden. | |
| 164 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableFoo)); | |
| 165 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBat)); | |
| 166 | |
| 167 // The other should take default values. | |
| 168 ASSERT_TRUE(base::FeatureList::IsEnabled(kEnableBar)); | |
| 169 ASSERT_FALSE(base::FeatureList::IsEnabled(kEnableBaz)); | |
| 170 } | |
| 171 | |
| 172 } // namespace shell | |
| 173 } // namespace chromecast | |
| OLD | NEW |