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. 4 5 // TODO(akalin): Change all users of this class to use SimpleTestClock 6 // or SimpleTestTickClock and remove this class. 7 8 // A helper class used to mock out calls to the static method base::Time::Now. 9 // 10 // Example usage: 11 // 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 // } 22 // 23 // Normally, you would instantiate a StopWatch with the real Now function: 24 // 25 // StopWatch watch(&base::Time::Now); 26 // 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: 30 // 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))); 38 // 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 // } 44 45 #ifndef BASE_TEST_MOCK_TIME_PROVIDER_H_ 46 #define BASE_TEST_MOCK_TIME_PROVIDER_H_ 47 48 #include "base/time/time.h" 49 #include "testing/gmock/include/gmock/gmock.h" 50 51 namespace base { 52 53 class MockTimeProvider { 54 public: 55 MockTimeProvider(); 56 ~MockTimeProvider(); 57 58 MOCK_METHOD0(Now, Time()); 59 60 static Time StaticNow(); 61 62 private: 63 static MockTimeProvider* instance_; 64 DISALLOW_COPY_AND_ASSIGN(MockTimeProvider); 65 }; 66 67 } // namespace base 68 69 #endif // BASE_TEST_MOCK_TIME_PROVIDER_H_ 70