Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: chrome/browser/chromeos/policy/heartbeat_scheduler_unittest.cc

Issue 1258313002: Send GCM id to DMServer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes addressing #9 and #10 Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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() : task_runner_(new base::TestSimpleTaskRunner()) {}
58 : task_runner_(new base::TestSimpleTaskRunner()),
59 scheduler_(
60 &gcm_driver_, kFakeEnrollmentDomain, kFakeDeviceId, task_runner_) {
61 }
62 62
63 void SetUp() override { 63 void SetUp() override {
64 InitializeScheduler(nullptr);
Andrew T Wilson (Slow) 2015/08/06 11:17:06 why not set a mock cloud policy client here, then
binjin 2015/08/06 13:33:01 Done.
64 settings_helper_.ReplaceProvider(chromeos::kHeartbeatEnabled); 65 settings_helper_.ReplaceProvider(chromeos::kHeartbeatEnabled);
65 } 66 }
66 67
67 void TearDown() override { 68 void TearDown() override {
68 content::RunAllBlockingPoolTasksUntilIdle(); 69 content::RunAllBlockingPoolTasksUntilIdle();
69 } 70 }
70 71
72 void InitializeScheduler(policy::CloudPolicyClient* cloud_policy_client) {
73 scheduler_.reset(new policy::HeartbeatScheduler(
74 &gcm_driver_, cloud_policy_client, kFakeEnrollmentDomain, kFakeDeviceId,
75 task_runner_));
76 }
77
71 void CheckPendingTaskDelay(base::Time last_heartbeat, 78 void CheckPendingTaskDelay(base::Time last_heartbeat,
72 base::TimeDelta expected_delay) { 79 base::TimeDelta expected_delay) {
73 EXPECT_FALSE(last_heartbeat.is_null()); 80 EXPECT_FALSE(last_heartbeat.is_null());
74 base::Time now = base::Time::NowFromSystemTime(); 81 base::Time now = base::Time::NowFromSystemTime();
75 EXPECT_GE(now, last_heartbeat); 82 EXPECT_GE(now, last_heartbeat);
76 base::TimeDelta actual_delay = task_runner_->NextPendingTaskDelay(); 83 base::TimeDelta actual_delay = task_runner_->NextPendingTaskDelay();
77 84
78 // NextPendingTaskDelay() returns the exact original delay value the task 85 // NextPendingTaskDelay() returns the exact original delay value the task
79 // was posted with. The heartbeat task would have been calculated to fire at 86 // 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 87 // |last_heartbeat| + |expected_delay|, but we don't know the exact time
(...skipping 10 matching lines...) Expand all
91 } 98 }
92 99
93 base::MessageLoop loop_; 100 base::MessageLoop loop_;
94 MockGCMDriver gcm_driver_; 101 MockGCMDriver gcm_driver_;
95 chromeos::ScopedCrosSettingsTestHelper settings_helper_; 102 chromeos::ScopedCrosSettingsTestHelper settings_helper_;
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 scoped_ptr<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
106 // heartbeats disabled. 112 // heartbeats disabled.
107 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false); 113 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false);
108 ASSERT_TRUE(task_runner_->GetPendingTasks().empty()); 114 ASSERT_TRUE(task_runner_->GetPendingTasks().empty());
109 } 115 }
110 116
111 TEST_F(HeartbeatSchedulerTest, PermanentlyFailedGCMRegistration) { 117 TEST_F(HeartbeatSchedulerTest, PermanentlyFailedGCMRegistration) {
112 // If heartbeats are enabled, we should register with GCMDriver. 118 // If heartbeats are enabled, we should register with GCMDriver.
113 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); 119 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
114 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); 120 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
115 gcm_driver_.CompleteRegistration( 121 gcm_driver_.CompleteRegistration(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) 163 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
158 .WillOnce(SaveArg<2>(&message)); 164 .WillOnce(SaveArg<2>(&message));
159 task_runner_->RunPendingTasks(); 165 task_runner_->RunPendingTasks();
160 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 166 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
161 167
162 // Complete sending a message - we should queue up the next heartbeat 168 // Complete sending a message - we should queue up the next heartbeat
163 // even if the previous attempt failed. 169 // even if the previous attempt failed.
164 gcm_driver_.CompleteSend( 170 gcm_driver_.CompleteSend(
165 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SERVER_ERROR); 171 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SERVER_ERROR);
166 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); 172 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
167 CheckPendingTaskDelay(scheduler_.last_heartbeat(), 173 CheckPendingTaskDelay(scheduler_->last_heartbeat(),
168 base::TimeDelta::FromMilliseconds(new_delay)); 174 base::TimeDelta::FromMilliseconds(new_delay));
169 } 175 }
170 176
171 TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) { 177 TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) {
172 // Makes sure that we can disable heartbeats on the fly. 178 // Makes sure that we can disable heartbeats on the fly.
173 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); 179 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
174 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true); 180 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
175 gcm::OutgoingMessage message; 181 gcm::OutgoingMessage message;
176 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) 182 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
177 .WillOnce(SaveArg<2>(&message)); 183 .WillOnce(SaveArg<2>(&message));
178 gcm_driver_.CompleteRegistration( 184 gcm_driver_.CompleteRegistration(
179 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); 185 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
180 // Should have a heartbeat task posted. 186 // Should have a heartbeat task posted.
181 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); 187 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
182 task_runner_->RunPendingTasks(); 188 task_runner_->RunPendingTasks();
183 189
184 // Complete sending a message - we should queue up the next heartbeat. 190 // Complete sending a message - we should queue up the next heartbeat.
185 gcm_driver_.CompleteSend( 191 gcm_driver_.CompleteSend(
186 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SUCCESS); 192 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SUCCESS);
187 193
188 // Should have a new heartbeat task posted. 194 // Should have a new heartbeat task posted.
189 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size()); 195 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size());
190 CheckPendingTaskDelay( 196 CheckPendingTaskDelay(
191 scheduler_.last_heartbeat(), 197 scheduler_->last_heartbeat(),
192 base::TimeDelta::FromMilliseconds( 198 base::TimeDelta::FromMilliseconds(
193 policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs)); 199 policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs));
194 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); 200 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
195 201
196 // Now disable heartbeats. Should get no more heartbeats sent. 202 // Now disable heartbeats. Should get no more heartbeats sent.
197 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false); 203 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false);
198 task_runner_->RunPendingTasks(); 204 task_runner_->RunPendingTasks();
199 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 205 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
200 } 206 }
201 207
(...skipping 13 matching lines...) Expand all
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"], &timestamp)); 226 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], &timestamp));
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 InitializeScheduler(&cloud_policy_client);
240
241 // Enable heartbests.
242 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
243 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _));
244 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
245 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
246 task_runner_->RunPendingTasks();
247
248 // Verifies that CloudPolicyClient got the update request, with a valid
249 // callback.
250 testing::Mock::VerifyAndClearExpectations(&cloud_policy_client);
251 EXPECT_FALSE(callback.is_null());
252 callback.Run(true);
253 }
254
225 } // namespace 255 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/policy/heartbeat_scheduler.cc ('k') | components/policy/core/common/cloud/cloud_policy_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698