test_pending_task.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_TEST_PENDING_TASK_H_
  5. #define BASE_TEST_TEST_PENDING_TASK_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/location.h"
  9. #include "base/time/time.h"
  10. #include "base/trace_event/base_tracing.h"
  11. namespace base {
  12. // TestPendingTask is a helper class for test TaskRunner
  13. // implementations. See test_simple_task_runner.h for example usage.
  14. struct TestPendingTask {
  15. enum TestNestability { NESTABLE, NON_NESTABLE };
  16. TestPendingTask();
  17. TestPendingTask(TestPendingTask&& other);
  18. TestPendingTask(const Location& location,
  19. OnceClosure task,
  20. TimeTicks post_time,
  21. TimeDelta delay,
  22. TestNestability nestability);
  23. ~TestPendingTask();
  24. TestPendingTask& operator=(TestPendingTask&& other);
  25. // Returns post_time + delay.
  26. TimeTicks GetTimeToRun() const;
  27. // Returns true if this task is nestable and |other| isn't, or if
  28. // this task's time to run is strictly earlier than |other|'s time
  29. // to run.
  30. //
  31. // Note that two tasks may both have the same nestability and delay.
  32. // In that case, the caller must use some other criterion (probably
  33. // the position in some queue) to break the tie. Conveniently, the
  34. // following STL functions already do so:
  35. //
  36. // - std::min_element
  37. // - std::stable_sort
  38. //
  39. // but the following STL functions don't:
  40. //
  41. // - std::max_element
  42. // - std::sort.
  43. bool ShouldRunBefore(const TestPendingTask& other) const;
  44. Location location;
  45. OnceClosure task;
  46. TimeTicks post_time;
  47. TimeDelta delay;
  48. TestNestability nestability;
  49. // Functions for using test pending task with tracing, useful in unit
  50. // testing.
  51. void AsValueInto(base::trace_event::TracedValue* state) const;
  52. std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() const;
  53. std::string ToString() const;
  54. private:
  55. DISALLOW_COPY_AND_ASSIGN(TestPendingTask);
  56. };
  57. // gtest helpers which allow pretty printing of the tasks, very useful in unit
  58. // testing.
  59. std::ostream& operator<<(std::ostream& os, const TestPendingTask& task);
  60. void PrintTo(const TestPendingTask& task, std::ostream* os);
  61. } // namespace base
  62. #endif // BASE_TEST_TEST_PENDING_TASK_H_