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" |
|
bartfab (slow)
2013/11/05 18:18:33
Nit: policy/policy_constants.h now implicitly pull
Joao da Silva
2013/11/07 20:27:27
The interface is not actually used, so forward-dec
| |
| 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::AnyNumber; | |
| 41 using testing::Return; | 42 using testing::Return; |
| 42 using testing::_; | 43 using testing::_; |
| 43 | 44 |
| 44 namespace policy { | 45 namespace policy { |
| 45 | 46 |
| 46 namespace { | 47 namespace { |
| 47 | 48 |
| 48 const char kMainSettingsPage[] = "chrome://settings-frame"; | 49 const char kMainSettingsPage[] = "chrome://settings-frame"; |
| 49 | 50 |
| 50 const char kCrosSettingsPrefix[] = "cros."; | 51 const char kCrosSettingsPrefix[] = "cros."; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 } | 204 } |
| 204 int error_code = -1; | 205 int error_code = -1; |
| 205 std::string error_string; | 206 std::string error_string; |
| 206 base::DictionaryValue* dict = NULL; | 207 base::DictionaryValue* dict = NULL; |
| 207 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 208 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 208 json, base::JSON_PARSE_RFC, &error_code, &error_string)); | 209 json, base::JSON_PARSE_RFC, &error_code, &error_string)); |
| 209 if (!value.get() || !value->GetAsDictionary(&dict)) { | 210 if (!value.get() || !value->GetAsDictionary(&dict)) { |
| 210 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string; | 211 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string; |
| 211 return; | 212 return; |
| 212 } | 213 } |
| 213 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | 214 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); |
| 214 for (const PolicyDefinitionList::Entry* policy = list->begin; | 215 if (!chrome_schema.valid()) { |
| 215 policy != list->end; ++policy) { | 216 ADD_FAILURE(); |
| 216 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()); | |
| 217 if (policy_test_case) | 222 if (policy_test_case) |
| 218 (*policy_test_cases_)[policy->name] = policy_test_case; | 223 (*policy_test_cases_)[it.key()] = policy_test_case; |
| 219 } | 224 } |
| 220 } | 225 } |
| 221 | 226 |
| 222 ~PolicyTestCases() { | 227 ~PolicyTestCases() { |
| 223 STLDeleteValues(policy_test_cases_); | 228 STLDeleteValues(policy_test_cases_); |
| 224 delete policy_test_cases_; | 229 delete policy_test_cases_; |
| 225 } | 230 } |
| 226 | 231 |
| 227 const PolicyTestCase* Get(const std::string& name) { | 232 const PolicyTestCase* Get(const std::string& name) { |
| 228 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... | |
| 373 } | 378 } |
| 374 if (!controlled_by.empty()) { | 379 if (!controlled_by.empty()) { |
| 375 EXPECT_TRUE(have_visible_indicators) | 380 EXPECT_TRUE(have_visible_indicators) |
| 376 << "Expected to find at least one visible controlled setting " | 381 << "Expected to find at least one visible controlled setting " |
| 377 << "indicator."; | 382 << "indicator."; |
| 378 } | 383 } |
| 379 } | 384 } |
| 380 | 385 |
| 381 } // namespace | 386 } // namespace |
| 382 | 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/05 18:18:33
Nit: #include "base/logging.h"
Joao da Silva
2013/11/07 20:27:27
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 keys() returned are the same. | |
|
bartfab (slow)
2013/11/05 18:18:33
Nit: s/keys()/key()s/
Joao da Silva
2013/11/07 20:27:27
Done.
| |
| 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 return Advance(); | |
|
bartfab (slow)
2013/11/05 18:18:33
This is the post-increment operator. Its return va
Joao da Silva
2013/11/07 20:27:27
Ah right, done
| |
| 459 } | |
| 460 | |
| 461 private: | |
| 462 scoped_ptr<Schema::Iterator> it_; | |
| 463 }; | |
| 464 | |
| 383 // Base class for tests that change policy and are parameterized with a policy | 465 // Base class for tests that change policy and are parameterized with a policy |
| 384 // definition. | 466 // definition. |
| 385 class PolicyPrefsTest | 467 class PolicyPrefsTest |
| 386 : public InProcessBrowserTest, | 468 : public InProcessBrowserTest, |
| 387 public testing::WithParamInterface<PolicyDefinitionList::Entry> { | 469 public testing::WithParamInterface<const char*> { |
| 388 protected: | 470 protected: |
| 389 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 471 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 390 EXPECT_CALL(provider_, IsInitializationComplete(_)) | 472 EXPECT_CALL(provider_, IsInitializationComplete(_)) |
| 391 .WillRepeatedly(Return(true)); | 473 .WillRepeatedly(Return(true)); |
| 392 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 474 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
| 393 } | 475 } |
| 394 | 476 |
| 395 virtual void SetUpOnMainThread() OVERRIDE { | 477 virtual void SetUpOnMainThread() OVERRIDE { |
| 396 ui_test_utils::WaitForTemplateURLServiceToLoad( | 478 ui_test_utils::WaitForTemplateURLServiceToLoad( |
| 397 TemplateURLServiceFactory::GetForProfile(browser()->profile())); | 479 TemplateURLServiceFactory::GetForProfile(browser()->profile())); |
| 398 } | 480 } |
| 399 | 481 |
| 400 void UpdateProviderPolicy(const PolicyMap& policy) { | 482 void UpdateProviderPolicy(const PolicyMap& policy) { |
| 401 provider_.UpdateChromePolicy(policy); | 483 provider_.UpdateChromePolicy(policy); |
| 402 base::RunLoop loop; | 484 base::RunLoop loop; |
| 403 loop.RunUntilIdle(); | 485 loop.RunUntilIdle(); |
| 404 } | 486 } |
| 405 | 487 |
| 406 PolicyTestCases policy_test_cases_; | 488 PolicyTestCases policy_test_cases_; |
| 407 MockConfigurationPolicyProvider provider_; | 489 MockConfigurationPolicyProvider provider_; |
| 408 }; | 490 }; |
| 409 | 491 |
| 410 TEST(PolicyPrefsTestCoverageTest, AllPoliciesHaveATestCase) { | 492 TEST(PolicyPrefsTestCoverageTest, AllPoliciesHaveATestCase) { |
| 411 // Verifies that all known policies have a test case in the JSON file. | 493 // Verifies that all known policies have a test case in the JSON file. |
| 412 // This test fails when a policy is added to | 494 // This test fails when a policy is added to |
| 413 // chrome/app/policy/policy_templates.json but a test case is not added to | 495 // chrome/app/policy/policy_templates.json but a test case is not added to |
| 414 // chrome/test/data/policy/policy_test_cases.json. | 496 // chrome/test/data/policy/policy_test_cases.json. |
| 497 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); | |
| 498 ASSERT_TRUE(chrome_schema.valid()); | |
| 499 | |
| 415 PolicyTestCases policy_test_cases; | 500 PolicyTestCases policy_test_cases; |
| 416 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | 501 for (Schema::Iterator it = chrome_schema.GetPropertiesIterator(); |
| 417 for (const PolicyDefinitionList::Entry* policy = list->begin; | 502 !it.IsAtEnd(); it.Advance()) { |
| 418 policy != list->end; ++policy) { | 503 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), it.key())) |
| 419 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), policy->name)) | 504 << "Missing policy test case for: " << it.key(); |
| 420 << "Missing policy test case for: " << policy->name; | |
| 421 } | 505 } |
| 422 } | 506 } |
| 423 | 507 |
| 424 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { | 508 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { |
| 425 // Verifies that policies make their corresponding preferences become managed, | 509 // Verifies that policies make their corresponding preferences become managed, |
| 426 // and that the user can't override that setting. | 510 // and that the user can't override that setting. |
| 427 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name); | 511 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam()); |
| 428 ASSERT_TRUE(test_case) << "PolicyTestCase not found for " << GetParam().name; | 512 ASSERT_TRUE(test_case) << "PolicyTestCase not found for " << GetParam(); |
| 429 const ScopedVector<PrefMapping>& pref_mappings = test_case->pref_mappings(); | 513 const ScopedVector<PrefMapping>& pref_mappings = test_case->pref_mappings(); |
| 430 if (!test_case->IsSupported() || pref_mappings.empty()) | 514 if (!test_case->IsSupported() || pref_mappings.empty()) |
| 431 return; | 515 return; |
| 432 LOG(INFO) << "Testing policy: " << test_case->name(); | 516 LOG(INFO) << "Testing policy: " << test_case->name(); |
| 433 | 517 |
| 434 for (ScopedVector<PrefMapping>::const_iterator | 518 for (ScopedVector<PrefMapping>::const_iterator |
| 435 pref_mapping = pref_mappings.begin(); | 519 pref_mapping = pref_mappings.begin(); |
| 436 pref_mapping != pref_mappings.end(); | 520 pref_mapping != pref_mappings.end(); |
| 437 ++pref_mapping) { | 521 ++pref_mapping) { |
| 438 // Skip Chrome OS preferences that use a different backend and cannot be | 522 // Skip Chrome OS preferences that use a different backend and cannot be |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 463 EXPECT_FALSE(pref->IsUserModifiable()); | 547 EXPECT_FALSE(pref->IsUserModifiable()); |
| 464 EXPECT_FALSE(pref->IsUserControlled()); | 548 EXPECT_FALSE(pref->IsUserControlled()); |
| 465 EXPECT_TRUE(pref->IsManaged()); | 549 EXPECT_TRUE(pref->IsManaged()); |
| 466 } | 550 } |
| 467 } | 551 } |
| 468 | 552 |
| 469 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { | 553 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { |
| 470 // Verifies that controlled setting indicators correctly show whether a pref's | 554 // Verifies that controlled setting indicators correctly show whether a pref's |
| 471 // value is recommended or enforced by a corresponding policy. | 555 // value is recommended or enforced by a corresponding policy. |
| 472 const PolicyTestCase* policy_test_case = | 556 const PolicyTestCase* policy_test_case = |
| 473 policy_test_cases_.Get(GetParam().name); | 557 policy_test_cases_.Get(GetParam()); |
| 474 ASSERT_TRUE(policy_test_case) << "PolicyTestCase not found for " | 558 ASSERT_TRUE(policy_test_case) << "PolicyTestCase not found for " |
| 475 << GetParam().name; | 559 << GetParam(); |
| 476 const ScopedVector<PrefMapping>& pref_mappings = | 560 const ScopedVector<PrefMapping>& pref_mappings = |
| 477 policy_test_case->pref_mappings(); | 561 policy_test_case->pref_mappings(); |
| 478 if (!policy_test_case->IsSupported() || pref_mappings.empty()) | 562 if (!policy_test_case->IsSupported() || pref_mappings.empty()) |
| 479 return; | 563 return; |
| 480 bool has_indicator_tests = false; | 564 bool has_indicator_tests = false; |
| 481 for (ScopedVector<PrefMapping>::const_iterator | 565 for (ScopedVector<PrefMapping>::const_iterator |
| 482 pref_mapping = pref_mappings.begin(); | 566 pref_mapping = pref_mappings.begin(); |
| 483 pref_mapping != pref_mappings.end(); | 567 pref_mapping != pref_mappings.end(); |
| 484 ++pref_mapping) { | 568 ++pref_mapping) { |
| 485 if (!(*pref_mapping)->indicator_test_cases().empty()) { | 569 if (!(*pref_mapping)->indicator_test_cases().empty()) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 558 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); | 642 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); |
| 559 VerifyControlledSettingIndicators(browser(), indicator_selector, | 643 VerifyControlledSettingIndicators(browser(), indicator_selector, |
| 560 (*indicator_test_case)->value(), | 644 (*indicator_test_case)->value(), |
| 561 "hasRecommendation", | 645 "hasRecommendation", |
| 562 (*indicator_test_case)->readonly()); | 646 (*indicator_test_case)->readonly()); |
| 563 prefs->ClearPref((*pref_mapping)->pref().c_str()); | 647 prefs->ClearPref((*pref_mapping)->pref().c_str()); |
| 564 } | 648 } |
| 565 } | 649 } |
| 566 } | 650 } |
| 567 | 651 |
| 568 INSTANTIATE_TEST_CASE_P( | 652 INSTANTIATE_TEST_CASE_P(PolicyPrefsTestInstance, |
| 569 PolicyPrefsTestInstance, | 653 PolicyPrefsTest, |
| 570 PolicyPrefsTest, | 654 testing::ValuesIn(TestCaseIterator::GetBegin(), |
| 571 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, | 655 TestCaseIterator::GetEnd())); |
| 572 GetChromePolicyDefinitionList()->end)); | |
| 573 | 656 |
| 574 } // namespace policy | 657 } // namespace policy |
| OLD | NEW |