• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Location & location,OnceClosure task,TimeTicks post_time,TimeDelta delay,TestNestability nestability)14 TestPendingTask::TestPendingTask(const 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 
39 TestPendingTask::~TestPendingTask() = default;
40 
41 // Unsupported in libchrome.
42 #if 0
43 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>
60 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 #endif
67 
ToString() const68 std::string TestPendingTask::ToString() const {
69   std::string output("TestPendingTask(");
70 // Unsupported in libchrome.
71 #if 0
72   AsValue()->AppendAsTraceFormat(&output);
73 #endif
74   output += ")";
75   return output;
76 }
77 
operator <<(std::ostream & os,const TestPendingTask & task)78 std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) {
79   PrintTo(task, &os);
80   return os;
81 }
82 
PrintTo(const TestPendingTask & task,std::ostream * os)83 void PrintTo(const TestPendingTask& task, std::ostream* os) {
84   *os << task.ToString();
85 }
86 
87 }  // namespace base
88