1 // 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.
5 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/test/test_mock_time_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/time/tick_clock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class LogoutConfirmationControllerTest
: public testing::Test
{
19 LogoutConfirmationControllerTest();
20 ~LogoutConfirmationControllerTest() override
;
26 scoped_refptr
<base::TestMockTimeTaskRunner
> runner_
;
27 base::ThreadTaskRunnerHandle runner_handle_
;
29 LogoutConfirmationController controller_
;
32 DISALLOW_COPY_AND_ASSIGN(LogoutConfirmationControllerTest
);
35 LogoutConfirmationControllerTest::LogoutConfirmationControllerTest()
36 : log_out_called_(false),
37 runner_(new base::TestMockTimeTaskRunner
),
38 runner_handle_(runner_
),
39 controller_(base::Bind(&LogoutConfirmationControllerTest::LogOut
,
40 base::Unretained(this))) {
41 controller_
.SetClockForTesting(runner_
->GetMockTickClock());
44 LogoutConfirmationControllerTest::~LogoutConfirmationControllerTest() {
47 void LogoutConfirmationControllerTest::LogOut() {
48 log_out_called_
= true;
51 // Verifies that the user is logged out immediately if logout confirmation with
52 // a zero-length countdown is requested.
53 TEST_F(LogoutConfirmationControllerTest
, ZeroDuration
) {
54 controller_
.ConfirmLogout(runner_
->NowTicks());
55 EXPECT_FALSE(log_out_called_
);
56 runner_
->FastForwardBy(base::TimeDelta());
57 EXPECT_TRUE(log_out_called_
);
60 // Verifies that the user is logged out when the countdown expires.
61 TEST_F(LogoutConfirmationControllerTest
, DurationExpired
) {
62 controller_
.ConfirmLogout(runner_
->NowTicks() +
63 base::TimeDelta::FromSeconds(10));
64 EXPECT_FALSE(log_out_called_
);
65 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(9));
66 EXPECT_FALSE(log_out_called_
);
67 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(2));
68 EXPECT_TRUE(log_out_called_
);
71 // Verifies that when a second request to confirm logout is made and the second
72 // request's countdown ends before the original request's, the user is logged
73 // out when the new countdown expires.
74 TEST_F(LogoutConfirmationControllerTest
, DurationShortened
) {
75 controller_
.ConfirmLogout(runner_
->NowTicks() +
76 base::TimeDelta::FromSeconds(30));
77 EXPECT_FALSE(log_out_called_
);
78 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(9));
79 EXPECT_FALSE(log_out_called_
);
80 controller_
.ConfirmLogout(runner_
->NowTicks() +
81 base::TimeDelta::FromSeconds(10));
82 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(9));
83 EXPECT_FALSE(log_out_called_
);
84 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(2));
85 EXPECT_TRUE(log_out_called_
);
88 // Verifies that when a second request to confirm logout is made and the second
89 // request's countdown ends after the original request's, the user is logged
90 // out when the original countdown expires.
91 TEST_F(LogoutConfirmationControllerTest
, DurationExtended
) {
92 controller_
.ConfirmLogout(runner_
->NowTicks() +
93 base::TimeDelta::FromSeconds(10));
94 EXPECT_FALSE(log_out_called_
);
95 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(9));
96 EXPECT_FALSE(log_out_called_
);
97 controller_
.ConfirmLogout(runner_
->NowTicks() +
98 base::TimeDelta::FromSeconds(10));
99 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(2));
100 EXPECT_TRUE(log_out_called_
);
103 // Verifies that when the screen is locked while the countdown is running, the
104 // user is not logged out, even when the original countdown expires.
105 TEST_F(LogoutConfirmationControllerTest
, Lock
) {
106 controller_
.ConfirmLogout(runner_
->NowTicks() +
107 base::TimeDelta::FromSeconds(10));
108 EXPECT_FALSE(log_out_called_
);
109 controller_
.OnLockStateChanged(true);
110 runner_
->FastForwardUntilNoTasksRemain();
111 EXPECT_FALSE(log_out_called_
);
114 // Verifies that when the user confirms the logout request, the user is logged
116 TEST_F(LogoutConfirmationControllerTest
, UserAccepted
) {
117 controller_
.ConfirmLogout(runner_
->NowTicks() +
118 base::TimeDelta::FromSeconds(10));
119 EXPECT_FALSE(log_out_called_
);
120 controller_
.OnLogoutConfirmed();
121 EXPECT_TRUE(log_out_called_
);
124 // Verifies that when the user denies the logout request, the user is not logged
125 // out, even when the original countdown expires.
126 TEST_F(LogoutConfirmationControllerTest
, UserDenied
) {
127 controller_
.ConfirmLogout(runner_
->NowTicks() +
128 base::TimeDelta::FromSeconds(10));
129 EXPECT_FALSE(log_out_called_
);
130 controller_
.OnDialogClosed();
131 runner_
->FastForwardUntilNoTasksRemain();
132 EXPECT_FALSE(log_out_called_
);
135 // Verifies that after the user has denied a logout request, a subsequent logout
136 // request is handled correctly and the user is logged out when the countdown
138 TEST_F(LogoutConfirmationControllerTest
, DurationExpiredAfterDeniedRequest
) {
139 controller_
.ConfirmLogout(runner_
->NowTicks() +
140 base::TimeDelta::FromSeconds(10));
141 EXPECT_FALSE(log_out_called_
);
142 controller_
.OnDialogClosed();
143 runner_
->FastForwardUntilNoTasksRemain();
144 EXPECT_FALSE(log_out_called_
);
146 controller_
.ConfirmLogout(runner_
->NowTicks() +
147 base::TimeDelta::FromSeconds(10));
148 EXPECT_FALSE(log_out_called_
);
149 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(9));
150 EXPECT_FALSE(log_out_called_
);
151 runner_
->FastForwardBy(base::TimeDelta::FromSeconds(2));
152 EXPECT_TRUE(log_out_called_
);