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