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

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

Issue 2894783003: Make Hands-Off Zero-Touch Enrollment compatibile with tests (Closed)
Patch Set: 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()
37 : fake_controller_(""),
38 local_state_(TestingBrowserProcess::GetGlobal()) {}
39
40 // Fast-forwards time by the specified amount.
41 void FastForwardTime(base::TimeDelta time) {
42 base::Time last = StartupUtils::GetTimeOfLastUpdateCheckWithoutUpdate();
43 ASSERT_FALSE(last.is_null());
44 base::Time modified_last = last - time;
45 StartupUtils::SaveTimeOfLastUpdateCheckWithoutUpdate(modified_last);
46 }
47
48 // Simulates an update being available (or not).
49 // The parameter "update_screen" points to the currently active UpdateScreen.
50 // The parameter "available" indicates whether an update is available.
51 // The parameter "critical" indicates whether that update is critical.
52 void SimulateUpdateAvailable(
53 const std::unique_ptr<UpdateScreen>& update_screen,
54 bool available,
55 bool critical) {
56 update_engine_status_.status =
57 UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE;
58 fake_update_engine_client_->NotifyObserversThatStatusChanged(
59 update_engine_status_);
60 if (critical) {
61 ASSERT_TRUE(available) << "Does not make sense for an update to be "
62 "critical if one is not even available.";
63 update_screen->is_ignore_update_deadlines_ = true;
64 }
65 update_engine_status_.status =
66 available ? UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE
67 : UpdateEngineClient::UPDATE_STATUS_IDLE;
68 fake_update_engine_client_->NotifyObserversThatStatusChanged(
69 update_engine_status_);
70 }
71
72 // testing::Test:
73 void SetUp() override {
74 // Configure the browser to use Hands-Off Enrollment.
75 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
76 switches::kEnterpriseEnableZeroTouchEnrollment, "hands-off");
77
78 // Initialize objects needed by UpdateScreen
79 fake_update_engine_client_ = new FakeUpdateEngineClient();
80 DBusThreadManager::GetSetterForTesting()->SetUpdateEngineClient(
81 std::unique_ptr<UpdateEngineClient>(fake_update_engine_client_));
82 NetworkHandler::Initialize();
83 mock_network_portal_detector_ = new MockNetworkPortalDetector();
84 network_portal_detector::SetNetworkPortalDetector(
85 mock_network_portal_detector_);
86 mock_error_screen_.reset(
87 new MockErrorScreen(&mock_base_screen_delegate_, &mock_error_view_));
88 // fake_controller_ = new pairing_chromeos::FakeHostPairingController("");
xiyuan 2017/05/19 17:48:09 Remove if it is not needed.
kumarniranjan 2017/05/19 18:03:02 Done.
89
90 // Ensure proper behavior of UpdateScreen's supporting objects.
91 EXPECT_CALL(*mock_network_portal_detector_, IsEnabled())
92 .Times(AnyNumber())
93 .WillRepeatedly(Return(false));
94 EXPECT_CALL(mock_base_screen_delegate_, GetErrorScreen())
95 .Times(AnyNumber())
96 .WillRepeatedly(Return(mock_error_screen_.get()));
97
98 // Later verifies that UpdateScreen successfully exits both times.
99 EXPECT_CALL(mock_base_screen_delegate_,
100 OnExit(_, ScreenExitCode::UPDATE_NOUPDATE, _))
101 .Times(2);
102 }
103
104 void TearDown() override {
105 TestingBrowserProcess::GetGlobal()->SetShuttingDown(true);
106 first_update_screen_.reset();
107 second_update_screen_.reset();
108 mock_error_screen_.reset();
109 network_portal_detector::Shutdown();
110 NetworkHandler::Shutdown();
111 DBusThreadManager::Shutdown();
112 }
113
114 protected:
115 // A pointer to the UpdateScreen that shows up during the first OOBE.
116 std::unique_ptr<UpdateScreen> first_update_screen_;
117
118 // A pointer to the UpdateScreen which shows up during the second OOBE.
119 // This test verifies proper behavior if the device is restarted before
120 // OOBE is complete, which is why there is a second OOBE.
121 std::unique_ptr<UpdateScreen> second_update_screen_;
122
123 // Accessory objects needed by UpdateScreen.
124 MockBaseScreenDelegate mock_base_screen_delegate_;
125 MockUpdateView mock_view_;
126 MockNetworkErrorView mock_error_view_;
127 UpdateEngineClient::Status update_engine_status_;
128 pairing_chromeos::FakeHostPairingController fake_controller_;
129 std::unique_ptr<MockErrorScreen> mock_error_screen_;
130 MockNetworkPortalDetector* mock_network_portal_detector_;
131 FakeUpdateEngineClient* fake_update_engine_client_;
132
133 private:
134 // Test versions of core browser infrastructure.
135 content::TestBrowserThreadBundle threads_;
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_.reset(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(first_update_screen_, true /* available */,
165 false /* critical */);
166
167 // DUT reboots...
168 // After rebooting, the DUT reaches UpdateScreen for the second time.
169 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_,
170 &mock_view_, &fake_controller_));
171 second_update_screen_->StartNetworkCheck();
172
173 // Verify that the DUT checks for updates again.
174 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2);
175
176 // No updates available this time.
177 SimulateUpdateAvailable(second_update_screen_, false /* available */,
178 false /* critical */);
179 }
180
181 // Test Scenario Description:
182 // In this description, "will" refers to an external event, and "should" refers
183 // to the expected behavior of the DUT in response to external events.
184 //
185 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it
186 // proceeds through OOBE automatically. When it hits the UpdateScreen,
187 // it checks for updates. It will find that there are no updates
188 // available, and it should leave the UpdateScreen without installing any
189 // updates. It continues with OOBE. Then, before OOBE is complete, something
190 // (could be user, environment, anything) will cause the DUT to reboot.
191 // Since OOBE is not complete, the DUT goes through OOBE again.
192 // When the DUT hits the UpdateScreen during this second OOBE run-through,
193 // more than one hour will have passed since the previous update check.
194 // Therefore, the DUT should check for updates again.
195 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfEnoughTimePasses) {
196 // DUT reaches UpdateScreen for the first time.
197 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_,
198 &mock_view_, &fake_controller_));
199 first_update_screen_->StartNetworkCheck();
200
201 // Verify that the DUT checks for updates.
202 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1);
203
204 // No updates are available.
205 SimulateUpdateAvailable(first_update_screen_, false /* available */,
206 false /* critical */);
207
208 // Fast-forward time by one hour.
209 FastForwardTime(base::TimeDelta::FromHours(1));
210
211 // DUT reboots...
212 // After rebooting, the DUT reaches UpdateScreen for the second time.
213 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_,
214 &mock_view_, &fake_controller_));
215 second_update_screen_->StartNetworkCheck();
216
217 // Verify that the DUT checks for updates again.
218 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2);
219
220 // No updates available this time either.
221 SimulateUpdateAvailable(second_update_screen_, false /* available */,
222 false /* critical */);
223 }
224
225 // Test Scenario Description:
226 // In this description, "will" refers to an external event, and "should" refers
227 // to the expected behavior of the DUT in response to external events.
228 //
229 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it
230 // proceeds through OOBE automatically. When it hits the UpdateScreen,
231 // it checks for updates. It will find that there are no updates
232 // available, and it should leave the UpdateScreen without installing any
233 // updates. It continues with OOBE. Then, before OOBE is complete, something
234 // (could be user, environment, anything) will cause the DUT to reboot.
235 // Since OOBE is not complete, the DUT goes through OOBE again.
236 // When the DUT hits the UpdateScreen during this second OOBE run-through,
237 // less than one hour will have passed since the previous update check.
238 // Therefore, the DUT should not check for updates again.
239 TEST_F(UpdateScreenUnitTest, ChecksForUpdateOnceButNotAgainIfTooSoon) {
240 // DUT reaches UpdateScreen for the first time.
241 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_,
242 &mock_view_, &fake_controller_));
243 first_update_screen_->StartNetworkCheck();
244
245 // Verify that the DUT checks for updates.
246 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1);
247
248 // No update available.
249 SimulateUpdateAvailable(first_update_screen_, false /* available */,
250 false /* critical */);
251
252 // DUT reboots...
253 // After rebooting, the DUT reaches UpdateScreen for the second time.
254 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_,
255 &mock_view_, &fake_controller_));
256 second_update_screen_->StartNetworkCheck();
257
258 // Verify that the DUT did not check for updates again.
259 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1);
260
261 // No update available this time either.
262 SimulateUpdateAvailable(second_update_screen_, false /* available */,
263 false /* critical */);
264 }
265
266 // Test Scenario Description:
267 // In this description, "will" refers to an external event, and "should" refers
268 // to the expected behavior of the DUT in response to external events.
269 //
270 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it
271 // proceeds through OOBE automatically. When it hits the UpdateScreen,
272 // it checks for updates. It will find that a critical update is available.
273 // The DUT installs the update, and because the update is critical, it reboots.
274 // Since OOBE is not complete, the DUT goes through OOBE again after reboot.
275 // When the DUT hits the UpdateScreen during this second OOBE run-through,
276 // it should check for updates again.
277 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfCriticalUpdate) {
278 // DUT reaches UpdateScreen for the first time.
279 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_,
280 &mock_view_, &fake_controller_));
281 first_update_screen_->StartNetworkCheck();
282
283 // Verify that the DUT checks for updates.
284 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1);
285
286 // An update is available, and it's critical!
287 SimulateUpdateAvailable(first_update_screen_, true /* available */,
288 true /* critical */);
289
290 // DUT reboots...
291 // After rebooting, the DUT reaches UpdateScreen for the second time.
292 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_,
293 &mock_view_, &fake_controller_));
294 second_update_screen_->StartNetworkCheck();
295
296 // Verify that the DUT checks for updates again.
297 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2);
298
299 // No update available this time.
300 SimulateUpdateAvailable(second_update_screen_, false /* available */,
301 false /* critical */);
302 }
303
304 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/screens/update_screen.cc ('k') | chrome/browser/chromeos/login/startup_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698