Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: chrome/browser/policy/device_token_fetcher_unittest.cc

Issue 11413050: chrome/browser: Update calls from RunAllPending() to RunUntilIdle(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/device_token_fetcher.h" 5 #include "chrome/browser/policy/device_token_fetcher.h"
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "chrome/browser/policy/cloud_policy_data_store.h" 9 #include "chrome/browser/policy/cloud_policy_data_store.h"
10 #include "chrome/browser/policy/logging_work_scheduler.h" 10 #include "chrome/browser/policy/logging_work_scheduler.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 virtual void SetUp() { 53 virtual void SetUp() {
54 cache_.reset(new UserPolicyCache( 54 cache_.reset(new UserPolicyCache(
55 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"), 55 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"),
56 false /* wait_for_policy_fetch */)); 56 false /* wait_for_policy_fetch */));
57 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)).Times(AnyNumber()); 57 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)).Times(AnyNumber());
58 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies()); 58 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies());
59 data_store_->AddObserver(&observer_); 59 data_store_->AddObserver(&observer_);
60 } 60 }
61 61
62 virtual void TearDown() { 62 virtual void TearDown() {
63 loop_.RunAllPending(); 63 loop_.RunUntilIdle();
64 data_store_->RemoveObserver(&observer_); 64 data_store_->RemoveObserver(&observer_);
65 } 65 }
66 66
67 void FetchToken(DeviceTokenFetcher* fetcher, CloudPolicyDataStore* store) { 67 void FetchToken(DeviceTokenFetcher* fetcher, CloudPolicyDataStore* store) {
68 store->SetupForTesting("", "fake_device_id", "fake_user_name", 68 store->SetupForTesting("", "fake_device_id", "fake_user_name",
69 "fake_auth_token", true); 69 "fake_auth_token", true);
70 fetcher->FetchToken(); 70 fetcher->FetchToken();
71 } 71 }
72 72
73 void CreateNewWaitingCache() { 73 void CreateNewWaitingCache() {
74 cache_.reset(new UserPolicyCache( 74 cache_.reset(new UserPolicyCache(
75 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"), 75 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"),
76 true /* wait_for_policy_fetch */)); 76 true /* wait_for_policy_fetch */));
77 // Make this cache's disk cache ready, but have it still waiting for a 77 // Make this cache's disk cache ready, but have it still waiting for a
78 // policy fetch. 78 // policy fetch.
79 cache_->Load(); 79 cache_->Load();
80 loop_.RunAllPending(); 80 loop_.RunUntilIdle();
81 ASSERT_TRUE(cache_->last_policy_refresh_time().is_null()); 81 ASSERT_TRUE(cache_->last_policy_refresh_time().is_null());
82 ASSERT_FALSE(cache_->IsReady()); 82 ASSERT_FALSE(cache_->IsReady());
83 } 83 }
84 84
85 void SetUpSuccessfulRegistrationExpectation(bool known_machine_id) { 85 void SetUpSuccessfulRegistrationExpectation(bool known_machine_id) {
86 EXPECT_CALL(service_, 86 EXPECT_CALL(service_,
87 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 87 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
88 .WillOnce(service_.SucceedJob(successful_registration_response_)); 88 .WillOnce(service_.SucceedJob(successful_registration_response_));
89 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)) 89 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _))
90 .WillOnce(VerifyRegisterRequest(known_machine_id)); 90 .WillOnce(VerifyRegisterRequest(known_machine_id));
(...skipping 15 matching lines...) Expand all
106 106
107 TEST_F(DeviceTokenFetcherTest, FetchToken) { 107 TEST_F(DeviceTokenFetcherTest, FetchToken) {
108 testing::InSequence s; 108 testing::InSequence s;
109 SetUpSuccessfulRegistrationExpectation(false); 109 SetUpSuccessfulRegistrationExpectation(false);
110 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 110 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
111 &notifier_); 111 &notifier_);
112 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 112 EXPECT_CALL(observer_, OnDeviceTokenChanged());
113 EXPECT_EQ("", data_store_->device_token()); 113 EXPECT_EQ("", data_store_->device_token());
114 EXPECT_EQ(DEVICE_MODE_PENDING, data_store_->device_mode()); 114 EXPECT_EQ(DEVICE_MODE_PENDING, data_store_->device_mode());
115 FetchToken(&fetcher, data_store_.get()); 115 FetchToken(&fetcher, data_store_.get());
116 loop_.RunAllPending(); 116 loop_.RunUntilIdle();
117 Mock::VerifyAndClearExpectations(&observer_); 117 Mock::VerifyAndClearExpectations(&observer_);
118 std::string token = data_store_->device_token(); 118 std::string token = data_store_->device_token();
119 EXPECT_NE("", token); 119 EXPECT_NE("", token);
120 // User policy registration should not set enrollment mode. 120 // User policy registration should not set enrollment mode.
121 EXPECT_EQ(DEVICE_MODE_PENDING, data_store_->device_mode()); 121 EXPECT_EQ(DEVICE_MODE_PENDING, data_store_->device_mode());
122 122
123 // Calling FetchToken() again should result in a new token being fetched. 123 // Calling FetchToken() again should result in a new token being fetched.
124 successful_registration_response_.mutable_register_response()-> 124 successful_registration_response_.mutable_register_response()->
125 set_device_management_token("new_fake_token"); 125 set_device_management_token("new_fake_token");
126 EXPECT_CALL(service_, 126 EXPECT_CALL(service_,
127 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 127 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
128 .WillOnce(service_.SucceedJob(successful_registration_response_)); 128 .WillOnce(service_.SucceedJob(successful_registration_response_));
129 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 129 EXPECT_CALL(observer_, OnDeviceTokenChanged());
130 FetchToken(&fetcher, data_store_.get()); 130 FetchToken(&fetcher, data_store_.get());
131 loop_.RunAllPending(); 131 loop_.RunUntilIdle();
132 Mock::VerifyAndClearExpectations(&observer_); 132 Mock::VerifyAndClearExpectations(&observer_);
133 std::string token2 = data_store_->device_token(); 133 std::string token2 = data_store_->device_token();
134 EXPECT_NE("", token2); 134 EXPECT_NE("", token2);
135 EXPECT_NE(token, token2); 135 EXPECT_NE(token, token2);
136 } 136 }
137 137
138 TEST_F(DeviceTokenFetcherTest, FetchDeviceToken) { 138 TEST_F(DeviceTokenFetcherTest, FetchDeviceToken) {
139 testing::InSequence s; 139 testing::InSequence s;
140 scoped_ptr<CloudPolicyDataStore> data_store( 140 scoped_ptr<CloudPolicyDataStore> data_store(
141 CloudPolicyDataStore::CreateForDevicePolicies()); 141 CloudPolicyDataStore::CreateForDevicePolicies());
142 SetUpSuccessfulRegistrationExpectation(false); 142 SetUpSuccessfulRegistrationExpectation(false);
143 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store.get(), 143 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store.get(),
144 &notifier_); 144 &notifier_);
145 EXPECT_EQ("", data_store->device_token()); 145 EXPECT_EQ("", data_store->device_token());
146 EXPECT_EQ(DEVICE_MODE_PENDING, data_store->device_mode()); 146 EXPECT_EQ(DEVICE_MODE_PENDING, data_store->device_mode());
147 FetchToken(&fetcher, data_store.get()); 147 FetchToken(&fetcher, data_store.get());
148 loop_.RunAllPending(); 148 loop_.RunUntilIdle();
149 EXPECT_NE("", data_store->device_token()); 149 EXPECT_NE("", data_store->device_token());
150 // For device registrations, the fetcher needs to determine device mode. 150 // For device registrations, the fetcher needs to determine device mode.
151 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, data_store->device_mode()); 151 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, data_store->device_mode());
152 } 152 }
153 153
154 TEST_F(DeviceTokenFetcherTest, FetchDeviceTokenMissingMode) { 154 TEST_F(DeviceTokenFetcherTest, FetchDeviceTokenMissingMode) {
155 testing::InSequence s; 155 testing::InSequence s;
156 scoped_ptr<CloudPolicyDataStore> data_store( 156 scoped_ptr<CloudPolicyDataStore> data_store(
157 CloudPolicyDataStore::CreateForDevicePolicies()); 157 CloudPolicyDataStore::CreateForDevicePolicies());
158 SetUpSuccessfulRegistrationExpectation(false); 158 SetUpSuccessfulRegistrationExpectation(false);
159 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store.get(), 159 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store.get(),
160 &notifier_); 160 &notifier_);
161 EXPECT_EQ("", data_store->device_token()); 161 EXPECT_EQ("", data_store->device_token());
162 EXPECT_EQ(DEVICE_MODE_PENDING, data_store->device_mode()); 162 EXPECT_EQ(DEVICE_MODE_PENDING, data_store->device_mode());
163 successful_registration_response_.mutable_register_response()-> 163 successful_registration_response_.mutable_register_response()->
164 clear_enrollment_type(); 164 clear_enrollment_type();
165 FetchToken(&fetcher, data_store.get()); 165 FetchToken(&fetcher, data_store.get());
166 loop_.RunAllPending(); 166 loop_.RunUntilIdle();
167 Mock::VerifyAndClearExpectations(&observer_); 167 Mock::VerifyAndClearExpectations(&observer_);
168 EXPECT_NE("", data_store->device_token()); 168 EXPECT_NE("", data_store->device_token());
169 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, data_store->device_mode()); 169 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, data_store->device_mode());
170 } 170 }
171 171
172 TEST_F(DeviceTokenFetcherTest, RetryOnError) { 172 TEST_F(DeviceTokenFetcherTest, RetryOnError) {
173 testing::InSequence s; 173 testing::InSequence s;
174 EXPECT_CALL(service_, 174 EXPECT_CALL(service_,
175 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 175 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
176 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)) 176 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED))
177 .WillOnce(service_.SucceedJob(successful_registration_response_)); 177 .WillOnce(service_.SucceedJob(successful_registration_response_));
178 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 178 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
179 &notifier_, new DummyWorkScheduler); 179 &notifier_, new DummyWorkScheduler);
180 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 180 EXPECT_CALL(observer_, OnDeviceTokenChanged());
181 FetchToken(&fetcher, data_store_.get()); 181 FetchToken(&fetcher, data_store_.get());
182 loop_.RunAllPending(); 182 loop_.RunUntilIdle();
183 Mock::VerifyAndClearExpectations(&observer_); 183 Mock::VerifyAndClearExpectations(&observer_);
184 EXPECT_NE("", data_store_->device_token()); 184 EXPECT_NE("", data_store_->device_token());
185 } 185 }
186 186
187 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { 187 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) {
188 EXPECT_CALL(service_, 188 EXPECT_CALL(service_,
189 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 189 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
190 .WillOnce(service_.FailJob(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED)); 190 .WillOnce(service_.FailJob(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED));
191 EXPECT_FALSE(cache_->is_unmanaged()); 191 EXPECT_FALSE(cache_->is_unmanaged());
192 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 192 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
193 &notifier_); 193 &notifier_);
194 EXPECT_CALL(observer_, OnDeviceTokenChanged()).Times(0); 194 EXPECT_CALL(observer_, OnDeviceTokenChanged()).Times(0);
195 FetchToken(&fetcher, data_store_.get()); 195 FetchToken(&fetcher, data_store_.get());
196 loop_.RunAllPending(); 196 loop_.RunUntilIdle();
197 Mock::VerifyAndClearExpectations(&observer_); 197 Mock::VerifyAndClearExpectations(&observer_);
198 EXPECT_EQ("", data_store_->device_token()); 198 EXPECT_EQ("", data_store_->device_token());
199 EXPECT_TRUE(cache_->is_unmanaged()); 199 EXPECT_TRUE(cache_->is_unmanaged());
200 } 200 }
201 201
202 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDone) { 202 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDone) {
203 CreateNewWaitingCache(); 203 CreateNewWaitingCache();
204 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 204 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
205 &notifier_); 205 &notifier_);
206 EXPECT_FALSE(cache_->IsReady()); 206 EXPECT_FALSE(cache_->IsReady());
207 } 207 }
208 208
209 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDoneWithoutPolicyFetch) { 209 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDoneWithoutPolicyFetch) {
210 CreateNewWaitingCache(); 210 CreateNewWaitingCache();
211 EXPECT_CALL(service_, 211 EXPECT_CALL(service_,
212 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 212 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
213 .WillOnce(service_.SucceedJob(successful_registration_response_)); 213 .WillOnce(service_.SucceedJob(successful_registration_response_));
214 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 214 EXPECT_CALL(observer_, OnDeviceTokenChanged());
215 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 215 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
216 &notifier_); 216 &notifier_);
217 FetchToken(&fetcher, data_store_.get()); 217 FetchToken(&fetcher, data_store_.get());
218 loop_.RunAllPending(); 218 loop_.RunUntilIdle();
219 // On successful token fetching the cache isn't set to ready, since the next 219 // On successful token fetching the cache isn't set to ready, since the next
220 // step is to fetch policy. Only failures to fetch the token should make 220 // step is to fetch policy. Only failures to fetch the token should make
221 // the cache ready. 221 // the cache ready.
222 EXPECT_FALSE(cache_->IsReady()); 222 EXPECT_FALSE(cache_->IsReady());
223 } 223 }
224 224
225 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneWhenUnmanaged) { 225 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneWhenUnmanaged) {
226 CreateNewWaitingCache(); 226 CreateNewWaitingCache();
227 cache_->SetUnmanaged(); 227 cache_->SetUnmanaged();
228 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 228 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
229 &notifier_); 229 &notifier_);
230 EXPECT_TRUE(cache_->IsReady()); 230 EXPECT_TRUE(cache_->IsReady());
231 } 231 }
232 232
233 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneOnFailures) { 233 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneOnFailures) {
234 CreateNewWaitingCache(); 234 CreateNewWaitingCache();
235 EXPECT_CALL(service_, 235 EXPECT_CALL(service_,
236 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 236 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
237 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); 237 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
238 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 238 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
239 &notifier_); 239 &notifier_);
240 FetchToken(&fetcher, data_store_.get()); 240 FetchToken(&fetcher, data_store_.get());
241 loop_.RunAllPending(); 241 loop_.RunUntilIdle();
242 // This is the opposite case of DontSetFetchingDone1. 242 // This is the opposite case of DontSetFetchingDone1.
243 EXPECT_TRUE(cache_->IsReady()); 243 EXPECT_TRUE(cache_->IsReady());
244 } 244 }
245 245
246 TEST_F(DeviceTokenFetcherTest, SetKnownMachineId) { 246 TEST_F(DeviceTokenFetcherTest, SetKnownMachineId) {
247 SetUpSuccessfulRegistrationExpectation(true); 247 SetUpSuccessfulRegistrationExpectation(true);
248 248
249 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 249 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
250 &notifier_); 250 &notifier_);
251 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 251 EXPECT_CALL(observer_, OnDeviceTokenChanged());
252 EXPECT_EQ("", data_store_->device_token()); 252 EXPECT_EQ("", data_store_->device_token());
253 253
254 data_store_->set_known_machine_id(true); 254 data_store_->set_known_machine_id(true);
255 FetchToken(&fetcher, data_store_.get()); 255 FetchToken(&fetcher, data_store_.get());
256 loop_.RunAllPending(); 256 loop_.RunUntilIdle();
257 257
258 Mock::VerifyAndClearExpectations(&observer_); 258 Mock::VerifyAndClearExpectations(&observer_);
259 std::string token = data_store_->device_token(); 259 std::string token = data_store_->device_token();
260 EXPECT_NE("", token); 260 EXPECT_NE("", token);
261 } 261 }
262 262
263 } // namespace policy 263 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/device_status_collector_unittest.cc ('k') | chrome/browser/policy/logging_work_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698