Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / system / automatic_reboot_manager_unittest.cc
1 // Copyright (c) 2013 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/system/automatic_reboot_manager.h"
6
7 #include <string>
8 #include <utility>
9
10 #include "ash/shell.h"
11 #include "ash/test/test_shell_delegate.h"
12 #include "base/compiler_specific.h"
13 #include "base/file_util.h"
14 #include "base/files/file_path.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/path_service.h"
19 #include "base/prefs/pref_registry_simple.h"
20 #include "base/prefs/testing_pref_service.h"
21 #include "base/run_loop.h"
22 #include "base/single_thread_task_runner.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/test/simple_test_tick_clock.h"
25 #include "base/thread_task_runner_handle.h"
26 #include "base/threading/sequenced_worker_pool.h"
27 #include "base/time/tick_clock.h"
28 #include "base/values.h"
29 #include "chrome/browser/chrome_notification_types.h"
30 #include "chrome/browser/chromeos/login/mock_user_manager.h"
31 #include "chrome/browser/chromeos/login/user_manager.h"
32 #include "chrome/common/pref_names.h"
33 #include "chrome/test/base/testing_browser_process.h"
34 #include "chromeos/chromeos_paths.h"
35 #include "chromeos/dbus/dbus_thread_manager.h"
36 #include "chromeos/dbus/fake_dbus_thread_manager.h"
37 #include "chromeos/dbus/fake_power_manager_client.h"
38 #include "chromeos/dbus/fake_update_engine_client.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/notification_details.h"
41 #include "content/public/browser/notification_service.h"
42 #include "content/public/browser/notification_source.h"
43 #include "content/public/test/test_browser_thread.h"
44 #include "testing/gmock/include/gmock/gmock.h"
45 #include "testing/gtest/include/gtest/gtest.h"
46 #include "ui/message_center/message_center.h"
47
48 using ::testing::ReturnPointee;
49
50 namespace chromeos {
51 namespace system {
52
53 namespace {
54
55 // A SingleThreadTaskRunner that mocks the current time and allows it to be
56 // fast-forwarded. The current time in ticks is returned by Now(). The
57 // corresponding device uptime is written to |uptime_file_|, providing a mock
58 // for /proc/uptime.
59 class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner {
60  public:
61   MockTimeSingleThreadTaskRunner();
62
63   // base::SingleThreadTaskRunner:
64   virtual bool RunsTasksOnCurrentThread() const OVERRIDE;
65   virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
66                                const base::Closure& task,
67                                base::TimeDelta delay) OVERRIDE;
68   virtual bool PostNonNestableDelayedTask(
69       const tracked_objects::Location& from_here,
70       const base::Closure& task,
71       base::TimeDelta delay) OVERRIDE;
72
73   void SetUptimeFile(const base::FilePath& uptime_file);
74   void SetUptime(const base::TimeDelta& uptime);
75
76   const base::TimeDelta& Uptime() const;
77   const base::TimeTicks& Now() const;
78
79   void FastForwardBy(const base::TimeDelta& delta);
80   void FastForwardUntilNoTasksRemain();
81   void RunUntilIdle();
82
83  private:
84   // Strict weak temporal ordering of tasks.
85   class TemporalOrder {
86    public:
87     bool operator()(
88         const std::pair<base::TimeTicks, base::Closure>& first_task,
89         const std::pair<base::TimeTicks, base::Closure>& second_task) const;
90   };
91
92   virtual ~MockTimeSingleThreadTaskRunner();
93
94   base::FilePath uptime_file_;
95   base::TimeDelta uptime_;
96   base::TimeTicks now_;
97   std::priority_queue<std::pair<base::TimeTicks, base::Closure>,
98                       std::vector<std::pair<base::TimeTicks, base::Closure> >,
99                       TemporalOrder> tasks_;
100
101   DISALLOW_COPY_AND_ASSIGN(MockTimeSingleThreadTaskRunner);
102 };
103
104 class MockTimeTickClock : public base::TickClock {
105  public:
106   explicit MockTimeTickClock(
107       scoped_refptr<MockTimeSingleThreadTaskRunner> task_runner);
108   virtual ~MockTimeTickClock();
109
110   // base::TickClock:
111   virtual base::TimeTicks NowTicks() OVERRIDE;
112
113  private:
114   scoped_refptr<MockTimeSingleThreadTaskRunner> task_runner_;
115
116   DISALLOW_COPY_AND_ASSIGN(MockTimeTickClock);
117 };
118
119 }  // namespace
120
121 class AutomaticRebootManagerBasicTest : public testing::Test {
122  protected:
123   typedef base::OneShotTimer<AutomaticRebootManager> Timer;
124
125   AutomaticRebootManagerBasicTest();
126   virtual ~AutomaticRebootManagerBasicTest();
127
128   // testing::Test:
129   virtual void SetUp() OVERRIDE;
130   virtual void TearDown() OVERRIDE;
131
132   void SetUpdateRebootNeededUptime(const base::TimeDelta& uptime);
133   void SetRebootAfterUpdate(bool reboot_after_update, bool expect_reboot);
134   void SetUptimeLimit(const base::TimeDelta& limit, bool expect_reboot);
135   void NotifyUpdateRebootNeeded();
136   void NotifyResumed(bool expect_reboot);
137   void NotifyTerminating(bool expect_reboot);
138
139   void FastForwardBy(const base::TimeDelta& delta, bool expect_reboot);
140   void FastForwardUntilNoTasksRemain(bool expect_reboot);
141
142   void CreateAutomaticRebootManager(bool expect_reboot);
143
144   bool ReadUpdateRebootNeededUptimeFromFile(base::TimeDelta* uptime);
145   void VerifyLoginScreenIdleTimerIsStopped() const;
146   void VerifyNoGracePeriod() const;
147   void VerifyGracePeriod(const base::TimeDelta& start_uptime) const;
148
149   bool is_user_logged_in_;
150   bool is_logged_in_as_kiosk_app_;
151
152   // The uptime is read in the blocking thread pool and then processed on the
153   // UI thread. This causes the UI thread to start processing the uptime when it
154   // has increased by a small offset already. The offset is calculated and
155   // stored in |uptime_processing_delay_| so that tests can accurately determine
156   // the uptime seen by the UI thread.
157   base::TimeDelta uptime_processing_delay_;
158   base::TimeDelta update_reboot_needed_uptime_;
159   base::TimeDelta uptime_limit_;
160
161   scoped_refptr<MockTimeSingleThreadTaskRunner> task_runner_;
162
163   scoped_ptr<AutomaticRebootManager> automatic_reboot_manager_;
164
165  protected:
166   FakePowerManagerClient* power_manager_client_;  // Not owned.
167   FakeUpdateEngineClient* update_engine_client_;  // Not owned.
168
169   // Sets the status of |update_engine_client_| to NEED_REBOOT for tests.
170   void SetUpdateStatusNeedReboot();
171
172  private:
173   void VerifyTimerIsStopped(const Timer* timer) const;
174   void VerifyTimerIsRunning(const Timer* timer,
175                             const base::TimeDelta& delay) const;
176   void VerifyLoginScreenIdleTimerIsRunning() const;
177
178   base::ScopedTempDir temp_dir_;
179   base::FilePath update_reboot_needed_uptime_file_;
180
181   bool reboot_after_update_;
182
183   base::ThreadTaskRunnerHandle ui_thread_task_runner_handle_;
184
185   TestingPrefServiceSimple local_state_;
186   MockUserManager* mock_user_manager_;  // Not owned.
187   ScopedUserManagerEnabler user_manager_enabler_;
188 };
189
190 enum AutomaticRebootManagerTestScenario {
191   AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_LOGIN_SCREEN,
192   AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_KIOSK_APP_SESSION,
193   AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_NON_KIOSK_APP_SESSION,
194 };
195
196 // This class runs each test case three times:
197 // * once while the login screen is being shown
198 // * once while a kiosk app session is in progress
199 // * once while a non-kiosk-app session is in progress
200 class AutomaticRebootManagerTest
201     : public AutomaticRebootManagerBasicTest,
202       public testing::WithParamInterface<AutomaticRebootManagerTestScenario> {
203  protected:
204   AutomaticRebootManagerTest();
205   virtual ~AutomaticRebootManagerTest();
206 };
207
208 void SaveUptimeToFile(const base::FilePath& path,
209                       const base::TimeDelta& uptime) {
210   if (path.empty() || uptime == base::TimeDelta())
211     return;
212
213   const std::string uptime_seconds = base::DoubleToString(uptime.InSecondsF());
214   ASSERT_EQ(static_cast<int>(uptime_seconds.size()),
215             base::WriteFile(path, uptime_seconds.c_str(),
216                             uptime_seconds.size()));
217 }
218
219 MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner() {
220 }
221
222 bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const {
223   return true;
224 }
225
226 bool MockTimeSingleThreadTaskRunner::PostDelayedTask(
227     const tracked_objects::Location& from_here,
228     const base::Closure& task,
229     base::TimeDelta delay) {
230   tasks_.push(std::pair<base::TimeTicks, base::Closure>(now_ + delay, task));
231   return true;
232 }
233
234 bool MockTimeSingleThreadTaskRunner::PostNonNestableDelayedTask(
235     const tracked_objects::Location& from_here,
236     const base::Closure& task,
237     base::TimeDelta delay) {
238   NOTREACHED();
239   return false;
240 }
241
242 void MockTimeSingleThreadTaskRunner::SetUptimeFile(
243     const base::FilePath& uptime_file) {
244   uptime_file_ = uptime_file;
245   SaveUptimeToFile(uptime_file_, uptime_);
246 }
247
248 void MockTimeSingleThreadTaskRunner::SetUptime(const base::TimeDelta& uptime) {
249   uptime_ = uptime;
250   SaveUptimeToFile(uptime_file_, uptime_);
251 }
252
253 const base::TimeDelta& MockTimeSingleThreadTaskRunner::Uptime() const {
254   return uptime_;
255 }
256
257 const base::TimeTicks& MockTimeSingleThreadTaskRunner::Now() const {
258   return now_;
259 }
260
261 void MockTimeSingleThreadTaskRunner::FastForwardBy(
262     const base::TimeDelta& delta) {
263   const base::TimeTicks latest = now_ + delta;
264   base::SequencedWorkerPool* blocking_pool =
265       content::BrowserThread::GetBlockingPool();
266   blocking_pool->FlushForTesting();
267   while (!tasks_.empty() && tasks_.top().first <= latest) {
268     uptime_ += tasks_.top().first - now_;
269     SaveUptimeToFile(uptime_file_, uptime_);
270     now_ = tasks_.top().first;
271     base::Closure task = tasks_.top().second;
272     tasks_.pop();
273     task.Run();
274     blocking_pool->FlushForTesting();
275   }
276   uptime_ += latest - now_;
277   SaveUptimeToFile(uptime_file_, uptime_);
278   now_ = latest;
279 }
280
281 void MockTimeSingleThreadTaskRunner::FastForwardUntilNoTasksRemain() {
282   base::SequencedWorkerPool* blocking_pool =
283       content::BrowserThread::GetBlockingPool();
284   blocking_pool->FlushForTesting();
285   while (!tasks_.empty()) {
286     uptime_ += tasks_.top().first - now_;
287     SaveUptimeToFile(uptime_file_, uptime_);
288     now_ = tasks_.top().first;
289     base::Closure task = tasks_.top().second;
290     tasks_.pop();
291     task.Run();
292     blocking_pool->FlushForTesting();
293   }
294 }
295
296 void MockTimeSingleThreadTaskRunner::RunUntilIdle() {
297   base::SequencedWorkerPool* blocking_pool =
298       content::BrowserThread::GetBlockingPool();
299   blocking_pool->FlushForTesting();
300   while (!tasks_.empty() && tasks_.top().first <= now_) {
301     base::Closure task = tasks_.top().second;
302     tasks_.pop();
303     task.Run();
304     blocking_pool->FlushForTesting();
305   }
306 }
307
308 bool MockTimeSingleThreadTaskRunner::TemporalOrder::operator()(
309     const std::pair<base::TimeTicks, base::Closure>& first_task,
310     const std::pair<base::TimeTicks, base::Closure>& second_task) const {
311   return first_task.first > second_task.first;
312 }
313
314 MockTimeSingleThreadTaskRunner::~MockTimeSingleThreadTaskRunner() {
315 }
316
317 MockTimeTickClock::MockTimeTickClock(
318     scoped_refptr<MockTimeSingleThreadTaskRunner> task_runner)
319     : task_runner_(task_runner) {
320 }
321
322 MockTimeTickClock::~MockTimeTickClock() {
323 }
324
325 base::TimeTicks MockTimeTickClock::NowTicks() {
326   return task_runner_->Now();
327 }
328
329 AutomaticRebootManagerBasicTest::AutomaticRebootManagerBasicTest()
330     : is_user_logged_in_(false),
331       is_logged_in_as_kiosk_app_(false),
332       task_runner_(new MockTimeSingleThreadTaskRunner),
333       power_manager_client_(NULL),
334       update_engine_client_(NULL),
335       reboot_after_update_(false),
336       ui_thread_task_runner_handle_(task_runner_),
337       mock_user_manager_(new MockUserManager),
338       user_manager_enabler_(mock_user_manager_) {
339 }
340
341 AutomaticRebootManagerBasicTest::~AutomaticRebootManagerBasicTest() {
342 }
343
344 void AutomaticRebootManagerBasicTest::SetUp() {
345   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
346   const base::FilePath& temp_dir = temp_dir_.path();
347   const base::FilePath uptime_file = temp_dir.Append("uptime");
348   task_runner_->SetUptimeFile(uptime_file);
349   ASSERT_FALSE(base::WriteFile(uptime_file, NULL, 0));
350   update_reboot_needed_uptime_file_ =
351       temp_dir.Append("update_reboot_needed_uptime");
352   ASSERT_FALSE(base::WriteFile(update_reboot_needed_uptime_file_, NULL, 0));
353   ASSERT_TRUE(PathService::Override(chromeos::FILE_UPTIME, uptime_file));
354   ASSERT_TRUE(PathService::Override(chromeos::FILE_UPDATE_REBOOT_NEEDED_UPTIME,
355                                     update_reboot_needed_uptime_file_));
356
357   TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
358   AutomaticRebootManager::RegisterPrefs(local_state_.registry());
359
360   FakeDBusThreadManager* dbus_manager = new FakeDBusThreadManager;
361   power_manager_client_ = new FakePowerManagerClient;
362   dbus_manager->SetPowerManagerClient(
363       scoped_ptr<PowerManagerClient>(power_manager_client_));
364   update_engine_client_ = new FakeUpdateEngineClient;
365   dbus_manager->SetUpdateEngineClient(
366       scoped_ptr<UpdateEngineClient>(update_engine_client_));
367   DBusThreadManager::InitializeForTesting(dbus_manager);
368
369   EXPECT_CALL(*mock_user_manager_, IsUserLoggedIn())
370      .WillRepeatedly(ReturnPointee(&is_user_logged_in_));
371   EXPECT_CALL(*mock_user_manager_, IsLoggedInAsKioskApp())
372      .WillRepeatedly(ReturnPointee(&is_logged_in_as_kiosk_app_));
373 }
374
375 void AutomaticRebootManagerBasicTest::TearDown() {
376   // Let the AutomaticRebootManager, if any, unregister itself as an observer of
377   // several subsystems.
378   automatic_reboot_manager_.reset();
379   task_runner_->RunUntilIdle();
380
381   DBusThreadManager::Shutdown();
382   TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
383 }
384
385 void AutomaticRebootManagerBasicTest::SetUpdateRebootNeededUptime(
386     const base::TimeDelta& uptime) {
387   update_reboot_needed_uptime_ = uptime;
388   SaveUptimeToFile(update_reboot_needed_uptime_file_, uptime);
389 }
390
391
392 void AutomaticRebootManagerBasicTest::SetRebootAfterUpdate(
393     bool reboot_after_update,
394     bool expect_reboot) {
395   reboot_after_update_ = reboot_after_update;
396   local_state_.SetManagedPref(
397       prefs::kRebootAfterUpdate,
398       base::Value::CreateBooleanValue(reboot_after_update));
399   task_runner_->RunUntilIdle();
400   EXPECT_EQ(expect_reboot ? 1 : 0,
401             power_manager_client_->num_request_restart_calls());
402 }
403
404 void AutomaticRebootManagerBasicTest::SetUptimeLimit(
405     const base::TimeDelta& limit,
406     bool expect_reboot) {
407   uptime_limit_ = limit;
408   if (limit == base::TimeDelta()) {
409     local_state_.RemoveManagedPref(prefs::kUptimeLimit);
410   } else {
411     local_state_.SetManagedPref(
412         prefs::kUptimeLimit,
413         base::Value::CreateIntegerValue(limit.InSeconds()));
414   }
415   task_runner_->RunUntilIdle();
416   EXPECT_EQ(expect_reboot ? 1 : 0,
417             power_manager_client_->num_request_restart_calls());
418 }
419
420 void AutomaticRebootManagerBasicTest::NotifyUpdateRebootNeeded() {
421   SetUpdateStatusNeedReboot();
422   automatic_reboot_manager_->UpdateStatusChanged(
423       update_engine_client_->GetLastStatus());
424   task_runner_->RunUntilIdle();
425   EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
426 }
427
428 void AutomaticRebootManagerBasicTest::NotifyResumed(bool expect_reboot) {
429   automatic_reboot_manager_->SuspendDone(base::TimeDelta::FromHours(1));
430   task_runner_->RunUntilIdle();
431   EXPECT_EQ(expect_reboot ? 1 : 0,
432             power_manager_client_->num_request_restart_calls());
433 }
434
435 void AutomaticRebootManagerBasicTest::NotifyTerminating(bool expect_reboot) {
436   automatic_reboot_manager_->Observe(
437       chrome::NOTIFICATION_APP_TERMINATING,
438       content::Source<AutomaticRebootManagerBasicTest>(this),
439       content::NotificationService::NoDetails());
440   task_runner_->RunUntilIdle();
441   EXPECT_EQ(expect_reboot ? 1 : 0,
442             power_manager_client_->num_request_restart_calls());
443 }
444
445 void AutomaticRebootManagerBasicTest::FastForwardBy(
446     const base::TimeDelta& delta,
447     bool expect_reboot) {
448   task_runner_->FastForwardBy(delta);
449   EXPECT_EQ(expect_reboot ? 1 : 0,
450             power_manager_client_->num_request_restart_calls());
451 }
452
453 void AutomaticRebootManagerBasicTest::FastForwardUntilNoTasksRemain(
454     bool expect_reboot) {
455   task_runner_->FastForwardUntilNoTasksRemain();
456   EXPECT_EQ(expect_reboot ? 1 : 0,
457             power_manager_client_->num_request_restart_calls());
458 }
459
460 void AutomaticRebootManagerBasicTest::CreateAutomaticRebootManager(
461     bool expect_reboot) {
462   automatic_reboot_manager_.reset(new AutomaticRebootManager(
463       scoped_ptr<base::TickClock>(new MockTimeTickClock(task_runner_))));
464   task_runner_->RunUntilIdle();
465   EXPECT_EQ(expect_reboot ? 1 : 0,
466             power_manager_client_->num_request_restart_calls());
467
468   uptime_processing_delay_ =
469       base::TimeTicks() - automatic_reboot_manager_->boot_time_ -
470       task_runner_->Uptime();
471   EXPECT_GE(uptime_processing_delay_, base::TimeDelta());
472   EXPECT_LE(uptime_processing_delay_, base::TimeDelta::FromSeconds(1));
473
474   if (is_user_logged_in_ || expect_reboot)
475     VerifyLoginScreenIdleTimerIsStopped();
476   else
477     VerifyLoginScreenIdleTimerIsRunning();
478 }
479
480 bool AutomaticRebootManagerBasicTest::ReadUpdateRebootNeededUptimeFromFile(
481     base::TimeDelta* uptime) {
482   std::string contents;
483   if (!base::ReadFileToString(update_reboot_needed_uptime_file_, &contents)) {
484     return false;
485   }
486   double seconds;
487   if (!base::StringToDouble(contents.substr(0, contents.find(' ')), &seconds) ||
488       seconds < 0.0) {
489     return false;
490   }
491   *uptime = base::TimeDelta::FromMilliseconds(seconds * 1000.0);
492   return true;
493 }
494
495 void AutomaticRebootManagerBasicTest::
496     VerifyLoginScreenIdleTimerIsStopped() const {
497   VerifyTimerIsStopped(
498       automatic_reboot_manager_->login_screen_idle_timer_.get());
499 }
500
501 void AutomaticRebootManagerBasicTest::VerifyNoGracePeriod() const {
502   EXPECT_FALSE(automatic_reboot_manager_->reboot_requested_);
503   VerifyTimerIsStopped(automatic_reboot_manager_->grace_start_timer_.get());
504   VerifyTimerIsStopped(automatic_reboot_manager_->grace_end_timer_.get());
505 }
506
507 void AutomaticRebootManagerBasicTest::VerifyGracePeriod(
508     const base::TimeDelta& start_uptime) const {
509   const base::TimeDelta start =
510       start_uptime - task_runner_->Uptime() - uptime_processing_delay_;
511   const base::TimeDelta end = start + base::TimeDelta::FromHours(24);
512   if (start <= base::TimeDelta()) {
513     EXPECT_TRUE(automatic_reboot_manager_->reboot_requested_);
514     VerifyTimerIsStopped(automatic_reboot_manager_->grace_start_timer_.get());
515     VerifyTimerIsRunning(automatic_reboot_manager_->grace_end_timer_.get(),
516                          end);
517   } else {
518     EXPECT_FALSE(automatic_reboot_manager_->reboot_requested_);
519     VerifyTimerIsRunning(automatic_reboot_manager_->grace_start_timer_.get(),
520                          start);
521     VerifyTimerIsRunning(automatic_reboot_manager_->grace_end_timer_.get(),
522                          end);
523   }
524 }
525
526 void AutomaticRebootManagerBasicTest::VerifyTimerIsStopped(
527     const Timer* timer) const {
528   if (timer)
529     EXPECT_FALSE(timer->IsRunning());
530 }
531
532 void AutomaticRebootManagerBasicTest::VerifyTimerIsRunning(
533     const Timer* timer,
534     const base::TimeDelta& delay) const {
535   ASSERT_TRUE(timer);
536   EXPECT_TRUE(timer->IsRunning());
537   EXPECT_EQ(delay.ToInternalValue(),
538             timer->GetCurrentDelay().ToInternalValue());
539 }
540
541 void AutomaticRebootManagerBasicTest::
542     VerifyLoginScreenIdleTimerIsRunning() const {
543   VerifyTimerIsRunning(
544       automatic_reboot_manager_->login_screen_idle_timer_.get(),
545       base::TimeDelta::FromSeconds(60));
546 }
547
548 void AutomaticRebootManagerBasicTest::SetUpdateStatusNeedReboot() {
549   UpdateEngineClient::Status client_status;
550   client_status.status = UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT;
551   update_engine_client_->set_default_status(client_status);
552 }
553
554 AutomaticRebootManagerTest::AutomaticRebootManagerTest() {
555   switch (GetParam()) {
556     case AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_LOGIN_SCREEN:
557       is_user_logged_in_ = false;
558       is_logged_in_as_kiosk_app_ = false;
559       break;
560     case AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_KIOSK_APP_SESSION:
561       is_user_logged_in_ = true;
562       is_logged_in_as_kiosk_app_ = true;
563       break;
564     case AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_NON_KIOSK_APP_SESSION:
565       is_user_logged_in_ = true;
566       is_logged_in_as_kiosk_app_ = false;
567       break;
568   }
569 }
570
571 AutomaticRebootManagerTest::~AutomaticRebootManagerTest() {
572 }
573
574 // Chrome is showing the login screen. The current uptime is 12 hours.
575 // Verifies that the idle timer is running. Further verifies that when a kiosk
576 // app session begins, the idle timer is stopped.
577 TEST_F(AutomaticRebootManagerBasicTest, LoginStopsIdleTimer) {
578   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
579
580   // Verify that the device does not reboot immediately and the login screen
581   // idle timer is started.
582   CreateAutomaticRebootManager(false);
583
584   // Notify that a kiosk app session has been started.
585   is_user_logged_in_ = true;
586   is_logged_in_as_kiosk_app_ = true;
587   automatic_reboot_manager_->Observe(
588       chrome::NOTIFICATION_LOGIN_USER_CHANGED,
589       content::Source<AutomaticRebootManagerBasicTest>(this),
590       content::NotificationService::NoDetails());
591
592   // Verify that the login screen idle timer is stopped.
593   VerifyLoginScreenIdleTimerIsStopped();
594
595   // Verify that the device does not reboot eventually.
596   FastForwardUntilNoTasksRemain(false);
597 }
598
599 // Chrome is showing the login screen. The current uptime is 12 hours.
600 // Verifies that the idle timer is running. Further verifies that when a
601 // non-kiosk-app session begins, the idle timer is stopped.
602 TEST_F(AutomaticRebootManagerBasicTest, NonKioskLoginStopsIdleTimer) {
603   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
604
605   // Verify that the device does not reboot immediately and the login screen
606   // idle timer is started.
607   CreateAutomaticRebootManager(false);
608
609   // Notify that a non-kiosk-app session has been started.
610   is_user_logged_in_ = true;
611   automatic_reboot_manager_->Observe(
612       chrome::NOTIFICATION_LOGIN_USER_CHANGED,
613       content::Source<AutomaticRebootManagerBasicTest>(this),
614       content::NotificationService::NoDetails());
615
616   // Verify that the login screen idle timer is stopped.
617   VerifyLoginScreenIdleTimerIsStopped();
618
619   // Verify that the device does not reboot eventually.
620   FastForwardUntilNoTasksRemain(false);
621 }
622
623 // Chrome is showing the login screen. The uptime limit is 6 hours. The current
624 // uptime is 12 hours.
625 // Verifies that user activity prevents the device from rebooting. Further
626 // verifies that when user activity ceases, the devices reboots.
627 TEST_F(AutomaticRebootManagerBasicTest, UserActivityResetsIdleTimer) {
628   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
629
630   // Verify that the device does not reboot immediately and the login screen
631   // idle timer is started.
632   CreateAutomaticRebootManager(false);
633
634   // Set the uptime limit. Verify that the device does not reboot immediately.
635   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
636
637   // Verify that a grace period has started.
638   VerifyGracePeriod(uptime_limit_);
639
640   // Fast forward the uptime by 25 minutes while simulating user activity every
641   // 50 seconds.
642   for (int i = 0; i < 30; ++i) {
643     // Fast forward uptime by 50 seconds. Verify that the device does not reboot
644     // immediately.
645     FastForwardBy(base::TimeDelta::FromSeconds(50), false);
646
647     // Simulate user activity.
648     automatic_reboot_manager_->OnUserActivity(NULL);
649   }
650
651   // Fast forward the uptime by 60 seconds without simulating user activity.
652   // Verify that the device reboots immediately.
653   FastForwardBy(base::TimeDelta::FromSeconds(60), true);
654 }
655
656 // Chrome is running a kiosk app session. The current uptime is 10 days.
657 // Verifies that when the device is suspended and then resumes, it does not
658 // immediately reboot.
659 TEST_F(AutomaticRebootManagerBasicTest, ResumeNoPolicy) {
660   is_user_logged_in_ = true;
661   is_logged_in_as_kiosk_app_ = true;
662   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
663
664   // Verify that the device does not reboot immediately.
665   CreateAutomaticRebootManager(false);
666
667   // Verify that no grace period has started.
668   VerifyNoGracePeriod();
669
670   // Notify that the device has resumed from 1 hour of sleep. Verify that the
671   // device does not reboot immediately.
672   NotifyResumed(false);
673
674   // Verify that the device does not reboot eventually.
675   FastForwardUntilNoTasksRemain(false);
676 }
677
678 // Chrome is running a non-kiosk-app session. The current uptime is 10 days.
679 // Verifies that when the device is suspended and then resumes, it does not
680 // immediately reboot.
681 TEST_F(AutomaticRebootManagerBasicTest, NonKioskResumeAppNoPolicy) {
682   is_user_logged_in_ = true;
683   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
684
685   // Verify that the device does not reboot immediately.
686   CreateAutomaticRebootManager(false);
687
688   // Verify that no grace period has started.
689   VerifyNoGracePeriod();
690
691   // Notify that the device has resumed from 1 hour of sleep. Verify that the
692   // device does not reboot immediately.
693   NotifyResumed(false);
694
695   // Verify that the device does not reboot eventually.
696   FastForwardUntilNoTasksRemain(false);
697 }
698
699 // Chrome is running a kiosk app session. The uptime limit is 24 hours. The
700 // current uptime is 12 hours.
701 // Verifies that when the device is suspended and then resumes, it does not
702 // immediately reboot.
703 TEST_F(AutomaticRebootManagerBasicTest, ResumeBeforeGracePeriod) {
704   is_user_logged_in_ = true;
705   is_logged_in_as_kiosk_app_ = true;
706   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
707
708   // Verify that the device does not reboot immediately.
709   CreateAutomaticRebootManager(false);
710
711   // Set the uptime limit. Verify that the device does not reboot immediately.
712   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
713
714   // Verify that a grace period has been scheduled to start in the future.
715   VerifyGracePeriod(uptime_limit_);
716
717   // Notify that the device has resumed from 1 hour of sleep. Verify that the
718   // device does not reboot immediately.
719   NotifyResumed(false);
720
721   // Verify that the device eventually reboots.
722   FastForwardUntilNoTasksRemain(true);
723 }
724
725 // Chrome is running a non-kiosk-app session. The uptime limit is 24 hours. The
726 // current uptime is 12 hours.
727 // Verifies that when the device is suspended and then resumes, it does not
728 // immediately reboot.
729 TEST_F(AutomaticRebootManagerBasicTest, NonKioskResumeBeforeGracePeriod) {
730   is_user_logged_in_ = true;
731   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
732
733   // Verify that the device does not reboot immediately.
734   CreateAutomaticRebootManager(false);
735
736   // Set the uptime limit. Verify that the device does not reboot immediately.
737   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
738
739   // Verify that a grace period has been scheduled to start in the future.
740   VerifyGracePeriod(uptime_limit_);
741
742   // Notify that the device has resumed from 1 hour of sleep. Verify that the
743   // device does not reboot immediately.
744   NotifyResumed(false);
745
746   // Verify that the device does not reboot eventually.
747   FastForwardUntilNoTasksRemain(false);
748 }
749
750 // Chrome is running a kiosk app session. The uptime limit is 6 hours. The
751 // current uptime is 12 hours.
752 // Verifies that when the device is suspended and then resumes, it immediately
753 // reboots.
754 TEST_F(AutomaticRebootManagerBasicTest, ResumeInGracePeriod) {
755   is_user_logged_in_ = true;
756   is_logged_in_as_kiosk_app_ = true;
757   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
758
759   // Verify that the device does not reboot immediately.
760   CreateAutomaticRebootManager(false);
761
762   // Set the uptime limit. Verify that the device does not reboot immediately.
763   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
764
765   // Verify that a grace period has started.
766   VerifyGracePeriod(uptime_limit_);
767
768   // Notify that the device has resumed from 1 hour of sleep. Verify that the
769   // device reboots immediately.
770   NotifyResumed(true);
771 }
772
773 // Chrome is running a non-kiosk-app session. The uptime limit is 6 hours. The
774 // current uptime is 12 hours.
775 // Verifies that when the device is suspended and then resumes, it does not
776 // immediately reboot.
777 TEST_F(AutomaticRebootManagerBasicTest, NonKioskResumeInGracePeriod) {
778   is_user_logged_in_ = true;
779   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
780
781   // Verify that the device does not reboot immediately.
782   CreateAutomaticRebootManager(false);
783
784   // Set the uptime limit. Verify that the device does not reboot immediately.
785   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
786
787   // Verify that a grace period has started.
788   VerifyGracePeriod(uptime_limit_);
789
790   // Notify that the device has resumed from 1 hour of sleep. Verify that the
791   // device does not reboot immediately.
792   NotifyResumed(false);
793
794   // Verify that the device does not reboot eventually.
795   FastForwardUntilNoTasksRemain(false);
796 }
797
798 // Chrome is running a kiosk app session. The uptime limit is 6 hours. The
799 // current uptime is 29 hours 30 minutes.
800 // Verifies that when the device is suspended and then resumes, it immediately
801 // reboots.
802 TEST_F(AutomaticRebootManagerBasicTest, ResumeAfterGracePeriod) {
803   is_user_logged_in_ = true;
804   is_logged_in_as_kiosk_app_ = true;
805   task_runner_->SetUptime(base::TimeDelta::FromHours(29) +
806                           base::TimeDelta::FromMinutes(30));
807
808   // Verify that the device does not reboot immediately.
809   CreateAutomaticRebootManager(false);
810
811   // Set the uptime limit. Verify that the device does not reboot immediately.
812   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
813
814   // Verify that a grace period has started.
815   VerifyGracePeriod(uptime_limit_);
816
817   // Notify that the device has resumed from 1 hour of sleep. Verify that the
818   // device reboots immediately.
819   NotifyResumed(true);
820 }
821
822 // Chrome is running a non-kiosk-app session. The uptime limit is 6 hours. The
823 // current uptime is 29 hours 30 minutes.
824 // Verifies that when the device is suspended and then resumes, it does not
825 // immediately reboot.
826 TEST_F(AutomaticRebootManagerBasicTest, NonKioskResumeAfterGracePeriod) {
827   is_user_logged_in_ = true;
828   task_runner_->SetUptime(base::TimeDelta::FromHours(29) +
829                           base::TimeDelta::FromMinutes(30));
830
831   // Verify that the device does not reboot immediately.
832   CreateAutomaticRebootManager(false);
833
834   // Set the uptime limit. Verify that the device does not reboot immediately.
835   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
836
837   // Verify that a grace period has started.
838   VerifyGracePeriod(uptime_limit_);
839
840   // Notify that the device has resumed from 1 hour of sleep. Verify that the
841   // device does not reboot immediately.
842   NotifyResumed(false);
843
844   // Verify that the device does not reboot eventually.
845   FastForwardUntilNoTasksRemain(false);
846 }
847
848 // Chrome is running. The current uptime is 10 days.
849 // Verifies that when the browser terminates, the device does not immediately
850 // reboot.
851 TEST_P(AutomaticRebootManagerTest, TerminateNoPolicy) {
852   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
853
854   // Verify that the device does not reboot immediately.
855   CreateAutomaticRebootManager(false);
856
857   // Verify that no grace period has started.
858   VerifyNoGracePeriod();
859
860   // Notify that the browser is terminating. Verify that the device does not
861   // reboot immediately.
862   NotifyTerminating(false);
863
864   // Verify that the device does not reboot eventually.
865   FastForwardUntilNoTasksRemain(false);
866 }
867
868 // Chrome is running. The uptime limit is set to 24 hours. The current uptime is
869 // 12 hours.
870 // Verifies that when the browser terminates, it does not immediately reboot.
871 TEST_P(AutomaticRebootManagerTest, TerminateBeforeGracePeriod) {
872   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
873
874   // Verify that the device does not reboot immediately.
875   CreateAutomaticRebootManager(false);
876
877   // Set the uptime limit. Verify that the device does not reboot immediately.
878   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
879
880   // Verify that a grace period has been scheduled to start in the future.
881   VerifyGracePeriod(uptime_limit_);
882
883   // Notify that the browser is terminating. Verify that the device does not
884   // reboot immediately.
885   NotifyTerminating(false);
886
887   // Verify that unless a non-kiosk-app session is in progress, the device
888   // eventually reboots.
889   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
890                                 is_logged_in_as_kiosk_app_);
891 }
892
893 // Chrome is running. The uptime limit is set to 6 hours. The current uptime is
894 // 12 hours.
895 // Verifies that when the browser terminates, the device immediately reboots if
896 // a kiosk app session is in progress.
897 TEST_P(AutomaticRebootManagerTest, TerminateInGracePeriod) {
898   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
899
900   // Verify that the device does not reboot immediately.
901   CreateAutomaticRebootManager(false);
902
903   // Set the uptime limit. Verify that the device does not reboot immediately.
904   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
905
906   // Verify that a grace period has started.
907   VerifyGracePeriod(uptime_limit_);
908
909   // Notify that the browser is terminating. Verify that the device immediately
910   // reboots if a kiosk app session is in progress.
911   NotifyTerminating(is_logged_in_as_kiosk_app_);
912
913   // Verify that if a non-kiosk-app session is in progress, the device does not
914   // reboot eventually.
915   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
916                                 is_logged_in_as_kiosk_app_);
917 }
918
919 // Chrome is running. The current uptime is 12 hours.
920 // Verifies that when the uptime limit is set to 24 hours, no reboot occurs and
921 // a grace period is scheduled to begin after 24 hours of uptime.
922 TEST_P(AutomaticRebootManagerTest, BeforeUptimeLimitGracePeriod) {
923   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
924
925   // Verify that the device does not reboot immediately.
926   CreateAutomaticRebootManager(false);
927
928   // Verify that no grace period has started.
929   VerifyNoGracePeriod();
930
931   // Set the uptime limit. Verify that the device does not reboot immediately.
932   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
933
934   // Verify that a grace period has been scheduled to start in the future.
935   VerifyGracePeriod(uptime_limit_);
936
937   // Verify that unless a non-kiosk-app session is in progress, the device
938   // eventually reboots.
939   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
940                                 is_logged_in_as_kiosk_app_);
941 }
942
943 // Chrome is running. The current uptime is 12 hours.
944 // Verifies that when the uptime limit is set to 6 hours, a reboot is requested
945 // and a grace period is started that will end after 6 + 24 hours of uptime.
946 TEST_P(AutomaticRebootManagerTest, InUptimeLimitGracePeriod) {
947   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
948
949   // Verify that the device does not reboot immediately.
950   CreateAutomaticRebootManager(false);
951
952   // Verify that no grace period has started.
953   VerifyNoGracePeriod();
954
955   // Set the uptime limit. Verify that the device does not reboot immediately.
956   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
957
958   // Verify that a grace period has started.
959   VerifyGracePeriod(uptime_limit_);
960
961   // Verify that unless a non-kiosk-app session is in progress, the device
962   // eventually reboots.
963   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
964                                 is_logged_in_as_kiosk_app_);
965 }
966
967 // Chrome is running. The current uptime is 10 days.
968 // Verifies that when the uptime limit is set to 6 hours, the device reboots
969 // immediately if no non-kiosk-app-session is in progress because the grace
970 // period ended after 6 + 24 hours of uptime.
971 TEST_P(AutomaticRebootManagerTest, AfterUptimeLimitGracePeriod) {
972   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
973
974   // Verify that the device does not reboot immediately.
975   CreateAutomaticRebootManager(false);
976
977   // Verify that no grace period has started.
978   VerifyNoGracePeriod();
979
980   // Set the uptime limit. Verify that unless a non-kiosk-app session is in
981   // progress, the the device immediately reboots.
982   SetUptimeLimit(base::TimeDelta::FromHours(6), !is_user_logged_in_ ||
983                                                 is_logged_in_as_kiosk_app_);
984
985   // Verify that if a non-kiosk-app session is in progress, the device does not
986   // reboot eventually.
987   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
988                                 is_logged_in_as_kiosk_app_);
989 }
990
991 // Chrome is running. The uptime limit is set to 12 hours. The current uptime is
992 // 6 hours.
993 // Verifies that when the uptime limit is removed, the grace period is removed.
994 TEST_P(AutomaticRebootManagerTest, UptimeLimitOffBeforeGracePeriod) {
995   task_runner_->SetUptime(base::TimeDelta::FromHours(6));
996
997   // Verify that the device does not reboot immediately.
998   CreateAutomaticRebootManager(false);
999
1000   // Set the uptime limit. Verify that the device does not reboot immediately.
1001   SetUptimeLimit(base::TimeDelta::FromHours(12), false);
1002
1003   // Verify that a grace period has been scheduled to start in the future.
1004   VerifyGracePeriod(uptime_limit_);
1005
1006   // Fast forward the uptime by 1 hour. Verify that the device does not reboot
1007   // immediately.
1008   FastForwardBy(base::TimeDelta::FromHours(1), false);
1009
1010   // Remove the uptime limit. Verify that the device does not reboot
1011   // immediately.
1012   SetUptimeLimit(base::TimeDelta(), false);
1013
1014   // Verify that the grace period has been removed.
1015   VerifyNoGracePeriod();
1016
1017   // Verify that the device does not reboot eventually.
1018   FastForwardUntilNoTasksRemain(false);
1019 }
1020
1021 // Chrome is running. The uptime limit is set to 12 hours. The current uptime is
1022 // 24 hours.
1023 // Verifies that when the uptime limit is removed, the grace period is removed.
1024 TEST_P(AutomaticRebootManagerTest, UptimeLimitOffInGracePeriod) {
1025   task_runner_->SetUptime(base::TimeDelta::FromHours(24));
1026
1027   // Verify that the device does not reboot immediately.
1028   CreateAutomaticRebootManager(false);
1029
1030   // Set the uptime limit. Verify that the device does not reboot immediately.
1031   SetUptimeLimit(base::TimeDelta::FromHours(12), false);
1032
1033   // Verify that a grace period has started.
1034   VerifyGracePeriod(uptime_limit_);
1035
1036   // Fast forward the uptime by 20 seconds. Verify that the device does not
1037   // reboot immediately.
1038   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1039
1040   // Remove the uptime limit. Verify that the device does not reboot
1041   // immediately.
1042   SetUptimeLimit(base::TimeDelta(), false);
1043
1044   // Verify that the grace period has been removed.
1045   VerifyNoGracePeriod();
1046
1047   // Verify that the device does not reboot eventually.
1048   FastForwardUntilNoTasksRemain(false);
1049 }
1050
1051 // Chrome is running. The uptime limit is set to 12 hours. The current uptime is
1052 // 6 hours.
1053 // Verifies that when the uptime limit is extended to 24 hours, the grace period
1054 // is rescheduled to start further in the future.
1055 TEST_P(AutomaticRebootManagerTest, ExtendUptimeLimitBeforeGracePeriod) {
1056   task_runner_->SetUptime(base::TimeDelta::FromHours(6));
1057
1058   // Verify that the device does not reboot immediately.
1059   CreateAutomaticRebootManager(false);
1060
1061   // Set the uptime limit. Verify that the device does not reboot immediately.
1062   SetUptimeLimit(base::TimeDelta::FromHours(12), false);
1063
1064   // Verify that a grace period has been scheduled to start in the future.
1065   VerifyGracePeriod(uptime_limit_);
1066
1067   // Fast forward the uptime by 20 seconds. Verify that the device does not
1068   // reboot immediately.
1069   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1070
1071   // Extend the uptime limit. Verify that the device does not reboot
1072   // immediately.
1073   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
1074
1075   // Verify that the grace period has been rescheduled to start further in the
1076   // future.
1077   VerifyGracePeriod(uptime_limit_);
1078
1079   // Verify that unless a non-kiosk-app session is in progress, the device
1080   // eventually reboots.
1081   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1082                                 is_logged_in_as_kiosk_app_);
1083 }
1084
1085 // Chrome is running. The uptime limit is set to 12 hours. The current uptime is
1086 // 18 hours.
1087 // Verifies that when the uptime limit is extended to 24 hours, the grace period
1088 // is rescheduled to start in the future.
1089 TEST_P(AutomaticRebootManagerTest, ExtendUptimeLimitInGracePeriod) {
1090   task_runner_->SetUptime(base::TimeDelta::FromHours(18));
1091
1092   // Verify that the device does not reboot immediately.
1093   CreateAutomaticRebootManager(false);
1094
1095   // Set the uptime limit. Verify that the device does not reboot immediately.
1096   SetUptimeLimit(base::TimeDelta::FromHours(12), false);
1097
1098   // Verify that a grace period has started.
1099   VerifyGracePeriod(uptime_limit_);
1100
1101   // Fast forward the uptime by 20 seconds. Verify that the device does not
1102   // reboot immediately.
1103   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1104
1105   // Extend the uptime limit. Verify that the device does not reboot
1106   // immediately.
1107   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
1108
1109   // Verify that the grace period has been rescheduled to start in the future.
1110   VerifyGracePeriod(uptime_limit_);
1111
1112   // Verify that unless a non-kiosk-app session is in progress, the device
1113   // eventually reboots.
1114   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1115                                 is_logged_in_as_kiosk_app_);
1116 }
1117
1118 // Chrome is running. The uptime limit is set to 18 hours. The current uptime is
1119 // 12 hours.
1120 // Verifies that when the uptime limit is shortened to 6 hours, the grace period
1121 // is rescheduled to have already started.
1122 TEST_P(AutomaticRebootManagerTest, ShortenUptimeLimitBeforeToInGracePeriod) {
1123   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1124
1125   // Verify that the device does not reboot immediately.
1126   CreateAutomaticRebootManager(false);
1127
1128   // Set the uptime limit. Verify that the device does not reboot immediately.
1129   SetUptimeLimit(base::TimeDelta::FromHours(18), false);
1130
1131   // Verify that a grace period has been scheduled to start in the future.
1132   VerifyGracePeriod(uptime_limit_);
1133
1134   // Fast forward the uptime by 20 seconds. Verify that the device does not
1135   // reboot immediately.
1136   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1137
1138   // Shorten the uptime limit. Verify that the device does not reboot
1139   // immediately.
1140   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
1141
1142   // Verify that the grace period has been rescheduled and has started already.
1143   VerifyGracePeriod(uptime_limit_);
1144
1145   // Verify that unless a non-kiosk-app session is in progress, the device
1146   // eventually reboots.
1147   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1148                                 is_logged_in_as_kiosk_app_);
1149 }
1150
1151 // Chrome is running. The uptime limit is set to 24 hours. The current uptime is
1152 // 36 hours.
1153 // Verifies that when the uptime limit is shortened to 18 hours, the grace
1154 // period is rescheduled to have started earlier.
1155 TEST_P(AutomaticRebootManagerTest, ShortenUptimeLimitInToInGracePeriod) {
1156   task_runner_->SetUptime(base::TimeDelta::FromHours(36));
1157
1158   // Verify that the device does not reboot immediately.
1159   CreateAutomaticRebootManager(false);
1160
1161   // Set the uptime limit. Verify that the device does not reboot immediately.
1162   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
1163
1164   // Verify that a grace period has started.
1165   VerifyGracePeriod(uptime_limit_);
1166
1167   // Fast forward the uptime by 20 seconds. Verify that the device does not
1168   // reboot immediately.
1169   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1170
1171   // Shorten the uptime limit. Verify that the device does not reboot
1172   // immediately.
1173   SetUptimeLimit(base::TimeDelta::FromHours(18), false);
1174
1175   // Verify that the grace period has been rescheduled to have started earlier.
1176   VerifyGracePeriod(uptime_limit_);
1177
1178   // Verify that unless a non-kiosk-app session is in progress, the device
1179   // eventually reboots.
1180   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1181                                 is_logged_in_as_kiosk_app_);
1182 }
1183
1184 // Chrome is running. The uptime limit is set to 24 hours. The current uptime is
1185 // 36 hours.
1186 // Verifies that when the uptime limit is shortened to 6 hours, the device
1187 // reboots immediately if no non-kiosk-app session is in progress because the
1188 // grace period ended after 6 + 24 hours of uptime.
1189 TEST_P(AutomaticRebootManagerTest, ShortenUptimeLimitInToAfterGracePeriod) {
1190   task_runner_->SetUptime(base::TimeDelta::FromHours(36));
1191
1192   // Verify that the device does not reboot immediately.
1193   CreateAutomaticRebootManager(false);
1194
1195   // Set the uptime limit. Verify that the device does not reboot immediately.
1196   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
1197
1198   // Verify that a grace period has started.
1199   VerifyGracePeriod(uptime_limit_);
1200
1201   // Fast forward the uptime by 20 seconds. Verify that the device does not
1202   // reboot immediately.
1203   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1204
1205   // Shorten the uptime limit. Verify that unless a non-kiosk-app session is in
1206   // progress, the the device immediately reboots.
1207   SetUptimeLimit(base::TimeDelta::FromHours(6), !is_user_logged_in_ ||
1208                                                 is_logged_in_as_kiosk_app_);
1209
1210   // Verify that if a non-kiosk-app session is in progress, the device does not
1211   // reboot eventually.
1212   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1213                                 is_logged_in_as_kiosk_app_);
1214 }
1215
1216 // Chrome is running. The current uptime is 12 hours.
1217 // Verifies that when an update is applied, the current uptime is persisted as
1218 // the time at which a reboot became necessary. Further verifies that when the
1219 // policy to automatically reboot after an update is not enabled, no reboot
1220 // occurs and no grace period is scheduled.
1221 TEST_P(AutomaticRebootManagerTest, UpdateNoPolicy) {
1222   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1223
1224   // Verify that the device does not reboot immediately.
1225   CreateAutomaticRebootManager(false);
1226
1227   // Verify that no grace period has started.
1228   VerifyNoGracePeriod();
1229
1230   // Notify that an update has been applied and a reboot is necessary. Verify
1231   // that the device does not reboot immediately.
1232   NotifyUpdateRebootNeeded();
1233
1234   // Verify that the current uptime has been persisted as the time at which a
1235   // reboot became necessary.
1236   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1237       &update_reboot_needed_uptime_));
1238   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1239
1240   // Verify that no grace period has started.
1241   VerifyNoGracePeriod();
1242
1243   // Verify that the device does not reboot eventually.
1244   FastForwardUntilNoTasksRemain(false);
1245 }
1246
1247 // Chrome is running. The current uptime is 12 hours.
1248 // Verifies that when an update is applied, the current uptime is persisted as
1249 // the time at which a reboot became necessary. Further verifies that when the
1250 // policy to automatically reboot after an update is enabled, a reboot is
1251 // requested and a grace period is started that will end 24 hours from now.
1252 TEST_P(AutomaticRebootManagerTest, Update) {
1253   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1254   SetRebootAfterUpdate(true, false);
1255
1256   // Verify that the device does not reboot immediately.
1257   CreateAutomaticRebootManager(false);
1258
1259   // Verify that no grace period has started.
1260   VerifyNoGracePeriod();
1261
1262   // Notify that an update has been applied and a reboot is necessary. Verify
1263   // that the device does not reboot immediately.
1264   NotifyUpdateRebootNeeded();
1265
1266   // Verify that the current uptime has been persisted as the time at which a
1267   // reboot became necessary.
1268   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1269       &update_reboot_needed_uptime_));
1270   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1271
1272   // Verify that a grace period has started.
1273   VerifyGracePeriod(update_reboot_needed_uptime_ + uptime_processing_delay_);
1274
1275   // Verify that unless a non-kiosk-app session is in progress, the device
1276   // eventually reboots.
1277   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1278                                 is_logged_in_as_kiosk_app_);
1279 }
1280
1281 // Chrome is running. The current uptime is 12 hours.
1282 // Verifies that when Chrome is notified twice that an update has been applied,
1283 // the second notification is ignored and the uptime at which it occured does
1284 // not get persisted as the time at which an update became necessary.
1285 TEST_P(AutomaticRebootManagerTest, UpdateAfterUpdate) {
1286   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1287   SetRebootAfterUpdate(true, false);
1288
1289   // Verify that the device does not reboot immediately.
1290   CreateAutomaticRebootManager(false);
1291
1292   // Verify that no grace period has started.
1293   VerifyNoGracePeriod();
1294
1295   // Notify that an update has been applied and a reboot is necessary. Verify
1296   // that the device does not reboot immediately.
1297   NotifyUpdateRebootNeeded();
1298
1299   // Verify that the current uptime has been persisted as the time at which a
1300   // reboot became necessary.
1301   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1302       &update_reboot_needed_uptime_));
1303   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1304
1305   // Verify that a grace period has started.
1306   VerifyGracePeriod(update_reboot_needed_uptime_ + uptime_processing_delay_);
1307
1308   // Fast forward the uptime by 20 seconds. Verify that the device does not
1309   // reboot immediately.
1310   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1311
1312   // Notify that an update has been applied and a reboot is necessary. Verify
1313   // that the device does not reboot immediately.
1314   NotifyUpdateRebootNeeded();
1315
1316   // Verify that the previously persisted time at which a reboot became
1317   // necessary has not been overwritten.
1318   base::TimeDelta new_update_reboot_needed_uptime;
1319   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1320       &new_update_reboot_needed_uptime));
1321   EXPECT_EQ(update_reboot_needed_uptime_, new_update_reboot_needed_uptime);
1322
1323   // Verify that unless a non-kiosk-app session is in progress, the device
1324   // eventually reboots.
1325   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1326                                 is_logged_in_as_kiosk_app_);
1327 }
1328
1329 // Chrome is running. The current uptime is 10 minutes.
1330 // Verifies that when the policy to automatically reboot after an update is
1331 // enabled, no reboot occurs a grace period is scheduled to begin after the
1332 // minimum of 1 hour of uptime. Further verifies that when an update is applied,
1333 // the current uptime is persisted as the time at which a reboot became
1334 // necessary.
1335 TEST_P(AutomaticRebootManagerTest, UpdateBeforeMinimumUptime) {
1336   task_runner_->SetUptime(base::TimeDelta::FromMinutes(10));
1337   SetRebootAfterUpdate(true, false);
1338
1339   // Verify that the device does not reboot immediately.
1340   CreateAutomaticRebootManager(false);
1341
1342   // Verify that no grace period has started.
1343   VerifyNoGracePeriod();
1344
1345   // Notify that an update has been applied and a reboot is necessary. Verify
1346   // that the device does not reboot immediately.
1347   NotifyUpdateRebootNeeded();
1348
1349   // Verify that the current uptime has been persisted as the time at which a
1350   // reboot became necessary.
1351   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1352       &update_reboot_needed_uptime_));
1353   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1354
1355   // Verify that a grace period has been scheduled to begin in the future.
1356   VerifyGracePeriod(base::TimeDelta::FromHours(1));
1357
1358   // Verify that unless a non-kiosk-app session is in progress, the device
1359   // eventually reboots.
1360   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1361                                 is_logged_in_as_kiosk_app_);
1362 }
1363
1364 // Chrome is running. An update was applied and a reboot became necessary to
1365 // complete the update process after 6 hours of uptime. The current uptime is
1366 // 12 hours.
1367 // Verifies that when the policy to automatically reboot after an update is
1368 // enabled, a reboot is requested and a grace period is started that will end
1369 // after 6 + 24 hours of uptime.
1370 TEST_P(AutomaticRebootManagerTest, PolicyAfterUpdateInGracePeriod) {
1371   task_runner_->SetUptime(base::TimeDelta::FromHours(6));
1372
1373   // Verify that the device does not reboot immediately.
1374   CreateAutomaticRebootManager(false);
1375
1376   // Notify that an update has been applied and a reboot is necessary. Verify
1377   // that the device does not reboot immediately.
1378   NotifyUpdateRebootNeeded();
1379
1380   // Fast forward the uptime to 12 hours. Verify that the device does not reboot
1381   // immediately.
1382   FastForwardBy(base::TimeDelta::FromHours(6), false);
1383
1384   // Simulate user activity.
1385   automatic_reboot_manager_->OnUserActivity(NULL);
1386
1387   // Enable automatic reboot after an update has been applied. Verify that the
1388   // device does not reboot immediately.
1389   SetRebootAfterUpdate(true, false);
1390
1391   // Verify that a grace period has started.
1392   VerifyGracePeriod(base::TimeDelta::FromHours(6) + uptime_processing_delay_);
1393
1394   // Verify that unless a non-kiosk-app session is in progress, the device
1395   // eventually reboots.
1396   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1397                                 is_logged_in_as_kiosk_app_);
1398 }
1399
1400 // Chrome is running. An update was applied and a reboot became necessary to
1401 // complete the update process after 6 hours of uptime. The current uptime is
1402 // 10 days.
1403 // Verifies that when the policy to automatically reboot after an update is
1404 // enabled, the device reboots immediately if no non-kiosk-app session is in
1405 // progress because the grace period ended after 6 + 24 hours of uptime.
1406 TEST_P(AutomaticRebootManagerTest, PolicyAfterUpdateAfterGracePeriod) {
1407   task_runner_->SetUptime(base::TimeDelta::FromHours(6));
1408
1409   // Verify that the device does not reboot immediately.
1410   CreateAutomaticRebootManager(false);
1411
1412   // Notify that an update has been applied and a reboot is necessary. Verify
1413   // that the device does not reboot immediately.
1414   NotifyUpdateRebootNeeded();
1415
1416   // Fast forward the uptime to 12 hours. Verify that the device does not reboot
1417   // immediately.
1418   FastForwardBy(base::TimeDelta::FromDays(10) - base::TimeDelta::FromHours(6),
1419                 false);
1420
1421   // Simulate user activity.
1422   automatic_reboot_manager_->OnUserActivity(NULL);
1423
1424   // Enable automatic rebooting after an update has been applied. Verify that
1425   // unless a non-kiosk-app session is in progress, the the device immediately
1426   // reboots.
1427   SetRebootAfterUpdate(true, !is_user_logged_in_ || is_logged_in_as_kiosk_app_);
1428
1429   // Verify that if a non-kiosk-app session is in progress, the device does not
1430   // reboot eventually.
1431   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1432                                 is_logged_in_as_kiosk_app_);
1433 }
1434
1435 // Chrome is running. An update was applied and a reboot became necessary to
1436 // complete the update process after 6 hours of uptime. The policy to
1437 // automatically reboot after an update is enabled. The current uptime is
1438 // 6 hours 20 seconds.
1439 // Verifies that when the policy to automatically reboot after an update is
1440 // disabled, the reboot request and grace period are removed.
1441 TEST_P(AutomaticRebootManagerTest, PolicyOffAfterUpdate) {
1442   task_runner_->SetUptime(base::TimeDelta::FromHours(6));
1443   SetRebootAfterUpdate(true, false);
1444
1445   // Verify that the device does not reboot immediately.
1446   CreateAutomaticRebootManager(false);
1447
1448   // Notify that an update has been applied and a reboot is necessary. Verify
1449   // that the device does not reboot immediately.
1450   NotifyUpdateRebootNeeded();
1451
1452   // Verify that a grace period has started.
1453   VerifyGracePeriod(task_runner_->Uptime() + uptime_processing_delay_);
1454
1455   // Fast forward the uptime by 20 seconds. Verify that the device does not
1456   // reboot immediately.
1457   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1458
1459   // Disable automatic rebooting after an update has been applied. Verify that
1460   // the device does not reboot immediately.
1461   SetRebootAfterUpdate(false, false);
1462
1463   // Verify that the grace period has been removed.
1464   VerifyNoGracePeriod();
1465
1466   // Verify that the device does not reboot eventually.
1467   FastForwardUntilNoTasksRemain(false);
1468 }
1469
1470 // Chrome is running. The current uptime is not available.
1471 // Verifies that even if an uptime limit is set, the policy to automatically
1472 // reboot after an update is enabled and an update has been applied, no reboot
1473 // occurs and no grace period is scheduled. Further verifies that no time is
1474 // persisted as the time at which a reboot became necessary.
1475 TEST_P(AutomaticRebootManagerTest, NoUptime) {
1476   // Verify that the device does not reboot immediately.
1477   CreateAutomaticRebootManager(false);
1478
1479   // Set the uptime limit. Verify that the device does not reboot immediately.
1480   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
1481
1482   // Verify that no grace period has started.
1483   VerifyNoGracePeriod();
1484
1485   // Enable automatic rebooting after an update has been applied. Verify that
1486   // the device does not reboot immediately.
1487   SetRebootAfterUpdate(true, false);
1488
1489   // Verify that no grace period has started.
1490   VerifyNoGracePeriod();
1491
1492   // Notify that an update has been applied and a reboot is necessary. Verify
1493   // that the device does not reboot immediately.
1494   NotifyUpdateRebootNeeded();
1495
1496   // Verify that no time is persisted as the time at which a reboot became
1497   // necessary.
1498   EXPECT_FALSE(ReadUpdateRebootNeededUptimeFromFile(
1499       &update_reboot_needed_uptime_));
1500
1501   // Verify that no grace period has started.
1502   VerifyNoGracePeriod();
1503
1504   // Verify that the device does not reboot eventually.
1505   FastForwardUntilNoTasksRemain(false);
1506 }
1507
1508 // Chrome is running. The policy to automatically reboot after an update is
1509 // enabled. The current uptime is 12 hours.
1510 // Verifies that when an uptime limit of 6 hours is set, the availability of an
1511 // update does not cause the grace period to be rescheduled. Further verifies
1512 // that the current uptime is persisted as the time at which a reboot became
1513 // necessary.
1514 TEST_P(AutomaticRebootManagerTest, UptimeLimitBeforeUpdate) {
1515   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1516   SetRebootAfterUpdate(true, false);
1517
1518   // Verify that the device does not reboot immediately.
1519   CreateAutomaticRebootManager(false);
1520
1521   // Set the uptime limit. Verify that the device does not reboot immediately.
1522   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
1523
1524   // Verify that a grace period has been scheduled to start in the future.
1525   VerifyGracePeriod(uptime_limit_);
1526
1527   // Fast forward the uptime by 20 seconds. Verify that the device does not
1528   // reboot immediately.
1529   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1530
1531   // Notify that an update has been applied and a reboot is necessary. Verify
1532   // that the device does not reboot immediately.
1533   NotifyUpdateRebootNeeded();
1534
1535   // Verify that the current uptime has been persisted as the time at which a
1536   // reboot became necessary.
1537   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1538       &update_reboot_needed_uptime_));
1539   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1540
1541   // Verify that the grace period has not been rescheduled.
1542   VerifyGracePeriod(uptime_limit_);
1543
1544   // Verify that unless a non-kiosk-app session is in progress, the device
1545   // eventually reboots.
1546   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1547                                 is_logged_in_as_kiosk_app_);
1548 }
1549
1550 // Chrome is running. The policy to automatically reboot after an update is
1551 // enabled. The current uptime is 12 hours.
1552 // Verifies that when an uptime limit of 24 hours is set, the availability of an
1553 // update causes the grace period to be rescheduled so that it ends 24 hours
1554 // from now. Further verifies that the current uptime is persisted as the time
1555 // at which a reboot became necessary.
1556 TEST_P(AutomaticRebootManagerTest, UpdateBeforeUptimeLimit) {
1557   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1558   SetRebootAfterUpdate(true, false);
1559
1560   // Verify that the device does not reboot immediately.
1561   CreateAutomaticRebootManager(false);
1562
1563   // Set the uptime limit. Verify that the device does not reboot immediately.
1564   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
1565
1566   // Verify that a grace period has been scheduled to start in the future.
1567   VerifyGracePeriod(uptime_limit_);
1568
1569   // Fast forward the uptime by 20 seconds. Verify that the device does not
1570   // reboot immediately.
1571   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1572
1573   // Notify that an update has been applied and a reboot is necessary. Verify
1574   // that the device does not reboot immediately.
1575   NotifyUpdateRebootNeeded();
1576
1577   // Verify that the current uptime has been persisted as the time at which a
1578   // reboot became necessary.
1579   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1580       &update_reboot_needed_uptime_));
1581   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1582
1583   // Verify that the grace period has been rescheduled to start at the time that
1584   // the update became available.
1585   VerifyGracePeriod(update_reboot_needed_uptime_ + uptime_processing_delay_);
1586
1587   // Verify that unless a non-kiosk-app session is in progress, the device
1588   // eventually reboots.
1589   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1590                                 is_logged_in_as_kiosk_app_);
1591 }
1592
1593 // Chrome is running. The uptime limit is set to 24 hours. An update was applied
1594 // and a reboot became necessary to complete the update process after 12 hours.
1595 // The policy to automatically reboot after an update is enabled. The current
1596 // uptime is 12 hours 20 seconds.
1597 // Verifies that when the policy to reboot after an update is disabled, the
1598 // grace period is rescheduled to start after 24 hours of uptime. Further
1599 // verifies that when the uptime limit is removed, the grace period is removed.
1600 TEST_P(AutomaticRebootManagerTest, PolicyOffThenUptimeLimitOff) {
1601   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1602   SetRebootAfterUpdate(true, false);
1603
1604   // Verify that the device does not reboot immediately.
1605   CreateAutomaticRebootManager(false);
1606
1607   // Set the uptime limit. Verify that the device does not reboot immediately.
1608   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
1609
1610   // Verify that the grace period has started.
1611   VerifyGracePeriod(uptime_limit_);
1612
1613   // Notify that an update has been applied and a reboot is necessary. Verify
1614   // that the device does not reboot immediately.
1615   NotifyUpdateRebootNeeded();
1616
1617   // Verify that the current uptime has been persisted as the time at which a
1618   // reboot became necessary.
1619   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1620       &update_reboot_needed_uptime_));
1621   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1622
1623   // Verify that a grace period has been rescheduled to end 24 hours from now.
1624   VerifyGracePeriod(update_reboot_needed_uptime_ + uptime_processing_delay_);
1625
1626   // Fast forward the uptime by 20 seconds. Verify that the device does not
1627   // reboot immediately.
1628   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1629
1630   // Disable automatic reboot after an update has been applied. Verify that the
1631   // device does not reboot immediately.
1632   SetRebootAfterUpdate(false, false);
1633
1634   // Verify that the grace period has been rescheduled to start after 24 hours
1635   // of uptime.
1636   VerifyGracePeriod(uptime_limit_);
1637
1638   // Remove the uptime limit. Verify that the device does not reboot
1639   // immediately.
1640   SetUptimeLimit(base::TimeDelta(), false);
1641
1642   // Verify that the grace period has been removed.
1643   VerifyNoGracePeriod();
1644
1645   // Verify that the device does not reboot eventually.
1646   FastForwardUntilNoTasksRemain(false);
1647 }
1648
1649 // Chrome is running. The uptime limit is set to 6 hours. An update was applied
1650 // and a reboot became necessary to complete the update process after 12 hours.
1651 // The policy to automatically reboot after an update is enabled. The current
1652 // uptime is 12 hours 20 seconds.
1653 // Verifies that when the uptime limit is removed, the grace period is
1654 // rescheduled to have started after 12 hours of uptime. Further verifies that
1655 // when the policy to reboot after an update is disabled, the reboot request and
1656 // grace period are removed.
1657 TEST_P(AutomaticRebootManagerTest, UptimeLimitOffThenPolicyOff) {
1658   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1659   SetRebootAfterUpdate(true, false);
1660
1661   // Verify that the device does not reboot immediately.
1662   CreateAutomaticRebootManager(false);
1663
1664   // Notify that an update has been applied and a reboot is necessary. Verify
1665   // that the device does not reboot immediately.
1666   NotifyUpdateRebootNeeded();
1667
1668   // Verify that the current uptime has been persisted as the time at which a
1669   // reboot became necessary.
1670   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1671       &update_reboot_needed_uptime_));
1672   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1673
1674   // Verify that the grace period has started.
1675   VerifyGracePeriod(update_reboot_needed_uptime_ + uptime_processing_delay_);
1676
1677   // Set the uptime limit. Verify that the device does not reboot immediately.
1678   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
1679
1680   // Verify that the grace period has been rescheduled to have started after
1681   // 6 hours of uptime.
1682   VerifyGracePeriod(uptime_limit_);
1683
1684   // Fast forward the uptime by 20 seconds. Verify that the device does not
1685   // reboot immediately.
1686   FastForwardBy(base::TimeDelta::FromSeconds(20), false);
1687
1688   // Remove the uptime limit. Verify that the device does not reboot
1689   // immediately.
1690   SetUptimeLimit(base::TimeDelta(), false);
1691
1692   // Verify that a grace period has been rescheduled to have started after 12
1693   // hours of uptime.
1694   VerifyGracePeriod(update_reboot_needed_uptime_ + uptime_processing_delay_);
1695
1696   // Disable automatic reboot after an update has been applied. Verify that the
1697   // device does not reboot immediately.
1698   SetRebootAfterUpdate(false, false);
1699
1700   // Verify that the grace period has been removed.
1701   VerifyNoGracePeriod();
1702
1703   // Verify that the device does not reboot eventually.
1704   FastForwardUntilNoTasksRemain(false);
1705 }
1706
1707 // Chrome is running. The uptime limit is 6 hours. The current uptime is
1708 // 29 hours 59 minutes 59 seconds.
1709 // Verifies that if no non-kiosk-app session is in progress, the device reboots
1710 // immediately when the grace period ends after 6 + 24 hours of uptime.
1711 TEST_P(AutomaticRebootManagerTest, GracePeriodEnd) {
1712   task_runner_->SetUptime(base::TimeDelta::FromHours(29) +
1713                           base::TimeDelta::FromMinutes(59) +
1714                           base::TimeDelta::FromSeconds(59));
1715
1716   // Verify that the device does not reboot immediately.
1717   CreateAutomaticRebootManager(false);
1718
1719   // Set the uptime limit. Verify that the device does not reboot immediately.
1720   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
1721
1722   // Verify that a grace period has started.
1723   VerifyGracePeriod(uptime_limit_);
1724
1725   // Fast forward the uptime by 1 second. Verify that unless a non-kiosk-app
1726   // session is in progress, the the device immediately reboots.
1727   FastForwardBy(base::TimeDelta::FromSeconds(1), !is_user_logged_in_ ||
1728                                                  is_logged_in_as_kiosk_app_);
1729
1730   // Verify that if a non-kiosk-app session is in progress, the device does not
1731   // reboot eventually.
1732   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1733                                 is_logged_in_as_kiosk_app_);
1734 }
1735
1736 // Chrome is starting. The current uptime is 10 days.
1737 // Verifies that when no automatic reboot policy is enabled, no reboot occurs
1738 // and no grace period is scheduled.
1739 TEST_P(AutomaticRebootManagerTest, StartNoPolicy) {
1740   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
1741
1742   // Verify that the device does not reboot immediately.
1743   CreateAutomaticRebootManager(false);
1744
1745   // Verify that no grace period has started.
1746   VerifyNoGracePeriod();
1747
1748   // Verify that the device does not reboot eventually.
1749   FastForwardUntilNoTasksRemain(false);
1750 }
1751
1752 // Chrome is starting. The uptime limit is set to 24 hours. The current uptime
1753 // is 12 hours.
1754 // Verifies that no reboot occurs and a grace period is scheduled to begin after
1755 // 24 hours of uptime.
1756 TEST_P(AutomaticRebootManagerTest, StartBeforeUptimeLimitGracePeriod) {
1757   SetUptimeLimit(base::TimeDelta::FromHours(24), false);
1758   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1759
1760   // Verify that the device does not reboot immediately.
1761   CreateAutomaticRebootManager(false);
1762
1763   // Verify that a grace period has been scheduled to start in the future.
1764   VerifyGracePeriod(uptime_limit_);
1765
1766   // Verify that unless a non-kiosk-app session is in progress, the device
1767   // eventually reboots.
1768   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1769                                 is_logged_in_as_kiosk_app_);
1770 }
1771
1772 // Chrome is starting. The uptime limit is set to 6 hours. The current uptime is
1773 // 10 days.
1774 // Verifies that if no non-kiosk-app session is in progress, the device reboots
1775 // immediately because the grace period ended after 6 + 24 hours of uptime.
1776 TEST_P(AutomaticRebootManagerTest, StartAfterUptimeLimitGracePeriod) {
1777   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
1778   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
1779
1780   // Verify that unless a non-kiosk-app session is in progress, the the device
1781   // immediately reboots.
1782   CreateAutomaticRebootManager(!is_user_logged_in_ ||
1783                                is_logged_in_as_kiosk_app_);
1784
1785   // Verify that if a non-kiosk-app session is in progress, the device does not
1786   // reboot eventually.
1787   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1788                                 is_logged_in_as_kiosk_app_);
1789 }
1790
1791 // Chrome is starting. The uptime limit is set to 6 hours. The current uptime is
1792 // 12 hours.
1793 // Verifies that a reboot is requested and a grace period is started that will
1794 // end after 6 + 24 hours of uptime.
1795 TEST_P(AutomaticRebootManagerTest, StartInUptimeLimitGracePeriod) {
1796   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
1797   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1798
1799   // Verify that the device does not reboot immediately.
1800   CreateAutomaticRebootManager(false);
1801
1802   // Verify that a grace period has started.
1803   VerifyGracePeriod(uptime_limit_);
1804
1805   // Verify that unless a non-kiosk-app session is in progress, the device
1806   // eventually reboots.
1807   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1808                                 is_logged_in_as_kiosk_app_);
1809 }
1810
1811 // Chrome is starting. An update was applied and a reboot became necessary to
1812 // complete the update process after 6 hours of uptime. The current uptime is
1813 // 10 days.
1814 // Verifies that when the policy to automatically reboot after an update is
1815 // enabled, the device reboots immediately if no non-kiosk-app session is in
1816 // progress because the grace period ended after 6 + 24 hours of uptime.
1817 TEST_P(AutomaticRebootManagerTest, StartAfterUpdateGracePeriod) {
1818   SetUpdateStatusNeedReboot();
1819   SetUpdateRebootNeededUptime(base::TimeDelta::FromHours(6));
1820   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
1821   SetRebootAfterUpdate(true, false);
1822
1823   // Verify that unless a non-kiosk-app session is in progress, the device
1824   // reboots immediately.
1825   CreateAutomaticRebootManager(!is_user_logged_in_ ||
1826                                is_logged_in_as_kiosk_app_);
1827
1828   // Verify that if a non-kiosk-app session is in progress, the device does not
1829   // reboot eventually.
1830   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1831                                 is_logged_in_as_kiosk_app_);
1832 }
1833
1834 // Chrome is starting. An update was applied and a reboot became necessary to
1835 // complete the update process after 6 hours of uptime. The current uptime is
1836 // 12 hours.
1837 // Verifies that when the policy to automatically reboot after an update is
1838 // enabled, a reboot is requested and a grace period is started that will end
1839 // after 6 + 24 hours of uptime.
1840 TEST_P(AutomaticRebootManagerTest, StartInUpdateGracePeriod) {
1841   SetUpdateStatusNeedReboot();
1842   SetUpdateRebootNeededUptime(base::TimeDelta::FromHours(6));
1843   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1844   SetRebootAfterUpdate(true, false);
1845
1846   // Verify that the device does not reboot immediately.
1847   CreateAutomaticRebootManager(false);
1848
1849   // Verify that a grace period has started.
1850   VerifyGracePeriod(update_reboot_needed_uptime_);
1851
1852   // Verify that unless a non-kiosk-app session is in progress, the device
1853   // eventually reboots.
1854   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1855                                 is_logged_in_as_kiosk_app_);
1856 }
1857
1858 // Chrome is starting. An update was applied and a reboot became necessary to
1859 // complete the update process after 10 minutes of uptime. The current uptime is
1860 // 20 minutes.
1861 // Verifies that when the policy to automatically reboot after an update is
1862 // enabled, no reboot occurs and a grace period is scheduled to begin after the
1863 // minimum of 1 hour of uptime.
1864 TEST_P(AutomaticRebootManagerTest, StartBeforeUpdateGracePeriod) {
1865   SetUpdateStatusNeedReboot();
1866   SetUpdateRebootNeededUptime(base::TimeDelta::FromMinutes(10));
1867   task_runner_->SetUptime(base::TimeDelta::FromMinutes(20));
1868   SetRebootAfterUpdate(true, false);
1869
1870   // Verify that the device does not reboot immediately.
1871   CreateAutomaticRebootManager(false);
1872
1873   // Verify that a grace period has been scheduled to start in the future.
1874   VerifyGracePeriod(base::TimeDelta::FromHours(1));
1875
1876   // Verify that unless a non-kiosk-app session is in progress, the device
1877   // eventually reboots.
1878   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1879                                 is_logged_in_as_kiosk_app_);
1880 }
1881
1882 // Chrome is starting. An update was applied and a reboot became necessary to
1883 // complete the update process after 6 hours of uptime. The current uptime is
1884 // 10 days.
1885 // Verifies that when the policy to automatically reboot after an update is not
1886 // enabled, no reboot occurs and no grace period is scheduled.
1887 TEST_P(AutomaticRebootManagerTest, StartUpdateNoPolicy) {
1888   SetUpdateStatusNeedReboot();
1889   SetUpdateRebootNeededUptime(base::TimeDelta::FromHours(6));
1890   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
1891
1892   // Verify that the device does not reboot immediately.
1893   CreateAutomaticRebootManager(false);
1894
1895   // Verify that no grace period has started.
1896   VerifyNoGracePeriod();
1897
1898   // Verify that the device does not reboot eventually.
1899   FastForwardUntilNoTasksRemain(false);
1900 }
1901
1902 // Chrome is starting. An update was applied and a reboot became necessary to
1903 // complete the update process but the time at which this happened was lost. The
1904 // current uptime is 10 days.
1905 // Verifies that the current uptime is persisted as the time at which a reboot
1906 // became necessary. Further verifies that when the policy to automatically
1907 // reboot after an update is enabled, a reboot is requested and a grace period
1908 // is started that will end 24 hours from now.
1909 TEST_P(AutomaticRebootManagerTest, StartUpdateTimeLost) {
1910   SetUpdateStatusNeedReboot();
1911   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
1912   SetRebootAfterUpdate(true, false);
1913
1914   // Verify that the device does not reboot immediately.
1915   CreateAutomaticRebootManager(false);
1916
1917   // Verify that the current uptime has been persisted as the time at which a
1918   // reboot became necessary.
1919   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1920       &update_reboot_needed_uptime_));
1921   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1922
1923   // Verify that a grace period has started.
1924   VerifyGracePeriod(update_reboot_needed_uptime_ + uptime_processing_delay_);
1925
1926   // Verify that unless a non-kiosk-app session is in progress, the device
1927   // eventually reboots.
1928   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
1929                                 is_logged_in_as_kiosk_app_);
1930 }
1931
1932 // Chrome is starting. An update was applied and a reboot became necessary to
1933 // complete the update process but the time at which this happened was lost. The
1934 // current uptime is 10 days.
1935 // Verifies that the current uptime is persisted as the time at which a reboot
1936 // became necessary. Further verifies that when the policy to automatically
1937 // reboot after an update is not enabled, no reboot occurs and no grace period
1938 // is scheduled.
1939 TEST_P(AutomaticRebootManagerTest, StartUpdateNoPolicyTimeLost) {
1940   SetUpdateStatusNeedReboot();
1941   task_runner_->SetUptime(base::TimeDelta::FromDays(10));
1942
1943   // Verify that the device does not reboot immediately.
1944   CreateAutomaticRebootManager(false);
1945
1946   // Verify that the current uptime has been persisted as the time at which a
1947   // reboot became necessary.
1948   EXPECT_TRUE(ReadUpdateRebootNeededUptimeFromFile(
1949       &update_reboot_needed_uptime_));
1950   EXPECT_EQ(task_runner_->Uptime(), update_reboot_needed_uptime_);
1951
1952   // Verify that no grace period has started.
1953   VerifyNoGracePeriod();
1954
1955   // Verify that the device does not reboot eventually.
1956   FastForwardUntilNoTasksRemain(false);
1957 }
1958
1959 // Chrome is starting. No update has been applied. The current uptime is
1960 // 12 hours.
1961 // Verifies that no time is persisted as the time at which a reboot became
1962 // necessary. Further verifies that no reboot occurs and no grace period is
1963 // scheduled.
1964 TEST_P(AutomaticRebootManagerTest, StartNoUpdate) {
1965   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1966   SetRebootAfterUpdate(true, false);
1967
1968   // Verify that the device does not reboot immediately.
1969   CreateAutomaticRebootManager(false);
1970
1971   // Verify that no time is persisted as the time at which a reboot became
1972   // necessary.
1973   EXPECT_FALSE(ReadUpdateRebootNeededUptimeFromFile(
1974       &update_reboot_needed_uptime_));
1975
1976   // Verify that no grace period has started.
1977   VerifyNoGracePeriod();
1978
1979   // Verify that the device does not reboot eventually.
1980   FastForwardUntilNoTasksRemain(false);
1981 }
1982
1983 // Chrome is starting. The uptime limit is set to 6 hours. Also, an update was
1984 // applied and a reboot became necessary to complete the update process after
1985 // 8 hours of uptime. The current uptime is 12 hours.
1986 // Verifies that when the policy to automatically reboot after an update is
1987 // enabled, a reboot is requested and a grace period is started that will end
1988 // after 6 + 24 hours of uptime.
1989 TEST_P(AutomaticRebootManagerTest, StartUptimeLimitBeforeUpdate) {
1990   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
1991   SetUpdateStatusNeedReboot();
1992   SetUpdateRebootNeededUptime(base::TimeDelta::FromHours(8));
1993   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
1994   SetRebootAfterUpdate(true, false);
1995
1996   // Verify that the device does not reboot immediately.
1997   CreateAutomaticRebootManager(false);
1998
1999   // Verify that a grace period has started.
2000   VerifyGracePeriod(uptime_limit_);
2001
2002   // Verify that unless a non-kiosk-app session is in progress, the device
2003   // eventually reboots.
2004   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
2005                                 is_logged_in_as_kiosk_app_);
2006 }
2007
2008 // Chrome is starting. The uptime limit is set to 8 hours. Also, an update was
2009 // applied and a reboot became necessary to complete the update process after
2010 // 6 hours of uptime. The current uptime is 12 hours.
2011 // Verifies that when the policy to automatically reboot after an update is
2012 // enabled, a reboot is requested and a grace period is started that will end
2013 // after 6 + 24 hours of uptime.
2014 TEST_P(AutomaticRebootManagerTest, StartUpdateBeforeUptimeLimit) {
2015   SetUptimeLimit(base::TimeDelta::FromHours(8), false);
2016   SetUpdateStatusNeedReboot();
2017   SetUpdateRebootNeededUptime(base::TimeDelta::FromHours(6));
2018   task_runner_->SetUptime(base::TimeDelta::FromHours(12));
2019   SetRebootAfterUpdate(true, false);
2020
2021   // Verify that the device does not reboot immediately.
2022   CreateAutomaticRebootManager(false);
2023
2024   // Verify that a grace period has started.
2025   VerifyGracePeriod(update_reboot_needed_uptime_);
2026
2027   // Verify that unless a non-kiosk-app session is in progress, the device
2028   // eventually reboots.
2029   FastForwardUntilNoTasksRemain(!is_user_logged_in_ ||
2030                                 is_logged_in_as_kiosk_app_);
2031 }
2032
2033 // Chrome is starting. The uptime limit is set to 6 hours. Also, an update was
2034 // applied and a reboot became necessary to complete the update process after
2035 // 6 hours of uptime. The current uptime is not available.
2036 // Verifies that even if the policy to automatically reboot after an update is
2037 // enabled, no reboot occurs and no grace period is scheduled.
2038 TEST_P(AutomaticRebootManagerTest, StartNoUptime) {
2039   SetUptimeLimit(base::TimeDelta::FromHours(6), false);
2040   SetUpdateStatusNeedReboot();
2041   SetUpdateRebootNeededUptime(base::TimeDelta::FromHours(6));
2042   SetRebootAfterUpdate(true, false);
2043
2044   // Verify that the device does not reboot immediately.
2045   CreateAutomaticRebootManager(false);
2046
2047   // Verify that no grace period has started.
2048   VerifyNoGracePeriod();
2049
2050   // Verify that the device does not reboot eventually.
2051   FastForwardUntilNoTasksRemain(false);
2052 }
2053
2054 INSTANTIATE_TEST_CASE_P(
2055     AutomaticRebootManagerTestInstance,
2056     AutomaticRebootManagerTest,
2057     ::testing::Values(
2058         AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_LOGIN_SCREEN,
2059         AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_KIOSK_APP_SESSION,
2060         AUTOMATIC_REBOOT_MANAGER_TEST_SCENARIO_NON_KIOSK_APP_SESSION));
2061
2062 }  // namespace system
2063 }  // namespace chromeos