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
|
// META: title=CacheStorage
// META: global=window,worker
// META: script=./resources/test-helpers.js
// META: timeout=long
promise_test(function(t) {
var cache_name = 'cache-storage/foo';
return self.caches.delete(cache_name)
.then(function() {
return self.caches.open(cache_name);
})
.then(function(cache) {
assert_true(cache instanceof Cache,
'CacheStorage.open should return a Cache.');
});
}, 'CacheStorage.open');
promise_test(function(t) {
var cache_name = 'cache-storage/bar';
var first_cache = null;
var second_cache = null;
return self.caches.open(cache_name)
.then(function(cache) {
first_cache = cache;
return self.caches.delete(cache_name);
})
.then(function() {
return first_cache.add('./resources/simple.txt');
})
.then(function() {
return self.caches.keys();
})
.then(function(cache_names) {
assert_equals(cache_names.indexOf(cache_name), -1);
return self.caches.open(cache_name);
})
.then(function(cache) {
second_cache = cache;
return second_cache.keys();
})
.then(function(keys) {
assert_equals(keys.length, 0);
return first_cache.keys();
})
.then(function(keys) {
assert_equals(keys.length, 1);
// Clean up
return self.caches.delete(cache_name);
});
}, 'CacheStorage.delete dooms, but does not delete immediately');
promise_test(function(t) {
// Note that this test may collide with other tests running in the same
// origin that also uses an empty cache name.
var cache_name = '';
return self.caches.delete(cache_name)
.then(function() {
return self.caches.open(cache_name);
})
.then(function(cache) {
assert_true(cache instanceof Cache,
'CacheStorage.open should accept an empty name.');
});
}, 'CacheStorage.open with an empty name');
promise_test(function(t) {
return promise_rejects_js(
t,
TypeError,
self.caches.open(),
'CacheStorage.open should throw TypeError if called with no arguments.');
}, 'CacheStorage.open with no arguments');
promise_test(function(t) {
var test_cases = [
{
name: 'cache-storage/lowercase',
should_not_match:
[
'cache-storage/Lowercase',
' cache-storage/lowercase',
'cache-storage/lowercase '
]
},
{
name: 'cache-storage/has a space',
should_not_match:
[
'cache-storage/has'
]
},
{
name: 'cache-storage/has\000_in_the_name',
should_not_match:
[
'cache-storage/has',
'cache-storage/has_in_the_name'
]
}
];
return Promise.all(test_cases.map(function(testcase) {
var cache_name = testcase.name;
return self.caches.delete(cache_name)
.then(function() {
return self.caches.open(cache_name);
})
.then(function() {
return self.caches.has(cache_name);
})
.then(function(result) {
assert_true(result,
'CacheStorage.has should return true for existing ' +
'cache.');
})
.then(function() {
return Promise.all(
testcase.should_not_match.map(function(cache_name) {
return self.caches.has(cache_name)
.then(function(result) {
assert_false(result,
'CacheStorage.has should only perform ' +
'exact matches on cache names.');
});
}));
})
.then(function() {
return self.caches.delete(cache_name);
});
}));
}, 'CacheStorage.has with existing cache');
promise_test(function(t) {
return self.caches.has('cheezburger')
.then(function(result) {
assert_false(result,
'CacheStorage.has should return false for ' +
'nonexistent cache.');
});
}, 'CacheStorage.has with nonexistent cache');
promise_test(function(t) {
var cache_name = 'cache-storage/open';
var cache;
return self.caches.delete(cache_name)
.then(function() {
return self.caches.open(cache_name);
})
.then(function(result) {
cache = result;
})
.then(function() {
return cache.add('./resources/simple.txt');
})
.then(function() {
return self.caches.open(cache_name);
})
.then(function(result) {
assert_true(result instanceof Cache,
'CacheStorage.open should return a Cache object');
assert_not_equals(result, cache,
'CacheStorage.open should return a new Cache ' +
'object each time its called.');
return Promise.all([cache.keys(), result.keys()]);
})
.then(function(results) {
var expected_urls = results[0].map(function(r) { return r.url });
var actual_urls = results[1].map(function(r) { return r.url });
assert_array_equals(actual_urls, expected_urls,
'CacheStorage.open should return a new Cache ' +
'object for the same backing store.');
});
}, 'CacheStorage.open with existing cache');
promise_test(function(t) {
var cache_name = 'cache-storage/delete';
return self.caches.delete(cache_name)
.then(function() {
return self.caches.open(cache_name);
})
.then(function() { return self.caches.delete(cache_name); })
.then(function(result) {
assert_true(result,
'CacheStorage.delete should return true after ' +
'deleting an existing cache.');
})
.then(function() { return self.caches.has(cache_name); })
.then(function(cache_exists) {
assert_false(cache_exists,
'CacheStorage.has should return false after ' +
'fulfillment of CacheStorage.delete promise.');
});
}, 'CacheStorage.delete with existing cache');
promise_test(function(t) {
return self.caches.delete('cheezburger')
.then(function(result) {
assert_false(result,
'CacheStorage.delete should return false for a ' +
'nonexistent cache.');
});
}, 'CacheStorage.delete with nonexistent cache');
promise_test(function(t) {
var unpaired_name = 'unpaired\uD800';
var converted_name = 'unpaired\uFFFD';
// The test assumes that a cache with converted_name does not
// exist, but if the implementation fails the test then such
// a cache will be created. Start off in a fresh state by
// deleting all caches.
return delete_all_caches()
.then(function() {
return self.caches.has(converted_name);
})
.then(function(cache_exists) {
assert_false(cache_exists,
'Test setup failure: cache should not exist');
})
.then(function() { return self.caches.open(unpaired_name); })
.then(function() { return self.caches.keys(); })
.then(function(keys) {
assert_true(keys.indexOf(unpaired_name) !== -1,
'keys should include cache with bad name');
})
.then(function() { return self.caches.has(unpaired_name); })
.then(function(cache_exists) {
assert_true(cache_exists,
'CacheStorage names should be not be converted.');
})
.then(function() { return self.caches.has(converted_name); })
.then(function(cache_exists) {
assert_false(cache_exists,
'CacheStorage names should be not be converted.');
});
}, 'CacheStorage names are DOMStrings not USVStrings');
done();
|