diff options
author | gavinp@chromium.org <gavinp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-26 05:43:17 +0000 |
---|---|---|
committer | gavinp@chromium.org <gavinp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-26 05:43:17 +0000 |
commit | 56fc83d5e086f3f5e1e554838e512ce02fe62787 (patch) | |
tree | b393c5d24399a7a9380904d8c66d8a7a512ee57b /net | |
parent | 2bb9b6f9a1cb79e6ed7bccf2478ee5abe235b930 (diff) | |
download | chromium_src-56fc83d5e086f3f5e1e554838e512ce02fe62787.zip chromium_src-56fc83d5e086f3f5e1e554838e512ce02fe62787.tar.gz chromium_src-56fc83d5e086f3f5e1e554838e512ce02fe62787.tar.bz2 |
Style changes in SimpleIndexFileTest.
Generally reduce the amount of testing cruft required in
simple_index_file.h.
R=pasko,rdsmith,clamy
BUG=None
Review URL: https://chromiumcodereview.appspot.com/16835005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@208635 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/disk_cache/simple/simple_index_file.h | 12 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index_file_unittest.cc | 96 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index_unittest.cc | 12 |
3 files changed, 64 insertions, 56 deletions
diff --git a/net/disk_cache/simple/simple_index_file.h b/net/disk_cache/simple/simple_index_file.h index d8cccf4..5cda160 100644 --- a/net/disk_cache/simple/simple_index_file.h +++ b/net/disk_cache/simple/simple_index_file.h @@ -6,6 +6,7 @@ #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ #include <string> +#include <vector> #include "base/basictypes.h" #include "base/containers/hash_tables.h" @@ -66,9 +67,9 @@ class NET_EXPORT_PRIVATE SimpleIndexFile { scoped_ptr<SimpleIndex::EntrySet>, bool force_index_flush)> IndexCompletionCallback; - explicit SimpleIndexFile(base::SingleThreadTaskRunner* cache_thread, - base::TaskRunner* worker_pool, - const base::FilePath& index_file_directory); + SimpleIndexFile(base::SingleThreadTaskRunner* cache_thread, + base::TaskRunner* worker_pool, + const base::FilePath& index_file_directory); virtual ~SimpleIndexFile(); // Get index entries based on current disk context. @@ -88,10 +89,7 @@ class NET_EXPORT_PRIVATE SimpleIndexFile { const base::Callback<void(int)>& reply_callback); private: - FRIEND_TEST_ALL_PREFIXES(SimpleIndexFileTest, IsIndexFileCorrupt); - FRIEND_TEST_ALL_PREFIXES(SimpleIndexFileTest, IsIndexFileStale); - FRIEND_TEST_ALL_PREFIXES(SimpleIndexFileTest, Serialize); - FRIEND_TEST_ALL_PREFIXES(SimpleIndexFileTest, WriteThenLoadIndex); + friend class WrappedSimpleIndexFile; // Using the mtime of the file and its mtime, detects if the index file is // stale. diff --git a/net/disk_cache/simple/simple_index_file_unittest.cc b/net/disk_cache/simple/simple_index_file_unittest.cc index caf3f40..ac22112 100644 --- a/net/disk_cache/simple/simple_index_file_unittest.cc +++ b/net/disk_cache/simple/simple_index_file_unittest.cc @@ -51,6 +51,25 @@ TEST(IndexMetadataTest, Serialize) { EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); } +// This friend derived class is able to reexport its ancestors private methods +// as public, for use in tests. +class WrappedSimpleIndexFile : public SimpleIndexFile { + public: + using SimpleIndexFile::Deserialize; + using SimpleIndexFile::IsIndexFileStale; + using SimpleIndexFile::kIndexFileName; + using SimpleIndexFile::LoadFromDisk; + using SimpleIndexFile::Serialize; + + explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory) + : SimpleIndexFile(base::MessageLoopProxy::current(), + base::MessageLoopProxy::current(), + index_file_directory) { + } + virtual ~WrappedSimpleIndexFile() { + } +}; + class SimpleIndexFileTest : public testing::Test { public: bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { @@ -58,15 +77,6 @@ class SimpleIndexFileTest : public testing::Test { a.entry_size_ == b.entry_size_; } - void IndexCompletionCallback( - scoped_ptr<SimpleIndex::EntrySet> index_file_entries, - bool force_index_flush) { - EXPECT_FALSE(callback_result_); - callback_result_.reset( - new IndexCompletionCallbackResult(index_file_entries.Pass(), - force_index_flush)); - } - protected: struct IndexCompletionCallbackResult { IndexCompletionCallbackResult( @@ -80,11 +90,25 @@ class SimpleIndexFileTest : public testing::Test { const bool force_index_flush; }; + SimpleIndexFile::IndexCompletionCallback GetCallback() { + return base::Bind(&SimpleIndexFileTest::IndexCompletionCallback, + base::Unretained(this)); + } + IndexCompletionCallbackResult* callback_result() { return callback_result_.get(); } private: + void IndexCompletionCallback( + scoped_ptr<SimpleIndex::EntrySet> index_file_entries, + bool force_index_flush) { + EXPECT_FALSE(callback_result_); + callback_result_.reset( + new IndexCompletionCallbackResult(index_file_entries.Pass(), + force_index_flush)); + } + scoped_ptr<IndexCompletionCallbackResult> callback_result_; }; @@ -103,13 +127,13 @@ TEST_F(SimpleIndexFileTest, Serialize) { SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); } - scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize( + scoped_ptr<Pickle> pickle = WrappedSimpleIndexFile::Serialize( index_metadata, entries); EXPECT_TRUE(pickle.get() != NULL); - scoped_ptr<SimpleIndex::EntrySet> new_entries = SimpleIndexFile::Deserialize( - reinterpret_cast<const char*>(pickle->data()), - pickle->size()); + scoped_ptr<SimpleIndex::EntrySet> new_entries = + WrappedSimpleIndexFile::Deserialize( + static_cast<const char*>(pickle->data()), pickle->size()); EXPECT_TRUE(new_entries.get() != NULL); EXPECT_EQ(entries.size(), new_entries->size()); @@ -127,26 +151,26 @@ TEST_F(SimpleIndexFileTest, IsIndexFileStale) { const std::string kIndexFileName = "simple-index"; const base::FilePath index_path = temp_dir.path().AppendASCII(kIndexFileName); - EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); + EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path)); const std::string kDummyData = "nothing to be seen here"; EXPECT_EQ(static_cast<int>(kDummyData.size()), file_util::WriteFile(index_path, kDummyData.data(), kDummyData.size())); - EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); + EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path)); const base::Time past_time = base::Time::Now() - base::TimeDelta::FromSeconds(10); EXPECT_TRUE(file_util::TouchFile(index_path, past_time, past_time)); EXPECT_TRUE(file_util::TouchFile(temp_dir.path(), past_time, past_time)); - EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); + EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path)); EXPECT_EQ(static_cast<int>(kDummyData.size()), file_util::WriteFile(temp_dir.path().AppendASCII("other_file"), kDummyData.data(), kDummyData.size())); - EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); + EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path)); } TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) { @@ -154,8 +178,8 @@ TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) { ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); const base::FilePath index_path = - temp_dir.path().AppendASCII(SimpleIndexFile::kIndexFileName); - EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); + temp_dir.path().AppendASCII(WrappedSimpleIndexFile::kIndexFileName); + EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path)); SimpleIndex::EntrySet entries; static const uint64 kHashes[] = { 11, 22, 33 }; @@ -170,23 +194,16 @@ TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) { const uint64 kCacheSize = 456U; { - SimpleIndexFile simple_index_file(base::MessageLoopProxy::current(), - base::MessageLoopProxy::current(), - temp_dir.path()); + WrappedSimpleIndexFile simple_index_file(temp_dir.path()); simple_index_file.WriteToDisk(entries, kCacheSize, base::TimeTicks(), false); base::RunLoop().RunUntilIdle(); EXPECT_TRUE(file_util::PathExists(index_path)); } - SimpleIndexFile simple_index_file(base::MessageLoopProxy::current(), - base::MessageLoopProxy::current(), - temp_dir.path()); - SimpleIndexFile::IndexCompletionCallback callback = - base::Bind(&SimpleIndexFileTest::IndexCompletionCallback, - base::Unretained(this)); + WrappedSimpleIndexFile simple_index_file(temp_dir.path()); simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(), - callback); + GetCallback()); base::RunLoop().RunUntilIdle(); EXPECT_TRUE(file_util::PathExists(index_path)); @@ -201,36 +218,29 @@ TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) { EXPECT_EQ(1U, read_entries->count(kHashes[i])); } -TEST_F(SimpleIndexFileTest, IsIndexFileCorrupt) { +TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { base::ScopedTempDir temp_dir; ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); const base::FilePath index_path = - temp_dir.path().AppendASCII(SimpleIndexFile::kIndexFileName); - EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); + temp_dir.path().AppendASCII(WrappedSimpleIndexFile::kIndexFileName); + EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path)); const std::string kDummyData = "nothing to be seen here"; EXPECT_EQ(static_cast<int>(kDummyData.size()), file_util::WriteFile(index_path, kDummyData.data(), kDummyData.size())); - EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); - - SimpleIndexFile simple_index_file(base::MessageLoopProxy::current(), - base::MessageLoopProxy::current(), - temp_dir.path()); - - SimpleIndexFile::IndexCompletionCallback callback = - base::Bind(&SimpleIndexFileTest::IndexCompletionCallback, - base::Unretained(this)); + EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path)); + WrappedSimpleIndexFile simple_index_file(temp_dir.path()); simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(), - callback); + GetCallback()); base::RunLoop().RunUntilIdle(); EXPECT_FALSE(file_util::PathExists(index_path)); ASSERT_TRUE(callback_result()); - EXPECT_TRUE(callback_result()->index_file_entries); EXPECT_TRUE(callback_result()->force_index_flush); + EXPECT_TRUE(callback_result()->index_file_entries); } } // namespace disk_cache diff --git a/net/disk_cache/simple/simple_index_unittest.cc b/net/disk_cache/simple/simple_index_unittest.cc index abda20b..52875e2 100644 --- a/net/disk_cache/simple/simple_index_unittest.cc +++ b/net/disk_cache/simple/simple_index_unittest.cc @@ -40,10 +40,10 @@ class EntryMetadataTest : public testing::Test { } }; -class TestSimpleIndexFile : public SimpleIndexFile, - public base::SupportsWeakPtr<TestSimpleIndexFile> { +class MockSimpleIndexFile : public SimpleIndexFile, + public base::SupportsWeakPtr<MockSimpleIndexFile> { public: - TestSimpleIndexFile() + MockSimpleIndexFile() : SimpleIndexFile(NULL, NULL, base::FilePath()), get_index_entries_calls_(0), doom_entry_set_calls_(0), @@ -103,7 +103,7 @@ class TestSimpleIndexFile : public SimpleIndexFile, class SimpleIndexTest : public testing::Test { public: virtual void SetUp() OVERRIDE { - scoped_ptr<TestSimpleIndexFile> index_file(new TestSimpleIndexFile()); + scoped_ptr<MockSimpleIndexFile> index_file(new MockSimpleIndexFile()); index_file_ = index_file->AsWeakPtr(); index_.reset(new SimpleIndex(NULL, base::FilePath(), index_file.PassAs<SimpleIndexFile>())); @@ -145,12 +145,12 @@ class SimpleIndexTest : public testing::Test { // Non-const for timer manipulation. SimpleIndex* index() { return index_.get(); } - const TestSimpleIndexFile* index_file() const { return index_file_.get(); } + const MockSimpleIndexFile* index_file() const { return index_file_.get(); } protected: SimpleIndex::EntrySet index_file_return_map_; scoped_ptr<SimpleIndex> index_; - base::WeakPtr<TestSimpleIndexFile> index_file_; + base::WeakPtr<MockSimpleIndexFile> index_file_; }; TEST_F(EntryMetadataTest, Basics) { |