OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/common/system/chromeos/session/logout_confirmation_controller.h" | |
6 | |
7 #include "base/bind.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/threading/thread_task_runner_handle.h" | |
12 #include "base/time/tick_clock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace ash { | |
16 | |
17 class LogoutConfirmationControllerTest : public testing::Test { | |
18 protected: | |
19 LogoutConfirmationControllerTest(); | |
20 ~LogoutConfirmationControllerTest() override; | |
21 | |
22 void LogOut(); | |
23 | |
24 bool log_out_called_; | |
25 | |
26 scoped_refptr<base::TestMockTimeTaskRunner> runner_; | |
27 base::ThreadTaskRunnerHandle runner_handle_; | |
28 | |
29 LogoutConfirmationController controller_; | |
30 | |
31 private: | |
32 DISALLOW_COPY_AND_ASSIGN(LogoutConfirmationControllerTest); | |
33 }; | |
34 | |
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()); | |
42 } | |
43 | |
44 LogoutConfirmationControllerTest::~LogoutConfirmationControllerTest() {} | |
45 | |
46 void 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. | |
52 TEST_F(LogoutConfirmationControllerTest, ZeroDuration) { | |
53 controller_.ConfirmLogout(runner_->NowTicks()); | |
54 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. | |
60 TEST_F(LogoutConfirmationControllerTest, DurationExpired) { | |
61 controller_.ConfirmLogout(runner_->NowTicks() + | |
62 base::TimeDelta::FromSeconds(10)); | |
63 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. | |
73 TEST_F(LogoutConfirmationControllerTest, DurationShortened) { | |
74 controller_.ConfirmLogout(runner_->NowTicks() + | |
75 base::TimeDelta::FromSeconds(30)); | |
76 EXPECT_FALSE(log_out_called_); | |
77 runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); | |
78 EXPECT_FALSE(log_out_called_); | |
79 controller_.ConfirmLogout(runner_->NowTicks() + | |
80 base::TimeDelta::FromSeconds(10)); | |
81 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. | |
90 TEST_F(LogoutConfirmationControllerTest, DurationExtended) { | |
91 controller_.ConfirmLogout(runner_->NowTicks() + | |
92 base::TimeDelta::FromSeconds(10)); | |
93 EXPECT_FALSE(log_out_called_); | |
94 runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); | |
95 EXPECT_FALSE(log_out_called_); | |
96 controller_.ConfirmLogout(runner_->NowTicks() + | |
97 base::TimeDelta::FromSeconds(10)); | |
98 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. | |
104 TEST_F(LogoutConfirmationControllerTest, Lock) { | |
105 controller_.ConfirmLogout(runner_->NowTicks() + | |
106 base::TimeDelta::FromSeconds(10)); | |
107 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. | |
115 TEST_F(LogoutConfirmationControllerTest, UserAccepted) { | |
116 controller_.ConfirmLogout(runner_->NowTicks() + | |
117 base::TimeDelta::FromSeconds(10)); | |
118 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. | |
125 TEST_F(LogoutConfirmationControllerTest, UserDenied) { | |
126 controller_.ConfirmLogout(runner_->NowTicks() + | |
127 base::TimeDelta::FromSeconds(10)); | |
128 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. | |
137 TEST_F(LogoutConfirmationControllerTest, DurationExpiredAfterDeniedRequest) { | |
138 controller_.ConfirmLogout(runner_->NowTicks() + | |
139 base::TimeDelta::FromSeconds(10)); | |
140 EXPECT_FALSE(log_out_called_); | |
141 controller_.OnDialogClosed(); | |
142 runner_->FastForwardUntilNoTasksRemain(); | |
143 EXPECT_FALSE(log_out_called_); | |
144 | |
145 controller_.ConfirmLogout(runner_->NowTicks() + | |
146 base::TimeDelta::FromSeconds(10)); | |
147 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 | |
154 } // namespace ash | |
OLD | NEW |