Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / appcache / appcache_unittest.cc
blobd097a4585ced2c9a2e3a6291384656df432689af
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 "testing/gtest/include/gtest/gtest.h"
6 #include "webkit/appcache/appcache.h"
7 #include "webkit/appcache/appcache_frontend_impl.h"
8 #include "webkit/appcache/appcache_host.h"
9 #include "webkit/appcache/mock_appcache_service.h"
11 namespace appcache {
13 class AppCacheTest : public testing::Test {
16 TEST(AppCacheTest, CleanupUnusedCache) {
17 MockAppCacheService service;
18 AppCacheFrontendImpl frontend;
19 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111));
20 cache->set_complete(true);
21 scoped_refptr<AppCacheGroup> group(
22 new AppCacheGroup(service.storage(), GURL("http://blah/manifest"), 111));
23 group->AddCache(cache);
25 AppCacheHost host1(1, &frontend, &service);
26 AppCacheHost host2(2, &frontend, &service);
28 host1.AssociateCompleteCache(cache.get());
29 host2.AssociateCompleteCache(cache.get());
31 host1.AssociateNoCache(GURL());
32 host2.AssociateNoCache(GURL());
35 TEST(AppCacheTest, AddModifyRemoveEntry) {
36 MockAppCacheService service;
37 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111));
39 EXPECT_TRUE(cache->entries().empty());
40 EXPECT_EQ(0L, cache->cache_size());
42 const GURL kFooUrl("http://foo.com");
43 const int64 kFooResponseId = 1;
44 const int64 kFooSize = 100;
45 AppCacheEntry entry1(AppCacheEntry::MASTER, kFooResponseId, kFooSize);
46 cache->AddEntry(kFooUrl, entry1);
47 EXPECT_EQ(entry1.types(), cache->GetEntry(kFooUrl)->types());
48 EXPECT_EQ(1UL, cache->entries().size());
49 EXPECT_EQ(kFooSize, cache->cache_size());
51 const GURL kBarUrl("http://bar.com");
52 const int64 kBarResponseId = 2;
53 const int64 kBarSize = 200;
54 AppCacheEntry entry2(AppCacheEntry::FALLBACK, kBarResponseId, kBarSize);
55 EXPECT_TRUE(cache->AddOrModifyEntry(kBarUrl, entry2));
56 EXPECT_EQ(entry2.types(), cache->GetEntry(kBarUrl)->types());
57 EXPECT_EQ(2UL, cache->entries().size());
58 EXPECT_EQ(kFooSize + kBarSize, cache->cache_size());
60 // Expected to return false when an existing entry is modified.
61 AppCacheEntry entry3(AppCacheEntry::EXPLICIT);
62 EXPECT_FALSE(cache->AddOrModifyEntry(kFooUrl, entry3));
63 EXPECT_EQ((AppCacheEntry::MASTER | AppCacheEntry::EXPLICIT),
64 cache->GetEntry(kFooUrl)->types());
65 // Only the type should be modified.
66 EXPECT_EQ(kFooResponseId, cache->GetEntry(kFooUrl)->response_id());
67 EXPECT_EQ(kFooSize, cache->GetEntry(kFooUrl)->response_size());
68 EXPECT_EQ(kFooSize + kBarSize, cache->cache_size());
70 EXPECT_EQ(entry2.types(), cache->GetEntry(kBarUrl)->types()); // unchanged
72 cache->RemoveEntry(kBarUrl);
73 EXPECT_EQ(kFooSize, cache->cache_size());
74 cache->RemoveEntry(kFooUrl);
75 EXPECT_EQ(0L, cache->cache_size());
76 EXPECT_TRUE(cache->entries().empty());
79 TEST(AppCacheTest, InitializeWithManifest) {
80 MockAppCacheService service;
82 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
83 EXPECT_TRUE(cache->fallback_namespaces_.empty());
84 EXPECT_TRUE(cache->online_whitelist_namespaces_.empty());
85 EXPECT_FALSE(cache->online_whitelist_all_);
87 Manifest manifest;
88 manifest.explicit_urls.insert("http://one.com");
89 manifest.explicit_urls.insert("http://two.com");
90 manifest.fallback_namespaces.push_back(
91 Namespace(FALLBACK_NAMESPACE, GURL("http://fb1.com"),
92 GURL("http://fbone.com"), true));
93 manifest.online_whitelist_namespaces.push_back(
94 Namespace(NETWORK_NAMESPACE, GURL("http://w1.com"), GURL(), false));
95 manifest.online_whitelist_namespaces.push_back(
96 Namespace(NETWORK_NAMESPACE, GURL("http://w2.com"), GURL(), false));
97 manifest.online_whitelist_all = true;
99 cache->InitializeWithManifest(&manifest);
100 const std::vector<Namespace>& fallbacks =
101 cache->fallback_namespaces_;
102 size_t expected = 1;
103 EXPECT_EQ(expected, fallbacks.size());
104 EXPECT_EQ(GURL("http://fb1.com"), fallbacks[0].namespace_url);
105 EXPECT_EQ(GURL("http://fbone.com"), fallbacks[0].target_url);
106 EXPECT_TRUE(fallbacks[0].is_pattern);
107 const NamespaceVector& whitelist = cache->online_whitelist_namespaces_;
108 expected = 2;
109 EXPECT_EQ(expected, whitelist.size());
110 EXPECT_EQ(GURL("http://w1.com"), whitelist[0].namespace_url);
111 EXPECT_EQ(GURL("http://w2.com"), whitelist[1].namespace_url);
112 EXPECT_TRUE(cache->online_whitelist_all_);
114 // Ensure collections in manifest were taken over by the cache rather than
115 // copied.
116 EXPECT_TRUE(manifest.fallback_namespaces.empty());
117 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
120 TEST(AppCacheTest, FindResponseForRequest) {
121 MockAppCacheService service;
123 const GURL kOnlineNamespaceUrl("http://blah/online_namespace");
124 const GURL kFallbackEntryUrl1("http://blah/fallback_entry1");
125 const GURL kFallbackNamespaceUrl1("http://blah/fallback_namespace/");
126 const GURL kFallbackEntryUrl2("http://blah/fallback_entry2");
127 const GURL kFallbackNamespaceUrl2("http://blah/fallback_namespace/longer");
128 const GURL kManifestUrl("http://blah/manifest");
129 const GURL kForeignExplicitEntryUrl("http://blah/foreign");
130 const GURL kInOnlineNamespaceUrl(
131 "http://blah/online_namespace/network");
132 const GURL kExplicitInOnlineNamespaceUrl(
133 "http://blah/online_namespace/explicit");
134 const GURL kFallbackTestUrl1("http://blah/fallback_namespace/1");
135 const GURL kFallbackTestUrl2("http://blah/fallback_namespace/longer2");
136 const GURL kInterceptNamespace("http://blah/intercept_namespace/");
137 const GURL kInterceptNamespaceWithinFallback(
138 "http://blah/fallback_namespace/intercept_namespace/");
139 const GURL kInterceptNamespaceEntry("http://blah/intercept_entry");
140 const GURL kOnlineNamespaceWithinOtherNamespaces(
141 "http://blah/fallback_namespace/intercept_namespace/1/online");
143 const int64 kFallbackResponseId1 = 1;
144 const int64 kFallbackResponseId2 = 2;
145 const int64 kManifestResponseId = 3;
146 const int64 kForeignExplicitResponseId = 4;
147 const int64 kExplicitInOnlineNamespaceResponseId = 5;
148 const int64 kInterceptResponseId = 6;
150 Manifest manifest;
151 manifest.online_whitelist_namespaces.push_back(
152 Namespace(NETWORK_NAMESPACE, kOnlineNamespaceUrl,
153 GURL(), false));
154 manifest.online_whitelist_namespaces.push_back(
155 Namespace(NETWORK_NAMESPACE, kOnlineNamespaceWithinOtherNamespaces,
156 GURL(), false));
157 manifest.fallback_namespaces.push_back(
158 Namespace(FALLBACK_NAMESPACE, kFallbackNamespaceUrl1,
159 kFallbackEntryUrl1, false));
160 manifest.fallback_namespaces.push_back(
161 Namespace(FALLBACK_NAMESPACE, kFallbackNamespaceUrl2,
162 kFallbackEntryUrl2, false));
163 manifest.intercept_namespaces.push_back(
164 Namespace(INTERCEPT_NAMESPACE, kInterceptNamespace,
165 kInterceptNamespaceEntry, false));
166 manifest.intercept_namespaces.push_back(
167 Namespace(INTERCEPT_NAMESPACE, kInterceptNamespaceWithinFallback,
168 kInterceptNamespaceEntry, false));
170 // Create a cache with some namespaces and entries.
171 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
172 cache->InitializeWithManifest(&manifest);
173 cache->AddEntry(
174 kFallbackEntryUrl1,
175 AppCacheEntry(AppCacheEntry::FALLBACK, kFallbackResponseId1));
176 cache->AddEntry(
177 kFallbackEntryUrl2,
178 AppCacheEntry(AppCacheEntry::FALLBACK, kFallbackResponseId2));
179 cache->AddEntry(
180 kManifestUrl,
181 AppCacheEntry(AppCacheEntry::MANIFEST, kManifestResponseId));
182 cache->AddEntry(
183 kForeignExplicitEntryUrl,
184 AppCacheEntry(AppCacheEntry::EXPLICIT | AppCacheEntry::FOREIGN,
185 kForeignExplicitResponseId));
186 cache->AddEntry(
187 kExplicitInOnlineNamespaceUrl,
188 AppCacheEntry(AppCacheEntry::EXPLICIT,
189 kExplicitInOnlineNamespaceResponseId));
190 cache->AddEntry(
191 kInterceptNamespaceEntry,
192 AppCacheEntry(AppCacheEntry::INTERCEPT, kInterceptResponseId));
193 cache->set_complete(true);
195 // See that we get expected results from FindResponseForRequest
197 bool found = false;
198 AppCacheEntry entry;
199 AppCacheEntry fallback_entry;
200 GURL intercept_namespace;
201 GURL fallback_namespace;
202 bool network_namespace = false;
204 found = cache->FindResponseForRequest(GURL("http://blah/miss"),
205 &entry, &intercept_namespace,
206 &fallback_entry, &fallback_namespace,
207 &network_namespace);
208 EXPECT_FALSE(found);
210 found = cache->FindResponseForRequest(kForeignExplicitEntryUrl,
211 &entry, &intercept_namespace,
212 &fallback_entry, &fallback_namespace,
213 &network_namespace);
214 EXPECT_TRUE(found);
215 EXPECT_EQ(kForeignExplicitResponseId, entry.response_id());
216 EXPECT_FALSE(fallback_entry.has_response_id());
217 EXPECT_FALSE(network_namespace);
219 entry = AppCacheEntry(); // reset
221 found = cache->FindResponseForRequest(kManifestUrl,
222 &entry, &intercept_namespace,
223 &fallback_entry, &fallback_namespace,
224 &network_namespace);
225 EXPECT_TRUE(found);
226 EXPECT_EQ(kManifestResponseId, entry.response_id());
227 EXPECT_FALSE(fallback_entry.has_response_id());
228 EXPECT_FALSE(network_namespace);
230 entry = AppCacheEntry(); // reset
232 found = cache->FindResponseForRequest(kInOnlineNamespaceUrl,
233 &entry, &intercept_namespace,
234 &fallback_entry, &fallback_namespace,
235 &network_namespace);
236 EXPECT_TRUE(found);
237 EXPECT_FALSE(entry.has_response_id());
238 EXPECT_FALSE(fallback_entry.has_response_id());
239 EXPECT_TRUE(network_namespace);
241 network_namespace = false; // reset
243 found = cache->FindResponseForRequest(kExplicitInOnlineNamespaceUrl,
244 &entry, &intercept_namespace,
245 &fallback_entry, &fallback_namespace,
246 &network_namespace);
247 EXPECT_TRUE(found);
248 EXPECT_EQ(kExplicitInOnlineNamespaceResponseId, entry.response_id());
249 EXPECT_FALSE(fallback_entry.has_response_id());
250 EXPECT_FALSE(network_namespace);
252 entry = AppCacheEntry(); // reset
254 found = cache->FindResponseForRequest(kFallbackTestUrl1,
255 &entry, &intercept_namespace,
256 &fallback_entry, &fallback_namespace,
257 &network_namespace);
258 EXPECT_TRUE(found);
259 EXPECT_FALSE(entry.has_response_id());
260 EXPECT_EQ(kFallbackResponseId1, fallback_entry.response_id());
261 EXPECT_EQ(kFallbackEntryUrl1,
262 cache->GetFallbackEntryUrl(fallback_namespace));
263 EXPECT_FALSE(network_namespace);
265 fallback_entry = AppCacheEntry(); // reset
267 found = cache->FindResponseForRequest(kFallbackTestUrl2,
268 &entry, &intercept_namespace,
269 &fallback_entry, &fallback_namespace,
270 &network_namespace);
271 EXPECT_TRUE(found);
272 EXPECT_FALSE(entry.has_response_id());
273 EXPECT_EQ(kFallbackResponseId2, fallback_entry.response_id());
274 EXPECT_EQ(kFallbackEntryUrl2,
275 cache->GetFallbackEntryUrl(fallback_namespace));
276 EXPECT_FALSE(network_namespace);
278 fallback_entry = AppCacheEntry(); // reset
280 found = cache->FindResponseForRequest(kOnlineNamespaceWithinOtherNamespaces,
281 &entry, &intercept_namespace,
282 &fallback_entry, &fallback_namespace,
283 &network_namespace);
284 EXPECT_TRUE(found);
285 EXPECT_FALSE(entry.has_response_id());
286 EXPECT_FALSE(fallback_entry.has_response_id());
287 EXPECT_TRUE(network_namespace);
289 fallback_entry = AppCacheEntry(); // reset
291 found = cache->FindResponseForRequest(
292 kOnlineNamespaceWithinOtherNamespaces.Resolve("online_resource"),
293 &entry, &intercept_namespace,
294 &fallback_entry, &fallback_namespace,
295 &network_namespace);
296 EXPECT_TRUE(found);
297 EXPECT_FALSE(entry.has_response_id());
298 EXPECT_FALSE(fallback_entry.has_response_id());
299 EXPECT_TRUE(network_namespace);
301 fallback_namespace = GURL();
303 found = cache->FindResponseForRequest(
304 kInterceptNamespace.Resolve("intercept_me"),
305 &entry, &intercept_namespace,
306 &fallback_entry, &fallback_namespace,
307 &network_namespace);
308 EXPECT_TRUE(found);
309 EXPECT_EQ(kInterceptResponseId, entry.response_id());
310 EXPECT_EQ(kInterceptNamespaceEntry,
311 cache->GetInterceptEntryUrl(intercept_namespace));
312 EXPECT_FALSE(fallback_entry.has_response_id());
313 EXPECT_TRUE(fallback_namespace.is_empty());
314 EXPECT_FALSE(network_namespace);
316 entry = AppCacheEntry(); // reset
318 found = cache->FindResponseForRequest(
319 kInterceptNamespaceWithinFallback.Resolve("intercept_me"),
320 &entry, &intercept_namespace,
321 &fallback_entry, &fallback_namespace,
322 &network_namespace);
323 EXPECT_TRUE(found);
324 EXPECT_EQ(kInterceptResponseId, entry.response_id());
325 EXPECT_EQ(kInterceptNamespaceEntry,
326 cache->GetInterceptEntryUrl(intercept_namespace));
327 EXPECT_FALSE(fallback_entry.has_response_id());
328 EXPECT_TRUE(fallback_namespace.is_empty());
329 EXPECT_FALSE(network_namespace);
332 TEST(AppCacheTest, FindInterceptPatternResponseForRequest) {
333 MockAppCacheService service;
335 // Setup an appcache with an intercept namespace that uses pattern matching.
336 const GURL kInterceptNamespaceBase("http://blah/intercept_namespace/");
337 const GURL kInterceptPatternNamespace(
338 kInterceptNamespaceBase.Resolve("*.hit*"));
339 const GURL kInterceptNamespaceEntry("http://blah/intercept_resource");
340 const int64 kInterceptResponseId = 1;
341 Manifest manifest;
342 manifest.intercept_namespaces.push_back(
343 Namespace(INTERCEPT_NAMESPACE, kInterceptPatternNamespace,
344 kInterceptNamespaceEntry, true));
345 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
346 cache->InitializeWithManifest(&manifest);
347 cache->AddEntry(
348 kInterceptNamespaceEntry,
349 AppCacheEntry(AppCacheEntry::INTERCEPT, kInterceptResponseId));
350 cache->set_complete(true);
352 // See that the pattern match works.
353 bool found = false;
354 AppCacheEntry entry;
355 AppCacheEntry fallback_entry;
356 GURL intercept_namespace;
357 GURL fallback_namespace;
358 bool network_namespace = false;
360 found = cache->FindResponseForRequest(
361 GURL("http://blah/miss"),
362 &entry, &intercept_namespace,
363 &fallback_entry, &fallback_namespace,
364 &network_namespace);
365 EXPECT_FALSE(found);
367 found = cache->FindResponseForRequest(
368 GURL("http://blah/intercept_namespace/another_miss"),
369 &entry, &intercept_namespace,
370 &fallback_entry, &fallback_namespace,
371 &network_namespace);
372 EXPECT_FALSE(found);
374 found = cache->FindResponseForRequest(
375 GURL("http://blah/intercept_namespace/path.hit"),
376 &entry, &intercept_namespace,
377 &fallback_entry, &fallback_namespace,
378 &network_namespace);
379 EXPECT_TRUE(found);
380 EXPECT_EQ(kInterceptResponseId, entry.response_id());
381 EXPECT_EQ(kInterceptNamespaceEntry,
382 cache->GetInterceptEntryUrl(intercept_namespace));
383 EXPECT_FALSE(fallback_entry.has_response_id());
384 EXPECT_TRUE(fallback_namespace.is_empty());
385 EXPECT_FALSE(network_namespace);
387 entry = AppCacheEntry(); // reset
389 found = cache->FindResponseForRequest(
390 GURL("http://blah/intercept_namespace/longer/path.hit?arg=ok"),
391 &entry, &intercept_namespace,
392 &fallback_entry, &fallback_namespace,
393 &network_namespace);
394 EXPECT_TRUE(found);
395 EXPECT_EQ(kInterceptResponseId, entry.response_id());
396 EXPECT_EQ(kInterceptNamespaceEntry,
397 cache->GetInterceptEntryUrl(intercept_namespace));
398 EXPECT_FALSE(fallback_entry.has_response_id());
399 EXPECT_TRUE(fallback_namespace.is_empty());
400 EXPECT_FALSE(network_namespace);
403 TEST(AppCacheTest, FindFallbackPatternResponseForRequest) {
404 MockAppCacheService service;
406 // Setup an appcache with a fallback namespace that uses pattern matching.
407 const GURL kFallbackNamespaceBase("http://blah/fallback_namespace/");
408 const GURL kFallbackPatternNamespace(
409 kFallbackNamespaceBase.Resolve("*.hit*"));
410 const GURL kFallbackNamespaceEntry("http://blah/fallback_resource");
411 const int64 kFallbackResponseId = 1;
412 Manifest manifest;
413 manifest.fallback_namespaces.push_back(
414 Namespace(FALLBACK_NAMESPACE, kFallbackPatternNamespace,
415 kFallbackNamespaceEntry, true));
416 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
417 cache->InitializeWithManifest(&manifest);
418 cache->AddEntry(
419 kFallbackNamespaceEntry,
420 AppCacheEntry(AppCacheEntry::FALLBACK, kFallbackResponseId));
421 cache->set_complete(true);
423 // See that the pattern match works.
424 bool found = false;
425 AppCacheEntry entry;
426 AppCacheEntry fallback_entry;
427 GURL intercept_namespace;
428 GURL fallback_namespace;
429 bool network_namespace = false;
431 found = cache->FindResponseForRequest(
432 GURL("http://blah/miss"),
433 &entry, &intercept_namespace,
434 &fallback_entry, &fallback_namespace,
435 &network_namespace);
436 EXPECT_FALSE(found);
438 found = cache->FindResponseForRequest(
439 GURL("http://blah/fallback_namespace/another_miss"),
440 &entry, &intercept_namespace,
441 &fallback_entry, &fallback_namespace,
442 &network_namespace);
443 EXPECT_FALSE(found);
445 found = cache->FindResponseForRequest(
446 GURL("http://blah/fallback_namespace/path.hit"),
447 &entry, &intercept_namespace,
448 &fallback_entry, &fallback_namespace,
449 &network_namespace);
450 EXPECT_TRUE(found);
451 EXPECT_FALSE(entry.has_response_id());
452 EXPECT_EQ(kFallbackResponseId, fallback_entry.response_id());
453 EXPECT_EQ(kFallbackNamespaceEntry,
454 cache->GetFallbackEntryUrl(fallback_namespace));
455 EXPECT_FALSE(network_namespace);
457 fallback_entry = AppCacheEntry();
458 fallback_namespace = GURL();
460 found = cache->FindResponseForRequest(
461 GURL("http://blah/fallback_namespace/longer/path.hit?arg=ok"),
462 &entry, &intercept_namespace,
463 &fallback_entry, &fallback_namespace,
464 &network_namespace);
465 EXPECT_TRUE(found);
466 EXPECT_FALSE(entry.has_response_id());
467 EXPECT_EQ(kFallbackResponseId, fallback_entry.response_id());
468 EXPECT_EQ(kFallbackNamespaceEntry,
469 cache->GetFallbackEntryUrl(fallback_namespace));
470 EXPECT_TRUE(intercept_namespace.is_empty());
471 EXPECT_FALSE(network_namespace);
475 TEST(AppCacheTest, FindNetworkNamespacePatternResponseForRequest) {
476 MockAppCacheService service;
478 // Setup an appcache with a network namespace that uses pattern matching.
479 const GURL kNetworkNamespaceBase("http://blah/network_namespace/");
480 const GURL kNetworkPatternNamespace(
481 kNetworkNamespaceBase.Resolve("*.hit*"));
482 Manifest manifest;
483 manifest.online_whitelist_namespaces.push_back(
484 Namespace(NETWORK_NAMESPACE, kNetworkPatternNamespace,
485 GURL(), true));
486 manifest.online_whitelist_all = false;
487 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
488 cache->InitializeWithManifest(&manifest);
489 cache->set_complete(true);
491 // See that the pattern match works.
492 bool found = false;
493 AppCacheEntry entry;
494 AppCacheEntry fallback_entry;
495 GURL intercept_namespace;
496 GURL fallback_namespace;
497 bool network_namespace = false;
499 found = cache->FindResponseForRequest(
500 GURL("http://blah/miss"),
501 &entry, &intercept_namespace,
502 &fallback_entry, &fallback_namespace,
503 &network_namespace);
504 EXPECT_FALSE(found);
506 found = cache->FindResponseForRequest(
507 GURL("http://blah/network_namespace/path.hit"),
508 &entry, &intercept_namespace,
509 &fallback_entry, &fallback_namespace,
510 &network_namespace);
511 EXPECT_TRUE(found);
512 EXPECT_TRUE(network_namespace);
513 EXPECT_FALSE(entry.has_response_id());
514 EXPECT_FALSE(fallback_entry.has_response_id());
517 TEST(AppCacheTest, ToFromDatabaseRecords) {
518 // Setup a cache with some entries.
519 const int64 kCacheId = 1234;
520 const int64 kGroupId = 4321;
521 const GURL kManifestUrl("http://foo.com/manifest");
522 const GURL kInterceptUrl("http://foo.com/intercept.html");
523 const GURL kFallbackUrl("http://foo.com/fallback.html");
524 const GURL kWhitelistUrl("http://foo.com/whitelist*");
525 const std::string kData(
526 "CACHE MANIFEST\r"
527 "CHROMIUM-INTERCEPT:\r"
528 "/intercept return /intercept.html\r"
529 "FALLBACK:\r"
530 "/ /fallback.html\r"
531 "NETWORK:\r"
532 "/whitelist* isPattern\r"
533 "*\r");
534 MockAppCacheService service;
535 scoped_refptr<AppCacheGroup> group =
536 new AppCacheGroup(service.storage(), kManifestUrl, kGroupId);
537 scoped_refptr<AppCache> cache(new AppCache(service.storage(), kCacheId));
538 Manifest manifest;
539 EXPECT_TRUE(
540 ParseManifest(kManifestUrl, kData.c_str(), kData.length(), manifest));
541 cache->InitializeWithManifest(&manifest);
542 EXPECT_EQ(NETWORK_NAMESPACE, cache->online_whitelist_namespaces_[0].type);
543 EXPECT_TRUE(cache->online_whitelist_namespaces_[0].is_pattern);
544 EXPECT_EQ(kWhitelistUrl,
545 cache->online_whitelist_namespaces_[0].namespace_url);
546 cache->AddEntry(
547 kManifestUrl,
548 AppCacheEntry(AppCacheEntry::MANIFEST, 1, 1));
549 cache->AddEntry(
550 kInterceptUrl,
551 AppCacheEntry(AppCacheEntry::INTERCEPT, 3, 3));
552 cache->AddEntry(
553 kFallbackUrl,
554 AppCacheEntry(AppCacheEntry::FALLBACK, 2, 2));
556 // Get it to produce database records and verify them.
557 AppCacheDatabase::CacheRecord cache_record;
558 std::vector<AppCacheDatabase::EntryRecord> entries;
559 std::vector<AppCacheDatabase::NamespaceRecord> intercepts;
560 std::vector<AppCacheDatabase::NamespaceRecord> fallbacks;
561 std::vector<AppCacheDatabase::OnlineWhiteListRecord> whitelists;
562 cache->ToDatabaseRecords(
563 group, &cache_record, &entries,
564 &intercepts, &fallbacks, &whitelists);
565 EXPECT_EQ(kCacheId, cache_record.cache_id);
566 EXPECT_EQ(kGroupId, cache_record.group_id);
567 EXPECT_TRUE(cache_record.online_wildcard);
568 EXPECT_EQ(1 + 2 + 3, cache_record.cache_size);
569 EXPECT_EQ(3u, entries.size());
570 EXPECT_EQ(1u, intercepts.size());
571 EXPECT_EQ(1u, fallbacks.size());
572 EXPECT_EQ(1u, whitelists.size());
573 cache = NULL;
575 // Create a new AppCache and populate it with those records and verify.
576 cache = new AppCache(service.storage(), kCacheId);
577 cache->InitializeWithDatabaseRecords(
578 cache_record, entries, intercepts,
579 fallbacks, whitelists);
580 EXPECT_TRUE(cache->online_whitelist_all_);
581 EXPECT_EQ(3u, cache->entries().size());
582 EXPECT_TRUE(cache->GetEntry(kManifestUrl));
583 EXPECT_TRUE(cache->GetEntry(kInterceptUrl));
584 EXPECT_TRUE(cache->GetEntry(kFallbackUrl));
585 EXPECT_EQ(kInterceptUrl,
586 cache->GetInterceptEntryUrl(GURL("http://foo.com/intercept")));
587 EXPECT_EQ(kFallbackUrl,
588 cache->GetFallbackEntryUrl(GURL("http://foo.com/")));
589 EXPECT_EQ(1 + 2 + 3, cache->cache_size());
590 EXPECT_EQ(NETWORK_NAMESPACE, cache->online_whitelist_namespaces_[0].type);
591 EXPECT_TRUE(cache->online_whitelist_namespaces_[0].is_pattern);
592 EXPECT_EQ(kWhitelistUrl,
593 cache->online_whitelist_namespaces_[0].namespace_url);
596 TEST(AppCacheTest, IsNamespaceMatch) {
597 Namespace prefix;
598 prefix.namespace_url = GURL("http://foo.com/prefix");
599 prefix.is_pattern = false;
600 EXPECT_TRUE(prefix.IsMatch(
601 GURL("http://foo.com/prefix_and_anothing_goes")));
602 EXPECT_FALSE(prefix.IsMatch(
603 GURL("http://foo.com/nope")));
605 Namespace bar_no_star;
606 bar_no_star.namespace_url = GURL("http://foo.com/bar");
607 bar_no_star.is_pattern = true;
608 EXPECT_TRUE(bar_no_star.IsMatch(
609 GURL("http://foo.com/bar")));
610 EXPECT_FALSE(bar_no_star.IsMatch(
611 GURL("http://foo.com/bar/nope")));
613 Namespace bar_star;
614 bar_star.namespace_url = GURL("http://foo.com/bar/*");
615 bar_star.is_pattern = true;
616 EXPECT_TRUE(bar_star.IsMatch(
617 GURL("http://foo.com/bar/")));
618 EXPECT_TRUE(bar_star.IsMatch(
619 GURL("http://foo.com/bar/should_match")));
620 EXPECT_FALSE(bar_star.IsMatch(
621 GURL("http://foo.com/not_bar/should_not_match")));
623 Namespace star_bar_star;
624 star_bar_star.namespace_url = GURL("http://foo.com/*/bar/*");
625 star_bar_star.is_pattern = true;
626 EXPECT_TRUE(star_bar_star.IsMatch(
627 GURL("http://foo.com/any/bar/should_match")));
628 EXPECT_TRUE(star_bar_star.IsMatch(
629 GURL("http://foo.com/any/bar/")));
630 EXPECT_FALSE(star_bar_star.IsMatch(
631 GURL("http://foo.com/any/not_bar/no_match")));
633 Namespace query_star_edit;
634 query_star_edit.namespace_url = GURL("http://foo.com/query?id=*&verb=edit*");
635 query_star_edit.is_pattern = true;
636 EXPECT_TRUE(query_star_edit.IsMatch(
637 GURL("http://foo.com/query?id=1234&verb=edit&option=blue")));
638 EXPECT_TRUE(query_star_edit.IsMatch(
639 GURL("http://foo.com/query?id=12345&option=blue&verb=edit")));
640 EXPECT_FALSE(query_star_edit.IsMatch(
641 GURL("http://foo.com/query?id=12345&option=blue&verb=print")));
642 EXPECT_TRUE(query_star_edit.IsMatch(
643 GURL("http://foo.com/query?id=123&verb=print&verb=edit")));
645 Namespace star_greediness;
646 star_greediness.namespace_url = GURL("http://foo.com/*/b");
647 star_greediness.is_pattern = true;
648 EXPECT_TRUE(star_greediness.IsMatch(
649 GURL("http://foo.com/a/b")));
650 EXPECT_TRUE(star_greediness.IsMatch(
651 GURL("http://foo.com/a/wxy/z/b")));
652 EXPECT_TRUE(star_greediness.IsMatch(
653 GURL("http://foo.com/a/b/b")));
654 EXPECT_TRUE(star_greediness.IsMatch(
655 GURL("http://foo.com/b/b")));
656 EXPECT_TRUE(star_greediness.IsMatch(
657 GURL("http://foo.com/a/b/b/b/b/b")));
658 EXPECT_TRUE(star_greediness.IsMatch(
659 GURL("http://foo.com/a/b/b/b/a/b")));
660 EXPECT_TRUE(star_greediness.IsMatch(
661 GURL("http://foo.com/a/b/01234567890abcdef/b")));
662 EXPECT_TRUE(star_greediness.IsMatch(
663 GURL("http://foo.com/a/b/01234567890abcdef/b01234567890abcdef/b")));
664 EXPECT_TRUE(star_greediness.IsMatch(
665 GURL("http://foo.com/a/b/01234567890abcdef_eat_some_more_characters_"
666 "/and_even_more_for_the_heck_of_it/01234567890abcdef/b")));
669 } // namespace appacache