OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "chrome/browser/chromeos/login/screens/update_screen.h" | |
6 #include "base/command_line.h" | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "base/test/scoped_mock_time_message_loop_task_runner.h" | |
9 #include "chrome/browser/chromeos/login/screens/mock_base_screen_delegate.h" | |
10 #include "chrome/browser/chromeos/login/screens/mock_error_screen.h" | |
11 #include "chrome/browser/chromeos/login/screens/mock_update_screen.h" | |
12 #include "chrome/browser/chromeos/login/startup_utils.h" | |
13 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
14 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
15 #include "chrome/test/base/scoped_testing_local_state.h" | |
16 #include "chrome/test/base/testing_browser_process.h" | |
17 #include "chromeos/chromeos_switches.h" | |
18 #include "chromeos/dbus/dbus_thread_manager.h" | |
19 #include "chromeos/dbus/fake_update_engine_client.h" | |
20 #include "chromeos/dbus/update_engine_client.h" | |
21 #include "chromeos/network/network_handler.h" | |
22 #include "chromeos/network/portal_detector/mock_network_portal_detector.h" | |
23 #include "chromeos/network/portal_detector/network_portal_detector.h" | |
24 #include "components/pairing/fake_host_pairing_controller.h" | |
25 #include "content/public/test/test_browser_thread_bundle.h" | |
26 #include "testing/gtest/include/gtest/gtest.h" | |
27 | |
28 using testing::_; | |
29 using testing::AnyNumber; | |
30 using testing::Return; | |
31 | |
32 namespace chromeos { | |
33 | |
34 class UpdateScreenUnitTest : public testing::Test { | |
35 public: | |
36 UpdateScreenUnitTest() : local_state_(TestingBrowserProcess::GetGlobal()) {} | |
37 | |
38 // Fast-forwards time by the specified amount. | |
39 void FastForwardTime(base::TimeDelta time) { | |
40 runner_.task_runner()->FastForwardBy(time); | |
xiyuan
2017/05/15 17:24:23
Is this relevant?
kumarniranjan
2017/05/15 22:08:22
Good catch! It's no longer needed.
| |
41 base::Time last = StartupUtils::GetTimeOfLastUpdateCheckWithoutUpdate(); | |
xiyuan
2017/05/15 17:24:23
ASSERT_FALSE(last.is_null());
kumarniranjan
2017/05/15 22:08:21
In StartupUtils::RegisterPrefs, I set this pref to
| |
42 base::Time modified_last = last - time; | |
43 StartupUtils::SaveTimeOfLastUpdateCheckWithoutUpdate(modified_last); | |
44 } | |
45 | |
46 // Simulates an update being available (or not). | |
47 // The parameter "available" indicates whether an update is available. | |
48 // The parameter "critical" indicates whether that update is critical. | |
49 void SimulateUpdateAvailable(bool available, bool critical = false) { | |
xiyuan
2017/05/15 17:24:23
Avoid using default arg.
kumarniranjan
2017/05/15 22:08:21
Done.
| |
50 update_engine_status_.status = | |
51 UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE; | |
52 fake_update_engine_client_->NotifyObserversThatStatusChanged( | |
53 update_engine_status_); | |
54 if (critical) { | |
55 ASSERT_TRUE(available) << "Does not make sense for an update to be " | |
56 "critical if one is not even available."; | |
57 first_update_screen_->is_ignore_update_deadlines_ = true; | |
58 } | |
59 update_engine_status_.status = | |
60 available ? UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE | |
61 : UpdateEngineClient::UPDATE_STATUS_IDLE; | |
62 fake_update_engine_client_->NotifyObserversThatStatusChanged( | |
63 update_engine_status_); | |
64 } | |
65 | |
66 // testing::Test: | |
67 void SetUp() override { | |
68 // Configure the browser to use Hands-Off Enrollment. | |
69 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
70 switches::kEnterpriseEnableZeroTouchEnrollment, "hands-off"); | |
71 | |
72 // Initialize objects needed by UpdateScreen | |
73 fake_update_engine_client_ = new FakeUpdateEngineClient(); | |
74 DBusThreadManager::GetSetterForTesting()->SetUpdateEngineClient( | |
75 std::unique_ptr<UpdateEngineClient>(fake_update_engine_client_)); | |
76 NetworkHandler::Initialize(); | |
77 mock_network_portal_detector_ = new MockNetworkPortalDetector(); | |
78 network_portal_detector::InitializeForTesting( | |
79 mock_network_portal_detector_); | |
80 mock_error_screen_ = | |
81 new MockErrorScreen(&mock_base_screen_delegate_, &mock_error_view_); | |
82 fake_controller_ = new pairing_chromeos::FakeHostPairingController(""); | |
83 | |
84 // Verify that we are using the right TaskRunner. | |
85 EXPECT_EQ(runner_.task_runner(), | |
86 base::MessageLoop::current()->task_runner().get()); | |
87 | |
88 // Ensure proper behavior of UpdateScreen's supporting objects. | |
89 EXPECT_CALL(*mock_network_portal_detector_, IsEnabled()) | |
90 .Times(AnyNumber()) | |
91 .WillRepeatedly(Return(false)); | |
92 EXPECT_CALL(mock_base_screen_delegate_, GetErrorScreen()) | |
93 .Times(AnyNumber()) | |
94 .WillRepeatedly(Return(mock_error_screen_)); | |
95 | |
96 // Later verifies that UpdateScreen successfully exits both times. | |
97 EXPECT_CALL(mock_base_screen_delegate_, | |
98 OnExit(_, ScreenExitCode::UPDATE_NOUPDATE, _)) | |
99 .Times(2); | |
100 } | |
101 | |
102 void TearDown() override { | |
103 TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); | |
104 delete first_update_screen_; | |
105 delete second_update_screen_; | |
106 delete mock_error_screen_; | |
xiyuan
2017/05/15 17:24:23
Use std::uniqe_ptr<> and call .reset() here instea
kumarniranjan
2017/05/15 22:08:21
Done.
| |
107 network_portal_detector::Shutdown(); | |
108 network_portal_detector::EndTest(); | |
109 NetworkHandler::Shutdown(); | |
110 DBusThreadManager::Shutdown(); | |
111 } | |
112 | |
113 protected: | |
114 // A pointer to the UpdateScreen that shows up during the first OOBE. | |
115 UpdateScreen* first_update_screen_; | |
116 | |
117 // A pointer to the UpdateScreen which shows up during the second OOBE. | |
118 // This test verifies proper behavior if the device is restarted before | |
119 // OOBE is complete, which is why there is a second OOBE. | |
120 UpdateScreen* second_update_screen_; | |
121 | |
122 // Accessory objects needed by UpdateScreen. | |
123 MockBaseScreenDelegate mock_base_screen_delegate_; | |
124 MockUpdateView mock_view_; | |
125 MockNetworkErrorView mock_error_view_; | |
126 UpdateEngineClient::Status update_engine_status_; | |
127 MockErrorScreen* mock_error_screen_; | |
128 pairing_chromeos::FakeHostPairingController* fake_controller_; | |
129 MockNetworkPortalDetector* mock_network_portal_detector_; | |
130 FakeUpdateEngineClient* fake_update_engine_client_; | |
131 | |
132 private: | |
133 // Test versions of core browser infrastructure. | |
134 content::TestBrowserThreadBundle threads_; | |
135 base::ScopedMockTimeMessageLoopTaskRunner runner_; | |
136 ScopedTestingLocalState local_state_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(UpdateScreenUnitTest); | |
139 }; | |
140 | |
141 // Test Scenario Description: | |
142 // In this description, "will" refers to an external event, and "should" refers | |
143 // to the expected behavior of the DUT in response to external events. | |
144 // | |
145 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
146 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
147 // it checks for updates. It will find that there is indeed an update | |
148 // available, will then install it. After installing the update, it should | |
149 // continue with Hands-Off OOBE. Then, before OOBE is complete, something | |
150 // (could be user, environment, anything) will cause the DUT to reboot. | |
151 // Since OOBE is not complete, the DUT goes through OOBE again. | |
152 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
153 // it should check for updates again. | |
154 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfFirstIsInstalled) { | |
155 // DUT reaches UpdateScreen for the first time. | |
156 first_update_screen_ = new UpdateScreen(&mock_base_screen_delegate_, | |
157 &mock_view_, fake_controller_); | |
158 first_update_screen_->StartNetworkCheck(); | |
159 | |
160 // Verify that the DUT checks for an update. | |
161 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
162 | |
163 // An update is available. | |
164 SimulateUpdateAvailable(true); | |
165 | |
166 // DUT reboots... | |
167 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
168 second_update_screen_ = new UpdateScreen(&mock_base_screen_delegate_, | |
169 &mock_view_, fake_controller_); | |
170 second_update_screen_->StartNetworkCheck(); | |
xiyuan
2017/05/15 17:24:23
Looks like |first_update_screen_| and |second_upda
kumarniranjan
2017/05/15 22:08:22
Done.
| |
171 | |
172 // Verify that the DUT checks for updates again. | |
173 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
174 | |
175 // No updates available this time. | |
176 SimulateUpdateAvailable(false); | |
xiyuan
2017/05/15 17:24:22
Is this call necessary? There is no follow up test
kumarniranjan
2017/05/15 22:08:22
It is necessary to satisfy the expectation defined
| |
177 } | |
178 | |
179 // Test Scenario Description: | |
180 // In this description, "will" refers to an external event, and "should" refers | |
181 // to the expected behavior of the DUT in response to external events. | |
182 // | |
183 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
184 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
185 // it checks for updates. It will find that there are no updates | |
186 // available, and it should leave the UpdateScreen without installing any | |
187 // updates. It continues with OOBE. Then, before OOBE is complete, something | |
188 // (could be user, environment, anything) will cause the DUT to reboot. | |
189 // Since OOBE is not complete, the DUT goes through OOBE again. | |
190 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
191 // more than one hour will have passed since the previous update check. | |
192 // Therefore, the DUT should check for updates again. | |
193 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfEnoughTimePasses) { | |
194 // DUT reaches UpdateScreen for the first time. | |
195 first_update_screen_ = new UpdateScreen(&mock_base_screen_delegate_, | |
196 &mock_view_, fake_controller_); | |
197 first_update_screen_->StartNetworkCheck(); | |
198 | |
199 // Verify that the DUT checks for updates. | |
200 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
201 | |
202 // No updates are available. | |
203 SimulateUpdateAvailable(false); | |
204 | |
205 // Fast-forward time by one hour. | |
206 FastForwardTime(base::TimeDelta::FromHours(1)); | |
207 | |
208 // DUT reboots... | |
209 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
210 second_update_screen_ = new UpdateScreen(&mock_base_screen_delegate_, | |
211 &mock_view_, fake_controller_); | |
212 second_update_screen_->StartNetworkCheck(); | |
213 | |
214 // Verify that the DUT checks for updates again. | |
215 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
216 | |
217 // No updates available this time either. | |
218 SimulateUpdateAvailable(false); | |
219 } | |
220 | |
221 // Test Scenario Description: | |
222 // In this description, "will" refers to an external event, and "should" refers | |
223 // to the expected behavior of the DUT in response to external events. | |
224 // | |
225 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
226 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
227 // it checks for updates. It will find that there are no updates | |
228 // available, and it should leave the UpdateScreen without installing any | |
229 // updates. It continues with OOBE. Then, before OOBE is complete, something | |
230 // (could be user, environment, anything) will cause the DUT to reboot. | |
231 // Since OOBE is not complete, the DUT goes through OOBE again. | |
232 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
233 // less than one hour will have passed since the previous update check. | |
234 // Therefore, the DUT should not check for updates again. | |
235 TEST_F(UpdateScreenUnitTest, ChecksForUpdateOnceButNotAgainIfTooSoon) { | |
236 // DUT reaches UpdateScreen for the first time. | |
237 first_update_screen_ = new UpdateScreen(&mock_base_screen_delegate_, | |
238 &mock_view_, fake_controller_); | |
239 first_update_screen_->StartNetworkCheck(); | |
240 | |
241 // Verify that the DUT checks for updates. | |
242 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
243 | |
244 // No update available. | |
245 SimulateUpdateAvailable(false); | |
246 | |
247 // DUT reboots... | |
248 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
249 second_update_screen_ = new UpdateScreen(&mock_base_screen_delegate_, | |
250 &mock_view_, fake_controller_); | |
251 second_update_screen_->StartNetworkCheck(); | |
252 | |
253 // Verify that the DUT did not check for updates again. | |
254 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
255 | |
256 // No update available this time either. | |
257 SimulateUpdateAvailable(false); | |
258 } | |
259 | |
260 // Test Scenario Description: | |
261 // In this description, "will" refers to an external event, and "should" refers | |
262 // to the expected behavior of the DUT in response to external events. | |
263 // | |
264 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
265 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
266 // it checks for updates. It will find that a critical update is available. | |
267 // The DUT installs the update, and because the update is critical, it reboots. | |
268 // Since OOBE is not complete, the DUT goes through OOBE again after reboot. | |
269 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
270 // it should check for updates again. | |
271 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfCriticalUpdate) { | |
272 // DUT reaches UpdateScreen for the first time. | |
273 first_update_screen_ = new UpdateScreen(&mock_base_screen_delegate_, | |
274 &mock_view_, fake_controller_); | |
275 first_update_screen_->StartNetworkCheck(); | |
276 | |
277 // Verify that the DUT checks for updates. | |
278 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
279 | |
280 // An update is available, and it's critical! | |
281 SimulateUpdateAvailable(true, true); | |
282 | |
283 // DUT reboots... | |
284 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
285 second_update_screen_ = new UpdateScreen(&mock_base_screen_delegate_, | |
286 &mock_view_, fake_controller_); | |
287 second_update_screen_->StartNetworkCheck(); | |
288 | |
289 // Verify that the DUT checks for updates again. | |
290 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
291 | |
292 // No update available this time. | |
293 SimulateUpdateAvailable(false); | |
294 } | |
295 | |
296 } // namespace chromeos | |
OLD | NEW |