blob: 3f2c79dfc9bfd773cc7319f3ac8f4a2ea39fd5cc [file] [log] [blame]
Daniel Eratb8cf9492015-07-06 13:18:13 -06001// 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
25TimeTicks TestPendingTask::GetTimeToRun() const {
26 return post_time + delay;
27}
28
29bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const {
30 if (nestability != other.nestability)
31 return (nestability == NESTABLE);
32 return GetTimeToRun() < other.GetTimeToRun();
33}
34
35TestPendingTask::~TestPendingTask() {}
36
37void TestPendingTask::AsValueInto(base::trace_event::TracedValue* state) const {
38 state->SetInteger("run_at", GetTimeToRun().ToInternalValue());
39 state->SetString("posting_function", location.ToString());
40 state->SetInteger("post_time", post_time.ToInternalValue());
41 state->SetInteger("delay", delay.ToInternalValue());
42 switch (nestability) {
43 case NESTABLE:
44 state->SetString("nestability", "NESTABLE");
45 break;
46 case NON_NESTABLE:
47 state->SetString("nestability", "NON_NESTABLE");
48 break;
49 }
50 state->SetInteger("delay", delay.ToInternalValue());
51}
52
53scoped_refptr<base::trace_event::ConvertableToTraceFormat>
54TestPendingTask::AsValue() const {
55 scoped_refptr<base::trace_event::TracedValue> state =
56 new base::trace_event::TracedValue();
57 AsValueInto(state.get());
58 return state;
59}
60
61std::string TestPendingTask::ToString() const {
62 std::string output("TestPendingTask(");
63 AsValue()->AppendAsTraceFormat(&output);
64 output += ")";
65 return output;
66}
67
68std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) {
69 PrintTo(task, &os);
70 return os;
71}
72
73void PrintTo(const TestPendingTask& task, std::ostream* os) {
74 *os << task.ToString();
75}
76
77} // namespace base