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/message_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; | |
Joao da Silva
2013/01/07 09:23:52
#include "base/run_loop.h"
Andrew T Wilson (Slow)
2013/01/07 14:11:32
Done.
| |
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
500 std::string indicator_selector = (*pref_mapping)->indicator_selector(); | 507 std::string indicator_selector = (*pref_mapping)->indicator_selector(); |
501 if (indicator_selector.empty()) | 508 if (indicator_selector.empty()) |
502 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; | 509 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; |
503 for (ScopedVector<IndicatorTestCase>::const_iterator | 510 for (ScopedVector<IndicatorTestCase>::const_iterator |
504 indicator_test_case = indicator_test_cases.begin(); | 511 indicator_test_case = indicator_test_cases.begin(); |
505 indicator_test_case != indicator_test_cases.end(); | 512 indicator_test_case != indicator_test_cases.end(); |
506 ++indicator_test_case) { | 513 ++indicator_test_case) { |
507 // Check that no controlled setting indicator is visible when no value is | 514 // Check that no controlled setting indicator is visible when no value is |
508 // set by policy. | 515 // set by policy. |
509 PolicyMap policies; | 516 PolicyMap policies; |
510 provider_.UpdateChromePolicy(policies); | 517 UpdateProviderPolicy(policies); |
511 VerifyControlledSettingIndicators(browser(), indicator_selector, | 518 VerifyControlledSettingIndicators(browser(), indicator_selector, |
512 "", "", false); | 519 "", "", false); |
513 // Check that the appropriate controlled setting indicator is shown when a | 520 // Check that the appropriate controlled setting indicator is shown when a |
514 // value is enforced by policy. | 521 // value is enforced by policy. |
515 policies.LoadFrom(&(*indicator_test_case)->policy(), | 522 policies.LoadFrom(&(*indicator_test_case)->policy(), |
516 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 523 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
517 provider_.UpdateChromePolicy(policies); | 524 UpdateProviderPolicy(policies); |
518 VerifyControlledSettingIndicators(browser(), indicator_selector, | 525 VerifyControlledSettingIndicators(browser(), indicator_selector, |
519 (*indicator_test_case)->value(), | 526 (*indicator_test_case)->value(), |
520 "policy", | 527 "policy", |
521 (*indicator_test_case)->readonly()); | 528 (*indicator_test_case)->readonly()); |
522 | 529 |
523 if (!policy_test_case->can_be_recommended()) | 530 if (!policy_test_case->can_be_recommended()) |
524 continue; | 531 continue; |
525 | 532 |
526 PrefService* local_state = g_browser_process->local_state(); | 533 PrefService* local_state = g_browser_process->local_state(); |
527 PrefService* user_prefs = browser()->profile()->GetPrefs(); | 534 PrefService* user_prefs = browser()->profile()->GetPrefs(); |
528 PrefService* prefs = (*pref_mapping)->is_local_state() ? | 535 PrefService* prefs = (*pref_mapping)->is_local_state() ? |
529 local_state : user_prefs; | 536 local_state : user_prefs; |
530 // The preference must have been registered. | 537 // The preference must have been registered. |
531 const PrefService::Preference* pref = | 538 const PrefService::Preference* pref = |
532 prefs->FindPreference((*pref_mapping)->pref().c_str()); | 539 prefs->FindPreference((*pref_mapping)->pref().c_str()); |
533 ASSERT_TRUE(pref); | 540 ASSERT_TRUE(pref); |
534 | 541 |
535 // Check that the appropriate controlled setting indicator is shown when a | 542 // Check that the appropriate controlled setting indicator is shown when a |
536 // value is recommended by policy and the user has not overridden the | 543 // value is recommended by policy and the user has not overridden the |
537 // recommendation. | 544 // recommendation. |
538 policies.LoadFrom(&(*indicator_test_case)->policy(), | 545 policies.LoadFrom(&(*indicator_test_case)->policy(), |
539 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); | 546 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); |
540 provider_.UpdateChromePolicy(policies); | 547 UpdateProviderPolicy(policies); |
541 VerifyControlledSettingIndicators(browser(), indicator_selector, | 548 VerifyControlledSettingIndicators(browser(), indicator_selector, |
542 (*indicator_test_case)->value(), | 549 (*indicator_test_case)->value(), |
543 "recommended", | 550 "recommended", |
544 (*indicator_test_case)->readonly()); | 551 (*indicator_test_case)->readonly()); |
545 // Check that the appropriate controlled setting indicator is shown when a | 552 // Check that the appropriate controlled setting indicator is shown when a |
546 // value is recommended by policy and the user has overriddent the | 553 // value is recommended by policy and the user has overriddent the |
547 // recommendation. | 554 // recommendation. |
548 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); | 555 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); |
549 VerifyControlledSettingIndicators(browser(), indicator_selector, | 556 VerifyControlledSettingIndicators(browser(), indicator_selector, |
550 (*indicator_test_case)->value(), | 557 (*indicator_test_case)->value(), |
551 "hasRecommendation", | 558 "hasRecommendation", |
552 (*indicator_test_case)->readonly()); | 559 (*indicator_test_case)->readonly()); |
553 prefs->ClearPref((*pref_mapping)->pref().c_str()); | 560 prefs->ClearPref((*pref_mapping)->pref().c_str()); |
554 } | 561 } |
555 } | 562 } |
556 } | 563 } |
557 | 564 |
558 INSTANTIATE_TEST_CASE_P( | 565 INSTANTIATE_TEST_CASE_P( |
559 PolicyPrefsTestInstance, | 566 PolicyPrefsTestInstance, |
560 PolicyPrefsTest, | 567 PolicyPrefsTest, |
561 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, | 568 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, |
562 GetChromePolicyDefinitionList()->end)); | 569 GetChromePolicyDefinitionList()->end)); |
563 | 570 |
564 } // namespace policy | 571 } // namespace policy |
OLD | NEW |