Chromium Code Reviews| Index: ash/system/logout_button/logout_button_tray_unittest.cc |
| diff --git a/ash/system/logout_button/logout_button_tray_unittest.cc b/ash/system/logout_button/logout_button_tray_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..707e8497e1be3df9db7a110049cb6bdcb9cc8d07 |
| --- /dev/null |
| +++ b/ash/system/logout_button/logout_button_tray_unittest.cc |
| @@ -0,0 +1,323 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/system/logout_button/logout_button_tray.h" |
| + |
| +#include <queue> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "ash/system/status_area_widget.h" |
| +#include "base/callback.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/events/event.h" |
| +#include "ui/events/keycodes/keyboard_codes.h" |
| +#include "ui/views/controls/button/label_button.h" |
| + |
| +namespace ash { |
| +namespace internal { |
| + |
| +class LogoutConfirmationDialogTest; |
| + |
| +// A SingleThreadTaskRunner that mocks the current time and allows it to be |
| +// fast-forwarded. |
| +class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner { |
| + public: |
| + MockTimeSingleThreadTaskRunner(); |
| + |
| + // base::SingleThreadTaskRunner: |
| + virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
| + virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| + const base::Closure& task, |
| + base::TimeDelta delay) OVERRIDE; |
| + virtual bool PostNonNestableDelayedTask( |
| + const tracked_objects::Location& from_here, |
| + const base::Closure& task, |
| + base::TimeDelta delay) OVERRIDE; |
| + |
| + const base::TimeTicks& GetCurrentTime() const; |
| + |
| + void FastForwardBy(base::TimeDelta delta); |
| + void FastForwardUntilNoTasksRemain(); |
| + |
| + private: |
| + // Strict weak temporal ordering of tasks. |
| + class TemporalOrder { |
| + public: |
| + bool operator()( |
| + const std::pair<base::TimeTicks, base::Closure>& first_task, |
| + const std::pair<base::TimeTicks, base::Closure>& second_task) const; |
| + }; |
| + |
| + virtual ~MockTimeSingleThreadTaskRunner(); |
| + |
| + base::TimeTicks now_; |
| + std::priority_queue<std::pair<base::TimeTicks, base::Closure>, |
| + std::vector<std::pair<base::TimeTicks, base::Closure> >, |
| + TemporalOrder> tasks_; |
| +}; |
|
stevenjb
2013/12/18 21:55:20
It looks like this was copied from session_length_
binjin
2013/12/19 16:30:39
Done.
|
| + |
| +class MockLogoutConfirmationDelegate |
| + : public LogoutConfirmationDialogView::Delegate { |
| + public: |
| + MockLogoutConfirmationDelegate( |
| + scoped_refptr<MockTimeSingleThreadTaskRunner> runner, |
| + LogoutConfirmationDialogTest* tester); |
| + |
| + // LogoutConfirmationDialogView::Delegate: |
| + virtual void LogoutCurrentUser() OVERRIDE; |
| + virtual base::TimeTicks GetCurrentTime() OVERRIDE const; |
| + |
| + bool WasLogoutCalled() const; |
|
stevenjb
2013/12/18 21:55:20
bool logout_called() const { return logout_called_
binjin
2013/12/19 16:30:39
Done.
|
| + |
| + private: |
| + bool logout_called_; |
| + |
| + scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; |
| + LogoutConfirmationDialogTest* tester_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockLogoutConfirmationDelegate); |
| +}; |
| + |
| +class LogoutConfirmationDialogTest : public testing::Test { |
| + public: |
| + LogoutConfirmationDialogTest(); |
| + virtual ~LogoutConfirmationDialogTest(); |
| + |
| + // testing::Test: |
| + virtual void SetUp() OVERRIDE; |
| + |
| + void ChangeDialogDuration(base::TimeDelta duration); |
| + void PressButton(); |
| + void PressDialogButtonYes(); |
| + void CloseDialog(); |
| + |
| + protected: |
| + scoped_ptr<LogoutButtonTray> logout_button_; |
| + scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; |
| + base::ThreadTaskRunnerHandle runner_handle_; |
| + MockLogoutConfirmationDelegate* delegate_; |
|
stevenjb
2013/12/18 21:55:20
nit: DISALLOW...
binjin
2013/12/19 16:30:39
Done.
|
| +}; |
| + |
| +MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner() { |
| +} |
| + |
| +bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const { |
| + return true; |
| +} |
| + |
| +bool MockTimeSingleThreadTaskRunner::PostDelayedTask( |
| + const tracked_objects::Location& from_here, |
| + const base::Closure& task, |
| + base::TimeDelta delay) { |
| + tasks_.push(std::pair<base::TimeTicks, base::Closure>(now_ + delay, task)); |
| + return true; |
| +} |
| + |
| +bool MockTimeSingleThreadTaskRunner::PostNonNestableDelayedTask( |
| + const tracked_objects::Location& from_here, |
| + const base::Closure& task, |
| + base::TimeDelta delay) { |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +const base::TimeTicks& MockTimeSingleThreadTaskRunner::GetCurrentTime() const { |
| + return now_; |
| +} |
| + |
| +void MockTimeSingleThreadTaskRunner::FastForwardBy(base::TimeDelta delta) { |
| + const base::TimeTicks latest = now_ + delta; |
| + while (!tasks_.empty() && tasks_.top().first <= latest) { |
| + now_ = tasks_.top().first; |
| + base::Closure task = tasks_.top().second; |
| + tasks_.pop(); |
| + task.Run(); |
| + } |
| + now_ = latest; |
| +} |
| + |
| +void MockTimeSingleThreadTaskRunner::FastForwardUntilNoTasksRemain() { |
| + while (!tasks_.empty()) { |
| + now_ = tasks_.top().first; |
| + base::Closure task = tasks_.top().second; |
| + tasks_.pop(); |
| + task.Run(); |
| + } |
| +} |
| + |
| +bool MockTimeSingleThreadTaskRunner::TemporalOrder::operator()( |
| + const std::pair<base::TimeTicks, base::Closure>& first_task, |
| + const std::pair<base::TimeTicks, base::Closure>& second_task) const { |
| + return first_task.first > second_task.first; |
| +} |
| + |
| +MockTimeSingleThreadTaskRunner::~MockTimeSingleThreadTaskRunner() { |
| +} |
| + |
| +MockLogoutConfirmationDelegate::MockLogoutConfirmationDelegate( |
| + scoped_refptr<MockTimeSingleThreadTaskRunner> runner, |
| + LogoutConfirmationDialogTest* tester) : runner_(runner), |
|
stevenjb
2013/12/18 21:55:20
: on new line
binjin
2013/12/19 16:30:39
Done.
|
| + tester_(tester) { |
| + logout_called_ = false; |
|
stevenjb
2013/12/18 21:55:20
Set in initializer list
binjin
2013/12/19 16:30:39
Done.
|
| +} |
| + |
| +void MockLogoutConfirmationDelegate::LogoutCurrentUser() { |
| + logout_called_ = true; |
| + tester_->CloseDialog(); |
| +} |
| + |
| +base::TimeTicks MockLogoutConfirmationDelegate::GetCurrentTime() const { |
| + return runner_->GetCurrentTime(); |
| +} |
| + |
| +bool MockLogoutConfirmationDelegate::WasLogoutCalled() const { |
| + return logout_called_; |
| +} |
| + |
| +LogoutConfirmationDialogTest::LogoutConfirmationDialogTest() |
| + : runner_(new MockTimeSingleThreadTaskRunner), |
| + runner_handle_(runner_) { |
| +} |
| + |
| +LogoutConfirmationDialogTest::~LogoutConfirmationDialogTest() { |
| +} |
| + |
| +void LogoutConfirmationDialogTest::PressButton() { |
| + const ui::TranslatedKeyEvent faked_event(false, |
| + static_cast<ui::KeyboardCode>(0), 0); |
|
stevenjb
2013/12/18 21:55:20
align second line with 'false' or put 'false' on s
binjin
2013/12/19 16:30:39
Done.
|
| + logout_button_->ButtonPressed( |
| + reinterpret_cast<views::Button*>(logout_button_->button_), faked_event); |
| +} |
| + |
| +void LogoutConfirmationDialogTest::PressDialogButtonYes() { |
| + logout_button_->confirmation_dialog_->Accept(); |
| + // |confirmation_dialog_| might already be destroyed, if not, manually call |
| + // OnClosed() to simulate real browser environment behavior. |
| + if (logout_button_->confirmation_dialog_) |
| + logout_button_->confirmation_dialog_->OnClosed(); |
| +} |
| + |
| +void LogoutConfirmationDialogTest::CloseDialog() { |
| + if (logout_button_->confirmation_dialog_) |
| + logout_button_->confirmation_dialog_->OnClosed(); |
| +} |
| + |
| +void LogoutConfirmationDialogTest::SetUp() { |
| + logout_button_.reset(new LogoutButtonTray(NULL)); |
| + delegate_ = new MockLogoutConfirmationDelegate(runner_, this); |
| + logout_button_->SetDelegateForTest( |
| + scoped_ptr<LogoutConfirmationDialogView::Delegate>(delegate_)); |
| + ChangeDialogDuration(base::TimeDelta::FromSeconds(20)); |
| +} |
| + |
| +void LogoutConfirmationDialogTest::ChangeDialogDuration( |
| + base::TimeDelta duration) { |
| + logout_button_->OnLogoutDialogDurationChanged(duration); |
| +} |
| + |
| +TEST_F(LogoutConfirmationDialogTest, NoClickWithDefaultValue) { |
| + PressButton(); |
| + |
| + // Verify that the dialog is showing immediately after the logout button was |
| + // pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| + EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| + |
| + // Verify that the dialog is still showing after 19 seconds since the logout |
| + // button was pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(19)); |
| + EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| + |
| + // Verify that the dialog is closed after 21 seconds since the logout button |
| + // was pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
| + EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| +} |
| + |
| +TEST_F(LogoutConfirmationDialogTest, ZeroPreferenceValue) { |
| + ChangeDialogDuration(base::TimeDelta::FromSeconds(0)); |
| + |
| + EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| + |
| + PressButton(); |
| + |
| + // Verify that user was logged out immediately after the logout button was |
| + // pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| + EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| + |
| + runner_->FastForwardUntilNoTasksRemain(); |
| + EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| +} |
| + |
| +TEST_F(LogoutConfirmationDialogTest, OnTheFlyDialogDurationChange) { |
| + ChangeDialogDuration(base::TimeDelta::FromSeconds(5)); |
| + |
| + EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| + |
| + PressButton(); |
| + |
| + // Verify that the dialog is showing immediately after the logout button was |
| + // pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| + EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| + |
| + // Verify that the dialog is still showing after 3 seconds since the logout |
| + // button was pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(3)); |
| + EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| + |
| + // And at this point we change the dialog duration preference. |
| + ChangeDialogDuration(base::TimeDelta::FromSeconds(10)); |
| + |
| + // Verify that the dialog is still showing after 9 seconds since the logout |
| + // button was pressed, with dialog duration preference changed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(6)); |
| + EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| + |
| + // Verify that the dialog is closed after 11 seconds since the logout button |
| + // was pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
| + EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| +} |
| + |
| +TEST_F(LogoutConfirmationDialogTest, UserClickedButton) { |
| + PressButton(); |
| + |
| + // Verify that the dialog is showing immediately after the logout button was |
| + // pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| + EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| + |
| + // Verify that the dialog is still showing after 3 seconds since the logout |
| + // button was pressed. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(3)); |
| + EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| + |
| + // And at this point we click the accept button. |
| + PressDialogButtonYes(); |
| + |
| + // Verify that the user was logged out immediately after the accept button |
| + // was clicked. |
| + runner_->FastForwardBy(base::TimeDelta::FromSeconds(0)); |
| + EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |