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() |
| 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 |
| 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 first_update_screen_.reset(); |
| 106 second_update_screen_.reset(); |
| 107 mock_error_screen_.reset(); |
| 108 network_portal_detector::Shutdown(); |
| 109 NetworkHandler::Shutdown(); |
| 110 DBusThreadManager::Shutdown(); |
| 111 } |
| 112 |
| 113 protected: |
| 114 // A pointer to the UpdateScreen that shows up during the first OOBE. |
| 115 std::unique_ptr<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 std::unique_ptr<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 pairing_chromeos::FakeHostPairingController fake_controller_; |
| 128 std::unique_ptr<MockErrorScreen> mock_error_screen_; |
| 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 ScopedTestingLocalState local_state_; |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(UpdateScreenUnitTest); |
| 138 }; |
| 139 |
| 140 // Test Scenario Description: |
| 141 // In this description, "will" refers to an external event, and "should" refers |
| 142 // to the expected behavior of the DUT in response to external events. |
| 143 // |
| 144 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it |
| 145 // proceeds through OOBE automatically. When it hits the UpdateScreen, |
| 146 // it checks for updates. It will find that there is indeed an update |
| 147 // available, will then install it. After installing the update, it should |
| 148 // continue with Hands-Off OOBE. Then, before OOBE is complete, something |
| 149 // (could be user, environment, anything) will cause the DUT to reboot. |
| 150 // Since OOBE is not complete, the DUT goes through OOBE again. |
| 151 // When the DUT hits the UpdateScreen during this second OOBE run-through, |
| 152 // it should check for updates again. |
| 153 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfFirstIsInstalled) { |
| 154 // DUT reaches UpdateScreen for the first time. |
| 155 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, |
| 156 &mock_view_, &fake_controller_)); |
| 157 first_update_screen_->StartNetworkCheck(); |
| 158 |
| 159 // Verify that the DUT checks for an update. |
| 160 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); |
| 161 |
| 162 // An update is available. |
| 163 SimulateUpdateAvailable(first_update_screen_, true /* available */, |
| 164 false /* critical */); |
| 165 |
| 166 // DUT reboots... |
| 167 // After rebooting, the DUT reaches UpdateScreen for the second time. |
| 168 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, |
| 169 &mock_view_, &fake_controller_)); |
| 170 second_update_screen_->StartNetworkCheck(); |
| 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(second_update_screen_, false /* available */, |
| 177 false /* critical */); |
| 178 } |
| 179 |
| 180 // Test Scenario Description: |
| 181 // In this description, "will" refers to an external event, and "should" refers |
| 182 // to the expected behavior of the DUT in response to external events. |
| 183 // |
| 184 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it |
| 185 // proceeds through OOBE automatically. When it hits the UpdateScreen, |
| 186 // it checks for updates. It will find that there are no updates |
| 187 // available, and it should leave the UpdateScreen without installing any |
| 188 // updates. It continues with OOBE. Then, before OOBE is complete, something |
| 189 // (could be user, environment, anything) will cause the DUT to reboot. |
| 190 // Since OOBE is not complete, the DUT goes through OOBE again. |
| 191 // When the DUT hits the UpdateScreen during this second OOBE run-through, |
| 192 // more than one hour will have passed since the previous update check. |
| 193 // Therefore, the DUT should check for updates again. |
| 194 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfEnoughTimePasses) { |
| 195 // DUT reaches UpdateScreen for the first time. |
| 196 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, |
| 197 &mock_view_, &fake_controller_)); |
| 198 first_update_screen_->StartNetworkCheck(); |
| 199 |
| 200 // Verify that the DUT checks for updates. |
| 201 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); |
| 202 |
| 203 // No updates are available. |
| 204 SimulateUpdateAvailable(first_update_screen_, false /* available */, |
| 205 false /* critical */); |
| 206 |
| 207 // Fast-forward time by one hour. |
| 208 FastForwardTime(base::TimeDelta::FromHours(1)); |
| 209 |
| 210 // DUT reboots... |
| 211 // After rebooting, the DUT reaches UpdateScreen for the second time. |
| 212 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, |
| 213 &mock_view_, &fake_controller_)); |
| 214 second_update_screen_->StartNetworkCheck(); |
| 215 |
| 216 // Verify that the DUT checks for updates again. |
| 217 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); |
| 218 |
| 219 // No updates available this time either. |
| 220 SimulateUpdateAvailable(second_update_screen_, false /* available */, |
| 221 false /* critical */); |
| 222 } |
| 223 |
| 224 // Test Scenario Description: |
| 225 // In this description, "will" refers to an external event, and "should" refers |
| 226 // to the expected behavior of the DUT in response to external events. |
| 227 // |
| 228 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it |
| 229 // proceeds through OOBE automatically. When it hits the UpdateScreen, |
| 230 // it checks for updates. It will find that there are no updates |
| 231 // available, and it should leave the UpdateScreen without installing any |
| 232 // updates. It continues with OOBE. Then, before OOBE is complete, something |
| 233 // (could be user, environment, anything) will cause the DUT to reboot. |
| 234 // Since OOBE is not complete, the DUT goes through OOBE again. |
| 235 // When the DUT hits the UpdateScreen during this second OOBE run-through, |
| 236 // less than one hour will have passed since the previous update check. |
| 237 // Therefore, the DUT should not check for updates again. |
| 238 TEST_F(UpdateScreenUnitTest, ChecksForUpdateOnceButNotAgainIfTooSoon) { |
| 239 // DUT reaches UpdateScreen for the first time. |
| 240 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, |
| 241 &mock_view_, &fake_controller_)); |
| 242 first_update_screen_->StartNetworkCheck(); |
| 243 |
| 244 // Verify that the DUT checks for updates. |
| 245 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); |
| 246 |
| 247 // No update available. |
| 248 SimulateUpdateAvailable(first_update_screen_, false /* available */, |
| 249 false /* critical */); |
| 250 |
| 251 // DUT reboots... |
| 252 // After rebooting, the DUT reaches UpdateScreen for the second time. |
| 253 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, |
| 254 &mock_view_, &fake_controller_)); |
| 255 second_update_screen_->StartNetworkCheck(); |
| 256 |
| 257 // Verify that the DUT did not check for updates again. |
| 258 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); |
| 259 |
| 260 // No update available this time either. |
| 261 SimulateUpdateAvailable(second_update_screen_, false /* available */, |
| 262 false /* critical */); |
| 263 } |
| 264 |
| 265 // Test Scenario Description: |
| 266 // In this description, "will" refers to an external event, and "should" refers |
| 267 // to the expected behavior of the DUT in response to external events. |
| 268 // |
| 269 // The DUT boots up and starts OOBE. Since it is a Hands-Off device, it |
| 270 // proceeds through OOBE automatically. When it hits the UpdateScreen, |
| 271 // it checks for updates. It will find that a critical update is available. |
| 272 // The DUT installs the update, and because the update is critical, it reboots. |
| 273 // Since OOBE is not complete, the DUT goes through OOBE again after reboot. |
| 274 // When the DUT hits the UpdateScreen during this second OOBE run-through, |
| 275 // it should check for updates again. |
| 276 TEST_F(UpdateScreenUnitTest, ChecksForUpdateBothTimesIfCriticalUpdate) { |
| 277 // DUT reaches UpdateScreen for the first time. |
| 278 first_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, |
| 279 &mock_view_, &fake_controller_)); |
| 280 first_update_screen_->StartNetworkCheck(); |
| 281 |
| 282 // Verify that the DUT checks for updates. |
| 283 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 1); |
| 284 |
| 285 // An update is available, and it's critical! |
| 286 SimulateUpdateAvailable(first_update_screen_, true /* available */, |
| 287 true /* critical */); |
| 288 |
| 289 // DUT reboots... |
| 290 // After rebooting, the DUT reaches UpdateScreen for the second time. |
| 291 second_update_screen_.reset(new UpdateScreen(&mock_base_screen_delegate_, |
| 292 &mock_view_, &fake_controller_)); |
| 293 second_update_screen_->StartNetworkCheck(); |
| 294 |
| 295 // Verify that the DUT checks for updates again. |
| 296 EXPECT_EQ(fake_update_engine_client_->request_update_check_call_count(), 2); |
| 297 |
| 298 // No update available this time. |
| 299 SimulateUpdateAvailable(second_update_screen_, false /* available */, |
| 300 false /* critical */); |
| 301 } |
| 302 |
| 303 } // namespace chromeos |
OLD | NEW |