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