• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TEST_SCOPED_MOCK_TIME_MESSAGE_LOOP_TASK_RUNNER_H_
6 #define BASE_TEST_SCOPED_MOCK_TIME_MESSAGE_LOOP_TASK_RUNNER_H_
7 
8 #include "base/memory/ref_counted.h"
9 #include "base/test/test_mock_time_task_runner.h"
10 
11 namespace base {
12 
13 class SingleThreadTaskRunner;
14 
15 // A scoped wrapper around TestMockTimeTaskRunner that replaces
16 // CurrentThread::Get()'s task runner (and consequently
17 // SingleThreadTaskRunner::GetCurrentDefault) with a TestMockTimeTaskRunner and
18 // resets it back at the end of its scope.
19 //
20 // Note: RunLoop() will not work in the scope of a
21 // ScopedMockTimeMessageLoopTaskRunner, the underlying TestMockTimeTaskRunner's
22 // methods must be used instead to pump tasks.
23 //
24 // Note: Use TaskEnvironment + TimeSource::MOCK_TIME instead of this in unit
25 // tests. In browser tests you unfortunately still need this at the moment to
26 // mock delayed tasks on the main thread...
27 class ScopedMockTimeMessageLoopTaskRunner {
28  public:
29   ScopedMockTimeMessageLoopTaskRunner();
30 
31   ScopedMockTimeMessageLoopTaskRunner(
32       const ScopedMockTimeMessageLoopTaskRunner&) = delete;
33   ScopedMockTimeMessageLoopTaskRunner& operator=(
34       const ScopedMockTimeMessageLoopTaskRunner&) = delete;
35 
36   ~ScopedMockTimeMessageLoopTaskRunner();
37 
task_runner()38   TestMockTimeTaskRunner* task_runner() { return task_runner_.get(); }
39   TestMockTimeTaskRunner* operator->() { return task_runner_.get(); }
40 
41  private:
42   const scoped_refptr<TestMockTimeTaskRunner> task_runner_;
43   scoped_refptr<SingleThreadTaskRunner> previous_task_runner_;
44 };
45 
46 }  // namespace base
47 
48 #endif  // BASE_TEST_SCOPED_MOCK_TIME_MESSAGE_LOOP_TASK_RUNNER_H_
49