// Copyright 2017 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef BASE_TEST_TASK_ENVIRONMENT_H_ #define BASE_TEST_TASK_ENVIRONMENT_H_ #include <memory> #include "base/compiler_specific.h" #include "base/functional/callback_forward.h" #include "base/memory/raw_ptr.h" #include "base/memory/ref_counted.h" #include "base/observer_list_types.h" #include "base/run_loop.h" #include "base/task/lazy_thread_pool_task_runner.h" #include "base/task/sequence_manager/sequence_manager.h" #include "base/task/sequence_manager/task_queue.h" #include "base/task/single_thread_task_runner.h" #include "base/test/scoped_run_loop_timeout.h" #include "base/threading/thread_checker.h" #include "base/time/time.h" #include "base/traits_bag.h" #include "build/build_config.h" namespace base { class Clock; class FileDescriptorWatcher; class TickClock; namespace subtle { class ScopedTimeClockOverrides; } namespace test { // This header exposes SingleThreadTaskEnvironment and TaskEnvironment. // // SingleThreadTaskEnvironment enables the following APIs within its scope: // - (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle on the main // thread // - RunLoop on the main thread // // TaskEnvironment additionally enables: // - posting to base::ThreadPool through base/task/thread_pool.h. // // Hint: For content::BrowserThreads, use content::BrowserTaskEnvironment. // // Tests should prefer SingleThreadTaskEnvironment over TaskEnvironment when the // former is sufficient. // // Tasks posted to the (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle // run synchronously when RunLoop::Run(UntilIdle) or // TaskEnvironment::RunUntil(Idle|Quit) is called on the main thread. // // The TaskEnvironment requires TestTimeouts::Initialize() to be called in order // to run posted tasks, so that it can watch for problematic long-running tasks. // // The TimeSource trait can be used to request that delayed tasks be under the // manual control of RunLoop::Run() and TaskEnvironment::FastForward*() methods. // // If a TaskEnvironment's ThreadPoolExecutionMode is QUEUED, ThreadPool tasks // run when RunUntilIdle(), RunUntilQuit(), or ~TaskEnvironment is called. If // ThreadPoolExecutionMode is ASYNC, they run as they are posted. // // All TaskEnvironment methods must be called from the main thread. // // Usage: // // class MyTestFixture : public testing::Test { // public: // (...) // // // protected rather than private visibility will allow controlling the // // task environment (e.g. RunUntilIdle(), FastForwardBy(), etc.). from the // // test body. // protected: // // Must generally be the first member to be initialized first and // // destroyed last (some members that require single-threaded // // initialization and tear down may need to come before -- e.g. // // base::test::ScopedFeatureList). Extra traits, like TimeSource, are // // best provided inline when declaring the TaskEnvironment, as // // such: // base::test::TaskEnvironment task_environment_{ // base::test::TaskEnvironment::TimeSource::MOCK_TIME}; // // // Other members go here (or further below in private section.) // }; class TaskEnvironment { … }; // SingleThreadTaskEnvironment takes the same traits as TaskEnvironment and is // used the exact same way. It's a short-form for // TaskEnvironment{TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY, ...}; class SingleThreadTaskEnvironment : public TaskEnvironment { … }; } // namespace test } // namespace base #endif // BASE_TEST_TASK_ENVIRONMENT_H_