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..58bd5306ed861287d3e1607c8cc73e9ab29bab60 |
| --- /dev/null |
| +++ b/ash/system/logout_button/logout_button_tray_unittest.cc |
| @@ -0,0 +1,303 @@ |
| +// 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 "ash/system/logout_button/logout_button_observer.h" |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: Already included by logout_button_tray.h.
binjin
2013/12/17 14:58:08
Done.
|
| +#include "ash/system/logout_button/logout_confirmation_dialog_view.h" |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: Already included by logout_button_tray.h.
binjin
2013/12/17 14:58:08
Done.
|
| +#include "base/memory/scoped_ptr.h" |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: Already included by logout_button_tray.h.
binjin
2013/12/17 14:58:08
Done.
|
| +#include "base/single_thread_task_runner.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "base/time/time.h" |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: Already included by logout_button_tray.h.
binjin
2013/12/17 14:58:08
Done.
|
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: The unittest file should have includes for al
binjin
2013/12/17 14:58:08
Done.
|
| +namespace ash { |
| +namespace internal { |
| + |
| +// 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(int64 milliseconds); |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: I know you inherited this from my code in its
binjin
2013/12/17 14:58:08
Done.
|
| + 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>, |
|
bartfab (slow)
2013/12/17 13:21:03
Nit 1: #include <queue>
Nit 2: #include <util>
Nit
binjin
2013/12/17 14:58:08
Done.
|
| + std::vector<std::pair<base::TimeTicks, base::Closure> >, |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: #include <vector>
binjin
2013/12/17 14:58:08
Done.
|
| + TemporalOrder> tasks_; |
| +}; |
| + |
| +class MockLogoutConfirmationDelegate |
| + : public LogoutConfirmationDialogView::Delegate { |
| + public: |
| + explicit MockLogoutConfirmationDelegate( |
| + MockTimeSingleThreadTaskRunner* runner); |
|
bartfab (slow)
2013/12/17 13:21:03
Why not use a scoped_refptr to ensure the |runner|
binjin
2013/12/17 14:58:08
Done.
|
| + |
| + // LogoutConfirmationDialogView::Delegate: |
| + virtual void LogoutCurrentUser() OVERRIDE; |
| + virtual base::TimeTicks GetCurrentTime() OVERRIDE const; |
| + |
| + bool WasLogoutCalled() const; |
| + |
| + private: |
| + bool logout_called_; |
| + |
| + MockTimeSingleThreadTaskRunner* runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockLogoutConfirmationDelegate); |
| +}; |
| + |
| +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(); |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: #include "base/logging.h"
binjin
2013/12/17 14:58:08
Done.
|
| + return false; |
| +} |
| + |
| +const base::TimeTicks& MockTimeSingleThreadTaskRunner::GetCurrentTime() const { |
| + return now_; |
| +} |
| + |
| +void MockTimeSingleThreadTaskRunner::FastForwardBy(int64 delta) { |
| + const base::TimeTicks latest = |
| + now_ + base::TimeDelta::FromMilliseconds(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( |
| + MockTimeSingleThreadTaskRunner* runner) { |
| + runner_ = runner; |
| + logout_called_ = false; |
| +} |
| + |
| +void MockLogoutConfirmationDelegate::LogoutCurrentUser() { |
| + logout_called_ = true; |
| +} |
| + |
| +base::TimeTicks MockLogoutConfirmationDelegate::GetCurrentTime() const { |
| + return runner_->GetCurrentTime(); |
| +} |
| + |
| +bool MockLogoutConfirmationDelegate::WasLogoutCalled() const { |
| + return logout_called_; |
| +} |
| + |
| +class LogoutConfirmationDialogTest : public testing::Test { |
| + public: |
| + LogoutConfirmationDialogTest(); |
| + virtual ~LogoutConfirmationDialogTest(); |
| + |
| + // testing::Test: |
| + virtual void SetUp() OVERRIDE; |
| + virtual void TearDown() OVERRIDE; |
| + |
| + void ChangeDialogDuration(base::TimeDelta duration); |
| + void PressButton(); |
| + |
| + protected: |
| + scoped_ptr<LogoutButtonTray> logout_button_; |
| + scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: #include "base/memory/ref_counted.h"
binjin
2013/12/17 14:58:08
Done.
|
| + base::ThreadTaskRunnerHandle runner_handle_; |
| + MockLogoutConfirmationDelegate* delegate_; |
| +}; |
| + |
| +LogoutConfirmationDialogTest::LogoutConfirmationDialogTest() |
| + : runner_(new MockTimeSingleThreadTaskRunner), |
| + runner_handle_(runner_) { |
| +} |
| + |
| +LogoutConfirmationDialogTest::~LogoutConfirmationDialogTest() { |
| +} |
| + |
| +void LogoutConfirmationDialogTest::PressButton() { |
| + ui::TranslatedKeyEvent faked_event(false, |
|
bartfab (slow)
2013/12/17 13:21:03
Nit 1: #include "ui/events/event.h"
Nit 2: const
binjin
2013/12/17 14:58:08
Done.
|
| + static_cast<ui::KeyboardCode>(0), 0); |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: #include "ui/events/keycodes/keyboard_codes.h
binjin
2013/12/17 14:58:08
Done.
|
| + logout_button_->ButtonPressed( |
| + reinterpret_cast<views::Button*>(logout_button_->button_), faked_event); |
| +} |
| + |
| +void LogoutConfirmationDialogTest::SetUp() { |
| + logout_button_.reset(new LogoutButtonTray(NULL)); |
| + delegate_ = new MockLogoutConfirmationDelegate(runner_.get()); |
| + logout_button_->SetDelegateForTest( |
| + scoped_ptr<LogoutConfirmationDialogView::Delegate>(delegate_)); |
| + ChangeDialogDuration(base::TimeDelta::FromSeconds(20)); |
| + delegate_ = static_cast<MockLogoutConfirmationDelegate*>( |
|
bartfab (slow)
2013/12/17 13:21:03
Why is this necessary? The |delegate_| you get bac
binjin
2013/12/17 14:58:08
Done.
|
| + logout_button_->GetConfirmationDelegateForTest()); |
| +} |
| + |
| +void LogoutConfirmationDialogTest::TearDown() { |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: If your TearDown() is empty, there is no need
binjin
2013/12/17 14:58:08
Done.
|
| +} |
| + |
| +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(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(19 * 1000); |
| + 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(2 * 1000); |
| + 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(0); |
| + EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| + |
| + runner_->FastForwardUntilNoTasksRemain(); |
| + EXPECT_FALSE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_TRUE(delegate_->WasLogoutCalled()); |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: This is a no-op because WasLogoutCalled() ret
binjin
2013/12/17 14:58:08
Done.
|
| +} |
| + |
| +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(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(3 * 1000); |
| + 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(6 * 1000); |
| + 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(2 * 1000); |
| + 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(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(3 * 1000); |
| + EXPECT_TRUE(logout_button_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(delegate_->WasLogoutCalled()); |
| + |
| + // And at this point we click the accept button. |
| + logout_button_->GetConfirmationDialogForTest()->Accept(); |
|
bartfab (slow)
2013/12/17 13:21:03
Nit: This is the only use of GetConfirmationDialog
binjin
2013/12/17 14:58:08
Done.
|
| + |
| + // Verify that the user was logged out immediately after the accept button |
| + // was clicked. |
| + runner_->FastForwardBy(0); |
| + EXPECT_TRUE(delegate_->WasLogoutCalled()); |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |