[Mac] Fixes a bug where instant suggest text would disappear on every other keystroke.
[chromium-blink-merge.git] / webkit / appcache / appcache_unittest.cc
blob5e1de6be29192a03284f453d0164a295995acb0f
1 // Copyright (c) 2009 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, 111));
20 cache->set_complete(true);
21 scoped_refptr<AppCacheGroup> group(
22 new AppCacheGroup(&service, GURL("http://blah/manifest"), 111));
23 group->AddCache(cache);
25 AppCacheHost host1(1, &frontend, &service);
26 AppCacheHost host2(2, &frontend, &service);
28 host1.AssociateCache(cache.get());
29 host2.AssociateCache(cache.get());
31 host1.AssociateCache(NULL);
32 host2.AssociateCache(NULL);
35 TEST(AppCacheTest, AddModifyRemoveEntry) {
36 MockAppCacheService service;
37 scoped_refptr<AppCache> cache(new AppCache(&service, 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, 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 FallbackNamespace(GURL("http://fb1.com"), GURL("http://fbone.com")));
92 manifest.online_whitelist_namespaces.push_back(GURL("http://w1.com"));
93 manifest.online_whitelist_namespaces.push_back(GURL("http://w2.com"));
94 manifest.online_whitelist_all = true;
96 cache->InitializeWithManifest(&manifest);
97 const std::vector<FallbackNamespace>& fallbacks =
98 cache->fallback_namespaces_;
99 size_t expected = 1;
100 EXPECT_EQ(expected, fallbacks.size());
101 EXPECT_EQ(GURL("http://fb1.com"), fallbacks[0].first);
102 EXPECT_EQ(GURL("http://fbone.com"), fallbacks[0].second);
103 const std::vector<GURL>& whitelist = cache->online_whitelist_namespaces_;
104 expected = 2;
105 EXPECT_EQ(expected, whitelist.size());
106 EXPECT_EQ(GURL("http://w1.com"), whitelist[0]);
107 EXPECT_EQ(GURL("http://w2.com"), whitelist[1]);
108 EXPECT_TRUE(cache->online_whitelist_all_);
110 // Ensure collections in manifest were taken over by the cache rather than
111 // copied.
112 EXPECT_TRUE(manifest.fallback_namespaces.empty());
113 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
116 TEST(AppCacheTest, FindResponseForRequest) {
117 MockAppCacheService service;
119 const GURL kOnlineNamespaceUrl("http://blah/online_namespace");
120 const GURL kFallbackEntryUrl1("http://blah/fallback_entry1");
121 const GURL kFallbackNamespaceUrl1("http://blah/fallback_namespace/");
122 const GURL kFallbackEntryUrl2("http://blah/fallback_entry2");
123 const GURL kFallbackNamespaceUrl2("http://blah/fallback_namespace/longer");
124 const GURL kManifestUrl("http://blah/manifest");
125 const GURL kForeignExplicitEntryUrl("http://blah/foreign");
126 const GURL kInOnlineNamespaceUrl(
127 "http://blah/online_namespace/network");
128 const GURL kExplicitInOnlineNamespaceUrl(
129 "http://blah/online_namespace/explicit");
130 const GURL kFallbackTestUrl1("http://blah/fallback_namespace/1");
131 const GURL kFallbackTestUrl2("http://blah/fallback_namespace/longer2");
132 const GURL kOnlineNamespaceWithinFallback(
133 "http://blah/fallback_namespace/1/online");
135 const int64 kFallbackResponseId1 = 1;
136 const int64 kFallbackResponseId2 = 2;
137 const int64 kManifestResponseId = 3;
138 const int64 kForeignExplicitResponseId = 4;
139 const int64 kExplicitInOnlineNamespaceResponseId = 5;
141 Manifest manifest;
142 manifest.online_whitelist_namespaces.push_back(kOnlineNamespaceUrl);
143 manifest.online_whitelist_namespaces.push_back(
144 kOnlineNamespaceWithinFallback);
145 manifest.fallback_namespaces.push_back(
146 FallbackNamespace(kFallbackNamespaceUrl1, kFallbackEntryUrl1));
147 manifest.fallback_namespaces.push_back(
148 FallbackNamespace(kFallbackNamespaceUrl2, kFallbackEntryUrl2));
150 // Create a cache with some namespaces and entries.
151 scoped_refptr<AppCache> cache(new AppCache(&service, 1234));
152 cache->InitializeWithManifest(&manifest);
153 cache->AddEntry(
154 kFallbackEntryUrl1,
155 AppCacheEntry(AppCacheEntry::FALLBACK, kFallbackResponseId1));
156 cache->AddEntry(
157 kFallbackEntryUrl2,
158 AppCacheEntry(AppCacheEntry::FALLBACK, kFallbackResponseId2));
159 cache->AddEntry(
160 kManifestUrl,
161 AppCacheEntry(AppCacheEntry::MANIFEST, kManifestResponseId));
162 cache->AddEntry(
163 kForeignExplicitEntryUrl,
164 AppCacheEntry(AppCacheEntry::EXPLICIT | AppCacheEntry::FOREIGN,
165 kForeignExplicitResponseId));
166 cache->AddEntry(
167 kExplicitInOnlineNamespaceUrl,
168 AppCacheEntry(AppCacheEntry::EXPLICIT,
169 kExplicitInOnlineNamespaceResponseId));
170 cache->set_complete(true);
172 // See that we get expected results from FindResponseForRequest
174 bool found = false;
175 AppCacheEntry entry;
176 AppCacheEntry fallback_entry;
177 GURL fallback_namespace;
178 bool network_namespace = false;
180 found = cache->FindResponseForRequest(GURL("http://blah/miss"),
181 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
182 EXPECT_FALSE(found);
184 found = cache->FindResponseForRequest(kForeignExplicitEntryUrl,
185 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
186 EXPECT_TRUE(found);
187 EXPECT_EQ(kForeignExplicitResponseId, entry.response_id());
188 EXPECT_FALSE(fallback_entry.has_response_id());
189 EXPECT_FALSE(network_namespace);
191 entry = AppCacheEntry(); // reset
193 found = cache->FindResponseForRequest(kManifestUrl,
194 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
195 EXPECT_TRUE(found);
196 EXPECT_EQ(kManifestResponseId, entry.response_id());
197 EXPECT_FALSE(fallback_entry.has_response_id());
198 EXPECT_FALSE(network_namespace);
200 entry = AppCacheEntry(); // reset
202 found = cache->FindResponseForRequest(kInOnlineNamespaceUrl,
203 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
204 EXPECT_TRUE(found);
205 EXPECT_FALSE(entry.has_response_id());
206 EXPECT_FALSE(fallback_entry.has_response_id());
207 EXPECT_TRUE(network_namespace);
209 network_namespace = false; // reset
211 found = cache->FindResponseForRequest(kExplicitInOnlineNamespaceUrl,
212 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
213 EXPECT_TRUE(found);
214 EXPECT_EQ(kExplicitInOnlineNamespaceResponseId, entry.response_id());
215 EXPECT_FALSE(fallback_entry.has_response_id());
216 EXPECT_FALSE(network_namespace);
218 entry = AppCacheEntry(); // reset
220 found = cache->FindResponseForRequest(kFallbackTestUrl1,
221 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
222 EXPECT_TRUE(found);
223 EXPECT_FALSE(entry.has_response_id());
224 EXPECT_EQ(kFallbackResponseId1, fallback_entry.response_id());
225 EXPECT_EQ(kFallbackEntryUrl1,
226 cache->GetFallbackEntryUrl(fallback_namespace));
227 EXPECT_FALSE(network_namespace);
229 fallback_entry = AppCacheEntry(); // reset
231 found = cache->FindResponseForRequest(kFallbackTestUrl2,
232 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
233 EXPECT_TRUE(found);
234 EXPECT_FALSE(entry.has_response_id());
235 EXPECT_EQ(kFallbackResponseId2, fallback_entry.response_id());
236 EXPECT_EQ(kFallbackEntryUrl2,
237 cache->GetFallbackEntryUrl(fallback_namespace));
238 EXPECT_FALSE(network_namespace);
240 fallback_entry = AppCacheEntry(); // reset
242 found = cache->FindResponseForRequest(kOnlineNamespaceWithinFallback,
243 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
244 EXPECT_TRUE(found);
245 EXPECT_FALSE(entry.has_response_id());
246 EXPECT_FALSE(fallback_entry.has_response_id());
247 EXPECT_TRUE(network_namespace);
249 fallback_entry = AppCacheEntry(); // reset
251 found = cache->FindResponseForRequest(
252 kOnlineNamespaceWithinFallback.Resolve("online_resource"),
253 &entry, &fallback_entry, &fallback_namespace, &network_namespace);
254 EXPECT_TRUE(found);
255 EXPECT_FALSE(entry.has_response_id());
256 EXPECT_FALSE(fallback_entry.has_response_id());
257 EXPECT_TRUE(network_namespace);
260 } // namespace appacache