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 "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(new PolicyBundle()); | |
| 36 PolicyBundle* loaded = MockLoad(); | |
| 37 if (loaded) | |
| 38 bundle->CopyFrom(*loaded); | |
|
Mattias Nissler (ping if slow)
2012/05/15 17:07:24
same comment as before.
Joao da Silva
2012/05/16 10:25:44
Done.
| |
| 39 return bundle.Pass(); | |
| 40 } | |
| 41 | |
| 42 MOCK_METHOD0(MockLoad, PolicyBundle*()); | |
| 29 MOCK_METHOD0(GetLastModification, base::Time()); | 43 MOCK_METHOD0(GetLastModification, base::Time()); |
| 30 }; | 44 }; |
| 31 | 45 |
| 32 TEST_F(AsynchronousPolicyTestBase, ProviderInit) { | 46 TEST_F(AsynchronousPolicyTestBase, ProviderInit) { |
| 33 base::Time last_modified; | 47 base::Time last_modified; |
| 34 FileBasedPolicyProviderDelegateMock* provider_delegate = | 48 FileBasedPolicyProviderDelegateMock* provider_delegate = |
| 35 new FileBasedPolicyProviderDelegateMock(); | 49 new FileBasedPolicyProviderDelegateMock(); |
| 36 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( | 50 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( |
| 37 Return(last_modified)); | 51 Return(last_modified)); |
| 38 InSequence s; | 52 InSequence s; |
| 39 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 53 PolicyBundle empty_bundle; |
| 40 PolicyMap* policies = new PolicyMap(); | 54 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); |
| 41 policies->Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 55 PolicyBundle bundle; |
| 42 Value::CreateBooleanValue(true)); | 56 bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 57 .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 58 base::Value::CreateBooleanValue(true)); | |
| 43 // A second call to Load gets triggered during the provider's construction | 59 // 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 | 60 // when the file watcher is initialized, since this file may have changed |
| 45 // between the initial load and creating watcher. | 61 // between the initial load and creating watcher. |
| 46 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies)); | 62 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&bundle)); |
| 47 FileBasedPolicyProvider provider(GetChromePolicyDefinitionList(), | 63 FileBasedPolicyProvider provider(GetChromePolicyDefinitionList(), |
| 48 provider_delegate); | 64 provider_delegate); |
| 49 loop_.RunAllPending(); | 65 loop_.RunAllPending(); |
| 50 PolicyMap policy_map; | 66 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 } | 67 } |
| 57 | 68 |
| 58 TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) { | 69 TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) { |
| 59 base::Time last_modified; | 70 base::Time last_modified; |
| 60 FileBasedPolicyProviderDelegateMock* provider_delegate = | 71 FileBasedPolicyProviderDelegateMock* provider_delegate = |
| 61 new FileBasedPolicyProviderDelegateMock(); | 72 new FileBasedPolicyProviderDelegateMock(); |
| 62 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( | 73 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( |
| 63 Return(last_modified)); | 74 Return(last_modified)); |
| 64 InSequence s; | 75 InSequence s; |
| 65 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 76 PolicyBundle empty_bundle; |
| 77 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); | |
| 66 FileBasedPolicyProvider file_based_provider(GetChromePolicyDefinitionList(), | 78 FileBasedPolicyProvider file_based_provider(GetChromePolicyDefinitionList(), |
| 67 provider_delegate); | 79 provider_delegate); |
| 68 // A second call to Load gets triggered during the provider's construction | 80 // 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 | 81 // when the file watcher is initialized, since this file may have changed |
| 70 // between the initial load and creating watcher. | 82 // between the initial load and creating watcher. |
| 71 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 83 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); |
| 72 loop_.RunAllPending(); | 84 loop_.RunAllPending(); |
| 73 // A third and final call to Load is made by the explicit Reload. This | 85 // A third and final call to Load is made by the explicit Reload. This |
| 74 // should be the one that provides the current policy. | 86 // should be the one that provides the current policy. |
| 75 PolicyMap* policies = new PolicyMap(); | 87 PolicyBundle bundle; |
| 76 policies->Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 88 bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 77 Value::CreateBooleanValue(true)); | 89 .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 78 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies)); | 90 base::Value::CreateBooleanValue(true)); |
| 91 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&bundle)); | |
| 79 MockConfigurationPolicyObserver observer; | 92 MockConfigurationPolicyObserver observer; |
| 80 ConfigurationPolicyObserverRegistrar registrar; | 93 ConfigurationPolicyObserverRegistrar registrar; |
| 81 registrar.Init(&file_based_provider, &observer); | 94 registrar.Init(&file_based_provider, &observer); |
| 82 EXPECT_CALL(observer, OnUpdatePolicy(&file_based_provider)).Times(1); | 95 EXPECT_CALL(observer, OnUpdatePolicy(&file_based_provider)).Times(1); |
| 83 file_based_provider.RefreshPolicies(); | 96 file_based_provider.RefreshPolicies(); |
| 84 loop_.RunAllPending(); | 97 loop_.RunAllPending(); |
| 85 PolicyMap policy_map; | 98 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 } | 99 } |
| 92 | 100 |
| 93 } // namespace policy | 101 } // namespace policy |
| OLD | NEW |