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