Commit 9d2e2082 authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

service worker: Port AppCacheDiskCacheTest

ServiceWorkerDiskCache was forked from AppCacheDiskCache. It should
have tests similar to AppCacheDiskCacheTest. This CL ports three
non-disabled tests.

Bug: 1117369
Change-Id: Ibb30a043d21bec7f7d003ea0768504e944248721
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2505702
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824209}
parent ebb63837
...@@ -5,15 +5,15 @@ ...@@ -5,15 +5,15 @@
#include "content/browser/service_worker/service_worker_disk_cache.h" #include "content/browser/service_worker/service_worker_disk_cache.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/disk_cache.h" #include "net/disk_cache/disk_cache.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
// TODO(bashi): Port tests from AppCacheDiskCacheTest.
namespace content { namespace content {
class ServiceWorkerDiskCacheTest : public testing::Test { class ServiceWorkerDiskCacheTest : public testing::Test {
...@@ -32,7 +32,7 @@ class ServiceWorkerDiskCacheTest : public testing::Test { ...@@ -32,7 +32,7 @@ class ServiceWorkerDiskCacheTest : public testing::Test {
void InitializeDiskCache(ServiceWorkerDiskCache* disk_cache) { void InitializeDiskCache(ServiceWorkerDiskCache* disk_cache) {
base::RunLoop loop; base::RunLoop loop;
disk_cache->InitWithDiskBackend( disk_cache->InitWithDiskBackend(
directory_.GetPath(), false, GetPath(), false,
/*post_cleanup_callback=*/base::DoNothing::Once(), /*post_cleanup_callback=*/base::DoNothing::Once(),
base::BindLambdaForTesting([&](int rv) { base::BindLambdaForTesting([&](int rv) {
ASSERT_EQ(rv, net::OK); ASSERT_EQ(rv, net::OK);
...@@ -41,6 +41,8 @@ class ServiceWorkerDiskCacheTest : public testing::Test { ...@@ -41,6 +41,8 @@ class ServiceWorkerDiskCacheTest : public testing::Test {
loop.Run(); loop.Run();
} }
base::FilePath GetPath() { return directory_.GetPath(); }
private: private:
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
base::ScopedTempDir directory_; base::ScopedTempDir directory_;
...@@ -77,4 +79,103 @@ TEST_F(ServiceWorkerDiskCacheTest, MultipleCallsForSameKey) { ...@@ -77,4 +79,103 @@ TEST_F(ServiceWorkerDiskCacheTest, MultipleCallsForSameKey) {
EXPECT_TRUE(doom_entry_called); EXPECT_TRUE(doom_entry_called);
} }
TEST_F(ServiceWorkerDiskCacheTest, DisablePriorToInitCompletion) {
// Create an instance and start it initializing, queue up
// one of each kind of "entry" function.
auto disk_cache = std::make_unique<ServiceWorkerDiskCache>();
EXPECT_FALSE(disk_cache->is_disabled());
size_t callback_count = 0;
auto completion_callback = base::BindLambdaForTesting([&](int rv) {
EXPECT_EQ(rv, net::ERR_ABORTED);
++callback_count;
});
auto entry_callback = base::BindLambdaForTesting(
[&](int rv, std::unique_ptr<ServiceWorkerDiskCacheEntry> entry) {
EXPECT_EQ(rv, net::ERR_ABORTED);
EXPECT_FALSE(entry);
++callback_count;
});
disk_cache->InitWithDiskBackend(GetPath(), false,
/*post_cleanup_callback=*/base::DoNothing(),
completion_callback);
disk_cache->CreateEntry(1, entry_callback);
disk_cache->OpenEntry(2, entry_callback);
disk_cache->DoomEntry(3, completion_callback);
// Pull the plug on all that.
EXPECT_FALSE(disk_cache->is_disabled());
disk_cache->Disable();
EXPECT_TRUE(disk_cache->is_disabled());
FlushCacheTasks();
EXPECT_EQ(callback_count, 4u);
// Ensure the directory can be deleted at this point.
EXPECT_TRUE(base::DirectoryExists(GetPath()));
EXPECT_FALSE(base::IsDirectoryEmpty(GetPath()));
EXPECT_TRUE(base::DeletePathRecursively(GetPath()));
EXPECT_FALSE(base::DirectoryExists(GetPath()));
}
TEST_F(ServiceWorkerDiskCacheTest, DisableAfterInitted) {
// Create an instance and start it initializing, queue up
// one of each kind of "entry" function.
auto disk_cache = std::make_unique<ServiceWorkerDiskCache>();
EXPECT_FALSE(disk_cache->is_disabled());
InitializeDiskCache(disk_cache.get());
// Pull the plug
disk_cache->Disable();
FlushCacheTasks();
// Ensure the directory can be deleted at this point.
EXPECT_TRUE(base::DirectoryExists(GetPath()));
EXPECT_FALSE(base::IsDirectoryEmpty(GetPath()));
EXPECT_TRUE(base::DeletePathRecursively(GetPath()));
EXPECT_FALSE(base::DirectoryExists(GetPath()));
// Methods should fail.
size_t callback_count = 0;
auto completion_callback = base::BindLambdaForTesting([&](int rv) {
EXPECT_EQ(rv, net::ERR_ABORTED);
++callback_count;
});
auto entry_callback = base::BindLambdaForTesting(
[&](int rv, std::unique_ptr<ServiceWorkerDiskCacheEntry> entry) {
EXPECT_EQ(rv, net::ERR_ABORTED);
EXPECT_FALSE(entry);
++callback_count;
});
disk_cache->CreateEntry(1, entry_callback);
disk_cache->OpenEntry(2, entry_callback);
disk_cache->DoomEntry(3, completion_callback);
FlushCacheTasks();
EXPECT_EQ(callback_count, 3u);
}
TEST_F(ServiceWorkerDiskCacheTest, CleanupCallback) {
// Test that things delete fine when we disable the cache and wait for
// the cleanup callback.
net::TestClosure cleanup_done;
net::TestCompletionCallback init_done;
auto disk_cache = std::make_unique<ServiceWorkerDiskCache>();
EXPECT_FALSE(disk_cache->is_disabled());
disk_cache->InitWithDiskBackend(GetPath(), false, cleanup_done.closure(),
init_done.callback());
EXPECT_EQ(net::OK, init_done.WaitForResult());
disk_cache->Disable();
cleanup_done.WaitForResult();
// Ensure the directory can be deleted at this point.
EXPECT_TRUE(base::DirectoryExists(GetPath()));
EXPECT_FALSE(base::IsDirectoryEmpty(GetPath()));
EXPECT_TRUE(base::DeletePathRecursively(GetPath()));
EXPECT_FALSE(base::DirectoryExists(GetPath()));
}
} // namespace content } // namespace content
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment