Home | History | Annotate | Download | only in test
      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 <string>
      6 
      7 #include "base/test/test_pending_task.h"
      8 
      9 namespace base {
     10 
     11 TestPendingTask::TestPendingTask() : nestability(NESTABLE) {}
     12 
     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) {}
     24 
     25 TestPendingTask::TestPendingTask(const TestPendingTask& other) = default;
     26 
     27 TimeTicks TestPendingTask::GetTimeToRun() const {
     28   return post_time + delay;
     29 }
     30 
     31 bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const {
     32   if (nestability != other.nestability)
     33     return (nestability == NESTABLE);
     34   return GetTimeToRun() < other.GetTimeToRun();
     35 }
     36 
     37 TestPendingTask::~TestPendingTask() {}
     38 
     39 void TestPendingTask::AsValueInto(base::trace_event::TracedValue* state) const {
     40   state->SetInteger("run_at", GetTimeToRun().ToInternalValue());
     41   state->SetString("posting_function", location.ToString());
     42   state->SetInteger("post_time", post_time.ToInternalValue());
     43   state->SetInteger("delay", delay.ToInternalValue());
     44   switch (nestability) {
     45     case NESTABLE:
     46       state->SetString("nestability", "NESTABLE");
     47       break;
     48     case NON_NESTABLE:
     49       state->SetString("nestability", "NON_NESTABLE");
     50       break;
     51   }
     52   state->SetInteger("delay", delay.ToInternalValue());
     53 }
     54 
     55 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
     56 TestPendingTask::AsValue() const {
     57   std::unique_ptr<base::trace_event::TracedValue> state(
     58       new base::trace_event::TracedValue());
     59   AsValueInto(state.get());
     60   return std::move(state);
     61 }
     62 
     63 std::string TestPendingTask::ToString() const {
     64   std::string output("TestPendingTask(");
     65   AsValue()->AppendAsTraceFormat(&output);
     66   output += ")";
     67   return output;
     68 }
     69 
     70 std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) {
     71   PrintTo(task, &os);
     72   return os;
     73 }
     74 
     75 void PrintTo(const TestPendingTask& task, std::ostream* os) {
     76   *os << task.ToString();
     77 }
     78 
     79 }  // namespace base
     80