test_cache_delete.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var name = "delete" + context;
  2. var c;
  3. function setupTest(reqs) {
  4. return new Promise(function(resolve, reject) {
  5. var cache;
  6. caches.open(name).then(function(c) {
  7. cache = c;
  8. return c.addAll(reqs);
  9. }).then(function() {
  10. resolve(cache);
  11. }).catch(function(err) {
  12. reject(err);
  13. });
  14. });
  15. }
  16. function testBasics() {
  17. var tests = [
  18. "//mochi.test:8888/?foo" + context,
  19. "//mochi.test:8888/?bar" + context,
  20. ];
  21. var cache;
  22. return setupTest(tests)
  23. .then(function(c) {
  24. cache = c;
  25. return cache.delete("//mochi.test:8888/?baz");
  26. }).then(function(deleted) {
  27. ok(!deleted, "Deleting a non-existing entry should fail");
  28. return cache.keys();
  29. }).then(function(keys) {
  30. is(keys.length, 2, "No entries from the cache should be deleted");
  31. return cache.delete(tests[0]);
  32. }).then(function(deleted) {
  33. ok(deleted, "Deleting an existing entry should succeed");
  34. return cache.keys();
  35. }).then(function(keys) {
  36. is(keys.length, 1, "Only one entry should exist now");
  37. ok(keys[0].url.indexOf(tests[1]) >= 0, "The correct entry must be deleted");
  38. });
  39. }
  40. function testFragment() {
  41. var tests = [
  42. "//mochi.test:8888/?foo" + context,
  43. "//mochi.test:8888/?bar" + context,
  44. "//mochi.test:8888/?baz" + context + "#fragment",
  45. ];
  46. var cache;
  47. return setupTest(tests)
  48. .then(function(c) {
  49. cache = c;
  50. return cache.delete(tests[0] + "#fragment");
  51. }).then(function(deleted) {
  52. ok(deleted, "Deleting an existing entry should succeed");
  53. return cache.keys();
  54. }).then(function(keys) {
  55. is(keys.length, 2, "Only one entry should exist now");
  56. ok(keys[0].url.indexOf(tests[1]) >= 0, "The correct entry must be deleted");
  57. ok(keys[1].url.indexOf(tests[2].replace("#fragment", "")) >= 0, "The correct entry must be deleted");
  58. // Now, delete a request that was added with a fragment
  59. return cache.delete("//mochi.test:8888/?baz" + context);
  60. }).then(function(deleted) {
  61. ok(deleted, "Deleting an existing entry should succeed");
  62. return cache.keys();
  63. }).then(function(keys) {
  64. is(keys.length, 1, "Only one entry should exist now");
  65. ok(keys[0].url.indexOf(tests[1]) >= 0, "3The correct entry must be deleted");
  66. });
  67. }
  68. function testInterleaved() {
  69. var tests = [
  70. "//mochi.test:8888/?foo" + context,
  71. "//mochi.test:8888/?bar" + context,
  72. ];
  73. var newURL = "//mochi.test:8888/?baz" + context;
  74. var cache;
  75. return setupTest(tests)
  76. .then(function(c) {
  77. cache = c;
  78. // Simultaneously add and delete a request
  79. return Promise.all([
  80. cache.delete(newURL),
  81. cache.add(newURL),
  82. ]);
  83. }).then(function(result) {
  84. ok(!result[1], "deletion should fail");
  85. return cache.keys();
  86. }).then(function(keys) {
  87. is(keys.length, 3, "Tree entries should still exist");
  88. ok(keys[0].url.indexOf(tests[0]) >= 0, "The correct entry must be deleted");
  89. ok(keys[1].url.indexOf(tests[1]) >= 0, "The correct entry must be deleted");
  90. ok(keys[2].url.indexOf(newURL) >= 0, "The new entry should be correctly inserted");
  91. });
  92. }
  93. // Make sure to clean up after each test step.
  94. function step(testPromise) {
  95. return testPromise.then(function() {
  96. caches.delete(name);
  97. });
  98. }
  99. step(testBasics()).then(function() {
  100. return step(testFragment());
  101. }).then(function() {
  102. return step(testInterleaved());
  103. }).then(function() {
  104. testDone();
  105. });