Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / base / test / test_pending_task.cc
blob3f2c79dfc9bfd773cc7319f3ac8f4a2ea39fd5cc
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.
5 #include <string>
7 #include "base/test/test_pending_task.h"
9 namespace base {
11 TestPendingTask::TestPendingTask() : nestability(NESTABLE) {}
13 TestPendingTask::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) {}
25 TimeTicks TestPendingTask::GetTimeToRun() const {
26 return post_time + delay;
29 bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const {
30 if (nestability != other.nestability)
31 return (nestability == NESTABLE);
32 return GetTimeToRun() < other.GetTimeToRun();
35 TestPendingTask::~TestPendingTask() {}
37 void 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;
50 state->SetInteger("delay", delay.ToInternalValue());
53 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
54 TestPendingTask::AsValue() const {
55 scoped_refptr<base::trace_event::TracedValue> state =
56 new base::trace_event::TracedValue();
57 AsValueInto(state.get());
58 return state;
61 std::string TestPendingTask::ToString() const {
62 std::string output("TestPendingTask(");
63 AsValue()->AppendAsTraceFormat(&output);
64 output += ")";
65 return output;
68 std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) {
69 PrintTo(task, &os);
70 return os;
73 void PrintTo(const TestPendingTask& task, std::ostream* os) {
74 *os << task.ToString();
77 } // namespace base