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