Update V8 to version 4.6.55.
[chromium-blink-merge.git] / content / browser / appcache / chrome_appcache_service_unittest.cc
blob0eca5fe0cfb2f118c0f5ac9c108a3a7b08dd8b70
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/location.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/single_thread_task_runner.h"
11 #include "content/browser/appcache/appcache_database.h"
12 #include "content/browser/appcache/appcache_storage_impl.h"
13 #include "content/browser/appcache/chrome_appcache_service.h"
14 #include "content/browser/browser_thread_impl.h"
15 #include "content/public/browser/resource_context.h"
16 #include "content/public/test/mock_special_storage_policy.h"
17 #include "content/public/test/test_browser_context.h"
18 #include "content/test/appcache_test_helper.h"
19 #include "net/url_request/url_request_context_getter.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 #include <set>
24 namespace content {
25 namespace {
26 const base::FilePath::CharType kTestingAppCacheDirname[] =
27 FILE_PATH_LITERAL("Application Cache");
29 // Examples of a protected and an unprotected origin, to be used througout the
30 // test.
31 const char kProtectedManifest[] = "http://www.protected.com/cache.manifest";
32 const char kNormalManifest[] = "http://www.normal.com/cache.manifest";
33 const char kSessionOnlyManifest[] = "http://www.sessiononly.com/cache.manifest";
35 class MockURLRequestContextGetter : public net::URLRequestContextGetter {
36 public:
37 MockURLRequestContextGetter(net::URLRequestContext* context,
38 base::SingleThreadTaskRunner* task_runner)
39 : context_(context), task_runner_(task_runner) {}
41 net::URLRequestContext* GetURLRequestContext() override { return context_; }
43 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
44 const override {
45 return task_runner_;
48 protected:
49 ~MockURLRequestContextGetter() override {}
51 private:
52 net::URLRequestContext* context_;
53 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
56 } // namespace
58 class ChromeAppCacheServiceTest : public testing::Test {
59 public:
60 ChromeAppCacheServiceTest()
61 : kProtectedManifestURL(kProtectedManifest),
62 kNormalManifestURL(kNormalManifest),
63 kSessionOnlyManifestURL(kSessionOnlyManifest),
64 file_thread_(BrowserThread::FILE, &message_loop_),
65 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
66 &message_loop_),
67 cache_thread_(BrowserThread::CACHE, &message_loop_),
68 io_thread_(BrowserThread::IO, &message_loop_) {}
70 protected:
71 scoped_refptr<ChromeAppCacheService> CreateAppCacheServiceImpl(
72 const base::FilePath& appcache_path,
73 bool init_storage);
74 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service);
76 base::MessageLoopForIO message_loop_;
77 base::ScopedTempDir temp_dir_;
78 const GURL kProtectedManifestURL;
79 const GURL kNormalManifestURL;
80 const GURL kSessionOnlyManifestURL;
82 private:
83 BrowserThreadImpl file_thread_;
84 BrowserThreadImpl file_user_blocking_thread_;
85 BrowserThreadImpl cache_thread_;
86 BrowserThreadImpl io_thread_;
87 TestBrowserContext browser_context_;
90 scoped_refptr<ChromeAppCacheService>
91 ChromeAppCacheServiceTest::CreateAppCacheServiceImpl(
92 const base::FilePath& appcache_path,
93 bool init_storage) {
94 scoped_refptr<ChromeAppCacheService> appcache_service =
95 new ChromeAppCacheService(NULL);
96 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
97 new MockSpecialStoragePolicy;
98 mock_policy->AddProtected(kProtectedManifestURL.GetOrigin());
99 mock_policy->AddSessionOnly(kSessionOnlyManifestURL.GetOrigin());
100 scoped_refptr<MockURLRequestContextGetter> mock_request_context_getter =
101 new MockURLRequestContextGetter(
102 browser_context_.GetResourceContext()->GetRequestContext(),
103 message_loop_.task_runner().get());
104 BrowserThread::PostTask(
105 BrowserThread::IO,
106 FROM_HERE,
107 base::Bind(&ChromeAppCacheService::InitializeOnIOThread,
108 appcache_service.get(),
109 appcache_path,
110 browser_context_.GetResourceContext(),
111 mock_request_context_getter,
112 mock_policy));
113 // Steps needed to initialize the storage of AppCache data.
114 message_loop_.RunUntilIdle();
115 if (init_storage) {
116 AppCacheStorageImpl* storage =
117 static_cast<AppCacheStorageImpl*>(
118 appcache_service->storage());
119 storage->database_->db_connection();
120 storage->disk_cache();
121 message_loop_.RunUntilIdle();
123 return appcache_service;
126 void ChromeAppCacheServiceTest::InsertDataIntoAppCache(
127 ChromeAppCacheService* appcache_service) {
128 AppCacheTestHelper appcache_helper;
129 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifestURL);
130 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifestURL);
131 appcache_helper.AddGroupAndCache(appcache_service, kSessionOnlyManifestURL);
133 // Verify that adding the data succeeded
134 std::set<GURL> origins;
135 appcache_helper.GetOriginsWithCaches(appcache_service, &origins);
136 ASSERT_EQ(3UL, origins.size());
137 ASSERT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end());
138 ASSERT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end());
139 ASSERT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) !=
140 origins.end());
143 TEST_F(ChromeAppCacheServiceTest, KeepOnDestruction) {
144 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
145 base::FilePath appcache_path =
146 temp_dir_.path().Append(kTestingAppCacheDirname);
148 // Create a ChromeAppCacheService and insert data into it
149 scoped_refptr<ChromeAppCacheService> appcache_service =
150 CreateAppCacheServiceImpl(appcache_path, true);
151 ASSERT_TRUE(base::PathExists(appcache_path));
152 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index")));
153 InsertDataIntoAppCache(appcache_service.get());
155 // Test: delete the ChromeAppCacheService
156 appcache_service = NULL;
157 message_loop_.RunUntilIdle();
159 // Recreate the appcache (for reading the data back)
160 appcache_service = CreateAppCacheServiceImpl(appcache_path, false);
162 // The directory is still there
163 ASSERT_TRUE(base::PathExists(appcache_path));
165 // The appcache data is also there, except the session-only origin.
166 AppCacheTestHelper appcache_helper;
167 std::set<GURL> origins;
168 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins);
169 EXPECT_EQ(2UL, origins.size());
170 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end());
171 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end());
172 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) ==
173 origins.end());
175 // Delete and let cleanup tasks run prior to returning.
176 appcache_service = NULL;
177 message_loop_.RunUntilIdle();
180 TEST_F(ChromeAppCacheServiceTest, SaveSessionState) {
181 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
182 base::FilePath appcache_path =
183 temp_dir_.path().Append(kTestingAppCacheDirname);
185 // Create a ChromeAppCacheService and insert data into it
186 scoped_refptr<ChromeAppCacheService> appcache_service =
187 CreateAppCacheServiceImpl(appcache_path, true);
188 ASSERT_TRUE(base::PathExists(appcache_path));
189 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index")));
190 InsertDataIntoAppCache(appcache_service.get());
192 // Save session state. This should bypass the destruction-time deletion.
193 appcache_service->set_force_keep_session_state();
195 // Test: delete the ChromeAppCacheService
196 appcache_service = NULL;
197 message_loop_.RunUntilIdle();
199 // Recreate the appcache (for reading the data back)
200 appcache_service = CreateAppCacheServiceImpl(appcache_path, false);
202 // The directory is still there
203 ASSERT_TRUE(base::PathExists(appcache_path));
205 // No appcache data was deleted.
206 AppCacheTestHelper appcache_helper;
207 std::set<GURL> origins;
208 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins);
209 EXPECT_EQ(3UL, origins.size());
210 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end());
211 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end());
212 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) !=
213 origins.end());
215 // Delete and let cleanup tasks run prior to returning.
216 appcache_service = NULL;
217 message_loop_.RunUntilIdle();
220 } // namespace content