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..465fee1979e237d7035ab3a7cde14f972e4817b0 |
| --- /dev/null |
| +++ b/ash/system/logout_button/logout_button_tray_unittest.cc |
| @@ -0,0 +1,273 @@ |
| +// 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 "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 { |
|
bartfab (slow)
2013/12/03 19:46:04
This entire class is a copy & paste from another p
binjin
2013/12/04 10:47:03
Can you suggest a proper place to move the shared
bartfab (slow)
2013/12/04 12:51:56
It should probably move to a file ending in test_u
binjin
2013/12/05 10:08:05
I'm not sure if it's okay to use chrome/* classes
bartfab (slow)
2013/12/06 13:50:04
As discussed offline, I agree that having this cod
|
| + 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 MockLogoutConfirmationSettingsProvider |
| + : public LogoutConfirmationSettingsProvider { |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: s/SettingsProvider/Delegate/. This name is mo
binjin
2013/12/05 10:08:05
Done.
|
| + public: |
| + MockLogoutConfirmationSettingsProvider( |
|
bartfab (slow)
2013/12/03 19:46:04
Single-argument constructors should be marked expl
binjin
2013/12/04 10:47:03
Done.
|
| + MockTimeSingleThreadTaskRunner* runner); |
| + |
| + // LogoutConfirmationSettingsProvider |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Add a colon at the end of the line.
binjin
2013/12/04 10:47:03
Done.
|
| + virtual void LogoutCurrentUser(LogoutConfirmationDialogView*) OVERRIDE; |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Add argument name.
binjin
2013/12/04 10:47:03
Done.
|
| + virtual base::TimeTicks GetCurrentTime() OVERRIDE; |
| + |
| + void SetLogoutCalled(bool called); |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: This is never used.
binjin
2013/12/04 10:47:03
Done.
|
| + bool IsLogoutCalled(); |
|
bartfab (slow)
2013/12/03 19:46:04
Nit 1: WasLogoutCalled() would be more correct.
Ni
binjin
2013/12/04 10:47:03
Done.
|
| + |
| + private: |
| + bool logout_called_; |
| + |
| + MockTimeSingleThreadTaskRunner* runner_; |
| +}; |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Add DISALLOW_COPY_AND_ASSIGN(MockLogoutConfir
binjin
2013/12/04 10:47:03
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(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() { |
| +} |
| + |
| +MockLogoutConfirmationSettingsProvider::MockLogoutConfirmationSettingsProvider( |
| + MockTimeSingleThreadTaskRunner* runner) { |
| + runner_ = runner; |
| + logout_called_ = false; |
| +} |
| + |
| +void MockLogoutConfirmationSettingsProvider::LogoutCurrentUser( |
| + LogoutConfirmationDialogView* dialog) { |
| + logout_called_ = true; |
| + if (dialog) |
| + dialog->DeleteDelegate(); |
| +} |
| + |
| +base::TimeTicks MockLogoutConfirmationSettingsProvider::GetCurrentTime() { |
| + return runner_->GetCurrentTime(); |
| +} |
| + |
| +void MockLogoutConfirmationSettingsProvider::SetLogoutCalled(bool called) { |
| + logout_called_ = called; |
| +} |
| + |
| +bool MockLogoutConfirmationSettingsProvider::IsLogoutCalled() { |
| + return logout_called_; |
| +} |
| + |
| +class LogoutConfirmationDialogTest : public testing::Test { |
| + public: |
| + LogoutConfirmationDialogTest() {} |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: You should be consistent with your inlining s
binjin
2013/12/04 10:47:03
Done.
|
| + virtual ~LogoutConfirmationDialogTest() {} |
| + |
| + // testing::Test |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Add a colon at the end of the line.
binjin
2013/12/04 10:47:03
Done.
|
| + virtual void SetUp() OVERRIDE; |
| + virtual void TearDown() OVERRIDE; |
| + |
| + void ChangeDialogDuration(int duration_ms); |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Make the argument a base::TimeDelta.
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + protected: |
| + scoped_ptr<LogoutButtonTray> tray_; |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: s/tray_/button_/ or s/tray_/logout_button_/ w
binjin
2013/12/04 10:47:03
Done.
|
| + scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; |
| + scoped_ptr<MockLogoutConfirmationSettingsProvider> provider_; |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: In line with renaming the class, this member
binjin
2013/12/05 10:08:05
Done.
|
| +}; |
| + |
| +void LogoutConfirmationDialogTest::SetUp() { |
| + runner_ = new MockTimeSingleThreadTaskRunner; |
| + tray_.reset(new LogoutButtonTray(NULL)); |
| + ChangeDialogDuration(20 * 1000); // default value, 20 seconds. |
| + provider_.reset(new MockLogoutConfirmationSettingsProvider(runner_.get())); |
| + LogoutConfirmationDialogView::SetProvider(provider_.get()); |
| +} |
| + |
| +void LogoutConfirmationDialogTest::TearDown() { |
| +} |
| + |
| +void LogoutConfirmationDialogTest::ChangeDialogDuration(int duration_ms) { |
| + tray_->OnLogoutDialogDurationChanged( |
| + base::TimeDelta::FromMilliseconds(duration_ms)); |
| +} |
| + |
| +TEST_F(LogoutConfirmationDialogTest, NoClickWithDefaultValue) { |
| + base::ThreadTaskRunnerHandle runner_handle(runner_); |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Could this be handled by the LogoutConfirmati
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); |
| + |
| + tray_->OnUserLogoutEvent(); |
| + |
| + runner_->FastForwardBy(1 * 1000); // 1 second after start |
|
bartfab (slow)
2013/12/03 19:46:04
Nit 1: Here and elsewhere: Terminate comments with
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_TRUE(tray_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(provider_->IsLogoutCalled()); |
| + |
| + runner_->FastForwardBy(18 * 1000); // 19 seconds after start |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Fast-forwarding by 1s and then 18s is rather
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_TRUE(tray_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(provider_->IsLogoutCalled()); |
| + |
| + runner_->FastForwardBy(2 * 1000); // 21 seconds after start |
| + |
| + EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); |
| + EXPECT_TRUE(provider_->IsLogoutCalled()); |
| +} |
| + |
| +TEST_F(LogoutConfirmationDialogTest, ZeroPreferenceValue) { |
| + base::ThreadTaskRunnerHandle runner_handle(runner_); |
| + |
| + ChangeDialogDuration(0); // set preference to 0 |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: The comment does not really add much.
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); |
| + |
| + tray_->OnUserLogoutEvent(); |
| + |
| + EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); |
| + |
| + runner_->FastForwardBy(1 * 1000); // 1 second |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Same as above: Why not runner_->FastForwardBy
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); |
| + EXPECT_TRUE(provider_->IsLogoutCalled()); |
| +} |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Add runner_->FastForwardUntilNoTasksRemain()
binjin
2013/12/05 10:08:05
I didn't follow you on this.
On 2013/12/03 19:46:
bartfab (slow)
2013/12/06 13:50:04
At the end of the test, you verify that the confir
binjin
2013/12/09 18:50:02
Done.
|
| + |
| +TEST_F(LogoutConfirmationDialogTest, OnTheFlyDialogDurationChange) { |
| + base::ThreadTaskRunnerHandle runner_handle(runner_); |
| + |
| + ChangeDialogDuration(5 * 1000); // set dialog duration to 5 seconds |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Here and elsewhere: If you change the argumen
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); |
| + |
| + tray_->OnUserLogoutEvent(); |
| + |
| + runner_->FastForwardBy(3 * 1000); // 3 seconds after start |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Same as above: Why not runner_->FastForwardBy
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_TRUE(tray_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(provider_->IsLogoutCalled()); |
| + |
| + ChangeDialogDuration(10 * 1000); // change dialog duration to 10 seconds |
| + |
| + runner_->FastForwardBy(6 * 1000); // 9 seconds after start |
| + |
| + EXPECT_TRUE(tray_->IsConfirmationDialogShowing()); |
| + EXPECT_FALSE(provider_->IsLogoutCalled()); |
| + |
| + runner_->FastForwardBy(2 * 1000); // 11 seconds after start |
| + |
| + EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); |
| + EXPECT_TRUE(provider_->IsLogoutCalled()); |
| +} |
| + |
| +TEST_F(LogoutConfirmationDialogTest, UserClickedButton) { |
| + base::ThreadTaskRunnerHandle runner_handle(runner_); |
| + |
| + tray_->OnUserLogoutEvent(); |
| + |
| + runner_->FastForwardBy(3 * 1000); // 3 seconds after start |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Same as above: Why not runner_->FastForwardBy
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_FALSE(provider_->IsLogoutCalled()); |
| + |
| + tray_->GetConfirmationDialog()->Accept(); |
| + |
| + runner_->FastForwardBy(1 * 1000); // 4 seconds after start |
|
bartfab (slow)
2013/12/03 19:46:04
Nit: Same as above: Why not runner_->FastForwardBy
binjin
2013/12/05 10:08:05
Done.
|
| + |
| + EXPECT_TRUE(provider_->IsLogoutCalled()); |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |