• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors
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 "base/test/scoped_mock_clock_override.h"
6 
7 #include <ostream>
8 
9 #include "base/check_op.h"
10 
11 namespace base {
12 
13 ScopedMockClockOverride* ScopedMockClockOverride::scoped_mock_clock_ = nullptr;
14 
ScopedMockClockOverride()15 ScopedMockClockOverride::ScopedMockClockOverride()
16     :  // Start the offset past zero so that it's not treated as a null value.
17       offset_(Days(365)) {
18   DCHECK(!scoped_mock_clock_)
19       << "Nested ScopedMockClockOverrides are not supported.";
20 
21   scoped_mock_clock_ = this;
22 
23   time_clock_overrides_ = std::make_unique<subtle::ScopedTimeClockOverrides>(
24       &ScopedMockClockOverride::Now, &ScopedMockClockOverride::NowTicks,
25       &ScopedMockClockOverride::NowThreadTicks);
26 }
27 
~ScopedMockClockOverride()28 ScopedMockClockOverride::~ScopedMockClockOverride() {
29   scoped_mock_clock_ = nullptr;
30 }
31 
Now()32 Time ScopedMockClockOverride::Now() {
33   return Time() + scoped_mock_clock_->offset_;
34 }
35 
NowTicks()36 TimeTicks ScopedMockClockOverride::NowTicks() {
37   return TimeTicks() + scoped_mock_clock_->offset_;
38 }
39 
NowThreadTicks()40 ThreadTicks ScopedMockClockOverride::NowThreadTicks() {
41   return ThreadTicks() + scoped_mock_clock_->offset_;
42 }
43 
Advance(TimeDelta delta)44 void ScopedMockClockOverride::Advance(TimeDelta delta) {
45   DCHECK_GT(delta, base::TimeDelta())
46       << "Monotonically increasing time may not go backwards";
47   offset_ += delta;
48 }
49 
50 }  // namespace base
51