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