summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/cache-storage/script-tests/cache-storage.js
blob: 594b01b5810cd8048550ac0567f924d695465b40 (plain)
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
if (self.importScripts) {
    importScripts('/resources/testharness.js');
    importScripts('../resources/testharness-helpers.js');
    importScripts('../resources/test-helpers.js');
}

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) {
    // 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 assert_promise_rejects(
      self.caches.open(),
      new TypeError(),
      '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 bad_name = 'unpaired\uD800';
    var converted_name = 'unpaired\uFFFD'; // Don't create cache with this name.
    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(bad_name); })
      .then(function() { return self.caches.keys(); })
      .then(function(keys) {
          assert_true(keys.indexOf(bad_name) !== -1,
                      'keys should include cache with bad name');
      })
      .then(function() { return self.caches.has(bad_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();