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 <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/test/scoped_task_environment.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
15 #include "content/browser/appcache/appcache_database.h" | 16 #include "content/browser/appcache/appcache_database.h" |
16 #include "content/browser/appcache/appcache_storage_impl.h" | 17 #include "content/browser/appcache/appcache_storage_impl.h" |
17 #include "content/browser/appcache/chrome_appcache_service.h" | 18 #include "content/browser/appcache/chrome_appcache_service.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/resource_context.h" | 20 #include "content/public/browser/resource_context.h" |
20 #include "content/public/test/test_browser_context.h" | 21 #include "content/public/test/test_browser_context.h" |
21 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
22 #include "content/test/appcache_test_helper.h" | 23 #include "content/test/appcache_test_helper.h" |
23 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 private: | 58 private: |
58 net::URLRequestContext* context_; | 59 net::URLRequestContext* context_; |
59 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 60 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
60 }; | 61 }; |
61 | 62 |
62 } // namespace | 63 } // namespace |
63 | 64 |
64 class ChromeAppCacheServiceTest : public testing::Test { | 65 class ChromeAppCacheServiceTest : public testing::Test { |
65 public: | 66 public: |
66 ChromeAppCacheServiceTest() | 67 ChromeAppCacheServiceTest() |
67 : kProtectedManifestURL(kProtectedManifest), | 68 : scoped_task_environment_( |
| 69 base::test::ScopedTaskEnvironment::MainThreadType::IO), |
| 70 kProtectedManifestURL(kProtectedManifest), |
68 kNormalManifestURL(kNormalManifest), | 71 kNormalManifestURL(kNormalManifest), |
69 kSessionOnlyManifestURL(kSessionOnlyManifest), | 72 kSessionOnlyManifestURL(kSessionOnlyManifest), |
70 thread_bundle_(TestBrowserThreadBundle::Options::IO_MAINLOOP) {} | 73 thread_bundle_(TestBrowserThreadBundle::Options::IO_MAINLOOP) {} |
71 | 74 |
72 protected: | 75 protected: |
73 scoped_refptr<ChromeAppCacheService> CreateAppCacheServiceImpl( | 76 scoped_refptr<ChromeAppCacheService> CreateAppCacheServiceImpl( |
74 const base::FilePath& appcache_path, | 77 const base::FilePath& appcache_path, |
75 bool init_storage); | 78 bool init_storage); |
76 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service); | 79 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service); |
77 | 80 |
| 81 base::test::ScopedTaskEnvironment scoped_task_environment_; |
78 base::ScopedTempDir temp_dir_; | 82 base::ScopedTempDir temp_dir_; |
79 const GURL kProtectedManifestURL; | 83 const GURL kProtectedManifestURL; |
80 const GURL kNormalManifestURL; | 84 const GURL kNormalManifestURL; |
81 const GURL kSessionOnlyManifestURL; | 85 const GURL kSessionOnlyManifestURL; |
82 | 86 |
83 private: | 87 private: |
84 TestBrowserThreadBundle thread_bundle_; | 88 TestBrowserThreadBundle thread_bundle_; |
85 TestBrowserContext browser_context_; | 89 TestBrowserContext browser_context_; |
86 }; | 90 }; |
87 | 91 |
(...skipping 11 matching lines...) Expand all Loading... |
99 new MockURLRequestContextGetter( | 103 new MockURLRequestContextGetter( |
100 browser_context_.GetResourceContext()->GetRequestContext(), | 104 browser_context_.GetResourceContext()->GetRequestContext(), |
101 base::ThreadTaskRunnerHandle::Get()); | 105 base::ThreadTaskRunnerHandle::Get()); |
102 BrowserThread::PostTask( | 106 BrowserThread::PostTask( |
103 BrowserThread::IO, FROM_HERE, | 107 BrowserThread::IO, FROM_HERE, |
104 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 108 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
105 appcache_service, appcache_path, | 109 appcache_service, appcache_path, |
106 browser_context_.GetResourceContext(), | 110 browser_context_.GetResourceContext(), |
107 base::RetainedRef(mock_request_context_getter), mock_policy)); | 111 base::RetainedRef(mock_request_context_getter), mock_policy)); |
108 // Steps needed to initialize the storage of AppCache data. | 112 // Steps needed to initialize the storage of AppCache data. |
109 base::RunLoop().RunUntilIdle(); | 113 scoped_task_environment_.RunUntilIdle(); |
110 if (init_storage) { | 114 if (init_storage) { |
111 AppCacheStorageImpl* storage = | 115 AppCacheStorageImpl* storage = |
112 static_cast<AppCacheStorageImpl*>( | 116 static_cast<AppCacheStorageImpl*>( |
113 appcache_service->storage()); | 117 appcache_service->storage()); |
114 storage->database_->db_connection(); | 118 storage->database_->db_connection(); |
115 storage->disk_cache(); | 119 storage->disk_cache(); |
116 base::RunLoop().RunUntilIdle(); | 120 scoped_task_environment_.RunUntilIdle(); |
117 } | 121 } |
118 return appcache_service; | 122 return appcache_service; |
119 } | 123 } |
120 | 124 |
121 void ChromeAppCacheServiceTest::InsertDataIntoAppCache( | 125 void ChromeAppCacheServiceTest::InsertDataIntoAppCache( |
122 ChromeAppCacheService* appcache_service) { | 126 ChromeAppCacheService* appcache_service) { |
123 AppCacheTestHelper appcache_helper; | 127 AppCacheTestHelper appcache_helper; |
124 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifestURL); | 128 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifestURL); |
125 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifestURL); | 129 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifestURL); |
126 appcache_helper.AddGroupAndCache(appcache_service, kSessionOnlyManifestURL); | 130 appcache_helper.AddGroupAndCache(appcache_service, kSessionOnlyManifestURL); |
(...skipping 15 matching lines...) Expand all Loading... |
142 | 146 |
143 // Create a ChromeAppCacheService and insert data into it | 147 // Create a ChromeAppCacheService and insert data into it |
144 scoped_refptr<ChromeAppCacheService> appcache_service = | 148 scoped_refptr<ChromeAppCacheService> appcache_service = |
145 CreateAppCacheServiceImpl(appcache_path, true); | 149 CreateAppCacheServiceImpl(appcache_path, true); |
146 ASSERT_TRUE(base::PathExists(appcache_path)); | 150 ASSERT_TRUE(base::PathExists(appcache_path)); |
147 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index"))); | 151 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index"))); |
148 InsertDataIntoAppCache(appcache_service.get()); | 152 InsertDataIntoAppCache(appcache_service.get()); |
149 | 153 |
150 // Test: delete the ChromeAppCacheService | 154 // Test: delete the ChromeAppCacheService |
151 appcache_service = NULL; | 155 appcache_service = NULL; |
152 base::RunLoop().RunUntilIdle(); | 156 scoped_task_environment_.RunUntilIdle(); |
153 | 157 |
154 // Recreate the appcache (for reading the data back) | 158 // Recreate the appcache (for reading the data back) |
155 appcache_service = CreateAppCacheServiceImpl(appcache_path, false); | 159 appcache_service = CreateAppCacheServiceImpl(appcache_path, false); |
156 | 160 |
157 // The directory is still there | 161 // The directory is still there |
158 ASSERT_TRUE(base::PathExists(appcache_path)); | 162 ASSERT_TRUE(base::PathExists(appcache_path)); |
159 | 163 |
160 // The appcache data is also there, except the session-only origin. | 164 // The appcache data is also there, except the session-only origin. |
161 AppCacheTestHelper appcache_helper; | 165 AppCacheTestHelper appcache_helper; |
162 std::set<GURL> origins; | 166 std::set<GURL> origins; |
163 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins); | 167 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins); |
164 EXPECT_EQ(2UL, origins.size()); | 168 EXPECT_EQ(2UL, origins.size()); |
165 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 169 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
166 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); | 170 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); |
167 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == | 171 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == |
168 origins.end()); | 172 origins.end()); |
169 | 173 |
170 // Delete and let cleanup tasks run prior to returning. | 174 // Delete and let cleanup tasks run prior to returning. |
171 appcache_service = NULL; | 175 appcache_service = NULL; |
172 base::RunLoop().RunUntilIdle(); | 176 scoped_task_environment_.RunUntilIdle(); |
173 } | 177 } |
174 | 178 |
175 TEST_F(ChromeAppCacheServiceTest, SaveSessionState) { | 179 TEST_F(ChromeAppCacheServiceTest, SaveSessionState) { |
176 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 180 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
177 base::FilePath appcache_path = | 181 base::FilePath appcache_path = |
178 temp_dir_.GetPath().Append(kTestingAppCacheDirname); | 182 temp_dir_.GetPath().Append(kTestingAppCacheDirname); |
179 | 183 |
180 // Create a ChromeAppCacheService and insert data into it | 184 // Create a ChromeAppCacheService and insert data into it |
181 scoped_refptr<ChromeAppCacheService> appcache_service = | 185 scoped_refptr<ChromeAppCacheService> appcache_service = |
182 CreateAppCacheServiceImpl(appcache_path, true); | 186 CreateAppCacheServiceImpl(appcache_path, true); |
183 ASSERT_TRUE(base::PathExists(appcache_path)); | 187 ASSERT_TRUE(base::PathExists(appcache_path)); |
184 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index"))); | 188 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index"))); |
185 InsertDataIntoAppCache(appcache_service.get()); | 189 InsertDataIntoAppCache(appcache_service.get()); |
186 | 190 |
187 // Save session state. This should bypass the destruction-time deletion. | 191 // Save session state. This should bypass the destruction-time deletion. |
188 appcache_service->set_force_keep_session_state(); | 192 appcache_service->set_force_keep_session_state(); |
189 | 193 |
190 // Test: delete the ChromeAppCacheService | 194 // Test: delete the ChromeAppCacheService |
191 appcache_service = NULL; | 195 appcache_service = NULL; |
192 base::RunLoop().RunUntilIdle(); | 196 scoped_task_environment_.RunUntilIdle(); |
193 | 197 |
194 // Recreate the appcache (for reading the data back) | 198 // Recreate the appcache (for reading the data back) |
195 appcache_service = CreateAppCacheServiceImpl(appcache_path, false); | 199 appcache_service = CreateAppCacheServiceImpl(appcache_path, false); |
196 | 200 |
197 // The directory is still there | 201 // The directory is still there |
198 ASSERT_TRUE(base::PathExists(appcache_path)); | 202 ASSERT_TRUE(base::PathExists(appcache_path)); |
199 | 203 |
200 // No appcache data was deleted. | 204 // No appcache data was deleted. |
201 AppCacheTestHelper appcache_helper; | 205 AppCacheTestHelper appcache_helper; |
202 std::set<GURL> origins; | 206 std::set<GURL> origins; |
203 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins); | 207 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins); |
204 EXPECT_EQ(3UL, origins.size()); | 208 EXPECT_EQ(3UL, origins.size()); |
205 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 209 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
206 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); | 210 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); |
207 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) != | 211 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) != |
208 origins.end()); | 212 origins.end()); |
209 | 213 |
210 // Delete and let cleanup tasks run prior to returning. | 214 // Delete and let cleanup tasks run prior to returning. |
211 appcache_service = NULL; | 215 appcache_service = NULL; |
212 base::RunLoop().RunUntilIdle(); | 216 scoped_task_environment_.RunUntilIdle(); |
213 } | 217 } |
214 | 218 |
215 } // namespace content | 219 } // namespace content |
OLD | NEW |