| 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 &cloud_policy_client_, |
| 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 14 matching lines...) Expand all Loading... |
| 86 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <= | 92 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <= |
| 87 // |now| - |last_heartbeat|. | 93 // |now| - |last_heartbeat|. |
| 88 base::TimeDelta delta = expected_delay - actual_delay; | 94 base::TimeDelta delta = expected_delay - actual_delay; |
| 89 EXPECT_LE(base::TimeDelta(), delta); | 95 EXPECT_LE(base::TimeDelta(), delta); |
| 90 EXPECT_GE(now - last_heartbeat, delta); | 96 EXPECT_GE(now - last_heartbeat, delta); |
| 91 } | 97 } |
| 92 | 98 |
| 93 base::MessageLoop loop_; | 99 base::MessageLoop loop_; |
| 94 MockGCMDriver gcm_driver_; | 100 MockGCMDriver gcm_driver_; |
| 95 chromeos::ScopedCrosSettingsTestHelper settings_helper_; | 101 chromeos::ScopedCrosSettingsTestHelper settings_helper_; |
| 102 testing::NiceMock<policy::MockCloudPolicyClient> cloud_policy_client_; |
| 96 | 103 |
| 97 // TaskRunner used to run individual tests. | 104 // TaskRunner used to run individual tests. |
| 98 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 105 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 99 | 106 |
| 100 // The HeartbeatScheduler instance under test. | 107 // The HeartbeatScheduler instance under test. |
| 101 policy::HeartbeatScheduler scheduler_; | 108 policy::HeartbeatScheduler scheduler_; |
| 102 }; | 109 }; |
| 103 | 110 |
| 104 TEST_F(HeartbeatSchedulerTest, Basic) { | 111 TEST_F(HeartbeatSchedulerTest, Basic) { |
| 105 // Just makes sure we can spin up and shutdown the scheduler with | 112 // Just makes sure we can spin up and shutdown the scheduler with |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 message.time_to_live); | 222 message.time_to_live); |
| 216 | 223 |
| 217 // Check the values in the message payload. | 224 // Check the values in the message payload. |
| 218 EXPECT_EQ("hb", message.data["type"]); | 225 EXPECT_EQ("hb", message.data["type"]); |
| 219 int64 timestamp; | 226 int64 timestamp; |
| 220 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); | 227 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); |
| 221 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); | 228 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); |
| 222 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); | 229 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); |
| 223 } | 230 } |
| 224 | 231 |
| 232 TEST_F(HeartbeatSchedulerTest, SendGcmIdUpdate) { |
| 233 // Verifies that GCM id update request was sent after GCM registration. |
| 234 cloud_policy_client_.SetDMToken(kDMToken); |
| 235 policy::CloudPolicyClient::StatusCallback callback; |
| 236 EXPECT_CALL(cloud_policy_client_, UpdateGcmId(kRegistrationId, _)) |
| 237 .WillOnce(SaveArg<1>(&callback)); |
| 238 |
| 239 // Enable heartbeats. |
| 240 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
| 241 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)); |
| 242 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); |
| 243 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
| 244 task_runner_->RunPendingTasks(); |
| 245 |
| 246 // Verifies that CloudPolicyClient got the update request, with a valid |
| 247 // callback. |
| 248 testing::Mock::VerifyAndClearExpectations(&cloud_policy_client_); |
| 249 EXPECT_FALSE(callback.is_null()); |
| 250 callback.Run(true); |
| 251 } |
| 252 |
| 225 } // namespace | 253 } // namespace |
| OLD | NEW |