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 TimeTicks TestPendingTask::GetTimeToRun() const {
     26   return post_time + delay;
     27 }
     28 
     29 bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const {
     30   if (nestability != other.nestability)
     31     return (nestability == NESTABLE);
     32   return GetTimeToRun() < other.GetTimeToRun();
     33 }
     34 
     35 TestPendingTask::~TestPendingTask() {}
     36 
     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;
     49   }
     50   state->SetInteger("delay", delay.ToInternalValue());
     51 }
     52 
     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;
     59 }
     60 
     61 std::string TestPendingTask::ToString() const {
     62   std::string output("TestPendingTask(");
     63   AsValue()->AppendAsTraceFormat(&output);
     64   output += ")";
     65   return output;
     66 }
     67 
     68 std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) {
     69   PrintTo(task, &os);
     70   return os;
     71 }
     72 
     73 void PrintTo(const TestPendingTask& task, std::ostream* os) {
     74   *os << task.ToString();
     75 }
     76 
     77 }  // namespace base
     78