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