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

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

Issue 845313008: Refactored device status uploading out of policy fetch infrastructure. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review feedback. Created 5 years, 10 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
(Empty)
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.
4
5 #include "base/prefs/pref_registry_simple.h"
6 #include "base/prefs/testing_pref_service.h"
7 #include "base/test/test_simple_task_runner.h"
8 #include "chrome/browser/chromeos/policy/device_status_collector.h"
9 #include "chrome/browser/chromeos/policy/status_uploader.h"
10 #include "chrome/browser/chromeos/settings/cros_settings.h"
11 #include "chrome/browser/chromeos/settings/device_settings_service.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/policy/core/common/cloud/cloud_policy_client.h"
14 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
15 #include "components/policy/core/common/cloud/mock_device_management_service.h"
16 #include "content/public/test/test_utils.h"
17 #include "net/url_request/url_request_context_getter.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 using ::testing::_;
22 using ::testing::Return;
23 using ::testing::SaveArg;
24 using ::testing::WithArgs;
25
26 namespace em = enterprise_management;
27
28 namespace {
29
30 class MockDeviceStatusCollector : public policy::DeviceStatusCollector {
31 public:
32 explicit MockDeviceStatusCollector(PrefService* local_state)
33 : DeviceStatusCollector(
34 local_state,
35 nullptr,
36 policy::DeviceStatusCollector::LocationUpdateRequester(),
37 policy::DeviceStatusCollector::VolumeInfoFetcher()) {
38 }
39
40 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest*));
41 MOCK_METHOD1(GetDeviceSessionStatus, bool(em::SessionStatusReportRequest*));
42 };
43
44 } // namespace
45
46 namespace policy {
47 class StatusUploaderTest : public testing::Test {
48 public:
49 StatusUploaderTest()
50 : task_runner_(new base::TestSimpleTaskRunner()) {
51 DeviceStatusCollector::RegisterPrefs(prefs_.registry());
52 prefs_.registry()->RegisterIntegerPref(
53 prefs::kDeviceStatusUploadRate,
54 StatusUploader::kDefaultUploadDelayMs);
55 }
56
57 void SetUp() override {
58 client_.SetDMToken("dm_token");
59 collector_.reset(new MockDeviceStatusCollector(&prefs_));
60 }
61
62 void TearDown() override {
63 content::RunAllBlockingPoolTasksUntilIdle();
64 }
65
66 // Given a pending task to upload status, mocks out a server response.
67 void RunPendingUploadTaskAndCheckNext(const StatusUploader& uploader) {
68 EXPECT_FALSE(task_runner_->GetPendingTasks().empty());
69 CloudPolicyClient::StatusCallback callback;
70 EXPECT_CALL(client_, UploadDeviceStatus(_, _, _))
71 .WillOnce(SaveArg<2>(&callback));
72 task_runner_->RunPendingTasks();
73 testing::Mock::VerifyAndClearExpectations(&device_management_service_);
74 // Make sure no status upload is queued up yet (since an upload is in
75 // progress).
76 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
77
78 // Now invoke the response.
79 callback.Run(true);
80
81 // Now that the previous request was satisfied, a task to do the next
82 // upload should be queued.
83 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
84
85 // The next refresh would have been scheduled when we invoked the callback.
86 // So the next task should be scheduled sometime between |last_upload| +
87 // kDefaultUploadDelayMs and |now| + kDefaultUploadDelayMs.
88 base::Time now = base::Time::NowFromSystemTime();
89 base::Time next_task = now + task_runner_->NextPendingTaskDelay();
90
91 base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(
92 StatusUploader::kDefaultUploadDelayMs);
93 EXPECT_LE(next_task, now + expected_delay);
94 EXPECT_GE(next_task, uploader.last_upload() + expected_delay);
95 }
96
97 base::MessageLoop loop_;
98 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
99 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
100 chromeos::ScopedTestCrosSettings test_cros_settings_;
101 scoped_ptr<MockDeviceStatusCollector> collector_;
102 MockCloudPolicyClient client_;
103 MockDeviceManagementService device_management_service_;
104 TestingPrefServiceSimple prefs_;
105 };
106
107 TEST_F(StatusUploaderTest, BasicTest) {
108 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
109 StatusUploader uploader(
110 &prefs_, &client_, collector_.Pass(), task_runner_);
111 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
112 // On startup, first update should happen immediately.
113 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay());
114 }
115
116 TEST_F(StatusUploaderTest, ResetTimerAfterStatusCollection) {
117 // Keep a pointer to the mock collector because the scoped_ptr gets cleared
118 // when it is passed to CreateStatusUploader() below.
119 MockDeviceStatusCollector* mock_collector = collector_.get();
120 StatusUploader uploader(
121 &prefs_, &client_, collector_.Pass(), task_runner_);
122 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillRepeatedly(Return(true));
123 EXPECT_CALL(*mock_collector, GetDeviceSessionStatus(_)).WillRepeatedly(
124 Return(true));
125 RunPendingUploadTaskAndCheckNext(uploader);
126
127 // Handle this response also, and ensure new task is queued.
128 RunPendingUploadTaskAndCheckNext(uploader);
129
130 // Now that the previous request was satisfied, a task to do the next
131 // upload should be queued again.
132 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
133 }
134
135 TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) {
136 // Keep a pointer to the mock collector because the scoped_ptr gets cleared
137 // when it is passed to CreateStatusUploader() below.
138 MockDeviceStatusCollector* mock_collector = collector_.get();
139 StatusUploader uploader(
140 &prefs_, &client_, collector_.Pass(), task_runner_);
141 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillOnce(Return(false));
142 EXPECT_CALL(*mock_collector, GetDeviceSessionStatus(_)).WillOnce(
143 Return(false));
144 task_runner_->RunPendingTasks();
145
146 // Make sure the next status upload is queued up.
147 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
148 }
149
150 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/policy/status_uploader.cc ('k') | chrome/browser/policy/test/policy_testserver.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698