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