OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/chromeos/policy/heartbeat_scheduler.h" | 5 #include "chrome/browser/chromeos/policy/heartbeat_scheduler.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 ASSERT_TRUE(observer_); | 93 ASSERT_TRUE(observer_); |
94 observer_->OnConnected(net::IPEndPoint()); | 94 observer_->OnConnected(net::IPEndPoint()); |
95 } | 95 } |
96 | 96 |
97 void IgnoreDefaultHeartbeatsInterval() { | 97 void IgnoreDefaultHeartbeatsInterval() { |
98 // Ignore default setting for heartbeats interval. | 98 // Ignore default setting for heartbeats interval. |
99 EXPECT_CALL(*this, AddHeartbeatInterval(_, 2 * 60 * 1000)) | 99 EXPECT_CALL(*this, AddHeartbeatInterval(_, 2 * 60 * 1000)) |
100 .Times(AnyNumber()); | 100 .Times(AnyNumber()); |
101 } | 101 } |
102 | 102 |
| 103 void ResetStore() { |
| 104 for (const auto& key_value : app_handlers()) |
| 105 key_value.second->OnStoreReset(); |
| 106 } |
| 107 |
103 private: | 108 private: |
104 gcm::GCMConnectionObserver* observer_ = nullptr; | 109 gcm::GCMConnectionObserver* observer_ = nullptr; |
105 | 110 |
106 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver); | 111 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver); |
107 }; | 112 }; |
108 | 113 |
109 class HeartbeatSchedulerTest : public testing::Test { | 114 class HeartbeatSchedulerTest : public testing::Test { |
110 public: | 115 public: |
111 HeartbeatSchedulerTest() | 116 HeartbeatSchedulerTest() |
112 : task_runner_(new base::TestSimpleTaskRunner()), | 117 : task_runner_(new base::TestSimpleTaskRunner()), |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 IgnoreUpstreamNotificationMsg(); | 208 IgnoreUpstreamNotificationMsg(); |
204 gcm_driver_.IgnoreDefaultHeartbeatsInterval(); | 209 gcm_driver_.IgnoreDefaultHeartbeatsInterval(); |
205 | 210 |
206 // Once we have successfully registered, we should send a heartbeat. | 211 // Once we have successfully registered, we should send a heartbeat. |
207 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); | 212 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
208 gcm_driver_.CompleteRegistration( | 213 gcm_driver_.CompleteRegistration( |
209 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 214 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
210 task_runner_->RunPendingTasks(); | 215 task_runner_->RunPendingTasks(); |
211 } | 216 } |
212 | 217 |
| 218 TEST_F(HeartbeatSchedulerTest, StoreResetDuringRegistration) { |
| 219 IgnoreUpstreamNotificationMsg(); |
| 220 |
| 221 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 222 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 223 |
| 224 gcm_driver_.ResetStore(); |
| 225 |
| 226 // Successful registration handled ok despite store reset. |
| 227 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
| 228 gcm_driver_.CompleteRegistration( |
| 229 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 230 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 231 task_runner_->RunPendingTasks(); |
| 232 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 233 } |
| 234 |
| 235 TEST_F(HeartbeatSchedulerTest, StoreResetAfterRegistration) { |
| 236 IgnoreUpstreamNotificationMsg(); |
| 237 |
| 238 // Start from a successful registration. |
| 239 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 240 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 241 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
| 242 gcm_driver_.CompleteRegistration( |
| 243 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 244 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 245 task_runner_->RunPendingTasks(); |
| 246 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 247 |
| 248 IgnoreUpstreamNotificationMsg(); |
| 249 gcm_driver_.IgnoreDefaultHeartbeatsInterval(); |
| 250 |
| 251 // Since registration succeeded, no need for a pending task. |
| 252 EXPECT_FALSE(task_runner_->HasPendingTask()); |
| 253 |
| 254 // Reseting the store should trigger re-registration. |
| 255 gcm_driver_.ResetStore(); |
| 256 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 257 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 258 task_runner_->RunPendingTasks(); |
| 259 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 260 |
| 261 // Once we have successfully re-registered, we should send a heartbeat. |
| 262 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
| 263 gcm_driver_.CompleteRegistration( |
| 264 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 265 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 266 task_runner_->RunPendingTasks(); |
| 267 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 268 } |
| 269 |
213 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) { | 270 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) { |
214 IgnoreUpstreamNotificationMsg(); | 271 IgnoreUpstreamNotificationMsg(); |
215 | 272 |
216 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 273 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
217 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | 274 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
218 gcm_driver_.CompleteRegistration( | 275 gcm_driver_.CompleteRegistration( |
219 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 276 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
220 | 277 |
221 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); | 278 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
222 // Should have a heartbeat task posted with zero delay on startup. | 279 // Should have a heartbeat task posted with zero delay on startup. |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 | 407 |
351 EXPECT_CALL(gcm_driver_, | 408 EXPECT_CALL(gcm_driver_, |
352 SendImpl(kHeartbeatGCMAppID, _, IsUpstreamNotificationMsg())) | 409 SendImpl(kHeartbeatGCMAppID, _, IsUpstreamNotificationMsg())) |
353 .Times(AtLeast(1)); | 410 .Times(AtLeast(1)); |
354 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 411 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
355 | 412 |
356 gcm_driver_.NotifyConnected(); | 413 gcm_driver_.NotifyConnected(); |
357 } | 414 } |
358 | 415 |
359 } // namespace | 416 } // namespace |
OLD | NEW |