| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/json/json_writer.h" | 6 #include "base/json/json_writer.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/api/storage/settings_frontend.h" | 10 #include "chrome/browser/extensions/api/storage/settings_frontend.h" |
| 10 #include "chrome/browser/extensions/api/storage/settings_namespace.h" | 11 #include "chrome/browser/extensions/api/storage/settings_namespace.h" |
| 11 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 12 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
| 12 #include "chrome/browser/extensions/extension_apitest.h" | 13 #include "chrome/browser/extensions/extension_apitest.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_system.h" | 15 #include "chrome/browser/extensions/extension_system.h" |
| 15 #include "chrome/browser/extensions/extension_system_factory.h" | 16 #include "chrome/browser/extensions/extension_system_factory.h" |
| 16 #include "chrome/browser/extensions/extension_test_message_listener.h" | 17 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "extensions/common/value_builder.h" | 21 #include "extensions/common/value_builder.h" |
| 21 #include "sync/api/sync_change.h" | 22 #include "sync/api/sync_change.h" |
| 22 #include "sync/api/sync_change_processor.h" | 23 #include "sync/api/sync_change_processor.h" |
| 23 #include "sync/api/sync_error_factory.h" | 24 #include "sync/api/sync_error_factory.h" |
| 24 #include "sync/api/sync_error_factory_mock.h" | 25 #include "sync/api/sync_error_factory_mock.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 26 | 27 |
| 27 #if defined(ENABLE_CONFIGURATION_POLICY) | 28 #if defined(ENABLE_CONFIGURATION_POLICY) |
| 28 #include "chrome/browser/policy/browser_policy_connector.h" | 29 #include "chrome/browser/policy/browser_policy_connector.h" |
| 29 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 30 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 30 #include "chrome/browser/policy/policy_bundle.h" | 31 #include "chrome/browser/policy/policy_bundle.h" |
| 31 #include "chrome/browser/policy/policy_domain_descriptor.h" | |
| 32 #include "chrome/browser/policy/policy_map.h" | 32 #include "chrome/browser/policy/policy_map.h" |
| 33 #include "chrome/browser/policy/policy_service.h" | 33 #include "chrome/browser/policy/schema_map.h" |
| 34 #include "chrome/browser/policy/profile_policy_connector.h" | 34 #include "chrome/browser/policy/schema_registry.h" |
| 35 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 35 #include "chrome/browser/policy/schema_registry_service.h" |
| 36 #include "chrome/browser/policy/schema_registry_service_factory.h" |
| 37 #include "components/policy/core/common/policy_namespace.h" |
| 38 #include "components/policy/core/common/schema.h" |
| 36 #endif | 39 #endif |
| 37 | 40 |
| 38 namespace extensions { | 41 namespace extensions { |
| 39 | 42 |
| 40 using settings_namespace::FromString; | |
| 41 using settings_namespace::LOCAL; | 43 using settings_namespace::LOCAL; |
| 42 using settings_namespace::MANAGED; | 44 using settings_namespace::MANAGED; |
| 43 using settings_namespace::Namespace; | 45 using settings_namespace::Namespace; |
| 44 using settings_namespace::SYNC; | 46 using settings_namespace::SYNC; |
| 45 using settings_namespace::ToString; | 47 using settings_namespace::ToString; |
| 46 using testing::AnyNumber; | 48 using testing::Mock; |
| 47 using testing::Return; | 49 using testing::Return; |
| 48 using testing::_; | 50 using testing::_; |
| 49 | 51 |
| 50 namespace { | 52 namespace { |
| 51 | 53 |
| 52 // TODO(kalman): test both EXTENSION_SETTINGS and APP_SETTINGS. | 54 // TODO(kalman): test both EXTENSION_SETTINGS and APP_SETTINGS. |
| 53 const syncer::ModelType kModelType = syncer::EXTENSION_SETTINGS; | 55 const syncer::ModelType kModelType = syncer::EXTENSION_SETTINGS; |
| 54 | 56 |
| 55 // The managed_storage extension has a key defined in its manifest, so that | 57 // The managed_storage extension has a key defined in its manifest, so that |
| 56 // its extension ID is well-known and the policy system can push policies for | 58 // its extension ID is well-known and the policy system can push policies for |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 return recipient_->GetAllSyncData(type); | 95 return recipient_->GetAllSyncData(type); |
| 94 } | 96 } |
| 95 | 97 |
| 96 private: | 98 private: |
| 97 // The recipient of all sync changes. | 99 // The recipient of all sync changes. |
| 98 syncer::SyncChangeProcessor* recipient_; | 100 syncer::SyncChangeProcessor* recipient_; |
| 99 | 101 |
| 100 DISALLOW_COPY_AND_ASSIGN(SyncChangeProcessorDelegate); | 102 DISALLOW_COPY_AND_ASSIGN(SyncChangeProcessorDelegate); |
| 101 }; | 103 }; |
| 102 | 104 |
| 105 class MockSchemaRegistryObserver : public policy::SchemaRegistry::Observer { |
| 106 public: |
| 107 MockSchemaRegistryObserver() {} |
| 108 virtual ~MockSchemaRegistryObserver() {} |
| 109 |
| 110 MOCK_METHOD1(OnSchemaRegistryUpdated, void(bool)); |
| 111 }; |
| 112 |
| 103 } // namespace | 113 } // namespace |
| 104 | 114 |
| 105 class ExtensionSettingsApiTest : public ExtensionApiTest { | 115 class ExtensionSettingsApiTest : public ExtensionApiTest { |
| 106 protected: | 116 protected: |
| 107 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 117 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 108 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | 118 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); |
| 109 | 119 |
| 110 #if defined(ENABLE_CONFIGURATION_POLICY) | 120 #if defined(ENABLE_CONFIGURATION_POLICY) |
| 111 EXPECT_CALL(policy_provider_, IsInitializationComplete(_)) | 121 EXPECT_CALL(policy_provider_, IsInitializationComplete(_)) |
| 112 .WillRepeatedly(Return(true)); | 122 .WillRepeatedly(Return(true)); |
| 113 EXPECT_CALL(policy_provider_, RegisterPolicyDomain(_)).Times(AnyNumber()); | |
| 114 policy::BrowserPolicyConnector::SetPolicyProviderForTesting( | 123 policy::BrowserPolicyConnector::SetPolicyProviderForTesting( |
| 115 &policy_provider_); | 124 &policy_provider_); |
| 116 #endif | 125 #endif |
| 117 } | 126 } |
| 118 | 127 |
| 119 void ReplyWhenSatisfied( | 128 void ReplyWhenSatisfied( |
| 120 Namespace settings_namespace, | 129 Namespace settings_namespace, |
| 121 const std::string& normal_action, | 130 const std::string& normal_action, |
| 122 const std::string& incognito_action) { | 131 const std::string& incognito_action) { |
| 123 MaybeLoadAndReplyWhenSatisfied( | 132 MaybeLoadAndReplyWhenSatisfied( |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 | 447 |
| 439 #if defined(ENABLE_CONFIGURATION_POLICY) | 448 #if defined(ENABLE_CONFIGURATION_POLICY) |
| 440 EXPECT_TRUE(frontend->IsStorageEnabled(MANAGED)); | 449 EXPECT_TRUE(frontend->IsStorageEnabled(MANAGED)); |
| 441 #else | 450 #else |
| 442 EXPECT_FALSE(frontend->IsStorageEnabled(MANAGED)); | 451 EXPECT_FALSE(frontend->IsStorageEnabled(MANAGED)); |
| 443 #endif | 452 #endif |
| 444 } | 453 } |
| 445 | 454 |
| 446 #if defined(ENABLE_CONFIGURATION_POLICY) | 455 #if defined(ENABLE_CONFIGURATION_POLICY) |
| 447 | 456 |
| 448 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, PolicyDomainDescriptor) { | 457 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, ExtensionsSchemas) { |
| 449 // Verifies that the PolicyDomainDescriptor for the extensions domain is | 458 // Verifies that the Schemas for the extensions domain are created on startup. |
| 450 // created on startup. | |
| 451 Profile* profile = browser()->profile(); | 459 Profile* profile = browser()->profile(); |
| 452 ExtensionSystem* extension_system = | 460 ExtensionSystem* extension_system = |
| 453 ExtensionSystemFactory::GetForProfile(profile); | 461 ExtensionSystemFactory::GetForProfile(profile); |
| 454 if (!extension_system->ready().is_signaled()) { | 462 if (!extension_system->ready().is_signaled()) { |
| 455 // Wait until the extension system is ready. | 463 // Wait until the extension system is ready. |
| 456 base::RunLoop run_loop; | 464 base::RunLoop run_loop; |
| 457 extension_system->ready().Post(FROM_HERE, run_loop.QuitClosure()); | 465 extension_system->ready().Post(FROM_HERE, run_loop.QuitClosure()); |
| 458 run_loop.Run(); | 466 run_loop.Run(); |
| 459 ASSERT_TRUE(extension_system->ready().is_signaled()); | 467 ASSERT_TRUE(extension_system->ready().is_signaled()); |
| 460 } | 468 } |
| 461 | 469 |
| 462 policy::ProfilePolicyConnector* connector = | 470 // This test starts without any test extensions installed. |
| 463 policy::ProfilePolicyConnectorFactory::GetForProfile(profile); | 471 EXPECT_FALSE(GetSingleLoadedExtension()); |
| 464 policy::PolicyService* service = connector->policy_service(); | 472 message_.clear(); |
| 465 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor = | 473 |
| 466 service->GetPolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS); | 474 policy::SchemaRegistry* registry = |
| 467 EXPECT_TRUE(descriptor.get()); | 475 policy::SchemaRegistryServiceFactory::GetForContext(profile); |
| 476 ASSERT_TRUE(registry); |
| 477 EXPECT_FALSE(registry->schema_map()->GetSchema(policy::PolicyNamespace( |
| 478 policy::POLICY_DOMAIN_EXTENSIONS, kManagedStorageExtensionId))); |
| 479 |
| 480 MockSchemaRegistryObserver observer; |
| 481 registry->AddObserver(&observer); |
| 482 |
| 483 // Install a managed extension. |
| 484 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 485 const Extension* extension = |
| 486 LoadExtension(test_data_dir_.AppendASCII("settings/managed_storage")); |
| 487 ASSERT_TRUE(extension); |
| 488 Mock::VerifyAndClearExpectations(&observer); |
| 489 registry->RemoveObserver(&observer); |
| 490 |
| 491 // Verify that its schema has been published, and verify its contents. |
| 492 const policy::Schema* schema = |
| 493 registry->schema_map()->GetSchema(policy::PolicyNamespace( |
| 494 policy::POLICY_DOMAIN_EXTENSIONS, kManagedStorageExtensionId)); |
| 495 ASSERT_TRUE(schema); |
| 496 |
| 497 ASSERT_TRUE(schema->valid()); |
| 498 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema->type()); |
| 499 ASSERT_TRUE(schema->GetKnownProperty("string-policy").valid()); |
| 500 EXPECT_EQ(base::Value::TYPE_STRING, |
| 501 schema->GetKnownProperty("string-policy").type()); |
| 502 ASSERT_TRUE(schema->GetKnownProperty("int-policy").valid()); |
| 503 EXPECT_EQ(base::Value::TYPE_INTEGER, |
| 504 schema->GetKnownProperty("int-policy").type()); |
| 505 ASSERT_TRUE(schema->GetKnownProperty("double-policy").valid()); |
| 506 EXPECT_EQ(base::Value::TYPE_DOUBLE, |
| 507 schema->GetKnownProperty("double-policy").type()); |
| 508 ASSERT_TRUE(schema->GetKnownProperty("boolean-policy").valid()); |
| 509 EXPECT_EQ(base::Value::TYPE_BOOLEAN, |
| 510 schema->GetKnownProperty("boolean-policy").type()); |
| 511 |
| 512 policy::Schema list = schema->GetKnownProperty("list-policy"); |
| 513 ASSERT_TRUE(list.valid()); |
| 514 ASSERT_EQ(base::Value::TYPE_LIST, list.type()); |
| 515 ASSERT_TRUE(list.GetItems().valid()); |
| 516 EXPECT_EQ(base::Value::TYPE_STRING, list.GetItems().type()); |
| 517 |
| 518 policy::Schema dict = schema->GetKnownProperty("dict-policy"); |
| 519 ASSERT_TRUE(dict.valid()); |
| 520 ASSERT_EQ(base::Value::TYPE_DICTIONARY, dict.type()); |
| 521 list = dict.GetKnownProperty("list"); |
| 522 ASSERT_TRUE(list.valid()); |
| 523 ASSERT_EQ(base::Value::TYPE_LIST, list.type()); |
| 524 dict = list.GetItems(); |
| 525 ASSERT_TRUE(dict.valid()); |
| 526 ASSERT_EQ(base::Value::TYPE_DICTIONARY, dict.type()); |
| 527 ASSERT_TRUE(dict.GetProperty("anything").valid()); |
| 528 EXPECT_EQ(base::Value::TYPE_INTEGER, dict.GetProperty("anything").type()); |
| 468 } | 529 } |
| 469 | 530 |
| 470 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, ManagedStorage) { | 531 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, ManagedStorage) { |
| 471 // Set policies for the test extension. | 532 // Set policies for the test extension. |
| 472 scoped_ptr<base::DictionaryValue> policy = extensions::DictionaryBuilder() | 533 scoped_ptr<base::DictionaryValue> policy = extensions::DictionaryBuilder() |
| 473 .Set("string-policy", "value") | 534 .Set("string-policy", "value") |
| 474 .Set("int-policy", -123) | 535 .Set("int-policy", -123) |
| 475 .Set("double-policy", 456e7) | 536 .Set("double-policy", 456e7) |
| 476 .SetBoolean("boolean-policy", true) | 537 .SetBoolean("boolean-policy", true) |
| 477 .Set("list-policy", extensions::ListBuilder() | 538 .Set("list-policy", extensions::ListBuilder() |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 SettingsFrontend* frontend = | 619 SettingsFrontend* frontend = |
| 559 browser()->profile()->GetExtensionService()->settings_frontend(); | 620 browser()->profile()->GetExtensionService()->settings_frontend(); |
| 560 frontend->DisableStorageForTesting(MANAGED); | 621 frontend->DisableStorageForTesting(MANAGED); |
| 561 EXPECT_FALSE(frontend->IsStorageEnabled(MANAGED)); | 622 EXPECT_FALSE(frontend->IsStorageEnabled(MANAGED)); |
| 562 // Now run the extension. | 623 // Now run the extension. |
| 563 ASSERT_TRUE(RunExtensionTest("settings/managed_storage_disabled")) | 624 ASSERT_TRUE(RunExtensionTest("settings/managed_storage_disabled")) |
| 564 << message_; | 625 << message_; |
| 565 } | 626 } |
| 566 | 627 |
| 567 } // namespace extensions | 628 } // namespace extensions |
| OLD | NEW |