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