| Index: content/browser/appcache/chrome_appcache_service_unittest.cc
|
| diff --git a/content/browser/appcache/chrome_appcache_service_unittest.cc b/content/browser/appcache/chrome_appcache_service_unittest.cc
|
| index 7f99286c83e963e1c3ccddd812d54acd42855c65..8fd6fdd4a9187d88d0295084103f49ff76e757ef 100644
|
| --- a/content/browser/appcache/chrome_appcache_service_unittest.cc
|
| +++ b/content/browser/appcache/chrome_appcache_service_unittest.cc
|
| @@ -5,25 +5,130 @@
|
| #include "base/file_util.h"
|
| #include "base/memory/ref_counted.h"
|
| #include "base/message_loop.h"
|
| -#include "base/scoped_temp_dir.h"
|
| #include "chrome/test/testing_browser_process.h"
|
| #include "chrome/test/testing_browser_process_test.h"
|
| #include "content/browser/appcache/chrome_appcache_service.h"
|
| #include "content/browser/browser_thread.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| #include "webkit/appcache/appcache_storage_impl.h"
|
| +#include "webkit/quota/mock_special_storage_policy.h"
|
| +
|
| +#include <set>
|
|
|
| namespace {
|
| -const FilePath::CharType kTestingAppCacheDirname[] =
|
| - FILE_PATH_LITERAL("Application Cache");
|
| -}
|
| +
|
| +// Examples of a protected and an unprotected origin, to be used througout the
|
| +// test.
|
| +const GURL kProtectedOrigin("http://www.protected.com/");
|
| +const GURL kProtectedManifest1("http://www.protected.com/1/cache.manifest");
|
| +const GURL kProtectedManifest2("http://www.protected.com/2/cache.manifest");
|
| +const GURL kNormalOrigin("http://www.normal.com/");
|
| +const GURL kNormalManifest1("http://www.normal.com/1/cache.manifest");
|
| +const GURL kNormalManifest2("http://www.normal.com/2/cache.manifest");
|
| +
|
| +} // namespace
|
|
|
| namespace appcache {
|
|
|
| +// This subclass overwrites 2 functions (GetAllAppCacheInfo and
|
| +// DeleteAppcachesForOrigin). It enables unit testing the internal logic of
|
| +// ChromeAppCacheInfo (deleting only non-protected origins from the app cache).
|
| +class TestableChromeAppCacheService : public ChromeAppCacheService {
|
| +public:
|
| + explicit TestableChromeAppCacheService(quota::QuotaManagerProxy* proxy);
|
| +
|
| + // Mock implementations.
|
| + virtual void GetAllAppCacheInfo(AppCacheInfoCollection* collection,
|
| + net::CompletionCallback* callback);
|
| + virtual void SyncGetAllAppCacheInfo(AppCacheInfoCollection* collection);
|
| + virtual void DeleteAppCacheGroup(const GURL& origin,
|
| + net::CompletionCallback* callback);
|
| + virtual void SyncDeleteAppCacheGroup(const GURL& origin);
|
| +
|
| + // Functions for controlling the mock implementations.
|
| + void set_empty(bool empty);
|
| + bool GroupDeleted(const GURL& manifest_url);
|
| +
|
| +private:
|
| + // Helper function for the mock implementation
|
| + static void DeferredCallCallback(net::CompletionCallback* callback, int rv);
|
| +
|
| + std::set<GURL> deleted_groups_;
|
| + bool empty_;
|
| +};
|
| +
|
| +TestableChromeAppCacheService::TestableChromeAppCacheService(
|
| + quota::QuotaManagerProxy* proxy)
|
| + : ChromeAppCacheService(proxy), empty_(false) {
|
| +}
|
| +
|
| +void TestableChromeAppCacheService::GetAllAppCacheInfo(
|
| + AppCacheInfoCollection* collection,
|
| + net::CompletionCallback* callback) {
|
| + if (!empty_) {
|
| + AppCacheInfo info1;
|
| + info1.manifest_url = kProtectedManifest1;
|
| + AppCacheInfo info2;
|
| + info2.manifest_url = kProtectedManifest2;
|
| + AppCacheInfoVector& info_vector1 =
|
| + collection->infos_by_origin[kProtectedOrigin];
|
| + info_vector1.push_back(info1);
|
| + info_vector1.push_back(info2);
|
| +
|
| + AppCacheInfo info3;
|
| + info3.manifest_url = kNormalManifest1;
|
| + AppCacheInfo info4;
|
| + info4.manifest_url = kNormalManifest2;
|
| + AppCacheInfoVector& info_vector2 =
|
| + collection->infos_by_origin[kNormalOrigin];
|
| + info_vector2.push_back(info3);
|
| + info_vector2.push_back(info4);
|
| + }
|
| + if (callback)
|
| + callback->Run(0);
|
| +}
|
| +
|
| +void TestableChromeAppCacheService::SyncGetAllAppCacheInfo(
|
| + AppCacheInfoCollection* collection) {
|
| + GetAllAppCacheInfo(collection, NULL);
|
| +}
|
| +
|
| +void TestableChromeAppCacheService::DeleteAppCacheGroup(
|
| + const GURL& manifest_url,
|
| + net::CompletionCallback* callback) {
|
| + deleted_groups_.insert(manifest_url);
|
| + // The callback must be called asynchronously, otherwise the code breaks.
|
| + // Defer to guarentee async completion.
|
| + MessageLoop::current()->PostTask(
|
| + FROM_HERE,
|
| + NewRunnableFunction(&DeferredCallCallback, callback, 0));
|
| +}
|
| +
|
| +void TestableChromeAppCacheService::SyncDeleteAppCacheGroup(
|
| + const GURL& manifest_url) {
|
| + deleted_groups_.insert(manifest_url);
|
| +}
|
| +
|
| +void TestableChromeAppCacheService::set_empty(bool empty) {
|
| + empty_ = empty;
|
| +}
|
| +
|
| +bool TestableChromeAppCacheService::GroupDeleted(const GURL& manifest_url) {
|
| + return (deleted_groups_.find(manifest_url) != deleted_groups_.end());
|
| +}
|
| +
|
| +void TestableChromeAppCacheService::DeferredCallCallback(
|
| + net::CompletionCallback* callback, int rv){
|
| + callback->Run(rv);
|
| +}
|
| +
|
| class ChromeAppCacheServiceTest : public TestingBrowserProcessTest {
|
| public:
|
| ChromeAppCacheServiceTest()
|
| : message_loop_(MessageLoop::TYPE_IO),
|
| + ALLOW_THIS_IN_INITIALIZER_LIST(appcache_cleared_callback_(
|
| + this, &ChromeAppCacheServiceTest::OnAppCacheCleared)),
|
| + callback_called_(false),
|
| db_thread_(BrowserThread::DB, &message_loop_),
|
| file_thread_(BrowserThread::FILE, &message_loop_),
|
| cache_thread_(BrowserThread::CACHE, &message_loop_),
|
| @@ -32,77 +137,158 @@ class ChromeAppCacheServiceTest : public TestingBrowserProcessTest {
|
|
|
| protected:
|
| MessageLoop message_loop_;
|
| - ScopedTempDir temp_dir_;
|
| + net::CompletionCallbackImpl<ChromeAppCacheServiceTest>
|
| + appcache_cleared_callback_;
|
| + bool callback_called_;
|
|
|
| private:
|
| + void OnAppCacheCleared(int rv);
|
| +
|
| BrowserThread db_thread_;
|
| BrowserThread file_thread_;
|
| BrowserThread cache_thread_;
|
| BrowserThread io_thread_;
|
| };
|
|
|
| -TEST_F(ChromeAppCacheServiceTest, KeepOnDestruction) {
|
| - ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
| - FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname);
|
| - scoped_refptr<ChromeAppCacheService> appcache_service =
|
| - new ChromeAppCacheService(NULL);
|
| +void ChromeAppCacheServiceTest::OnAppCacheCleared(int rv) {
|
| + callback_called_ = true;
|
| +}
|
| +
|
| +TEST_F(ChromeAppCacheServiceTest, ClearAppCache) {
|
| + // Tests ChromeAppCacheService::ClearAppCache(): nonprotected origins should
|
| + // be deleted an protected origins should not.
|
| + scoped_refptr<TestableChromeAppCacheService> appcache_service =
|
| + new TestableChromeAppCacheService(NULL);
|
| const content::ResourceContext* resource_context = NULL;
|
| + scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
|
| + new quota::MockSpecialStoragePolicy;
|
| + mock_policy->AddProtected(kProtectedOrigin);
|
| + // Passing an empty FilePath is ok; nothing will be saved on the disk during
|
| + // the test.
|
| BrowserThread::PostTask(
|
| BrowserThread::IO, FROM_HERE,
|
| NewRunnableMethod(appcache_service.get(),
|
| &ChromeAppCacheService::InitializeOnIOThread,
|
| - appcache_path,
|
| + FilePath(),
|
| resource_context,
|
| - scoped_refptr<quota::SpecialStoragePolicy>(NULL),
|
| - false));
|
| - // Make the steps needed to initialize the storage of AppCache data.
|
| + mock_policy));
|
| + // Spin the message loop so that the initialization really happens.
|
| + message_loop_.RunAllPending();
|
| +
|
| + appcache_service->ClearAppCache(&appcache_cleared_callback_);
|
| +
|
| + // Spin the message loop so that the asynchronous operations (mainly, calling
|
| + // the callback) happen.
|
| message_loop_.RunAllPending();
|
| - appcache::AppCacheStorageImpl* storage =
|
| - static_cast<appcache::AppCacheStorageImpl*>(appcache_service->storage());
|
| - ASSERT_TRUE(storage->database_->db_connection());
|
| - ASSERT_EQ(1, storage->NewCacheId());
|
| - storage->disk_cache();
|
| +
|
| + // The appcaches of the protected origin weren't deleted, but the appcache of
|
| + // the normal origin were.
|
| + EXPECT_FALSE(appcache_service->GroupDeleted(kProtectedManifest1));
|
| + EXPECT_FALSE(appcache_service->GroupDeleted(kProtectedManifest2));
|
| + EXPECT_TRUE(appcache_service->GroupDeleted(kNormalManifest1));
|
| + EXPECT_TRUE(appcache_service->GroupDeleted(kNormalManifest2));
|
| + EXPECT_TRUE(callback_called_);
|
| +}
|
| +
|
| +TEST_F(ChromeAppCacheServiceTest, ClearEmptyAppCache) {
|
| + // Tests ChromeAppCacheService::ClearAppCache(): the callback function should
|
| + // be called even if there are no appcaches to clear.
|
| + scoped_refptr<TestableChromeAppCacheService> appcache_service =
|
| + new TestableChromeAppCacheService(NULL);
|
| + appcache_service->set_empty(true);
|
| + const content::ResourceContext* resource_context = NULL;
|
| + scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
|
| + new quota::MockSpecialStoragePolicy;
|
| + // Passing an empty FilePath is ok; nothing will be saved on the disk during
|
| + // the test.
|
| + BrowserThread::PostTask(
|
| + BrowserThread::IO, FROM_HERE,
|
| + NewRunnableMethod(appcache_service.get(),
|
| + &ChromeAppCacheService::InitializeOnIOThread,
|
| + FilePath(),
|
| + resource_context,
|
| + mock_policy));
|
| + // Spin the message loop so that the initialization really happens.
|
| message_loop_.RunAllPending();
|
|
|
| - ASSERT_TRUE(file_util::PathExists(appcache_path));
|
| - ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index")));
|
| + appcache_service->ClearAppCache(&appcache_cleared_callback_);
|
|
|
| - appcache_service = NULL;
|
| + // Spin the message loop so that the asynchronous operations (mainly, calling
|
| + // the callback) happen.
|
| message_loop_.RunAllPending();
|
|
|
| - ASSERT_TRUE(file_util::PathExists(appcache_path));
|
| + EXPECT_TRUE(callback_called_);
|
| }
|
|
|
| -TEST_F(ChromeAppCacheServiceTest, RemoveOnDestruction) {
|
| - ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
| - FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname);
|
| - scoped_refptr<ChromeAppCacheService> appcache_service =
|
| - new ChromeAppCacheService(NULL);
|
| +TEST_F(ChromeAppCacheServiceTest, ClearAppCacheWithoutCallback) {
|
| + // Tests ChromeAppCacheService::ClearAppCache(): passing NULL as callback
|
| + // should be ok and the functionality should be the same as in the case with
|
| + // the callback.
|
| + scoped_refptr<TestableChromeAppCacheService> appcache_service =
|
| + new TestableChromeAppCacheService(NULL);
|
| const content::ResourceContext* resource_context = NULL;
|
| + scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
|
| + new quota::MockSpecialStoragePolicy;
|
| + mock_policy->AddProtected(kProtectedOrigin);
|
| + // Passing an empty FilePath is ok; nothing will be saved on the disk during
|
| + // the test.
|
| BrowserThread::PostTask(
|
| BrowserThread::IO, FROM_HERE,
|
| NewRunnableMethod(appcache_service.get(),
|
| &ChromeAppCacheService::InitializeOnIOThread,
|
| - appcache_path,
|
| + FilePath(),
|
| resource_context,
|
| - scoped_refptr<quota::SpecialStoragePolicy>(NULL),
|
| - true));
|
| - // Make the steps needed to initialize the storage of AppCache data.
|
| + mock_policy));
|
| + // Spin the message loop so that the initialization really happens.
|
| message_loop_.RunAllPending();
|
| - appcache::AppCacheStorageImpl* storage =
|
| - static_cast<appcache::AppCacheStorageImpl*>(appcache_service->storage());
|
| - ASSERT_TRUE(storage->database_->db_connection());
|
| - ASSERT_EQ(1, storage->NewCacheId());
|
| - storage->disk_cache();
|
| +
|
| + appcache_service->ClearAppCache(NULL);
|
| +
|
| + // Spin the message loop so that the asynchronous operations have a chance to
|
| + // happen.
|
| message_loop_.RunAllPending();
|
|
|
| - ASSERT_TRUE(file_util::PathExists(appcache_path));
|
| - ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index")));
|
| + // The appcaches of the protected origin weren't deleted, but the appcache of
|
| + // the normal origin were.
|
| + EXPECT_FALSE(appcache_service->GroupDeleted(kProtectedManifest1));
|
| + EXPECT_FALSE(appcache_service->GroupDeleted(kProtectedManifest2));
|
| + EXPECT_TRUE(appcache_service->GroupDeleted(kNormalManifest1));
|
| + EXPECT_TRUE(appcache_service->GroupDeleted(kNormalManifest2));
|
| +}
|
|
|
| - appcache_service = NULL;
|
| +TEST_F(ChromeAppCacheServiceTest, SyncClearAppCache) {
|
| + // Tests ChromeAppCacheService::SyncClearAppCacheOnIOThread(): nonprotected
|
| + // origins should be deleted an protected origins should not.
|
| + scoped_refptr<TestableChromeAppCacheService> appcache_service =
|
| + new TestableChromeAppCacheService(NULL);
|
| + const content::ResourceContext* resource_context = NULL;
|
| + scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
|
| + new quota::MockSpecialStoragePolicy;
|
| + mock_policy->AddProtected(kProtectedOrigin);
|
| + // Passing an empty FilePath is ok; nothing will be saved on the disk during
|
| + // the test.
|
| + BrowserThread::PostTask(
|
| + BrowserThread::IO, FROM_HERE,
|
| + NewRunnableMethod(appcache_service.get(),
|
| + &ChromeAppCacheService::InitializeOnIOThread,
|
| + FilePath(),
|
| + resource_context,
|
| + mock_policy));
|
| + // Spin the message loop so that the initialization really happens.
|
| message_loop_.RunAllPending();
|
|
|
| - ASSERT_FALSE(file_util::PathExists(appcache_path));
|
| + appcache_service->SyncClearAppCacheOnIOThread();
|
| + // The task that completes the appcache clearing synchronously is posted to
|
| + // the IO thread. Run it, but don't run anything else.
|
| + message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask());
|
| + message_loop_.Run();
|
| +
|
| + // The appcaches of the protected origin weren't deleted, but the appcache of
|
| + // the normal origin were.
|
| + EXPECT_FALSE(appcache_service->GroupDeleted(kProtectedManifest1));
|
| + EXPECT_FALSE(appcache_service->GroupDeleted(kProtectedManifest2));
|
| + EXPECT_TRUE(appcache_service->GroupDeleted(kNormalManifest1));
|
| + EXPECT_TRUE(appcache_service->GroupDeleted(kNormalManifest2));
|
| }
|
|
|
| } // namespace appcache
|
|
|