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