| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/callback.h" | 6 #include "base/callback.h" |
| 7 #include "chrome/browser/policy/asynchronous_policy_loader.h" | 7 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
| 8 #include "chrome/browser/policy/asynchronous_policy_provider.h" | 8 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
| 9 #include "chrome/browser/policy/asynchronous_policy_test_base.h" | 9 #include "chrome/browser/policy/asynchronous_policy_test_base.h" |
| 10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 | 12 |
| 13 using ::testing::InSequence; | 13 using ::testing::InSequence; |
| 14 using ::testing::Mock; | 14 using ::testing::Mock; |
| 15 using ::testing::Return; | 15 using ::testing::Return; |
| 16 using ::testing::_; | 16 using ::testing::_; |
| 17 | 17 |
| 18 namespace policy { | 18 namespace policy { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 void IgnoreCallback() { | 22 void IgnoreCallback() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 class MockConfigurationPolicyObserver | 27 class MockConfigurationPolicyObserver |
| 28 : public ConfigurationPolicyProvider::Observer { | 28 : public ConfigurationPolicyProvider::Observer { |
| 29 public: | 29 public: |
| 30 MOCK_METHOD0(OnUpdatePolicy, void()); | 30 MOCK_METHOD1(OnUpdatePolicy, void(ConfigurationPolicyProvider*)); |
| 31 void OnProviderGoingAway() {} | 31 void OnProviderGoingAway() {} |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 class AsynchronousPolicyLoaderTest : public AsynchronousPolicyTestBase { | 34 class AsynchronousPolicyLoaderTest : public AsynchronousPolicyTestBase { |
| 35 public: | 35 public: |
| 36 AsynchronousPolicyLoaderTest() {} | 36 AsynchronousPolicyLoaderTest() {} |
| 37 virtual ~AsynchronousPolicyLoaderTest() {} | 37 virtual ~AsynchronousPolicyLoaderTest() {} |
| 38 | 38 |
| 39 virtual void SetUp() { | 39 virtual void SetUp() { |
| 40 AsynchronousPolicyTestBase::SetUp(); | 40 AsynchronousPolicyTestBase::SetUp(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 InSequence s; | 82 InSequence s; |
| 83 ProviderDelegateMock* delegate = new ProviderDelegateMock(); | 83 ProviderDelegateMock* delegate = new ProviderDelegateMock(); |
| 84 EXPECT_CALL(*delegate, Load()).WillOnce( | 84 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 85 CreateSequencedTestDictionary(&dictionary_number)); | 85 CreateSequencedTestDictionary(&dictionary_number)); |
| 86 EXPECT_CALL(*delegate, Load()).WillOnce( | 86 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 87 CreateSequencedTestDictionary(&dictionary_number)); | 87 CreateSequencedTestDictionary(&dictionary_number)); |
| 88 scoped_refptr<AsynchronousPolicyLoader> loader = | 88 scoped_refptr<AsynchronousPolicyLoader> loader = |
| 89 new AsynchronousPolicyLoader(delegate, 10); | 89 new AsynchronousPolicyLoader(delegate, 10); |
| 90 loader->Init(ignore_callback_); | 90 loader->Init(ignore_callback_); |
| 91 loop_.RunAllPending(); | 91 loop_.RunAllPending(); |
| 92 loader->Reload(true); | 92 loader->Reload(ignore_callback_, true); |
| 93 loop_.RunAllPending(); | 93 loop_.RunAllPending(); |
| 94 | 94 |
| 95 const DictionaryValue* loaded_dict(loader->policy()); | 95 const DictionaryValue* loaded_dict(loader->policy()); |
| 96 int loaded_number; | 96 int loaded_number; |
| 97 EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number)); | 97 EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number)); |
| 98 EXPECT_EQ(dictionary_number, loaded_number); | 98 EXPECT_EQ(dictionary_number, loaded_number); |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Ensure that calling stop on the loader stops subsequent reloads from | 101 // Ensure that calling stop on the loader stops subsequent reloads from |
| 102 // happening. | 102 // happening. |
| 103 TEST_F(AsynchronousPolicyLoaderTest, Stop) { | 103 TEST_F(AsynchronousPolicyLoaderTest, Stop) { |
| 104 ProviderDelegateMock* delegate = new ProviderDelegateMock(); | 104 ProviderDelegateMock* delegate = new ProviderDelegateMock(); |
| 105 ON_CALL(*delegate, Load()).WillByDefault(CreateTestDictionary()); | 105 ON_CALL(*delegate, Load()).WillByDefault(CreateTestDictionary()); |
| 106 EXPECT_CALL(*delegate, Load()).Times(1); | 106 EXPECT_CALL(*delegate, Load()).Times(1); |
| 107 scoped_refptr<AsynchronousPolicyLoader> loader = | 107 scoped_refptr<AsynchronousPolicyLoader> loader = |
| 108 new AsynchronousPolicyLoader(delegate, 10); | 108 new AsynchronousPolicyLoader(delegate, 10); |
| 109 loader->Init(ignore_callback_); | 109 loader->Init(ignore_callback_); |
| 110 loop_.RunAllPending(); | 110 loop_.RunAllPending(); |
| 111 loader->Stop(); | 111 loader->Stop(); |
| 112 loop_.RunAllPending(); | 112 loop_.RunAllPending(); |
| 113 loader->Reload(true); | 113 loader->Reload(ignore_callback_, true); |
| 114 loop_.RunAllPending(); | 114 loop_.RunAllPending(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Verifies that the provider is notified upon policy reload. | 117 // Verifies that the provider is notified upon policy reload. |
| 118 TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) { | 118 TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) { |
| 119 InSequence s; | 119 InSequence s; |
| 120 MockConfigurationPolicyObserver observer; | 120 MockConfigurationPolicyObserver observer; |
| 121 int dictionary_number_1 = 0; | 121 int dictionary_number_1 = 0; |
| 122 int dictionary_number_2 = 0; | 122 int dictionary_number_2 = 0; |
| 123 | 123 |
| 124 ProviderDelegateMock* delegate = new ProviderDelegateMock(); | 124 ProviderDelegateMock* delegate = new ProviderDelegateMock(); |
| 125 EXPECT_CALL(*delegate, Load()).WillOnce( | 125 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 126 CreateSequencedTestDictionary(&dictionary_number_1)); | 126 CreateSequencedTestDictionary(&dictionary_number_1)); |
| 127 | 127 |
| 128 scoped_refptr<AsynchronousPolicyLoader> loader = | 128 scoped_refptr<AsynchronousPolicyLoader> loader = |
| 129 new AsynchronousPolicyLoader(delegate, 10); | 129 new AsynchronousPolicyLoader(delegate, 10); |
| 130 AsynchronousPolicyProvider provider(NULL, loader); | 130 AsynchronousPolicyProvider provider(NULL, loader); |
| 131 // |registrar| must be declared last so that it is destroyed first. | 131 // |registrar| must be declared last so that it is destroyed first. |
| 132 ConfigurationPolicyObserverRegistrar registrar; | 132 ConfigurationPolicyObserverRegistrar registrar; |
| 133 registrar.Init(&provider, &observer); | 133 registrar.Init(&provider, &observer); |
| 134 Mock::VerifyAndClearExpectations(delegate); | 134 Mock::VerifyAndClearExpectations(delegate); |
| 135 | 135 |
| 136 EXPECT_CALL(*delegate, Load()).WillOnce( | 136 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 137 CreateSequencedTestDictionary(&dictionary_number_2)); | 137 CreateSequencedTestDictionary(&dictionary_number_2)); |
| 138 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | 138 EXPECT_CALL(observer, OnUpdatePolicy(_)).Times(1); |
| 139 loader->Reload(true); | 139 provider.RefreshPolicies(); |
| 140 loop_.RunAllPending(); | 140 loop_.RunAllPending(); |
| 141 Mock::VerifyAndClearExpectations(delegate); | 141 Mock::VerifyAndClearExpectations(delegate); |
| 142 Mock::VerifyAndClearExpectations(&observer); | 142 Mock::VerifyAndClearExpectations(&observer); |
| 143 | 143 |
| 144 EXPECT_CALL(*delegate, Load()).WillOnce( | 144 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 145 CreateSequencedTestDictionary(&dictionary_number_1)); | 145 CreateSequencedTestDictionary(&dictionary_number_1)); |
| 146 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | 146 EXPECT_CALL(observer, OnUpdatePolicy(_)).Times(1); |
| 147 loader->Reload(true); | 147 provider.RefreshPolicies(); |
| 148 loop_.RunAllPending(); | 148 loop_.RunAllPending(); |
| 149 Mock::VerifyAndClearExpectations(delegate); | 149 Mock::VerifyAndClearExpectations(delegate); |
| 150 Mock::VerifyAndClearExpectations(&observer); | 150 Mock::VerifyAndClearExpectations(&observer); |
| 151 } | 151 } |
| 152 | 152 |
| 153 } // namespace policy | 153 } // namespace policy |
| OLD | NEW |