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