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..f438225574e5c349ef85ae18198ca15fa3fc59c0 |
--- /dev/null |
+++ b/ash/system/logout_button/logout_button_tray_unittest.cc |
@@ -0,0 +1,301 @@ |
+// 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" |
+#include "ash/system/logout_button/logout_confirmation_dialog_view.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "base/time/time.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+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); |
+ 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_; |
+}; |
+ |
+class MockLogoutConfirmationDelegate |
+ : public LogoutConfirmationDialogView::LogoutConfirmationDelegate { |
+ public: |
+ explicit MockLogoutConfirmationDelegate( |
+ MockTimeSingleThreadTaskRunner* runner); |
+ |
+ // LogoutConfirmationDialogView::LogoutConfirmationDelegate: |
+ virtual void LogoutCurrentUser() OVERRIDE; |
+ virtual base::TimeTicks GetCurrentTime() OVERRIDE; |
+ |
+ 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(); |
+ 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() { |
+ 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_; |
+ base::ThreadTaskRunnerHandle runner_handle_; |
+ MockLogoutConfirmationDelegate* delegate_; |
+}; |
+ |
+LogoutConfirmationDialogTest::LogoutConfirmationDialogTest() |
+ : runner_(new MockTimeSingleThreadTaskRunner), |
+ runner_handle_(runner_) { |
+} |
+ |
+LogoutConfirmationDialogTest::~LogoutConfirmationDialogTest() { |
+} |
+ |
+void LogoutConfirmationDialogTest::PressButton() { |
+ ui::TranslatedKeyEvent faked_event(false, |
+ static_cast<ui::KeyboardCode>(0), 0); |
+ logout_button_->ButtonPressed( |
+ reinterpret_cast<views::Button*>(logout_button_->button_), faked_event); |
+} |
+ |
+void LogoutConfirmationDialogTest::SetUp() { |
+ logout_button_.reset(new LogoutButtonTray(NULL, |
+ new MockLogoutConfirmationDelegate(runner_.get()))); |
+ ChangeDialogDuration(base::TimeDelta::FromSeconds(20)); |
+ delegate_ = static_cast<MockLogoutConfirmationDelegate*>( |
+ logout_button_->GetConfirmationDelegateForTest()); |
+} |
+ |
+void LogoutConfirmationDialogTest::TearDown() { |
+} |
+ |
+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()); |
+} |
+ |
+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(); |
+ |
+ // 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 |