diff options
author | stevet@chromium.org <stevet@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-28 14:29:06 +0000 |
---|---|---|
committer | stevet@chromium.org <stevet@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-28 14:29:06 +0000 |
commit | c4996f76eb3298229264f2402aeb62f56f8f4790 (patch) | |
tree | 79fae951e6c178d5b8edcad89a7a2c5bf015cdce /base/test | |
parent | f9f39163edfe72a91cdeb3c62d5c209914696a96 (diff) | |
download | chromium_src-c4996f76eb3298229264f2402aeb62f56f8f4790.zip chromium_src-c4996f76eb3298229264f2402aeb62f56f8f4790.tar.gz chromium_src-c4996f76eb3298229264f2402aeb62f56f8f4790.tar.bz2 |
Added last_modified field to TemplateURL and database. Updated unit tests, including refactoring MockTimeProvider out of media/.
BUG=None
TEST=No visible changes. Ensure that changed unittests all pass.
Review URL: http://codereview.chromium.org/7232023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90764 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test')
-rw-r--r-- | base/test/mock_time_provider.cc | 31 | ||||
-rw-r--r-- | base/test/mock_time_provider.h | 66 |
2 files changed, 97 insertions, 0 deletions
diff --git a/base/test/mock_time_provider.cc b/base/test/mock_time_provider.cc new file mode 100644 index 0000000..9e5547f --- /dev/null +++ b/base/test/mock_time_provider.cc @@ -0,0 +1,31 @@ +// 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. + +#include "base/logging.h" +#include "base/test/mock_time_provider.h" + +using ::testing::DefaultValue; + +namespace base { + +MockTimeProvider* MockTimeProvider::instance_ = NULL; + +MockTimeProvider::MockTimeProvider() { + DCHECK(!instance_) << "Only one instance of MockTimeProvider can exist"; + DCHECK(!DefaultValue<Time>::IsSet()); + instance_ = this; + DefaultValue<Time>::Set(Time::FromInternalValue(0)); +} + +MockTimeProvider::~MockTimeProvider() { + instance_ = NULL; + DefaultValue<Time>::Clear(); +} + +// static +Time MockTimeProvider::StaticNow() { + return instance_->Now(); +} + +} // namespace base diff --git a/base/test/mock_time_provider.h b/base/test/mock_time_provider.h new file mode 100644 index 0000000..9ff46e4 --- /dev/null +++ b/base/test/mock_time_provider.h @@ -0,0 +1,66 @@ +// 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. + +// 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)); +// } + +#include "base/time.h" +#include "testing/gmock/include/gmock/gmock.h" + +#ifndef BASE_TEST_MOCK_TIME_PROVIDER_H_ +#define BASE_TEST_MOCK_TIME_PROVIDER_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_ |