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