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

Unified Diff: base/test/test_mock_time_task_runner.h

Issue 2657013002: Introduce ThreadTaskRunnerHandle::OverrideForTesting and TestMockTimeTaskRunner::ScopedContext. (Closed)
Patch Set: fix RecentTabHelperTest crash? Created 3 years, 10 months 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
Index: base/test/test_mock_time_task_runner.h
diff --git a/base/test/test_mock_time_task_runner.h b/base/test/test_mock_time_task_runner.h
index bf945a0b16f983c1bb09b57e22966da2d2aa091d..54ebbdb7a8d6b637ea3d0de64b2f3af138bda0ab 100644
--- a/base/test/test_mock_time_task_runner.h
+++ b/base/test/test_mock_time_task_runner.h
@@ -12,6 +12,7 @@
#include <queue>
#include <vector>
+#include "base/callback_helpers.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h"
@@ -47,6 +48,53 @@ class TickClock;
// that it supports running delayed tasks in the correct temporal order.
class TestMockTimeTaskRunner : public SingleThreadTaskRunner {
public:
+ // Everything that is executed in the scope of a ScopedContext will behave as
+ // though it ran under |scope| (i.e. ThreadTaskRunnerHandle,
+ // RunsTasksOnCurrentThread, etc.). This allows the test body to be all in one
+ // block when multiple TestMockTimeTaskRunners share the main thread. For
+ // example:
+ //
+ // class ExampleFixture {
+ // protected:
+ // DoBarOnFoo() {
+ // DCHECK(foo_task_runner_->RunsOnCurrentThread());
+ // EXPECT_EQ(foo_task_runner_, ThreadTaskRunnerHandle::Get());
+ // DoBar();
+ // }
+ //
+ // // Mock main task runner.
+ // base::MessageLoop message_loop_;
+ // base::ScopedMockTimeMessageLoopTaskRunner main_task_runner_;
+ //
+ // // Mock foo task runner.
+ // scoped_refptr<TestMockTimeTaskRunner> foo_task_runner_ =
+ // new TestMockTimeTaskRunner();
+ // };
+ //
+ // TEST_F(ExampleFixture, DoBarOnFoo) {
+ // DoThingsOnMain();
+ // {
+ // TestMockTimeTaskRunner::ScopedContext scoped_context(
+ // foo_task_runner_.get());
+ // DoBarOnFoo();
+ // }
+ // DoMoreThingsOnMain();
+ // }
+ //
+ class ScopedContext {
+ public:
+ // Note: |scope| is ran until idle as part of this constructor to ensure
+ // that anything which runs in the underlying scope runs after any already
+ // pending tasks (the contrary would break the SequencedTraskRunner
+ // contract).
+ explicit ScopedContext(scoped_refptr<TestMockTimeTaskRunner> scope);
+ ~ScopedContext();
+
+ private:
+ ScopedClosureRunner on_destroy_;
+ DISALLOW_COPY_AND_ASSIGN(ScopedContext);
+ };
+
// Constructs an instance whose virtual time will start at the Unix epoch, and
// whose time ticks will start at zero.
TestMockTimeTaskRunner();

Powered by Google App Engine
This is Rietveld 408576698