summaryrefslogtreecommitdiffstats
path: root/base/test/mock_time_provider.h
blob: b4932099a1f5fc7c48b4c0690299022ad28ad896 (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
// Copyright (c) 2011 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.

// TODO(akalin): Change all users of this class to use SimpleTestClock
// or SimpleTestTickClock and remove this class.

// A helper class used to mock out calls to the static method base::Time::Now.
//
// Example usage:
//
// typedef base::Time(TimeProvider)();
// class StopWatch {
//  public:
//   StopWatch(TimeProvider* time_provider);
//   void Start();
//   base::TimeDelta Stop();
//  private:
//   TimeProvider* time_provider_;
//   ...
// }
//
// Normally, you would instantiate a StopWatch with the real Now function:
//
// StopWatch watch(&base::Time::Now);
//
// But when testing, you want to instantiate it with
// MockTimeProvider::StaticNow, which calls an internally mocked out member.
// This allows you to set expectations on the Now method. For example:
//
// TEST_F(StopWatchTest, BasicTest) {
//   InSequence s;
//   StrictMock<MockTimeProvider> mock_time;
//   EXPECT_CALL(mock_time, Now())
//       .WillOnce(Return(Time::FromDoubleT(4)));
//   EXPECT_CALL(mock_time, Now())
//       .WillOnce(Return(Time::FromDoubleT(10)));
//
//   StopWatch sw(&MockTimeProvider::StaticNow);
//   sw.Start();  // First call to Now.
//   TimeDelta elapsed = sw.stop();  // Second call to Now.
//   ASSERT_EQ(elapsed, TimeDelta::FromSeconds(6));
// }

#ifndef BASE_TEST_MOCK_TIME_PROVIDER_H_
#define BASE_TEST_MOCK_TIME_PROVIDER_H_

#include "base/time.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace base {

class MockTimeProvider {
 public:
  MockTimeProvider();
  ~MockTimeProvider();

  MOCK_METHOD0(Now, Time());

  static Time StaticNow();

 private:
  static MockTimeProvider* instance_;
  DISALLOW_COPY_AND_ASSIGN(MockTimeProvider);
};

}  // namespace base

#endif  // BASE_TEST_MOCK_TIME_PROVIDER_H_