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.
12 // typedef base::Time(TimeProvider)();
15 // StopWatch(TimeProvider* time_provider);
17 // base::TimeDelta Stop();
19 // TimeProvider* time_provider_;
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) {
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));
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"
53 class MockTimeProvider
{
58 MOCK_METHOD0(Now
, Time());
60 static Time
StaticNow();
63 static MockTimeProvider
* instance_
;
64 DISALLOW_COPY_AND_ASSIGN(MockTimeProvider
);
69 #endif // BASE_TEST_MOCK_TIME_PROVIDER_H_