Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / policy / policy_prefs_browsertest.cc
blobcfbd3c8029418ef7bdd75adfedd1b48aa080842d
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/files/file_path.h"
14 #include "base/files/file_util.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/policy_types.h"
40 #include "components/policy/core/common/schema.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/test/browser_test_utils.h"
43 #include "policy/policy_constants.h"
44 #include "testing/gmock/include/gmock/gmock.h"
45 #include "testing/gtest/include/gtest/gtest.h"
46 #include "url/gurl.h"
48 using testing::Return;
49 using testing::_;
51 namespace policy {
53 namespace {
55 const char kMainSettingsPage[] = "chrome://settings-frame";
57 const char kCrosSettingsPrefix[] = "cros.";
59 std::string GetPolicyName(const std::string& policy_name_decorated) {
60 const size_t offset = policy_name_decorated.find('.');
61 if (offset != std::string::npos)
62 return policy_name_decorated.substr(0, offset);
63 return policy_name_decorated;
66 // Contains the details of a single test case verifying that the controlled
67 // setting indicators for a pref affected by a policy work correctly. This is
68 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json.
69 class IndicatorTestCase {
70 public:
71 IndicatorTestCase(const base::DictionaryValue& policy,
72 const std::string& value,
73 bool readonly)
74 : policy_(policy.DeepCopy()), value_(value), readonly_(readonly) {}
75 ~IndicatorTestCase() {}
77 const base::DictionaryValue& policy() const { return *policy_; }
79 const std::string& value() const { return value_; }
81 bool readonly() const { return readonly_; }
83 private:
84 scoped_ptr<base::DictionaryValue> policy_;
85 std::string value_;
86 bool readonly_;
88 DISALLOW_COPY_AND_ASSIGN(IndicatorTestCase);
91 // Contains the testing details for a single pref affected by a policy. This is
92 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json.
93 class PrefMapping {
94 public:
95 PrefMapping(const std::string& pref,
96 bool is_local_state,
97 bool check_for_mandatory,
98 bool check_for_recommended,
99 const std::string& indicator_test_setup_js,
100 const std::string& indicator_selector)
101 : pref_(pref),
102 is_local_state_(is_local_state),
103 check_for_mandatory_(check_for_mandatory),
104 check_for_recommended_(check_for_recommended),
105 indicator_test_setup_js_(indicator_test_setup_js),
106 indicator_selector_(indicator_selector) {}
107 ~PrefMapping() {}
109 const std::string& pref() const { return pref_; }
111 bool is_local_state() const { return is_local_state_; }
113 bool check_for_mandatory() const { return check_for_mandatory_; }
115 bool check_for_recommended() const { return check_for_recommended_; }
117 const std::string& indicator_test_setup_js() const {
118 return indicator_test_setup_js_;
121 const std::string& indicator_selector() const {
122 return indicator_selector_;
125 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const {
126 return indicator_test_cases_;
128 void AddIndicatorTestCase(IndicatorTestCase* test_case) {
129 indicator_test_cases_.push_back(test_case);
132 private:
133 const std::string pref_;
134 const bool is_local_state_;
135 const bool check_for_mandatory_;
136 const bool check_for_recommended_;
137 const std::string indicator_test_setup_js_;
138 const std::string indicator_selector_;
139 ScopedVector<IndicatorTestCase> indicator_test_cases_;
141 DISALLOW_COPY_AND_ASSIGN(PrefMapping);
144 // Contains the testing details for a single policy. This is part of the data
145 // loaded from chrome/test/data/policy/policy_test_cases.json.
146 class PolicyTestCase {
147 public:
148 PolicyTestCase(const std::string& name,
149 bool is_official_only,
150 bool can_be_recommended,
151 const std::string& indicator_selector)
152 : name_(name),
153 is_official_only_(is_official_only),
154 can_be_recommended_(can_be_recommended),
155 indicator_selector_(indicator_selector) {}
156 ~PolicyTestCase() {}
158 const std::string& name() const { return name_; }
160 bool is_official_only() const { return is_official_only_; }
162 bool can_be_recommended() const { return can_be_recommended_; }
164 bool IsOsSupported() const {
165 #if defined(OS_WIN)
166 const std::string os("win");
167 #elif defined(OS_MACOSX)
168 const std::string os("mac");
169 #elif defined(OS_CHROMEOS)
170 const std::string os("chromeos");
171 #elif defined(OS_LINUX)
172 const std::string os("linux");
173 #else
174 #error "Unknown platform"
175 #endif
176 return std::find(supported_os_.begin(), supported_os_.end(), os) !=
177 supported_os_.end();
179 void AddSupportedOs(const std::string& os) { supported_os_.push_back(os); }
181 bool IsSupported() const {
182 #if !defined(GOOGLE_CHROME_BUILD)
183 if (is_official_only())
184 return false;
185 #endif
186 return IsOsSupported();
189 const base::DictionaryValue& test_policy() const { return test_policy_; }
190 void SetTestPolicy(const base::DictionaryValue& policy) {
191 test_policy_.Clear();
192 test_policy_.MergeDictionary(&policy);
195 const ScopedVector<PrefMapping>& pref_mappings() const {
196 return pref_mappings_;
198 void AddPrefMapping(PrefMapping* pref_mapping) {
199 pref_mappings_.push_back(pref_mapping);
202 const std::string& indicator_selector() const { return indicator_selector_; }
204 private:
205 std::string name_;
206 bool is_official_only_;
207 bool can_be_recommended_;
208 std::vector<std::string> supported_os_;
209 base::DictionaryValue test_policy_;
210 ScopedVector<PrefMapping> pref_mappings_;
211 std::string indicator_selector_;
213 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase);
216 // Parses all policy test cases and makes then available in a map.
217 class PolicyTestCases {
218 public:
219 typedef std::vector<PolicyTestCase*> PolicyTestCaseVector;
220 typedef std::map<std::string, PolicyTestCaseVector> PolicyTestCaseMap;
221 typedef PolicyTestCaseMap::const_iterator iterator;
223 PolicyTestCases() {
224 base::FilePath path = ui_test_utils::GetTestFilePath(
225 base::FilePath(FILE_PATH_LITERAL("policy")),
226 base::FilePath(FILE_PATH_LITERAL("policy_test_cases.json")));
227 std::string json;
228 if (!base::ReadFileToString(path, &json)) {
229 ADD_FAILURE();
230 return;
232 int error_code = -1;
233 std::string error_string;
234 base::DictionaryValue* dict = NULL;
235 scoped_ptr<base::Value> value = base::JSONReader::ReadAndReturnError(
236 json, base::JSON_PARSE_RFC, &error_code, &error_string);
237 if (!value.get() || !value->GetAsDictionary(&dict)) {
238 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string;
239 return;
241 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
242 if (!chrome_schema.valid()) {
243 ADD_FAILURE();
244 return;
246 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
247 it.Advance()) {
248 const std::string policy_name = GetPolicyName(it.key());
249 if (!chrome_schema.GetKnownProperty(policy_name).valid())
250 continue;
251 PolicyTestCase* policy_test_case = GetPolicyTestCase(dict, it.key());
252 if (policy_test_case)
253 policy_test_cases_[policy_name].push_back(policy_test_case);
257 ~PolicyTestCases() {
258 for (iterator policy = policy_test_cases_.begin();
259 policy != policy_test_cases_.end();
260 ++policy) {
261 for (PolicyTestCaseVector::const_iterator test_case =
262 policy->second.begin();
263 test_case != policy->second.end();
264 ++test_case) {
265 delete *test_case;
270 const PolicyTestCaseVector* Get(const std::string& name) const {
271 const iterator it = policy_test_cases_.find(name);
272 return it == end() ? NULL : &it->second;
275 const PolicyTestCaseMap& map() const { return policy_test_cases_; }
276 iterator begin() const { return policy_test_cases_.begin(); }
277 iterator end() const { return policy_test_cases_.end(); }
279 private:
280 PolicyTestCase* GetPolicyTestCase(const base::DictionaryValue* tests,
281 const std::string& name) {
282 const base::DictionaryValue* policy_test_dict = NULL;
283 if (!tests->GetDictionaryWithoutPathExpansion(name, &policy_test_dict))
284 return NULL;
285 bool is_official_only = false;
286 policy_test_dict->GetBoolean("official_only", &is_official_only);
287 bool can_be_recommended = false;
288 policy_test_dict->GetBoolean("can_be_recommended", &can_be_recommended);
289 std::string indicator_selector;
290 policy_test_dict->GetString("indicator_selector", &indicator_selector);
291 PolicyTestCase* policy_test_case = new PolicyTestCase(name,
292 is_official_only,
293 can_be_recommended,
294 indicator_selector);
295 const base::ListValue* os_list = NULL;
296 if (policy_test_dict->GetList("os", &os_list)) {
297 for (size_t i = 0; i < os_list->GetSize(); ++i) {
298 std::string os;
299 if (os_list->GetString(i, &os))
300 policy_test_case->AddSupportedOs(os);
303 const base::DictionaryValue* policy = NULL;
304 if (policy_test_dict->GetDictionary("test_policy", &policy))
305 policy_test_case->SetTestPolicy(*policy);
306 const base::ListValue* pref_mappings = NULL;
307 if (policy_test_dict->GetList("pref_mappings", &pref_mappings)) {
308 for (size_t i = 0; i < pref_mappings->GetSize(); ++i) {
309 const base::DictionaryValue* pref_mapping_dict = NULL;
310 std::string pref;
311 if (!pref_mappings->GetDictionary(i, &pref_mapping_dict) ||
312 !pref_mapping_dict->GetString("pref", &pref)) {
313 ADD_FAILURE() << "Malformed pref_mappings entry in "
314 << "policy_test_cases.json.";
315 continue;
317 bool is_local_state = false;
318 pref_mapping_dict->GetBoolean("local_state", &is_local_state);
319 bool check_for_mandatory = true;
320 pref_mapping_dict->GetBoolean("check_for_mandatory",
321 &check_for_mandatory);
322 bool check_for_recommended = true;
323 pref_mapping_dict->GetBoolean("check_for_recommended",
324 &check_for_recommended);
325 std::string indicator_test_setup_js;
326 pref_mapping_dict->GetString("indicator_test_setup_js",
327 &indicator_test_setup_js);
328 std::string indicator_selector;
329 pref_mapping_dict->GetString("indicator_selector", &indicator_selector);
330 PrefMapping* pref_mapping = new PrefMapping(pref,
331 is_local_state,
332 check_for_mandatory,
333 check_for_recommended,
334 indicator_test_setup_js,
335 indicator_selector);
336 const base::ListValue* indicator_tests = NULL;
337 if (pref_mapping_dict->GetList("indicator_tests", &indicator_tests)) {
338 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) {
339 const base::DictionaryValue* indicator_test_dict = NULL;
340 const base::DictionaryValue* policy = NULL;
341 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) ||
342 !indicator_test_dict->GetDictionary("policy", &policy)) {
343 ADD_FAILURE() << "Malformed indicator_tests entry in "
344 << "policy_test_cases.json.";
345 continue;
347 std::string value;
348 indicator_test_dict->GetString("value", &value);
349 bool readonly = false;
350 indicator_test_dict->GetBoolean("readonly", &readonly);
351 pref_mapping->AddIndicatorTestCase(
352 new IndicatorTestCase(*policy, value, readonly));
355 policy_test_case->AddPrefMapping(pref_mapping);
358 return policy_test_case;
361 PolicyTestCaseMap policy_test_cases_;
363 DISALLOW_COPY_AND_ASSIGN(PolicyTestCases);
366 // Returns a pseudo-random integer distributed in [0, range).
367 int GetRandomNumber(int range) {
368 return rand() % range;
371 // Splits all known policies into subsets of the given |chunk_size|. The
372 // policies are shuffled so that there is no correlation between their initial
373 // alphabetic ordering and the assignment to chunks. This ensures that the
374 // expected number of policies with long-running test cases is equal for each
375 // subset. The shuffle algorithm uses a fixed seed, ensuring that no randomness
376 // is introduced into the testing process.
377 std::vector<std::vector<std::string> > SplitPoliciesIntoChunks(int chunk_size) {
378 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
379 if (!chrome_schema.valid())
380 ADD_FAILURE();
382 std::vector<std::string> policies;
383 for (Schema::Iterator it = chrome_schema.GetPropertiesIterator();
384 !it.IsAtEnd(); it.Advance()) {
385 policies.push_back(it.key());
388 // Use a fixed random seed to obtain a reproducible shuffle.
389 srand(1);
390 std::random_shuffle(policies.begin(), policies.end(), GetRandomNumber);
392 std::vector<std::vector<std::string> > chunks;
393 std::vector<std::string>::const_iterator it = policies.begin();
394 const std::vector<std::string>::const_iterator end = policies.end();
395 for ( ; end - it >= chunk_size; it += chunk_size)
396 chunks.push_back(std::vector<std::string>(it, it + chunk_size));
397 if (it != end)
398 chunks.push_back(std::vector<std::string>(it, end));
399 return chunks;
402 void VerifyControlledSettingIndicators(Browser* browser,
403 const std::string& selector,
404 const std::string& value,
405 const std::string& controlled_by,
406 bool readonly) {
407 std::stringstream javascript;
408 javascript << "var nodes = document.querySelectorAll("
409 << " 'span.controlled-setting-indicator"
410 << selector.c_str() << "');"
411 << "var indicators = [];"
412 << "for (var i = 0; i < nodes.length; i++) {"
413 << " var node = nodes[i];"
414 << " var indicator = {};"
415 << " indicator.value = node.value || '';"
416 << " indicator.controlledBy = node.controlledBy || '';"
417 << " indicator.readOnly = node.readOnly || false;"
418 << " indicator.visible ="
419 << " window.getComputedStyle(node).display != 'none';"
420 << " indicators.push(indicator)"
421 << "}"
422 << "domAutomationController.send(JSON.stringify(indicators));";
423 content::WebContents* contents =
424 browser->tab_strip_model()->GetActiveWebContents();
425 std::string json;
426 // Retrieve the state of all controlled setting indicators matching the
427 // |selector| as JSON.
428 ASSERT_TRUE(content::ExecuteScriptAndExtractString(contents, javascript.str(),
429 &json));
430 scoped_ptr<base::Value> value_ptr = base::JSONReader::Read(json);
431 const base::ListValue* indicators = NULL;
432 ASSERT_TRUE(value_ptr.get());
433 ASSERT_TRUE(value_ptr->GetAsList(&indicators));
434 // Verify that controlled setting indicators representing |value| are visible
435 // and have the correct state while those not representing |value| are
436 // invisible.
437 if (!controlled_by.empty()) {
438 EXPECT_GT(indicators->GetSize(), 0u)
439 << "Expected to find at least one controlled setting indicator.";
441 bool have_visible_indicators = false;
442 for (base::ListValue::const_iterator indicator = indicators->begin();
443 indicator != indicators->end(); ++indicator) {
444 const base::DictionaryValue* properties = NULL;
445 ASSERT_TRUE((*indicator)->GetAsDictionary(&properties));
446 std::string indicator_value;
447 std::string indicator_controlled_by;
448 bool indicator_readonly;
449 bool indicator_visible;
450 EXPECT_TRUE(properties->GetString("value", &indicator_value));
451 EXPECT_TRUE(properties->GetString("controlledBy",
452 &indicator_controlled_by));
453 EXPECT_TRUE(properties->GetBoolean("readOnly", &indicator_readonly));
454 EXPECT_TRUE(properties->GetBoolean("visible", &indicator_visible));
455 if (!controlled_by.empty() && (indicator_value == value)) {
456 EXPECT_EQ(controlled_by, indicator_controlled_by);
457 EXPECT_EQ(readonly, indicator_readonly);
458 EXPECT_TRUE(indicator_visible);
459 have_visible_indicators = true;
460 } else {
461 EXPECT_FALSE(indicator_visible);
464 if (!controlled_by.empty()) {
465 EXPECT_TRUE(have_visible_indicators)
466 << "Expected to find at least one visible controlled setting "
467 << "indicator.";
471 } // namespace
473 typedef InProcessBrowserTest PolicyPrefsTestCoverageTest;
475 IN_PROC_BROWSER_TEST_F(PolicyPrefsTestCoverageTest, AllPoliciesHaveATestCase) {
476 // Verifies that all known policies have a test case in the JSON file.
477 // This test fails when a policy is added to
478 // components/policy/resources/policy_templates.json but a test case is not
479 // added to chrome/test/data/policy/policy_test_cases.json.
480 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
481 ASSERT_TRUE(chrome_schema.valid());
483 PolicyTestCases policy_test_cases;
484 for (Schema::Iterator it = chrome_schema.GetPropertiesIterator();
485 !it.IsAtEnd(); it.Advance()) {
486 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), it.key()))
487 << "Missing policy test case for: " << it.key();
491 // Base class for tests that change policy.
492 class PolicyPrefsTest : public InProcessBrowserTest {
493 protected:
494 void SetUpInProcessBrowserTestFixture() override {
495 EXPECT_CALL(provider_, IsInitializationComplete(_))
496 .WillRepeatedly(Return(true));
497 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
500 void SetUpOnMainThread() override {
501 ui_test_utils::WaitForTemplateURLServiceToLoad(
502 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
505 void TearDownOnMainThread() override { ClearProviderPolicy(); }
507 void ClearProviderPolicy() {
508 provider_.UpdateChromePolicy(PolicyMap());
509 base::RunLoop().RunUntilIdle();
512 void SetProviderPolicy(const base::DictionaryValue& policies,
513 PolicyLevel level) {
514 PolicyMap policy_map;
515 for (base::DictionaryValue::Iterator it(policies);
516 !it.IsAtEnd(); it.Advance()) {
517 const PolicyDetails* policy_details = GetChromePolicyDetails(it.key());
518 ASSERT_TRUE(policy_details);
519 policy_map.Set(
520 it.key(),
521 level,
522 POLICY_SCOPE_USER,
523 POLICY_SOURCE_CLOUD,
524 it.value().DeepCopy(),
525 policy_details->max_external_data_size ?
526 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(),
527 it.key()) :
528 NULL);
530 provider_.UpdateChromePolicy(policy_map);
531 base::RunLoop().RunUntilIdle();
534 MockConfigurationPolicyProvider provider_;
537 // Verifies that policies make their corresponding preferences become managed,
538 // and that the user can't override that setting.
539 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, PolicyToPrefsMapping) {
540 PrefService* local_state = g_browser_process->local_state();
541 PrefService* user_prefs = browser()->profile()->GetPrefs();
543 const PolicyTestCases test_cases;
544 for (PolicyTestCases::iterator policy = test_cases.begin();
545 policy != test_cases.end();
546 ++policy) {
547 for (PolicyTestCases::PolicyTestCaseVector::const_iterator test_case =
548 policy->second.begin();
549 test_case != policy->second.end();
550 ++test_case) {
551 const ScopedVector<PrefMapping>& pref_mappings =
552 (*test_case)->pref_mappings();
553 if (!(*test_case)->IsSupported() || pref_mappings.empty())
554 continue;
556 LOG(INFO) << "Testing policy: " << policy->first;
558 for (ScopedVector<PrefMapping>::const_iterator pref_mapping =
559 pref_mappings.begin();
560 pref_mapping != pref_mappings.end();
561 ++pref_mapping) {
562 // Skip Chrome OS preferences that use a different backend and cannot be
563 // retrieved through the prefs mechanism.
564 if (base::StartsWith((*pref_mapping)->pref(), kCrosSettingsPrefix,
565 base::CompareCase::SENSITIVE))
566 continue;
568 // Skip preferences that should not be checked when the policy is set to
569 // a mandatory value.
570 if (!(*pref_mapping)->check_for_mandatory())
571 continue;
573 PrefService* prefs =
574 (*pref_mapping)->is_local_state() ? local_state : user_prefs;
575 // The preference must have been registered.
576 const PrefService::Preference* pref =
577 prefs->FindPreference((*pref_mapping)->pref().c_str());
578 ASSERT_TRUE(pref);
580 // Verify that setting the policy overrides the pref.
581 ClearProviderPolicy();
582 prefs->ClearPref((*pref_mapping)->pref().c_str());
583 EXPECT_TRUE(pref->IsDefaultValue());
584 EXPECT_TRUE(pref->IsUserModifiable());
585 EXPECT_FALSE(pref->IsUserControlled());
586 EXPECT_FALSE(pref->IsManaged());
588 SetProviderPolicy((*test_case)->test_policy(), POLICY_LEVEL_MANDATORY);
589 EXPECT_FALSE(pref->IsDefaultValue());
590 EXPECT_FALSE(pref->IsUserModifiable());
591 EXPECT_FALSE(pref->IsUserControlled());
592 EXPECT_TRUE(pref->IsManaged());
598 class PolicyPrefIndicatorTest
599 : public PolicyPrefsTest,
600 public testing::WithParamInterface<std::vector<std::string> > {
603 // Verifies that controlled setting indicators correctly show whether a pref's
604 // value is recommended or enforced by a corresponding policy.
605 IN_PROC_BROWSER_TEST_P(PolicyPrefIndicatorTest, CheckPolicyIndicators) {
606 const PolicyTestCases test_cases;
607 PrefService* local_state = g_browser_process->local_state();
608 PrefService* user_prefs = browser()->profile()->GetPrefs();
610 ui_test_utils::NavigateToURL(browser(), GURL(kMainSettingsPage));
612 for (std::vector<std::string>::const_iterator policy = GetParam().begin();
613 policy != GetParam().end();
614 ++policy) {
615 const std::vector<PolicyTestCase*>* policy_test_cases =
616 test_cases.Get(*policy);
617 ASSERT_TRUE(policy_test_cases) << "PolicyTestCase not found for "
618 << *policy;
619 for (std::vector<PolicyTestCase*>::const_iterator test_case =
620 policy_test_cases->begin();
621 test_case != policy_test_cases->end();
622 ++test_case) {
623 PolicyTestCase* policy_test_case = *test_case;
624 if (!policy_test_case->IsSupported())
625 continue;
626 const ScopedVector<PrefMapping>& pref_mappings =
627 policy_test_case->pref_mappings();
628 if (policy_test_case->indicator_selector().empty()) {
629 bool has_pref_indicator_tests = false;
630 for (ScopedVector<PrefMapping>::const_iterator pref_mapping =
631 pref_mappings.begin();
632 pref_mapping != pref_mappings.end();
633 ++pref_mapping) {
634 if (!(*pref_mapping)->indicator_test_cases().empty()) {
635 has_pref_indicator_tests = true;
636 break;
639 if (!has_pref_indicator_tests)
640 continue;
643 LOG(INFO) << "Testing policy: " << *policy;
645 if (!policy_test_case->indicator_selector().empty()) {
646 // Check that no controlled setting indicator is visible when no value
647 // is set by policy.
648 ClearProviderPolicy();
649 VerifyControlledSettingIndicators(
650 browser(),
651 policy_test_case->indicator_selector(),
652 std::string(),
653 std::string(),
654 false);
655 // Check that the appropriate controlled setting indicator is shown when
656 // a value is enforced by policy.
657 SetProviderPolicy(policy_test_case->test_policy(),
658 POLICY_LEVEL_MANDATORY);
659 VerifyControlledSettingIndicators(
660 browser(),
661 policy_test_case->indicator_selector(),
662 std::string(),
663 "policy",
664 false);
665 // Check that no controlled setting indicator is visible when previously
666 // enforced value is removed.
667 ClearProviderPolicy();
668 VerifyControlledSettingIndicators(
669 browser(),
670 policy_test_case->indicator_selector(),
671 std::string(),
672 std::string(),
673 false);
676 for (ScopedVector<PrefMapping>::const_iterator
677 pref_mapping = pref_mappings.begin();
678 pref_mapping != pref_mappings.end();
679 ++pref_mapping) {
680 const ScopedVector<IndicatorTestCase>& indicator_test_cases =
681 (*pref_mapping)->indicator_test_cases();
682 if (indicator_test_cases.empty())
683 continue;
685 if (!(*pref_mapping)->indicator_test_setup_js().empty()) {
686 ASSERT_TRUE(content::ExecuteScript(
687 browser()->tab_strip_model()->GetActiveWebContents(),
688 (*pref_mapping)->indicator_test_setup_js()));
691 std::string indicator_selector = (*pref_mapping)->indicator_selector();
692 if (indicator_selector.empty())
693 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]";
694 for (ScopedVector<IndicatorTestCase>::const_iterator
695 indicator_test_case = indicator_test_cases.begin();
696 indicator_test_case != indicator_test_cases.end();
697 ++indicator_test_case) {
698 // Check that no controlled setting indicator is visible when no value
699 // is set by policy.
700 ClearProviderPolicy();
701 VerifyControlledSettingIndicators(browser(),
702 indicator_selector,
703 std::string(),
704 std::string(),
705 false);
707 if ((*pref_mapping)->check_for_mandatory()) {
708 // Check that the appropriate controlled setting indicator is shown
709 // when a value is enforced by policy.
710 SetProviderPolicy((*indicator_test_case)->policy(),
711 POLICY_LEVEL_MANDATORY);
713 VerifyControlledSettingIndicators(
714 browser(),
715 indicator_selector,
716 (*indicator_test_case)->value(),
717 "policy",
718 (*indicator_test_case)->readonly());
721 if (!policy_test_case->can_be_recommended() ||
722 !(*pref_mapping)->check_for_recommended()) {
723 continue;
726 PrefService* prefs =
727 (*pref_mapping)->is_local_state() ? local_state : user_prefs;
728 // The preference must have been registered.
729 const PrefService::Preference* pref =
730 prefs->FindPreference((*pref_mapping)->pref().c_str());
731 ASSERT_TRUE(pref);
733 // Check that the appropriate controlled setting indicator is shown
734 // when a value is recommended by policy and the user has not
735 // overridden the recommendation.
736 SetProviderPolicy((*indicator_test_case)->policy(),
737 POLICY_LEVEL_RECOMMENDED);
738 VerifyControlledSettingIndicators(browser(),
739 indicator_selector,
740 (*indicator_test_case)->value(),
741 "recommended",
742 (*indicator_test_case)->readonly());
743 // Check that the appropriate controlled setting indicator is shown
744 // when a value is recommended by policy and the user has overridden
745 // the recommendation.
746 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue());
747 VerifyControlledSettingIndicators(browser(),
748 indicator_selector,
749 (*indicator_test_case)->value(),
750 "hasRecommendation",
751 (*indicator_test_case)->readonly());
752 prefs->ClearPref((*pref_mapping)->pref().c_str());
759 INSTANTIATE_TEST_CASE_P(PolicyPrefIndicatorTestInstance,
760 PolicyPrefIndicatorTest,
761 testing::ValuesIn(SplitPoliciesIntoChunks(10)));
763 } // namespace policy