[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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 | |
James Cook | a35a1e2 | 2017-04-08 02:33:08 | [diff] [blame^] | 5 | #include "ash/system/session/logout_confirmation_controller.h" |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 6 | |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
engedy | 10b4843 | 2015-01-13 22:09:47 | [diff] [blame] | 10 | #include "base/test/test_mock_time_task_runner.h" |
gab | 41aedcb1 | 2016-05-11 17:37:01 | [diff] [blame] | 11 | #include "base/threading/thread_task_runner_handle.h" |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 12 | #include "base/time/tick_clock.h" |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace ash { |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 16 | |
| 17 | class LogoutConfirmationControllerTest : public testing::Test { |
| 18 | protected: |
| 19 | LogoutConfirmationControllerTest(); |
dcheng | 222b9c7 | 2015-01-16 00:48:01 | [diff] [blame] | 20 | ~LogoutConfirmationControllerTest() override; |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 21 | |
| 22 | void LogOut(); |
| 23 | |
| 24 | bool log_out_called_; |
| 25 | |
engedy | 10b4843 | 2015-01-13 22:09:47 | [diff] [blame] | 26 | scoped_refptr<base::TestMockTimeTaskRunner> runner_; |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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), |
engedy | 10b4843 | 2015-01-13 22:09:47 | [diff] [blame] | 37 | runner_(new base::TestMockTimeTaskRunner), |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 38 | runner_handle_(runner_), |
| 39 | controller_(base::Bind(&LogoutConfirmationControllerTest::LogOut, |
| 40 | base::Unretained(this))) { |
engedy | 10b4843 | 2015-01-13 22:09:47 | [diff] [blame] | 41 | controller_.SetClockForTesting(runner_->GetMockTickClock()); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 42 | } |
| 43 | |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 44 | LogoutConfirmationControllerTest::~LogoutConfirmationControllerTest() {} |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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) { |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 53 | controller_.ConfirmLogout(runner_->NowTicks()); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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) { |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 61 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 62 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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) { |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 74 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 75 | base::TimeDelta::FromSeconds(30)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 76 | EXPECT_FALSE(log_out_called_); |
| 77 | runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); |
| 78 | EXPECT_FALSE(log_out_called_); |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 79 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 80 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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) { |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 91 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 92 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 93 | EXPECT_FALSE(log_out_called_); |
| 94 | runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); |
| 95 | EXPECT_FALSE(log_out_called_); |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 96 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 97 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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) { |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 105 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 106 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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) { |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 116 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 117 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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) { |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 126 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 127 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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) { |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 138 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 139 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 140 | EXPECT_FALSE(log_out_called_); |
| 141 | controller_.OnDialogClosed(); |
| 142 | runner_->FastForwardUntilNoTasksRemain(); |
| 143 | EXPECT_FALSE(log_out_called_); |
| 144 | |
engedy | 5cae03fa | 2015-02-13 15:17:44 | [diff] [blame] | 145 | controller_.ConfirmLogout(runner_->NowTicks() + |
| 146 | base::TimeDelta::FromSeconds(10)); |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 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 | |
[email protected] | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 154 | } // namespace ash |