Index: services/url_response_disk_cache/url_response_disk_cache_db_unittests.cc |
diff --git a/services/url_response_disk_cache/url_response_disk_cache_db_unittests.cc b/services/url_response_disk_cache/url_response_disk_cache_db_unittests.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4efbfe28d0c0d4c92642da37d99597dee82b26fc |
--- /dev/null |
+++ b/services/url_response_disk_cache/url_response_disk_cache_db_unittests.cc |
@@ -0,0 +1,115 @@ |
+// Copyright 2015 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/files/scoped_temp_dir.h" |
+#include "services/url_response_disk_cache/url_response_disk_cache_db.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace mojo { |
+ |
+namespace { |
+ |
+class URLResponseDiskCacheDBTest : public ::testing::Test { |
+ protected: |
+ void SetUp() override { |
+ ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); |
+ Open(); |
+ } |
+ |
+ void Open() { |
+ db_ = nullptr; |
+ base::FilePath db_path = tmp_dir_.path().Append("db"); |
+ db_ = new URLResponseDiskCacheDB(db_path); |
+ } |
+ |
+ CacheEntryPtr NewEntry() { |
+ CacheEntryPtr entry = CacheEntry::New(); |
+ entry->response = URLResponse::New(); |
+ entry->cache_directory = "/cache"; |
+ entry->content_path = "/cache/content"; |
+ return entry.Pass(); |
+ } |
+ |
+ base::ScopedTempDir tmp_dir_; |
+ scoped_refptr<URLResponseDiskCacheDB> db_; |
+}; |
+ |
+TEST_F(URLResponseDiskCacheDBTest, Create) {} |
+ |
+TEST_F(URLResponseDiskCacheDBTest, Version) { |
+ EXPECT_EQ(0lu, db_->GetVersion()); |
+ db_->SetVersion(15); |
+ EXPECT_EQ(15lu, db_->GetVersion()); |
+} |
+ |
+TEST_F(URLResponseDiskCacheDBTest, Persist) { |
+ db_->SetVersion(15); |
+ EXPECT_EQ(15lu, db_->GetVersion()); |
+ Open(); |
+ EXPECT_EQ(15lu, db_->GetVersion()); |
+} |
+ |
+TEST_F(URLResponseDiskCacheDBTest, Entry) { |
+ std::string origin = "origin"; |
+ std::string url = "url"; |
+ db_->Put(origin, url, NewEntry()); |
+ CacheEntryPtr entry = db_->Get(origin, url); |
ppi
2015/09/08 15:46:26
can you put two entries and verify that a newer wa
qsr
2015/09/11 15:47:57
Renamed and Done.
|
+ EXPECT_TRUE(entry); |
+ Open(); |
+ entry = db_->Get(origin, url); |
+ EXPECT_TRUE(entry); |
+} |
+ |
+TEST_F(URLResponseDiskCacheDBTest, Iterator) { |
ppi
2015/09/08 15:46:26
can you verify the ordering contract?
qsr
2015/09/11 15:47:57
Done.
|
+ std::string origin = "origin"; |
+ std::string url = "url"; |
+ db_->Put(origin, url, NewEntry()); |
+ CacheEntryPtr entry = db_->Get(origin, url); |
+ EXPECT_TRUE(entry); |
+ entry = CacheEntry::New(); |
+ scoped_ptr<DBReader::Iterator> iterator = db_->Iterate(); |
+ EXPECT_TRUE(iterator->HasNext()); |
+ InternalEntryKeyPtr key; |
+ iterator->GetNext(&key, &entry); |
+ EXPECT_FALSE(iterator->HasNext()); |
+ EXPECT_TRUE(key); |
+ EXPECT_TRUE(entry); |
+} |
+ |
+TEST_F(URLResponseDiskCacheDBTest, Delete) { |
+ std::string origin = "origin"; |
+ std::string url = "url"; |
+ db_->Put(origin, url, NewEntry()); |
+ CacheEntryPtr entry = db_->Get(origin, url); |
+ EXPECT_TRUE(entry); |
+ entry = CacheEntry::New(); |
+ scoped_ptr<DBReader::Iterator> iterator = db_->Iterate(); |
+ EXPECT_TRUE(iterator->HasNext()); |
+ InternalEntryKeyPtr key; |
+ iterator->GetNext(&key, &entry); |
+ EXPECT_FALSE(iterator->HasNext()); |
+ EXPECT_TRUE(key); |
+ EXPECT_TRUE(entry); |
+ db_->Delete(key.Pass()); |
+ entry = db_->Get(origin, url); |
+ EXPECT_FALSE(entry); |
+} |
+ |
+TEST_F(URLResponseDiskCacheDBTest, Snapshot) { |
+ std::string origin = "origin"; |
+ std::string url = "url"; |
+ db_->Put(origin, url, NewEntry()); |
+ scoped_ptr<DBReader> snapshot = db_->GetSnapshot(); |
+ std::string url2 = "url"; |
ppi
2015/09/08 15:46:26
did you mean this to be a different url from |url|
qsr
2015/09/11 15:47:58
Yes. Done.
|
+ db_->Put(origin, url2, NewEntry()); |
+ |
+ scoped_ptr<DBReader::Iterator> iterator = snapshot->Iterate(); |
+ EXPECT_TRUE(iterator->HasNext()); |
+ iterator->GetNext(nullptr, nullptr); |
+ EXPECT_FALSE(iterator->HasNext()); |
+} |
+ |
+} // namespace |
+ |
+} // namespace mojo |