OLD | NEW |
(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_device_management_service.h" |
| 15 #include "content/public/test/test_utils.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using ::testing::_; |
| 21 using ::testing::Return; |
| 22 |
| 23 namespace em = enterprise_management; |
| 24 |
| 25 namespace { |
| 26 |
| 27 class MockDeviceStatusCollector : public policy::DeviceStatusCollector { |
| 28 public: |
| 29 explicit MockDeviceStatusCollector(PrefService* local_state) |
| 30 : DeviceStatusCollector( |
| 31 local_state, |
| 32 nullptr, |
| 33 policy::DeviceStatusCollector::LocationUpdateRequester(), |
| 34 policy::DeviceStatusCollector::VolumeInfoFetcher()) { |
| 35 } |
| 36 |
| 37 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest*)); |
| 38 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest*)); |
| 39 }; |
| 40 |
| 41 } // namespace |
| 42 |
| 43 namespace policy { |
| 44 class StatusUploaderTest : public testing::Test { |
| 45 public: |
| 46 StatusUploaderTest() |
| 47 : task_runner_(new base::TestSimpleTaskRunner()) { |
| 48 DeviceStatusCollector::RegisterPrefs(prefs_.registry()); |
| 49 prefs_.registry()->RegisterIntegerPref( |
| 50 prefs::kDeviceStatusUploadRate, |
| 51 StatusUploader::kDefaultUploadDelayMs); |
| 52 } |
| 53 |
| 54 void SetUp() override { |
| 55 client_.reset(new CloudPolicyClient( |
| 56 "machine_id", |
| 57 "machine_model", |
| 58 GetPolicyVerificationKey(), |
| 59 UserAffiliation::USER_AFFILIATION_NONE, |
| 60 &device_management_service_, |
| 61 scoped_refptr<net::URLRequestContextGetter>())); |
| 62 client_->SetupRegistration("dm_token", "client_id"); |
| 63 |
| 64 collector_.reset(new MockDeviceStatusCollector(&prefs_)); |
| 65 } |
| 66 |
| 67 void TearDown() override { |
| 68 content::RunAllBlockingPoolTasksUntilIdle(); |
| 69 } |
| 70 |
| 71 // Given a pending task to upload status, mocks out a server response. |
| 72 void MockResponseForPendingUploadTask() { |
| 73 EXPECT_FALSE(task_runner_->GetPendingTasks().empty()); |
| 74 MockDeviceManagementJob* upload_request = NULL; |
| 75 |
| 76 EXPECT_CALL(device_management_service_, |
| 77 CreateJob(DeviceManagementRequestJob::TYPE_UPLOAD_STATUS, _)) |
| 78 .WillOnce(device_management_service_.CreateAsyncJob(&upload_request)); |
| 79 |
| 80 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)); |
| 81 task_runner_->RunPendingTasks(); |
| 82 testing::Mock::VerifyAndClearExpectations(&device_management_service_); |
| 83 |
| 84 ASSERT_TRUE(upload_request); |
| 85 // Make sure no status upload is queued up yet (since an upload is in |
| 86 // progress). |
| 87 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); |
| 88 |
| 89 upload_request->SendResponse(DM_STATUS_SUCCESS, |
| 90 em::DeviceManagementResponse()); |
| 91 } |
| 92 |
| 93 base::MessageLoop loop_; |
| 94 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 95 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 96 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 97 scoped_ptr<MockDeviceStatusCollector> collector_; |
| 98 scoped_ptr<CloudPolicyClient> client_; |
| 99 MockDeviceManagementService device_management_service_; |
| 100 TestingPrefServiceSimple prefs_; |
| 101 }; |
| 102 |
| 103 TEST_F(StatusUploaderTest, BasicTest) { |
| 104 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); |
| 105 StatusUploader uploader( |
| 106 &prefs_, client_.get(), collector_.Pass(), task_runner_); |
| 107 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
| 108 // On startup, first update should happen immediately. |
| 109 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay()); |
| 110 } |
| 111 |
| 112 TEST_F(StatusUploaderTest, ResetTimerAfterStatusCollection) { |
| 113 // Keep a pointer to the mock collector because the scoped_ptr gets cleared |
| 114 // when it is passed to CreateStatusUploader() below. |
| 115 MockDeviceStatusCollector* mock_collector = collector_.get(); |
| 116 StatusUploader uploader( |
| 117 &prefs_, client_.get(), collector_.Pass(), task_runner_); |
| 118 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillRepeatedly(Return(true)); |
| 119 EXPECT_CALL(*mock_collector, GetSessionStatus(_)).WillRepeatedly( |
| 120 Return(true)); |
| 121 MockResponseForPendingUploadTask(); |
| 122 |
| 123 // Now that the previous request was satisfied, a task to do the next |
| 124 // upload should be queued. |
| 125 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
| 126 |
| 127 // Handle this response also. |
| 128 MockResponseForPendingUploadTask(); |
| 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_.get(), collector_.Pass(), task_runner_); |
| 141 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillOnce(Return(false)); |
| 142 EXPECT_CALL(*mock_collector, GetSessionStatus(_)).WillOnce(Return(false)); |
| 143 task_runner_->RunPendingTasks(); |
| 144 |
| 145 // Make sure the next status upload is queued up. |
| 146 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
| 147 } |
| 148 |
| 149 } // namespace policy |
OLD | NEW |