• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 <limits>
6 #include <string>
7 
8 #include "cc/test/test_now_source.h"
9 
10 namespace cc {
11 
12 // TestNowSource::Constructors
Create()13 scoped_refptr<TestNowSource> TestNowSource::Create() {
14   return make_scoped_refptr(new TestNowSource());
15 }
16 
Create(base::TimeTicks initial)17 scoped_refptr<TestNowSource> TestNowSource::Create(base::TimeTicks initial) {
18   return make_scoped_refptr(new TestNowSource(initial));
19 }
20 
Create(int64_t initial)21 scoped_refptr<TestNowSource> TestNowSource::Create(int64_t initial) {
22   return make_scoped_refptr(new TestNowSource(initial));
23 }
24 
TestNowSource()25 TestNowSource::TestNowSource()
26     : initial_(base::TimeTicks::FromInternalValue(10000)), now_() {
27   Reset();
28 }
29 
TestNowSource(base::TimeTicks initial)30 TestNowSource::TestNowSource(base::TimeTicks initial)
31     : initial_(initial), now_() {
32   Reset();
33 }
34 
TestNowSource(int64_t initial)35 TestNowSource::TestNowSource(int64_t initial)
36     : initial_(base::TimeTicks::FromInternalValue(initial)), now_() {
37   Reset();
38 }
39 
~TestNowSource()40 TestNowSource::~TestNowSource() {
41 }
42 
43 // TestNowSource actual functionality
Reset()44 void TestNowSource::Reset() {
45   TRACE_EVENT_INSTANT2("cc",
46                        "TestNowSource::Reset",
47                        TRACE_EVENT_SCOPE_THREAD,
48                        "previous",
49                        now_,
50                        "initial",
51                        initial_);
52   now_ = initial_;
53 }
54 
Now() const55 base::TimeTicks TestNowSource::Now() const {
56   return now_;
57 }
58 
SetNow(base::TimeTicks time)59 void TestNowSource::SetNow(base::TimeTicks time) {
60   TRACE_EVENT_INSTANT2("cc",
61                        "TestNowSource::SetNow",
62                        TRACE_EVENT_SCOPE_THREAD,
63                        "previous",
64                        now_,
65                        "new",
66                        time);
67   DCHECK(time >= now_);  // Time should always go forward.
68   now_ = time;
69 }
70 
AdvanceNow(base::TimeDelta period)71 void TestNowSource::AdvanceNow(base::TimeDelta period) {
72   TRACE_EVENT_INSTANT2("cc",
73                        "TestNowSource::AdvanceNow",
74                        TRACE_EVENT_SCOPE_THREAD,
75                        "previous",
76                        now_,
77                        "by",
78                        period.ToInternalValue());
79   DCHECK(now_ != kAbsoluteMaxNow);
80   DCHECK(period >= base::TimeDelta());  // Time should always go forward.
81   now_ += period;
82 }
83 
84 const base::TimeTicks TestNowSource::kAbsoluteMaxNow =
85     base::TimeTicks::FromInternalValue(std::numeric_limits<int64_t>::max());
86 
87 // TestNowSource::Convenience functions
AdvanceNowMicroseconds(int64_t period_in_microseconds)88 void TestNowSource::AdvanceNowMicroseconds(int64_t period_in_microseconds) {
89   AdvanceNow(base::TimeDelta::FromMicroseconds(period_in_microseconds));
90 }
SetNowMicroseconds(int64_t time_in_microseconds)91 void TestNowSource::SetNowMicroseconds(int64_t time_in_microseconds) {
92   SetNow(base::TimeTicks::FromInternalValue(time_in_microseconds));
93 }
94 
95 // TestNowSource::Tracing functions
AsValueInto(base::debug::TracedValue * state) const96 void TestNowSource::AsValueInto(base::debug::TracedValue* state) const {
97   state->SetInteger("now_in_microseconds", now_.ToInternalValue());
98 }
99 
AsValue() const100 scoped_refptr<base::debug::ConvertableToTraceFormat> TestNowSource::AsValue()
101     const {
102   scoped_refptr<base::debug::TracedValue> state =
103       new base::debug::TracedValue();
104   AsValueInto(state.get());
105   return state;
106 }
107 
108 // TestNowSource::Pretty printing functions
ToString() const109 std::string TestNowSource::ToString() const {
110   std::string output("TestNowSource(");
111   AsValue()->AppendAsTraceFormat(&output);
112   output += ")";
113   return output;
114 }
115 
operator <<(::std::ostream & os,const scoped_refptr<TestNowSource> & src)116 ::std::ostream& operator<<(::std::ostream& os,
117                            const scoped_refptr<TestNowSource>& src) {
118   os << src->ToString();
119   return os;
120 }
PrintTo(const scoped_refptr<TestNowSource> & src,::std::ostream * os)121 void PrintTo(const scoped_refptr<TestNowSource>& src, ::std::ostream* os) {
122   *os << src;
123 }
124 
125 }  // namespace cc
126