blob: 70ebeb5c80fdd9128dc8993e9a71acff6861a051 [file] [log] [blame]
[email protected]7b3f91ee2014-03-06 11:41:401// Copyright 2014 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
James Cooka35a1e22017-04-08 02:33:085#include "ash/system/session/logout_confirmation_controller.h"
[email protected]7b3f91ee2014-03-06 11:41:406
[email protected]7b3f91ee2014-03-06 11:41:407#include "base/bind.h"
8#include "base/bind_helpers.h"
[email protected]7b3f91ee2014-03-06 11:41:409#include "base/memory/ref_counted.h"
engedy10b48432015-01-13 22:09:4710#include "base/test/test_mock_time_task_runner.h"
gab41aedcb12016-05-11 17:37:0111#include "base/threading/thread_task_runner_handle.h"
engedy5cae03fa2015-02-13 15:17:4412#include "base/time/tick_clock.h"
[email protected]7b3f91ee2014-03-06 11:41:4013#include "testing/gtest/include/gtest/gtest.h"
14
15namespace ash {
[email protected]7b3f91ee2014-03-06 11:41:4016
17class LogoutConfirmationControllerTest : public testing::Test {
18 protected:
19 LogoutConfirmationControllerTest();
dcheng222b9c72015-01-16 00:48:0120 ~LogoutConfirmationControllerTest() override;
[email protected]7b3f91ee2014-03-06 11:41:4021
22 void LogOut();
23
24 bool log_out_called_;
25
engedy10b48432015-01-13 22:09:4726 scoped_refptr<base::TestMockTimeTaskRunner> runner_;
[email protected]7b3f91ee2014-03-06 11:41:4027 base::ThreadTaskRunnerHandle runner_handle_;
28
29 LogoutConfirmationController controller_;
30
31 private:
32 DISALLOW_COPY_AND_ASSIGN(LogoutConfirmationControllerTest);
33};
34
35LogoutConfirmationControllerTest::LogoutConfirmationControllerTest()
36 : log_out_called_(false),
engedy10b48432015-01-13 22:09:4737 runner_(new base::TestMockTimeTaskRunner),
[email protected]7b3f91ee2014-03-06 11:41:4038 runner_handle_(runner_),
39 controller_(base::Bind(&LogoutConfirmationControllerTest::LogOut,
40 base::Unretained(this))) {
engedy10b48432015-01-13 22:09:4741 controller_.SetClockForTesting(runner_->GetMockTickClock());
[email protected]7b3f91ee2014-03-06 11:41:4042}
43
jamescookb8dcef522016-06-25 14:42:5544LogoutConfirmationControllerTest::~LogoutConfirmationControllerTest() {}
[email protected]7b3f91ee2014-03-06 11:41:4045
46void LogoutConfirmationControllerTest::LogOut() {
47 log_out_called_ = true;
48}
49
50// Verifies that the user is logged out immediately if logout confirmation with
51// a zero-length countdown is requested.
52TEST_F(LogoutConfirmationControllerTest, ZeroDuration) {
engedy5cae03fa2015-02-13 15:17:4453 controller_.ConfirmLogout(runner_->NowTicks());
[email protected]7b3f91ee2014-03-06 11:41:4054 EXPECT_FALSE(log_out_called_);
55 runner_->FastForwardBy(base::TimeDelta());
56 EXPECT_TRUE(log_out_called_);
57}
58
59// Verifies that the user is logged out when the countdown expires.
60TEST_F(LogoutConfirmationControllerTest, DurationExpired) {
engedy5cae03fa2015-02-13 15:17:4461 controller_.ConfirmLogout(runner_->NowTicks() +
62 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:4063 EXPECT_FALSE(log_out_called_);
64 runner_->FastForwardBy(base::TimeDelta::FromSeconds(9));
65 EXPECT_FALSE(log_out_called_);
66 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2));
67 EXPECT_TRUE(log_out_called_);
68}
69
70// Verifies that when a second request to confirm logout is made and the second
71// request's countdown ends before the original request's, the user is logged
72// out when the new countdown expires.
73TEST_F(LogoutConfirmationControllerTest, DurationShortened) {
engedy5cae03fa2015-02-13 15:17:4474 controller_.ConfirmLogout(runner_->NowTicks() +
75 base::TimeDelta::FromSeconds(30));
[email protected]7b3f91ee2014-03-06 11:41:4076 EXPECT_FALSE(log_out_called_);
77 runner_->FastForwardBy(base::TimeDelta::FromSeconds(9));
78 EXPECT_FALSE(log_out_called_);
engedy5cae03fa2015-02-13 15:17:4479 controller_.ConfirmLogout(runner_->NowTicks() +
80 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:4081 runner_->FastForwardBy(base::TimeDelta::FromSeconds(9));
82 EXPECT_FALSE(log_out_called_);
83 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2));
84 EXPECT_TRUE(log_out_called_);
85}
86
87// Verifies that when a second request to confirm logout is made and the second
88// request's countdown ends after the original request's, the user is logged
89// out when the original countdown expires.
90TEST_F(LogoutConfirmationControllerTest, DurationExtended) {
engedy5cae03fa2015-02-13 15:17:4491 controller_.ConfirmLogout(runner_->NowTicks() +
92 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:4093 EXPECT_FALSE(log_out_called_);
94 runner_->FastForwardBy(base::TimeDelta::FromSeconds(9));
95 EXPECT_FALSE(log_out_called_);
engedy5cae03fa2015-02-13 15:17:4496 controller_.ConfirmLogout(runner_->NowTicks() +
97 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:4098 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2));
99 EXPECT_TRUE(log_out_called_);
100}
101
102// Verifies that when the screen is locked while the countdown is running, the
103// user is not logged out, even when the original countdown expires.
104TEST_F(LogoutConfirmationControllerTest, Lock) {
engedy5cae03fa2015-02-13 15:17:44105 controller_.ConfirmLogout(runner_->NowTicks() +
106 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:40107 EXPECT_FALSE(log_out_called_);
108 controller_.OnLockStateChanged(true);
109 runner_->FastForwardUntilNoTasksRemain();
110 EXPECT_FALSE(log_out_called_);
111}
112
113// Verifies that when the user confirms the logout request, the user is logged
114// out immediately.
115TEST_F(LogoutConfirmationControllerTest, UserAccepted) {
engedy5cae03fa2015-02-13 15:17:44116 controller_.ConfirmLogout(runner_->NowTicks() +
117 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:40118 EXPECT_FALSE(log_out_called_);
119 controller_.OnLogoutConfirmed();
120 EXPECT_TRUE(log_out_called_);
121}
122
123// Verifies that when the user denies the logout request, the user is not logged
124// out, even when the original countdown expires.
125TEST_F(LogoutConfirmationControllerTest, UserDenied) {
engedy5cae03fa2015-02-13 15:17:44126 controller_.ConfirmLogout(runner_->NowTicks() +
127 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:40128 EXPECT_FALSE(log_out_called_);
129 controller_.OnDialogClosed();
130 runner_->FastForwardUntilNoTasksRemain();
131 EXPECT_FALSE(log_out_called_);
132}
133
134// Verifies that after the user has denied a logout request, a subsequent logout
135// request is handled correctly and the user is logged out when the countdown
136// expires.
137TEST_F(LogoutConfirmationControllerTest, DurationExpiredAfterDeniedRequest) {
engedy5cae03fa2015-02-13 15:17:44138 controller_.ConfirmLogout(runner_->NowTicks() +
139 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:40140 EXPECT_FALSE(log_out_called_);
141 controller_.OnDialogClosed();
142 runner_->FastForwardUntilNoTasksRemain();
143 EXPECT_FALSE(log_out_called_);
144
engedy5cae03fa2015-02-13 15:17:44145 controller_.ConfirmLogout(runner_->NowTicks() +
146 base::TimeDelta::FromSeconds(10));
[email protected]7b3f91ee2014-03-06 11:41:40147 EXPECT_FALSE(log_out_called_);
148 runner_->FastForwardBy(base::TimeDelta::FromSeconds(9));
149 EXPECT_FALSE(log_out_called_);
150 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2));
151 EXPECT_TRUE(log_out_called_);
152}
153
[email protected]7b3f91ee2014-03-06 11:41:40154} // namespace ash