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

base / test / scoped_mock_clock_override.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/scoped_mock_clock_override.h"

#include <ostream>

#include "base/check_op.h"

namespace base {

ScopedMockClockOverride* ScopedMockClockOverride::scoped_mock_clock_ = nullptr;

ScopedMockClockOverride::ScopedMockClockOverride()
    :  // Start the offset past zero so that it's not treated as a null value.
      offset_(Days(365)) {
  DCHECK(!scoped_mock_clock_)
      << "Nested ScopedMockClockOverrides are not supported.";

  scoped_mock_clock_ = this;

  time_clock_overrides_ = std::make_unique<subtle::ScopedTimeClockOverrides>(
      &ScopedMockClockOverride::Now, &ScopedMockClockOverride::NowTicks,
      &ScopedMockClockOverride::NowThreadTicks);
}

ScopedMockClockOverride::~ScopedMockClockOverride() {
  scoped_mock_clock_ = nullptr;
}

Time ScopedMockClockOverride::Now() {
  return Time() + scoped_mock_clock_->offset_;
}

TimeTicks ScopedMockClockOverride::NowTicks() {
  return TimeTicks() + scoped_mock_clock_->offset_;
}

ThreadTicks ScopedMockClockOverride::NowThreadTicks() {
  return ThreadTicks() + scoped_mock_clock_->offset_;
}

void ScopedMockClockOverride::Advance(TimeDelta delta) {
  DCHECK_GT(delta, base::TimeDelta())
      << "Monotonically increasing time may not go backwards";
  offset_ += delta;
}

}  // namespace base