OLD | NEW |
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 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/run_loop.h" |
17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
18 #include "base/string_util.h" | 19 #include "base/string_util.h" |
19 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
20 #include "base/values.h" | 21 #include "base/values.h" |
21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
22 #include "chrome/browser/policy/browser_policy_connector.h" | 23 #include "chrome/browser/policy/browser_policy_connector.h" |
23 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 24 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
24 #include "chrome/browser/policy/policy_map.h" | 25 #include "chrome/browser/policy/policy_map.h" |
25 #include "chrome/browser/prefs/pref_service.h" | 26 #include "chrome/browser/prefs/pref_service.h" |
26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 EXPECT_CALL(provider_, IsInitializationComplete()) | 387 EXPECT_CALL(provider_, IsInitializationComplete()) |
387 .WillRepeatedly(Return(true)); | 388 .WillRepeatedly(Return(true)); |
388 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 389 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
389 } | 390 } |
390 | 391 |
391 virtual void SetUpOnMainThread() OVERRIDE { | 392 virtual void SetUpOnMainThread() OVERRIDE { |
392 ui_test_utils::WaitForTemplateURLServiceToLoad( | 393 ui_test_utils::WaitForTemplateURLServiceToLoad( |
393 TemplateURLServiceFactory::GetForProfile(browser()->profile())); | 394 TemplateURLServiceFactory::GetForProfile(browser()->profile())); |
394 } | 395 } |
395 | 396 |
| 397 void UpdateProviderPolicy(const PolicyMap& policy) { |
| 398 provider_.UpdateChromePolicy(policy); |
| 399 base::RunLoop loop; |
| 400 loop.RunUntilIdle(); |
| 401 } |
| 402 |
396 PolicyTestCases policy_test_cases_; | 403 PolicyTestCases policy_test_cases_; |
397 MockConfigurationPolicyProvider provider_; | 404 MockConfigurationPolicyProvider provider_; |
398 }; | 405 }; |
399 | 406 |
400 TEST(PolicyPrefsTestCoverageTest, AllPoliciesHaveATestCase) { | 407 TEST(PolicyPrefsTestCoverageTest, AllPoliciesHaveATestCase) { |
401 // Verifies that all known policies have a test case in the JSON file. | 408 // Verifies that all known policies have a test case in the JSON file. |
402 // This test fails when a policy is added to | 409 // This test fails when a policy is added to |
403 // chrome/app/policy/policy_templates.json but a test case is not added to | 410 // chrome/app/policy/policy_templates.json but a test case is not added to |
404 // chrome/test/data/policy/policy_test_cases.json. | 411 // chrome/test/data/policy/policy_test_cases.json. |
405 PolicyTestCases policy_test_cases; | 412 PolicyTestCases policy_test_cases; |
(...skipping 29 matching lines...) Expand all Loading... |
435 PrefService* prefs = (*pref_mapping)->is_local_state() ? | 442 PrefService* prefs = (*pref_mapping)->is_local_state() ? |
436 local_state : user_prefs; | 443 local_state : user_prefs; |
437 // The preference must have been registered. | 444 // The preference must have been registered. |
438 const PrefService::Preference* pref = | 445 const PrefService::Preference* pref = |
439 prefs->FindPreference((*pref_mapping)->pref().c_str()); | 446 prefs->FindPreference((*pref_mapping)->pref().c_str()); |
440 ASSERT_TRUE(pref); | 447 ASSERT_TRUE(pref); |
441 prefs->ClearPref((*pref_mapping)->pref().c_str()); | 448 prefs->ClearPref((*pref_mapping)->pref().c_str()); |
442 | 449 |
443 // Verify that setting the policy overrides the pref. | 450 // Verify that setting the policy overrides the pref. |
444 const PolicyMap kNoPolicies; | 451 const PolicyMap kNoPolicies; |
445 provider_.UpdateChromePolicy(kNoPolicies); | 452 UpdateProviderPolicy(kNoPolicies); |
446 EXPECT_TRUE(pref->IsDefaultValue()); | 453 EXPECT_TRUE(pref->IsDefaultValue()); |
447 EXPECT_TRUE(pref->IsUserModifiable()); | 454 EXPECT_TRUE(pref->IsUserModifiable()); |
448 EXPECT_FALSE(pref->IsUserControlled()); | 455 EXPECT_FALSE(pref->IsUserControlled()); |
449 EXPECT_FALSE(pref->IsManaged()); | 456 EXPECT_FALSE(pref->IsManaged()); |
450 | 457 |
451 provider_.UpdateChromePolicy(test_case->test_policy()); | 458 UpdateProviderPolicy(test_case->test_policy()); |
452 EXPECT_FALSE(pref->IsDefaultValue()); | 459 EXPECT_FALSE(pref->IsDefaultValue()); |
453 EXPECT_FALSE(pref->IsUserModifiable()); | 460 EXPECT_FALSE(pref->IsUserModifiable()); |
454 EXPECT_FALSE(pref->IsUserControlled()); | 461 EXPECT_FALSE(pref->IsUserControlled()); |
455 EXPECT_TRUE(pref->IsManaged()); | 462 EXPECT_TRUE(pref->IsManaged()); |
456 } | 463 } |
457 } | 464 } |
458 | 465 |
459 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { | 466 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { |
460 // Verifies that controlled setting indicators correctly show whether a pref's | 467 // Verifies that controlled setting indicators correctly show whether a pref's |
461 // value is recommended or enforced by a corresponding policy. | 468 // value is recommended or enforced by a corresponding policy. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 std::string indicator_selector = (*pref_mapping)->indicator_selector(); | 506 std::string indicator_selector = (*pref_mapping)->indicator_selector(); |
500 if (indicator_selector.empty()) | 507 if (indicator_selector.empty()) |
501 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; | 508 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; |
502 for (ScopedVector<IndicatorTestCase>::const_iterator | 509 for (ScopedVector<IndicatorTestCase>::const_iterator |
503 indicator_test_case = indicator_test_cases.begin(); | 510 indicator_test_case = indicator_test_cases.begin(); |
504 indicator_test_case != indicator_test_cases.end(); | 511 indicator_test_case != indicator_test_cases.end(); |
505 ++indicator_test_case) { | 512 ++indicator_test_case) { |
506 // Check that no controlled setting indicator is visible when no value is | 513 // Check that no controlled setting indicator is visible when no value is |
507 // set by policy. | 514 // set by policy. |
508 PolicyMap policies; | 515 PolicyMap policies; |
509 provider_.UpdateChromePolicy(policies); | 516 UpdateProviderPolicy(policies); |
510 VerifyControlledSettingIndicators(browser(), indicator_selector, | 517 VerifyControlledSettingIndicators(browser(), indicator_selector, |
511 "", "", false); | 518 "", "", false); |
512 // Check that the appropriate controlled setting indicator is shown when a | 519 // Check that the appropriate controlled setting indicator is shown when a |
513 // value is enforced by policy. | 520 // value is enforced by policy. |
514 policies.LoadFrom(&(*indicator_test_case)->policy(), | 521 policies.LoadFrom(&(*indicator_test_case)->policy(), |
515 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 522 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
516 provider_.UpdateChromePolicy(policies); | 523 UpdateProviderPolicy(policies); |
517 VerifyControlledSettingIndicators(browser(), indicator_selector, | 524 VerifyControlledSettingIndicators(browser(), indicator_selector, |
518 (*indicator_test_case)->value(), | 525 (*indicator_test_case)->value(), |
519 "policy", | 526 "policy", |
520 (*indicator_test_case)->readonly()); | 527 (*indicator_test_case)->readonly()); |
521 | 528 |
522 if (!policy_test_case->can_be_recommended()) | 529 if (!policy_test_case->can_be_recommended()) |
523 continue; | 530 continue; |
524 | 531 |
525 PrefService* local_state = g_browser_process->local_state(); | 532 PrefService* local_state = g_browser_process->local_state(); |
526 PrefService* user_prefs = browser()->profile()->GetPrefs(); | 533 PrefService* user_prefs = browser()->profile()->GetPrefs(); |
527 PrefService* prefs = (*pref_mapping)->is_local_state() ? | 534 PrefService* prefs = (*pref_mapping)->is_local_state() ? |
528 local_state : user_prefs; | 535 local_state : user_prefs; |
529 // The preference must have been registered. | 536 // The preference must have been registered. |
530 const PrefService::Preference* pref = | 537 const PrefService::Preference* pref = |
531 prefs->FindPreference((*pref_mapping)->pref().c_str()); | 538 prefs->FindPreference((*pref_mapping)->pref().c_str()); |
532 ASSERT_TRUE(pref); | 539 ASSERT_TRUE(pref); |
533 | 540 |
534 // Check that the appropriate controlled setting indicator is shown when a | 541 // Check that the appropriate controlled setting indicator is shown when a |
535 // value is recommended by policy and the user has not overridden the | 542 // value is recommended by policy and the user has not overridden the |
536 // recommendation. | 543 // recommendation. |
537 policies.LoadFrom(&(*indicator_test_case)->policy(), | 544 policies.LoadFrom(&(*indicator_test_case)->policy(), |
538 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); | 545 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); |
539 provider_.UpdateChromePolicy(policies); | 546 UpdateProviderPolicy(policies); |
540 VerifyControlledSettingIndicators(browser(), indicator_selector, | 547 VerifyControlledSettingIndicators(browser(), indicator_selector, |
541 (*indicator_test_case)->value(), | 548 (*indicator_test_case)->value(), |
542 "recommended", | 549 "recommended", |
543 (*indicator_test_case)->readonly()); | 550 (*indicator_test_case)->readonly()); |
544 // Check that the appropriate controlled setting indicator is shown when a | 551 // Check that the appropriate controlled setting indicator is shown when a |
545 // value is recommended by policy and the user has overriddent the | 552 // value is recommended by policy and the user has overriddent the |
546 // recommendation. | 553 // recommendation. |
547 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); | 554 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); |
548 VerifyControlledSettingIndicators(browser(), indicator_selector, | 555 VerifyControlledSettingIndicators(browser(), indicator_selector, |
549 (*indicator_test_case)->value(), | 556 (*indicator_test_case)->value(), |
550 "hasRecommendation", | 557 "hasRecommendation", |
551 (*indicator_test_case)->readonly()); | 558 (*indicator_test_case)->readonly()); |
552 prefs->ClearPref((*pref_mapping)->pref().c_str()); | 559 prefs->ClearPref((*pref_mapping)->pref().c_str()); |
553 } | 560 } |
554 } | 561 } |
555 } | 562 } |
556 | 563 |
557 INSTANTIATE_TEST_CASE_P( | 564 INSTANTIATE_TEST_CASE_P( |
558 PolicyPrefsTestInstance, | 565 PolicyPrefsTestInstance, |
559 PolicyPrefsTest, | 566 PolicyPrefsTest, |
560 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, | 567 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, |
561 GetChromePolicyDefinitionList()->end)); | 568 GetChromePolicyDefinitionList()->end)); |
562 | 569 |
563 } // namespace policy | 570 } // namespace policy |
OLD | NEW |