Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1997)

Unified Diff: base/timer/timer_unittest.cc

Issue 2484023002: Use a TickClock instead of TimeTicks::Now() in Timer. (Closed)
Patch Set: Added unit tests and removed Chrome OS changes. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/timer/timer.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/timer/timer_unittest.cc
diff --git a/base/timer/timer_unittest.cc b/base/timer/timer_unittest.cc
index 6fcd25b93a3af6491be0df785136f28bdf4fe86f..b34da20ba3f1ce4170303054aa1dea668ad15448 100644
--- a/base/timer/timer_unittest.cc
+++ b/base/timer/timer_unittest.cc
@@ -8,11 +8,15 @@
#include <memory>
+#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
+#include "base/test/test_mock_time_task_runner.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
+#include "base/time/tick_clock.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -32,6 +36,17 @@ const base::MessageLoop::Type testing_message_loops[] = {
const int kNumTestingMessageLoops = arraysize(testing_message_loops);
+class Receiver {
+ public:
+ Receiver() : count_(0) {}
+ void OnCalled() { count_++; }
+ bool WasCalled() { return count_ > 0; }
+ int TimesCalled() { return count_; }
+
+ private:
+ int count_;
+};
+
class OneShotTimerTester {
public:
explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10)
@@ -341,6 +356,21 @@ TEST(TimerTest, OneShotTimer_CustomTaskRunner) {
EXPECT_TRUE(did_run);
}
+TEST(TimerTest, OneShotTimerWithTickClock) {
+ scoped_refptr<base::TestMockTimeTaskRunner> task_runner(
+ new base::TestMockTimeTaskRunner(base::Time::Now(),
+ base::TimeTicks::Now()));
+ std::unique_ptr<base::TickClock> tick_clock(task_runner->GetMockTickClock());
+ base::MessageLoop message_loop;
+ message_loop.SetTaskRunner(task_runner);
+ Receiver receiver;
+ base::OneShotTimer timer(tick_clock.get());
+ timer.Start(FROM_HERE, base::TimeDelta::FromSeconds(1),
+ base::Bind(&Receiver::OnCalled, base::Unretained(&receiver)));
+ task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1));
+ EXPECT_TRUE(receiver.WasCalled());
+}
+
TEST(TimerTest, RepeatingTimer) {
for (int i = 0; i < kNumTestingMessageLoops; i++) {
RunTest_RepeatingTimer(testing_message_loops[i],
@@ -369,6 +399,24 @@ TEST(TimerTest, RepeatingTimerZeroDelay_Cancel) {
}
}
+TEST(TimerTest, RepeatingTimerWithTickClock) {
+ scoped_refptr<base::TestMockTimeTaskRunner> task_runner(
+ new base::TestMockTimeTaskRunner(base::Time::Now(),
+ base::TimeTicks::Now()));
+ std::unique_ptr<base::TickClock> tick_clock(task_runner->GetMockTickClock());
+ base::MessageLoop message_loop;
+ message_loop.SetTaskRunner(task_runner);
+ Receiver receiver;
+ const int expected_times_called = 10;
+ base::RepeatingTimer timer(tick_clock.get());
+ timer.Start(FROM_HERE, base::TimeDelta::FromSeconds(1),
+ base::Bind(&Receiver::OnCalled, base::Unretained(&receiver)));
+ task_runner->FastForwardBy(
+ base::TimeDelta::FromSeconds(expected_times_called));
+ timer.Stop();
+ EXPECT_EQ(expected_times_called, receiver.TimesCalled());
+}
+
TEST(TimerTest, DelayTimer_NoCall) {
for (int i = 0; i < kNumTestingMessageLoops; i++) {
RunTest_DelayTimer_NoCall(testing_message_loops[i]);
@@ -394,6 +442,26 @@ TEST(TimerTest, DelayTimer_Deleted) {
}
}
+TEST(TimerTest, DelayTimerWithTickClock) {
+ scoped_refptr<base::TestMockTimeTaskRunner> task_runner(
+ new base::TestMockTimeTaskRunner(base::Time::Now(),
+ base::TimeTicks::Now()));
+ std::unique_ptr<base::TickClock> tick_clock(task_runner->GetMockTickClock());
+ base::MessageLoop message_loop;
+ message_loop.SetTaskRunner(task_runner);
+ Receiver receiver;
+ base::DelayTimer timer(FROM_HERE, base::TimeDelta::FromSeconds(1), &receiver,
+ &Receiver::OnCalled, tick_clock.get());
+ task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(999));
+ EXPECT_FALSE(receiver.WasCalled());
+ timer.Reset();
+ task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(999));
+ EXPECT_FALSE(receiver.WasCalled());
+ timer.Reset();
+ task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1));
+ EXPECT_TRUE(receiver.WasCalled());
+}
+
TEST(TimerTest, MessageLoopShutdown) {
// This test is designed to verify that shutdown of the
// message loop does not cause crashes if there were pending
« no previous file with comments | « base/timer/timer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698