| 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 // Defensive copy in case OnStoreReset calls Add/RemoveAppHandler. |
| 105 std::vector<gcm::GCMAppHandler*> app_handler_values; |
| 106 for (const auto& key_value : app_handlers()) |
| 107 app_handler_values.push_back(key_value.second); |
| 108 for (gcm::GCMAppHandler* app_handler : app_handler_values) { |
| 109 app_handler->OnStoreReset(); |
| 110 // app_handler might now have been deleted. |
| 111 } |
| 112 } |
| 113 |
| 103 private: | 114 private: |
| 104 gcm::GCMConnectionObserver* observer_ = nullptr; | 115 gcm::GCMConnectionObserver* observer_ = nullptr; |
| 105 | 116 |
| 106 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver); | 117 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver); |
| 107 }; | 118 }; |
| 108 | 119 |
| 109 class HeartbeatSchedulerTest : public testing::Test { | 120 class HeartbeatSchedulerTest : public testing::Test { |
| 110 public: | 121 public: |
| 111 HeartbeatSchedulerTest() | 122 HeartbeatSchedulerTest() |
| 112 : task_runner_(new base::TestSimpleTaskRunner()), | 123 : task_runner_(new base::TestSimpleTaskRunner()), |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 IgnoreUpstreamNotificationMsg(); | 214 IgnoreUpstreamNotificationMsg(); |
| 204 gcm_driver_.IgnoreDefaultHeartbeatsInterval(); | 215 gcm_driver_.IgnoreDefaultHeartbeatsInterval(); |
| 205 | 216 |
| 206 // Once we have successfully registered, we should send a heartbeat. | 217 // Once we have successfully registered, we should send a heartbeat. |
| 207 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); | 218 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
| 208 gcm_driver_.CompleteRegistration( | 219 gcm_driver_.CompleteRegistration( |
| 209 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 220 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 210 task_runner_->RunPendingTasks(); | 221 task_runner_->RunPendingTasks(); |
| 211 } | 222 } |
| 212 | 223 |
| 224 TEST_F(HeartbeatSchedulerTest, StoreResetDuringRegistration) { |
| 225 IgnoreUpstreamNotificationMsg(); |
| 226 |
| 227 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 228 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 229 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 230 |
| 231 gcm_driver_.ResetStore(); |
| 232 |
| 233 IgnoreUpstreamNotificationMsg(); |
| 234 |
| 235 // Successful registration handled ok despite store reset. |
| 236 EXPECT_FALSE(task_runner_->HasPendingTask()); |
| 237 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
| 238 gcm_driver_.CompleteRegistration( |
| 239 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 240 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 241 task_runner_->RunPendingTasks(); |
| 242 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 243 } |
| 244 |
| 245 TEST_F(HeartbeatSchedulerTest, StoreResetAfterRegistration) { |
| 246 IgnoreUpstreamNotificationMsg(); |
| 247 |
| 248 // Start from a successful registration. |
| 249 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 250 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 251 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
| 252 gcm_driver_.CompleteRegistration( |
| 253 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 254 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 255 task_runner_->RunPendingTasks(); |
| 256 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 257 |
| 258 gcm_driver_.IgnoreDefaultHeartbeatsInterval(); |
| 259 |
| 260 // Reseting the store should trigger re-registration. |
| 261 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 262 gcm_driver_.ResetStore(); |
| 263 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 264 |
| 265 IgnoreUpstreamNotificationMsg(); |
| 266 |
| 267 // Once we have successfully re-registered, we should send a heartbeat. |
| 268 EXPECT_FALSE(task_runner_->HasPendingTask()); |
| 269 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
| 270 gcm_driver_.CompleteRegistration( |
| 271 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 272 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 273 task_runner_->RunPendingTasks(); |
| 274 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 275 } |
| 276 |
| 213 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) { | 277 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) { |
| 214 IgnoreUpstreamNotificationMsg(); | 278 IgnoreUpstreamNotificationMsg(); |
| 215 | 279 |
| 216 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 280 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 217 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | 281 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 218 gcm_driver_.CompleteRegistration( | 282 gcm_driver_.CompleteRegistration( |
| 219 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 283 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 220 | 284 |
| 221 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); | 285 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 222 // Should have a heartbeat task posted with zero delay on startup. | 286 // 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 | 414 |
| 351 EXPECT_CALL(gcm_driver_, | 415 EXPECT_CALL(gcm_driver_, |
| 352 SendImpl(kHeartbeatGCMAppID, _, IsUpstreamNotificationMsg())) | 416 SendImpl(kHeartbeatGCMAppID, _, IsUpstreamNotificationMsg())) |
| 353 .Times(AtLeast(1)); | 417 .Times(AtLeast(1)); |
| 354 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 418 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 355 | 419 |
| 356 gcm_driver_.NotifyConnected(); | 420 gcm_driver_.NotifyConnected(); |
| 357 } | 421 } |
| 358 | 422 |
| 359 } // namespace | 423 } // namespace |
| OLD | NEW |