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