null_task_runner.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_NULL_TASK_RUNNER_H_
  5. #define BASE_TEST_NULL_TASK_RUNNER_H_
  6. #include "base/callback.h"
  7. #include "base/compiler_specific.h"
  8. #include "base/task/single_thread_task_runner.h"
  9. namespace base {
  10. // ATTENTION: Prefer SingleThreadTaskEnvironment or TaskEnvironment w/
  11. // ThreadPoolExecutionMode::QUEUED over this class. A NullTaskRunner might seem
  12. // appealing, but not running tasks is under-testing the potential side-effects
  13. // of the code under tests. All tests should be okay if tasks born from their
  14. // actions are run or deleted at a later point.
  15. //
  16. // Helper class for tests that need to provide an implementation of a
  17. // *TaskRunner class but don't actually care about tasks being run.
  18. class NullTaskRunner : public base::SingleThreadTaskRunner {
  19. public:
  20. NullTaskRunner();
  21. NullTaskRunner(const NullTaskRunner&) = delete;
  22. NullTaskRunner& operator=(const NullTaskRunner&) = delete;
  23. bool PostDelayedTask(const Location& from_here,
  24. base::OnceClosure task,
  25. base::TimeDelta delay) override;
  26. bool PostNonNestableDelayedTask(const Location& from_here,
  27. base::OnceClosure task,
  28. base::TimeDelta delay) override;
  29. // Always returns true to avoid triggering DCHECKs.
  30. bool RunsTasksInCurrentSequence() const override;
  31. protected:
  32. ~NullTaskRunner() override;
  33. };
  34. } // namespace base
  35. #endif // BASE_TEST_NULL_TASK_RUNNER_H_