Setup a experiment to enable background tracing.
[chromium-blink-merge.git] / cc / test / test_now_source.cc
blobe1a633f166283fb0592687894a047333e3daf772
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.
5 #include <limits>
6 #include <string>
8 #include "cc/test/test_now_source.h"
10 namespace cc {
12 // TestNowSource::Constructors
13 scoped_refptr<TestNowSource> TestNowSource::Create() {
14 return make_scoped_refptr(new TestNowSource());
17 scoped_refptr<TestNowSource> TestNowSource::Create(base::TimeTicks initial) {
18 return make_scoped_refptr(new TestNowSource(initial));
21 scoped_refptr<TestNowSource> TestNowSource::Create(int64_t initial) {
22 return make_scoped_refptr(new TestNowSource(initial));
25 TestNowSource::TestNowSource()
26 : initial_(base::TimeTicks::FromInternalValue(10000)),
27 now_(),
28 num_now_calls_(0) {
29 Reset();
32 TestNowSource::TestNowSource(base::TimeTicks initial)
33 : initial_(initial), now_(), num_now_calls_(0) {
34 Reset();
37 TestNowSource::TestNowSource(int64_t initial)
38 : initial_(base::TimeTicks::FromInternalValue(initial)),
39 now_(),
40 num_now_calls_(0) {
41 Reset();
44 TestNowSource::~TestNowSource() {
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_;
59 base::TimeTicks TestNowSource::Now() const {
60 num_now_calls_++;
61 return now_;
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;
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;
89 const base::TimeTicks TestNowSource::kAbsoluteMaxNow =
90 base::TimeTicks::FromInternalValue(std::numeric_limits<int64_t>::max());
92 // TestNowSource::Convenience functions
93 void TestNowSource::AdvanceNowMicroseconds(int64_t period_in_microseconds) {
94 AdvanceNow(base::TimeDelta::FromMicroseconds(period_in_microseconds));
96 void TestNowSource::SetNowMicroseconds(int64_t time_in_microseconds) {
97 SetNow(base::TimeTicks::FromInternalValue(time_in_microseconds));
100 // TestNowSource::Tracing functions
101 void TestNowSource::AsValueInto(base::trace_event::TracedValue* state) const {
102 state->SetInteger("now_in_microseconds", now_.ToInternalValue());
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;
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;
121 ::std::ostream& operator<<(::std::ostream& os,
122 const scoped_refptr<TestNowSource>& src) {
123 os << src->ToString();
124 return os;
126 void PrintTo(const scoped_refptr<TestNowSource>& src, ::std::ostream* os) {
127 *os << src;
130 } // namespace cc