Roll usrcstplib -> r9048.
[chromium-blink-merge.git] / base / test / mock_time_provider.h
blob7c58648ecd229ff825c4af4f5af3c289ce5c65c2
1 // Copyright (c) 2011 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.
5 // TODO(akalin): Change all users of this class to use SimpleTestClock
6 // or SimpleTestTickClock and remove this class.
8 // A helper class used to mock out calls to the static method base::Time::Now.
9 //
10 // Example usage:
12 // typedef base::Time(TimeProvider)();
13 // class StopWatch {
14 // public:
15 // StopWatch(TimeProvider* time_provider);
16 // void Start();
17 // base::TimeDelta Stop();
18 // private:
19 // TimeProvider* time_provider_;
20 // ...
21 // }
23 // Normally, you would instantiate a StopWatch with the real Now function:
25 // StopWatch watch(&base::Time::Now);
27 // But when testing, you want to instantiate it with
28 // MockTimeProvider::StaticNow, which calls an internally mocked out member.
29 // This allows you to set expectations on the Now method. For example:
31 // TEST_F(StopWatchTest, BasicTest) {
32 // InSequence s;
33 // StrictMock<MockTimeProvider> mock_time;
34 // EXPECT_CALL(mock_time, Now())
35 // .WillOnce(Return(Time::FromDoubleT(4)));
36 // EXPECT_CALL(mock_time, Now())
37 // .WillOnce(Return(Time::FromDoubleT(10)));
39 // StopWatch sw(&MockTimeProvider::StaticNow);
40 // sw.Start(); // First call to Now.
41 // TimeDelta elapsed = sw.stop(); // Second call to Now.
42 // ASSERT_EQ(elapsed, TimeDelta::FromSeconds(6));
43 // }
45 #ifndef BASE_TEST_MOCK_TIME_PROVIDER_H_
46 #define BASE_TEST_MOCK_TIME_PROVIDER_H_
48 #include "base/time/time.h"
49 #include "testing/gmock/include/gmock/gmock.h"
51 namespace base {
53 class MockTimeProvider {
54 public:
55 MockTimeProvider();
56 ~MockTimeProvider();
58 MOCK_METHOD0(Now, Time());
60 static Time StaticNow();
62 private:
63 static MockTimeProvider* instance_;
64 DISALLOW_COPY_AND_ASSIGN(MockTimeProvider);
67 } // namespace base
69 #endif // BASE_TEST_MOCK_TIME_PROVIDER_H_