test_caches.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. function arraysHaveSameContent(arr1, arr2) {
  2. if (arr1.length != arr2.length) {
  3. return false;
  4. }
  5. return arr1.every(function(value, index) {
  6. return arr2[index] === value;
  7. });
  8. }
  9. function testHas() {
  10. var name = "caches-has" + context;
  11. return caches.has(name).then(function(has) {
  12. ok(!has, name + " should not exist yet");
  13. return caches.open(name);
  14. }).then(function(c) {
  15. return caches.has(name);
  16. }).then(function(has) {
  17. ok(has, name + " should now exist");
  18. return caches.delete(name);
  19. }).then(function(deleted) {
  20. ok(deleted, "The deletion should finish successfully");
  21. return caches.has(name);
  22. }).then(function(has) {
  23. ok(!has, name + " should not exist any more");
  24. });
  25. }
  26. function testKeys() {
  27. var names = [
  28. // The names here are intentionally unsorted, to ensure the insertion order
  29. // and make sure we don't confuse it with an alphabetically sorted list.
  30. "caches-keys4" + context,
  31. "caches-keys0" + context,
  32. "caches-keys1" + context,
  33. "caches-keys3" + context,
  34. ];
  35. return caches.keys().then(function(keys) {
  36. is(keys.length, 0, "No keys should exist yet");
  37. return Promise.all(names.map(function(name) {
  38. return caches.open(name);
  39. }));
  40. }).then(function() {
  41. return caches.keys();
  42. }).then(function(keys) {
  43. ok(arraysHaveSameContent(keys, names), "Keys must match in insertion order");
  44. return Promise.all(names.map(function(name) {
  45. return caches.delete(name);
  46. }));
  47. }).then(function(deleted) {
  48. ok(arraysHaveSameContent(deleted, [true, true, true, true]), "All deletions must succeed");
  49. return caches.keys();
  50. }).then(function(keys) {
  51. is(keys.length, 0, "No keys should exist any more");
  52. });
  53. }
  54. function testMatchAcrossCaches() {
  55. var tests = [
  56. // The names here are intentionally unsorted, to ensure the insertion order
  57. // and make sure we don't confuse it with an alphabetically sorted list.
  58. {
  59. name: "caches-xmatch5" + context,
  60. request: "//mochi.test:8888/?5" + context,
  61. },
  62. {
  63. name: "caches-xmatch2" + context,
  64. request: "//mochi.test:8888/tests/dom/cache/test/mochitest/test_caches.js?2" + context,
  65. },
  66. {
  67. name: "caches-xmatch4" + context,
  68. request: "//mochi.test:8888/?4" + context,
  69. },
  70. ];
  71. return Promise.all(tests.map(function(test) {
  72. return caches.open(test.name).then(function(c) {
  73. return c.add(test.request);
  74. });
  75. })).then(function() {
  76. return caches.match("//mochi.test:8888/?5" + context, {ignoreSearch: true});
  77. }).then(function(match) {
  78. ok(match.url.indexOf("?5") > 0, "Match should come from the first cache");
  79. return caches.delete("caches-xmatch2" + context); // This should not change anything!
  80. }).then(function(deleted) {
  81. ok(deleted, "Deletion should finish successfully");
  82. return caches.match("//mochi.test:8888/?" + context, {ignoreSearch: true});
  83. }).then(function(match) {
  84. ok(match.url.indexOf("?5") > 0, "Match should still come from the first cache");
  85. return caches.delete("caches-xmatch5" + context); // This should eliminate the first match!
  86. }).then(function(deleted) {
  87. ok(deleted, "Deletion should finish successfully");
  88. return caches.match("//mochi.test:8888/?" + context, {ignoreSearch: true});
  89. }).then(function(match) {
  90. ok(match.url.indexOf("?4") > 0, "Match should come from the third cache");
  91. return caches.delete("caches-xmatch4" + context); // Game over!
  92. }).then(function(deleted) {
  93. ok(deleted, "Deletion should finish successfully");
  94. return caches.match("//mochi.test:8888/?" + context, {ignoreSearch: true});
  95. }).then(function(match) {
  96. is(typeof match, "undefined", "No matches should be found");
  97. });
  98. }
  99. function testDelete() {
  100. return caches.delete("delete" + context).then(function(deleted) {
  101. ok(!deleted, "Attempting to delete a non-existing cache should fail");
  102. return caches.open("delete" + context);
  103. }).then(function() {
  104. return caches.delete("delete" + context);
  105. }).then(function(deleted) {
  106. ok(deleted, "Delete should now succeed");
  107. });
  108. }
  109. testHas().then(function() {
  110. return testKeys();
  111. }).then(function() {
  112. return testMatchAcrossCaches();
  113. }).then(function() {
  114. return testDelete();
  115. }).then(function() {
  116. testDone();
  117. });