| 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" | |
| 14 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 15 |
| 18 using ::testing::_; | 16 using ::testing::_; |
| 19 using ::testing::SaveArg; | 17 using ::testing::SaveArg; |
| 20 | 18 |
| 21 namespace { | 19 namespace { |
| 22 const char* const kFakeEnrollmentDomain = "example.com"; | 20 const char* const kFakeEnrollmentDomain = "example.com"; |
| 23 const char* const kFakeDeviceId = "fake_device_id"; | 21 const char* const kFakeDeviceId = "fake_device_id"; |
| 24 const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring"; | 22 const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring"; |
| 25 const char* const kRegistrationId = "registration_id"; | |
| 26 const char* const kDMToken = "fake_dm_token"; | |
| 27 | 23 |
| 28 class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> { | 24 class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> { |
| 29 public: | 25 public: |
| 30 MockGCMDriver() { | 26 MockGCMDriver() { |
| 31 } | 27 } |
| 32 | 28 |
| 33 ~MockGCMDriver() override { | 29 ~MockGCMDriver() override { |
| 34 } | 30 } |
| 35 | 31 |
| 36 MOCK_METHOD2(RegisterImpl, | 32 MOCK_METHOD2(RegisterImpl, |
| 37 void(const std::string&, const std::vector<std::string>&)); | 33 void(const std::string&, const std::vector<std::string>&)); |
| 38 MOCK_METHOD3(SendImpl, | 34 MOCK_METHOD3(SendImpl, |
| 39 void(const std::string&, | 35 void(const std::string&, |
| 40 const std::string&, | 36 const std::string&, |
| 41 const gcm::OutgoingMessage& message)); | 37 const gcm::OutgoingMessage& message)); |
| 42 | 38 |
| 43 // Helper function to complete a registration previously started by | 39 // Helper function to complete a registration previously started by |
| 44 // Register(). | 40 // Register(). |
| 45 void CompleteRegistration(const std::string& app_id, | 41 void CompleteRegistration(const std::string& app_id, |
| 46 gcm::GCMClient::Result result) { | 42 gcm::GCMClient::Result result) { |
| 47 RegisterFinished(app_id, kRegistrationId, result); | 43 RegisterFinished(app_id, "registration_id", result); |
| 48 } | 44 } |
| 49 | 45 |
| 50 // Helper function to complete a send operation previously started by | 46 // Helper function to complete a send operation previously started by |
| 51 // Send(). | 47 // Send(). |
| 52 void CompleteSend(const std::string& app_id, | 48 void CompleteSend(const std::string& app_id, |
| 53 const std::string& message_id, | 49 const std::string& message_id, |
| 54 gcm::GCMClient::Result result) { | 50 gcm::GCMClient::Result result) { |
| 55 SendFinished(app_id, message_id, result); | 51 SendFinished(app_id, message_id, result); |
| 56 } | 52 } |
| 57 }; | 53 }; |
| 58 | 54 |
| 59 class HeartbeatSchedulerTest : public testing::Test { | 55 class HeartbeatSchedulerTest : public testing::Test { |
| 60 public: | 56 public: |
| 61 HeartbeatSchedulerTest() | 57 HeartbeatSchedulerTest() |
| 62 : task_runner_(new base::TestSimpleTaskRunner()), | 58 : task_runner_(new base::TestSimpleTaskRunner()), |
| 63 scheduler_(&gcm_driver_, | 59 scheduler_( |
| 64 &cloud_policy_client_, | 60 &gcm_driver_, kFakeEnrollmentDomain, kFakeDeviceId, task_runner_) { |
| 65 kFakeEnrollmentDomain, | 61 } |
| 66 kFakeDeviceId, | |
| 67 task_runner_) {} | |
| 68 | 62 |
| 69 void SetUp() override { | 63 void SetUp() override { |
| 70 settings_helper_.ReplaceProvider(chromeos::kHeartbeatEnabled); | 64 settings_helper_.ReplaceProvider(chromeos::kHeartbeatEnabled); |
| 71 } | 65 } |
| 72 | 66 |
| 73 void TearDown() override { | 67 void TearDown() override { |
| 74 content::RunAllBlockingPoolTasksUntilIdle(); | 68 content::RunAllBlockingPoolTasksUntilIdle(); |
| 75 } | 69 } |
| 76 | 70 |
| 77 void CheckPendingTaskDelay(base::Time last_heartbeat, | 71 void CheckPendingTaskDelay(base::Time last_heartbeat, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 92 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <= | 86 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <= |
| 93 // |now| - |last_heartbeat|. | 87 // |now| - |last_heartbeat|. |
| 94 base::TimeDelta delta = expected_delay - actual_delay; | 88 base::TimeDelta delta = expected_delay - actual_delay; |
| 95 EXPECT_LE(base::TimeDelta(), delta); | 89 EXPECT_LE(base::TimeDelta(), delta); |
| 96 EXPECT_GE(now - last_heartbeat, delta); | 90 EXPECT_GE(now - last_heartbeat, delta); |
| 97 } | 91 } |
| 98 | 92 |
| 99 base::MessageLoop loop_; | 93 base::MessageLoop loop_; |
| 100 MockGCMDriver gcm_driver_; | 94 MockGCMDriver gcm_driver_; |
| 101 chromeos::ScopedCrosSettingsTestHelper settings_helper_; | 95 chromeos::ScopedCrosSettingsTestHelper settings_helper_; |
| 102 testing::NiceMock<policy::MockCloudPolicyClient> cloud_policy_client_; | |
| 103 | 96 |
| 104 // TaskRunner used to run individual tests. | 97 // TaskRunner used to run individual tests. |
| 105 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 98 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 106 | 99 |
| 107 // The HeartbeatScheduler instance under test. | 100 // The HeartbeatScheduler instance under test. |
| 108 policy::HeartbeatScheduler scheduler_; | 101 policy::HeartbeatScheduler scheduler_; |
| 109 }; | 102 }; |
| 110 | 103 |
| 111 TEST_F(HeartbeatSchedulerTest, Basic) { | 104 TEST_F(HeartbeatSchedulerTest, Basic) { |
| 112 // Just makes sure we can spin up and shutdown the scheduler with | 105 // 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... |
| 222 message.time_to_live); | 215 message.time_to_live); |
| 223 | 216 |
| 224 // Check the values in the message payload. | 217 // Check the values in the message payload. |
| 225 EXPECT_EQ("hb", message.data["type"]); | 218 EXPECT_EQ("hb", message.data["type"]); |
| 226 int64 timestamp; | 219 int64 timestamp; |
| 227 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); | 220 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); |
| 228 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); | 221 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); |
| 229 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); | 222 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); |
| 230 } | 223 } |
| 231 | 224 |
| 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 | |
| 253 } // namespace | 225 } // namespace |
| OLD | NEW |