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/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/test/test_simple_task_runner.h" | 8 #include "base/test/test_simple_task_runner.h" |
| 9 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" | 9 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" |
| 10 #include "chromeos/settings/cros_settings_names.h" | 10 #include "chromeos/settings/cros_settings_names.h" |
| 11 #include "components/gcm_driver/fake_gcm_driver.h" | 11 #include "components/gcm_driver/fake_gcm_driver.h" |
| 12 #include "components/policy/core/common/cloud/cloud_policy_client.h" | |
| 13 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | |
| 12 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 17 |
| 16 using ::testing::_; | 18 using ::testing::_; |
| 17 using ::testing::SaveArg; | 19 using ::testing::SaveArg; |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 const char* const kFakeEnrollmentDomain = "example.com"; | 22 const char* const kFakeEnrollmentDomain = "example.com"; |
| 21 const char* const kFakeDeviceId = "fake_device_id"; | 23 const char* const kFakeDeviceId = "fake_device_id"; |
| 22 const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring"; | 24 const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring"; |
| 25 const char* const kRegistrationId = "registration_id"; | |
| 26 const char* const kDMToken = "fake_dm_token"; | |
| 23 | 27 |
| 24 class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> { | 28 class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> { |
| 25 public: | 29 public: |
| 26 MockGCMDriver() { | 30 MockGCMDriver() { |
| 27 } | 31 } |
| 28 | 32 |
| 29 ~MockGCMDriver() override { | 33 ~MockGCMDriver() override { |
| 30 } | 34 } |
| 31 | 35 |
| 32 MOCK_METHOD2(RegisterImpl, | 36 MOCK_METHOD2(RegisterImpl, |
| 33 void(const std::string&, const std::vector<std::string>&)); | 37 void(const std::string&, const std::vector<std::string>&)); |
| 34 MOCK_METHOD3(SendImpl, | 38 MOCK_METHOD3(SendImpl, |
| 35 void(const std::string&, | 39 void(const std::string&, |
| 36 const std::string&, | 40 const std::string&, |
| 37 const gcm::OutgoingMessage& message)); | 41 const gcm::OutgoingMessage& message)); |
| 38 | 42 |
| 39 // Helper function to complete a registration previously started by | 43 // Helper function to complete a registration previously started by |
| 40 // Register(). | 44 // Register(). |
| 41 void CompleteRegistration(const std::string& app_id, | 45 void CompleteRegistration(const std::string& app_id, |
| 42 gcm::GCMClient::Result result) { | 46 gcm::GCMClient::Result result) { |
| 43 RegisterFinished(app_id, "registration_id", result); | 47 RegisterFinished(app_id, kRegistrationId, result); |
| 44 } | 48 } |
| 45 | 49 |
| 46 // Helper function to complete a send operation previously started by | 50 // Helper function to complete a send operation previously started by |
| 47 // Send(). | 51 // Send(). |
| 48 void CompleteSend(const std::string& app_id, | 52 void CompleteSend(const std::string& app_id, |
| 49 const std::string& message_id, | 53 const std::string& message_id, |
| 50 gcm::GCMClient::Result result) { | 54 gcm::GCMClient::Result result) { |
| 51 SendFinished(app_id, message_id, result); | 55 SendFinished(app_id, message_id, result); |
| 52 } | 56 } |
| 53 }; | 57 }; |
| 54 | 58 |
| 55 class HeartbeatSchedulerTest : public testing::Test { | 59 class HeartbeatSchedulerTest : public testing::Test { |
| 56 public: | 60 public: |
| 57 HeartbeatSchedulerTest() | 61 HeartbeatSchedulerTest() |
| 58 : task_runner_(new base::TestSimpleTaskRunner()), | 62 : task_runner_(new base::TestSimpleTaskRunner()), |
| 59 scheduler_( | 63 scheduler_(&gcm_driver_, |
| 60 &gcm_driver_, kFakeEnrollmentDomain, kFakeDeviceId, task_runner_) { | 64 nullptr, |
| 61 } | 65 kFakeEnrollmentDomain, |
| 66 kFakeDeviceId, | |
| 67 task_runner_) {} | |
| 62 | 68 |
| 63 void SetUp() override { | 69 void SetUp() override { |
| 64 settings_helper_.ReplaceProvider(chromeos::kHeartbeatEnabled); | 70 settings_helper_.ReplaceProvider(chromeos::kHeartbeatEnabled); |
| 65 } | 71 } |
| 66 | 72 |
| 67 void TearDown() override { | 73 void TearDown() override { |
| 68 content::RunAllBlockingPoolTasksUntilIdle(); | 74 content::RunAllBlockingPoolTasksUntilIdle(); |
| 69 } | 75 } |
| 70 | 76 |
| 71 void CheckPendingTaskDelay(base::Time last_heartbeat, | 77 void CheckPendingTaskDelay(base::Time last_heartbeat, |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 message.time_to_live); | 221 message.time_to_live); |
| 216 | 222 |
| 217 // Check the values in the message payload. | 223 // Check the values in the message payload. |
| 218 EXPECT_EQ("hb", message.data["type"]); | 224 EXPECT_EQ("hb", message.data["type"]); |
| 219 int64 timestamp; | 225 int64 timestamp; |
| 220 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); | 226 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); |
| 221 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); | 227 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); |
| 222 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); | 228 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); |
| 223 } | 229 } |
| 224 | 230 |
| 231 TEST_F(HeartbeatSchedulerTest, SendGcmIdMappingUpdate) { | |
| 232 // Verifies that GCM id mapping update was sent after GCM registration. | |
| 233 policy::MockCloudPolicyClient cloud_policy_client; | |
| 234 cloud_policy_client.SetDMToken(kDMToken); | |
| 235 policy::CloudPolicyClient::StatusCallback callback; | |
| 236 EXPECT_CALL(cloud_policy_client, UpdateGcmIdMapping(kRegistrationId, _)) | |
| 237 .WillOnce(SaveArg<1>(&callback)); | |
| 238 | |
| 239 scheduler_.SetCloudPolicyClientForTesting(&cloud_policy_client); | |
| 240 | |
| 241 EXPECT_FALSE(scheduler_.latest_gcm_id_mapping_succeeded()); | |
| 242 | |
| 243 // Enable heartbests. | |
| 244 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | |
| 245 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)); | |
| 246 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); | |
| 247 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | |
| 248 task_runner_->RunPendingTasks(); | |
| 249 | |
| 250 // Verifies that CloudPolicyClient get the update request, and call the | |
| 251 // callback with success flag. | |
| 252 testing::Mock::VerifyAndClearExpectations(&cloud_policy_client); | |
| 253 callback.Run(true); | |
| 254 | |
| 255 EXPECT_TRUE(scheduler_.latest_gcm_id_mapping_succeeded()); | |
|
Andrew T Wilson (Slow)
2015/08/03 13:52:26
Don't think these checks of latest_gcm_id_mapping_
binjin
2015/08/03 17:54:25
Done.
| |
| 256 } | |
| 257 | |
| 225 } // namespace | 258 } // namespace |
| OLD | NEW |