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" is a unique pointer to the currently | |
48 // active UpdateScreen. | |
49 // The parameter "available" indicates whether an update is available. | |
50 // The parameter "critical" indicates whether that update is critical. | |
51 void SimulateUpdateAvailable( | |
52 const std::unique_ptr<UpdateScreen>& update_screen, | |
xiyuan
2017/05/15 22:30:20
We can pass UpdateScreen* as long as we can guaran
kumarniranjan
2017/05/15 23:21:53
Done.
| |
53 bool available, | |
54 bool critical) { | |
55 update_engine_status_.status = | |
56 UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE; | |
57 fake_update_engine_client_->NotifyObserversThatStatusChanged( | |
58 update_engine_status_); | |
59 if (critical) { | |
60 ASSERT_TRUE(available) << "Does not make sense for an update to be " | |
61 "critical if one is not even available."; | |
62 update_screen->is_ignore_update_deadlines_ = true; | |
63 } | |
64 update_engine_status_.status = | |
65 available ? UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE | |
66 : UpdateEngineClient::UPDATE_STATUS_IDLE; | |
67 fake_update_engine_client_->NotifyObserversThatStatusChanged( | |
68 update_engine_status_); | |
69 } | |
70 | |
71 // testing::Test: | |
72 void SetUp() override { | |
73 // Configure the browser to use Hands-Off Enrollment. | |
74 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
75 switches::kEnterpriseEnableZeroTouchEnrollment, "hands-off"); | |
76 | |
77 // Initialize objects needed by UpdateScreen | |
78 fake_update_engine_client_ = new FakeUpdateEngineClient(); | |
79 DBusThreadManager::GetSetterForTesting()->SetUpdateEngineClient( | |
80 std::unique_ptr<UpdateEngineClient>(fake_update_engine_client_)); | |
81 NetworkHandler::Initialize(); | |
82 mock_network_portal_detector_ = new MockNetworkPortalDetector(); | |
83 network_portal_detector::InitializeForTesting( | |
84 mock_network_portal_detector_); | |
85 mock_error_screen_.reset( | |
86 new MockErrorScreen(&mock_base_screen_delegate_, &mock_error_view_)); | |
87 fake_controller_ = new pairing_chromeos::FakeHostPairingController(""); | |
88 | |
89 // Ensure proper behavior of UpdateScreen's supporting objects. | |
90 EXPECT_CALL(*mock_network_portal_detector_, IsEnabled()) | |
91 .Times(AnyNumber()) | |
92 .WillRepeatedly(Return(false)); | |
93 EXPECT_CALL(mock_base_screen_delegate_, GetErrorScreen()) | |
94 .Times(AnyNumber()) | |
95 .WillRepeatedly(Return(mock_error_screen_.get())); | |
96 | |
97 // Later verifies that UpdateScreen successfully exits both times. | |
98 EXPECT_CALL(mock_base_screen_delegate_, | |
99 OnExit(_, ScreenExitCode::UPDATE_NOUPDATE, _)) | |
100 .Times(2); | |
101 } | |
102 | |
103 void TearDown() override { | |
104 TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); | |
105 network_portal_detector::Shutdown(); | |
106 NetworkHandler::Shutdown(); | |
107 DBusThreadManager::Shutdown(); | |
108 } | |
109 | |
110 protected: | |
111 // Accessory objects needed by UpdateScreen. | |
112 MockBaseScreenDelegate mock_base_screen_delegate_; | |
113 MockUpdateView mock_view_; | |
114 MockNetworkErrorView mock_error_view_; | |
115 UpdateEngineClient::Status update_engine_status_; | |
116 std::unique_ptr<MockErrorScreen> mock_error_screen_; | |
117 pairing_chromeos::FakeHostPairingController* fake_controller_; | |
118 MockNetworkPortalDetector* mock_network_portal_detector_; | |
119 FakeUpdateEngineClient* fake_update_engine_client_; | |
120 | |
121 private: | |
122 // Test versions of core browser infrastructure. | |
123 content::TestBrowserThreadBundle threads_; | |
124 ScopedTestingLocalState local_state_; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(UpdateScreenUnitTest); | |
127 }; | |
128 | |
129 // Test Scenario Description: | |
130 // In this description, "will" refers to an external event, and "should" refers | |
131 // to the expected behavior of the DUT in response to external events. | |
132 // | |
133 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
134 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
135 // it checks for updates. It will find that there is indeed an update | |
136 // available, will then install it. After installing the update, it should | |
137 // continue with Hands-Off OOBE. Then, before OOBE is complete, something | |
138 // (could be user, environment, anything) will cause the DUT to reboot. | |
139 // Since OOBE is not complete, the DUT goes through OOBE again. | |
140 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
141 // it should check for updates again. | |
142 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfFirstIsInstalled) { | |
143 // DUT reaches UpdateScreen for the first time. | |
144 std::unique_ptr<UpdateScreen> first_update_screen(new UpdateScreen( | |
145 &mock_base_screen_delegate_, &mock_view_, fake_controller_)); | |
xiyuan
2017/05/15 22:30:20
nit: std::unique_ptr<UpdateScreen> first_update_sc
kumarniranjan
2017/05/15 23:21:53
True. I changed it to use var on stack
| |
146 first_update_screen->StartNetworkCheck(); | |
147 | |
148 // Verify that the DUT checks for an update. | |
149 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
150 | |
151 // An update is available. | |
152 SimulateUpdateAvailable(first_update_screen, true, false); | |
xiyuan
2017/05/15 22:30:20
nit: document the booleans
i.e.
SimulateUpdateA
kumarniranjan
2017/05/15 23:21:53
Done.
| |
153 | |
154 // DUT reboots... | |
155 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
156 std::unique_ptr<UpdateScreen> second_update_screen(new UpdateScreen( | |
157 &mock_base_screen_delegate_, &mock_view_, fake_controller_)); | |
158 second_update_screen->StartNetworkCheck(); | |
159 | |
160 // Verify that the DUT checks for updates again. | |
161 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
162 | |
163 // No updates available this time. | |
164 SimulateUpdateAvailable(second_update_screen, false, false); | |
165 } | |
166 | |
167 // Test Scenario Description: | |
168 // In this description, "will" refers to an external event, and "should" refers | |
169 // to the expected behavior of the DUT in response to external events. | |
170 // | |
171 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
172 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
173 // it checks for updates. It will find that there are no updates | |
174 // available, and it should leave the UpdateScreen without installing any | |
175 // updates. It continues with OOBE. Then, before OOBE is complete, something | |
176 // (could be user, environment, anything) will cause the DUT to reboot. | |
177 // Since OOBE is not complete, the DUT goes through OOBE again. | |
178 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
179 // more than one hour will have passed since the previous update check. | |
180 // Therefore, the DUT should check for updates again. | |
181 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfEnoughTimePasses) { | |
182 // DUT reaches UpdateScreen for the first time. | |
183 std::unique_ptr<UpdateScreen> first_update_screen(new UpdateScreen( | |
184 &mock_base_screen_delegate_, &mock_view_, fake_controller_)); | |
185 first_update_screen->StartNetworkCheck(); | |
186 | |
187 // Verify that the DUT checks for updates. | |
188 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
189 | |
190 // No updates are available. | |
191 SimulateUpdateAvailable(first_update_screen, false, false); | |
192 | |
193 // Fast-forward time by one hour. | |
194 FastForwardTime(base::TimeDelta::FromHours(1)); | |
195 | |
196 // DUT reboots... | |
197 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
198 std::unique_ptr<UpdateScreen> second_update_screen(new UpdateScreen( | |
199 &mock_base_screen_delegate_, &mock_view_, fake_controller_)); | |
200 second_update_screen->StartNetworkCheck(); | |
201 | |
202 // Verify that the DUT checks for updates again. | |
203 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
204 | |
205 // No updates available this time either. | |
206 SimulateUpdateAvailable(second_update_screen, false, false); | |
207 } | |
208 | |
209 // Test Scenario Description: | |
210 // In this description, "will" refers to an external event, and "should" refers | |
211 // to the expected behavior of the DUT in response to external events. | |
212 // | |
213 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
214 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
215 // it checks for updates. It will find that there are no updates | |
216 // available, and it should leave the UpdateScreen without installing any | |
217 // updates. It continues with OOBE. Then, before OOBE is complete, something | |
218 // (could be user, environment, anything) will cause the DUT to reboot. | |
219 // Since OOBE is not complete, the DUT goes through OOBE again. | |
220 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
221 // less than one hour will have passed since the previous update check. | |
222 // Therefore, the DUT should not check for updates again. | |
223 TEST_F(UpdateScreenUnitTest, ChecksForUpdateOnceButNotAgainIfTooSoon) { | |
224 // DUT reaches UpdateScreen for the first time. | |
225 std::unique_ptr<UpdateScreen> first_update_screen(new UpdateScreen( | |
226 &mock_base_screen_delegate_, &mock_view_, fake_controller_)); | |
227 first_update_screen->StartNetworkCheck(); | |
228 | |
229 // Verify that the DUT checks for updates. | |
230 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
231 | |
232 // No update available. | |
233 SimulateUpdateAvailable(first_update_screen, false, false); | |
234 | |
235 // DUT reboots... | |
236 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
237 std::unique_ptr<UpdateScreen> second_update_screen(new UpdateScreen( | |
238 &mock_base_screen_delegate_, &mock_view_, fake_controller_)); | |
239 second_update_screen->StartNetworkCheck(); | |
240 | |
241 // Verify that the DUT did not check for updates again. | |
242 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
243 | |
244 // No update available this time either. | |
245 SimulateUpdateAvailable(second_update_screen, false, false); | |
246 } | |
247 | |
248 // Test Scenario Description: | |
249 // In this description, "will" refers to an external event, and "should" refers | |
250 // to the expected behavior of the DUT in response to external events. | |
251 // | |
252 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it | |
253 // proceeds through OOBE automatically. When it hits the UpdateScreen, | |
254 // it checks for updates. It will find that a critical update is available. | |
255 // The DUT installs the update, and because the update is critical, it reboots. | |
256 // Since OOBE is not complete, the DUT goes through OOBE again after reboot. | |
257 // When the DUT hits the UpdateScreen during this second OOBE run-through, | |
258 // it should check for updates again. | |
259 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfCriticalUpdate) { | |
260 // DUT reaches UpdateScreen for the first time. | |
261 std::unique_ptr<UpdateScreen> first_update_screen(new UpdateScreen( | |
262 &mock_base_screen_delegate_, &mock_view_, fake_controller_)); | |
263 first_update_screen->StartNetworkCheck(); | |
264 | |
265 // Verify that the DUT checks for updates. | |
266 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); | |
267 | |
268 // An update is available, and it's critical! | |
269 SimulateUpdateAvailable(first_update_screen, true, true); | |
270 | |
271 // DUT reboots... | |
272 // After rebooting, the DUT reaches UpdateScreen for the second time. | |
273 std::unique_ptr<UpdateScreen> second_update_screen(new UpdateScreen( | |
274 &mock_base_screen_delegate_, &mock_view_, fake_controller_)); | |
275 second_update_screen->StartNetworkCheck(); | |
276 | |
277 // Verify that the DUT checks for updates again. | |
278 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); | |
279 | |
280 // No update available this time. | |
281 SimulateUpdateAvailable(second_update_screen, false, false); | |
282 } | |
283 | |
284 } // namespace chromeos | |
OLD | NEW |