| 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; |
| 14 using ::testing::Mock; |
| 15 using ::testing::Return; |
| 13 using ::testing::_; | 16 using ::testing::_; |
| 14 using ::testing::InSequence; | |
| 15 using ::testing::Return; | |
| 16 | 17 |
| 17 namespace policy { | 18 namespace policy { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 void IgnoreCallback() { | 22 void IgnoreCallback() { |
| 22 } | 23 } |
| 23 | 24 |
| 24 } // namespace | 25 } // namespace |
| 25 | 26 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 return test_dictionary; | 60 return test_dictionary; |
| 60 } | 61 } |
| 61 | 62 |
| 62 ACTION(RescheduleImmediatePolicyReload) { | 63 ACTION(RescheduleImmediatePolicyReload) { |
| 63 *arg1 = base::TimeDelta(); | 64 *arg1 = base::TimeDelta(); |
| 64 return false; | 65 return false; |
| 65 } | 66 } |
| 66 | 67 |
| 67 TEST_F(AsynchronousPolicyLoaderTest, InitialLoad) { | 68 TEST_F(AsynchronousPolicyLoaderTest, InitialLoad) { |
| 68 DictionaryValue* template_dict(new DictionaryValue()); | 69 DictionaryValue* template_dict(new DictionaryValue()); |
| 69 EXPECT_CALL(*delegate_, Load()).WillOnce(Return(template_dict)); | 70 ProviderDelegateMock* delegate = new ProviderDelegateMock(); |
| 71 EXPECT_CALL(*delegate, Load()).WillOnce(Return(template_dict)); |
| 70 scoped_refptr<AsynchronousPolicyLoader> loader = | 72 scoped_refptr<AsynchronousPolicyLoader> loader = |
| 71 new AsynchronousPolicyLoader(delegate_.release(), 10); | 73 new AsynchronousPolicyLoader(delegate, 10); |
| 72 loader->Init(ignore_callback_); | 74 loader->Init(ignore_callback_); |
| 73 const DictionaryValue* loaded_dict(loader->policy()); | 75 const DictionaryValue* loaded_dict(loader->policy()); |
| 74 EXPECT_TRUE(loaded_dict->Equals(template_dict)); | 76 EXPECT_TRUE(loaded_dict->Equals(template_dict)); |
| 75 } | 77 } |
| 76 | 78 |
| 77 // Verify that the fallback policy requests are made. | 79 // Verify that the fallback policy requests are made. |
| 78 TEST_F(AsynchronousPolicyLoaderTest, InitialLoadWithFallback) { | 80 TEST_F(AsynchronousPolicyLoaderTest, InitialLoadWithFallback) { |
| 79 int dictionary_number = 0; | 81 int dictionary_number = 0; |
| 80 InSequence s; | 82 InSequence s; |
| 81 EXPECT_CALL(*delegate_, Load()).WillOnce( | 83 ProviderDelegateMock* delegate = new ProviderDelegateMock(); |
| 84 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 82 CreateSequencedTestDictionary(&dictionary_number)); | 85 CreateSequencedTestDictionary(&dictionary_number)); |
| 83 EXPECT_CALL(*delegate_, Load()).WillOnce( | 86 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 84 CreateSequencedTestDictionary(&dictionary_number)); | 87 CreateSequencedTestDictionary(&dictionary_number)); |
| 85 scoped_refptr<AsynchronousPolicyLoader> loader = | 88 scoped_refptr<AsynchronousPolicyLoader> loader = |
| 86 new AsynchronousPolicyLoader(delegate_.release(), 10); | 89 new AsynchronousPolicyLoader(delegate, 10); |
| 87 loader->Init(ignore_callback_); | 90 loader->Init(ignore_callback_); |
| 88 loop_.RunAllPending(); | 91 loop_.RunAllPending(); |
| 89 loader->Reload(); | 92 loader->Reload(true); |
| 90 loop_.RunAllPending(); | 93 loop_.RunAllPending(); |
| 91 | 94 |
| 92 const DictionaryValue* loaded_dict(loader->policy()); | 95 const DictionaryValue* loaded_dict(loader->policy()); |
| 93 int loaded_number; | 96 int loaded_number; |
| 94 EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number)); | 97 EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number)); |
| 95 EXPECT_EQ(dictionary_number, loaded_number); | 98 EXPECT_EQ(dictionary_number, loaded_number); |
| 96 } | 99 } |
| 97 | 100 |
| 98 // Ensure that calling stop on the loader stops subsequent reloads from | 101 // Ensure that calling stop on the loader stops subsequent reloads from |
| 99 // happening. | 102 // happening. |
| 100 TEST_F(AsynchronousPolicyLoaderTest, Stop) { | 103 TEST_F(AsynchronousPolicyLoaderTest, Stop) { |
| 101 ON_CALL(*delegate_, Load()).WillByDefault(CreateTestDictionary()); | 104 ProviderDelegateMock* delegate = new ProviderDelegateMock(); |
| 102 EXPECT_CALL(*delegate_, Load()).Times(1); | 105 ON_CALL(*delegate, Load()).WillByDefault(CreateTestDictionary()); |
| 106 EXPECT_CALL(*delegate, Load()).Times(1); |
| 103 scoped_refptr<AsynchronousPolicyLoader> loader = | 107 scoped_refptr<AsynchronousPolicyLoader> loader = |
| 104 new AsynchronousPolicyLoader(delegate_.release(), 10); | 108 new AsynchronousPolicyLoader(delegate, 10); |
| 105 loader->Init(ignore_callback_); | 109 loader->Init(ignore_callback_); |
| 106 loop_.RunAllPending(); | 110 loop_.RunAllPending(); |
| 107 loader->Stop(); | 111 loader->Stop(); |
| 108 loop_.RunAllPending(); | 112 loop_.RunAllPending(); |
| 109 loader->Reload(); | 113 loader->Reload(true); |
| 110 loop_.RunAllPending(); | 114 loop_.RunAllPending(); |
| 111 } | 115 } |
| 112 | 116 |
| 113 // Verifies that the provider is notified upon policy reload. | 117 // Verifies that the provider is notified upon policy reload. |
| 114 TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) { | 118 TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) { |
| 115 InSequence s; | 119 InSequence s; |
| 116 MockConfigurationPolicyObserver observer; | 120 MockConfigurationPolicyObserver observer; |
| 117 int dictionary_number_1 = 0; | 121 int dictionary_number_1 = 0; |
| 118 int dictionary_number_2 = 0; | 122 int dictionary_number_2 = 0; |
| 119 EXPECT_CALL(*delegate_, Load()).WillOnce( | 123 |
| 124 ProviderDelegateMock* delegate = new ProviderDelegateMock(); |
| 125 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 120 CreateSequencedTestDictionary(&dictionary_number_1)); | 126 CreateSequencedTestDictionary(&dictionary_number_1)); |
| 121 EXPECT_CALL(*delegate_, Load()).WillOnce( | 127 |
| 122 CreateSequencedTestDictionary(&dictionary_number_2)); | |
| 123 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | |
| 124 EXPECT_CALL(*delegate_, Load()).WillOnce( | |
| 125 CreateSequencedTestDictionary(&dictionary_number_2)); | |
| 126 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | |
| 127 EXPECT_CALL(*delegate_, Load()).WillOnce( | |
| 128 CreateSequencedTestDictionary(&dictionary_number_1)); | |
| 129 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | |
| 130 scoped_refptr<AsynchronousPolicyLoader> loader = | 128 scoped_refptr<AsynchronousPolicyLoader> loader = |
| 131 new AsynchronousPolicyLoader(delegate_.release(), 10); | 129 new AsynchronousPolicyLoader(delegate, 10); |
| 132 AsynchronousPolicyProvider provider(NULL, loader); | 130 AsynchronousPolicyProvider provider(NULL, loader); |
| 133 // |registrar| must be declared last so that it is destroyed first. | 131 // |registrar| must be declared last so that it is destroyed first. |
| 134 ConfigurationPolicyObserverRegistrar registrar; | 132 ConfigurationPolicyObserverRegistrar registrar; |
| 135 registrar.Init(&provider, &observer); | 133 registrar.Init(&provider, &observer); |
| 134 Mock::VerifyAndClearExpectations(delegate); |
| 135 |
| 136 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 137 CreateSequencedTestDictionary(&dictionary_number_2)); |
| 138 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); |
| 139 loader->Reload(true); |
| 136 loop_.RunAllPending(); | 140 loop_.RunAllPending(); |
| 137 loader->Reload(); | 141 Mock::VerifyAndClearExpectations(delegate); |
| 142 Mock::VerifyAndClearExpectations(&observer); |
| 143 |
| 144 EXPECT_CALL(*delegate, Load()).WillOnce( |
| 145 CreateSequencedTestDictionary(&dictionary_number_1)); |
| 146 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); |
| 147 loader->Reload(true); |
| 138 loop_.RunAllPending(); | 148 loop_.RunAllPending(); |
| 139 loader->Reload(); | 149 Mock::VerifyAndClearExpectations(delegate); |
| 140 loop_.RunAllPending(); | 150 Mock::VerifyAndClearExpectations(&observer); |
| 141 loader->Reload(); | |
| 142 loop_.RunAllPending(); | |
| 143 } | 151 } |
| 144 | 152 |
| 145 } // namespace policy | 153 } // namespace policy |
| OLD | NEW |