Commit d2ecaf74 authored by Xiaohan Wang's avatar Xiaohan Wang Committed by Commit Bot

base: Clarify that MAIN_THREAD_MOCK_TIME affects base::Time::Now()

When using ScopedTaskEnvironment::NowSource::MAIN_THREAD_MOCK_TIME, both
base::Time::Now() and base::TimeTicks::Now() are affected. However, this
isn't clear in the comment and there's no unit test covering it.

This CL updates the comment and added tests to cover both
base::Time::Now() and base::TimeTicks::Now().

Bug: 917527
Test: New tests added.
Change-Id: Ida9b5f334e12e0bceb9e952f97f0ee9ef01ffc45
Reviewed-on: https://chromium-review.googlesource.com/c/1450402
Commit-Queue: Xiaohan Wang <xhwang@chromium.org>
Commit-Queue: Gabriel Charette <gab@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#628456}
parent 353a1914
...@@ -100,11 +100,12 @@ class ScopedTaskEnvironment { ...@@ -100,11 +100,12 @@ class ScopedTaskEnvironment {
}; };
enum class NowSource { enum class NowSource {
// base::TimeTicks::Now is real time. // base::Time::Now() and base::TimeTicks::Now() are real time.
REAL_TIME, REAL_TIME,
// base::TimeTicks::Now is driven from the main thread's MOCK_TIME. This // base::Time::Now() and base::TimeTicks::Now() are driven from the main
// may alter the order of delayed and non-delayed tasks on other threads. // thread's MOCK_TIME. This may alter the order of delayed and non-delayed
// tasks on other threads.
// //
// Warning some platform APIs are still real time, and don't interact with // Warning some platform APIs are still real time, and don't interact with
// MOCK_TIME as expected, e.g.: // MOCK_TIME as expected, e.g.:
......
...@@ -322,6 +322,28 @@ TEST_F(ScopedTaskEnvironmentTest, FastForwardAdvanceMockClock) { ...@@ -322,6 +322,28 @@ TEST_F(ScopedTaskEnvironmentTest, FastForwardAdvanceMockClock) {
EXPECT_EQ(start_time + kDelay, clock->Now()); EXPECT_EQ(start_time + kDelay, clock->Now());
} }
TEST_F(ScopedTaskEnvironmentTest, FastForwardAdvanceTime) {
constexpr base::TimeDelta kDelay = TimeDelta::FromSeconds(42);
ScopedTaskEnvironment scoped_task_environment(
ScopedTaskEnvironment::MainThreadType::MOCK_TIME,
ScopedTaskEnvironment::NowSource::MAIN_THREAD_MOCK_TIME);
const Time start_time = base::Time::Now();
scoped_task_environment.FastForwardBy(kDelay);
EXPECT_EQ(start_time + kDelay, base::Time::Now());
}
TEST_F(ScopedTaskEnvironmentTest, FastForwardAdvanceTimeTicks) {
constexpr base::TimeDelta kDelay = TimeDelta::FromSeconds(42);
ScopedTaskEnvironment scoped_task_environment(
ScopedTaskEnvironment::MainThreadType::MOCK_TIME,
ScopedTaskEnvironment::NowSource::MAIN_THREAD_MOCK_TIME);
const TimeTicks start_time = base::TimeTicks::Now();
scoped_task_environment.FastForwardBy(kDelay);
EXPECT_EQ(start_time + kDelay, base::TimeTicks::Now());
}
#if defined(OS_WIN) #if defined(OS_WIN)
// Regression test to ensure that ScopedTaskEnvironment enables the MTA in the // Regression test to ensure that ScopedTaskEnvironment enables the MTA in the
// thread pool (so that the test environment matches that of the browser process // thread pool (so that the test environment matches that of the browser process
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment