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
10namespace cc {
11
12// TestNowSource::Constructors
13scoped_refptr<TestNowSource> TestNowSource::Create() {
14  return make_scoped_refptr(new TestNowSource());
15}
16
17scoped_refptr<TestNowSource> TestNowSource::Create(base::TimeTicks initial) {
18  return make_scoped_refptr(new TestNowSource(initial));
19}
20
21scoped_refptr<TestNowSource> TestNowSource::Create(int64_t initial) {
22  return make_scoped_refptr(new TestNowSource(initial));
23}
24
25TestNowSource::TestNowSource()
26    : initial_(base::TimeTicks::FromInternalValue(10000)), now_() {
27  Reset();
28}
29
30TestNowSource::TestNowSource(base::TimeTicks initial)
31    : initial_(initial), now_() {
32  Reset();
33}
34
35TestNowSource::TestNowSource(int64_t initial)
36    : initial_(base::TimeTicks::FromInternalValue(initial)), now_() {
37  Reset();
38}
39
40TestNowSource::~TestNowSource() {
41}
42
43// TestNowSource actual functionality
44void 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
55base::TimeTicks TestNowSource::Now() const {
56  return now_;
57}
58
59void 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
71void 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
84const base::TimeTicks TestNowSource::kAbsoluteMaxNow =
85    base::TimeTicks::FromInternalValue(std::numeric_limits<int64_t>::max());
86
87// TestNowSource::Convenience functions
88void TestNowSource::AdvanceNowMicroseconds(int64_t period_in_microseconds) {
89  AdvanceNow(base::TimeDelta::FromMicroseconds(period_in_microseconds));
90}
91void TestNowSource::SetNowMicroseconds(int64_t time_in_microseconds) {
92  SetNow(base::TimeTicks::FromInternalValue(time_in_microseconds));
93}
94
95// TestNowSource::Tracing functions
96void TestNowSource::AsValueInto(base::debug::TracedValue* state) const {
97  state->SetInteger("now_in_microseconds", now_.ToInternalValue());
98}
99
100scoped_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
109std::string TestNowSource::ToString() const {
110  std::string output("TestNowSource(");
111  AsValue()->AppendAsTraceFormat(&output);
112  output += ")";
113  return output;
114}
115
116::std::ostream& operator<<(::std::ostream& os,
117                           const scoped_refptr<TestNowSource>& src) {
118  os << src->ToString();
119  return os;
120}
121void PrintTo(const scoped_refptr<TestNowSource>& src, ::std::ostream* os) {
122  *os << src;
123}
124
125}  // namespace cc
126