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 base::Time last = StartupUtils::GetTimeOfLastUpdateCheckWithoutUpdate(); | |
41 ASSERT_FALSE(last.is_null()); | |
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 "update_screen" points to the currently active UpdateScreen. | |
48 // The parameter "available" indicates whether an update is available. | |
49 // The parameter "critical" indicates whether that update is critical. | |
50 void SimulateUpdateAvailable( | |
51 const std::unique_ptr<UpdateScreen>& update_screen, | |
52 bool available, | |
53 bool critical) { | |
54 update_engine_status_.status = | |
55 UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE; | |
56 fake_update_engine_client_->NotifyObserversThatStatusChanged( | |
57 update_engine_status_); | |
58 if (critical) { | |
59 ASSERT_TRUE(available) << "Does not make sense for an update to be " | |
60 "critical if one is not even available."; | |
61 update_screen->is_ignore_update_deadlines_ = true; | |
62 } | |
63 update_engine_status_.status = | |
64 available ? UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE | |
65 : UpdateEngineClient::UPDATE_STATUS_IDLE; | |
66 fake_update_engine_client_->NotifyObserversThatStatusChanged( | |
67 update_engine_status_); | |
68 } | |
69 | |
70 // testing::Test: | |
71 void SetUp() override { | |
72 // Configure the browser to use Hands-Off Enrollment. | |
73 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
74 switches::kEnterpriseEnableZeroTouchEnrollment, "hands-off"); | |
75 | |
76 // Initialize objects needed by UpdateScreen | |
77 fake_update_engine_client_ = new FakeUpdateEngineClient(); | |
78 DBusThreadManager::GetSetterForTesting()->SetUpdateEngineClient( | |
79 std::unique_ptr<UpdateEngineClient>(fake_update_engine_client_)); | |
80 NetworkHandler::Initialize(); | |
81 mock_network_portal_detector_ = new MockNetworkPortalDetector(); | |
82 network_portal_detector::SetNetworkPortalDetector( | |
83 mock_network_portal_detector_); | |
84 mock_error_screen_.reset( | |
85 new MockErrorScreen(&mock_base_screen_delegate_, &mock_error_view_)); | |
86 fake_controller_ = new pairing_chromeos::FakeHostPairingController(""); | |
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_.get())); | |
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 first_update_screen_.reset(); | |
105 second_update_screen_.reset(); | |
106 mock_error_screen_.reset(); | |
107 network_portal_detector::Shutdown(); | |
108 NetworkHandler::Shutdown(); | |
109 DBusThreadManager::Shutdown(); | |
110 } | |
111 | |
112 protected: | |
113 // A pointer to the UpdateScreen that shows up during the first OOBE. | |
114 std::unique_ptr<UpdateScreen> first_update_screen_; | |
115 | |
116 // A pointer to the UpdateScreen which shows up during the second OOBE. | |
117 // This test verifies proper behavior if the device is restarted before | |
118 // OOBE is complete, which is why there is a second OOBE. | |
119 std::unique_ptr<UpdateScreen> second_update_screen_; | |
120 | |
121 // Accessory objects needed by UpdateScreen. | |
122 MockBaseScreenDelegate mock_base_screen_delegate_; | |
123 MockUpdateView mock_view_; | |
124 MockNetworkErrorView mock_error_view_; | |
125 UpdateEngineClient::Status update_engine_status_; | |
126 std::unique_ptr<MockErrorScreen> mock_error_screen_; | |
127 pairing_chromeos::FakeHostPairingController* fake_controller_; | |
128 MockNetworkPortalDetector* mock_network_portal_detector_; | |
129 FakeUpdateEngineClient* fake_update_engine_client_; | |
130 | |
131 private: | |
132 // Test versions of core browser infrastructure. | |
133 content::TestBrowserThreadBundle threads_; | |
134 ScopedTestingLocalState local_state_; | |
135 | |
136 DISALLOW_COPY_AND_ASSIGN(UpdateScreenUnitTest); | |
137 }; | |
138 | |
139 // Test Scenario Description: | |
140 // In this description, "will" refers to an external event, and "should" refers | |
141 // to the expected behavior of the DUT in response to external events. | |
142 // | |
143 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
144 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
145 // it checks for updates. It will find that there is indeed an update | |
146 // available, will then install it. After installing the update, it should | |
147 // continue with Hands-Off OOBE. Then, before OOBE is complete, something | |
148 // (could be user, environment, anything) will cause the DUT to reboot. | |
149 // Since OOBE is not complete, the DUT goes through OOBE again. | |
150 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
151 // it should check for updates again. | |
152 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfFirstIsInstalled) { | |
153 // DUT reaches UpdateScreen for the first time. | |
154 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, | |
155 &mock_view_, fake_controller_)); | |
156 first_update_screen_->StartNetworkCheck(); | |
157 | |
158 // Verify that the DUT checks for an update. | |
159 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
160 | |
161 // An update is available. | |
162 SimulateUpdateAvailable(first_update_screen_, true /* available */, | |
163 false /* critical */); | |
164 | |
165 // DUT reboots... | |
166 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
167 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, | |
168 &mock_view_, fake_controller_)); | |
169 second_update_screen_->StartNetworkCheck(); | |
170 | |
171 // Verify that the DUT checks for updates again. | |
172 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
173 | |
174 // No updates available this time. | |
175 SimulateUpdateAvailable(second_update_screen_, false /* available */, | |
176 false /* critical */); | |
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_.reset(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(first_update_screen_, false /* available */, | |
204 false /* critical */); | |
205 | |
206 // Fast-forward time by one hour. | |
207 FastForwardTime(base::TimeDelta::FromHours(1)); | |
208 | |
209 // DUT reboots... | |
210 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
211 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, | |
212 &mock_view_, fake_controller_)); | |
213 second_update_screen_->StartNetworkCheck(); | |
214 | |
215 // Verify that the DUT checks for updates again. | |
216 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
217 | |
218 // No updates available this time either. | |
219 SimulateUpdateAvailable(second_update_screen_, false /* available */, | |
220 false /* critical */); | |
221 } | |
222 | |
223 // Test Scenario Description: | |
224 // In this description, "will" refers to an external event, and "should" refers | |
225 // to the expected behavior of the DUT in response to external events. | |
226 // | |
227 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
228 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
229 // it checks for updates. It will find that there are no updates | |
230 // available, and it should leave the UpdateScreen without installing any | |
231 // updates. It continues with OOBE. Then, before OOBE is complete, something | |
232 // (could be user, environment, anything) will cause the DUT to reboot. | |
233 // Since OOBE is not complete, the DUT goes through OOBE again. | |
234 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
235 // less than one hour will have passed since the previous update check. | |
236 // Therefore, the DUT should not check for updates again. | |
237 TEST_F(UpdateScreenUnitTest, ChecksForUpdateOnceButNotAgainIfTooSoon) { | |
238 // DUT reaches UpdateScreen for the first time. | |
239 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, | |
240 &mock_view_, fake_controller_)); | |
241 first_update_screen_->StartNetworkCheck(); | |
242 | |
243 // Verify that the DUT checks for updates. | |
244 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
245 | |
246 // No update available. | |
247 SimulateUpdateAvailable(first_update_screen_, false /* available */, | |
248 false /* critical */); | |
249 | |
250 // DUT reboots... | |
251 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
252 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, | |
253 &mock_view_, fake_controller_)); | |
254 second_update_screen_->StartNetworkCheck(); | |
255 | |
256 // Verify that the DUT did not check for updates again. | |
257 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
258 | |
259 // No update available this time either. | |
260 SimulateUpdateAvailable(second_update_screen_, false /* available */, | |
261 false /* critical */); | |
262 } | |
263 | |
264 // Test Scenario Description: | |
265 // In this description, "will" refers to an external event, and "should" refers | |
266 // to the expected behavior of the DUT in response to external events. | |
267 // | |
268 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
269 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
270 // it checks for updates. It will find that a critical update is available. | |
271 // The DUT installs the update, and because the update is critical, it reboots. | |
272 // Since OOBE is not complete, the DUT goes through OOBE again after reboot. | |
273 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
274 // it should check for updates again. | |
275 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfCriticalUpdate) { | |
276 // DUT reaches UpdateScreen for the first time. | |
277 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, | |
278 &mock_view_, fake_controller_)); | |
279 first_update_screen_->StartNetworkCheck(); | |
280 | |
281 // Verify that the DUT checks for updates. | |
282 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
283 | |
284 // An update is available, and it's critical! | |
285 SimulateUpdateAvailable(first_update_screen_, true /* available */, | |
286 true /* critical */); | |
287 | |
288 // DUT reboots... | |
289 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
290 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, | |
291 &mock_view_, fake_controller_)); | |
292 second_update_screen_->StartNetworkCheck(); | |
293 | |
294 // Verify that the DUT checks for updates again. | |
295 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
296 | |
297 // No update available this time. | |
298 SimulateUpdateAvailable(second_update_screen_, false /* available */, | |
299 false /* critical */); | |
300 } | |
301 | |
302 } // namespace chromeos | |
OLD | NEW |