1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84

base / test / test_pending_task.h [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_TEST_TEST_PENDING_TASK_H_
#define BASE_TEST_TEST_PENDING_TASK_H_

#include <string>

#include "base/functional/callback.h"
#include "base/location.h"
#include "base/time/time.h"

namespace base {

namespace trace_event {
class TracedValue;
class ConvertableToTraceFormat;
}  // namespace trace_event

// TestPendingTask is a helper class for test TaskRunner
// implementations.  See test_simple_task_runner.h for example usage.

struct TestPendingTask {
  enum TestNestability { NESTABLE, NON_NESTABLE };

  TestPendingTask();
  TestPendingTask(const Location& location,
                  OnceClosure task,
                  TimeTicks post_time,
                  TimeDelta delay,
                  TestNestability nestability);

  TestPendingTask(const TestPendingTask&) = delete;
  TestPendingTask& operator=(const TestPendingTask&) = delete;

  TestPendingTask(TestPendingTask&& other);

  ~TestPendingTask();

  TestPendingTask& operator=(TestPendingTask&& other);

  // Returns post_time + delay.
  TimeTicks GetTimeToRun() const;

  // Returns true if this task is nestable and |other| isn't, or if
  // this task's time to run is strictly earlier than |other|'s time
  // to run.
  //
  // Note that two tasks may both have the same nestability and delay.
  // In that case, the caller must use some other criterion (probably
  // the position in some queue) to break the tie.  Conveniently, the
  // following STL functions already do so:
  //
  //   - std::min_element
  //   - std::stable_sort
  //
  // but the following STL functions don't:
  //
  //   - std::max_element
  //   - std::sort.
  bool ShouldRunBefore(const TestPendingTask& other) const;

  Location location;
  OnceClosure task;
  TimeTicks post_time;
  TimeDelta delay;
  TestNestability nestability;

  // Functions for using test pending task with tracing, useful in unit
  // testing.
  void AsValueInto(base::trace_event::TracedValue* state) const;
  std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() const;
  std::string ToString() const;
};

// gtest helpers which allow pretty printing of the tasks, very useful in unit
// testing.
std::ostream& operator<<(std::ostream& os, const TestPendingTask& task);
void PrintTo(const TestPendingTask& task, std::ostream* os);

}  // namespace base

#endif  // BASE_TEST_TEST_PENDING_TASK_H_