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

base / test / test_pending_task.cc [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.

#include "base/test/test_pending_task.h"

#include <string>
#include <utility>

#include "base/trace_event/base_tracing.h"

namespace base {

TestPendingTask::TestPendingTask() : nestability(NESTABLE) {}

TestPendingTask::TestPendingTask(const Location& location,
                                 OnceClosure task,
                                 TimeTicks post_time,
                                 TimeDelta delay,
                                 TestNestability nestability)
    : location(location),
      task(std::move(task)),
      post_time(post_time),
      delay(delay),
      nestability(nestability) {}

TestPendingTask::TestPendingTask(TestPendingTask&& other) = default;

TestPendingTask& TestPendingTask::operator=(TestPendingTask&& other) = default;

TimeTicks TestPendingTask::GetTimeToRun() const {
  return post_time + delay;
}

bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const {
  if (nestability != other.nestability)
    return (nestability == NESTABLE);
  return GetTimeToRun() < other.GetTimeToRun();
}

TestPendingTask::~TestPendingTask() = default;

void TestPendingTask::AsValueInto(base::trace_event::TracedValue* state) const {
  state->SetInteger("run_at", GetTimeToRun().ToInternalValue());
  state->SetString("posting_function", location.ToString());
  state->SetInteger("post_time", post_time.ToInternalValue());
  state->SetInteger("delay", delay.ToInternalValue());
  switch (nestability) {
    case NESTABLE:
      state->SetString("nestability", "NESTABLE");
      break;
    case NON_NESTABLE:
      state->SetString("nestability", "NON_NESTABLE");
      break;
  }
  state->SetInteger("delay", delay.ToInternalValue());
}

std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
TestPendingTask::AsValue() const {
  std::unique_ptr<base::trace_event::TracedValue> state(
      new base::trace_event::TracedValue());
  AsValueInto(state.get());
  return std::move(state);
}

std::string TestPendingTask::ToString() const {
  std::string output("TestPendingTask(");
  AsValue()->AppendAsTraceFormat(&output);
  output += ")";
  return output;
}

std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) {
  PrintTo(task, &os);
  return os;
}

void PrintTo(const TestPendingTask& task, std::ostream* os) {
  *os << task.ToString();
}

}  // namespace base