Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / status_uploader_unittest.cc
blob4ef34eb7d63efc594d9afbac761e9a933ca8b47d
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 "base/prefs/testing_pref_service.h"
6 #include "base/test/test_simple_task_runner.h"
7 #include "base/time/time.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/device_settings_service.h"
11 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
12 #include "chromeos/settings/cros_settings_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"
21 using ::testing::_;
22 using ::testing::Return;
23 using ::testing::SaveArg;
24 using ::testing::WithArgs;
26 namespace em = enterprise_management;
28 namespace {
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 policy::DeviceStatusCollector::CPUStatisticsFetcher()) {}
40 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest*));
41 MOCK_METHOD1(GetDeviceSessionStatus, bool(em::SessionStatusReportRequest*));
44 } // namespace
46 namespace policy {
47 class StatusUploaderTest : public testing::Test {
48 public:
49 StatusUploaderTest()
50 : task_runner_(new base::TestSimpleTaskRunner()),
51 device_settings_provider_(nullptr) {
52 DeviceStatusCollector::RegisterPrefs(prefs_.registry());
55 void SetUp() override {
56 client_.SetDMToken("dm_token");
57 collector_.reset(new MockDeviceStatusCollector(&prefs_));
59 // Swap out the DeviceSettingsProvider with our stub settings provider
60 // so we can set values for the upload frequency.
61 chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get();
62 device_settings_provider_ =
63 cros_settings->GetProvider(chromeos::kReportDeviceVersionInfo);
64 EXPECT_TRUE(device_settings_provider_);
65 EXPECT_TRUE(
66 cros_settings->RemoveSettingsProvider(device_settings_provider_));
67 cros_settings->AddSettingsProvider(&stub_settings_provider_);
71 void TearDown() override {
72 content::RunAllBlockingPoolTasksUntilIdle();
73 // Restore the real DeviceSettingsProvider.
74 chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get();
75 EXPECT_TRUE(cros_settings->RemoveSettingsProvider(
76 &stub_settings_provider_));
77 cros_settings->AddSettingsProvider(device_settings_provider_);
80 // Given a pending task to upload status, mocks out a server response.
81 void RunPendingUploadTaskAndCheckNext(const StatusUploader& uploader,
82 base::TimeDelta expected_delay) {
83 EXPECT_FALSE(task_runner_->GetPendingTasks().empty());
84 CloudPolicyClient::StatusCallback callback;
85 EXPECT_CALL(client_, UploadDeviceStatus(_, _, _))
86 .WillOnce(SaveArg<2>(&callback));
87 task_runner_->RunPendingTasks();
88 testing::Mock::VerifyAndClearExpectations(&device_management_service_);
90 // Make sure no status upload is queued up yet (since an upload is in
91 // progress).
92 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
94 // Now invoke the response.
95 callback.Run(true);
97 // Now that the previous request was satisfied, a task to do the next
98 // upload should be queued.
99 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
101 CheckPendingTaskDelay(uploader, expected_delay);
104 void CheckPendingTaskDelay(const StatusUploader& uploader,
105 base::TimeDelta expected_delay) {
106 // The next task should be scheduled sometime between |last_upload| +
107 // |expected_delay| and |now| + |expected_delay|.
108 base::Time now = base::Time::NowFromSystemTime();
109 base::Time next_task = now + task_runner_->NextPendingTaskDelay();
111 EXPECT_LE(next_task, now + expected_delay);
112 EXPECT_GE(next_task, uploader.last_upload() + expected_delay);
115 base::MessageLoop loop_;
116 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
117 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
118 chromeos::ScopedTestCrosSettings test_cros_settings_;
119 scoped_ptr<MockDeviceStatusCollector> collector_;
120 chromeos::CrosSettingsProvider* device_settings_provider_;
121 chromeos::StubCrosSettingsProvider stub_settings_provider_;
122 MockCloudPolicyClient client_;
123 MockDeviceManagementService device_management_service_;
124 TestingPrefServiceSimple prefs_;
127 TEST_F(StatusUploaderTest, BasicTest) {
128 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
129 StatusUploader uploader(&client_, collector_.Pass(), task_runner_);
130 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
131 // On startup, first update should happen immediately.
132 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay());
135 TEST_F(StatusUploaderTest, DifferentFrequencyAtStart) {
136 // Keep a pointer to the mock collector because collector_ gets cleared
137 // when it is passed to the StatusUploader constructor below.
138 MockDeviceStatusCollector* const mock_collector = collector_.get();
139 const int new_delay = StatusUploader::kDefaultUploadDelayMs * 2;
140 chromeos::CrosSettings::Get()->SetInteger(chromeos::kReportUploadFrequency,
141 new_delay);
142 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(
143 new_delay);
144 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
145 StatusUploader uploader(&client_, collector_.Pass(), task_runner_);
146 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size());
147 // On startup, first update should happen immediately.
148 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay());
150 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillRepeatedly(Return(true));
151 EXPECT_CALL(*mock_collector, GetDeviceSessionStatus(_)).WillRepeatedly(
152 Return(true));
153 // Second update should use the delay specified in settings.
154 RunPendingUploadTaskAndCheckNext(uploader, expected_delay);
157 TEST_F(StatusUploaderTest, ResetTimerAfterStatusCollection) {
158 // Keep a pointer to the mock collector because collector_ gets cleared
159 // when it is passed to the StatusUploader constructor below.
160 MockDeviceStatusCollector* const mock_collector = collector_.get();
161 StatusUploader uploader(&client_, collector_.Pass(), task_runner_);
162 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillRepeatedly(Return(true));
163 EXPECT_CALL(*mock_collector, GetDeviceSessionStatus(_)).WillRepeatedly(
164 Return(true));
165 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(
166 StatusUploader::kDefaultUploadDelayMs);
167 RunPendingUploadTaskAndCheckNext(uploader, expected_delay);
169 // Handle this response also, and ensure new task is queued.
170 RunPendingUploadTaskAndCheckNext(uploader, expected_delay);
172 // Now that the previous request was satisfied, a task to do the next
173 // upload should be queued again.
174 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
177 TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) {
178 // Keep a pointer to the mock collector because collector_ gets cleared
179 // when it is passed to the StatusUploader constructor below.
180 MockDeviceStatusCollector* mock_collector = collector_.get();
181 StatusUploader uploader(&client_, collector_.Pass(), task_runner_);
182 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillOnce(Return(false));
183 EXPECT_CALL(*mock_collector, GetDeviceSessionStatus(_)).WillOnce(
184 Return(false));
185 task_runner_->RunPendingTasks();
187 // Make sure the next status upload is queued up.
188 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
189 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(
190 StatusUploader::kDefaultUploadDelayMs);
191 CheckPendingTaskDelay(uploader, expected_delay);
194 TEST_F(StatusUploaderTest, ChangeFrequency) {
195 // Keep a pointer to the mock collector because collector_ gets cleared
196 // when it is passed to the StatusUploader constructor below.
197 MockDeviceStatusCollector* const mock_collector = collector_.get();
198 StatusUploader uploader(&client_, collector_.Pass(), task_runner_);
199 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillRepeatedly(Return(true));
200 EXPECT_CALL(*mock_collector, GetDeviceSessionStatus(_)).WillRepeatedly(
201 Return(true));
202 // Change the frequency. The new frequency should be reflected in the timing
203 // used for the next callback.
204 const int new_delay = StatusUploader::kDefaultUploadDelayMs * 2;
205 chromeos::CrosSettings::Get()->SetInteger(chromeos::kReportUploadFrequency,
206 new_delay);
207 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(
208 new_delay);
209 RunPendingUploadTaskAndCheckNext(uploader, expected_delay);
212 } // namespace policy