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 it wrapped | |
| 33 // in a passed scoped_ptr. | |
| 34 virtual scoped_ptr<PolicyBundle> Load() OVERRIDE { | |
| 35 scoped_ptr<PolicyBundle> bundle(MockLoad()); | |
| 36 return bundle.Pass(); | |
| 37 } | |
| 38 | |
| 39 MOCK_METHOD0(MockLoad, PolicyBundle*()); | |
|
Mattias Nissler (ping if slow)
2012/05/14 16:55:59
Should document that this actually needs to return
Joao da Silva
2012/05/15 13:07:05
Modified as suggested before.
| |
| 29 MOCK_METHOD0(GetLastModification, base::Time()); | 40 MOCK_METHOD0(GetLastModification, base::Time()); |
| 30 }; | 41 }; |
| 31 | 42 |
| 32 TEST_F(AsynchronousPolicyTestBase, ProviderInit) { | 43 TEST_F(AsynchronousPolicyTestBase, ProviderInit) { |
| 33 base::Time last_modified; | 44 base::Time last_modified; |
| 34 FileBasedPolicyProviderDelegateMock* provider_delegate = | 45 FileBasedPolicyProviderDelegateMock* provider_delegate = |
| 35 new FileBasedPolicyProviderDelegateMock(); | 46 new FileBasedPolicyProviderDelegateMock(); |
| 36 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( | 47 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( |
| 37 Return(last_modified)); | 48 Return(last_modified)); |
| 38 InSequence s; | 49 InSequence s; |
| 39 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 50 EXPECT_CALL(*provider_delegate, MockLoad()) |
| 40 PolicyMap* policies = new PolicyMap(); | 51 .WillOnce(Return(new PolicyBundle)); |
| 41 policies->Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 52 PolicyBundle* bundle = new PolicyBundle(); |
| 42 Value::CreateBooleanValue(true)); | 53 bundle->Get(POLICY_DOMAIN_CHROME, "") |
| 54 .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 55 base::Value::CreateBooleanValue(true)); | |
| 43 // A second call to Load gets triggered during the provider's construction | 56 // 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 | 57 // when the file watcher is initialized, since this file may have changed |
| 45 // between the initial load and creating watcher. | 58 // between the initial load and creating watcher. |
| 46 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies)); | 59 EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(bundle)); |
| 47 FileBasedPolicyProvider provider(GetChromePolicyDefinitionList(), | 60 FileBasedPolicyProvider provider(GetChromePolicyDefinitionList(), |
| 48 provider_delegate); | 61 provider_delegate); |
| 49 loop_.RunAllPending(); | 62 loop_.RunAllPending(); |
| 50 PolicyMap policy_map; | 63 PolicyBundle expected_bundle; |
| 51 provider.Provide(&policy_map); | 64 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 52 base::FundamentalValue expected(true); | 65 .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 53 EXPECT_TRUE(Value::Equals(&expected, | 66 base::Value::CreateBooleanValue(true)); |
| 54 policy_map.GetValue(key::kSyncDisabled))); | 67 EXPECT_TRUE(provider.policies().Equals(expected_bundle)); |
|
Mattias Nissler (ping if slow)
2012/05/14 16:55:59
And if you were to avoid passing ownership, you co
Joao da Silva
2012/05/15 13:07:05
Yep, done.
| |
| 55 EXPECT_EQ(1U, policy_map.size()); | |
| 56 } | 68 } |
| 57 | 69 |
| 58 TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) { | 70 TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) { |
| 59 base::Time last_modified; | 71 base::Time last_modified; |
| 60 FileBasedPolicyProviderDelegateMock* provider_delegate = | 72 FileBasedPolicyProviderDelegateMock* provider_delegate = |
| 61 new FileBasedPolicyProviderDelegateMock(); | 73 new FileBasedPolicyProviderDelegateMock(); |
| 62 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( | 74 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( |
| 63 Return(last_modified)); | 75 Return(last_modified)); |
| 64 InSequence s; | 76 InSequence s; |
| 65 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 77 EXPECT_CALL(*provider_delegate, MockLoad()) |
| 78 .WillOnce(Return(new PolicyBundle)); | |
| 66 FileBasedPolicyProvider file_based_provider(GetChromePolicyDefinitionList(), | 79 FileBasedPolicyProvider file_based_provider(GetChromePolicyDefinitionList(), |
| 67 provider_delegate); | 80 provider_delegate); |
| 68 // A second call to Load gets triggered during the provider's construction | 81 // 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 | 82 // when the file watcher is initialized, since this file may have changed |
| 70 // between the initial load and creating watcher. | 83 // between the initial load and creating watcher. |
| 71 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(new PolicyMap)); | 84 EXPECT_CALL(*provider_delegate, MockLoad()) |
| 85 .WillOnce(Return(new PolicyBundle)); | |
| 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 = new PolicyBundle(); |
| 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 PolicyBundle expected_bundle; |
| 86 file_based_provider.Provide(&policy_map); | 101 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 87 base::FundamentalValue expected(true); | 102 .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 88 EXPECT_TRUE(Value::Equals(&expected, | 103 base::Value::CreateBooleanValue(true)); |
| 89 policy_map.GetValue(key::kSyncDisabled))); | 104 EXPECT_TRUE(file_based_provider.policies().Equals(expected_bundle)); |
| 90 EXPECT_EQ(1U, policy_map.size()); | |
| 91 } | 105 } |
| 92 | 106 |
| 93 } // namespace policy | 107 } // namespace policy |
| OLD | NEW |