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#include <string>
6
7#include "base/test/test_pending_task.h"
8
9namespace base {
10
11TestPendingTask::TestPendingTask() : nestability(NESTABLE) {}
12
13TestPendingTask::TestPendingTask(
14    const tracked_objects::Location& location,
15    const Closure& task,
16    TimeTicks post_time,
17    TimeDelta delay,
18    TestNestability nestability)
19    : location(location),
20      task(task),
21      post_time(post_time),
22      delay(delay),
23      nestability(nestability) {}
24
25TestPendingTask::TestPendingTask(const TestPendingTask& other) = default;
26
27TimeTicks TestPendingTask::GetTimeToRun() const {
28  return post_time + delay;
29}
30
31bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const {
32  if (nestability != other.nestability)
33    return (nestability == NESTABLE);
34  return GetTimeToRun() < other.GetTimeToRun();
35}
36
37TestPendingTask::~TestPendingTask() {}
38
39void TestPendingTask::AsValueInto(base::trace_event::TracedValue* state) const {
40  state->SetInteger("run_at", GetTimeToRun().ToInternalValue());
41  state->SetString("posting_function", location.ToString());
42  state->SetInteger("post_time", post_time.ToInternalValue());
43  state->SetInteger("delay", delay.ToInternalValue());
44  switch (nestability) {
45    case NESTABLE:
46      state->SetString("nestability", "NESTABLE");
47      break;
48    case NON_NESTABLE:
49      state->SetString("nestability", "NON_NESTABLE");
50      break;
51  }
52  state->SetInteger("delay", delay.ToInternalValue());
53}
54
55std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
56TestPendingTask::AsValue() const {
57  std::unique_ptr<base::trace_event::TracedValue> state(
58      new base::trace_event::TracedValue());
59  AsValueInto(state.get());
60  return std::move(state);
61}
62
63std::string TestPendingTask::ToString() const {
64  std::string output("TestPendingTask(");
65  AsValue()->AppendAsTraceFormat(&output);
66  output += ")";
67  return output;
68}
69
70std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) {
71  PrintTo(task, &os);
72  return os;
73}
74
75void PrintTo(const TestPendingTask& task, std::ostream* os) {
76  *os << task.ToString();
77}
78
79}  // namespace base
80