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

Side by Side Diff: chrome/browser/policy/policy_prefs_browsertest.cc

Issue 11193002: Add third batch of controlled setting indicators (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <algorithm> 5 #include <algorithm>
6 #include <map> 6 #include <map>
7 #include <sstream> 7 #include <sstream>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 "chrome://settings-frame/passwords", 46 "chrome://settings-frame/passwords",
47 "chrome://settings-frame/autofill", 47 "chrome://settings-frame/autofill",
48 "chrome://settings-frame/content", 48 "chrome://settings-frame/content",
49 "chrome://settings-frame/homePageOverlay", 49 "chrome://settings-frame/homePageOverlay",
50 "chrome://settings-frame/languages", 50 "chrome://settings-frame/languages",
51 #if defined(OS_CHROMEOS) 51 #if defined(OS_CHROMEOS)
52 "chrome://settings-frame/accounts", 52 "chrome://settings-frame/accounts",
53 #endif 53 #endif
54 }; 54 };
55 55
56 // Contains the details of one test case verifying the behavior of controlled 56 // Contains the details of a single test case verifying that the controlled
57 // setting indicators in the settings UI for a policy, part of the data loaded 57 // setting indicators for a pref affected by a policy work correctly. This is
58 // from chrome/test/data/policy/policy_test_cases.json. 58 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json.
59 class IndicatorTestCase { 59 class IndicatorTestCase {
60 public: 60 public:
61 IndicatorTestCase(const base::DictionaryValue& policy, 61 IndicatorTestCase(const base::DictionaryValue& policy,
62 const std::string& value, 62 const std::string& value,
63 bool readonly) 63 bool readonly)
64 : policy_(policy.DeepCopy()), value_(value), readonly_(readonly) {} 64 : policy_(policy.DeepCopy()), value_(value), readonly_(readonly) {}
65 ~IndicatorTestCase() {} 65 ~IndicatorTestCase() {}
66 66
67 const base::DictionaryValue& policy() const { return *policy_; } 67 const base::DictionaryValue& policy() const { return *policy_; }
68
68 const std::string& value() const { return value_; } 69 const std::string& value() const { return value_; }
70
69 bool readonly() const { return readonly_; } 71 bool readonly() const { return readonly_; }
70 72
71 private: 73 private:
72 scoped_ptr<base::DictionaryValue> policy_; 74 scoped_ptr<base::DictionaryValue> policy_;
73 std::string value_; 75 std::string value_;
74 bool readonly_; 76 bool readonly_;
75 77
76 DISALLOW_COPY_AND_ASSIGN(IndicatorTestCase); 78 DISALLOW_COPY_AND_ASSIGN(IndicatorTestCase);
77 }; 79 };
78 80
79 // Contains the testing details for a single policy, loaded from 81 // Contains the testing details for a single pref affected by a policy. This is
80 // chrome/test/data/policy/policy_test_cases.json. 82 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json.
81 class PolicyTestCase { 83 class PrefMapping {
82 public: 84 public:
83 explicit PolicyTestCase(const std::string& name) 85 PrefMapping(const std::string& pref,
84 : name_(name), 86 bool is_local_state,
85 can_be_recommended_(false), 87 const std::string& indicator_test_setup_js,
86 is_local_state_(false), 88 const std::string& indicator_selector)
87 official_only_(false) {} 89 : pref_(pref),
88 ~PolicyTestCase() {} 90 is_local_state_(is_local_state),
91 indicator_selector_(indicator_selector) {
92 indicator_test_setup_js_ = ASCIIToWide(indicator_test_setup_js);
93 }
94 ~PrefMapping() {}
89 95
90 const std::string& name() const { return name_; } 96 const std::string& pref() const { return pref_; }
91 97
92 void set_pref(const std::string& pref) { pref_ = pref; } 98 bool is_local_state() const { return is_local_state_; }
93 const std::string& pref() const { return pref_; }
94 const char* pref_name() const { return pref_.c_str(); }
95
96 bool can_be_recommended() const { return can_be_recommended_; }
97 void set_can_be_recommended(bool can_be_recommended) {
98 can_be_recommended_ = can_be_recommended;
99 }
100
101 const PolicyMap& test_policy() const { return test_policy_; }
102 void set_test_policy(const PolicyMap& policy) {
103 test_policy_.CopyFrom(policy);
104 }
105 99
106 const std::wstring& indicator_test_setup_js() const { 100 const std::wstring& indicator_test_setup_js() const {
107 return indicator_test_setup_js_; 101 return indicator_test_setup_js_;
108 } 102 }
109 void set_indicator_test_setup_js(const std::string& indicator_test_setup_js) {
110 indicator_test_setup_js_ = ASCIIToWide(indicator_test_setup_js);
111 }
112 103
113 const std::string& indicator_selector() const { return indicator_selector_; } 104 const std::string& indicator_selector() const {
114 void set_indicator_selector(const std::string& indicator_selector) { 105 return indicator_selector_;
115 indicator_selector_ = indicator_selector;
116 } 106 }
117 107
118 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const { 108 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const {
119 return indicator_test_cases_; 109 return indicator_test_cases_;
120 } 110 }
121 void AddIndicatorTestCase(IndicatorTestCase* test_case) { 111 void AddIndicatorTestCase(IndicatorTestCase* test_case) {
122 indicator_test_cases_.push_back(test_case); 112 indicator_test_cases_.push_back(test_case);
123 } 113 }
124 114
125 const std::vector<GURL>& settings_pages() const { return settings_pages_; } 115 private:
126 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } 116 std::string pref_;
117 bool is_local_state_;
118 std::wstring indicator_test_setup_js_;
119 std::string indicator_selector_;
120 ScopedVector<IndicatorTestCase> indicator_test_cases_;
121
122 DISALLOW_COPY_AND_ASSIGN(PrefMapping);
123 };
124
125 // Contains the testing details for a single policy. This is part of the data
126 // loaded from chrome/test/data/policy/policy_test_cases.json.
127 class PolicyTestCase {
128 public:
129 PolicyTestCase(const std::string& name,
130 bool is_official_only,
131 bool can_be_recommended)
132 : name_(name),
133 is_official_only_(is_official_only),
134 can_be_recommended_(can_be_recommended) {}
135 ~PolicyTestCase() {}
136
137 const std::string& name() const { return name_; }
138
139 bool is_official_only() const { return is_official_only_; }
140
141 bool can_be_recommended() const { return can_be_recommended_; }
127 142
128 bool IsOsSupported() const { 143 bool IsOsSupported() const {
129 #if defined(OS_WIN) 144 #if defined(OS_WIN)
130 const std::string os("win"); 145 const std::string os("win");
131 #elif defined(OS_MACOSX) 146 #elif defined(OS_MACOSX)
132 const std::string os("mac"); 147 const std::string os("mac");
133 #elif defined(OS_CHROMEOS) 148 #elif defined(OS_CHROMEOS)
134 const std::string os("chromeos"); 149 const std::string os("chromeos");
135 #elif defined(OS_LINUX) 150 #elif defined(OS_LINUX)
136 const std::string os("linux"); 151 const std::string os("linux");
137 #else 152 #else
138 #error "Unknown platform" 153 #error "Unknown platform"
139 #endif 154 #endif
140 return std::find(supported_os_.begin(), supported_os_.end(), os) != 155 return std::find(supported_os_.begin(), supported_os_.end(), os) !=
141 supported_os_.end(); 156 supported_os_.end();
142 } 157 }
143
144 void AddSupportedOs(const std::string& os) { supported_os_.push_back(os); } 158 void AddSupportedOs(const std::string& os) { supported_os_.push_back(os); }
145 159
146 bool is_local_state() const { return is_local_state_; }
147 void set_local_state(bool flag) { is_local_state_ = flag; }
148
149 bool is_official_only() const { return official_only_; }
150 void set_official_only(bool flag) { official_only_ = flag; }
151
152 bool IsSupported() const { 160 bool IsSupported() const {
153 #if !defined(OFFICIAL_BUILD) 161 #if !defined(OFFICIAL_BUILD)
154 if (is_official_only()) 162 if (is_official_only())
155 return false; 163 return false;
156 #endif 164 #endif
157 return IsOsSupported(); 165 return IsOsSupported();
158 } 166 }
159 167
168 const PolicyMap& test_policy() const { return test_policy_; }
169 void SetTestPolicy(const PolicyMap& policy) {
170 test_policy_.CopyFrom(policy);
171 }
172
173 const std::vector<GURL>& settings_pages() const { return settings_pages_; }
174 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); }
175
176 const ScopedVector<PrefMapping>& pref_mappings() const {
177 return pref_mappings_;
178 }
179 void AddPrefMapping(PrefMapping* pref_mapping) {
180 pref_mappings_.push_back(pref_mapping);
181 }
182
160 private: 183 private:
161 std::string name_; 184 std::string name_;
162 std::string pref_; 185 bool is_official_only_;
163 bool can_be_recommended_; 186 bool can_be_recommended_;
187 std::vector<std::string> supported_os_;
164 PolicyMap test_policy_; 188 PolicyMap test_policy_;
165 std::wstring indicator_test_setup_js_;
166 std::string indicator_selector_;
167 ScopedVector<IndicatorTestCase> indicator_test_cases_;
168 std::vector<GURL> settings_pages_; 189 std::vector<GURL> settings_pages_;
169 std::vector<std::string> supported_os_; 190 ScopedVector<PrefMapping> pref_mappings_;
170 bool is_local_state_;
171 bool official_only_;
172 191
173 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); 192 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase);
174 }; 193 };
175 194
176 // Parses all the test cases and makes then available in a map. 195 // Parses all policy test cases and makes then available in a map.
177 class TestCases { 196 class PolicyTestCases {
178 public: 197 public:
179 typedef std::map<std::string, PolicyTestCase*> TestCaseMap; 198 typedef std::map<std::string, PolicyTestCase*> PolicyTestCaseMap;
180 typedef TestCaseMap::const_iterator iterator; 199 typedef PolicyTestCaseMap::const_iterator iterator;
181 200
182 TestCases() { 201 PolicyTestCases() {
183 test_cases_ = new std::map<std::string, PolicyTestCase*>(); 202 policy_test_cases_ = new std::map<std::string, PolicyTestCase*>();
184 203
185 FilePath path = ui_test_utils::GetTestFilePath( 204 FilePath path = ui_test_utils::GetTestFilePath(
186 FilePath(FILE_PATH_LITERAL("policy")), 205 FilePath(FILE_PATH_LITERAL("policy")),
187 FilePath(FILE_PATH_LITERAL("policy_test_cases.json"))); 206 FilePath(FILE_PATH_LITERAL("policy_test_cases.json")));
188 std::string json; 207 std::string json;
189 if (!file_util::ReadFileToString(path, &json)) { 208 if (!file_util::ReadFileToString(path, &json)) {
190 ADD_FAILURE(); 209 ADD_FAILURE();
191 return; 210 return;
192 } 211 }
193 int error_code = -1; 212 int error_code = -1;
194 std::string error_string; 213 std::string error_string;
195 base::DictionaryValue* dict = NULL; 214 base::DictionaryValue* dict = NULL;
196 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( 215 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
197 json, base::JSON_PARSE_RFC, &error_code, &error_string)); 216 json, base::JSON_PARSE_RFC, &error_code, &error_string));
198 if (!value.get() || !value->GetAsDictionary(&dict)) { 217 if (!value.get() || !value->GetAsDictionary(&dict)) {
199 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string; 218 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string;
200 return; 219 return;
201 } 220 }
202 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); 221 const PolicyDefinitionList* list = GetChromePolicyDefinitionList();
203 for (const PolicyDefinitionList::Entry* policy = list->begin; 222 for (const PolicyDefinitionList::Entry* policy = list->begin;
204 policy != list->end; ++policy) { 223 policy != list->end; ++policy) {
205 PolicyTestCase* test_case = GetTestCase(dict, policy->name); 224 PolicyTestCase* policy_test_case = GetPolicyTestCase(dict, policy->name);
206 if (test_case) 225 if (policy_test_case)
207 (*test_cases_)[policy->name] = test_case; 226 (*policy_test_cases_)[policy->name] = policy_test_case;
208 } 227 }
209 } 228 }
210 229
211 ~TestCases() { 230 ~PolicyTestCases() {
212 STLDeleteValues(test_cases_); 231 STLDeleteValues(policy_test_cases_);
213 delete test_cases_; 232 delete policy_test_cases_;
214 } 233 }
215 234
216 const PolicyTestCase* Get(const std::string& name) { 235 const PolicyTestCase* Get(const std::string& name) {
217 iterator it = test_cases_->find(name); 236 iterator it = policy_test_cases_->find(name);
218 return it == end() ? NULL : it->second; 237 return it == end() ? NULL : it->second;
219 } 238 }
220 239
221 const TestCaseMap& map() const { return *test_cases_; } 240 const PolicyTestCaseMap& map() const { return *policy_test_cases_; }
222 iterator begin() const { return test_cases_->begin(); } 241 iterator begin() const { return policy_test_cases_->begin(); }
223 iterator end() const { return test_cases_->end(); } 242 iterator end() const { return policy_test_cases_->end(); }
224 243
225 private: 244 private:
226 PolicyTestCase* GetTestCase(const base::DictionaryValue* tests, 245 PolicyTestCase* GetPolicyTestCase(const base::DictionaryValue* tests,
227 const std::string& name) { 246 const std::string& name) {
228 const base::DictionaryValue* dict = NULL; 247 const base::DictionaryValue* policy_test_dict = NULL;
229 if (!tests->GetDictionary(name, &dict)) 248 if (!tests->GetDictionary(name, &policy_test_dict))
230 return NULL; 249 return NULL;
231 PolicyTestCase* test_case = new PolicyTestCase(name); 250 bool is_official_only = false;
232 std::string pref; 251 policy_test_dict->GetBoolean("official_only", &is_official_only);
233 if (dict->GetString("pref", &pref)) 252 bool can_be_recommended = false;
234 test_case->set_pref(pref); 253 policy_test_dict->GetBoolean("can_be_recommended", &can_be_recommended);
235 bool flag = false; 254 PolicyTestCase* policy_test_case =
236 if (dict->GetBoolean("can_be_recommended", &flag)) 255 new PolicyTestCase(name, is_official_only, can_be_recommended);
237 test_case->set_can_be_recommended(flag); 256 const base::ListValue* os_list = NULL;
238 const base::DictionaryValue* policy_dict = NULL; 257 if (policy_test_dict->GetList("os", &os_list)) {
239 if (dict->GetDictionary("test_policy", &policy_dict)) { 258 for (size_t i = 0; i < os_list->GetSize(); ++i) {
240 PolicyMap policies; 259 std::string os;
241 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 260 if (os_list->GetString(i, &os))
242 test_case->set_test_policy(policies); 261 policy_test_case->AddSupportedOs(os);
243 }
244 std::string indicator_test_setup_js;
245 if (dict->GetString("indicator_test_setup_js", &indicator_test_setup_js))
246 test_case->set_indicator_test_setup_js(indicator_test_setup_js);
247 std::string indicator_selector;
248 if (dict->GetString("indicator_selector", &indicator_selector))
249 test_case->set_indicator_selector(indicator_selector);
250 const base::ListValue* indicator_tests = NULL;
251 if (dict->GetList("indicator_tests", &indicator_tests)) {
252 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) {
253 const base::DictionaryValue* indicator_test_dict = NULL;
254 const base::DictionaryValue* policy = NULL;
255 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) ||
256 !indicator_test_dict->GetDictionary("policy", &policy)) {
257 continue;
258 }
259 std::string value;
260 indicator_test_dict->GetString("value", &value);
261 bool readonly = false;
262 indicator_test_dict->GetBoolean("readonly", &readonly);
263 test_case->AddIndicatorTestCase(
264 new IndicatorTestCase(*policy, value, readonly));
265 } 262 }
266 } 263 }
264 const base::DictionaryValue* policy_dict = NULL;
265 if (policy_test_dict->GetDictionary("test_policy", &policy_dict)) {
266 PolicyMap policy;
267 policy.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
268 policy_test_case->SetTestPolicy(policy);
269 }
267 const base::ListValue* settings_pages = NULL; 270 const base::ListValue* settings_pages = NULL;
268 if (dict->GetList("settings_pages", &settings_pages)) { 271 if (policy_test_dict->GetList("settings_pages", &settings_pages)) {
269 for (size_t i = 0; i < settings_pages->GetSize(); ++i) { 272 for (size_t i = 0; i < settings_pages->GetSize(); ++i) {
270 std::string page; 273 std::string page;
271 if (settings_pages->GetString(i, &page)) 274 if (settings_pages->GetString(i, &page))
272 test_case->AddSettingsPage(GURL(page)); 275 policy_test_case->AddSettingsPage(GURL(page));
273 } 276 }
274 } 277 }
275 const base::ListValue* supported_os = NULL; 278 const base::ListValue* pref_mappings = NULL;
276 if (dict->GetList("os", &supported_os)) { 279 if (policy_test_dict->GetList("pref_mappings", &pref_mappings)) {
277 for (size_t i = 0; i < supported_os->GetSize(); ++i) { 280 for (size_t i = 0; i < pref_mappings->GetSize(); ++i) {
278 std::string os; 281 const base::DictionaryValue* pref_mapping_dict = NULL;
279 if (supported_os->GetString(i, &os)) 282 std::string pref;
280 test_case->AddSupportedOs(os); 283 if (!pref_mappings->GetDictionary(i, &pref_mapping_dict) ||
284 !pref_mapping_dict->GetString("pref", &pref)) {
285 continue;
Joao da Silva 2012/10/16 17:58:41 add a failure if the input is broken
bartfab (slow) 2012/10/16 20:50:31 Done.
286 }
287 bool is_local_state = false;
288 pref_mapping_dict->GetBoolean("local_state", &is_local_state);
289 std::string indicator_test_setup_js;
290 pref_mapping_dict->GetString("indicator_test_setup_js",
291 &indicator_test_setup_js);
292 std::string indicator_selector;
293 pref_mapping_dict->GetString("indicator_selector", &indicator_selector);
294 PrefMapping* pref_mapping = new PrefMapping(
295 pref, is_local_state, indicator_test_setup_js, indicator_selector);
296 const base::ListValue* indicator_tests = NULL;
297 if (pref_mapping_dict->GetList("indicator_tests", &indicator_tests)) {
298 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) {
299 const base::DictionaryValue* indicator_test_dict = NULL;
300 const base::DictionaryValue* policy = NULL;
301 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) ||
302 !indicator_test_dict->GetDictionary("policy", &policy)) {
303 continue;
Joao da Silva 2012/10/16 17:58:41 add failure
bartfab (slow) 2012/10/16 20:50:31 Done.
304 }
305 std::string value;
306 indicator_test_dict->GetString("value", &value);
307 bool readonly = false;
308 indicator_test_dict->GetBoolean("readonly", &readonly);
309 pref_mapping->AddIndicatorTestCase(
310 new IndicatorTestCase(*policy, value, readonly));
311 }
312 }
313 policy_test_case->AddPrefMapping(pref_mapping);
281 } 314 }
282 } 315 }
283 if (dict->GetBoolean("local_state", &flag)) 316 return policy_test_case;
284 test_case->set_local_state(flag);
285 if (dict->GetBoolean("official_only", &flag))
286 test_case->set_official_only(flag);
287 return test_case;
288 } 317 }
289 318
290 TestCaseMap* test_cases_; 319 PolicyTestCaseMap* policy_test_cases_;
291 320
292 DISALLOW_COPY_AND_ASSIGN(TestCases); 321 DISALLOW_COPY_AND_ASSIGN(PolicyTestCases);
293 }; 322 };
294 323
295 bool IsBannerVisible(Browser* browser) { 324 bool IsBannerVisible(Browser* browser) {
296 content::WebContents* contents = chrome::GetActiveWebContents(browser); 325 content::WebContents* contents = chrome::GetActiveWebContents(browser);
297 bool result = false; 326 bool result = false;
298 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool( 327 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool(
299 contents->GetRenderViewHost(), 328 contents->GetRenderViewHost(),
300 std::wstring(), 329 std::wstring(),
301 L"var visible = false;" 330 L"var visible = false;"
302 L"var banners = document.querySelectorAll('.page-banner');" 331 L"var banners = document.querySelectorAll('.page-banner');"
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 410
382 // Base class for tests that change policies. 411 // Base class for tests that change policies.
383 class PolicyBaseTest : public InProcessBrowserTest { 412 class PolicyBaseTest : public InProcessBrowserTest {
384 protected: 413 protected:
385 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 414 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
386 EXPECT_CALL(provider_, IsInitializationComplete()) 415 EXPECT_CALL(provider_, IsInitializationComplete())
387 .WillRepeatedly(Return(true)); 416 .WillRepeatedly(Return(true));
388 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); 417 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
389 } 418 }
390 419
391 TestCases test_cases_; 420 PolicyTestCases policy_test_cases_;
392 MockConfigurationPolicyProvider provider_; 421 MockConfigurationPolicyProvider provider_;
393 }; 422 };
394 423
395 // A class of tests that change policy and don't need parameters. 424 // A class of tests that change policy and don't need parameters.
396 class PolicyPrefsBannerTest : public PolicyBaseTest {}; 425 class PolicyPrefsBannerTest : public PolicyBaseTest {};
397 426
398 // A class of tests that change policy and are parameterized with a policy 427 // A class of tests that change policy and are parameterized with a policy
399 // definition. 428 // definition.
400 class PolicyPrefsTest 429 class PolicyPrefsTest
401 : public PolicyBaseTest, 430 : public PolicyBaseTest,
402 public testing::WithParamInterface<PolicyDefinitionList::Entry> {}; 431 public testing::WithParamInterface<PolicyDefinitionList::Entry> {};
403 432
404 TEST(PolicyPrefsTest, AllPoliciesHaveATestCase) { 433 TEST(PolicyPrefsTest, AllPoliciesHaveATestCase) {
405 // Verifies that all known policies have a test case in the JSON file. 434 // Verifies that all known policies have a test case in the JSON file.
406 // This test fails when a policy is added to 435 // This test fails when a policy is added to
407 // chrome/app/policy/policy_templates.json but a test case is not added to 436 // chrome/app/policy/policy_templates.json but a test case is not added to
408 // chrome/test/data/policy/policy_test_cases.json. 437 // chrome/test/data/policy/policy_test_cases.json.
409 TestCases test_cases; 438 PolicyTestCases policy_test_cases;
410 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); 439 const PolicyDefinitionList* list = GetChromePolicyDefinitionList();
411 for (const PolicyDefinitionList::Entry* policy = list->begin; 440 for (const PolicyDefinitionList::Entry* policy = list->begin;
412 policy != list->end; ++policy) { 441 policy != list->end; ++policy) {
413 EXPECT_TRUE(ContainsKey(test_cases.map(), policy->name)) 442 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), policy->name))
414 << "Missing policy test case for: " << policy->name; 443 << "Missing policy test case for: " << policy->name;
415 } 444 }
416 } 445 }
417 446
418 IN_PROC_BROWSER_TEST_P(PolicyPrefsSettingsBannerTest, NoPoliciesNoBanner) { 447 IN_PROC_BROWSER_TEST_P(PolicyPrefsSettingsBannerTest, NoPoliciesNoBanner) {
419 // Verifies that the banner isn't shown in the settings UI when no policies 448 // Verifies that the banner isn't shown in the settings UI when no policies
420 // are set. 449 // are set.
421 ui_test_utils::NavigateToURL(browser(), GURL(GetParam())); 450 ui_test_utils::NavigateToURL(browser(), GURL(GetParam()));
422 EXPECT_FALSE(IsBannerVisible(browser())); 451 EXPECT_FALSE(IsBannerVisible(browser()));
423 } 452 }
424 453
425 INSTANTIATE_TEST_CASE_P(PolicyPrefsSettingsBannerTestInstance, 454 INSTANTIATE_TEST_CASE_P(PolicyPrefsSettingsBannerTestInstance,
426 PolicyPrefsSettingsBannerTest, 455 PolicyPrefsSettingsBannerTest,
427 testing::ValuesIn(kSettingsPages)); 456 testing::ValuesIn(kSettingsPages));
428 457
429 IN_PROC_BROWSER_TEST_F(PolicyPrefsBannerTest, TogglePolicyTogglesBanner) { 458 IN_PROC_BROWSER_TEST_F(PolicyPrefsBannerTest, TogglePolicyTogglesBanner) {
430 // Verifies that the banner appears and disappears as policies are added and 459 // Verifies that the banner appears and disappears as policies are added and
431 // removed. 460 // removed.
432 // |test_case| is just a particular policy that should trigger the banner 461 // |test_case| is just a particular policy that should trigger the banner
433 // on the main settings page. 462 // on the main settings page.
434 const PolicyTestCase* test_case = test_cases_.Get("ShowHomeButton"); 463 const PolicyTestCase* test_case = policy_test_cases_.Get("ShowHomeButton");
435 ASSERT_TRUE(test_case); 464 ASSERT_TRUE(test_case);
436 // No banner by default. 465 // No banner by default.
437 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); 466 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0]));
438 EXPECT_FALSE(IsBannerVisible(browser())); 467 EXPECT_FALSE(IsBannerVisible(browser()));
439 // Adding a policy makes the banner show up. 468 // Adding a policy makes the banner show up.
440 provider_.UpdateChromePolicy(test_case->test_policy()); 469 provider_.UpdateChromePolicy(test_case->test_policy());
441 EXPECT_TRUE(IsBannerVisible(browser())); 470 EXPECT_TRUE(IsBannerVisible(browser()));
442 // And removing it makes the banner go away. 471 // And removing it makes the banner go away.
443 const PolicyMap kNoPolicies; 472 const PolicyMap kNoPolicies;
444 provider_.UpdateChromePolicy(kNoPolicies); 473 provider_.UpdateChromePolicy(kNoPolicies);
445 EXPECT_FALSE(IsBannerVisible(browser())); 474 EXPECT_FALSE(IsBannerVisible(browser()));
446 // Do it again, just in case. 475 // Do it again, just in case.
447 provider_.UpdateChromePolicy(test_case->test_policy()); 476 provider_.UpdateChromePolicy(test_case->test_policy());
448 EXPECT_TRUE(IsBannerVisible(browser())); 477 EXPECT_TRUE(IsBannerVisible(browser()));
449 provider_.UpdateChromePolicy(kNoPolicies); 478 provider_.UpdateChromePolicy(kNoPolicies);
450 EXPECT_FALSE(IsBannerVisible(browser())); 479 EXPECT_FALSE(IsBannerVisible(browser()));
451 } 480 }
452 481
453 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { 482 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) {
454 // Verifies that policies make their corresponding preferences become managed, 483 // Verifies that policies make their corresponding preferences become managed,
455 // and that the user can't override that setting. 484 // and that the user can't override that setting.
456 const PolicyTestCase* test_case = test_cases_.Get(GetParam().name); 485 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name);
457 ASSERT_TRUE(test_case); 486 ASSERT_TRUE(test_case);
458 if (!test_case->IsSupported() || test_case->pref().empty()) 487 const ScopedVector<PrefMapping>& pref_mappings = test_case->pref_mappings();
488 if (!test_case->IsSupported() || pref_mappings.empty())
459 return; 489 return;
460 LOG(INFO) << "Testing policy: " << test_case->name(); 490 LOG(INFO) << "Testing policy: " << test_case->name();
461 491
462 PrefService* prefs = test_case->is_local_state() ? 492 for (ScopedVector<PrefMapping>::const_iterator
463 g_browser_process->local_state() : browser()->profile()->GetPrefs(); 493 pref_mapping = pref_mappings.begin();
464 // The preference must have been registered. 494 pref_mapping != pref_mappings.end();
465 const PrefService::Preference* pref = 495 ++pref_mapping) {
466 prefs->FindPreference(test_case->pref_name()); 496 PrefService* prefs = (*pref_mapping)->is_local_state() ?
467 ASSERT_TRUE(pref); 497 g_browser_process->local_state() : browser()->profile()->GetPrefs();
468 prefs->ClearPref(test_case->pref_name()); 498 // The preference must have been registered.
499 const PrefService::Preference* pref =
500 prefs->FindPreference((*pref_mapping)->pref().c_str());
501 ASSERT_TRUE(pref);
502 prefs->ClearPref((*pref_mapping)->pref().c_str());
469 503
470 // Verify that setting the policy overrides the pref. 504 // Verify that setting the policy overrides the pref.
471 EXPECT_TRUE(pref->IsDefaultValue()); 505 const PolicyMap kNoPolicies;
472 EXPECT_TRUE(pref->IsUserModifiable()); 506 provider_.UpdateChromePolicy(kNoPolicies);
473 EXPECT_FALSE(pref->IsUserControlled()); 507 EXPECT_TRUE(pref->IsDefaultValue());
474 EXPECT_FALSE(pref->IsManaged()); 508 EXPECT_TRUE(pref->IsUserModifiable());
509 EXPECT_FALSE(pref->IsUserControlled());
510 EXPECT_FALSE(pref->IsManaged());
475 511
476 provider_.UpdateChromePolicy(test_case->test_policy()); 512 provider_.UpdateChromePolicy(test_case->test_policy());
477 EXPECT_FALSE(pref->IsDefaultValue()); 513 EXPECT_FALSE(pref->IsDefaultValue());
478 EXPECT_FALSE(pref->IsUserModifiable()); 514 EXPECT_FALSE(pref->IsUserModifiable());
479 EXPECT_FALSE(pref->IsUserControlled()); 515 EXPECT_FALSE(pref->IsUserControlled());
480 EXPECT_TRUE(pref->IsManaged()); 516 EXPECT_TRUE(pref->IsManaged());
517 }
481 } 518 }
482 519
483 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckAllPoliciesThatShowTheBanner) { 520 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckAllPoliciesThatShowTheBanner) {
484 // Verifies that the banner appears for each policy that affects a control 521 // Verifies that the banner appears for each policy that affects a control
485 // in the settings UI. 522 // in the settings UI.
486 const PolicyTestCase* test_case = test_cases_.Get(GetParam().name); 523 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name);
487 ASSERT_TRUE(test_case); 524 ASSERT_TRUE(test_case);
488 if (!test_case->IsSupported() || test_case->settings_pages().empty()) 525 if (!test_case->IsSupported() || test_case->settings_pages().empty())
489 return; 526 return;
490 LOG(INFO) << "Testing policy: " << test_case->name(); 527 LOG(INFO) << "Testing policy: " << test_case->name();
491 528
492 const std::vector<GURL>& pages = test_case->settings_pages(); 529 const std::vector<GURL>& pages = test_case->settings_pages();
493 for (size_t i = 0; i < pages.size(); ++i) { 530 for (size_t i = 0; i < pages.size(); ++i) {
494 ui_test_utils::NavigateToURL(browser(), pages[i]); 531 ui_test_utils::NavigateToURL(browser(), pages[i]);
495 EXPECT_FALSE(IsBannerVisible(browser())); 532 EXPECT_FALSE(IsBannerVisible(browser()));
496 provider_.UpdateChromePolicy(test_case->test_policy()); 533 provider_.UpdateChromePolicy(test_case->test_policy());
497 EXPECT_TRUE(IsBannerVisible(browser())); 534 EXPECT_TRUE(IsBannerVisible(browser()));
498 const PolicyMap kNoPolicies; 535 const PolicyMap kNoPolicies;
499 provider_.UpdateChromePolicy(kNoPolicies); 536 provider_.UpdateChromePolicy(kNoPolicies);
500 EXPECT_FALSE(IsBannerVisible(browser())); 537 EXPECT_FALSE(IsBannerVisible(browser()));
501 } 538 }
502 } 539 }
503 540
504 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { 541 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) {
505 // Verifies that controlled setting indicators correctly show whether a pref's 542 // Verifies that controlled setting indicators correctly show whether a pref's
506 // value is recommended or enforced by a corresponding policy. 543 // value is recommended or enforced by a corresponding policy.
507 const PolicyTestCase* policy_test_case = test_cases_.Get(GetParam().name); 544 const PolicyTestCase* policy_test_case =
545 policy_test_cases_.Get(GetParam().name);
508 ASSERT_TRUE(policy_test_case); 546 ASSERT_TRUE(policy_test_case);
509 const ScopedVector<IndicatorTestCase>& indicator_test_cases = 547 const ScopedVector<PrefMapping>& pref_mappings =
510 policy_test_case->indicator_test_cases(); 548 policy_test_case->pref_mappings();
511 if (!policy_test_case->IsSupported() || indicator_test_cases.empty()) 549 if (!policy_test_case->IsSupported() || pref_mappings.empty())
550 return;
551 bool has_indicator_tests = false;
552 for (ScopedVector<PrefMapping>::const_iterator
553 pref_mapping = pref_mappings.begin();
554 pref_mapping != pref_mappings.end();
555 ++pref_mapping) {
556 if (!(*pref_mapping)->indicator_test_cases().empty()) {
557 has_indicator_tests = true;
558 break;
559 }
560 }
561 if (!has_indicator_tests)
512 return; 562 return;
513 LOG(INFO) << "Testing policy: " << policy_test_case->name(); 563 LOG(INFO) << "Testing policy: " << policy_test_case->name();
514 564
515 PrefService* prefs = policy_test_case->is_local_state() ? 565 for (ScopedVector<PrefMapping>::const_iterator
516 g_browser_process->local_state() : browser()->profile()->GetPrefs(); 566 pref_mapping = pref_mappings.begin();
517 // The preference must have been registered. 567 pref_mapping != pref_mappings.end();
518 const PrefService::Preference* pref = 568 ++pref_mapping) {
519 prefs->FindPreference(policy_test_case->pref_name()); 569 const ScopedVector<IndicatorTestCase>&
520 ASSERT_TRUE(pref); 570 indicator_test_cases = (*pref_mapping)->indicator_test_cases();
571 if (indicator_test_cases.empty())
572 continue;
573 PrefService* prefs = (*pref_mapping)->is_local_state() ?
574 g_browser_process->local_state() : browser()->profile()->GetPrefs();
575 // The preference must have been registered.
576 const PrefService::Preference* pref =
577 prefs->FindPreference((*pref_mapping)->pref().c_str());
578 ASSERT_TRUE(pref);
521 579
522 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); 580 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0]));
523 if (!policy_test_case->indicator_test_setup_js().empty()) { 581 if (!(*pref_mapping)->indicator_test_setup_js().empty()) {
524 ASSERT_TRUE(content::ExecuteJavaScript( 582 ASSERT_TRUE(content::ExecuteJavaScript(
525 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"", 583 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"",
526 policy_test_case->indicator_test_setup_js())); 584 (*pref_mapping)->indicator_test_setup_js()));
527 } 585 }
528 586
529 std::string indicator_selector = policy_test_case->indicator_selector(); 587 std::string indicator_selector = (*pref_mapping)->indicator_selector();
530 if (indicator_selector.empty()) 588 if (indicator_selector.empty())
531 indicator_selector = "[pref=\"" + policy_test_case->pref() + "\"]"; 589 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]";
532 for (ScopedVector<IndicatorTestCase>::const_iterator 590 for (ScopedVector<IndicatorTestCase>::const_iterator
533 indicator_test_case = indicator_test_cases.begin(); 591 indicator_test_case = indicator_test_cases.begin();
534 indicator_test_case != indicator_test_cases.end(); 592 indicator_test_case != indicator_test_cases.end();
535 ++indicator_test_case) { 593 ++indicator_test_case) {
536 // Check that no controlled setting indicator is visible when no value is 594 // Check that no controlled setting indicator is visible when no value is
537 // set by policy. 595 // set by policy.
538 PolicyMap policies; 596 PolicyMap policies;
539 provider_.UpdateChromePolicy(policies); 597 provider_.UpdateChromePolicy(policies);
540 VerifyControlledSettingIndicators(browser(), indicator_selector, 598 VerifyControlledSettingIndicators(browser(), indicator_selector,
541 "", "", false); 599 "", "", false);
542 // Check that the appropriate controlled setting indicator is shown when a 600 // Check that the appropriate controlled setting indicator is shown when a
543 // value is enforced by policy. 601 // value is enforced by policy.
544 policies.LoadFrom(&(*indicator_test_case)->policy(), 602 policies.LoadFrom(&(*indicator_test_case)->policy(),
545 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 603 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
546 provider_.UpdateChromePolicy(policies); 604 provider_.UpdateChromePolicy(policies);
547 VerifyControlledSettingIndicators(browser(), indicator_selector, 605 VerifyControlledSettingIndicators(browser(), indicator_selector,
548 (*indicator_test_case)->value(), 606 (*indicator_test_case)->value(),
549 "policy", 607 "policy",
550 (*indicator_test_case)->readonly()); 608 (*indicator_test_case)->readonly());
551 if (!policy_test_case->can_be_recommended()) 609 if (!policy_test_case->can_be_recommended())
552 continue; 610 continue;
553 // Check that the appropriate controlled setting indicator is shown when a 611 // Check that the appropriate controlled setting indicator is shown when a
554 // value is recommended by policy and the user has not overridden the 612 // value is recommended by policy and the user has not overridden the
555 // recommendation. 613 // recommendation.
556 policies.LoadFrom(&(*indicator_test_case)->policy(), 614 policies.LoadFrom(&(*indicator_test_case)->policy(),
557 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); 615 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
558 provider_.UpdateChromePolicy(policies); 616 provider_.UpdateChromePolicy(policies);
559 VerifyControlledSettingIndicators(browser(), indicator_selector, 617 VerifyControlledSettingIndicators(browser(), indicator_selector,
560 (*indicator_test_case)->value(), 618 (*indicator_test_case)->value(),
561 "recommended", 619 "recommended",
562 (*indicator_test_case)->readonly()); 620 (*indicator_test_case)->readonly());
563 // Check that the appropriate controlled setting indicator is shown when a 621 // Check that the appropriate controlled setting indicator is shown when a
564 // value is recommended by policy and the user has overriddent the 622 // value is recommended by policy and the user has overriddent the
565 // recommendation. 623 // recommendation.
566 prefs->Set(policy_test_case->pref_name(), *pref->GetValue()); 624 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue());
567 VerifyControlledSettingIndicators(browser(), indicator_selector, 625 VerifyControlledSettingIndicators(browser(), indicator_selector,
568 (*indicator_test_case)->value(), 626 (*indicator_test_case)->value(),
569 "hasRecommendation", 627 "hasRecommendation",
570 (*indicator_test_case)->readonly()); 628 (*indicator_test_case)->readonly());
571 prefs->ClearPref(policy_test_case->pref_name()); 629 prefs->ClearPref((*pref_mapping)->pref().c_str());
630 }
572 } 631 }
573 } 632 }
574 633
575 INSTANTIATE_TEST_CASE_P( 634 INSTANTIATE_TEST_CASE_P(
576 PolicyPrefsTestInstance, 635 PolicyPrefsTestInstance,
577 PolicyPrefsTest, 636 PolicyPrefsTest,
578 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, 637 testing::ValuesIn(GetChromePolicyDefinitionList()->begin,
579 GetChromePolicyDefinitionList()->end)); 638 GetChromePolicyDefinitionList()->end));
580 639
581 } // namespace policy 640 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698