Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / heartbeat_scheduler_unittest.cc
blob6b409f3537de804df957bdae2f23d46fe9462efa
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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/heartbeat_scheduler.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
10 #include "chromeos/settings/cros_settings_names.h"
11 #include "components/gcm_driver/fake_gcm_driver.h"
12 #include "content/public/test/test_utils.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using ::testing::_;
17 using ::testing::SaveArg;
19 namespace {
20 const char* const kFakeEnrollmentDomain = "example.com";
21 const char* const kFakeDeviceId = "fake_device_id";
22 const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring";
24 class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> {
25 public:
26 MockGCMDriver() {
29 ~MockGCMDriver() override {
32 MOCK_METHOD2(RegisterImpl,
33 void(const std::string&, const std::vector<std::string>&));
34 MOCK_METHOD3(SendImpl,
35 void(const std::string&,
36 const std::string&,
37 const gcm::OutgoingMessage& message));
39 // Helper function to complete a registration previously started by
40 // Register().
41 void CompleteRegistration(const std::string& app_id,
42 gcm::GCMClient::Result result) {
43 RegisterFinished(app_id, "registration_id", result);
46 // Helper function to complete a send operation previously started by
47 // Send().
48 void CompleteSend(const std::string& app_id,
49 const std::string& message_id,
50 gcm::GCMClient::Result result) {
51 SendFinished(app_id, message_id, result);
55 class HeartbeatSchedulerTest : public testing::Test {
56 public:
57 HeartbeatSchedulerTest()
58 : task_runner_(new base::TestSimpleTaskRunner()),
59 scheduler_(
60 &gcm_driver_, kFakeEnrollmentDomain, kFakeDeviceId, task_runner_) {
63 void SetUp() override {
64 settings_helper_.ReplaceProvider(chromeos::kHeartbeatEnabled);
67 void TearDown() override {
68 content::RunAllBlockingPoolTasksUntilIdle();
71 void CheckPendingTaskDelay(base::Time last_heartbeat,
72 base::TimeDelta expected_delay) {
73 EXPECT_FALSE(last_heartbeat.is_null());
74 base::Time now = base::Time::NowFromSystemTime();
75 EXPECT_GE(now, last_heartbeat);
76 base::TimeDelta actual_delay = task_runner_->NextPendingTaskDelay();
78 // NextPendingTaskDelay() returns the exact original delay value the task
79 // was posted with. The heartbeat task would have been calculated to fire at
80 // |last_heartbeat| + |expected_delay|, but we don't know the exact time
81 // when the task was posted (if it was a couple of milliseconds after
82 // |last_heartbeat|, then |actual_delay| would be a couple of milliseconds
83 // smaller than |expected_delay|.
85 // We do know that the task was posted sometime between |last_heartbeat|
86 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <=
87 // |now| - |last_heartbeat|.
88 base::TimeDelta delta = expected_delay - actual_delay;
89 EXPECT_LE(base::TimeDelta(), delta);
90 EXPECT_GE(now - last_heartbeat, delta);
93 base::MessageLoop loop_;
94 MockGCMDriver gcm_driver_;
95 chromeos::ScopedCrosSettingsTestHelper settings_helper_;
97 // TaskRunner used to run individual tests.
98 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
100 // The HeartbeatScheduler instance under test.
101 policy::HeartbeatScheduler scheduler_;
104 TEST_F(HeartbeatSchedulerTest, Basic) {
105 // Just makes sure we can spin up and shutdown the scheduler with
106 // heartbeats disabled.
107 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false);
108 ASSERT_TRUE(task_runner_->GetPendingTasks().empty());
111 TEST_F(HeartbeatSchedulerTest, PermanentlyFailedGCMRegistration) {
112 // If heartbeats are enabled, we should register with GCMDriver.
113 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
114 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
115 gcm_driver_.CompleteRegistration(
116 kHeartbeatGCMAppID, gcm::GCMClient::GCM_DISABLED);
118 // There should be no heartbeat tasks pending, because registration failed.
119 ASSERT_TRUE(task_runner_->GetPendingTasks().empty());
122 TEST_F(HeartbeatSchedulerTest, TemporarilyFailedGCMRegistration) {
123 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
124 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
125 gcm_driver_.CompleteRegistration(
126 kHeartbeatGCMAppID, gcm::GCMClient::SERVER_ERROR);
127 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
129 // Should have a pending task to try registering again.
130 ASSERT_FALSE(task_runner_->GetPendingTasks().empty());
131 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
132 task_runner_->RunPendingTasks();
133 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
135 // Once we have successfully registered, we should send a heartbeat.
136 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _));
137 gcm_driver_.CompleteRegistration(
138 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
139 task_runner_->RunPendingTasks();
142 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) {
143 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
144 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
145 gcm_driver_.CompleteRegistration(
146 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
148 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
149 // Should have a heartbeat task posted with zero delay on startup.
150 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay());
151 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
153 const int new_delay = 1234*1000; // 1234 seconds.
154 settings_helper_.SetInteger(chromeos::kHeartbeatFrequency, new_delay);
155 // Now run pending heartbeat task, should send a heartbeat.
156 gcm::OutgoingMessage message;
157 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
158 .WillOnce(SaveArg<2>(&message));
159 task_runner_->RunPendingTasks();
160 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
162 // Complete sending a message - we should queue up the next heartbeat
163 // even if the previous attempt failed.
164 gcm_driver_.CompleteSend(
165 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SERVER_ERROR);
166 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
167 CheckPendingTaskDelay(scheduler_.last_heartbeat(),
168 base::TimeDelta::FromMilliseconds(new_delay));
171 TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) {
172 // Makes sure that we can disable heartbeats on the fly.
173 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
174 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
175 gcm::OutgoingMessage message;
176 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
177 .WillOnce(SaveArg<2>(&message));
178 gcm_driver_.CompleteRegistration(
179 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
180 // Should have a heartbeat task posted.
181 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
182 task_runner_->RunPendingTasks();
184 // Complete sending a message - we should queue up the next heartbeat.
185 gcm_driver_.CompleteSend(
186 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SUCCESS);
188 // Should have a new heartbeat task posted.
189 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size());
190 CheckPendingTaskDelay(
191 scheduler_.last_heartbeat(),
192 base::TimeDelta::FromMilliseconds(
193 policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs));
194 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
196 // Now disable heartbeats. Should get no more heartbeats sent.
197 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false);
198 task_runner_->RunPendingTasks();
199 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
202 TEST_F(HeartbeatSchedulerTest, CheckMessageContents) {
203 gcm::OutgoingMessage message;
204 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
205 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
206 .WillOnce(SaveArg<2>(&message));
207 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
208 gcm_driver_.CompleteRegistration(
209 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
210 task_runner_->RunPendingTasks();
212 // Heartbeats should have a time-to-live equivalent to the heartbeat frequency
213 // so we don't have more than one heartbeat queued at a time.
214 EXPECT_EQ(policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs/1000,
215 message.time_to_live);
217 // Check the values in the message payload.
218 EXPECT_EQ("hb", message.data["type"]);
219 int64 timestamp;
220 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], &timestamp));
221 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]);
222 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]);
225 } // namespace