summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/mochitest/test_caches.js
blob: c1d3abaff143a04e13b3957d26b8de7acb668175 (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
/* global context testDone:true */

function arraysHaveSameContent(arr1, arr2) {
  if (arr1.length != arr2.length) {
    return false;
  }
  return arr1.every(function (value, index) {
    return arr2[index] === value;
  });
}

function testHas() {
  var name = "caches-has" + context;
  return caches
    .has(name)
    .then(function (has) {
      ok(!has, name + " should not exist yet");
      return caches.open(name);
    })
    .then(function (c) {
      return caches.has(name);
    })
    .then(function (has) {
      ok(has, name + " should now exist");
      return caches.delete(name);
    })
    .then(function (deleted) {
      ok(deleted, "The deletion should finish successfully");
      return caches.has(name);
    })
    .then(function (has) {
      ok(!has, name + " should not exist any more");
    });
}

function testKeys() {
  var names = [
    // The names here are intentionally unsorted, to ensure the insertion order
    // and make sure we don't confuse it with an alphabetically sorted list.
    "caches-keys4" + context,
    "caches-keys0" + context,
    "caches-keys1" + context,
    "caches-keys3" + context,
  ];
  return caches
    .keys()
    .then(function (keys) {
      is(keys.length, 0, "No keys should exist yet");
      return Promise.all(
        names.map(function (name) {
          return caches.open(name);
        })
      );
    })
    .then(function () {
      return caches.keys();
    })
    .then(function (keys) {
      ok(
        arraysHaveSameContent(keys, names),
        "Keys must match in insertion order"
      );
      return Promise.all(
        names.map(function (name) {
          return caches.delete(name);
        })
      );
    })
    .then(function (deleted) {
      ok(
        arraysHaveSameContent(deleted, [true, true, true, true]),
        "All deletions must succeed"
      );
      return caches.keys();
    })
    .then(function (keys) {
      is(keys.length, 0, "No keys should exist any more");
    });
}

function testMatchAcrossCaches() {
  var tests = [
    // The names here are intentionally unsorted, to ensure the insertion order
    // and make sure we don't confuse it with an alphabetically sorted list.
    {
      name: "caches-xmatch5" + context,
      request: "//mochi.test:8888/?5" + context,
    },
    {
      name: "caches-xmatch2" + context,
      request:
        "//mochi.test:8888/tests/dom/cache/test/mochitest/test_caches.js?2" +
        context,
    },
    {
      name: "caches-xmatch4" + context,
      request: "//mochi.test:8888/?4" + context,
    },
  ];
  return Promise.all(
    tests.map(function (test) {
      return caches.open(test.name).then(function (c) {
        return c.add(test.request);
      });
    })
  )
    .then(function () {
      return caches.match("//mochi.test:8888/?5" + context, {
        ignoreSearch: true,
      });
    })
    .then(function (match) {
      ok(match.url.indexOf("?5") > 0, "Match should come from the first cache");
      return caches.delete("caches-xmatch2" + context); // This should not change anything!
    })
    .then(function (deleted) {
      ok(deleted, "Deletion should finish successfully");
      return caches.match("//mochi.test:8888/?" + context, {
        ignoreSearch: true,
      });
    })
    .then(function (match) {
      ok(
        match.url.indexOf("?5") > 0,
        "Match should still come from the first cache"
      );
      return caches.delete("caches-xmatch5" + context); // This should eliminate the first match!
    })
    .then(function (deleted) {
      ok(deleted, "Deletion should finish successfully");
      return caches.match("//mochi.test:8888/?" + context, {
        ignoreSearch: true,
      });
    })
    .then(function (match) {
      ok(match.url.indexOf("?4") > 0, "Match should come from the third cache");
      return caches.delete("caches-xmatch4" + context); // Game over!
    })
    .then(function (deleted) {
      ok(deleted, "Deletion should finish successfully");
      return caches.match("//mochi.test:8888/?" + context, {
        ignoreSearch: true,
      });
    })
    .then(function (match) {
      is(typeof match, "undefined", "No matches should be found");
    });
}

function testDelete() {
  return caches
    .delete("delete" + context)
    .then(function (deleted) {
      ok(!deleted, "Attempting to delete a non-existing cache should fail");
      return caches.open("delete" + context);
    })
    .then(function () {
      return caches.delete("delete" + context);
    })
    .then(function (deleted) {
      ok(deleted, "Delete should now succeed");
    });
}

testHas()
  .then(function () {
    return testKeys();
  })
  .then(function () {
    return testMatchAcrossCaches();
  })
  .then(function () {
    return testDelete();
  })
  .then(function () {
    testDone();
  });