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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 10 #include "content/browser/appcache/appcache_group.h" |
| 11 #include "content/browser/appcache/appcache_host.h" |
| 12 #include "content/browser/appcache/appcache_response.h" |
| 13 #include "content/browser/appcache/appcache_update_job.h" |
10 #include "content/browser/appcache/mock_appcache_service.h" | 14 #include "content/browser/appcache/mock_appcache_service.h" |
11 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
12 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
13 #include "net/url_request/url_request_error_job.h" | 17 #include "net/url_request/url_request_error_job.h" |
14 #include "net/url_request/url_request_job_factory_impl.h" | 18 #include "net/url_request/url_request_job_factory_impl.h" |
15 #include "net/url_request/url_request_test_job.h" | 19 #include "net/url_request/url_request_test_job.h" |
16 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "webkit/browser/appcache/appcache_group.h" | |
19 #include "webkit/browser/appcache/appcache_host.h" | |
20 #include "webkit/browser/appcache/appcache_response.h" | |
21 #include "webkit/browser/appcache/appcache_update_job.h" | |
22 | |
23 using appcache::AppCache; | |
24 using appcache::AppCacheEntry; | |
25 using appcache::AppCacheFrontend; | |
26 using appcache::AppCacheHost; | |
27 using appcache::AppCacheGroup; | |
28 using appcache::AppCacheResponseInfo; | |
29 using appcache::AppCacheUpdateJob; | |
30 using appcache::AppCacheResponseWriter; | |
31 using appcache::APPCACHE_CACHED_EVENT; | |
32 using appcache::APPCACHE_CHECKING_EVENT; | |
33 using appcache::APPCACHE_DOWNLOADING_EVENT; | |
34 using appcache::APPCACHE_ERROR_EVENT; | |
35 using appcache::AppCacheEventID; | |
36 using appcache::APPCACHE_FALLBACK_NAMESPACE; | |
37 using appcache::HttpResponseInfoIOBuffer; | |
38 using appcache::kAppCacheNoCacheId; | |
39 using appcache::kAppCacheNoResponseId; | |
40 using appcache::Namespace; | |
41 using appcache::APPCACHE_NETWORK_NAMESPACE; | |
42 using appcache::APPCACHE_NO_UPDATE_EVENT; | |
43 using appcache::APPCACHE_OBSOLETE_EVENT; | |
44 using appcache::APPCACHE_PROGRESS_EVENT; | |
45 using appcache::APPCACHE_UPDATE_READY_EVENT; | |
46 using appcache::AppCacheStatus; | |
47 | 22 |
48 namespace content { | 23 namespace content { |
49 class AppCacheUpdateJobTest; | 24 class AppCacheUpdateJobTest; |
50 | 25 |
51 namespace { | 26 namespace { |
52 | 27 |
53 const char kManifest1Contents[] = | 28 const char kManifest1Contents[] = |
54 "CACHE MANIFEST\n" | 29 "CACHE MANIFEST\n" |
55 "explicit1\n" | 30 "explicit1\n" |
56 "FALLBACK:\n" | 31 "FALLBACK:\n" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 class MockHttpServerJobFactory | 195 class MockHttpServerJobFactory |
221 : public net::URLRequestJobFactory::ProtocolHandler { | 196 : public net::URLRequestJobFactory::ProtocolHandler { |
222 public: | 197 public: |
223 virtual net::URLRequestJob* MaybeCreateJob( | 198 virtual net::URLRequestJob* MaybeCreateJob( |
224 net::URLRequest* request, | 199 net::URLRequest* request, |
225 net::NetworkDelegate* network_delegate) const OVERRIDE { | 200 net::NetworkDelegate* network_delegate) const OVERRIDE { |
226 return MockHttpServer::JobFactory(request, network_delegate); | 201 return MockHttpServer::JobFactory(request, network_delegate); |
227 } | 202 } |
228 }; | 203 }; |
229 | 204 |
230 inline bool operator==(const Namespace& lhs, const Namespace& rhs) { | 205 inline bool operator==(const AppCacheNamespace& lhs, |
| 206 const AppCacheNamespace& rhs) { |
231 return lhs.type == rhs.type && | 207 return lhs.type == rhs.type && |
232 lhs.namespace_url == rhs.namespace_url && | 208 lhs.namespace_url == rhs.namespace_url && |
233 lhs.target_url == rhs.target_url; | 209 lhs.target_url == rhs.target_url; |
234 } | 210 } |
235 | 211 |
236 } // namespace | 212 } // namespace |
237 | 213 |
238 class MockFrontend : public AppCacheFrontend { | 214 class MockFrontend : public AppCacheFrontend { |
239 public: | 215 public: |
240 MockFrontend() | 216 MockFrontend() |
241 : ignore_progress_events_(false), verify_progress_events_(false), | 217 : ignore_progress_events_(false), verify_progress_events_(false), |
242 last_progress_total_(-1), last_progress_complete_(-1), | 218 last_progress_total_(-1), last_progress_complete_(-1), |
243 start_update_trigger_(APPCACHE_CHECKING_EVENT), update_(NULL) { | 219 start_update_trigger_(APPCACHE_CHECKING_EVENT), update_(NULL) { |
244 } | 220 } |
245 | 221 |
246 virtual void OnCacheSelected( | 222 virtual void OnCacheSelected( |
247 int host_id, const appcache::AppCacheInfo& info) OVERRIDE { | 223 int host_id, const AppCacheInfo& info) OVERRIDE { |
248 } | 224 } |
249 | 225 |
250 virtual void OnStatusChanged(const std::vector<int>& host_ids, | 226 virtual void OnStatusChanged(const std::vector<int>& host_ids, |
251 AppCacheStatus status) OVERRIDE { | 227 AppCacheStatus status) OVERRIDE { |
252 } | 228 } |
253 | 229 |
254 virtual void OnEventRaised(const std::vector<int>& host_ids, | 230 virtual void OnEventRaised(const std::vector<int>& host_ids, |
255 AppCacheEventID event_id) OVERRIDE { | 231 AppCacheEventID event_id) OVERRIDE { |
256 raised_events_.push_back(RaisedEvent(host_ids, event_id)); | 232 raised_events_.push_back(RaisedEvent(host_ids, event_id)); |
257 | 233 |
258 // Trigger additional updates if requested. | 234 // Trigger additional updates if requested. |
259 if (event_id == start_update_trigger_ && update_) { | 235 if (event_id == start_update_trigger_ && update_) { |
260 for (std::vector<AppCacheHost*>::iterator it = update_hosts_.begin(); | 236 for (std::vector<AppCacheHost*>::iterator it = update_hosts_.begin(); |
261 it != update_hosts_.end(); ++it) { | 237 it != update_hosts_.end(); ++it) { |
262 AppCacheHost* host = *it; | 238 AppCacheHost* host = *it; |
263 update_->StartUpdate(host, | 239 update_->StartUpdate(host, |
264 (host ? host->pending_master_entry_url() : GURL())); | 240 (host ? host->pending_master_entry_url() : GURL())); |
265 } | 241 } |
266 update_hosts_.clear(); // only trigger once | 242 update_hosts_.clear(); // only trigger once |
267 } | 243 } |
268 } | 244 } |
269 | 245 |
270 virtual void OnErrorEventRaised(const std::vector<int>& host_ids, | 246 virtual void OnErrorEventRaised(const std::vector<int>& host_ids, |
271 const appcache::AppCacheErrorDetails& details) | 247 const AppCacheErrorDetails& details) |
272 OVERRIDE { | 248 OVERRIDE { |
273 error_message_ = details.message; | 249 error_message_ = details.message; |
274 OnEventRaised(host_ids, APPCACHE_ERROR_EVENT); | 250 OnEventRaised(host_ids, APPCACHE_ERROR_EVENT); |
275 } | 251 } |
276 | 252 |
277 virtual void OnProgressEventRaised(const std::vector<int>& host_ids, | 253 virtual void OnProgressEventRaised(const std::vector<int>& host_ids, |
278 const GURL& url, | 254 const GURL& url, |
279 int num_total, | 255 int num_total, |
280 int num_complete) OVERRIDE { | 256 int num_complete) OVERRIDE { |
281 if (!ignore_progress_events_) | 257 if (!ignore_progress_events_) |
(...skipping 17 matching lines...) Expand all Loading... |
299 EXPECT_TRUE(url.is_empty()); | 275 EXPECT_TRUE(url.is_empty()); |
300 else | 276 else |
301 EXPECT_TRUE(url.is_valid()); | 277 EXPECT_TRUE(url.is_valid()); |
302 | 278 |
303 last_progress_total_ = num_total; | 279 last_progress_total_ = num_total; |
304 last_progress_complete_ = num_complete; | 280 last_progress_complete_ = num_complete; |
305 } | 281 } |
306 } | 282 } |
307 | 283 |
308 virtual void OnLogMessage(int host_id, | 284 virtual void OnLogMessage(int host_id, |
309 appcache::AppCacheLogLevel log_level, | 285 AppCacheLogLevel log_level, |
310 const std::string& message) OVERRIDE { | 286 const std::string& message) OVERRIDE { |
311 } | 287 } |
312 | 288 |
313 virtual void OnContentBlocked(int host_id, | 289 virtual void OnContentBlocked(int host_id, |
314 const GURL& manifest_url) OVERRIDE { | 290 const GURL& manifest_url) OVERRIDE { |
315 } | 291 } |
316 | 292 |
317 void AddExpectedEvent(const std::vector<int>& host_ids, | 293 void AddExpectedEvent(const std::vector<int>& host_ids, |
318 AppCacheEventID event_id) { | 294 AppCacheEventID event_id) { |
319 DCHECK(!ignore_progress_events_ || event_id != APPCACHE_PROGRESS_EVENT); | 295 DCHECK(!ignore_progress_events_ || event_id != APPCACHE_PROGRESS_EVENT); |
(...skipping 2946 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3266 for (AppCache::EntryMap::iterator i = expect_extra_entries_.begin(); | 3242 for (AppCache::EntryMap::iterator i = expect_extra_entries_.begin(); |
3267 i != expect_extra_entries_.end(); ++i) { | 3243 i != expect_extra_entries_.end(); ++i) { |
3268 entry = cache->GetEntry(i->first); | 3244 entry = cache->GetEntry(i->first); |
3269 ASSERT_TRUE(entry); | 3245 ASSERT_TRUE(entry); |
3270 EXPECT_EQ(i->second.types(), entry->types()); | 3246 EXPECT_EQ(i->second.types(), entry->types()); |
3271 } | 3247 } |
3272 | 3248 |
3273 expected = 1; | 3249 expected = 1; |
3274 ASSERT_EQ(expected, cache->fallback_namespaces_.size()); | 3250 ASSERT_EQ(expected, cache->fallback_namespaces_.size()); |
3275 EXPECT_TRUE(cache->fallback_namespaces_[0] == | 3251 EXPECT_TRUE(cache->fallback_namespaces_[0] == |
3276 Namespace( | 3252 AppCacheNamespace( |
3277 APPCACHE_FALLBACK_NAMESPACE, | 3253 APPCACHE_FALLBACK_NAMESPACE, |
3278 MockHttpServer::GetMockUrl("files/fallback1"), | 3254 MockHttpServer::GetMockUrl("files/fallback1"), |
3279 MockHttpServer::GetMockUrl("files/fallback1a"), | 3255 MockHttpServer::GetMockUrl("files/fallback1a"), |
3280 false)); | 3256 false)); |
3281 | 3257 |
3282 EXPECT_TRUE(cache->online_whitelist_namespaces_.empty()); | 3258 EXPECT_TRUE(cache->online_whitelist_namespaces_.empty()); |
3283 EXPECT_TRUE(cache->online_whitelist_all_); | 3259 EXPECT_TRUE(cache->online_whitelist_all_); |
3284 | 3260 |
3285 EXPECT_TRUE(cache->update_time_ > base::Time()); | 3261 EXPECT_TRUE(cache->update_time_ > base::Time()); |
3286 } | 3262 } |
3287 | 3263 |
3288 void VerifyManifestMergedTypes(AppCache* cache) { | 3264 void VerifyManifestMergedTypes(AppCache* cache) { |
3289 size_t expected = 2; | 3265 size_t expected = 2; |
3290 EXPECT_EQ(expected, cache->entries().size()); | 3266 EXPECT_EQ(expected, cache->entries().size()); |
3291 AppCacheEntry* entry = cache->GetEntry( | 3267 AppCacheEntry* entry = cache->GetEntry( |
3292 MockHttpServer::GetMockUrl("files/manifest-merged-types")); | 3268 MockHttpServer::GetMockUrl("files/manifest-merged-types")); |
3293 ASSERT_TRUE(entry); | 3269 ASSERT_TRUE(entry); |
3294 EXPECT_EQ(AppCacheEntry::EXPLICIT | AppCacheEntry::MANIFEST, | 3270 EXPECT_EQ(AppCacheEntry::EXPLICIT | AppCacheEntry::MANIFEST, |
3295 entry->types()); | 3271 entry->types()); |
3296 entry = cache->GetEntry(MockHttpServer::GetMockUrl("files/explicit1")); | 3272 entry = cache->GetEntry(MockHttpServer::GetMockUrl("files/explicit1")); |
3297 ASSERT_TRUE(entry); | 3273 ASSERT_TRUE(entry); |
3298 EXPECT_EQ(AppCacheEntry::EXPLICIT | AppCacheEntry::FALLBACK | | 3274 EXPECT_EQ(AppCacheEntry::EXPLICIT | AppCacheEntry::FALLBACK | |
3299 AppCacheEntry::MASTER, entry->types()); | 3275 AppCacheEntry::MASTER, entry->types()); |
3300 | 3276 |
3301 expected = 1; | 3277 expected = 1; |
3302 ASSERT_EQ(expected, cache->fallback_namespaces_.size()); | 3278 ASSERT_EQ(expected, cache->fallback_namespaces_.size()); |
3303 EXPECT_TRUE(cache->fallback_namespaces_[0] == | 3279 EXPECT_TRUE(cache->fallback_namespaces_[0] == |
3304 Namespace( | 3280 AppCacheNamespace( |
3305 APPCACHE_FALLBACK_NAMESPACE, | 3281 APPCACHE_FALLBACK_NAMESPACE, |
3306 MockHttpServer::GetMockUrl("files/fallback1"), | 3282 MockHttpServer::GetMockUrl("files/fallback1"), |
3307 MockHttpServer::GetMockUrl("files/explicit1"), | 3283 MockHttpServer::GetMockUrl("files/explicit1"), |
3308 false)); | 3284 false)); |
3309 | 3285 |
3310 EXPECT_EQ(expected, cache->online_whitelist_namespaces_.size()); | 3286 EXPECT_EQ(expected, cache->online_whitelist_namespaces_.size()); |
3311 EXPECT_TRUE(cache->online_whitelist_namespaces_[0] == | 3287 EXPECT_TRUE(cache->online_whitelist_namespaces_[0] == |
3312 Namespace( | 3288 AppCacheNamespace( |
3313 APPCACHE_NETWORK_NAMESPACE, | 3289 APPCACHE_NETWORK_NAMESPACE, |
3314 MockHttpServer::GetMockUrl("files/online1"), | 3290 MockHttpServer::GetMockUrl("files/online1"), |
3315 GURL(), false)); | 3291 GURL(), false)); |
3316 EXPECT_FALSE(cache->online_whitelist_all_); | 3292 EXPECT_FALSE(cache->online_whitelist_all_); |
3317 | 3293 |
3318 EXPECT_TRUE(cache->update_time_ > base::Time()); | 3294 EXPECT_TRUE(cache->update_time_ > base::Time()); |
3319 } | 3295 } |
3320 | 3296 |
3321 void VerifyEmptyManifest(AppCache* cache) { | 3297 void VerifyEmptyManifest(AppCache* cache) { |
3322 const char* kManifestPath = tested_manifest_path_override_ ? | 3298 const char* kManifestPath = tested_manifest_path_override_ ? |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3491 MockFrontend::RaisedEvents events = mock_frontend.raised_events_; | 3467 MockFrontend::RaisedEvents events = mock_frontend.raised_events_; |
3492 size_t expected = 2; | 3468 size_t expected = 2; |
3493 EXPECT_EQ(expected, events.size()); | 3469 EXPECT_EQ(expected, events.size()); |
3494 expected = 1; | 3470 expected = 1; |
3495 EXPECT_EQ(expected, events[0].first.size()); | 3471 EXPECT_EQ(expected, events[0].first.size()); |
3496 EXPECT_EQ(host.host_id(), events[0].first[0]); | 3472 EXPECT_EQ(host.host_id(), events[0].first[0]); |
3497 EXPECT_EQ(APPCACHE_CHECKING_EVENT, events[0].second); | 3473 EXPECT_EQ(APPCACHE_CHECKING_EVENT, events[0].second); |
3498 | 3474 |
3499 EXPECT_EQ(expected, events[1].first.size()); | 3475 EXPECT_EQ(expected, events[1].first.size()); |
3500 EXPECT_EQ(host.host_id(), events[1].first[0]); | 3476 EXPECT_EQ(host.host_id(), events[1].first[0]); |
3501 EXPECT_EQ(appcache::APPCACHE_DOWNLOADING_EVENT, events[1].second); | 3477 EXPECT_EQ(APPCACHE_DOWNLOADING_EVENT, events[1].second); |
3502 | 3478 |
3503 EXPECT_EQ(AppCacheGroup::DOWNLOADING, group->update_status()); | 3479 EXPECT_EQ(AppCacheGroup::DOWNLOADING, group->update_status()); |
3504 } | 3480 } |
3505 | 3481 |
3506 TEST_F(AppCacheUpdateJobTest, StartCacheAttempt) { | 3482 TEST_F(AppCacheUpdateJobTest, StartCacheAttempt) { |
3507 RunTestOnIOThread(&AppCacheUpdateJobTest::StartCacheAttemptTest); | 3483 RunTestOnIOThread(&AppCacheUpdateJobTest::StartCacheAttemptTest); |
3508 } | 3484 } |
3509 | 3485 |
3510 TEST_F(AppCacheUpdateJobTest, StartUpgradeAttempt) { | 3486 TEST_F(AppCacheUpdateJobTest, StartUpgradeAttempt) { |
3511 RunTestOnIOThread(&AppCacheUpdateJobTest::StartUpgradeAttemptTest); | 3487 RunTestOnIOThread(&AppCacheUpdateJobTest::StartUpgradeAttemptTest); |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3711 | 3687 |
3712 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsSuccess) { | 3688 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsSuccess) { |
3713 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsSuccessTest); | 3689 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsSuccessTest); |
3714 } | 3690 } |
3715 | 3691 |
3716 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsDenied) { | 3692 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsDenied) { |
3717 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsDeniedTest); | 3693 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsDeniedTest); |
3718 } | 3694 } |
3719 | 3695 |
3720 } // namespace content | 3696 } // namespace content |
OLD | NEW |