Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: chrome/browser/chromeos/login/screens/update_screen_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698