diff options
author | erg <erg@chromium.org> | 2015-01-12 13:38:57 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-12 21:39:57 +0000 |
commit | 42f058496033336073d38ba0a1e36052d5b0475c (patch) | |
tree | 25c98584d9c8448913a974057cc06856cff29dc2 /ash | |
parent | 2f67cfeab4e2f157fc8da9b32a376ef587d483aa (diff) | |
download | chromium_src-42f058496033336073d38ba0a1e36052d5b0475c.zip chromium_src-42f058496033336073d38ba0a1e36052d5b0475c.tar.gz chromium_src-42f058496033336073d38ba0a1e36052d5b0475c.tar.bz2 |
Revert of Add TestMockTimeTaskRunner in base/test. (patchset #5 id:100001 of https://codereview.chromium.org/823143004/)
Reason for revert:
Suspect that this patch broke the chromeos asan bots. It looks like all the AutomaticRebootManagerTestInstance/AutomaticRebootManagerTest tests now leak memory:
https://build.chromium.org/p/chromium.memory/builders/Linux%20Chromium%20OS%20ASan%20LSan%20Tests%20%281%29/builds/5799
Original issue's description:
> Add TestMockTimeTaskRunner in base/test.
>
> Multiple implementations of this functionality exist with slight modifications. This CL consolidates them.
>
> BUG=329911
> TBR=thestig@chromium.org # For base/base.gyp changes.
>
> Committed: https://crrev.com/8d897fb17470c526155b2a9e86752be9a7f33c03
> Cr-Commit-Position: refs/heads/master@{#311091}
TBR=bartfab@chromium.org,phajdan.jr@chromium.org,stevenjb@chromium.org,thestig@chromium.org,engedy@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=329911
Review URL: https://codereview.chromium.org/844353002
Cr-Commit-Position: refs/heads/master@{#311114}
Diffstat (limited to 'ash')
-rw-r--r-- | ash/system/chromeos/session/logout_confirmation_controller_unittest.cc | 166 |
1 files changed, 151 insertions, 15 deletions
diff --git a/ash/system/chromeos/session/logout_confirmation_controller_unittest.cc b/ash/system/chromeos/session/logout_confirmation_controller_unittest.cc index f459d8a..fb9d379 100644 --- a/ash/system/chromeos/session/logout_confirmation_controller_unittest.cc +++ b/ash/system/chromeos/session/logout_confirmation_controller_unittest.cc @@ -4,14 +4,149 @@ #include "ash/system/chromeos/session/logout_confirmation_controller.h" +#include <queue> +#include <utility> +#include <vector> + #include "base/bind.h" #include "base/bind_helpers.h" +#include "base/compiler_specific.h" +#include "base/location.h" #include "base/memory/ref_counted.h" -#include "base/test/test_mock_time_task_runner.h" +#include "base/single_thread_task_runner.h" #include "base/thread_task_runner_handle.h" +#include "base/time/tick_clock.h" #include "testing/gtest/include/gtest/gtest.h" namespace ash { +namespace { + +// A SingleThreadTaskRunner that mocks the current time and allows it to be +// fast-forwarded. TODO(bartfab): Copies of this class exist in several tests. +// Consolidate them (crbug.com/329911). +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_; + + DISALLOW_COPY_AND_ASSIGN(MockTimeSingleThreadTaskRunner); +}; + +// A base::TickClock that uses a MockTimeSingleThreadTaskRunner as the source of +// the current time. +class MockClock : public base::TickClock { + public: + explicit MockClock(scoped_refptr<MockTimeSingleThreadTaskRunner> task_runner); + virtual ~MockClock(); + + // base::TickClock: + virtual base::TimeTicks NowTicks() override; + + private: + scoped_refptr<MockTimeSingleThreadTaskRunner> task_runner_; + + DISALLOW_COPY_AND_ASSIGN(MockClock); +}; + + +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::make_pair(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() { +} + +MockClock::MockClock(scoped_refptr<MockTimeSingleThreadTaskRunner> task_runner) + : task_runner_(task_runner) { +} + +MockClock::~MockClock() { +} + +base::TimeTicks MockClock::NowTicks() { + return task_runner_->GetCurrentTime(); +} + +} // namespace class LogoutConfirmationControllerTest : public testing::Test { protected: @@ -22,7 +157,7 @@ class LogoutConfirmationControllerTest : public testing::Test { bool log_out_called_; - scoped_refptr<base::TestMockTimeTaskRunner> runner_; + scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; base::ThreadTaskRunnerHandle runner_handle_; LogoutConfirmationController controller_; @@ -33,11 +168,12 @@ class LogoutConfirmationControllerTest : public testing::Test { LogoutConfirmationControllerTest::LogoutConfirmationControllerTest() : log_out_called_(false), - runner_(new base::TestMockTimeTaskRunner), + runner_(new MockTimeSingleThreadTaskRunner), runner_handle_(runner_), controller_(base::Bind(&LogoutConfirmationControllerTest::LogOut, base::Unretained(this))) { - controller_.SetClockForTesting(runner_->GetMockTickClock()); + controller_.SetClockForTesting( + scoped_ptr<base::TickClock>(new MockClock(runner_))); } LogoutConfirmationControllerTest::~LogoutConfirmationControllerTest() { @@ -50,7 +186,7 @@ void LogoutConfirmationControllerTest::LogOut() { // Verifies that the user is logged out immediately if logout confirmation with // a zero-length countdown is requested. TEST_F(LogoutConfirmationControllerTest, ZeroDuration) { - controller_.ConfirmLogout(runner_->GetCurrentMockTime()); + controller_.ConfirmLogout(runner_->GetCurrentTime()); EXPECT_FALSE(log_out_called_); runner_->FastForwardBy(base::TimeDelta()); EXPECT_TRUE(log_out_called_); @@ -59,7 +195,7 @@ TEST_F(LogoutConfirmationControllerTest, ZeroDuration) { // Verifies that the user is logged out when the countdown expires. TEST_F(LogoutConfirmationControllerTest, DurationExpired) { controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); EXPECT_FALSE(log_out_called_); runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); EXPECT_FALSE(log_out_called_); @@ -72,12 +208,12 @@ TEST_F(LogoutConfirmationControllerTest, DurationExpired) { // out when the new countdown expires. TEST_F(LogoutConfirmationControllerTest, DurationShortened) { controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(30)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(30)); EXPECT_FALSE(log_out_called_); runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); EXPECT_FALSE(log_out_called_); controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); EXPECT_FALSE(log_out_called_); runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); @@ -89,12 +225,12 @@ TEST_F(LogoutConfirmationControllerTest, DurationShortened) { // out when the original countdown expires. TEST_F(LogoutConfirmationControllerTest, DurationExtended) { controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); EXPECT_FALSE(log_out_called_); runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); EXPECT_FALSE(log_out_called_); controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); EXPECT_TRUE(log_out_called_); } @@ -103,7 +239,7 @@ TEST_F(LogoutConfirmationControllerTest, DurationExtended) { // user is not logged out, even when the original countdown expires. TEST_F(LogoutConfirmationControllerTest, Lock) { controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); EXPECT_FALSE(log_out_called_); controller_.OnLockStateChanged(true); runner_->FastForwardUntilNoTasksRemain(); @@ -114,7 +250,7 @@ TEST_F(LogoutConfirmationControllerTest, Lock) { // out immediately. TEST_F(LogoutConfirmationControllerTest, UserAccepted) { controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); EXPECT_FALSE(log_out_called_); controller_.OnLogoutConfirmed(); EXPECT_TRUE(log_out_called_); @@ -124,7 +260,7 @@ TEST_F(LogoutConfirmationControllerTest, UserAccepted) { // out, even when the original countdown expires. TEST_F(LogoutConfirmationControllerTest, UserDenied) { controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); EXPECT_FALSE(log_out_called_); controller_.OnDialogClosed(); runner_->FastForwardUntilNoTasksRemain(); @@ -136,14 +272,14 @@ TEST_F(LogoutConfirmationControllerTest, UserDenied) { // expires. TEST_F(LogoutConfirmationControllerTest, DurationExpiredAfterDeniedRequest) { controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); EXPECT_FALSE(log_out_called_); controller_.OnDialogClosed(); runner_->FastForwardUntilNoTasksRemain(); EXPECT_FALSE(log_out_called_); controller_.ConfirmLogout( - runner_->GetCurrentMockTime() + base::TimeDelta::FromSeconds(10)); + runner_->GetCurrentTime() + base::TimeDelta::FromSeconds(10)); EXPECT_FALSE(log_out_called_); runner_->FastForwardBy(base::TimeDelta::FromSeconds(9)); EXPECT_FALSE(log_out_called_); |