1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
// Copyright (c) 2009 The Chromium Authos. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/appcache/appcache.h"
#include "webkit/appcache/appcache_group.h"
#include "webkit/appcache/appcache_response.h"
#include "webkit/appcache/appcache_storage.h"
#include "webkit/appcache/mock_appcache_service.h"
namespace appcache {
class MockAppCacheStorageTest : public testing::Test {
public:
class MockStorageDelegate : public AppCacheStorage::Delegate {
public:
explicit MockStorageDelegate()
: loaded_cache_id_(0), stored_group_success_(false),
obsoleted_success_(false), found_cache_id_(kNoCacheId) {
}
void OnCacheLoaded(AppCache* cache, int64 cache_id) {
loaded_cache_ = cache;
loaded_cache_id_ = cache_id;
}
void OnGroupLoaded(AppCacheGroup* group, const GURL& manifest_url) {
loaded_group_ = group;
loaded_manifest_url_ = manifest_url;
}
void OnGroupAndNewestCacheStored(AppCacheGroup* group, bool success) {
stored_group_ = group;
stored_group_success_ = success;
}
void OnGroupMadeObsolete(AppCacheGroup* group, bool success) {
obsoleted_group_ = group;
obsoleted_success_ = success;
}
void OnMainResponseFound(const GURL& url, const AppCacheEntry& entry,
const AppCacheEntry& fallback_entry,
int64 cache_id, const GURL& manifest_url) {
found_url_ = url;
found_entry_ = entry;
found_fallback_entry_ = fallback_entry;
found_cache_id_ = cache_id;
found_manifest_url_ = manifest_url;
}
scoped_refptr<AppCache> loaded_cache_;
int64 loaded_cache_id_;
scoped_refptr<AppCacheGroup> loaded_group_;
GURL loaded_manifest_url_;
scoped_refptr<AppCacheGroup> stored_group_;
bool stored_group_success_;
scoped_refptr<AppCacheGroup> obsoleted_group_;
bool obsoleted_success_;
GURL found_url_;
AppCacheEntry found_entry_;
AppCacheEntry found_fallback_entry_;
int64 found_cache_id_;
GURL found_manifest_url_;
};
};
TEST_F(MockAppCacheStorageTest, LoadCache_Miss) {
// Attempt to load a cache that doesn't exist. Should
// complete asyncly.
MockAppCacheService service;
MockStorageDelegate delegate;
service.storage()->LoadCache(111, &delegate);
EXPECT_NE(111, delegate.loaded_cache_id_);
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_EQ(111, delegate.loaded_cache_id_);
EXPECT_FALSE(delegate.loaded_cache_);
}
TEST_F(MockAppCacheStorageTest, LoadCache_NearHit) {
// Attempt to load a cache that is currently in use
// and does not require loading from disk. This
// load should complete syncly.
MockAppCacheService service;
// Setup some preconditions. Make an 'unstored' cache for
// us to load. The ctor should put it in the working set.
int64 cache_id = service.storage()->NewCacheId();
scoped_refptr<AppCache> cache = new AppCache(&service, cache_id);
// Conduct the test.
MockStorageDelegate delegate;
service.storage()->LoadCache(cache_id, &delegate);
EXPECT_EQ(cache_id, delegate.loaded_cache_id_);
EXPECT_EQ(cache.get(), delegate.loaded_cache_.get());
}
TEST_F(MockAppCacheStorageTest, CreateGroup) {
// Attempt to load/create a group that doesn't exist.
// Should complete asyncly.
MockAppCacheService service;
MockAppCacheStorage* storage =
reinterpret_cast<MockAppCacheStorage*>(service.storage());
MockStorageDelegate delegate;
GURL manifest_url("http://blah/");
service.storage()->LoadOrCreateGroup(manifest_url, &delegate);
EXPECT_NE(manifest_url, delegate.loaded_manifest_url_);
EXPECT_FALSE(delegate.loaded_group_.get());
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_EQ(manifest_url, delegate.loaded_manifest_url_);
EXPECT_TRUE(delegate.loaded_group_.get());
EXPECT_TRUE(delegate.loaded_group_->HasOneRef());
EXPECT_FALSE(delegate.loaded_group_->newest_complete_cache());
EXPECT_TRUE(storage->stored_groups_.empty());
}
TEST_F(MockAppCacheStorageTest, LoadGroup_NearHit) {
// Attempt to load a group that is currently in use
// and does not require loading from disk. This
// load should complete syncly.
MockAppCacheService service;
MockStorageDelegate delegate;
// Setup some preconditions. Create a group that appears
// to be "unstored" and "currently in use".
GURL manifest_url("http://blah/");
service.storage()->LoadOrCreateGroup(manifest_url, &delegate);
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_EQ(manifest_url, delegate.loaded_manifest_url_);
EXPECT_TRUE(delegate.loaded_group_.get());
// Reset our delegate, and take a reference to the new group.
scoped_refptr<AppCacheGroup> group;
group.swap(delegate.loaded_group_);
delegate.loaded_manifest_url_ = GURL();
// Conduct the test.
service.storage()->LoadOrCreateGroup(manifest_url, &delegate);
EXPECT_EQ(manifest_url, delegate.loaded_manifest_url_);
EXPECT_EQ(group.get(), delegate.loaded_group_.get());
}
TEST_F(MockAppCacheStorageTest, LoadGroupAndCache_FarHit) {
// Attempt to load a cache that is not currently in use
// and does require loading from disk. This
// load should complete asyncly.
MockAppCacheService service;
MockAppCacheStorage* storage =
reinterpret_cast<MockAppCacheStorage*>(service.storage());
// Setup some preconditions. Create a group and newest cache that
// appears to be "stored" and "not currently in use".
GURL manifest_url("http://blah/");
scoped_refptr<AppCacheGroup> group =
new AppCacheGroup(&service, manifest_url);
int64 cache_id = storage->NewCacheId();
scoped_refptr<AppCache> cache = new AppCache(&service, cache_id);
cache->set_complete(true);
group->AddCache(cache);
storage->AddStoredGroup(group);
storage->AddStoredCache(cache);
// Drop the references from above so the only refs to these
// objects are from within the storage class. This is to make
// these objects appear as "not currently in use".
AppCache* cache_ptr = cache.get();
AppCacheGroup* group_ptr = group.get();
cache = NULL;
group = NULL;
// Setup a delegate to receive completion callbacks.
MockStorageDelegate delegate;
// Conduct the cache load test.
EXPECT_NE(cache_id, delegate.loaded_cache_id_);
EXPECT_NE(cache_ptr, delegate.loaded_cache_.get());
storage->LoadCache(cache_id, &delegate);
EXPECT_NE(cache_id, delegate.loaded_cache_id_);
EXPECT_NE(cache_ptr, delegate.loaded_cache_.get());
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_EQ(cache_id, delegate.loaded_cache_id_);
EXPECT_EQ(cache_ptr, delegate.loaded_cache_.get());
delegate.loaded_cache_ = NULL;
// Conduct the group load test.
EXPECT_NE(manifest_url, delegate.loaded_manifest_url_);
EXPECT_FALSE(delegate.loaded_group_.get());
storage->LoadOrCreateGroup(manifest_url, &delegate);
EXPECT_NE(manifest_url, delegate.loaded_manifest_url_);
EXPECT_FALSE(delegate.loaded_group_.get());
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_EQ(manifest_url, delegate.loaded_manifest_url_);
EXPECT_EQ(group_ptr, delegate.loaded_group_.get());
}
TEST_F(MockAppCacheStorageTest, StoreNewGroup) {
// Store a group and its newest cache. Should complete asyncly.
MockAppCacheService service;
MockAppCacheStorage* storage =
reinterpret_cast<MockAppCacheStorage*>(service.storage());
// Setup some preconditions. Create a group and newest cache that
// appears to be "unstored".
GURL manifest_url("http://blah/");
scoped_refptr<AppCacheGroup> group =
new AppCacheGroup(&service, manifest_url);
int64 cache_id = storage->NewCacheId();
scoped_refptr<AppCache> cache = new AppCache(&service, cache_id);
cache->set_complete(true);
// Hold a ref to the cache simulate the UpdateJob holding that ref,
// and hold a ref to the group to simulate the CacheHost holding that ref.
// Conduct the store test.
MockStorageDelegate delegate;
EXPECT_TRUE(storage->stored_caches_.empty());
EXPECT_TRUE(storage->stored_groups_.empty());
storage->StoreGroupAndNewestCache(group, cache, &delegate);
EXPECT_FALSE(delegate.stored_group_success_);
EXPECT_TRUE(storage->stored_caches_.empty());
EXPECT_TRUE(storage->stored_groups_.empty());
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_TRUE(delegate.stored_group_success_);
EXPECT_FALSE(storage->stored_caches_.empty());
EXPECT_FALSE(storage->stored_groups_.empty());
EXPECT_EQ(cache, group->newest_complete_cache());
}
TEST_F(MockAppCacheStorageTest, StoreExistingGroup) {
// Store a group and its newest cache. Should complete asyncly.
MockAppCacheService service;
MockAppCacheStorage* storage =
reinterpret_cast<MockAppCacheStorage*>(service.storage());
// Setup some preconditions. Create a group and old complete cache
// that appear to be "stored", and a newest unstored complete cache.
GURL manifest_url("http://blah/");
scoped_refptr<AppCacheGroup> group =
new AppCacheGroup(&service, manifest_url);
int64 old_cache_id = storage->NewCacheId();
scoped_refptr<AppCache> old_cache = new AppCache(&service, old_cache_id);
old_cache->set_complete(true);
group->AddCache(old_cache);
storage->AddStoredGroup(group);
storage->AddStoredCache(old_cache);
int64 new_cache_id = storage->NewCacheId();
scoped_refptr<AppCache> new_cache = new AppCache(&service, new_cache_id);
new_cache->set_complete(true);
// Hold our refs to simulate the UpdateJob holding these refs.
// Conduct the test.
MockStorageDelegate delegate;
EXPECT_EQ(size_t(1), storage->stored_caches_.size());
EXPECT_EQ(size_t(1), storage->stored_groups_.size());
EXPECT_TRUE(storage->IsCacheStored(old_cache));
EXPECT_FALSE(storage->IsCacheStored(new_cache));
storage->StoreGroupAndNewestCache(group, new_cache, &delegate);
EXPECT_FALSE(delegate.stored_group_success_);
EXPECT_EQ(size_t(1), storage->stored_caches_.size());
EXPECT_EQ(size_t(1), storage->stored_groups_.size());
EXPECT_TRUE(storage->IsCacheStored(old_cache));
EXPECT_FALSE(storage->IsCacheStored(new_cache));
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_TRUE(delegate.stored_group_success_);
EXPECT_EQ(size_t(1), storage->stored_caches_.size());
EXPECT_EQ(size_t(1), storage->stored_groups_.size());
EXPECT_FALSE(storage->IsCacheStored(old_cache));
EXPECT_TRUE(storage->IsCacheStored(new_cache));
EXPECT_EQ(new_cache.get(), group->newest_complete_cache());
}
TEST_F(MockAppCacheStorageTest, MakeGroupObsolete) {
// Make a group obsolete, should complete asyncly.
MockAppCacheService service;
MockAppCacheStorage* storage =
reinterpret_cast<MockAppCacheStorage*>(service.storage());
// Setup some preconditions. Create a group and newest cache that
// appears to be "stored" and "currently in use".
GURL manifest_url("http://blah/");
scoped_refptr<AppCacheGroup> group =
new AppCacheGroup(&service, manifest_url);
int64 cache_id = storage->NewCacheId();
scoped_refptr<AppCache> cache = new AppCache(&service, cache_id);
cache->set_complete(true);
group->AddCache(cache);
storage->AddStoredGroup(group);
storage->AddStoredCache(cache);
// Hold our refs to simulate the UpdateJob holding these refs.
// Conduct the test.
MockStorageDelegate delegate;
EXPECT_FALSE(group->is_obsolete());
EXPECT_EQ(size_t(1), storage->stored_caches_.size());
EXPECT_EQ(size_t(1), storage->stored_groups_.size());
EXPECT_FALSE(cache->HasOneRef());
EXPECT_FALSE(group->HasOneRef());
storage->MakeGroupObsolete(group, &delegate);
EXPECT_FALSE(group->is_obsolete());
EXPECT_EQ(size_t(1), storage->stored_caches_.size());
EXPECT_EQ(size_t(1), storage->stored_groups_.size());
EXPECT_FALSE(cache->HasOneRef());
EXPECT_FALSE(group->HasOneRef());
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_TRUE(delegate.obsoleted_success_);
EXPECT_EQ(group.get(), delegate.obsoleted_group_.get());
EXPECT_TRUE(group->is_obsolete());
EXPECT_TRUE(storage->stored_caches_.empty());
EXPECT_TRUE(storage->stored_groups_.empty());
EXPECT_TRUE(cache->HasOneRef());
EXPECT_FALSE(group->HasOneRef());
delegate.obsoleted_group_ = NULL;
cache = NULL;
EXPECT_TRUE(group->HasOneRef());
}
TEST_F(MockAppCacheStorageTest, MarkEntryAsForeign) {
// Should complete syncly.
MockAppCacheService service;
MockAppCacheStorage* storage =
reinterpret_cast<MockAppCacheStorage*>(service.storage());
// Setup some preconditions. Create a cache with an entry.
GURL entry_url("http://blan/entry");
int64 cache_id = storage->NewCacheId();
scoped_refptr<AppCache> cache = new AppCache(&service, cache_id);
cache->AddEntry(entry_url, AppCacheEntry(AppCacheEntry::EXPLICIT));
// Conduct the test.
MockStorageDelegate delegate;
EXPECT_FALSE(cache->GetEntry(entry_url)->IsForeign());
storage->MarkEntryAsForeign(entry_url, cache_id);
EXPECT_TRUE(cache->GetEntry(entry_url)->IsForeign());
EXPECT_TRUE(cache->GetEntry(entry_url)->IsExplicit());
}
TEST_F(MockAppCacheStorageTest, FindNoMainResponse) {
// Should complete asyncly.
MockAppCacheService service;
MockAppCacheStorage* storage =
reinterpret_cast<MockAppCacheStorage*>(service.storage());
// Conduct the test.
MockStorageDelegate delegate;
GURL url("http://blah/some_url");
EXPECT_NE(url, delegate.found_url_);
storage->FindResponseForMainRequest(url, &delegate);
EXPECT_NE(url, delegate.found_url_);
MessageLoop::current()->RunAllPending(); // Do async task execution.
EXPECT_EQ(url, delegate.found_url_);
EXPECT_TRUE(delegate.found_manifest_url_.is_empty());
EXPECT_EQ(kNoCacheId, delegate.found_cache_id_);
EXPECT_EQ(kNoResponseId, delegate.found_entry_.response_id());
EXPECT_EQ(kNoResponseId, delegate.found_fallback_entry_.response_id());
EXPECT_EQ(0, delegate.found_entry_.types());
EXPECT_EQ(0, delegate.found_fallback_entry_.types());
}
} // namespace appcache
|