Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / policy / policy_prefs_browsertest.cc
blob2d86b96cfe1db3515a09d86dd9ddb01bc2fc32df
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
3 // found in the LICENSE file.
5 #include <algorithm>
6 #include <cstdlib>
7 #include <map>
8 #include <sstream>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/file_util.h"
14 #include "base/files/file_path.h"
15 #include "base/json/json_reader.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/scoped_vector.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/prefs/pref_service.h"
21 #include "base/run_loop.h"
22 #include "base/stl_util.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h"
25 #include "base/values.h"
26 #include "chrome/browser/browser_process.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/search_engines/template_url_service_factory.h"
29 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h"
33 #include "components/policy/core/browser/browser_policy_connector.h"
34 #include "components/policy/core/common/external_data_fetcher.h"
35 #include "components/policy/core/common/external_data_manager.h"
36 #include "components/policy/core/common/mock_configuration_policy_provider.h"
37 #include "components/policy/core/common/policy_details.h"
38 #include "components/policy/core/common/policy_map.h"
39 #include "components/policy/core/common/schema.h"
40 #include "content/public/browser/web_contents.h"
41 #include "content/public/test/browser_test_utils.h"
42 #include "policy/policy_constants.h"
43 #include "testing/gmock/include/gmock/gmock.h"
44 #include "testing/gtest/include/gtest/gtest.h"
45 #include "url/gurl.h"
47 using testing::Return;
48 using testing::_;
50 namespace policy {
52 namespace {
54 const char kMainSettingsPage[] = "chrome://settings-frame";
56 const char kCrosSettingsPrefix[] = "cros.";
58 // Contains the details of a single test case verifying that the controlled
59 // setting indicators for a pref affected by a policy work correctly. This is
60 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json.
61 class IndicatorTestCase {
62 public:
63 IndicatorTestCase(const base::DictionaryValue& policy,
64 const std::string& value,
65 bool readonly)
66 : policy_(policy.DeepCopy()), value_(value), readonly_(readonly) {}
67 ~IndicatorTestCase() {}
69 const base::DictionaryValue& policy() const { return *policy_; }
71 const std::string& value() const { return value_; }
73 bool readonly() const { return readonly_; }
75 private:
76 scoped_ptr<base::DictionaryValue> policy_;
77 std::string value_;
78 bool readonly_;
80 DISALLOW_COPY_AND_ASSIGN(IndicatorTestCase);
83 // Contains the testing details for a single pref affected by a policy. This is
84 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json.
85 class PrefMapping {
86 public:
87 PrefMapping(const std::string& pref,
88 bool is_local_state,
89 bool check_for_mandatory,
90 bool check_for_recommended,
91 const std::string& indicator_test_setup_js,
92 const std::string& indicator_selector)
93 : pref_(pref),
94 is_local_state_(is_local_state),
95 check_for_mandatory_(check_for_mandatory),
96 check_for_recommended_(check_for_recommended),
97 indicator_test_setup_js_(indicator_test_setup_js),
98 indicator_selector_(indicator_selector) {}
99 ~PrefMapping() {}
101 const std::string& pref() const { return pref_; }
103 bool is_local_state() const { return is_local_state_; }
105 bool check_for_mandatory() const { return check_for_mandatory_; }
107 bool check_for_recommended() const { return check_for_recommended_; }
109 const std::string& indicator_test_setup_js() const {
110 return indicator_test_setup_js_;
113 const std::string& indicator_selector() const {
114 return indicator_selector_;
117 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const {
118 return indicator_test_cases_;
120 void AddIndicatorTestCase(IndicatorTestCase* test_case) {
121 indicator_test_cases_.push_back(test_case);
124 private:
125 const std::string pref_;
126 const bool is_local_state_;
127 const bool check_for_mandatory_;
128 const bool check_for_recommended_;
129 const std::string indicator_test_setup_js_;
130 const std::string indicator_selector_;
131 ScopedVector<IndicatorTestCase> indicator_test_cases_;
133 DISALLOW_COPY_AND_ASSIGN(PrefMapping);
136 // Contains the testing details for a single policy. This is part of the data
137 // loaded from chrome/test/data/policy/policy_test_cases.json.
138 class PolicyTestCase {
139 public:
140 PolicyTestCase(const std::string& name,
141 bool is_official_only,
142 bool can_be_recommended,
143 const std::string& indicator_selector)
144 : name_(name),
145 is_official_only_(is_official_only),
146 can_be_recommended_(can_be_recommended),
147 indicator_selector_(indicator_selector) {}
148 ~PolicyTestCase() {}
150 const std::string& name() const { return name_; }
152 bool is_official_only() const { return is_official_only_; }
154 bool can_be_recommended() const { return can_be_recommended_; }
156 bool IsOsSupported() const {
157 #if defined(OS_WIN)
158 const std::string os("win");
159 #elif defined(OS_MACOSX)
160 const std::string os("mac");
161 #elif defined(OS_CHROMEOS)
162 const std::string os("chromeos");
163 #elif defined(OS_LINUX)
164 const std::string os("linux");
165 #else
166 #error "Unknown platform"
167 #endif
168 return std::find(supported_os_.begin(), supported_os_.end(), os) !=
169 supported_os_.end();
171 void AddSupportedOs(const std::string& os) { supported_os_.push_back(os); }
173 bool IsSupported() const {
174 #if !defined(OFFICIAL_BUILD)
175 if (is_official_only())
176 return false;
177 #endif
178 return IsOsSupported();
181 const base::DictionaryValue& test_policy() const { return test_policy_; }
182 void SetTestPolicy(const base::DictionaryValue& policy) {
183 test_policy_.Clear();
184 test_policy_.MergeDictionary(&policy);
187 const ScopedVector<PrefMapping>& pref_mappings() const {
188 return pref_mappings_;
190 void AddPrefMapping(PrefMapping* pref_mapping) {
191 pref_mappings_.push_back(pref_mapping);
194 const std::string& indicator_selector() const { return indicator_selector_; }
196 private:
197 std::string name_;
198 bool is_official_only_;
199 bool can_be_recommended_;
200 std::vector<std::string> supported_os_;
201 base::DictionaryValue test_policy_;
202 ScopedVector<PrefMapping> pref_mappings_;
203 std::string indicator_selector_;
205 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase);
208 // Parses all policy test cases and makes then available in a map.
209 class PolicyTestCases {
210 public:
211 typedef std::map<std::string, PolicyTestCase*> PolicyTestCaseMap;
212 typedef PolicyTestCaseMap::const_iterator iterator;
214 PolicyTestCases() {
215 base::FilePath path = ui_test_utils::GetTestFilePath(
216 base::FilePath(FILE_PATH_LITERAL("policy")),
217 base::FilePath(FILE_PATH_LITERAL("policy_test_cases.json")));
218 std::string json;
219 if (!base::ReadFileToString(path, &json)) {
220 ADD_FAILURE();
221 return;
223 int error_code = -1;
224 std::string error_string;
225 base::DictionaryValue* dict = NULL;
226 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
227 json, base::JSON_PARSE_RFC, &error_code, &error_string));
228 if (!value.get() || !value->GetAsDictionary(&dict)) {
229 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string;
230 return;
232 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
233 if (!chrome_schema.valid()) {
234 ADD_FAILURE();
235 return;
237 for (Schema::Iterator it = chrome_schema.GetPropertiesIterator();
238 !it.IsAtEnd(); it.Advance()) {
239 PolicyTestCase* policy_test_case = GetPolicyTestCase(dict, it.key());
240 if (policy_test_case)
241 policy_test_cases_[it.key()] = policy_test_case;
245 ~PolicyTestCases() {
246 STLDeleteValues(&policy_test_cases_);
249 const PolicyTestCase* Get(const std::string& name) const {
250 const iterator it = policy_test_cases_.find(name);
251 return it == end() ? NULL : it->second;
254 const PolicyTestCaseMap& map() const { return policy_test_cases_; }
255 iterator begin() const { return policy_test_cases_.begin(); }
256 iterator end() const { return policy_test_cases_.end(); }
258 private:
259 PolicyTestCase* GetPolicyTestCase(const base::DictionaryValue* tests,
260 const std::string& name) {
261 const base::DictionaryValue* policy_test_dict = NULL;
262 if (!tests->GetDictionary(name, &policy_test_dict))
263 return NULL;
264 bool is_official_only = false;
265 policy_test_dict->GetBoolean("official_only", &is_official_only);
266 bool can_be_recommended = false;
267 policy_test_dict->GetBoolean("can_be_recommended", &can_be_recommended);
268 std::string indicator_selector;
269 policy_test_dict->GetString("indicator_selector", &indicator_selector);
270 PolicyTestCase* policy_test_case = new PolicyTestCase(name,
271 is_official_only,
272 can_be_recommended,
273 indicator_selector);
274 const base::ListValue* os_list = NULL;
275 if (policy_test_dict->GetList("os", &os_list)) {
276 for (size_t i = 0; i < os_list->GetSize(); ++i) {
277 std::string os;
278 if (os_list->GetString(i, &os))
279 policy_test_case->AddSupportedOs(os);
282 const base::DictionaryValue* policy = NULL;
283 if (policy_test_dict->GetDictionary("test_policy", &policy))
284 policy_test_case->SetTestPolicy(*policy);
285 const base::ListValue* pref_mappings = NULL;
286 if (policy_test_dict->GetList("pref_mappings", &pref_mappings)) {
287 for (size_t i = 0; i < pref_mappings->GetSize(); ++i) {
288 const base::DictionaryValue* pref_mapping_dict = NULL;
289 std::string pref;
290 if (!pref_mappings->GetDictionary(i, &pref_mapping_dict) ||
291 !pref_mapping_dict->GetString("pref", &pref)) {
292 ADD_FAILURE() << "Malformed pref_mappings entry in "
293 << "policy_test_cases.json.";
294 continue;
296 bool is_local_state = false;
297 pref_mapping_dict->GetBoolean("local_state", &is_local_state);
298 bool check_for_mandatory = true;
299 pref_mapping_dict->GetBoolean("check_for_mandatory",
300 &check_for_mandatory);
301 bool check_for_recommended = true;
302 pref_mapping_dict->GetBoolean("check_for_recommended",
303 &check_for_recommended);
304 std::string indicator_test_setup_js;
305 pref_mapping_dict->GetString("indicator_test_setup_js",
306 &indicator_test_setup_js);
307 std::string indicator_selector;
308 pref_mapping_dict->GetString("indicator_selector", &indicator_selector);
309 PrefMapping* pref_mapping = new PrefMapping(pref,
310 is_local_state,
311 check_for_mandatory,
312 check_for_recommended,
313 indicator_test_setup_js,
314 indicator_selector);
315 const base::ListValue* indicator_tests = NULL;
316 if (pref_mapping_dict->GetList("indicator_tests", &indicator_tests)) {
317 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) {
318 const base::DictionaryValue* indicator_test_dict = NULL;
319 const base::DictionaryValue* policy = NULL;
320 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) ||
321 !indicator_test_dict->GetDictionary("policy", &policy)) {
322 ADD_FAILURE() << "Malformed indicator_tests entry in "
323 << "policy_test_cases.json.";
324 continue;
326 std::string value;
327 indicator_test_dict->GetString("value", &value);
328 bool readonly = false;
329 indicator_test_dict->GetBoolean("readonly", &readonly);
330 pref_mapping->AddIndicatorTestCase(
331 new IndicatorTestCase(*policy, value, readonly));
334 policy_test_case->AddPrefMapping(pref_mapping);
337 return policy_test_case;
340 PolicyTestCaseMap policy_test_cases_;
342 DISALLOW_COPY_AND_ASSIGN(PolicyTestCases);
345 // Returns a pseudo-random integer distributed in [0, range).
346 int GetRandomNumber(int range) {
347 return rand() % range;
350 // Splits all known policies into subsets of the given |chunk_size|. The
351 // policies are shuffled so that there is no correlation between their initial
352 // alphabetic ordering and the assignment to chunks. This ensures that the
353 // expected number of policies with long-running test cases is equal for each
354 // subset. The shuffle algorithm uses a fixed seed, ensuring that no randomness
355 // is introduced into the testing process.
356 std::vector<std::vector<std::string> > SplitPoliciesIntoChunks(int chunk_size) {
357 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
358 if (!chrome_schema.valid())
359 ADD_FAILURE();
361 std::vector<std::string> policies;
362 for (Schema::Iterator it = chrome_schema.GetPropertiesIterator();
363 !it.IsAtEnd(); it.Advance()) {
364 policies.push_back(it.key());
367 // Use a fixed random seed to obtain a reproducible shuffle.
368 srand(1);
369 std::random_shuffle(policies.begin(), policies.end(), GetRandomNumber);
371 std::vector<std::vector<std::string> > chunks;
372 std::vector<std::string>::const_iterator it = policies.begin();
373 const std::vector<std::string>::const_iterator end = policies.end();
374 for ( ; end - it >= chunk_size; it += chunk_size)
375 chunks.push_back(std::vector<std::string>(it, it + chunk_size));
376 if (it != end)
377 chunks.push_back(std::vector<std::string>(it, end));
378 return chunks;
381 void VerifyControlledSettingIndicators(Browser* browser,
382 const std::string& selector,
383 const std::string& value,
384 const std::string& controlled_by,
385 bool readonly) {
386 std::stringstream javascript;
387 javascript << "var nodes = document.querySelectorAll("
388 << " 'span.controlled-setting-indicator"
389 << selector.c_str() << "');"
390 << "var indicators = [];"
391 << "for (var i = 0; i < nodes.length; i++) {"
392 << " var node = nodes[i];"
393 << " var indicator = {};"
394 << " indicator.value = node.value || '';"
395 << " indicator.controlledBy = node.controlledBy || '';"
396 << " indicator.readOnly = node.readOnly || false;"
397 << " indicator.visible ="
398 << " window.getComputedStyle(node).display != 'none';"
399 << " indicators.push(indicator)"
400 << "}"
401 << "domAutomationController.send(JSON.stringify(indicators));";
402 content::WebContents* contents =
403 browser->tab_strip_model()->GetActiveWebContents();
404 std::string json;
405 // Retrieve the state of all controlled setting indicators matching the
406 // |selector| as JSON.
407 ASSERT_TRUE(content::ExecuteScriptAndExtractString(contents, javascript.str(),
408 &json));
409 scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json));
410 const base::ListValue* indicators = NULL;
411 ASSERT_TRUE(value_ptr.get());
412 ASSERT_TRUE(value_ptr->GetAsList(&indicators));
413 // Verify that controlled setting indicators representing |value| are visible
414 // and have the correct state while those not representing |value| are
415 // invisible.
416 if (!controlled_by.empty()) {
417 EXPECT_GT(indicators->GetSize(), 0u)
418 << "Expected to find at least one controlled setting indicator.";
420 bool have_visible_indicators = false;
421 for (base::ListValue::const_iterator indicator = indicators->begin();
422 indicator != indicators->end(); ++indicator) {
423 const base::DictionaryValue* properties = NULL;
424 ASSERT_TRUE((*indicator)->GetAsDictionary(&properties));
425 std::string indicator_value;
426 std::string indicator_controlled_by;
427 bool indicator_readonly;
428 bool indicator_visible;
429 EXPECT_TRUE(properties->GetString("value", &indicator_value));
430 EXPECT_TRUE(properties->GetString("controlledBy",
431 &indicator_controlled_by));
432 EXPECT_TRUE(properties->GetBoolean("readOnly", &indicator_readonly));
433 EXPECT_TRUE(properties->GetBoolean("visible", &indicator_visible));
434 if (!controlled_by.empty() && (indicator_value == value)) {
435 EXPECT_EQ(controlled_by, indicator_controlled_by);
436 EXPECT_EQ(readonly, indicator_readonly);
437 EXPECT_TRUE(indicator_visible);
438 have_visible_indicators = true;
439 } else {
440 EXPECT_FALSE(indicator_visible);
443 if (!controlled_by.empty()) {
444 EXPECT_TRUE(have_visible_indicators)
445 << "Expected to find at least one visible controlled setting "
446 << "indicator.";
450 } // namespace
452 typedef InProcessBrowserTest PolicyPrefsTestCoverageTest;
454 IN_PROC_BROWSER_TEST_F(PolicyPrefsTestCoverageTest, AllPoliciesHaveATestCase) {
455 // Verifies that all known policies have a test case in the JSON file.
456 // This test fails when a policy is added to
457 // components/policy/resources/policy_templates.json but a test case is not
458 // added to chrome/test/data/policy/policy_test_cases.json.
459 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
460 ASSERT_TRUE(chrome_schema.valid());
462 PolicyTestCases policy_test_cases;
463 for (Schema::Iterator it = chrome_schema.GetPropertiesIterator();
464 !it.IsAtEnd(); it.Advance()) {
465 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), it.key()))
466 << "Missing policy test case for: " << it.key();
470 // Base class for tests that change policy.
471 class PolicyPrefsTest : public InProcessBrowserTest {
472 protected:
473 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
474 EXPECT_CALL(provider_, IsInitializationComplete(_))
475 .WillRepeatedly(Return(true));
476 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
479 virtual void SetUpOnMainThread() OVERRIDE {
480 ui_test_utils::WaitForTemplateURLServiceToLoad(
481 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
484 virtual void TearDownOnMainThread() OVERRIDE {
485 ClearProviderPolicy();
488 void ClearProviderPolicy() {
489 provider_.UpdateChromePolicy(PolicyMap());
490 base::RunLoop().RunUntilIdle();
493 void SetProviderPolicy(const base::DictionaryValue& policies,
494 PolicyLevel level) {
495 PolicyMap policy_map;
496 for (base::DictionaryValue::Iterator it(policies);
497 !it.IsAtEnd(); it.Advance()) {
498 const PolicyDetails* policy_details = GetChromePolicyDetails(it.key());
499 ASSERT_TRUE(policy_details);
500 policy_map.Set(
501 it.key(),
502 level,
503 POLICY_SCOPE_USER,
504 it.value().DeepCopy(),
505 policy_details->max_external_data_size ?
506 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(),
507 it.key()) :
508 NULL);
510 provider_.UpdateChromePolicy(policy_map);
511 base::RunLoop().RunUntilIdle();
514 MockConfigurationPolicyProvider provider_;
517 // Verifies that policies make their corresponding preferences become managed,
518 // and that the user can't override that setting.
519 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, PolicyToPrefsMapping) {
520 PrefService* local_state = g_browser_process->local_state();
521 PrefService* user_prefs = browser()->profile()->GetPrefs();
523 const PolicyTestCases test_cases;
524 for (PolicyTestCases::iterator it = test_cases.begin();
525 it != test_cases.end(); ++it) {
526 const ScopedVector<PrefMapping>& pref_mappings =
527 it->second->pref_mappings();
528 if (!it->second->IsSupported() || pref_mappings.empty())
529 continue;
531 LOG(INFO) << "Testing policy: " << it->first;
533 for (ScopedVector<PrefMapping>::const_iterator
534 pref_mapping = pref_mappings.begin();
535 pref_mapping != pref_mappings.end();
536 ++pref_mapping) {
537 // Skip Chrome OS preferences that use a different backend and cannot be
538 // retrieved through the prefs mechanism.
539 if (StartsWithASCII((*pref_mapping)->pref(), kCrosSettingsPrefix, true))
540 continue;
542 // Skip preferences that should not be checked when the policy is set to
543 // a mandatory value.
544 if (!(*pref_mapping)->check_for_mandatory())
545 continue;
547 PrefService* prefs = (*pref_mapping)->is_local_state() ?
548 local_state : user_prefs;
549 // The preference must have been registered.
550 const PrefService::Preference* pref =
551 prefs->FindPreference((*pref_mapping)->pref().c_str());
552 ASSERT_TRUE(pref);
554 // Verify that setting the policy overrides the pref.
555 ClearProviderPolicy();
556 prefs->ClearPref((*pref_mapping)->pref().c_str());
557 EXPECT_TRUE(pref->IsDefaultValue());
558 EXPECT_TRUE(pref->IsUserModifiable());
559 EXPECT_FALSE(pref->IsUserControlled());
560 EXPECT_FALSE(pref->IsManaged());
562 SetProviderPolicy(it->second->test_policy(), POLICY_LEVEL_MANDATORY);
563 EXPECT_FALSE(pref->IsDefaultValue());
564 EXPECT_FALSE(pref->IsUserModifiable());
565 EXPECT_FALSE(pref->IsUserControlled());
566 EXPECT_TRUE(pref->IsManaged());
571 class PolicyPrefIndicatorTest
572 : public PolicyPrefsTest,
573 public testing::WithParamInterface<std::vector<std::string> > {
576 // Verifies that controlled setting indicators correctly show whether a pref's
577 // value is recommended or enforced by a corresponding policy.
578 IN_PROC_BROWSER_TEST_P(PolicyPrefIndicatorTest, CheckPolicyIndicators) {
579 const PolicyTestCases test_cases;
580 PrefService* local_state = g_browser_process->local_state();
581 PrefService* user_prefs = browser()->profile()->GetPrefs();
583 ui_test_utils::NavigateToURL(browser(), GURL(kMainSettingsPage));
585 for (std::vector<std::string>::const_iterator it = GetParam().begin();
586 it != GetParam().end(); ++it) {
587 const PolicyTestCase* policy_test_case = test_cases.Get(*it);
588 ASSERT_TRUE(policy_test_case) << "PolicyTestCase not found for " << *it;
589 if (!policy_test_case->IsSupported())
590 continue;
591 const ScopedVector<PrefMapping>& pref_mappings =
592 policy_test_case->pref_mappings();
593 if (policy_test_case->indicator_selector().empty()) {
594 bool has_pref_indicator_tests = false;
595 for (ScopedVector<PrefMapping>::const_iterator
596 pref_mapping = pref_mappings.begin();
597 pref_mapping != pref_mappings.end();
598 ++pref_mapping) {
599 if (!(*pref_mapping)->indicator_test_cases().empty()) {
600 has_pref_indicator_tests = true;
601 break;
604 if (!has_pref_indicator_tests)
605 continue;
608 LOG(INFO) << "Testing policy: " << *it;
610 if (!policy_test_case->indicator_selector().empty()) {
611 // Check that no controlled setting indicator is visible when no value is
612 // set by policy.
613 ClearProviderPolicy();
614 VerifyControlledSettingIndicators(browser(),
615 policy_test_case->indicator_selector(),
616 std::string(),
617 std::string(),
618 false);
619 // Check that the appropriate controlled setting indicator is shown when a
620 // value is enforced by policy.
621 SetProviderPolicy(policy_test_case->test_policy(),
622 POLICY_LEVEL_MANDATORY);
623 VerifyControlledSettingIndicators(browser(),
624 policy_test_case->indicator_selector(),
625 std::string(),
626 "policy",
627 false);
630 for (ScopedVector<PrefMapping>::const_iterator
631 pref_mapping = pref_mappings.begin();
632 pref_mapping != pref_mappings.end();
633 ++pref_mapping) {
634 const ScopedVector<IndicatorTestCase>&
635 indicator_test_cases = (*pref_mapping)->indicator_test_cases();
636 if (indicator_test_cases.empty())
637 continue;
639 if (!(*pref_mapping)->indicator_test_setup_js().empty()) {
640 ASSERT_TRUE(content::ExecuteScript(
641 browser()->tab_strip_model()->GetActiveWebContents(),
642 (*pref_mapping)->indicator_test_setup_js()));
645 std::string indicator_selector = (*pref_mapping)->indicator_selector();
646 if (indicator_selector.empty())
647 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]";
648 for (ScopedVector<IndicatorTestCase>::const_iterator
649 indicator_test_case = indicator_test_cases.begin();
650 indicator_test_case != indicator_test_cases.end();
651 ++indicator_test_case) {
652 // Check that no controlled setting indicator is visible when no value
653 // is set by policy.
654 ClearProviderPolicy();
655 VerifyControlledSettingIndicators(
656 browser(), indicator_selector, std::string(), std::string(), false);
658 if ((*pref_mapping)->check_for_mandatory()) {
659 // Check that the appropriate controlled setting indicator is shown
660 // when a value is enforced by policy.
661 SetProviderPolicy((*indicator_test_case)->policy(),
662 POLICY_LEVEL_MANDATORY);
664 VerifyControlledSettingIndicators(browser(),
665 indicator_selector,
666 (*indicator_test_case)->value(),
667 "policy",
668 (*indicator_test_case)->readonly());
671 if (!policy_test_case->can_be_recommended() ||
672 !(*pref_mapping)->check_for_recommended()) {
673 continue;
676 PrefService* prefs = (*pref_mapping)->is_local_state() ?
677 local_state : user_prefs;
678 // The preference must have been registered.
679 const PrefService::Preference* pref =
680 prefs->FindPreference((*pref_mapping)->pref().c_str());
681 ASSERT_TRUE(pref);
683 // Check that the appropriate controlled setting indicator is shown when
684 // a value is recommended by policy and the user has not overridden the
685 // recommendation.
686 SetProviderPolicy((*indicator_test_case)->policy(),
687 POLICY_LEVEL_RECOMMENDED);
688 VerifyControlledSettingIndicators(browser(), indicator_selector,
689 (*indicator_test_case)->value(),
690 "recommended",
691 (*indicator_test_case)->readonly());
692 // Check that the appropriate controlled setting indicator is shown when
693 // a value is recommended by policy and the user has overridden the
694 // recommendation.
695 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue());
696 VerifyControlledSettingIndicators(browser(), indicator_selector,
697 (*indicator_test_case)->value(),
698 "hasRecommendation",
699 (*indicator_test_case)->readonly());
700 prefs->ClearPref((*pref_mapping)->pref().c_str());
706 INSTANTIATE_TEST_CASE_P(PolicyPrefIndicatorTestInstance,
707 PolicyPrefIndicatorTest,
708 testing::ValuesIn(SplitPoliciesIntoChunks(10)));
710 } // namespace policy