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