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