Index: chrome/browser/chromeos/policy/status_uploader_unittest.cc |
diff --git a/chrome/browser/chromeos/policy/status_uploader_unittest.cc b/chrome/browser/chromeos/policy/status_uploader_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8e19ca999c16a1d37b12bf03e648db64816f3689 |
--- /dev/null |
+++ b/chrome/browser/chromeos/policy/status_uploader_unittest.cc |
@@ -0,0 +1,149 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/prefs/pref_registry_simple.h" |
+#include "base/prefs/testing_pref_service.h" |
+#include "base/test/test_simple_task_runner.h" |
+#include "chrome/browser/chromeos/policy/device_status_collector.h" |
+#include "chrome/browser/chromeos/policy/status_uploader.h" |
+#include "chrome/browser/chromeos/settings/cros_settings.h" |
+#include "chrome/browser/chromeos/settings/device_settings_service.h" |
+#include "chrome/common/pref_names.h" |
+#include "components/policy/core/common/cloud/cloud_policy_client.h" |
+#include "components/policy/core/common/cloud/mock_device_management_service.h" |
+#include "content/public/test/test_utils.h" |
+#include "net/url_request/url_request_context_getter.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using ::testing::_; |
+using ::testing::Return; |
+ |
+namespace em = enterprise_management; |
+ |
+namespace { |
+ |
+class MockDeviceStatusCollector : public policy::DeviceStatusCollector { |
+ public: |
+ explicit MockDeviceStatusCollector(PrefService* local_state) |
+ : DeviceStatusCollector( |
+ local_state, |
+ nullptr, |
+ policy::DeviceStatusCollector::LocationUpdateRequester(), |
+ policy::DeviceStatusCollector::VolumeInfoFetcher()) { |
+ } |
+ |
+ MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest*)); |
+ MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest*)); |
+}; |
+ |
+} // namespace |
+ |
+namespace policy { |
+class StatusUploaderTest : public testing::Test { |
+ public: |
+ StatusUploaderTest() |
+ : task_runner_(new base::TestSimpleTaskRunner()) { |
+ DeviceStatusCollector::RegisterPrefs(prefs_.registry()); |
+ prefs_.registry()->RegisterIntegerPref( |
+ prefs::kDeviceStatusUploadRate, |
+ StatusUploader::kDefaultUploadDelayMs); |
+ } |
+ |
+ void SetUp() override { |
+ client_.reset(new CloudPolicyClient( |
+ "machine_id", |
+ "machine_model", |
+ GetPolicyVerificationKey(), |
+ UserAffiliation::USER_AFFILIATION_NONE, |
+ &device_management_service_, |
+ scoped_refptr<net::URLRequestContextGetter>())); |
+ client_->SetupRegistration("dm_token", "client_id"); |
+ |
+ collector_.reset(new MockDeviceStatusCollector(&prefs_)); |
+ } |
+ |
+ void TearDown() override { |
+ content::RunAllBlockingPoolTasksUntilIdle(); |
+ } |
+ |
+ // Given a pending task to upload status, mocks out a server response. |
+ void MockResponseForPendingUploadTask() { |
+ EXPECT_FALSE(task_runner_->GetPendingTasks().empty()); |
+ MockDeviceManagementJob* upload_request = NULL; |
+ |
+ EXPECT_CALL(device_management_service_, |
+ CreateJob(DeviceManagementRequestJob::TYPE_UPLOAD_STATUS, _)) |
+ .WillOnce(device_management_service_.CreateAsyncJob(&upload_request)); |
+ |
+ EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)); |
+ task_runner_->RunPendingTasks(); |
+ testing::Mock::VerifyAndClearExpectations(&device_management_service_); |
+ |
+ ASSERT_TRUE(upload_request); |
+ // Make sure no status upload is queued up yet (since an upload is in |
+ // progress). |
+ EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); |
+ |
+ upload_request->SendResponse(DM_STATUS_SUCCESS, |
+ em::DeviceManagementResponse()); |
+ } |
+ |
+ base::MessageLoop loop_; |
+ scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
+ chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
+ chromeos::ScopedTestCrosSettings test_cros_settings_; |
+ scoped_ptr<MockDeviceStatusCollector> collector_; |
+ scoped_ptr<CloudPolicyClient> client_; |
+ MockDeviceManagementService device_management_service_; |
+ TestingPrefServiceSimple prefs_; |
+}; |
+ |
+TEST_F(StatusUploaderTest, BasicTest) { |
+ EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); |
+ StatusUploader uploader( |
+ &prefs_, client_.get(), collector_.Pass(), task_runner_); |
+ EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
+ // On startup, first update should happen immediately. |
+ EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay()); |
+} |
+ |
+TEST_F(StatusUploaderTest, ResetTimerAfterStatusCollection) { |
+ // Keep a pointer to the mock collector because the scoped_ptr gets cleared |
+ // when it is passed to CreateStatusUploader() below. |
+ MockDeviceStatusCollector* mock_collector = collector_.get(); |
+ StatusUploader uploader( |
+ &prefs_, client_.get(), collector_.Pass(), task_runner_); |
+ EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillRepeatedly(Return(true)); |
+ EXPECT_CALL(*mock_collector, GetSessionStatus(_)).WillRepeatedly( |
+ Return(true)); |
+ MockResponseForPendingUploadTask(); |
+ |
+ // Now that the previous request was satisfied, a task to do the next |
+ // upload should be queued. |
+ EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
+ |
+ // Handle this response also. |
+ MockResponseForPendingUploadTask(); |
+ |
+ // Now that the previous request was satisfied, a task to do the next |
+ // upload should be queued again. |
+ EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
+} |
+ |
+TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) { |
+ // Keep a pointer to the mock collector because the scoped_ptr gets cleared |
+ // when it is passed to CreateStatusUploader() below. |
+ MockDeviceStatusCollector* mock_collector = collector_.get(); |
+ StatusUploader uploader( |
+ &prefs_, client_.get(), collector_.Pass(), task_runner_); |
+ EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillOnce(Return(false)); |
+ EXPECT_CALL(*mock_collector, GetSessionStatus(_)).WillOnce(Return(false)); |
+ task_runner_->RunPendingTasks(); |
+ |
+ // Make sure the next status upload is queued up. |
+ EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
+} |
+ |
+} // namespace policy |