summaryrefslogtreecommitdiffstats
path: root/cc/test/test_now_source.cc
blob: e1a633f166283fb0592687894a047333e3daf772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <limits>
#include <string>

#include "cc/test/test_now_source.h"

namespace cc {

// TestNowSource::Constructors
scoped_refptr<TestNowSource> TestNowSource::Create() {
  return make_scoped_refptr(new TestNowSource());
}

scoped_refptr<TestNowSource> TestNowSource::Create(base::TimeTicks initial) {
  return make_scoped_refptr(new TestNowSource(initial));
}

scoped_refptr<TestNowSource> TestNowSource::Create(int64_t initial) {
  return make_scoped_refptr(new TestNowSource(initial));
}

TestNowSource::TestNowSource()
    : initial_(base::TimeTicks::FromInternalValue(10000)),
      now_(),
      num_now_calls_(0) {
  Reset();
}

TestNowSource::TestNowSource(base::TimeTicks initial)
    : initial_(initial), now_(), num_now_calls_(0) {
  Reset();
}

TestNowSource::TestNowSource(int64_t initial)
    : initial_(base::TimeTicks::FromInternalValue(initial)),
      now_(),
      num_now_calls_(0) {
  Reset();
}

TestNowSource::~TestNowSource() {
}

// TestNowSource actual functionality
void TestNowSource::Reset() {
  TRACE_EVENT_INSTANT2("cc",
                       "TestNowSource::Reset",
                       TRACE_EVENT_SCOPE_THREAD,
                       "previous",
                       now_,
                       "initial",
                       initial_);
  now_ = initial_;
}

base::TimeTicks TestNowSource::Now() const {
  num_now_calls_++;
  return now_;
}

void TestNowSource::SetNow(base::TimeTicks time) {
  TRACE_EVENT_INSTANT2("cc",
                       "TestNowSource::SetNow",
                       TRACE_EVENT_SCOPE_THREAD,
                       "previous",
                       now_,
                       "new",
                       time);
  DCHECK(time >= now_);  // Time should always go forward.
  now_ = time;
}

void TestNowSource::AdvanceNow(base::TimeDelta period) {
  TRACE_EVENT_INSTANT2("cc",
                       "TestNowSource::AdvanceNow",
                       TRACE_EVENT_SCOPE_THREAD,
                       "previous",
                       now_,
                       "by",
                       period.ToInternalValue());
  DCHECK(now_ != kAbsoluteMaxNow);
  DCHECK(period >= base::TimeDelta());  // Time should always go forward.
  now_ += period;
}

const base::TimeTicks TestNowSource::kAbsoluteMaxNow =
    base::TimeTicks::FromInternalValue(std::numeric_limits<int64_t>::max());

// TestNowSource::Convenience functions
void TestNowSource::AdvanceNowMicroseconds(int64_t period_in_microseconds) {
  AdvanceNow(base::TimeDelta::FromMicroseconds(period_in_microseconds));
}
void TestNowSource::SetNowMicroseconds(int64_t time_in_microseconds) {
  SetNow(base::TimeTicks::FromInternalValue(time_in_microseconds));
}

// TestNowSource::Tracing functions
void TestNowSource::AsValueInto(base::trace_event::TracedValue* state) const {
  state->SetInteger("now_in_microseconds", now_.ToInternalValue());
}

scoped_refptr<base::trace_event::ConvertableToTraceFormat>
TestNowSource::AsValue() const {
  scoped_refptr<base::trace_event::TracedValue> state =
      new base::trace_event::TracedValue();
  AsValueInto(state.get());
  return state;
}

// TestNowSource::Pretty printing functions
std::string TestNowSource::ToString() const {
  std::string output("TestNowSource(");
  AsValue()->AppendAsTraceFormat(&output);
  output += ")";
  return output;
}

::std::ostream& operator<<(::std::ostream& os,
                           const scoped_refptr<TestNowSource>& src) {
  os << src->ToString();
  return os;
}
void PrintTo(const scoped_refptr<TestNowSource>& src, ::std::ostream* os) {
  *os << src;
}

}  // namespace cc