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_local_account.h"
9 #include "chrome/browser/chromeos/policy/device_status_collector.h"
10 #include "chrome/browser/chromeos/policy/status_uploader.h"
11 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.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_browser_thread_bundle.h"
17 #include "content/public/test/test_utils.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/base/user_activity/user_activity_detector.h"
22 #include "ui/events/platform/platform_event_source.h"
23 #include "ui/events/platform/platform_event_types.h"
26 #include "ui/events/devices/x11/device_data_manager_x11.h"
27 #include "ui/events/devices/x11/touch_factory_x11.h"
28 #include "ui/events/test/events_test_utils_x11.h"
31 #if defined(USE_OZONE)
32 #include "ui/events/event.h"
33 #include "ui/events/event_utils.h"
37 using ::testing::Invoke
;
38 using ::testing::Return
;
39 using ::testing::SaveArg
;
40 using ::testing::WithArgs
;
42 namespace em
= enterprise_management
;
46 class MockDeviceStatusCollector
: public policy::DeviceStatusCollector
{
48 explicit MockDeviceStatusCollector(PrefService
* local_state
)
49 : DeviceStatusCollector(
52 policy::DeviceStatusCollector::LocationUpdateRequester(),
53 policy::DeviceStatusCollector::VolumeInfoFetcher(),
54 policy::DeviceStatusCollector::CPUStatisticsFetcher(),
55 policy::DeviceStatusCollector::CPUTempFetcher()) {}
57 MOCK_METHOD1(GetDeviceStatus
, bool(em::DeviceStatusReportRequest
*));
58 MOCK_METHOD1(GetDeviceSessionStatus
, bool(em::SessionStatusReportRequest
*));
60 // Explicit mock implementation declared here, since gmock::Invoke can't
61 // handle returning non-moveable types like scoped_ptr.
62 scoped_ptr
<policy::DeviceLocalAccount
> GetAutoLaunchedKioskSessionInfo()
64 return make_scoped_ptr(new policy::DeviceLocalAccount(
65 policy::DeviceLocalAccount::TYPE_KIOSK_APP
, "account_id", "app_id",
73 class StatusUploaderTest
: public testing::Test
{
75 StatusUploaderTest() : task_runner_(new base::TestSimpleTaskRunner()) {
76 DeviceStatusCollector::RegisterPrefs(prefs_
.registry());
79 void SetUp() override
{
81 ui::DeviceDataManagerX11::CreateInstance();
83 client_
.SetDMToken("dm_token");
84 collector_
.reset(new MockDeviceStatusCollector(&prefs_
));
85 settings_helper_
.ReplaceProvider(chromeos::kReportUploadFrequency
);
88 void TearDown() override
{
89 content::RunAllBlockingPoolTasksUntilIdle();
92 // Given a pending task to upload status, mocks out a server response.
93 void RunPendingUploadTaskAndCheckNext(const StatusUploader
& uploader
,
94 base::TimeDelta expected_delay
) {
95 EXPECT_FALSE(task_runner_
->GetPendingTasks().empty());
96 CloudPolicyClient::StatusCallback callback
;
97 EXPECT_CALL(client_
, UploadDeviceStatus(_
, _
, _
))
98 .WillOnce(SaveArg
<2>(&callback
));
99 task_runner_
->RunPendingTasks();
100 testing::Mock::VerifyAndClearExpectations(&device_management_service_
);
102 // Make sure no status upload is queued up yet (since an upload is in
104 EXPECT_TRUE(task_runner_
->GetPendingTasks().empty());
106 // Now invoke the response.
109 // Now that the previous request was satisfied, a task to do the next
110 // upload should be queued.
111 EXPECT_EQ(1U, task_runner_
->GetPendingTasks().size());
113 CheckPendingTaskDelay(uploader
, expected_delay
);
116 void CheckPendingTaskDelay(const StatusUploader
& uploader
,
117 base::TimeDelta expected_delay
) {
118 // The next task should be scheduled sometime between |last_upload| +
119 // |expected_delay| and |now| + |expected_delay|.
120 base::Time now
= base::Time::NowFromSystemTime();
121 base::Time next_task
= now
+ task_runner_
->NextPendingTaskDelay();
123 EXPECT_LE(next_task
, now
+ expected_delay
);
124 EXPECT_GE(next_task
, uploader
.last_upload() + expected_delay
);
127 content::TestBrowserThreadBundle thread_bundle_
;
128 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
129 chromeos::ScopedCrosSettingsTestHelper settings_helper_
;
130 scoped_ptr
<MockDeviceStatusCollector
> collector_
;
131 ui::UserActivityDetector detector_
;
132 MockCloudPolicyClient client_
;
133 MockDeviceManagementService device_management_service_
;
134 TestingPrefServiceSimple prefs_
;
137 TEST_F(StatusUploaderTest
, BasicTest
) {
138 EXPECT_TRUE(task_runner_
->GetPendingTasks().empty());
139 StatusUploader
uploader(&client_
, collector_
.Pass(), task_runner_
);
140 EXPECT_EQ(1U, task_runner_
->GetPendingTasks().size());
141 // On startup, first update should happen immediately.
142 EXPECT_EQ(base::TimeDelta(), task_runner_
->NextPendingTaskDelay());
145 TEST_F(StatusUploaderTest
, DifferentFrequencyAtStart
) {
146 // Keep a pointer to the mock collector because collector_ gets cleared
147 // when it is passed to the StatusUploader constructor below.
148 MockDeviceStatusCollector
* const mock_collector
= collector_
.get();
149 const int new_delay
= StatusUploader::kDefaultUploadDelayMs
* 2;
150 settings_helper_
.SetInteger(chromeos::kReportUploadFrequency
, new_delay
);
151 const base::TimeDelta expected_delay
= base::TimeDelta::FromMilliseconds(
153 EXPECT_TRUE(task_runner_
->GetPendingTasks().empty());
154 StatusUploader
uploader(&client_
, collector_
.Pass(), task_runner_
);
155 ASSERT_EQ(1U, task_runner_
->GetPendingTasks().size());
156 // On startup, first update should happen immediately.
157 EXPECT_EQ(base::TimeDelta(), task_runner_
->NextPendingTaskDelay());
159 EXPECT_CALL(*mock_collector
, GetDeviceStatus(_
)).WillRepeatedly(Return(true));
160 EXPECT_CALL(*mock_collector
, GetDeviceSessionStatus(_
)).WillRepeatedly(
162 // Second update should use the delay specified in settings.
163 RunPendingUploadTaskAndCheckNext(uploader
, expected_delay
);
166 TEST_F(StatusUploaderTest
, ResetTimerAfterStatusCollection
) {
167 // Keep a pointer to the mock collector because collector_ gets cleared
168 // when it is passed to the StatusUploader constructor below.
169 MockDeviceStatusCollector
* const mock_collector
= collector_
.get();
170 StatusUploader
uploader(&client_
, collector_
.Pass(), task_runner_
);
171 EXPECT_CALL(*mock_collector
, GetDeviceStatus(_
)).WillRepeatedly(Return(true));
172 EXPECT_CALL(*mock_collector
, GetDeviceSessionStatus(_
)).WillRepeatedly(
174 const base::TimeDelta expected_delay
= base::TimeDelta::FromMilliseconds(
175 StatusUploader::kDefaultUploadDelayMs
);
176 RunPendingUploadTaskAndCheckNext(uploader
, expected_delay
);
178 // Handle this response also, and ensure new task is queued.
179 RunPendingUploadTaskAndCheckNext(uploader
, expected_delay
);
181 // Now that the previous request was satisfied, a task to do the next
182 // upload should be queued again.
183 EXPECT_EQ(1U, task_runner_
->GetPendingTasks().size());
186 TEST_F(StatusUploaderTest
, ResetTimerAfterFailedStatusCollection
) {
187 // Keep a pointer to the mock collector because collector_ gets cleared
188 // when it is passed to the StatusUploader constructor below.
189 MockDeviceStatusCollector
* mock_collector
= collector_
.get();
190 StatusUploader
uploader(&client_
, collector_
.Pass(), task_runner_
);
191 EXPECT_CALL(*mock_collector
, GetDeviceStatus(_
)).WillOnce(Return(false));
192 EXPECT_CALL(*mock_collector
, GetDeviceSessionStatus(_
)).WillOnce(
194 task_runner_
->RunPendingTasks();
196 // Make sure the next status upload is queued up.
197 EXPECT_EQ(1U, task_runner_
->GetPendingTasks().size());
198 const base::TimeDelta expected_delay
= base::TimeDelta::FromMilliseconds(
199 StatusUploader::kDefaultUploadDelayMs
);
200 CheckPendingTaskDelay(uploader
, expected_delay
);
203 TEST_F(StatusUploaderTest
, ChangeFrequency
) {
204 // Keep a pointer to the mock collector because collector_ gets cleared
205 // when it is passed to the StatusUploader constructor below.
206 MockDeviceStatusCollector
* const mock_collector
= collector_
.get();
207 StatusUploader
uploader(&client_
, collector_
.Pass(), task_runner_
);
208 EXPECT_CALL(*mock_collector
, GetDeviceStatus(_
)).WillRepeatedly(Return(true));
209 EXPECT_CALL(*mock_collector
, GetDeviceSessionStatus(_
)).WillRepeatedly(
211 // Change the frequency. The new frequency should be reflected in the timing
212 // used for the next callback.
213 const int new_delay
= StatusUploader::kDefaultUploadDelayMs
* 2;
214 settings_helper_
.SetInteger(chromeos::kReportUploadFrequency
, new_delay
);
215 const base::TimeDelta expected_delay
= base::TimeDelta::FromMilliseconds(
217 RunPendingUploadTaskAndCheckNext(uploader
, expected_delay
);
220 #if defined(USE_X11) || defined(USE_OZONE)
221 TEST_F(StatusUploaderTest
, NoUploadAfterUserInput
) {
222 StatusUploader
uploader(&client_
, collector_
.Pass(), task_runner_
);
223 // Should allow data upload before there is user input.
224 EXPECT_TRUE(uploader
.IsSessionDataUploadAllowed());
226 // Now mock user input, and no session data should be allowed.
228 ui::ScopedXI2Event native_event
;
229 const int kPointerDeviceId
= 10;
230 std::vector
<int> device_list
;
231 device_list
.push_back(kPointerDeviceId
);
232 ui::TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list
);
233 native_event
.InitGenericButtonEvent(
234 kPointerDeviceId
, ui::ET_MOUSE_PRESSED
, gfx::Point(),
235 ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_CONTROL_DOWN
);
236 #elif defined(USE_OZONE)
237 ui::MouseEvent
e(ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(),
238 ui::EventTimeForNow(), 0, 0);
239 const ui::PlatformEvent
& native_event
= &e
;
241 ui::UserActivityDetector::Get()->DidProcessEvent(native_event
);
242 EXPECT_FALSE(uploader
.IsSessionDataUploadAllowed());
246 TEST_F(StatusUploaderTest
, NoUploadAfterVideoCapture
) {
247 StatusUploader
uploader(&client_
, collector_
.Pass(), task_runner_
);
248 // Should allow data upload before there is video capture.
249 EXPECT_TRUE(uploader
.IsSessionDataUploadAllowed());
251 // Now mock video capture, and no session data should be allowed.
252 MediaCaptureDevicesDispatcher::GetInstance()->OnMediaRequestStateChanged(
253 0, 0, 0, GURL("http://www.google.com"),
254 content::MEDIA_DEVICE_VIDEO_CAPTURE
,
255 content::MEDIA_REQUEST_STATE_OPENING
);
256 base::RunLoop().RunUntilIdle();
257 EXPECT_FALSE(uploader
.IsSessionDataUploadAllowed());
260 } // namespace policy