Bug 1145445 - Add a test for CacheStorage; r=bkelly
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 19 Mar 2015 20:19:11 -0400
changeset 263799 37427ecf4c277e44907af1f4f66cfe0cabb888e4
parent 263798 396697e37f14e8662d6a7b1235a732b65ae2a3c5
child 263800 e30f286527154374d630d5e522d9b7f3dc4987bd
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1145445
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1145445 - Add a test for CacheStorage; r=bkelly
dom/cache/test/mochitest/mochitest.ini
dom/cache/test/mochitest/test_cache.js
dom/cache/test/mochitest/test_cache_matchAll_request.js
dom/cache/test/mochitest/test_cache_match_request.js
dom/cache/test/mochitest/test_cache_overwrite.js
dom/cache/test/mochitest/test_caches.html
dom/cache/test/mochitest/test_caches.js
--- a/dom/cache/test/mochitest/mochitest.ini
+++ b/dom/cache/test/mochitest/mochitest.ini
@@ -9,15 +9,17 @@ support-files =
   driver.js
   serviceworker_driver.js
   test_cache_match_request.js
   test_cache_matchAll_request.js
   test_cache_overwrite.js
   mirror.sjs
   test_cache_match_vary.js
   vary.sjs
+  test_caches.js
 
 [test_cache.html]
 [test_cache_add.html]
 [test_cache_match_request.html]
 [test_cache_matchAll_request.html]
 [test_cache_overwrite.html]
 [test_cache_match_vary.html]
+[test_caches.html]
--- a/dom/cache/test/mochitest/test_cache.js
+++ b/dom/cache/test/mochitest/test_cache.js
@@ -119,11 +119,13 @@ caches.open(name).then(function(openCach
   ok(keys.indexOf(name) >= 0, 'snafu should exist');
   ok(keys.indexOf(foobar) >= keys.indexOf(name), 'foobar should come after it');
   return caches.delete(foobar);
 }).then(function(deleteResult) {
   ok(deleteResult, 'delete should succeed');
   return caches.has(foobar);
 }).then(function(hasMissingCache) {
   ok(!hasMissingCache, 'has should have a result');
-}).then(function() {
+  return caches.delete(name);
+}).then(function(deleteResult) {
+  ok(deleteResult, 'delete should succeed');
   testDone();
 })
--- a/dom/cache/test/mochitest/test_cache_matchAll_request.js
+++ b/dom/cache/test/mochitest/test_cache_matchAll_request.js
@@ -165,10 +165,13 @@ function testRequest(request1, request2,
   }).then(function() {
     // Now, drop the cache, reopen and verify that we can't find the request any more.
     c = null;
     return caches.open(name);
   }).then(function(cache) {
     return cache.matchAll();
   }).then(function(r) {
     is(r.length, 0, "Searching in the cache after deletion should not succeed");
+    return caches.delete(name);
+  }).then(function(deleted) {
+    ok(deleted, "The cache should be deleted successfully");
   });
 }
--- a/dom/cache/test/mochitest/test_cache_match_request.js
+++ b/dom/cache/test/mochitest/test_cache_match_request.js
@@ -127,10 +127,13 @@ function testRequest(request, unknownReq
   }).then(function() {
     // Now, drop the cache, reopen and verify that we can't find the request any more.
     c = null;
     return caches.open(name);
   }).then(function(cache) {
     return cache.match(request);
   }).then(function(r) {
     is(typeof r, "undefined", "Searching in the cache after deletion should not succeed");
+    return caches.delete(name);
+  }).then(function(deleted) {
+    ok(deleted, "The cache should be deleted successfully");
   });
 }
--- a/dom/cache/test/mochitest/test_cache_overwrite.js
+++ b/dom/cache/test/mochitest/test_cache_overwrite.js
@@ -35,10 +35,13 @@ fetch(new Request(requestURL, {headers: 
   // with a different value.
   return c.add(new Request(requestURL, {headers: {"Mirror": "bar"}}));
 }).then(function() {
   return c.matchAll();
 }).then(function(r) {
   is(r.length, 1, "Only one request should be in the cache");
   return checkResponse(r[0]);
 }).then(function() {
+  return caches.delete(name);
+}).then(function(deleted) {
+  ok(deleted, "The cache should be deleted successfully");
   testDone();
 });
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/test_caches.html
@@ -0,0 +1,22 @@
+<!-- Any copyright is dedicated to the Public Domain.
+   - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test the CacheStorage API</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+  <script type="text/javascript" src="driver.js"></script>
+</head>
+<body>
+<iframe id="frame"></iframe>
+<script class="testbody" type="text/javascript">
+  // These tests can only be run in sequential mode because we need to be able
+  // to rely on the global state of the CacheStorage at all times.
+  runTests("test_caches.js", "sequential")
+    .then(function() {
+      SimpleTest.finish();
+    });
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/test_caches.js
@@ -0,0 +1,122 @@
+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();
+});