Chromium Code Reviews| 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 "base/macros.h" | |
| 7 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
| 9 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" | 10 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" |
| 10 #include "chromeos/settings/cros_settings_names.h" | 11 #include "chromeos/settings/cros_settings_names.h" |
| 12 #include "components/gcm_driver/common/gcm_messages.h" | |
| 11 #include "components/gcm_driver/fake_gcm_driver.h" | 13 #include "components/gcm_driver/fake_gcm_driver.h" |
| 12 #include "components/policy/core/common/cloud/cloud_policy_client.h" | 14 #include "components/policy/core/common/cloud/cloud_policy_client.h" |
| 13 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | 15 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" |
| 14 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 17 #include "net/base/ip_endpoint.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 20 |
| 18 using ::testing::_; | 21 using ::testing::_; |
| 22 using ::testing::AnyNumber; | |
| 23 using ::testing::AtLeast; | |
| 24 using ::testing::Contains; | |
| 25 using ::testing::Field; | |
| 26 using ::testing::Key; | |
| 27 using ::testing::Matches; | |
| 28 using ::testing::Pair; | |
| 19 using ::testing::SaveArg; | 29 using ::testing::SaveArg; |
| 20 | 30 |
| 21 namespace { | 31 namespace { |
| 22 const char* const kFakeEnrollmentDomain = "example.com"; | 32 const char* const kFakeEnrollmentDomain = "example.com"; |
| 23 const char* const kFakeDeviceId = "fake_device_id"; | 33 const char* const kFakeDeviceId = "fake_device_id"; |
| 24 const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring"; | 34 const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring"; |
| 25 const char* const kRegistrationId = "registration_id"; | 35 const char* const kRegistrationId = "registration_id"; |
| 26 const char* const kDMToken = "fake_dm_token"; | 36 const char* const kDMToken = "fake_dm_token"; |
| 27 | 37 |
| 38 MATCHER(IsHeartbeatMsg, "") { | |
| 39 return Matches( | |
| 40 Field(&gcm::OutgoingMessage::data, Contains(Pair("type", "hb"))))(arg); | |
| 41 } | |
| 42 | |
| 43 MATCHER(IsUpstreamNotificationMsg, "") { | |
| 44 return Matches(Field(&gcm::OutgoingMessage::data, Contains(Key("notify"))))( | |
| 45 arg); | |
| 46 } | |
| 47 | |
| 28 class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> { | 48 class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> { |
| 29 public: | 49 public: |
| 30 MockGCMDriver() { | 50 MockGCMDriver() { |
| 31 } | 51 } |
| 32 | 52 |
| 33 ~MockGCMDriver() override { | 53 ~MockGCMDriver() override { |
| 34 } | 54 } |
| 35 | 55 |
| 36 MOCK_METHOD2(RegisterImpl, | 56 MOCK_METHOD2(RegisterImpl, |
| 37 void(const std::string&, const std::vector<std::string>&)); | 57 void(const std::string&, const std::vector<std::string>&)); |
| 38 MOCK_METHOD3(SendImpl, | 58 MOCK_METHOD3(SendImpl, |
| 39 void(const std::string&, | 59 void(const std::string&, |
| 40 const std::string&, | 60 const std::string&, |
| 41 const gcm::OutgoingMessage& message)); | 61 const gcm::OutgoingMessage& message)); |
| 42 | 62 |
| 43 // Helper function to complete a registration previously started by | 63 // Helper function to complete a registration previously started by |
| 44 // Register(). | 64 // Register(). |
| 45 void CompleteRegistration(const std::string& app_id, | 65 void CompleteRegistration(const std::string& app_id, |
| 46 gcm::GCMClient::Result result) { | 66 gcm::GCMClient::Result result) { |
| 47 RegisterFinished(app_id, kRegistrationId, result); | 67 RegisterFinished(app_id, kRegistrationId, result); |
| 48 } | 68 } |
| 49 | 69 |
| 50 // Helper function to complete a send operation previously started by | 70 // Helper function to complete a send operation previously started by |
| 51 // Send(). | 71 // Send(). |
| 52 void CompleteSend(const std::string& app_id, | 72 void CompleteSend(const std::string& app_id, |
| 53 const std::string& message_id, | 73 const std::string& message_id, |
| 54 gcm::GCMClient::Result result) { | 74 gcm::GCMClient::Result result) { |
| 55 SendFinished(app_id, message_id, result); | 75 SendFinished(app_id, message_id, result); |
| 56 } | 76 } |
| 77 | |
| 78 void AddConnectionObserver(gcm::GCMConnectionObserver* observer) override { | |
| 79 EXPECT_FALSE(observer_); | |
| 80 observer_ = observer; | |
| 81 } | |
| 82 | |
| 83 void RemoveConnectionObserver(gcm::GCMConnectionObserver* observer) override { | |
| 84 EXPECT_TRUE(observer_); | |
| 85 observer_ = nullptr; | |
| 86 } | |
| 87 | |
| 88 void NotifyConnected() { | |
| 89 ASSERT_TRUE(observer_); | |
| 90 observer_->OnConnected(net::IPEndPoint()); | |
| 91 } | |
| 92 | |
| 93 private: | |
| 94 gcm::GCMConnectionObserver* observer_ = nullptr; | |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver); | |
| 57 }; | 97 }; |
| 58 | 98 |
| 59 class HeartbeatSchedulerTest : public testing::Test { | 99 class HeartbeatSchedulerTest : public testing::Test { |
| 60 public: | 100 public: |
| 61 HeartbeatSchedulerTest() | 101 HeartbeatSchedulerTest() |
| 62 : task_runner_(new base::TestSimpleTaskRunner()), | 102 : task_runner_(new base::TestSimpleTaskRunner()), |
| 63 scheduler_(&gcm_driver_, | 103 scheduler_(&gcm_driver_, |
| 64 &cloud_policy_client_, | 104 &cloud_policy_client_, |
| 65 kFakeEnrollmentDomain, | 105 kFakeEnrollmentDomain, |
| 66 kFakeDeviceId, | 106 kFakeDeviceId, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 89 // smaller than |expected_delay|. | 129 // smaller than |expected_delay|. |
| 90 // | 130 // |
| 91 // We do know that the task was posted sometime between |last_heartbeat| | 131 // We do know that the task was posted sometime between |last_heartbeat| |
| 92 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <= | 132 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <= |
| 93 // |now| - |last_heartbeat|. | 133 // |now| - |last_heartbeat|. |
| 94 base::TimeDelta delta = expected_delay - actual_delay; | 134 base::TimeDelta delta = expected_delay - actual_delay; |
| 95 EXPECT_LE(base::TimeDelta(), delta); | 135 EXPECT_LE(base::TimeDelta(), delta); |
| 96 EXPECT_GE(now - last_heartbeat, delta); | 136 EXPECT_GE(now - last_heartbeat, delta); |
| 97 } | 137 } |
| 98 | 138 |
| 139 void IgnoreUpstreamNotificationMsg() { | |
| 140 EXPECT_CALL(gcm_driver_, | |
| 141 SendImpl(kHeartbeatGCMAppID, _, IsUpstreamNotificationMsg())) | |
| 142 .Times(AnyNumber()); | |
| 143 } | |
| 144 | |
| 99 base::MessageLoop loop_; | 145 base::MessageLoop loop_; |
| 100 MockGCMDriver gcm_driver_; | 146 MockGCMDriver gcm_driver_; |
| 101 chromeos::ScopedCrosSettingsTestHelper settings_helper_; | 147 chromeos::ScopedCrosSettingsTestHelper settings_helper_; |
| 102 testing::NiceMock<policy::MockCloudPolicyClient> cloud_policy_client_; | 148 testing::NiceMock<policy::MockCloudPolicyClient> cloud_policy_client_; |
| 103 | 149 |
| 104 // TaskRunner used to run individual tests. | 150 // TaskRunner used to run individual tests. |
| 105 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 151 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 106 | 152 |
| 107 // The HeartbeatScheduler instance under test. | 153 // The HeartbeatScheduler instance under test. |
| 108 policy::HeartbeatScheduler scheduler_; | 154 policy::HeartbeatScheduler scheduler_; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 120 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 166 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 121 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | 167 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 122 gcm_driver_.CompleteRegistration( | 168 gcm_driver_.CompleteRegistration( |
| 123 kHeartbeatGCMAppID, gcm::GCMClient::GCM_DISABLED); | 169 kHeartbeatGCMAppID, gcm::GCMClient::GCM_DISABLED); |
| 124 | 170 |
| 125 // There should be no heartbeat tasks pending, because registration failed. | 171 // There should be no heartbeat tasks pending, because registration failed. |
| 126 ASSERT_TRUE(task_runner_->GetPendingTasks().empty()); | 172 ASSERT_TRUE(task_runner_->GetPendingTasks().empty()); |
| 127 } | 173 } |
| 128 | 174 |
| 129 TEST_F(HeartbeatSchedulerTest, TemporarilyFailedGCMRegistration) { | 175 TEST_F(HeartbeatSchedulerTest, TemporarilyFailedGCMRegistration) { |
| 176 IgnoreUpstreamNotificationMsg(); | |
| 177 | |
| 130 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 178 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 131 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | 179 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 132 gcm_driver_.CompleteRegistration( | 180 gcm_driver_.CompleteRegistration( |
| 133 kHeartbeatGCMAppID, gcm::GCMClient::SERVER_ERROR); | 181 kHeartbeatGCMAppID, gcm::GCMClient::SERVER_ERROR); |
| 134 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | 182 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 135 | 183 |
| 184 IgnoreUpstreamNotificationMsg(); | |
| 185 | |
| 136 // Should have a pending task to try registering again. | 186 // Should have a pending task to try registering again. |
| 137 ASSERT_FALSE(task_runner_->GetPendingTasks().empty()); | 187 ASSERT_FALSE(task_runner_->GetPendingTasks().empty()); |
| 138 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 188 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 139 task_runner_->RunPendingTasks(); | 189 task_runner_->RunPendingTasks(); |
| 140 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | 190 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 141 | 191 |
| 192 IgnoreUpstreamNotificationMsg(); | |
| 193 | |
| 142 // Once we have successfully registered, we should send a heartbeat. | 194 // Once we have successfully registered, we should send a heartbeat. |
| 143 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)); | 195 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())); |
| 144 gcm_driver_.CompleteRegistration( | 196 gcm_driver_.CompleteRegistration( |
| 145 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 197 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 146 task_runner_->RunPendingTasks(); | 198 task_runner_->RunPendingTasks(); |
| 147 } | 199 } |
| 148 | 200 |
| 149 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) { | 201 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) { |
| 202 IgnoreUpstreamNotificationMsg(); | |
| 203 | |
| 150 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 204 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 151 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | 205 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 152 gcm_driver_.CompleteRegistration( | 206 gcm_driver_.CompleteRegistration( |
| 153 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 207 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 154 | 208 |
| 155 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | 209 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
| 156 // Should have a heartbeat task posted with zero delay on startup. | 210 // Should have a heartbeat task posted with zero delay on startup. |
| 157 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay()); | 211 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay()); |
| 158 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | 212 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 159 | 213 |
| 214 IgnoreUpstreamNotificationMsg(); | |
| 215 | |
| 160 const int new_delay = 1234*1000; // 1234 seconds. | 216 const int new_delay = 1234*1000; // 1234 seconds. |
| 161 settings_helper_.SetInteger(chromeos::kHeartbeatFrequency, new_delay); | 217 settings_helper_.SetInteger(chromeos::kHeartbeatFrequency, new_delay); |
| 162 // Now run pending heartbeat task, should send a heartbeat. | 218 // Now run pending heartbeat task, should send a heartbeat. |
| 163 gcm::OutgoingMessage message; | 219 gcm::OutgoingMessage message; |
| 164 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) | 220 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())) |
| 165 .WillOnce(SaveArg<2>(&message)); | 221 .WillOnce(SaveArg<2>(&message)); |
| 166 task_runner_->RunPendingTasks(); | 222 task_runner_->RunPendingTasks(); |
| 167 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | 223 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); |
| 168 | 224 |
| 169 // Complete sending a message - we should queue up the next heartbeat | 225 // Complete sending a message - we should queue up the next heartbeat |
| 170 // even if the previous attempt failed. | 226 // even if the previous attempt failed. |
| 171 gcm_driver_.CompleteSend( | 227 gcm_driver_.CompleteSend( |
| 172 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SERVER_ERROR); | 228 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SERVER_ERROR); |
| 173 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | 229 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
| 174 CheckPendingTaskDelay(scheduler_.last_heartbeat(), | 230 CheckPendingTaskDelay(scheduler_.last_heartbeat(), |
| 175 base::TimeDelta::FromMilliseconds(new_delay)); | 231 base::TimeDelta::FromMilliseconds(new_delay)); |
| 176 } | 232 } |
| 177 | 233 |
| 178 TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) { | 234 TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) { |
| 235 IgnoreUpstreamNotificationMsg(); | |
| 236 | |
| 179 // Makes sure that we can disable heartbeats on the fly. | 237 // Makes sure that we can disable heartbeats on the fly. |
| 180 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 238 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 181 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | 239 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 182 gcm::OutgoingMessage message; | 240 gcm::OutgoingMessage message; |
| 183 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) | 241 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())) |
| 184 .WillOnce(SaveArg<2>(&message)); | 242 .WillOnce(SaveArg<2>(&message)); |
| 185 gcm_driver_.CompleteRegistration( | 243 gcm_driver_.CompleteRegistration( |
| 186 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 244 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 187 // Should have a heartbeat task posted. | 245 // Should have a heartbeat task posted. |
| 188 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | 246 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
| 189 task_runner_->RunPendingTasks(); | 247 task_runner_->RunPendingTasks(); |
| 190 | 248 |
| 191 // Complete sending a message - we should queue up the next heartbeat. | 249 // Complete sending a message - we should queue up the next heartbeat. |
| 192 gcm_driver_.CompleteSend( | 250 gcm_driver_.CompleteSend( |
| 193 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SUCCESS); | 251 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SUCCESS); |
| 194 | 252 |
| 195 // Should have a new heartbeat task posted. | 253 // Should have a new heartbeat task posted. |
| 196 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size()); | 254 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size()); |
| 197 CheckPendingTaskDelay( | 255 CheckPendingTaskDelay( |
| 198 scheduler_.last_heartbeat(), | 256 scheduler_.last_heartbeat(), |
| 199 base::TimeDelta::FromMilliseconds( | 257 base::TimeDelta::FromMilliseconds( |
| 200 policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs)); | 258 policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs)); |
| 201 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | 259 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
| 202 | 260 |
| 261 IgnoreUpstreamNotificationMsg(); | |
|
Andrew T Wilson (Slow)
2015/11/18 11:43:32
Why is this required? How many notification messag
binjin
2015/11/19 05:50:27
I didn't consider much, I just add one call after
| |
| 262 | |
| 203 // Now disable heartbeats. Should get no more heartbeats sent. | 263 // Now disable heartbeats. Should get no more heartbeats sent. |
| 204 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false); | 264 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false); |
| 205 task_runner_->RunPendingTasks(); | 265 task_runner_->RunPendingTasks(); |
| 206 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | 266 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); |
| 207 } | 267 } |
| 208 | 268 |
| 209 TEST_F(HeartbeatSchedulerTest, CheckMessageContents) { | 269 TEST_F(HeartbeatSchedulerTest, CheckMessageContents) { |
| 270 IgnoreUpstreamNotificationMsg(); | |
| 271 | |
| 210 gcm::OutgoingMessage message; | 272 gcm::OutgoingMessage message; |
| 211 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 273 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 212 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) | 274 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())) |
| 213 .WillOnce(SaveArg<2>(&message)); | 275 .WillOnce(SaveArg<2>(&message)); |
| 214 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | 276 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 215 gcm_driver_.CompleteRegistration( | 277 gcm_driver_.CompleteRegistration( |
| 216 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 278 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 217 task_runner_->RunPendingTasks(); | 279 task_runner_->RunPendingTasks(); |
| 218 | 280 |
| 219 // Heartbeats should have a time-to-live equivalent to the heartbeat frequency | 281 // Heartbeats should have a time-to-live equivalent to the heartbeat frequency |
| 220 // so we don't have more than one heartbeat queued at a time. | 282 // so we don't have more than one heartbeat queued at a time. |
| 221 EXPECT_EQ(policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs/1000, | 283 EXPECT_EQ(policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs/1000, |
| 222 message.time_to_live); | 284 message.time_to_live); |
| 223 | 285 |
| 224 // Check the values in the message payload. | 286 // Check the values in the message payload. |
| 225 EXPECT_EQ("hb", message.data["type"]); | 287 EXPECT_EQ("hb", message.data["type"]); |
| 226 int64 timestamp; | 288 int64 timestamp; |
| 227 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); | 289 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); |
| 228 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); | 290 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); |
| 229 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); | 291 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); |
| 230 } | 292 } |
| 231 | 293 |
| 232 TEST_F(HeartbeatSchedulerTest, SendGcmIdUpdate) { | 294 TEST_F(HeartbeatSchedulerTest, SendGcmIdUpdate) { |
| 233 // Verifies that GCM id update request was sent after GCM registration. | 295 // Verifies that GCM id update request was sent after GCM registration. |
| 234 cloud_policy_client_.SetDMToken(kDMToken); | 296 cloud_policy_client_.SetDMToken(kDMToken); |
| 235 policy::CloudPolicyClient::StatusCallback callback; | 297 policy::CloudPolicyClient::StatusCallback callback; |
| 236 EXPECT_CALL(cloud_policy_client_, UpdateGcmId(kRegistrationId, _)) | 298 EXPECT_CALL(cloud_policy_client_, UpdateGcmId(kRegistrationId, _)) |
| 237 .WillOnce(SaveArg<1>(&callback)); | 299 .WillOnce(SaveArg<1>(&callback)); |
| 238 | 300 |
| 239 // Enable heartbeats. | 301 // Enable heartbeats. |
| 240 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 302 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 241 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)); | 303 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) |
| 304 .Times(AtLeast(1)); | |
| 242 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | 305 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 243 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 306 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 244 task_runner_->RunPendingTasks(); | 307 task_runner_->RunPendingTasks(); |
| 245 | 308 |
| 246 // Verifies that CloudPolicyClient got the update request, with a valid | 309 // Verifies that CloudPolicyClient got the update request, with a valid |
| 247 // callback. | 310 // callback. |
| 248 testing::Mock::VerifyAndClearExpectations(&cloud_policy_client_); | 311 testing::Mock::VerifyAndClearExpectations(&cloud_policy_client_); |
| 249 EXPECT_FALSE(callback.is_null()); | 312 EXPECT_FALSE(callback.is_null()); |
| 250 callback.Run(true); | 313 callback.Run(true); |
| 251 } | 314 } |
| 252 | 315 |
| 316 TEST_F(HeartbeatSchedulerTest, GcmUpstreamNotificationSignup) { | |
| 317 // Verifies that upstream notification works as expected. | |
| 318 cloud_policy_client_.SetDMToken(kDMToken); | |
| 319 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)) | |
| 320 .Times(AnyNumber()); | |
| 321 EXPECT_CALL(cloud_policy_client_, UpdateGcmId(kRegistrationId, _)); | |
| 322 | |
| 323 // GCM connected event before the registration should be ignored. | |
| 324 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | |
| 325 gcm_driver_.NotifyConnected(); | |
| 326 task_runner_->RunPendingTasks(); | |
| 327 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | |
| 328 | |
| 329 // Ignore unintested calls. | |
| 330 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)) | |
| 331 .Times(AnyNumber()); | |
| 332 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg())) | |
| 333 .Times(AnyNumber()); | |
| 334 | |
| 335 EXPECT_CALL(gcm_driver_, | |
| 336 SendImpl(kHeartbeatGCMAppID, _, IsUpstreamNotificationMsg())) | |
| 337 .Times(AtLeast(1)); | |
| 338 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | |
| 339 | |
| 340 gcm_driver_.NotifyConnected(); | |
| 341 } | |
| 342 | |
| 253 } // namespace | 343 } // namespace |
| OLD | NEW |