| 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/values.h" | 5 #include "base/values.h" |
| 6 #include "chrome/browser/policy/asynchronous_policy_loader.h" | 6 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
| 7 #include "chrome/browser/policy/asynchronous_policy_test_base.h" | 7 #include "chrome/browser/policy/asynchronous_policy_test_base.h" |
| 8 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 8 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 9 #include "chrome/browser/policy/configuration_policy_provider.h" | 9 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 10 #include "chrome/browser/policy/file_based_policy_provider.h" | 10 #include "chrome/browser/policy/file_based_policy_provider.h" |
| 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/policy_bundle.h" |
| 12 #include "chrome/browser/policy/policy_map.h" | 13 #include "chrome/browser/policy/policy_map.h" |
| 13 #include "policy/policy_constants.h" | 14 #include "policy/policy_constants.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 using testing::InSequence; | 18 using testing::InSequence; |
| 18 using testing::Return; | 19 using testing::Return; |
| 19 using testing::_; | 20 using testing::_; |
| 20 | 21 |
| 21 namespace policy { | 22 namespace policy { |
| 22 | 23 |
| 23 class FileBasedPolicyProviderDelegateMock | 24 class FileBasedPolicyProviderDelegateMock |
| 24 : public FileBasedPolicyProvider::ProviderDelegate { | 25 : public FileBasedPolicyProvider::ProviderDelegate { |
| 25 public: | 26 public: |
| 26 FileBasedPolicyProviderDelegateMock() | 27 FileBasedPolicyProviderDelegateMock() |
| 27 : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {} | 28 : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {} |
| 28 MOCK_METHOD0(Load, PolicyMap*()); | 29 |
| 30 // Load() returns a scoped_ptr<PolicyBundle> but it can't be mocked because |
| 31 // scoped_ptr is moveable but not copyable. This override forwards the |
| 32 // call to MockLoad() which returns a PolicyBundle*, and returns a copy |
| 33 // wrapped in a passed scoped_ptr. |
| 34 virtual scoped_ptr<PolicyBundle> Load() OVERRIDE { |
| 35 scoped_ptr<PolicyBundle> bundle; |
| 36 PolicyBundle* loaded = MockLoad(); |
| 37 if (loaded) { |
| 38 bundle.reset(new PolicyBundle()); |
| 39 bundle->CopyFrom(*loaded); |
| 40 } |
| 41 return bundle.Pass(); |
| 42 } |
| 43 |
| 44 MOCK_METHOD0(MockLoad, PolicyBundle*()); |
| 29 MOCK_METHOD0(GetLastModification, base::Time()); | 45 MOCK_METHOD0(GetLastModification, base::Time()); |
| 30 }; | 46 }; |
| 31 | 47 |
| 32 TEST_F(AsynchronousPolicyTestBase, ProviderInit) { | 48 TEST_F(AsynchronousPolicyTestBase, ProviderInit) { |
| 33 base::Time last_modified; | 49 base::Time last_modified; |
| 34 FileBasedPolicyProviderDelegateMock* provider_delegate = | 50 FileBasedPolicyProviderDelegateMock* provider_delegate = |
| 35 new FileBasedPolicyProviderDelegateMock(); | 51 new FileBasedPolicyProviderDelegateMock(); |
| 36 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( | 52 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( |
| 37 Return(last_modified)); | 53 Return(last_modified)); |
| 38 InSequence s; | 54 InSequence s; |
| 39 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 55 PolicyBundle empty_bundle; |
| 40 PolicyMap* policies = new PolicyMap(); | 56 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); |
| 41 policies->Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 57 PolicyBundle bundle; |
| 42 Value::CreateBooleanValue(true)); | 58 bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 59 .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 60 base::Value::CreateBooleanValue(true)); |
| 43 // A second call to Load gets triggered during the provider's construction | 61 // A second call to Load gets triggered during the provider's construction |
| 44 // when the file watcher is initialized, since this file may have changed | 62 // when the file watcher is initialized, since this file may have changed |
| 45 // between the initial load and creating watcher. | 63 // between the initial load and creating watcher. |
| 46 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies)); | 64 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&bundle)); |
| 47 FileBasedPolicyProvider provider(GetChromePolicyDefinitionList(), | 65 FileBasedPolicyProvider provider(GetChromePolicyDefinitionList(), |
| 48 provider_delegate); | 66 provider_delegate); |
| 49 loop_.RunAllPending(); | 67 loop_.RunAllPending(); |
| 50 PolicyMap policy_map; | 68 EXPECT_TRUE(provider.policies().Equals(bundle)); |
| 51 provider.Provide(&policy_map); | |
| 52 base::FundamentalValue expected(true); | |
| 53 EXPECT_TRUE(Value::Equals(&expected, | |
| 54 policy_map.GetValue(key::kSyncDisabled))); | |
| 55 EXPECT_EQ(1U, policy_map.size()); | |
| 56 } | 69 } |
| 57 | 70 |
| 58 TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) { | 71 TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) { |
| 59 base::Time last_modified; | 72 base::Time last_modified; |
| 60 FileBasedPolicyProviderDelegateMock* provider_delegate = | 73 FileBasedPolicyProviderDelegateMock* provider_delegate = |
| 61 new FileBasedPolicyProviderDelegateMock(); | 74 new FileBasedPolicyProviderDelegateMock(); |
| 62 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( | 75 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( |
| 63 Return(last_modified)); | 76 Return(last_modified)); |
| 64 InSequence s; | 77 InSequence s; |
| 65 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 78 PolicyBundle empty_bundle; |
| 79 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); |
| 66 FileBasedPolicyProvider file_based_provider(GetChromePolicyDefinitionList(), | 80 FileBasedPolicyProvider file_based_provider(GetChromePolicyDefinitionList(), |
| 67 provider_delegate); | 81 provider_delegate); |
| 68 // A second call to Load gets triggered during the provider's construction | 82 // A second call to Load gets triggered during the provider's construction |
| 69 // when the file watcher is initialized, since this file may have changed | 83 // when the file watcher is initialized, since this file may have changed |
| 70 // between the initial load and creating watcher. | 84 // between the initial load and creating watcher. |
| 71 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 85 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); |
| 72 loop_.RunAllPending(); | 86 loop_.RunAllPending(); |
| 73 // A third and final call to Load is made by the explicit Reload. This | 87 // A third and final call to Load is made by the explicit Reload. This |
| 74 // should be the one that provides the current policy. | 88 // should be the one that provides the current policy. |
| 75 PolicyMap* policies = new PolicyMap(); | 89 PolicyBundle bundle; |
| 76 policies->Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 90 bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 77 Value::CreateBooleanValue(true)); | 91 .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 78 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies)); | 92 base::Value::CreateBooleanValue(true)); |
| 93 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&bundle)); |
| 79 MockConfigurationPolicyObserver observer; | 94 MockConfigurationPolicyObserver observer; |
| 80 ConfigurationPolicyObserverRegistrar registrar; | 95 ConfigurationPolicyObserverRegistrar registrar; |
| 81 registrar.Init(&file_based_provider, &observer); | 96 registrar.Init(&file_based_provider, &observer); |
| 82 EXPECT_CALL(observer, OnUpdatePolicy(&file_based_provider)).Times(1); | 97 EXPECT_CALL(observer, OnUpdatePolicy(&file_based_provider)).Times(1); |
| 83 file_based_provider.RefreshPolicies(); | 98 file_based_provider.RefreshPolicies(); |
| 84 loop_.RunAllPending(); | 99 loop_.RunAllPending(); |
| 85 PolicyMap policy_map; | 100 EXPECT_TRUE(file_based_provider.policies().Equals(bundle)); |
| 86 file_based_provider.Provide(&policy_map); | |
| 87 base::FundamentalValue expected(true); | |
| 88 EXPECT_TRUE(Value::Equals(&expected, | |
| 89 policy_map.GetValue(key::kSyncDisabled))); | |
| 90 EXPECT_EQ(1U, policy_map.size()); | |
| 91 } | 101 } |
| 92 | 102 |
| 93 } // namespace policy | 103 } // namespace policy |
| OLD | NEW |