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 
5 #ifndef BASE_TEST_TEST_PENDING_TASK_H_
6 #define BASE_TEST_TEST_PENDING_TASK_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "base/location.h"
12 #include "base/time/time.h"
13 #include "base/trace_event/trace_event_argument.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 tracked_objects::Location& location,
25                   const Closure& task,
26                   TimeTicks post_time,
27                   TimeDelta delay,
28                   TestNestability nestability);
29   ~TestPendingTask();
30 
31   // Returns post_time + delay.
32   TimeTicks GetTimeToRun() const;
33 
34   // Returns true if this task is nestable and |other| isn't, or if
35   // this task's time to run is strictly earlier than |other|'s time
36   // to run.
37   //
38   // Note that two tasks may both have the same nestability and delay.
39   // In that case, the caller must use some other criterion (probably
40   // the position in some queue) to break the tie.  Conveniently, the
41   // following STL functions already do so:
42   //
43   //   - std::min_element
44   //   - std::stable_sort
45   //
46   // but the following STL functions don't:
47   //
48   //   - std::max_element
49   //   - std::sort.
50   bool ShouldRunBefore(const TestPendingTask& other) const;
51 
52   tracked_objects::Location location;
53   Closure task;
54   TimeTicks post_time;
55   TimeDelta delay;
56   TestNestability nestability;
57 
58   // Functions for using test pending task with tracing, useful in unit
59   // testing.
60   void AsValueInto(base::trace_event::TracedValue* state) const;
61   scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValue() const;
62   std::string ToString() const;
63 };
64 
65 // gtest helpers which allow pretty printing of the tasks, very useful in unit
66 // testing.
67 std::ostream& operator<<(std::ostream& os, const TestPendingTask& task);
68 void PrintTo(const TestPendingTask& task, std::ostream* os);
69 
70 }  // namespace base
71 
72 #endif  // BASE_TEST_TEST_PENDING_TASK_H_
73