OLD | NEW |
(Empty) | |
| 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 "ash/system/logout_button/logout_button_tray.h" |
| 6 |
| 7 #include <queue> |
| 8 #include <utility> |
| 9 #include <vector> |
| 10 |
| 11 #include "ash/system/status_area_widget.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/events/event.h" |
| 19 #include "ui/events/keycodes/keyboard_codes.h" |
| 20 #include "ui/views/controls/button/label_button.h" |
| 21 |
| 22 namespace ash { |
| 23 namespace internal { |
| 24 |
| 25 // A SingleThreadTaskRunner that mocks the current time and allows it to be |
| 26 // fast-forwarded. |
| 27 class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner { |
| 28 public: |
| 29 MockTimeSingleThreadTaskRunner(); |
| 30 |
| 31 // base::SingleThreadTaskRunner: |
| 32 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
| 33 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 34 const base::Closure& task, |
| 35 base::TimeDelta delay) OVERRIDE; |
| 36 virtual bool PostNonNestableDelayedTask( |
| 37 const tracked_objects::Location& from_here, |
| 38 const base::Closure& task, |
| 39 base::TimeDelta delay) OVERRIDE; |
| 40 |
| 41 const base::TimeTicks& GetCurrentTime() const; |
| 42 |
| 43 void FastForwardBy(base::TimeDelta delta); |
| 44 void FastForwardUntilNoTasksRemain(); |
| 45 |
| 46 private: |
| 47 // Strict weak temporal ordering of tasks. |
| 48 class TemporalOrder { |
| 49 public: |
| 50 bool operator()( |
| 51 const std::pair<base::TimeTicks, base::Closure>& first_task, |
| 52 const std::pair<base::TimeTicks, base::Closure>& second_task) const; |
| 53 }; |
| 54 |
| 55 virtual ~MockTimeSingleThreadTaskRunner(); |
| 56 |
| 57 base::TimeTicks now_; |
| 58 std::priority_queue<std::pair<base::TimeTicks, base::Closure>, |
| 59 std::vector<std::pair<base::TimeTicks, base::Closure> >, |
| 60 TemporalOrder> tasks_; |
| 61 }; |
| 62 |
| 63 class MockLogoutConfirmationDelegate |
| 64 : public LogoutConfirmationDialogView::Delegate { |
| 65 public: |
| 66 explicit MockLogoutConfirmationDelegate( |
| 67 scoped_refptr<MockTimeSingleThreadTaskRunner> runner); |
| 68 |
| 69 // LogoutConfirmationDialogView::Delegate: |
| 70 virtual void LogoutCurrentUser() OVERRIDE; |
| 71 virtual base::TimeTicks GetCurrentTime() OVERRIDE const; |
| 72 |
| 73 bool WasLogoutCalled() const; |
| 74 |
| 75 private: |
| 76 bool logout_called_; |
| 77 |
| 78 scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(MockLogoutConfirmationDelegate); |
| 81 }; |
| 82 |
| 83 MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner() { |
| 84 } |
| 85 |
| 86 bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const { |
| 87 return true; |
| 88 } |
| 89 |
| 90 bool MockTimeSingleThreadTaskRunner::PostDelayedTask( |
| 91 const tracked_objects::Location& from_here, |
| 92 const base::Closure& task, |
| 93 base::TimeDelta delay) { |
| 94 tasks_.push(std::pair<base::TimeTicks, base::Closure>(now_ + delay, task)); |
| 95 return true; |
| 96 } |
| 97 |
| 98 bool MockTimeSingleThreadTaskRunner::PostNonNestableDelayedTask( |
| 99 const tracked_objects::Location& from_here, |
| 100 const base::Closure& task, |
| 101 base::TimeDelta delay) { |
| 102 NOTREACHED(); |
| 103 return false; |
| 104 } |
| 105 |
| 106 const base::TimeTicks& MockTimeSingleThreadTaskRunner::GetCurrentTime() const { |
| 107 return now_; |
| 108 } |
| 109 |
| 110 void MockTimeSingleThreadTaskRunner::FastForwardBy(base::TimeDelta delta) { |
| 111 const base::TimeTicks latest = now_ + delta; |
| 112 while (!tasks_.empty() && tasks_.top().first <= latest) { |
| 113 now_ = tasks_.top().first; |
| 114 base::Closure task = tasks_.top().second; |
| 115 tasks_.pop(); |
| 116 task.Run(); |
| 117 } |
| 118 now_ = latest; |
| 119 } |
| 120 |
| 121 void MockTimeSingleThreadTaskRunner::FastForwardUntilNoTasksRemain() { |
| 122 while (!tasks_.empty()) { |
| 123 now_ = tasks_.top().first; |
| 124 base::Closure task = tasks_.top().second; |
| 125 tasks_.pop(); |
| 126 task.Run(); |
| 127 } |
| 128 } |
| 129 |
| 130 bool MockTimeSingleThreadTaskRunner::TemporalOrder::operator()( |
| 131 const std::pair<base::TimeTicks, base::Closure>& first_task, |
| 132 const std::pair<base::TimeTicks, base::Closure>& second_task) const { |
| 133 return first_task.first > second_task.first; |
| 134 } |
| 135 |
| 136 MockTimeSingleThreadTaskRunner::~MockTimeSingleThreadTaskRunner() { |
| 137 } |
| 138 |
| 139 MockLogoutConfirmationDelegate::MockLogoutConfirmationDelegate( |
| 140 scoped_refptr<MockTimeSingleThreadTaskRunner> runner) : runner_(runner) { |
| 141 logout_called_ = false; |
| 142 } |
| 143 |
| 144 void MockLogoutConfirmationDelegate::LogoutCurrentUser() { |
| 145 logout_called_ = true; |
| 146 } |
| 147 |
| 148 base::TimeTicks MockLogoutConfirmationDelegate::GetCurrentTime() const { |
| 149 return runner_->GetCurrentTime(); |
| 150 } |
| 151 |
| 152 bool MockLogoutConfirmationDelegate::WasLogoutCalled() const { |
| 153 return logout_called_; |
| 154 } |
| 155 |
| 156 class LogoutConfirmationDialogTest : public testing::Test { |
| 157 public: |
| 158 LogoutConfirmationDialogTest(); |
| 159 virtual ~LogoutConfirmationDialogTest(); |
| 160 |
| 161 // testing::Test: |
| 162 virtual void SetUp() OVERRIDE; |
| 163 |
| 164 void ChangeDialogDuration(base::TimeDelta duration); |
| 165 void PressButton(); |
| 166 void PressDialogButtonYes(); |
| 167 |
| 168 protected: |
| 169 scoped_ptr<LogoutButtonTray> logout_button_; |
| 170 scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; |
| 171 base::ThreadTaskRunnerHandle runner_handle_; |
| 172 MockLogoutConfirmationDelegate* delegate_; |
| 173 }; |
| 174 |
| 175 LogoutConfirmationDialogTest::LogoutConfirmationDialogTest() |
| 176 : runner_(new MockTimeSingleThreadTaskRunner), |
| 177 runner_handle_(runner_) { |
| 178 } |
| 179 |
| 180 LogoutConfirmationDialogTest::~LogoutConfirmationDialogTest() { |
| 181 } |
| 182 |
| 183 void LogoutConfirmationDialogTest::PressButton() { |
| 184 const ui::TranslatedKeyEvent faked_event(false, |
| 185 static_cast<ui::KeyboardCode>(0), 0); |
| 186 logout_button_->ButtonPressed( |
| 187 reinterpret_cast<views::Button*>(logout_button_->button_), faked_event); |
| 188 } |
| 189 |
| 190 void LogoutConfirmationDialogTest::PressDialogButtonYes() { |
| 191 logout_button_->confirmation_dialog_->Accept(); |
| 192 // |confirmation_dialog_| might already be destroyed, if not, manually call |
| 193 // OnClosed() to simulate real browser environment behavior. |
| 194 if (logout_button_->confirmation_dialog_) |
| 195 logout_button_->confirmation_dialog_->OnClosed(); |
| 196 } |
| 197 |
| 198 void LogoutConfirmationDialogTest::SetUp() { |
| 199 logout_button_.reset(new LogoutButtonTray(NULL)); |
| 200 delegate_ = new MockLogoutConfirmationDelegate(runner_); |
| 201 logout_button_->SetDelegateForTest( |
| 202 scoped_ptr<LogoutConfirmationDialogView::Delegate>(delegate_)); |
| 203 ChangeDialogDuration(base::TimeDelta::FromSeconds(20)); |
| 204 } |
| 205 |
| 206 void LogoutConfirmationDialogTest::ChangeDialogDuration( |
| 207 base::TimeDelta duration) { |
| 208 logout_button_->OnLogoutDialogDurationChanged(duration); |
| 209 } |
| 210 |
| 211 TEST_F(LogoutConfirmationDialogTest, NoClickWithDefaultValue) { |
| 212 PressButton(); |
| 213 |
| 214 // Verify that the dialog is showing immediately after the logout button was |
| 215 // pressed. |
| 216 runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| 217 EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| 218 EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| 219 |
| 220 // Verify that the dialog is still showing after 19 seconds since the logout |
| 221 // button was pressed. |
| 222 runner_->FastForwardBy(base::TimeDelta::FromSeconds(19)); |
| 223 EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| 224 EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| 225 |
| 226 // Verify that the dialog is closed after 21 seconds since the logout button |
| 227 // was pressed. |
| 228 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
| 229 EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| 230 EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| 231 } |
| 232 |
| 233 TEST_F(LogoutConfirmationDialogTest, ZeroPreferenceValue) { |
| 234 ChangeDialogDuration(base::TimeDelta::FromSeconds(0)); |
| 235 |
| 236 EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| 237 |
| 238 PressButton(); |
| 239 |
| 240 // Verify that user was logged out immediately after the logout button was |
| 241 // pressed. |
| 242 runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| 243 EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| 244 EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| 245 |
| 246 runner_->FastForwardUntilNoTasksRemain(); |
| 247 EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| 248 } |
| 249 |
| 250 TEST_F(LogoutConfirmationDialogTest, OnTheFlyDialogDurationChange) { |
| 251 ChangeDialogDuration(base::TimeDelta::FromSeconds(5)); |
| 252 |
| 253 EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| 254 |
| 255 PressButton(); |
| 256 |
| 257 // Verify that the dialog is showing immediately after the logout button was |
| 258 // pressed. |
| 259 runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| 260 EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| 261 EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| 262 |
| 263 // Verify that the dialog is still showing after 3 seconds since the logout |
| 264 // button was pressed. |
| 265 runner_->FastForwardBy(base::TimeDelta::FromSeconds(3)); |
| 266 EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| 267 EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| 268 |
| 269 // And at this point we change the dialog duration preference. |
| 270 ChangeDialogDuration(base::TimeDelta::FromSeconds(10)); |
| 271 |
| 272 // Verify that the dialog is still showing after 9 seconds since the logout |
| 273 // button was pressed, with dialog duration preference changed. |
| 274 runner_->FastForwardBy(base::TimeDelta::FromSeconds(6)); |
| 275 EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| 276 EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| 277 |
| 278 // Verify that the dialog is closed after 11 seconds since the logout button |
| 279 // was pressed. |
| 280 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
| 281 EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| 282 EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| 283 } |
| 284 |
| 285 TEST_F(LogoutConfirmationDialogTest, UserClickedButton) { |
| 286 PressButton(); |
| 287 |
| 288 // Verify that the dialog is showing immediately after the logout button was |
| 289 // pressed. |
| 290 runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| 291 EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| 292 EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| 293 |
| 294 // Verify that the dialog is still showing after 3 seconds since the logout |
| 295 // button was pressed. |
| 296 runner_->FastForwardBy(base::TimeDelta::FromSeconds(3)); |
| 297 EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| 298 EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| 299 |
| 300 // And at this point we click the accept button. |
| 301 PressDialogButtonYes(); |
| 302 |
| 303 // Verify that the user was logged out immediately after the accept button |
| 304 // was clicked. |
| 305 runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| 306 EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| 307 } |
| 308 |
| 309 } // namespace internal |
| 310 } // namespace ash |
OLD | NEW |