null_task_runner.h 1.5 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/macros.h"
  9. #include "base/single_thread_task_runner.h"
  10. namespace base {
  11. // ATTENTION: Prefer SingleThreadTaskEnvironment or TaskEnvironment w/
  12. // ThreadPoolExecutionMode::QUEUED over this class. A NullTaskRunner might seem
  13. // appealing, but not running tasks is under-testing the potential side-effects
  14. // of the code under tests. All tests should be okay if tasks born from their
  15. // actions are run or deleted at a later point.
  16. //
  17. // Helper class for tests that need to provide an implementation of a
  18. // *TaskRunner class but don't actually care about tasks being run.
  19. class NullTaskRunner : public base::SingleThreadTaskRunner {
  20. public:
  21. NullTaskRunner();
  22. bool PostDelayedTask(const Location& from_here,
  23. base::OnceClosure task,
  24. base::TimeDelta delay) override;
  25. bool PostNonNestableDelayedTask(const Location& from_here,
  26. base::OnceClosure task,
  27. base::TimeDelta delay) override;
  28. // Always returns true to avoid triggering DCHECKs.
  29. bool RunsTasksInCurrentSequence() const override;
  30. protected:
  31. ~NullTaskRunner() override;
  32. DISALLOW_COPY_AND_ASSIGN(NullTaskRunner);
  33. };
  34. } // namespace base
  35. #endif // BASE_TEST_NULL_TASK_RUNNER_H_