driver.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Any copyright is dedicated to the Public Domain.
  2. // http://creativecommons.org/publicdomain/zero/1.0/
  3. //
  4. // This helper script exposes a runTests function that takes the name of a
  5. // test script as its input argument and runs the test in three different
  6. // contexts:
  7. // 1. Regular Worker context
  8. // 2. Service Worker context
  9. // 3. Window context
  10. // The function returns a promise which will get resolved once all tests
  11. // finish. The testFile argument is the name of the test file to be run
  12. // in the different contexts, and the optional order argument can be set
  13. // to either "parallel" or "sequential" depending on how the caller wants
  14. // the tests to be run. If this argument is not provided, the default is
  15. // "both", which runs the tests in both modes.
  16. // The caller of this function is responsible to call SimpleTest.finish
  17. // when the returned promise is resolved.
  18. function runTests(testFile, order) {
  19. function setupPrefs() {
  20. return new Promise(function(resolve, reject) {
  21. SpecialPowers.pushPrefEnv({
  22. "set": [["dom.caches.enabled", true],
  23. ["dom.caches.testing.enabled", true],
  24. ["dom.serviceWorkers.enabled", true],
  25. ["dom.serviceWorkers.testing.enabled", true],
  26. ["dom.serviceWorkers.exemptFromPerDomainMax", true]]
  27. }, function() {
  28. resolve();
  29. });
  30. });
  31. }
  32. // adapted from dom/indexedDB/test/helpers.js
  33. function clearStorage() {
  34. return new Promise(function(resolve, reject) {
  35. var qms = SpecialPowers.Services.qms;
  36. var principal = SpecialPowers.wrap(document).nodePrincipal;
  37. var request = qms.clearStoragesForPrincipal(principal);
  38. var cb = SpecialPowers.wrapCallback(resolve);
  39. request.callback = cb;
  40. });
  41. }
  42. function loadScript(script) {
  43. return new Promise(function(resolve, reject) {
  44. var s = document.createElement("script");
  45. s.src = script;
  46. s.onerror = reject;
  47. s.onload = resolve;
  48. document.body.appendChild(s);
  49. });
  50. }
  51. function importDrivers() {
  52. return Promise.all([loadScript("worker_driver.js"),
  53. loadScript("serviceworker_driver.js")]);
  54. }
  55. function runWorkerTest() {
  56. return workerTestExec(testFile);
  57. }
  58. function runServiceWorkerTest() {
  59. return serviceWorkerTestExec(testFile);
  60. }
  61. function runFrameTest() {
  62. return new Promise(function(resolve, reject) {
  63. var iframe = document.createElement("iframe");
  64. iframe.src = "frame.html";
  65. iframe.onload = function() {
  66. var doc = iframe.contentDocument;
  67. var s = doc.createElement("script");
  68. s.src = testFile;
  69. window.addEventListener("message", function onMessage(event) {
  70. if (event.data.context != "Window") {
  71. return;
  72. }
  73. if (event.data.type == 'finish') {
  74. window.removeEventListener("message", onMessage);
  75. resolve();
  76. } else if (event.data.type == 'status') {
  77. ok(event.data.status, event.data.context + ": " + event.data.msg);
  78. }
  79. }, false);
  80. doc.body.appendChild(s);
  81. };
  82. document.body.appendChild(iframe);
  83. });
  84. }
  85. SimpleTest.waitForExplicitFinish();
  86. if (typeof order == "undefined") {
  87. order = "sequential"; // sequential by default, see bug 1143222.
  88. // TODO: Make this "both" again.
  89. }
  90. ok(order == "parallel" || order == "sequential" || order == "both",
  91. "order argument should be valid");
  92. if (order == "both") {
  93. info("Running tests in both modes; first: sequential");
  94. return runTests(testFile, "sequential")
  95. .then(function() {
  96. info("Running tests in parallel mode");
  97. return runTests(testFile, "parallel");
  98. });
  99. }
  100. if (order == "sequential") {
  101. return setupPrefs()
  102. .then(importDrivers)
  103. .then(runWorkerTest)
  104. .then(clearStorage)
  105. .then(runServiceWorkerTest)
  106. .then(clearStorage)
  107. .then(runFrameTest)
  108. .then(clearStorage)
  109. .catch(function(e) {
  110. ok(false, "A promise was rejected during test execution: " + e);
  111. });
  112. }
  113. return setupPrefs()
  114. .then(importDrivers)
  115. .then(() => Promise.all([runWorkerTest(), runServiceWorkerTest(), runFrameTest()]))
  116. .then(clearStorage)
  117. .catch(function(e) {
  118. ok(false, "A promise was rejected during test execution: " + e);
  119. });
  120. }