Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / appcache / chrome_appcache_service_unittest.cc
blob450bfe2c49ece05d1a38cb56c0de72237622bf56
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/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/appcache/appcache_database.h"
11 #include "content/browser/appcache/appcache_storage_impl.h"
12 #include "content/browser/appcache/chrome_appcache_service.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/public/browser/resource_context.h"
15 #include "content/public/test/mock_special_storage_policy.h"
16 #include "content/public/test/test_browser_context.h"
17 #include "content/test/appcache_test_helper.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 #include <set>
23 namespace content {
24 namespace {
25 const base::FilePath::CharType kTestingAppCacheDirname[] =
26 FILE_PATH_LITERAL("Application Cache");
28 // Examples of a protected and an unprotected origin, to be used througout the
29 // test.
30 const char kProtectedManifest[] = "http://www.protected.com/cache.manifest";
31 const char kNormalManifest[] = "http://www.normal.com/cache.manifest";
32 const char kSessionOnlyManifest[] = "http://www.sessiononly.com/cache.manifest";
34 class MockURLRequestContextGetter : public net::URLRequestContextGetter {
35 public:
36 MockURLRequestContextGetter(
37 net::URLRequestContext* context,
38 base::MessageLoopProxy* message_loop_proxy)
39 : context_(context), message_loop_proxy_(message_loop_proxy) {
42 net::URLRequestContext* GetURLRequestContext() override { return context_; }
44 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
45 const override {
46 return message_loop_proxy_;
49 protected:
50 ~MockURLRequestContextGetter() override {}
52 private:
53 net::URLRequestContext* context_;
54 scoped_refptr<base::SingleThreadTaskRunner> message_loop_proxy_;
57 } // namespace
59 class ChromeAppCacheServiceTest : public testing::Test {
60 public:
61 ChromeAppCacheServiceTest()
62 : kProtectedManifestURL(kProtectedManifest),
63 kNormalManifestURL(kNormalManifest),
64 kSessionOnlyManifestURL(kSessionOnlyManifest),
65 file_thread_(BrowserThread::FILE, &message_loop_),
66 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
67 &message_loop_),
68 cache_thread_(BrowserThread::CACHE, &message_loop_),
69 io_thread_(BrowserThread::IO, &message_loop_) {}
71 protected:
72 scoped_refptr<ChromeAppCacheService> CreateAppCacheServiceImpl(
73 const base::FilePath& appcache_path,
74 bool init_storage);
75 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service);
77 base::MessageLoopForIO message_loop_;
78 base::ScopedTempDir temp_dir_;
79 const GURL kProtectedManifestURL;
80 const GURL kNormalManifestURL;
81 const GURL kSessionOnlyManifestURL;
83 private:
84 BrowserThreadImpl file_thread_;
85 BrowserThreadImpl file_user_blocking_thread_;
86 BrowserThreadImpl cache_thread_;
87 BrowserThreadImpl io_thread_;
88 TestBrowserContext browser_context_;
91 scoped_refptr<ChromeAppCacheService>
92 ChromeAppCacheServiceTest::CreateAppCacheServiceImpl(
93 const base::FilePath& appcache_path,
94 bool init_storage) {
95 scoped_refptr<ChromeAppCacheService> appcache_service =
96 new ChromeAppCacheService(NULL);
97 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
98 new MockSpecialStoragePolicy;
99 mock_policy->AddProtected(kProtectedManifestURL.GetOrigin());
100 mock_policy->AddSessionOnly(kSessionOnlyManifestURL.GetOrigin());
101 scoped_refptr<MockURLRequestContextGetter> mock_request_context_getter =
102 new MockURLRequestContextGetter(
103 browser_context_.GetResourceContext()->GetRequestContext(),
104 message_loop_.message_loop_proxy().get());
105 BrowserThread::PostTask(
106 BrowserThread::IO,
107 FROM_HERE,
108 base::Bind(&ChromeAppCacheService::InitializeOnIOThread,
109 appcache_service.get(),
110 appcache_path,
111 browser_context_.GetResourceContext(),
112 mock_request_context_getter,
113 mock_policy));
114 // Steps needed to initialize the storage of AppCache data.
115 message_loop_.RunUntilIdle();
116 if (init_storage) {
117 AppCacheStorageImpl* storage =
118 static_cast<AppCacheStorageImpl*>(
119 appcache_service->storage());
120 storage->database_->db_connection();
121 storage->disk_cache();
122 message_loop_.RunUntilIdle();
124 return appcache_service;
127 void ChromeAppCacheServiceTest::InsertDataIntoAppCache(
128 ChromeAppCacheService* appcache_service) {
129 AppCacheTestHelper appcache_helper;
130 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifestURL);
131 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifestURL);
132 appcache_helper.AddGroupAndCache(appcache_service, kSessionOnlyManifestURL);
134 // Verify that adding the data succeeded
135 std::set<GURL> origins;
136 appcache_helper.GetOriginsWithCaches(appcache_service, &origins);
137 ASSERT_EQ(3UL, origins.size());
138 ASSERT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end());
139 ASSERT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end());
140 ASSERT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) !=
141 origins.end());
144 TEST_F(ChromeAppCacheServiceTest, KeepOnDestruction) {
145 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
146 base::FilePath appcache_path =
147 temp_dir_.path().Append(kTestingAppCacheDirname);
149 // Create a ChromeAppCacheService and insert data into it
150 scoped_refptr<ChromeAppCacheService> appcache_service =
151 CreateAppCacheServiceImpl(appcache_path, true);
152 ASSERT_TRUE(base::PathExists(appcache_path));
153 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index")));
154 InsertDataIntoAppCache(appcache_service.get());
156 // Test: delete the ChromeAppCacheService
157 appcache_service = NULL;
158 message_loop_.RunUntilIdle();
160 // Recreate the appcache (for reading the data back)
161 appcache_service = CreateAppCacheServiceImpl(appcache_path, false);
163 // The directory is still there
164 ASSERT_TRUE(base::PathExists(appcache_path));
166 // The appcache data is also there, except the session-only origin.
167 AppCacheTestHelper appcache_helper;
168 std::set<GURL> origins;
169 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins);
170 EXPECT_EQ(2UL, origins.size());
171 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end());
172 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end());
173 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) ==
174 origins.end());
176 // Delete and let cleanup tasks run prior to returning.
177 appcache_service = NULL;
178 message_loop_.RunUntilIdle();
181 TEST_F(ChromeAppCacheServiceTest, SaveSessionState) {
182 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
183 base::FilePath appcache_path =
184 temp_dir_.path().Append(kTestingAppCacheDirname);
186 // Create a ChromeAppCacheService and insert data into it
187 scoped_refptr<ChromeAppCacheService> appcache_service =
188 CreateAppCacheServiceImpl(appcache_path, true);
189 ASSERT_TRUE(base::PathExists(appcache_path));
190 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index")));
191 InsertDataIntoAppCache(appcache_service.get());
193 // Save session state. This should bypass the destruction-time deletion.
194 appcache_service->set_force_keep_session_state();
196 // Test: delete the ChromeAppCacheService
197 appcache_service = NULL;
198 message_loop_.RunUntilIdle();
200 // Recreate the appcache (for reading the data back)
201 appcache_service = CreateAppCacheServiceImpl(appcache_path, false);
203 // The directory is still there
204 ASSERT_TRUE(base::PathExists(appcache_path));
206 // No appcache data was deleted.
207 AppCacheTestHelper appcache_helper;
208 std::set<GURL> origins;
209 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins);
210 EXPECT_EQ(3UL, origins.size());
211 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end());
212 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end());
213 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) !=
214 origins.end());
216 // Delete and let cleanup tasks run prior to returning.
217 appcache_service = NULL;
218 message_loop_.RunUntilIdle();
221 } // namespace content