OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stack> | 5 #include <stack> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 }; | 124 }; |
125 | 125 |
126 class MockHttpServerJobFactory | 126 class MockHttpServerJobFactory |
127 : public net::URLRequestJobFactory::ProtocolHandler { | 127 : public net::URLRequestJobFactory::ProtocolHandler { |
128 public: | 128 public: |
129 MockHttpServerJobFactory( | 129 MockHttpServerJobFactory( |
130 scoped_ptr<net::URLRequestInterceptor> appcache_start_interceptor) | 130 scoped_ptr<net::URLRequestInterceptor> appcache_start_interceptor) |
131 : appcache_start_interceptor_(appcache_start_interceptor.Pass()) { | 131 : appcache_start_interceptor_(appcache_start_interceptor.Pass()) { |
132 } | 132 } |
133 | 133 |
134 virtual net::URLRequestJob* MaybeCreateJob( | 134 net::URLRequestJob* MaybeCreateJob( |
135 net::URLRequest* request, | 135 net::URLRequest* request, |
136 net::NetworkDelegate* network_delegate) const override { | 136 net::NetworkDelegate* network_delegate) const override { |
137 net::URLRequestJob* appcache_job = | 137 net::URLRequestJob* appcache_job = |
138 appcache_start_interceptor_->MaybeInterceptRequest( | 138 appcache_start_interceptor_->MaybeInterceptRequest( |
139 request, network_delegate); | 139 request, network_delegate); |
140 if (appcache_job) | 140 if (appcache_job) |
141 return appcache_job; | 141 return appcache_job; |
142 return MockHttpServer::CreateJob(request, network_delegate); | 142 return MockHttpServer::CreateJob(request, network_delegate); |
143 } | 143 } |
144 private: | 144 private: |
145 scoped_ptr<net::URLRequestInterceptor> appcache_start_interceptor_; | 145 scoped_ptr<net::URLRequestInterceptor> appcache_start_interceptor_; |
146 }; | 146 }; |
147 | 147 |
148 class IOThread : public base::Thread { | 148 class IOThread : public base::Thread { |
149 public: | 149 public: |
150 explicit IOThread(const char* name) | 150 explicit IOThread(const char* name) |
151 : base::Thread(name) { | 151 : base::Thread(name) { |
152 } | 152 } |
153 | 153 |
154 virtual ~IOThread() { | 154 ~IOThread() override { Stop(); } |
155 Stop(); | |
156 } | |
157 | 155 |
158 net::URLRequestContext* request_context() { | 156 net::URLRequestContext* request_context() { |
159 return request_context_.get(); | 157 return request_context_.get(); |
160 } | 158 } |
161 | 159 |
162 virtual void Init() override { | 160 void Init() override { |
163 scoped_ptr<net::URLRequestJobFactoryImpl> factory( | 161 scoped_ptr<net::URLRequestJobFactoryImpl> factory( |
164 new net::URLRequestJobFactoryImpl()); | 162 new net::URLRequestJobFactoryImpl()); |
165 factory->SetProtocolHandler( | 163 factory->SetProtocolHandler( |
166 "http", | 164 "http", |
167 new MockHttpServerJobFactory( | 165 new MockHttpServerJobFactory( |
168 AppCacheInterceptor::CreateStartInterceptor())); | 166 AppCacheInterceptor::CreateStartInterceptor())); |
169 job_factory_ = factory.Pass(); | 167 job_factory_ = factory.Pass(); |
170 request_context_.reset(new net::TestURLRequestContext()); | 168 request_context_.reset(new net::TestURLRequestContext()); |
171 request_context_->set_job_factory(job_factory_.get()); | 169 request_context_->set_job_factory(job_factory_.get()); |
172 AppCacheInterceptor::EnsureRegistered(); | 170 AppCacheInterceptor::EnsureRegistered(); |
173 } | 171 } |
174 | 172 |
175 virtual void CleanUp() override { | 173 void CleanUp() override { |
176 request_context_.reset(); | 174 request_context_.reset(); |
177 job_factory_.reset(); | 175 job_factory_.reset(); |
178 } | 176 } |
179 | 177 |
180 private: | 178 private: |
181 scoped_ptr<net::URLRequestJobFactory> job_factory_; | 179 scoped_ptr<net::URLRequestJobFactory> job_factory_; |
182 scoped_ptr<net::URLRequestContext> request_context_; | 180 scoped_ptr<net::URLRequestContext> request_context_; |
183 }; | 181 }; |
184 | 182 |
185 scoped_ptr<IOThread> io_thread; | 183 scoped_ptr<IOThread> io_thread; |
186 scoped_ptr<base::Thread> db_thread; | 184 scoped_ptr<base::Thread> db_thread; |
187 | 185 |
188 } // namespace | 186 } // namespace |
189 | 187 |
190 class AppCacheStorageImplTest : public testing::Test { | 188 class AppCacheStorageImplTest : public testing::Test { |
191 public: | 189 public: |
192 class MockStorageDelegate : public AppCacheStorage::Delegate { | 190 class MockStorageDelegate : public AppCacheStorage::Delegate { |
193 public: | 191 public: |
194 explicit MockStorageDelegate(AppCacheStorageImplTest* test) | 192 explicit MockStorageDelegate(AppCacheStorageImplTest* test) |
195 : loaded_cache_id_(0), stored_group_success_(false), | 193 : loaded_cache_id_(0), stored_group_success_(false), |
196 would_exceed_quota_(false), obsoleted_success_(false), | 194 would_exceed_quota_(false), obsoleted_success_(false), |
197 found_cache_id_(kAppCacheNoCacheId), test_(test) { | 195 found_cache_id_(kAppCacheNoCacheId), test_(test) { |
198 } | 196 } |
199 | 197 |
200 virtual void OnCacheLoaded(AppCache* cache, int64 cache_id) override { | 198 void OnCacheLoaded(AppCache* cache, int64 cache_id) override { |
201 loaded_cache_ = cache; | 199 loaded_cache_ = cache; |
202 loaded_cache_id_ = cache_id; | 200 loaded_cache_id_ = cache_id; |
203 test_->ScheduleNextTask(); | 201 test_->ScheduleNextTask(); |
204 } | 202 } |
205 | 203 |
206 virtual void OnGroupLoaded(AppCacheGroup* group, | 204 void OnGroupLoaded(AppCacheGroup* group, |
207 const GURL& manifest_url) override { | 205 const GURL& manifest_url) override { |
208 loaded_group_ = group; | 206 loaded_group_ = group; |
209 loaded_manifest_url_ = manifest_url; | 207 loaded_manifest_url_ = manifest_url; |
210 loaded_groups_newest_cache_ = group ? group->newest_complete_cache() | 208 loaded_groups_newest_cache_ = group ? group->newest_complete_cache() |
211 : NULL; | 209 : NULL; |
212 test_->ScheduleNextTask(); | 210 test_->ScheduleNextTask(); |
213 } | 211 } |
214 | 212 |
215 virtual void OnGroupAndNewestCacheStored( | 213 void OnGroupAndNewestCacheStored(AppCacheGroup* group, |
216 AppCacheGroup* group, AppCache* newest_cache, bool success, | 214 AppCache* newest_cache, |
217 bool would_exceed_quota) override { | 215 bool success, |
| 216 bool would_exceed_quota) override { |
218 stored_group_ = group; | 217 stored_group_ = group; |
219 stored_group_success_ = success; | 218 stored_group_success_ = success; |
220 would_exceed_quota_ = would_exceed_quota; | 219 would_exceed_quota_ = would_exceed_quota; |
221 test_->ScheduleNextTask(); | 220 test_->ScheduleNextTask(); |
222 } | 221 } |
223 | 222 |
224 virtual void OnGroupMadeObsolete(AppCacheGroup* group, | 223 void OnGroupMadeObsolete(AppCacheGroup* group, |
225 bool success, | 224 bool success, |
226 int response_code) override { | 225 int response_code) override { |
227 obsoleted_group_ = group; | 226 obsoleted_group_ = group; |
228 obsoleted_success_ = success; | 227 obsoleted_success_ = success; |
229 test_->ScheduleNextTask(); | 228 test_->ScheduleNextTask(); |
230 } | 229 } |
231 | 230 |
232 virtual void OnMainResponseFound(const GURL& url, | 231 void OnMainResponseFound(const GURL& url, |
233 const AppCacheEntry& entry, | 232 const AppCacheEntry& entry, |
234 const GURL& namespace_entry_url, | 233 const GURL& namespace_entry_url, |
235 const AppCacheEntry& fallback_entry, | 234 const AppCacheEntry& fallback_entry, |
236 int64 cache_id, | 235 int64 cache_id, |
237 int64 group_id, | 236 int64 group_id, |
238 const GURL& manifest_url) override { | 237 const GURL& manifest_url) override { |
239 found_url_ = url; | 238 found_url_ = url; |
240 found_entry_ = entry; | 239 found_entry_ = entry; |
241 found_namespace_entry_url_ = namespace_entry_url; | 240 found_namespace_entry_url_ = namespace_entry_url; |
242 found_fallback_entry_ = fallback_entry; | 241 found_fallback_entry_ = fallback_entry; |
243 found_cache_id_ = cache_id; | 242 found_cache_id_ = cache_id; |
244 found_group_id_ = group_id; | 243 found_group_id_ = group_id; |
245 found_manifest_url_ = manifest_url; | 244 found_manifest_url_ = manifest_url; |
246 test_->ScheduleNextTask(); | 245 test_->ScheduleNextTask(); |
247 } | 246 } |
248 | 247 |
(...skipping 20 matching lines...) Expand all Loading... |
269 class MockQuotaManager : public storage::QuotaManager { | 268 class MockQuotaManager : public storage::QuotaManager { |
270 public: | 269 public: |
271 MockQuotaManager() | 270 MockQuotaManager() |
272 : QuotaManager(true /* is_incognito */, | 271 : QuotaManager(true /* is_incognito */, |
273 base::FilePath(), | 272 base::FilePath(), |
274 io_thread->message_loop_proxy().get(), | 273 io_thread->message_loop_proxy().get(), |
275 db_thread->message_loop_proxy().get(), | 274 db_thread->message_loop_proxy().get(), |
276 NULL), | 275 NULL), |
277 async_(false) {} | 276 async_(false) {} |
278 | 277 |
279 virtual void GetUsageAndQuota( | 278 void GetUsageAndQuota(const GURL& origin, |
280 const GURL& origin, | 279 storage::StorageType type, |
281 storage::StorageType type, | 280 const GetUsageAndQuotaCallback& callback) override { |
282 const GetUsageAndQuotaCallback& callback) override { | |
283 EXPECT_EQ(storage::kStorageTypeTemporary, type); | 281 EXPECT_EQ(storage::kStorageTypeTemporary, type); |
284 if (async_) { | 282 if (async_) { |
285 base::MessageLoop::current()->PostTask( | 283 base::MessageLoop::current()->PostTask( |
286 FROM_HERE, | 284 FROM_HERE, |
287 base::Bind(&MockQuotaManager::CallCallback, | 285 base::Bind(&MockQuotaManager::CallCallback, |
288 base::Unretained(this), | 286 base::Unretained(this), |
289 callback)); | 287 callback)); |
290 return; | 288 return; |
291 } | 289 } |
292 CallCallback(callback); | 290 CallCallback(callback); |
293 } | 291 } |
294 | 292 |
295 void CallCallback(const GetUsageAndQuotaCallback& callback) { | 293 void CallCallback(const GetUsageAndQuotaCallback& callback) { |
296 callback.Run(storage::kQuotaStatusOk, 0, kMockQuota); | 294 callback.Run(storage::kQuotaStatusOk, 0, kMockQuota); |
297 } | 295 } |
298 | 296 |
299 bool async_; | 297 bool async_; |
300 | 298 |
301 protected: | 299 protected: |
302 virtual ~MockQuotaManager() {} | 300 ~MockQuotaManager() override {} |
303 }; | 301 }; |
304 | 302 |
305 class MockQuotaManagerProxy : public storage::QuotaManagerProxy { | 303 class MockQuotaManagerProxy : public storage::QuotaManagerProxy { |
306 public: | 304 public: |
307 MockQuotaManagerProxy() | 305 MockQuotaManagerProxy() |
308 : QuotaManagerProxy(NULL, NULL), | 306 : QuotaManagerProxy(NULL, NULL), |
309 notify_storage_accessed_count_(0), | 307 notify_storage_accessed_count_(0), |
310 notify_storage_modified_count_(0), | 308 notify_storage_modified_count_(0), |
311 last_delta_(0), | 309 last_delta_(0), |
312 mock_manager_(new MockQuotaManager) { | 310 mock_manager_(new MockQuotaManager) { |
313 manager_ = mock_manager_.get(); | 311 manager_ = mock_manager_.get(); |
314 } | 312 } |
315 | 313 |
316 virtual void NotifyStorageAccessed(storage::QuotaClient::ID client_id, | 314 void NotifyStorageAccessed(storage::QuotaClient::ID client_id, |
317 const GURL& origin, | 315 const GURL& origin, |
318 storage::StorageType type) override { | 316 storage::StorageType type) override { |
319 EXPECT_EQ(storage::QuotaClient::kAppcache, client_id); | 317 EXPECT_EQ(storage::QuotaClient::kAppcache, client_id); |
320 EXPECT_EQ(storage::kStorageTypeTemporary, type); | 318 EXPECT_EQ(storage::kStorageTypeTemporary, type); |
321 ++notify_storage_accessed_count_; | 319 ++notify_storage_accessed_count_; |
322 last_origin_ = origin; | 320 last_origin_ = origin; |
323 } | 321 } |
324 | 322 |
325 virtual void NotifyStorageModified(storage::QuotaClient::ID client_id, | 323 void NotifyStorageModified(storage::QuotaClient::ID client_id, |
326 const GURL& origin, | 324 const GURL& origin, |
327 storage::StorageType type, | 325 storage::StorageType type, |
328 int64 delta) override { | 326 int64 delta) override { |
329 EXPECT_EQ(storage::QuotaClient::kAppcache, client_id); | 327 EXPECT_EQ(storage::QuotaClient::kAppcache, client_id); |
330 EXPECT_EQ(storage::kStorageTypeTemporary, type); | 328 EXPECT_EQ(storage::kStorageTypeTemporary, type); |
331 ++notify_storage_modified_count_; | 329 ++notify_storage_modified_count_; |
332 last_origin_ = origin; | 330 last_origin_ = origin; |
333 last_delta_ = delta; | 331 last_delta_ = delta; |
334 } | 332 } |
335 | 333 |
336 // Not needed for our tests. | 334 // Not needed for our tests. |
337 virtual void RegisterClient(storage::QuotaClient* client) override {} | 335 void RegisterClient(storage::QuotaClient* client) override {} |
338 virtual void NotifyOriginInUse(const GURL& origin) override {} | 336 void NotifyOriginInUse(const GURL& origin) override {} |
339 virtual void NotifyOriginNoLongerInUse(const GURL& origin) override {} | 337 void NotifyOriginNoLongerInUse(const GURL& origin) override {} |
340 virtual void SetUsageCacheEnabled(storage::QuotaClient::ID client_id, | 338 void SetUsageCacheEnabled(storage::QuotaClient::ID client_id, |
341 const GURL& origin, | 339 const GURL& origin, |
342 storage::StorageType type, | 340 storage::StorageType type, |
343 bool enabled) override {} | 341 bool enabled) override {} |
344 virtual void GetUsageAndQuota( | 342 void GetUsageAndQuota(base::SequencedTaskRunner* original_task_runner, |
345 base::SequencedTaskRunner* original_task_runner, | 343 const GURL& origin, |
346 const GURL& origin, | 344 storage::StorageType type, |
347 storage::StorageType type, | 345 const GetUsageAndQuotaCallback& callback) override {} |
348 const GetUsageAndQuotaCallback& callback) override {} | |
349 | 346 |
350 int notify_storage_accessed_count_; | 347 int notify_storage_accessed_count_; |
351 int notify_storage_modified_count_; | 348 int notify_storage_modified_count_; |
352 GURL last_origin_; | 349 GURL last_origin_; |
353 int last_delta_; | 350 int last_delta_; |
354 scoped_refptr<MockQuotaManager> mock_manager_; | 351 scoped_refptr<MockQuotaManager> mock_manager_; |
355 | 352 |
356 protected: | 353 protected: |
357 virtual ~MockQuotaManagerProxy() {} | 354 ~MockQuotaManagerProxy() override {} |
358 }; | 355 }; |
359 | 356 |
360 template <class Method> | 357 template <class Method> |
361 void RunMethod(Method method) { | 358 void RunMethod(Method method) { |
362 (this->*method)(); | 359 (this->*method)(); |
363 } | 360 } |
364 | 361 |
365 // Helper callback to run a test on our io_thread. The io_thread is spun up | 362 // Helper callback to run a test on our io_thread. The io_thread is spun up |
366 // once and reused for all tests. | 363 // once and reused for all tests. |
367 template <class Method> | 364 template <class Method> |
(...skipping 1238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1606 // These tests are somewhat of a system integration test. | 1603 // These tests are somewhat of a system integration test. |
1607 // They rely on running a mock http server on our IO thread, | 1604 // They rely on running a mock http server on our IO thread, |
1608 // and involves other appcache classes to get some code | 1605 // and involves other appcache classes to get some code |
1609 // coverage thruout when Reinitialize happens. | 1606 // coverage thruout when Reinitialize happens. |
1610 | 1607 |
1611 class MockServiceObserver : public AppCacheServiceImpl::Observer { | 1608 class MockServiceObserver : public AppCacheServiceImpl::Observer { |
1612 public: | 1609 public: |
1613 explicit MockServiceObserver(AppCacheStorageImplTest* test) | 1610 explicit MockServiceObserver(AppCacheStorageImplTest* test) |
1614 : test_(test) {} | 1611 : test_(test) {} |
1615 | 1612 |
1616 virtual void OnServiceReinitialized( | 1613 void OnServiceReinitialized( |
1617 AppCacheStorageReference* old_storage_ref) override { | 1614 AppCacheStorageReference* old_storage_ref) override { |
1618 observed_old_storage_ = old_storage_ref; | 1615 observed_old_storage_ = old_storage_ref; |
1619 test_->ScheduleNextTask(); | 1616 test_->ScheduleNextTask(); |
1620 } | 1617 } |
1621 | 1618 |
1622 scoped_refptr<AppCacheStorageReference> observed_old_storage_; | 1619 scoped_refptr<AppCacheStorageReference> observed_old_storage_; |
1623 AppCacheStorageImplTest* test_; | 1620 AppCacheStorageImplTest* test_; |
1624 }; | 1621 }; |
1625 | 1622 |
1626 class MockAppCacheFrontend : public AppCacheFrontend { | 1623 class MockAppCacheFrontend : public AppCacheFrontend { |
1627 public: | 1624 public: |
1628 MockAppCacheFrontend() : error_event_was_raised_(false) {} | 1625 MockAppCacheFrontend() : error_event_was_raised_(false) {} |
1629 | 1626 |
1630 virtual void OnCacheSelected( | 1627 void OnCacheSelected(int host_id, const AppCacheInfo& info) override {} |
1631 int host_id, const AppCacheInfo& info) override {} | 1628 void OnStatusChanged(const std::vector<int>& host_ids, |
1632 virtual void OnStatusChanged(const std::vector<int>& host_ids, | 1629 AppCacheStatus status) override {} |
1633 AppCacheStatus status) override {} | 1630 void OnEventRaised(const std::vector<int>& host_ids, |
1634 virtual void OnEventRaised(const std::vector<int>& host_ids, | 1631 AppCacheEventID event_id) override {} |
1635 AppCacheEventID event_id) override {} | 1632 void OnProgressEventRaised(const std::vector<int>& host_ids, |
1636 virtual void OnProgressEventRaised( | 1633 const GURL& url, |
1637 const std::vector<int>& host_ids, | 1634 int num_total, |
1638 const GURL& url, | 1635 int num_complete) override {} |
1639 int num_total, int num_complete) override {} | 1636 void OnErrorEventRaised(const std::vector<int>& host_ids, |
1640 virtual void OnErrorEventRaised(const std::vector<int>& host_ids, | 1637 const AppCacheErrorDetails& details) override { |
1641 const AppCacheErrorDetails& details) | |
1642 override { | |
1643 error_event_was_raised_ = true; | 1638 error_event_was_raised_ = true; |
1644 } | 1639 } |
1645 virtual void OnLogMessage(int host_id, AppCacheLogLevel log_level, | 1640 void OnLogMessage(int host_id, |
1646 const std::string& message) override {} | 1641 AppCacheLogLevel log_level, |
1647 virtual void OnContentBlocked( | 1642 const std::string& message) override {} |
1648 int host_id, const GURL& manifest_url) override {} | 1643 void OnContentBlocked(int host_id, const GURL& manifest_url) override {} |
1649 | 1644 |
1650 bool error_event_was_raised_; | 1645 bool error_event_was_raised_; |
1651 }; | 1646 }; |
1652 | 1647 |
1653 enum ReinitTestCase { | 1648 enum ReinitTestCase { |
1654 CORRUPT_CACHE_ON_INSTALL, | 1649 CORRUPT_CACHE_ON_INSTALL, |
1655 CORRUPT_CACHE_ON_LOAD_EXISTING, | 1650 CORRUPT_CACHE_ON_LOAD_EXISTING, |
1656 CORRUPT_SQL_ON_INSTALL | 1651 CORRUPT_SQL_ON_INSTALL |
1657 }; | 1652 }; |
1658 | 1653 |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2031 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize2); | 2026 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize2); |
2032 } | 2027 } |
2033 | 2028 |
2034 TEST_F(AppCacheStorageImplTest, Reinitialize3) { | 2029 TEST_F(AppCacheStorageImplTest, Reinitialize3) { |
2035 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize3); | 2030 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize3); |
2036 } | 2031 } |
2037 | 2032 |
2038 // That's all folks! | 2033 // That's all folks! |
2039 | 2034 |
2040 } // namespace content | 2035 } // namespace content |
OLD | NEW |