Chromium Code Reviews| 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_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::Invoke; | |
| 23 using ::testing::Return; | |
| 24 using ::testing::WithArgs; | |
| 25 | |
| 26 namespace em = enterprise_management; | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 void StatusCallbackSuccess( | |
| 31 const policy::CloudPolicyClient::StatusCallback& callback) { | |
| 32 base::MessageLoop::current()->PostTask( | |
| 33 FROM_HERE, base::Bind(callback, true)); | |
| 34 } | |
| 35 | |
| 36 class MockDeviceStatusCollector : public policy::DeviceStatusCollector { | |
| 37 public: | |
| 38 explicit MockDeviceStatusCollector(PrefService* local_state) | |
| 39 : DeviceStatusCollector( | |
| 40 local_state, | |
| 41 nullptr, | |
| 42 policy::DeviceStatusCollector::LocationUpdateRequester(), | |
| 43 policy::DeviceStatusCollector::VolumeInfoFetcher()) { | |
| 44 } | |
| 45 | |
| 46 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest*)); | |
| 47 MOCK_METHOD1(GetDeviceSessionStatus, bool(em::SessionStatusReportRequest*)); | |
| 48 }; | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 namespace policy { | |
| 53 class StatusUploaderTest : public testing::Test { | |
| 54 public: | |
| 55 StatusUploaderTest() | |
| 56 : task_runner_(new base::TestSimpleTaskRunner()) { | |
| 57 DeviceStatusCollector::RegisterPrefs(prefs_.registry()); | |
| 58 prefs_.registry()->RegisterIntegerPref( | |
| 59 prefs::kDeviceStatusUploadRate, | |
| 60 StatusUploader::kDefaultUploadDelayMs); | |
| 61 } | |
| 62 | |
| 63 void SetUp() override { | |
| 64 client_.SetDMToken("dm_token"); | |
| 65 collector_.reset(new MockDeviceStatusCollector(&prefs_)); | |
| 66 } | |
| 67 | |
| 68 void TearDown() override { | |
| 69 content::RunAllBlockingPoolTasksUntilIdle(); | |
| 70 } | |
| 71 | |
| 72 // Given a pending task to upload status, mocks out a server response. | |
| 73 void RunPendingUploadTaskAndCheckNext(const StatusUploader& uploader) { | |
| 74 EXPECT_FALSE(task_runner_->GetPendingTasks().empty()); | |
| 75 EXPECT_CALL(client_, UploadDeviceStatus(_, _, _)) | |
| 76 .WillOnce(WithArgs<2>(Invoke(StatusCallbackSuccess))); | |
| 77 task_runner_->RunPendingTasks(); | |
| 78 testing::Mock::VerifyAndClearExpectations(&device_management_service_); | |
| 79 // Make sure no status upload is queued up yet (since an upload is in | |
| 80 // progress). | |
| 81 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
| 82 | |
| 83 // Now invoke the response. | |
|
Mattias Nissler (ping if slow)
2015/01/28 15:44:06
nit: It's probably clearer to just record the call
Andrew T Wilson (Slow)
2015/01/28 16:13:09
OK, done. The only reason I didn't is because I do
| |
| 84 loop_.RunUntilIdle(); | |
| 85 | |
| 86 // Now that the previous request was satisfied, a task to do the next | |
| 87 // upload should be queued. | |
| 88 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | |
| 89 | |
| 90 // The next refresh would have been scheduled sometime during the call to | |
| 91 // MockResponseForPendingUploadTask() above. So the next task should be | |
|
Mattias Nissler (ping if slow)
2015/01/28 15:44:06
nit: stale function name reference
Andrew T Wilson (Slow)
2015/01/28 16:13:09
Done.
| |
| 92 // scheduled sometime between |last_upload| + kDefaultUploadDelayMs and | |
| 93 // |now| + kDefaultUploadDelayMs. | |
| 94 base::Time now = base::Time::NowFromSystemTime(); | |
| 95 base::Time next_task = now + task_runner_->NextPendingTaskDelay(); | |
| 96 | |
| 97 base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds( | |
| 98 StatusUploader::kDefaultUploadDelayMs); | |
| 99 EXPECT_LE(next_task, now + expected_delay); | |
| 100 EXPECT_GE(next_task, uploader.last_upload() + expected_delay); | |
| 101 } | |
| 102 | |
| 103 base::MessageLoop loop_; | |
| 104 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 105 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | |
| 106 chromeos::ScopedTestCrosSettings test_cros_settings_; | |
| 107 scoped_ptr<MockDeviceStatusCollector> collector_; | |
| 108 MockCloudPolicyClient client_; | |
| 109 MockDeviceManagementService device_management_service_; | |
| 110 TestingPrefServiceSimple prefs_; | |
| 111 }; | |
| 112 | |
| 113 TEST_F(StatusUploaderTest, BasicTest) { | |
| 114 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
| 115 StatusUploader uploader( | |
| 116 &prefs_, &client_, collector_.Pass(), task_runner_); | |
| 117 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | |
| 118 // On startup, first update should happen immediately. | |
| 119 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay()); | |
| 120 } | |
| 121 | |
| 122 TEST_F(StatusUploaderTest, ResetTimerAfterStatusCollection) { | |
| 123 // Keep a pointer to the mock collector because the scoped_ptr gets cleared | |
| 124 // when it is passed to CreateStatusUploader() below. | |
| 125 MockDeviceStatusCollector* mock_collector = collector_.get(); | |
| 126 StatusUploader uploader( | |
| 127 &prefs_, &client_, collector_.Pass(), task_runner_); | |
| 128 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillRepeatedly(Return(true)); | |
| 129 EXPECT_CALL(*mock_collector, GetDeviceSessionStatus(_)).WillRepeatedly( | |
| 130 Return(true)); | |
| 131 RunPendingUploadTaskAndCheckNext(uploader); | |
| 132 | |
| 133 // Handle this response also, and ensure new task is queued. | |
| 134 RunPendingUploadTaskAndCheckNext(uploader); | |
| 135 | |
| 136 // Now that the previous request was satisfied, a task to do the next | |
| 137 // upload should be queued again. | |
| 138 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | |
| 139 } | |
| 140 | |
| 141 TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) { | |
| 142 // Keep a pointer to the mock collector because the scoped_ptr gets cleared | |
| 143 // when it is passed to CreateStatusUploader() below. | |
| 144 MockDeviceStatusCollector* mock_collector = collector_.get(); | |
| 145 StatusUploader uploader( | |
| 146 &prefs_, &client_, collector_.Pass(), task_runner_); | |
| 147 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillOnce(Return(false)); | |
| 148 EXPECT_CALL(*mock_collector, GetDeviceSessionStatus(_)).WillOnce( | |
| 149 Return(false)); | |
| 150 task_runner_->RunPendingTasks(); | |
| 151 | |
| 152 // Make sure the next status upload is queued up. | |
| 153 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | |
| 154 } | |
| 155 | |
| 156 } // namespace policy | |
| OLD | NEW |