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 <iterator> |
6 #include <map> | 7 #include <map> |
7 #include <sstream> | 8 #include <sstream> |
8 #include <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/file_util.h" | 13 #include "base/file_util.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/json/json_reader.h" | 15 #include "base/json/json_reader.h" |
| 16 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
17 #include "base/prefs/pref_service.h" | 19 #include "base/prefs/pref_service.h" |
18 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
19 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
20 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
22 #include "base/values.h" | 24 #include "base/values.h" |
23 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
24 #include "chrome/browser/policy/browser_policy_connector.h" | 26 #include "chrome/browser/policy/browser_policy_connector.h" |
25 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 27 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
26 #include "chrome/browser/policy/policy_map.h" | 28 #include "chrome/browser/policy/policy_map.h" |
27 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/browser/search_engines/template_url_service_factory.h" | 30 #include "chrome/browser/search_engines/template_url_service_factory.h" |
29 #include "chrome/browser/ui/browser.h" | 31 #include "chrome/browser/ui/browser.h" |
30 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 32 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
31 #include "chrome/test/base/in_process_browser_test.h" | 33 #include "chrome/test/base/in_process_browser_test.h" |
32 #include "chrome/test/base/ui_test_utils.h" | 34 #include "chrome/test/base/ui_test_utils.h" |
| 35 #include "components/policy/core/common/schema.h" |
33 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
34 #include "content/public/test/browser_test_utils.h" | 37 #include "content/public/test/browser_test_utils.h" |
35 #include "policy/policy_constants.h" | 38 #include "policy/policy_constants.h" |
36 #include "testing/gmock/include/gmock/gmock.h" | 39 #include "testing/gmock/include/gmock/gmock.h" |
37 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
38 #include "url/gurl.h" | 41 #include "url/gurl.h" |
39 | 42 |
40 using testing::Return; | 43 using testing::Return; |
41 using testing::_; | 44 using testing::_; |
42 | 45 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 205 } |
203 int error_code = -1; | 206 int error_code = -1; |
204 std::string error_string; | 207 std::string error_string; |
205 base::DictionaryValue* dict = NULL; | 208 base::DictionaryValue* dict = NULL; |
206 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 209 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
207 json, base::JSON_PARSE_RFC, &error_code, &error_string)); | 210 json, base::JSON_PARSE_RFC, &error_code, &error_string)); |
208 if (!value.get() || !value->GetAsDictionary(&dict)) { | 211 if (!value.get() || !value->GetAsDictionary(&dict)) { |
209 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string; | 212 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string; |
210 return; | 213 return; |
211 } | 214 } |
212 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | 215 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); |
213 for (const PolicyDefinitionList::Entry* policy = list->begin; | 216 if (!chrome_schema.valid()) { |
214 policy != list->end; ++policy) { | 217 ADD_FAILURE(); |
215 PolicyTestCase* policy_test_case = GetPolicyTestCase(dict, policy->name); | 218 return; |
| 219 } |
| 220 for (Schema::Iterator it = chrome_schema.GetPropertiesIterator(); |
| 221 !it.IsAtEnd(); it.Advance()) { |
| 222 PolicyTestCase* policy_test_case = GetPolicyTestCase(dict, it.key()); |
216 if (policy_test_case) | 223 if (policy_test_case) |
217 (*policy_test_cases_)[policy->name] = policy_test_case; | 224 (*policy_test_cases_)[it.key()] = policy_test_case; |
218 } | 225 } |
219 } | 226 } |
220 | 227 |
221 ~PolicyTestCases() { | 228 ~PolicyTestCases() { |
222 STLDeleteValues(policy_test_cases_); | 229 STLDeleteValues(policy_test_cases_); |
223 delete policy_test_cases_; | 230 delete policy_test_cases_; |
224 } | 231 } |
225 | 232 |
226 const PolicyTestCase* Get(const std::string& name) { | 233 const PolicyTestCase* Get(const std::string& name) { |
227 iterator it = policy_test_cases_->find(name); | 234 iterator it = policy_test_cases_->find(name); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 } | 379 } |
373 if (!controlled_by.empty()) { | 380 if (!controlled_by.empty()) { |
374 EXPECT_TRUE(have_visible_indicators) | 381 EXPECT_TRUE(have_visible_indicators) |
375 << "Expected to find at least one visible controlled setting " | 382 << "Expected to find at least one visible controlled setting " |
376 << "indicator."; | 383 << "indicator."; |
377 } | 384 } |
378 } | 385 } |
379 | 386 |
380 } // namespace | 387 } // namespace |
381 | 388 |
| 389 // A forward iterator that iterates over the Chrome policy names, using the |
| 390 // Chrome schema data. |
| 391 class TestCaseIterator |
| 392 : public std::iterator<std::forward_iterator_tag, const char*> { |
| 393 public: |
| 394 static TestCaseIterator GetBegin() { |
| 395 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); |
| 396 CHECK(chrome_schema.valid()); |
| 397 return TestCaseIterator(chrome_schema.GetPropertiesIterator()); |
| 398 } |
| 399 |
| 400 static TestCaseIterator GetEnd() { |
| 401 return TestCaseIterator(); |
| 402 } |
| 403 |
| 404 TestCaseIterator() {} |
| 405 |
| 406 explicit TestCaseIterator(const Schema::Iterator& it) |
| 407 : it_(it.IsAtEnd() ? NULL : new Schema::Iterator(it)) {} |
| 408 |
| 409 TestCaseIterator(const TestCaseIterator& other) |
| 410 : it_(other.it_ ? new Schema::Iterator(*other.it_) : NULL) {} |
| 411 |
| 412 ~TestCaseIterator() {} |
| 413 |
| 414 TestCaseIterator& operator=(const TestCaseIterator& other) { |
| 415 it_.reset(other.it_ ? new Schema::Iterator(*other.it_) : NULL); |
| 416 return *this; |
| 417 } |
| 418 |
| 419 bool Equals(const TestCaseIterator& other) const { |
| 420 // Assume that both iterators are working with the same Schema; therefore |
| 421 // the key()s returned are the same. |
| 422 if (!it_ || !other.it_) |
| 423 return !it_ && !other.it_; |
| 424 return it_->key() == other.it_->key(); |
| 425 } |
| 426 |
| 427 bool operator==(const TestCaseIterator& other) const { |
| 428 return Equals(other); |
| 429 } |
| 430 |
| 431 bool operator !=(const TestCaseIterator& other) const { |
| 432 return !Equals(other); |
| 433 } |
| 434 |
| 435 const char* operator*() const { |
| 436 if (!it_) { |
| 437 NOTREACHED(); |
| 438 return NULL; |
| 439 } |
| 440 return it_->key(); |
| 441 } |
| 442 |
| 443 TestCaseIterator& Advance() { |
| 444 if (it_) { |
| 445 it_->Advance(); |
| 446 if (it_->IsAtEnd()) |
| 447 it_.reset(); |
| 448 } else { |
| 449 NOTREACHED(); |
| 450 } |
| 451 return *this; |
| 452 } |
| 453 |
| 454 TestCaseIterator& operator++() { |
| 455 return Advance(); |
| 456 } |
| 457 |
| 458 TestCaseIterator operator++(int) { |
| 459 TestCaseIterator current = *this; |
| 460 Advance(); |
| 461 return current; |
| 462 } |
| 463 |
| 464 private: |
| 465 scoped_ptr<Schema::Iterator> it_; |
| 466 }; |
| 467 |
382 // Base class for tests that change policy and are parameterized with a policy | 468 // Base class for tests that change policy and are parameterized with a policy |
383 // definition. | 469 // definition. |
384 class PolicyPrefsTest | 470 class PolicyPrefsTest |
385 : public InProcessBrowserTest, | 471 : public InProcessBrowserTest, |
386 public testing::WithParamInterface<PolicyDefinitionList::Entry> { | 472 public testing::WithParamInterface<const char*> { |
387 protected: | 473 protected: |
388 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 474 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
389 EXPECT_CALL(provider_, IsInitializationComplete(_)) | 475 EXPECT_CALL(provider_, IsInitializationComplete(_)) |
390 .WillRepeatedly(Return(true)); | 476 .WillRepeatedly(Return(true)); |
391 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 477 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
392 } | 478 } |
393 | 479 |
394 virtual void SetUpOnMainThread() OVERRIDE { | 480 virtual void SetUpOnMainThread() OVERRIDE { |
395 ui_test_utils::WaitForTemplateURLServiceToLoad( | 481 ui_test_utils::WaitForTemplateURLServiceToLoad( |
396 TemplateURLServiceFactory::GetForProfile(browser()->profile())); | 482 TemplateURLServiceFactory::GetForProfile(browser()->profile())); |
397 } | 483 } |
398 | 484 |
399 void UpdateProviderPolicy(const PolicyMap& policy) { | 485 void UpdateProviderPolicy(const PolicyMap& policy) { |
400 provider_.UpdateChromePolicy(policy); | 486 provider_.UpdateChromePolicy(policy); |
401 base::RunLoop loop; | 487 base::RunLoop loop; |
402 loop.RunUntilIdle(); | 488 loop.RunUntilIdle(); |
403 } | 489 } |
404 | 490 |
405 PolicyTestCases policy_test_cases_; | 491 PolicyTestCases policy_test_cases_; |
406 MockConfigurationPolicyProvider provider_; | 492 MockConfigurationPolicyProvider provider_; |
407 }; | 493 }; |
408 | 494 |
409 TEST(PolicyPrefsTestCoverageTest, AllPoliciesHaveATestCase) { | 495 TEST(PolicyPrefsTestCoverageTest, AllPoliciesHaveATestCase) { |
410 // Verifies that all known policies have a test case in the JSON file. | 496 // Verifies that all known policies have a test case in the JSON file. |
411 // This test fails when a policy is added to | 497 // This test fails when a policy is added to |
412 // chrome/app/policy/policy_templates.json but a test case is not added to | 498 // chrome/app/policy/policy_templates.json but a test case is not added to |
413 // chrome/test/data/policy/policy_test_cases.json. | 499 // chrome/test/data/policy/policy_test_cases.json. |
| 500 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); |
| 501 ASSERT_TRUE(chrome_schema.valid()); |
| 502 |
414 PolicyTestCases policy_test_cases; | 503 PolicyTestCases policy_test_cases; |
415 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | 504 for (Schema::Iterator it = chrome_schema.GetPropertiesIterator(); |
416 for (const PolicyDefinitionList::Entry* policy = list->begin; | 505 !it.IsAtEnd(); it.Advance()) { |
417 policy != list->end; ++policy) { | 506 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), it.key())) |
418 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), policy->name)) | 507 << "Missing policy test case for: " << it.key(); |
419 << "Missing policy test case for: " << policy->name; | |
420 } | 508 } |
421 } | 509 } |
422 | 510 |
423 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { | 511 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { |
424 // Verifies that policies make their corresponding preferences become managed, | 512 // Verifies that policies make their corresponding preferences become managed, |
425 // and that the user can't override that setting. | 513 // and that the user can't override that setting. |
426 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name); | 514 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam()); |
427 ASSERT_TRUE(test_case) << "PolicyTestCase not found for " << GetParam().name; | 515 ASSERT_TRUE(test_case) << "PolicyTestCase not found for " << GetParam(); |
428 const ScopedVector<PrefMapping>& pref_mappings = test_case->pref_mappings(); | 516 const ScopedVector<PrefMapping>& pref_mappings = test_case->pref_mappings(); |
429 if (!test_case->IsSupported() || pref_mappings.empty()) | 517 if (!test_case->IsSupported() || pref_mappings.empty()) |
430 return; | 518 return; |
431 LOG(INFO) << "Testing policy: " << test_case->name(); | 519 LOG(INFO) << "Testing policy: " << test_case->name(); |
432 | 520 |
433 for (ScopedVector<PrefMapping>::const_iterator | 521 for (ScopedVector<PrefMapping>::const_iterator |
434 pref_mapping = pref_mappings.begin(); | 522 pref_mapping = pref_mappings.begin(); |
435 pref_mapping != pref_mappings.end(); | 523 pref_mapping != pref_mappings.end(); |
436 ++pref_mapping) { | 524 ++pref_mapping) { |
437 // Skip Chrome OS preferences that use a different backend and cannot be | 525 // Skip Chrome OS preferences that use a different backend and cannot be |
(...skipping 24 matching lines...) Expand all Loading... |
462 EXPECT_FALSE(pref->IsUserModifiable()); | 550 EXPECT_FALSE(pref->IsUserModifiable()); |
463 EXPECT_FALSE(pref->IsUserControlled()); | 551 EXPECT_FALSE(pref->IsUserControlled()); |
464 EXPECT_TRUE(pref->IsManaged()); | 552 EXPECT_TRUE(pref->IsManaged()); |
465 } | 553 } |
466 } | 554 } |
467 | 555 |
468 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { | 556 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { |
469 // Verifies that controlled setting indicators correctly show whether a pref's | 557 // Verifies that controlled setting indicators correctly show whether a pref's |
470 // value is recommended or enforced by a corresponding policy. | 558 // value is recommended or enforced by a corresponding policy. |
471 const PolicyTestCase* policy_test_case = | 559 const PolicyTestCase* policy_test_case = |
472 policy_test_cases_.Get(GetParam().name); | 560 policy_test_cases_.Get(GetParam()); |
473 ASSERT_TRUE(policy_test_case) << "PolicyTestCase not found for " | 561 ASSERT_TRUE(policy_test_case) << "PolicyTestCase not found for " |
474 << GetParam().name; | 562 << GetParam(); |
475 const ScopedVector<PrefMapping>& pref_mappings = | 563 const ScopedVector<PrefMapping>& pref_mappings = |
476 policy_test_case->pref_mappings(); | 564 policy_test_case->pref_mappings(); |
477 if (!policy_test_case->IsSupported() || pref_mappings.empty()) | 565 if (!policy_test_case->IsSupported() || pref_mappings.empty()) |
478 return; | 566 return; |
479 bool has_indicator_tests = false; | 567 bool has_indicator_tests = false; |
480 for (ScopedVector<PrefMapping>::const_iterator | 568 for (ScopedVector<PrefMapping>::const_iterator |
481 pref_mapping = pref_mappings.begin(); | 569 pref_mapping = pref_mappings.begin(); |
482 pref_mapping != pref_mappings.end(); | 570 pref_mapping != pref_mappings.end(); |
483 ++pref_mapping) { | 571 ++pref_mapping) { |
484 if (!(*pref_mapping)->indicator_test_cases().empty()) { | 572 if (!(*pref_mapping)->indicator_test_cases().empty()) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); | 645 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); |
558 VerifyControlledSettingIndicators(browser(), indicator_selector, | 646 VerifyControlledSettingIndicators(browser(), indicator_selector, |
559 (*indicator_test_case)->value(), | 647 (*indicator_test_case)->value(), |
560 "hasRecommendation", | 648 "hasRecommendation", |
561 (*indicator_test_case)->readonly()); | 649 (*indicator_test_case)->readonly()); |
562 prefs->ClearPref((*pref_mapping)->pref().c_str()); | 650 prefs->ClearPref((*pref_mapping)->pref().c_str()); |
563 } | 651 } |
564 } | 652 } |
565 } | 653 } |
566 | 654 |
567 INSTANTIATE_TEST_CASE_P( | 655 INSTANTIATE_TEST_CASE_P(PolicyPrefsTestInstance, |
568 PolicyPrefsTestInstance, | 656 PolicyPrefsTest, |
569 PolicyPrefsTest, | 657 testing::ValuesIn(TestCaseIterator::GetBegin(), |
570 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, | 658 TestCaseIterator::GetEnd())); |
571 GetChromePolicyDefinitionList()->end)); | |
572 | 659 |
573 } // namespace policy | 660 } // namespace policy |
OLD | NEW |