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 "chrome/browser/policy/cloud_policy_service.h" | 5 #include "chrome/browser/policy/cloud_policy_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "chrome/browser/policy/mock_cloud_policy_client.h" | 9 #include "chrome/browser/policy/mock_cloud_policy_client.h" |
| 10 #include "chrome/browser/policy/mock_cloud_policy_store.h" | 10 #include "chrome/browser/policy/mock_cloud_policy_store.h" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 MOCK_METHOD1(OnInitializationCompleted, void(CloudPolicyService* service)); | 26 MOCK_METHOD1(OnInitializationCompleted, void(CloudPolicyService* service)); |
| 27 private: | 27 private: |
| 28 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyServiceObserver); | 28 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyServiceObserver); |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 class CloudPolicyServiceTest : public testing::Test { | 31 class CloudPolicyServiceTest : public testing::Test { |
| 32 public: | 32 public: |
| 33 CloudPolicyServiceTest() | 33 CloudPolicyServiceTest() |
| 34 : service_(&client_, &store_) {} | 34 : service_(&client_, &store_) {} |
| 35 | 35 |
| 36 MOCK_METHOD0(OnPolicyRefresh, void(void)); | 36 MOCK_METHOD1(OnPolicyRefresh, void(bool)); |
| 37 | 37 |
| 38 protected: | 38 protected: |
| 39 MockCloudPolicyClient client_; | 39 MockCloudPolicyClient client_; |
| 40 MockCloudPolicyStore store_; | 40 MockCloudPolicyStore store_; |
| 41 CloudPolicyService service_; | 41 CloudPolicyService service_; |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 MATCHER_P(ProtoMatches, proto, "") { | 44 MATCHER_P(ProtoMatches, proto, "") { |
| 45 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); | 45 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); |
| 46 } | 46 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 | 83 |
| 84 TEST_F(CloudPolicyServiceTest, PolicyUpdateClientFailure) { | 84 TEST_F(CloudPolicyServiceTest, PolicyUpdateClientFailure) { |
| 85 client_.SetStatus(DM_STATUS_REQUEST_FAILED); | 85 client_.SetStatus(DM_STATUS_REQUEST_FAILED); |
| 86 EXPECT_CALL(store_, Store(_)).Times(0); | 86 EXPECT_CALL(store_, Store(_)).Times(0); |
| 87 client_.NotifyPolicyFetched(); | 87 client_.NotifyPolicyFetched(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 TEST_F(CloudPolicyServiceTest, RefreshPolicySuccess) { | 90 TEST_F(CloudPolicyServiceTest, RefreshPolicySuccess) { |
| 91 testing::InSequence seq; | 91 testing::InSequence seq; |
| 92 | 92 |
| 93 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 93 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); |
|
Mattias Nissler (ping if slow)
2012/12/07 15:20:45
Hm, all these violate the "don't mix expectation s
Andrew T Wilson (Slow)
2012/12/07 17:34:29
What is that rule?
Mattias Nissler (ping if slow)
2012/12/10 09:01:08
From https://code.google.com/p/googlemock/wiki/For
| |
| 94 client_.SetDMToken("fake token"); | 94 client_.SetDMToken("fake token"); |
| 95 | 95 |
| 96 // Trigger a fetch on the client. | 96 // Trigger a fetch on the client. |
| 97 EXPECT_CALL(client_, FetchPolicy()).Times(1); | 97 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 98 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 98 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 99 base::Unretained(this))); | 99 base::Unretained(this))); |
| 100 | 100 |
| 101 // Client responds, push policy to store. | 101 // Client responds, push policy to store. |
| 102 em::PolicyFetchResponse policy; | 102 em::PolicyFetchResponse policy; |
| 103 policy.set_policy_data("fake policy"); | 103 policy.set_policy_data("fake policy"); |
| 104 client_.SetPolicy(policy); | 104 client_.SetPolicy(policy); |
| 105 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | 105 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 106 client_.NotifyPolicyFetched(); | 106 client_.NotifyPolicyFetched(); |
| 107 | 107 |
| 108 // Store reloads policy, callback gets triggered. | 108 // Store reloads policy, callback gets triggered. |
| 109 store_.policy_.reset(new em::PolicyData()); | 109 store_.policy_.reset(new em::PolicyData()); |
| 110 store_.policy_->set_request_token("token"); | 110 store_.policy_->set_request_token("token"); |
| 111 store_.policy_->set_device_id("device-id"); | 111 store_.policy_->set_device_id("device-id"); |
| 112 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); | 112 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(1); |
| 113 store_.NotifyStoreLoaded(); | 113 store_.NotifyStoreLoaded(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST_F(CloudPolicyServiceTest, RefreshPolicyNotRegistered) { | 116 TEST_F(CloudPolicyServiceTest, RefreshPolicyNotRegistered) { |
| 117 // Clear the token so the client is not registered. | 117 // Clear the token so the client is not registered. |
| 118 client_.SetDMToken(""); | 118 client_.SetDMToken(""); |
| 119 | 119 |
| 120 EXPECT_CALL(client_, FetchPolicy()).Times(0); | 120 EXPECT_CALL(client_, FetchPolicy()).Times(0); |
| 121 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); | 121 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); |
| 122 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 122 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 123 base::Unretained(this))); | 123 base::Unretained(this))); |
| 124 } | 124 } |
| 125 | 125 |
| 126 TEST_F(CloudPolicyServiceTest, RefreshPolicyClientError) { | 126 TEST_F(CloudPolicyServiceTest, RefreshPolicyClientError) { |
| 127 testing::InSequence seq; | 127 testing::InSequence seq; |
| 128 | 128 |
| 129 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 129 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); |
| 130 client_.SetDMToken("fake token"); | 130 client_.SetDMToken("fake token"); |
| 131 | 131 |
| 132 // Trigger a fetch on the client. | 132 // Trigger a fetch on the client. |
| 133 EXPECT_CALL(client_, FetchPolicy()).Times(1); | 133 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 134 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 134 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 135 base::Unretained(this))); | 135 base::Unretained(this))); |
| 136 | 136 |
| 137 // Client responds with an error, which should trigger the callback. | 137 // Client responds with an error, which should trigger the callback. |
| 138 client_.SetStatus(DM_STATUS_REQUEST_FAILED); | 138 client_.SetStatus(DM_STATUS_REQUEST_FAILED); |
| 139 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); | 139 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); |
| 140 client_.NotifyClientError(); | 140 client_.NotifyClientError(); |
| 141 } | 141 } |
| 142 | 142 |
| 143 TEST_F(CloudPolicyServiceTest, RefreshPolicyStoreError) { | 143 TEST_F(CloudPolicyServiceTest, RefreshPolicyStoreError) { |
| 144 testing::InSequence seq; | 144 testing::InSequence seq; |
| 145 | 145 |
| 146 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 146 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); |
| 147 client_.SetDMToken("fake token"); | 147 client_.SetDMToken("fake token"); |
| 148 | 148 |
| 149 // Trigger a fetch on the client. | 149 // Trigger a fetch on the client. |
| 150 EXPECT_CALL(client_, FetchPolicy()).Times(1); | 150 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 151 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 151 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 152 base::Unretained(this))); | 152 base::Unretained(this))); |
| 153 | 153 |
| 154 // Client responds, push policy to store. | 154 // Client responds, push policy to store. |
| 155 em::PolicyFetchResponse policy; | 155 em::PolicyFetchResponse policy; |
| 156 policy.set_policy_data("fake policy"); | 156 policy.set_policy_data("fake policy"); |
| 157 client_.SetPolicy(policy); | 157 client_.SetPolicy(policy); |
| 158 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | 158 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 159 client_.NotifyPolicyFetched(); | 159 client_.NotifyPolicyFetched(); |
| 160 | 160 |
| 161 // Store fails, which should trigger the callback. | 161 // Store fails, which should trigger the callback. |
| 162 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); | 162 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); |
| 163 store_.NotifyStoreError(); | 163 store_.NotifyStoreError(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 TEST_F(CloudPolicyServiceTest, RefreshPolicyConcurrent) { | 166 TEST_F(CloudPolicyServiceTest, RefreshPolicyConcurrent) { |
| 167 testing::InSequence seq; | 167 testing::InSequence seq; |
| 168 | 168 |
| 169 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 169 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); |
| 170 client_.SetDMToken("fake token"); | 170 client_.SetDMToken("fake token"); |
| 171 | 171 |
| 172 // Trigger a fetch on the client. | 172 // Trigger a fetch on the client. |
| 173 EXPECT_CALL(client_, FetchPolicy()).Times(1); | 173 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 174 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 174 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 175 base::Unretained(this))); | 175 base::Unretained(this))); |
| 176 | 176 |
| 177 // Triggering another policy refresh should generate a new fetch request. | 177 // Triggering another policy refresh should generate a new fetch request. |
| 178 EXPECT_CALL(client_, FetchPolicy()).Times(1); | 178 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 179 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 179 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 180 base::Unretained(this))); | 180 base::Unretained(this))); |
| 181 | 181 |
| 182 // Client responds, push policy to store. | 182 // Client responds, push policy to store. |
| 183 em::PolicyFetchResponse policy; | 183 em::PolicyFetchResponse policy; |
| 184 policy.set_policy_data("fake policy"); | 184 policy.set_policy_data("fake policy"); |
| 185 client_.SetPolicy(policy); | 185 client_.SetPolicy(policy); |
| 186 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | 186 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 187 client_.NotifyPolicyFetched(); | 187 client_.NotifyPolicyFetched(); |
| 188 | 188 |
| 189 // Trigger another policy fetch. | 189 // Trigger another policy fetch. |
| 190 EXPECT_CALL(client_, FetchPolicy()).Times(1); | 190 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 191 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 191 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 192 base::Unretained(this))); | 192 base::Unretained(this))); |
| 193 | 193 |
| 194 // The store finishing the first load should not generate callbacks. | 194 // The store finishing the first load should not generate callbacks. |
| 195 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 195 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); |
| 196 store_.NotifyStoreLoaded(); | 196 store_.NotifyStoreLoaded(); |
| 197 | 197 |
| 198 // Second policy fetch finishes. | 198 // Second policy fetch finishes. |
| 199 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | 199 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 200 client_.NotifyPolicyFetched(); | 200 client_.NotifyPolicyFetched(); |
| 201 | 201 |
| 202 // Corresponding store operation finishes, all _three_ callbacks fire. | 202 // Corresponding store operation finishes, all _three_ callbacks fire. |
| 203 EXPECT_CALL(*this, OnPolicyRefresh()).Times(3); | 203 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(3); |
| 204 store_.NotifyStoreLoaded(); | 204 store_.NotifyStoreLoaded(); |
| 205 } | 205 } |
| 206 | 206 |
| 207 TEST_F(CloudPolicyServiceTest, StoreAlreadyInitialized) { | 207 TEST_F(CloudPolicyServiceTest, StoreAlreadyInitialized) { |
| 208 // Service should start off initialized if the store has already loaded | 208 // Service should start off initialized if the store has already loaded |
| 209 // policy. | 209 // policy. |
| 210 store_.NotifyStoreLoaded(); | 210 store_.NotifyStoreLoaded(); |
| 211 CloudPolicyService service(&client_, &store_); | 211 CloudPolicyService service(&client_, &store_); |
| 212 EXPECT_TRUE(service.IsInitializationComplete()); | 212 EXPECT_TRUE(service.IsInitializationComplete()); |
| 213 } | 213 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 225 testing::Mock::VerifyAndClearExpectations(&observer); | 225 testing::Mock::VerifyAndClearExpectations(&observer); |
| 226 | 226 |
| 227 // Now, the next time the store is loaded, the observer should not be called | 227 // Now, the next time the store is loaded, the observer should not be called |
| 228 // again. | 228 // again. |
| 229 EXPECT_CALL(observer, OnInitializationCompleted(&service_)).Times(0); | 229 EXPECT_CALL(observer, OnInitializationCompleted(&service_)).Times(0); |
| 230 store_.NotifyStoreLoaded(); | 230 store_.NotifyStoreLoaded(); |
| 231 service_.RemoveObserver(&observer); | 231 service_.RemoveObserver(&observer); |
| 232 } | 232 } |
| 233 | 233 |
| 234 } // namespace policy | 234 } // namespace policy |
| OLD | NEW |