Index: content/browser/cache_storage/cache_storage_unittest.cc |
diff --git a/content/browser/cache_storage/cache_storage_unittest.cc b/content/browser/cache_storage/cache_storage_unittest.cc |
index faf0eb10635b87b59c3f83c1088bd4c1a6452f5e..d46cab32059bcad6fdb27813b04f20e78a2e4a1b 100644 |
--- a/content/browser/cache_storage/cache_storage_unittest.cc |
+++ b/content/browser/cache_storage/cache_storage_unittest.cc |
@@ -6,10 +6,14 @@ |
#include "base/bind.h" |
#include "base/callback.h" |
+#include "base/files/file_util.h" |
#include "base/files/scoped_temp_dir.h" |
+#include "base/guid.h" |
#include "base/run_loop.h" |
#include "base/stl_util.h" |
+#include "base/strings/utf_string_conversions.h" |
#include "base/thread_task_runner_handle.h" |
+#include "content/browser/cache_storage/cache_storage.pb.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
#include "net/url_request/url_request_context_getter.h" |
#include "storage/browser/quota/quota_manager_proxy.h" |
@@ -19,6 +23,14 @@ namespace content { |
namespace { |
const char kOrigin[] = "http://example.com/"; |
+ |
+void EnumerateCachesCallback(CacheStorage::StringVector* cache_names_out, |
+ CacheStorageError* error_out, |
+ const CacheStorage::StringVector& cache_names, |
+ CacheStorageError error) { |
+ *cache_names_out = cache_names; |
+ *error_out = error; |
+} |
} |
class TestCacheStorage : public CacheStorage { |
@@ -70,7 +82,7 @@ class CacheStorageTest : public testing::Test { |
test_cache_storage_.reset(new TestCacheStorage(temp_dir_.path())); |
} |
- bool OpenCache(const std::string& cache_name) { |
+ bool OpenCache(const base::string16& cache_name) { |
bool callback_called = false; |
test_cache_storage_->OpenCache( |
cache_name, base::Bind(&CacheStorageTest::OpenCacheCallback, |
@@ -88,6 +100,15 @@ class CacheStorageTest : public testing::Test { |
callback_error_ = error; |
} |
+ bool EnumerateCaches(CacheStorage::StringVector* cache_names) { |
+ CacheStorageError error = CACHE_STORAGE_ERROR_NOT_FOUND; |
+ test_cache_storage_->EnumerateCaches( |
+ base::Bind(&EnumerateCachesCallback, base::Unretained(cache_names), |
+ base::Unretained(&error))); |
+ base::RunLoop().RunUntilIdle(); |
+ return error == CACHE_STORAGE_OK; |
+ } |
+ |
base::ScopedTempDir temp_dir_; |
TestBrowserThreadBundle browser_thread_bundle_; |
scoped_ptr<TestCacheStorage> test_cache_storage_; |
@@ -98,24 +119,50 @@ class CacheStorageTest : public testing::Test { |
TEST_F(CacheStorageTest, PreserveCache) { |
test_cache_storage_->set_delay_preserved_cache_callback(true); |
- EXPECT_TRUE(OpenCache("foo")); |
+ EXPECT_TRUE(OpenCache(base::UTF8ToUTF16("foo"))); |
EXPECT_TRUE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
test_cache_storage_->RunPreservedCacheCallback(); |
EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
// Try opening it again, since the cache is already open (callback_cache_ is |
// referencing it) the cache shouldn't be preserved again. |
- EXPECT_TRUE(OpenCache("foo")); |
+ EXPECT_TRUE(OpenCache(base::UTF8ToUTF16("foo"))); |
EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
// Remove the reference to the cache so that it's deleted. After that, the |
// next time it's opened will require the cache to be created again and |
// preserved. |
callback_cache_ = nullptr; |
- EXPECT_TRUE(OpenCache("foo")); |
+ EXPECT_TRUE(OpenCache(base::UTF8ToUTF16("foo"))); |
EXPECT_TRUE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
test_cache_storage_->RunPreservedCacheCallback(); |
EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
} |
+// Until M50 we didn't use |name_utf16| but used |name| in CacheStorageIndex. |
+// This test checks the backward compatibility. |
+TEST_F(CacheStorageTest, ReadOldIndex) { |
+ CacheStorageIndex index; |
+ index.set_origin(kOrigin); |
+ CacheStorageIndex::Cache* index_cache1 = index.add_cache(); |
+ index_cache1->set_name("aaa"); |
+ index_cache1->set_cache_dir( |
+ temp_dir_.path().AppendASCII(base::GenerateGUID()).value()); |
+ CacheStorageIndex::Cache* index_cache2 = index.add_cache(); |
+ index_cache2->set_name("bbb"); |
+ index_cache2->set_cache_dir( |
+ temp_dir_.path().AppendASCII(base::GenerateGUID()).value()); |
+ |
+ std::string serialized; |
+ ASSERT_TRUE(index.SerializeToString(&serialized)); |
+ base::WriteFile(temp_dir_.path().AppendASCII(CacheStorage::kIndexFileName), |
+ serialized.c_str(), serialized.size()); |
+ |
+ CacheStorage::StringVector cache_names; |
+ EXPECT_TRUE(EnumerateCaches(&cache_names)); |
+ EXPECT_EQ(2u, cache_names.size()); |
+ EXPECT_EQ(base::UTF8ToUTF16("aaa"), cache_names[0]); |
+ EXPECT_EQ(base::UTF8ToUTF16("bbb"), cache_names[1]); |
+} |
+ |
} // namespace content |