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