| OLD | NEW |
| 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/status_uploader.h" | 5 #include "chrome/browser/chromeos/policy/status_uploader.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/test/test_simple_task_runner.h" | 10 #include "base/test/test_simple_task_runner.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 namespace em = enterprise_management; | 46 namespace em = enterprise_management; |
| 47 | 47 |
| 48 namespace { | 48 namespace { |
| 49 | 49 |
| 50 class MockDeviceStatusCollector : public policy::DeviceStatusCollector { | 50 class MockDeviceStatusCollector : public policy::DeviceStatusCollector { |
| 51 public: | 51 public: |
| 52 explicit MockDeviceStatusCollector(PrefService* local_state) | 52 explicit MockDeviceStatusCollector(PrefService* local_state) |
| 53 : DeviceStatusCollector( | 53 : DeviceStatusCollector( |
| 54 local_state, | 54 local_state, |
| 55 nullptr, | 55 nullptr, |
| 56 policy::DeviceStatusCollector::LocationUpdateRequester(), | |
| 57 policy::DeviceStatusCollector::VolumeInfoFetcher(), | 56 policy::DeviceStatusCollector::VolumeInfoFetcher(), |
| 58 policy::DeviceStatusCollector::CPUStatisticsFetcher(), | 57 policy::DeviceStatusCollector::CPUStatisticsFetcher(), |
| 59 policy::DeviceStatusCollector::CPUTempFetcher()) {} | 58 policy::DeviceStatusCollector::CPUTempFetcher()) {} |
| 60 | 59 |
| 61 MOCK_METHOD1( | 60 MOCK_METHOD1(GetDeviceAndSessionStatusAsync, |
| 62 GetDeviceStatusAsync, | 61 void(const policy::DeviceStatusCollector::StatusCallback&)); |
| 63 void(const policy::DeviceStatusCollector::DeviceStatusCallback&)); | |
| 64 MOCK_METHOD1( | |
| 65 GetDeviceSessionStatusAsync, | |
| 66 void(const policy::DeviceStatusCollector::DeviceSessionStatusCallback&)); | |
| 67 | 62 |
| 68 // Explicit mock implementation declared here, since gmock::Invoke can't | 63 // Explicit mock implementation declared here, since gmock::Invoke can't |
| 69 // handle returning non-moveable types like scoped_ptr. | 64 // handle returning non-moveable types like scoped_ptr. |
| 70 std::unique_ptr<policy::DeviceLocalAccount> GetAutoLaunchedKioskSessionInfo() | 65 std::unique_ptr<policy::DeviceLocalAccount> GetAutoLaunchedKioskSessionInfo() |
| 71 override { | 66 override { |
| 72 return base::MakeUnique<policy::DeviceLocalAccount>( | 67 return base::MakeUnique<policy::DeviceLocalAccount>( |
| 73 policy::DeviceLocalAccount::TYPE_KIOSK_APP, "account_id", "app_id", | 68 policy::DeviceLocalAccount::TYPE_KIOSK_APP, "account_id", "app_id", |
| 74 "update_url"); | 69 "update_url"); |
| 75 } | 70 } |
| 76 }; | 71 }; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 100 void TearDown() override { | 95 void TearDown() override { |
| 101 content::RunAllBlockingPoolTasksUntilIdle(); | 96 content::RunAllBlockingPoolTasksUntilIdle(); |
| 102 } | 97 } |
| 103 | 98 |
| 104 // Given a pending task to upload status, mocks out a server response. | 99 // Given a pending task to upload status, mocks out a server response. |
| 105 void RunPendingUploadTaskAndCheckNext(const StatusUploader& uploader, | 100 void RunPendingUploadTaskAndCheckNext(const StatusUploader& uploader, |
| 106 base::TimeDelta expected_delay) { | 101 base::TimeDelta expected_delay) { |
| 107 // Running the task should pass two callbacks into GetDeviceStatusAsync | 102 // Running the task should pass two callbacks into GetDeviceStatusAsync |
| 108 // and GetDeviceSessionStatusAsync. We'll grab these two callbacks. | 103 // and GetDeviceSessionStatusAsync. We'll grab these two callbacks. |
| 109 EXPECT_TRUE(task_runner_->HasPendingTask()); | 104 EXPECT_TRUE(task_runner_->HasPendingTask()); |
| 110 DeviceStatusCollector::DeviceStatusCallback ds_callback; | 105 DeviceStatusCollector::StatusCallback status_callback; |
| 111 DeviceStatusCollector::DeviceSessionStatusCallback ss_callback; | 106 EXPECT_CALL(*collector_ptr_, GetDeviceAndSessionStatusAsync(_)) |
| 112 EXPECT_CALL(*collector_ptr_, GetDeviceStatusAsync(_)) | 107 .WillOnce(SaveArg<0>(&status_callback)); |
| 113 .WillOnce(SaveArg<0>(&ds_callback)); | |
| 114 EXPECT_CALL(*collector_ptr_, GetDeviceSessionStatusAsync(_)) | |
| 115 .WillOnce(SaveArg<0>(&ss_callback)); | |
| 116 task_runner_->RunPendingTasks(); | 108 task_runner_->RunPendingTasks(); |
| 117 testing::Mock::VerifyAndClearExpectations(&device_management_service_); | 109 testing::Mock::VerifyAndClearExpectations(&device_management_service_); |
| 118 | 110 |
| 119 // Send some "valid" (read: non-nullptr) to the device/session callbacks | 111 // Send some "valid" (read: non-nullptr) to the device/session callbacks |
| 120 // in order to simulate valid status data. | 112 // in order to simulate valid status data. |
| 121 std::unique_ptr<em::DeviceStatusReportRequest> device_status = | 113 std::unique_ptr<em::DeviceStatusReportRequest> device_status = |
| 122 base::MakeUnique<em::DeviceStatusReportRequest>(); | 114 base::MakeUnique<em::DeviceStatusReportRequest>(); |
| 123 std::unique_ptr<em::SessionStatusReportRequest> session_status = | 115 std::unique_ptr<em::SessionStatusReportRequest> session_status = |
| 124 base::MakeUnique<em::SessionStatusReportRequest>(); | 116 base::MakeUnique<em::SessionStatusReportRequest>(); |
| 125 | 117 |
| 126 // Running the session and device callbacks should trigger | 118 // Running the session and device callbacks should trigger |
| 127 // CloudPolicyClient::UploadDeviceStatus. | 119 // CloudPolicyClient::UploadDeviceStatus. |
| 128 CloudPolicyClient::StatusCallback callback; | 120 CloudPolicyClient::StatusCallback callback; |
| 129 EXPECT_CALL(client_, UploadDeviceStatus(_, _, _)) | 121 EXPECT_CALL(client_, UploadDeviceStatus(_, _, _)) |
| 130 .WillOnce(SaveArg<2>(&callback)); | 122 .WillOnce(SaveArg<2>(&callback)); |
| 131 ds_callback.Run(std::move(device_status)); | 123 status_callback.Run(std::move(device_status), std::move(session_status)); |
| 132 ss_callback.Run(std::move(session_status)); | |
| 133 testing::Mock::VerifyAndClearExpectations(&device_management_service_); | 124 testing::Mock::VerifyAndClearExpectations(&device_management_service_); |
| 134 | 125 |
| 135 // Make sure no status upload is queued up yet (since an upload is in | 126 // Make sure no status upload is queued up yet (since an upload is in |
| 136 // progress). | 127 // progress). |
| 137 EXPECT_FALSE(task_runner_->HasPendingTask()); | 128 EXPECT_FALSE(task_runner_->HasPendingTask()); |
| 138 | 129 |
| 139 // Now invoke the response. | 130 // Now invoke the response. |
| 140 callback.Run(true); | 131 callback.Run(true); |
| 141 | 132 |
| 142 // Now that the previous request was satisfied, a task to do the next | 133 // Now that the previous request was satisfied, a task to do the next |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); | 198 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 208 } | 199 } |
| 209 | 200 |
| 210 TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) { | 201 TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) { |
| 211 StatusUploader uploader(&client_, std::move(collector_), task_runner_); | 202 StatusUploader uploader(&client_, std::move(collector_), task_runner_); |
| 212 | 203 |
| 213 // Running the queued task should pass two callbacks into GetDeviceStatusAsync | 204 // Running the queued task should pass two callbacks into GetDeviceStatusAsync |
| 214 // and GetDeviceSessionStatusAsync. We'll grab these two callbacks and send | 205 // and GetDeviceSessionStatusAsync. We'll grab these two callbacks and send |
| 215 // nullptrs to them in order to simulate failure to get status. | 206 // nullptrs to them in order to simulate failure to get status. |
| 216 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); | 207 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 217 DeviceStatusCollector::DeviceStatusCallback ds_callback; | 208 DeviceStatusCollector::StatusCallback status_callback; |
| 218 DeviceStatusCollector::DeviceSessionStatusCallback ss_callback; | 209 EXPECT_CALL(*collector_ptr_, GetDeviceAndSessionStatusAsync(_)) |
| 219 EXPECT_CALL(*collector_ptr_, GetDeviceStatusAsync(_)) | 210 .WillOnce(SaveArg<0>(&status_callback)); |
| 220 .WillOnce(SaveArg<0>(&ds_callback)); | |
| 221 EXPECT_CALL(*collector_ptr_, GetDeviceSessionStatusAsync(_)) | |
| 222 .WillOnce(SaveArg<0>(&ss_callback)); | |
| 223 task_runner_->RunPendingTasks(); | 211 task_runner_->RunPendingTasks(); |
| 224 testing::Mock::VerifyAndClearExpectations(&device_management_service_); | 212 testing::Mock::VerifyAndClearExpectations(&device_management_service_); |
| 225 | 213 |
| 226 // Running the session and device callbacks should trigger | 214 // Running the session and device callbacks should trigger |
| 227 // StatusUploader::OnStatusReceived, which in turn should recognize the | 215 // StatusUploader::OnStatusReceived, which in turn should recognize the |
| 228 // failure to get status and queue another status upload. | 216 // failure to get status and queue another status upload. |
| 229 std::unique_ptr<em::DeviceStatusReportRequest> invalid_device_status; | 217 std::unique_ptr<em::DeviceStatusReportRequest> invalid_device_status; |
| 230 ds_callback.Run(std::move(invalid_device_status)); | |
| 231 EXPECT_EQ(0U, task_runner_->NumPendingTasks()); // Not yet... | |
| 232 | |
| 233 std::unique_ptr<em::SessionStatusReportRequest> invalid_session_status; | 218 std::unique_ptr<em::SessionStatusReportRequest> invalid_session_status; |
| 234 ss_callback.Run(std::move(invalid_session_status)); | 219 status_callback.Run(std::move(invalid_device_status), |
| 235 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); // but now! | 220 std::move(invalid_session_status)); |
| 221 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 236 | 222 |
| 237 // Check the delay of the queued upload | 223 // Check the delay of the queued upload |
| 238 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds( | 224 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds( |
| 239 StatusUploader::kDefaultUploadDelayMs); | 225 StatusUploader::kDefaultUploadDelayMs); |
| 240 CheckPendingTaskDelay(uploader, expected_delay); | 226 CheckPendingTaskDelay(uploader, expected_delay); |
| 241 } | 227 } |
| 242 | 228 |
| 243 TEST_F(StatusUploaderTest, ChangeFrequency) { | 229 TEST_F(StatusUploaderTest, ChangeFrequency) { |
| 244 StatusUploader uploader(&client_, std::move(collector_), task_runner_); | 230 StatusUploader uploader(&client_, std::move(collector_), task_runner_); |
| 245 // Change the frequency. The new frequency should be reflected in the timing | 231 // Change the frequency. The new frequency should be reflected in the timing |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 // Now mock video capture, and no session data should be allowed. | 271 // Now mock video capture, and no session data should be allowed. |
| 286 MediaCaptureDevicesDispatcher::GetInstance()->OnMediaRequestStateChanged( | 272 MediaCaptureDevicesDispatcher::GetInstance()->OnMediaRequestStateChanged( |
| 287 0, 0, 0, GURL("http://www.google.com"), | 273 0, 0, 0, GURL("http://www.google.com"), |
| 288 content::MEDIA_DEVICE_VIDEO_CAPTURE, | 274 content::MEDIA_DEVICE_VIDEO_CAPTURE, |
| 289 content::MEDIA_REQUEST_STATE_OPENING); | 275 content::MEDIA_REQUEST_STATE_OPENING); |
| 290 base::RunLoop().RunUntilIdle(); | 276 base::RunLoop().RunUntilIdle(); |
| 291 EXPECT_FALSE(uploader.IsSessionDataUploadAllowed()); | 277 EXPECT_FALSE(uploader.IsSessionDataUploadAllowed()); |
| 292 } | 278 } |
| 293 | 279 |
| 294 } // namespace policy | 280 } // namespace policy |
| OLD | NEW |