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 "net/url_request/url_request_context_getter.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using ::testing::_; | |
20 using ::testing::Return; | |
21 | |
22 namespace em = enterprise_management; | |
23 | |
24 namespace { | |
25 | |
26 class MockDeviceStatusCollector : public policy::DeviceStatusCollector { | |
27 public: | |
28 explicit MockDeviceStatusCollector(PrefService* local_state) | |
29 : DeviceStatusCollector( | |
30 local_state, | |
31 nullptr, | |
32 policy::DeviceStatusCollector::LocationUpdateRequester(), | |
33 policy::DeviceStatusCollector::VolumeInfoFetcher()) { | |
34 } | |
35 | |
36 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest*)); | |
37 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest*)); | |
38 }; | |
39 | |
40 } // namespace | |
41 | |
42 namespace policy { | |
43 class StatusUploaderTest : public testing::Test { | |
44 public: | |
45 StatusUploaderTest() | |
46 : task_runner_(new base::TestSimpleTaskRunner()) { | |
47 DeviceStatusCollector::RegisterPrefs(prefs_.registry()); | |
48 prefs_.registry()->RegisterIntegerPref( | |
49 prefs::kDeviceStatusUploadRate, | |
50 StatusUploader::kDefaultUploadDelayMs); | |
51 } | |
52 | |
53 void SetUp() override { | |
54 client_.reset(new CloudPolicyClient( | |
55 "machine_id", | |
56 "machine_model", | |
57 GetPolicyVerificationKey(), | |
58 UserAffiliation::USER_AFFILIATION_NONE, | |
59 &device_management_service_, | |
60 scoped_refptr<net::URLRequestContextGetter>())); | |
61 client_->SetupRegistration("dm_token", "client_id"); | |
62 } | |
63 | |
64 | |
65 // Helper function that creates a StatusUploader object to use in tests. | |
66 scoped_ptr<StatusUploader> CreateStatusUploader( | |
67 scoped_ptr<DeviceStatusCollector> collector) { | |
68 return make_scoped_ptr(new StatusUploader( | |
69 &prefs_, | |
70 client_.get(), | |
71 collector.Pass(), | |
72 task_runner_)); | |
73 } | |
74 | |
75 // Given a pending task to upload status, mocks out a server response. | |
76 void MockResponseForPendingUploadTask() { | |
77 EXPECT_FALSE(task_runner_->GetPendingTasks().empty()); | |
78 MockDeviceManagementJob* upload_request = NULL; | |
79 | |
80 EXPECT_CALL(device_management_service_, | |
81 CreateJob(DeviceManagementRequestJob::TYPE_UPLOAD_STATUS, _)) | |
82 .WillOnce(device_management_service_.CreateAsyncJob(&upload_request)); | |
83 | |
84 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)); | |
85 task_runner_->RunPendingTasks(); | |
86 testing::Mock::VerifyAndClearExpectations(&device_management_service_); | |
87 | |
88 EXPECT_TRUE(upload_request); | |
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
This needs to be ASSERT_TRUE, otherwise you crash
Andrew T Wilson (Slow)
2015/01/23 19:16:19
Done.
| |
89 // Make sure no status upload is queued up yet (since an upload is in | |
90 // progress). | |
91 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
92 | |
93 upload_request->SendResponse(DM_STATUS_SUCCESS, | |
94 em::DeviceManagementResponse()); | |
95 } | |
96 | |
97 base::MessageLoop loop_; | |
98 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
99 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | |
100 chromeos::ScopedTestCrosSettings test_cros_settings_; | |
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
It's kinda sad that we have to bring up this heavy
Andrew T Wilson (Slow)
2015/01/23 19:16:19
Yeah, this is the cost of having a CrosSettings si
| |
101 scoped_ptr<CloudPolicyClient> client_; | |
102 MockDeviceManagementService device_management_service_; | |
103 TestingPrefServiceSimple prefs_; | |
104 }; | |
105 | |
106 TEST_F(StatusUploaderTest, BasicTest) { | |
107 scoped_ptr<MockDeviceStatusCollector> collector( | |
108 new MockDeviceStatusCollector(&prefs_)); | |
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
All the tests need a collector - move it to the fi
Andrew T Wilson (Slow)
2015/01/23 19:16:19
Done.
| |
109 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
110 scoped_ptr<StatusUploader> uploader( | |
111 CreateStatusUploader(collector.Pass()).Pass()); | |
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
The additional boilerplate hardly justifies why yo
Andrew T Wilson (Slow)
2015/01/23 19:16:19
Yep, it used to do more stuff. Happy to un-refacto
| |
112 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | |
113 // On startup, first update should happen immediately. | |
114 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay()); | |
115 } | |
116 | |
117 TEST_F(StatusUploaderTest, ResetTimerAfterStatusCollection) { | |
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
Would it make sense to check the delays as well to
Andrew T Wilson (Slow)
2015/01/23 19:16:19
I originally tried this and it's hard to do well -
Mattias Nissler (ping if slow)
2015/01/26 10:37:02
cloud_policy_refresh_scheduler_unittest.cc has som
Andrew T Wilson (Slow)
2015/01/28 13:19:11
Done.
| |
118 scoped_ptr<MockDeviceStatusCollector> collector( | |
119 new MockDeviceStatusCollector(&prefs_)); | |
120 // Keep a pointer to the mock collector because the scoped_ptr gets cleared | |
121 // when it is passed to CreateStatusUploader() below. | |
122 MockDeviceStatusCollector* mock_collector = collector.get(); | |
123 scoped_ptr<StatusUploader> uploader( | |
124 CreateStatusUploader(collector.Pass()).Pass()); | |
125 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillRepeatedly(Return(true)); | |
126 EXPECT_CALL(*mock_collector, GetSessionStatus(_)).WillRepeatedly( | |
127 Return(true)); | |
128 MockResponseForPendingUploadTask(); | |
129 | |
130 // Now that the previous request was satisfied, a task to do the next | |
131 // upload should be queued. | |
132 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | |
133 | |
134 // Handle this response also. | |
135 MockResponseForPendingUploadTask(); | |
136 | |
137 // Now that the previous request was satisfied, a task to do the next | |
138 // upload should be queued again. | |
139 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | |
140 } | |
141 | |
142 TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) { | |
143 scoped_ptr<MockDeviceStatusCollector> collector( | |
144 new MockDeviceStatusCollector(&prefs_)); | |
145 // Keep a pointer to the mock collector because the scoped_ptr gets cleared | |
146 // when it is passed to CreateStatusUploader() below. | |
147 MockDeviceStatusCollector* mock_collector = collector.get(); | |
148 scoped_ptr<StatusUploader> uploader( | |
149 CreateStatusUploader(collector.Pass()).Pass()); | |
150 EXPECT_CALL(*mock_collector, GetDeviceStatus(_)).WillOnce(Return(false)); | |
151 EXPECT_CALL(*mock_collector, GetSessionStatus(_)).WillOnce(Return(false)); | |
152 task_runner_->RunPendingTasks(); | |
153 | |
154 // Make sure the next status upload is queued up. | |
155 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | |
156 } | |
157 | |
158 } // namespace policy | |
OLD | NEW |