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