Bug 1323967 - Improve how Worklet tests are written, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 16 Dec 2016 22:21:51 +0100
changeset 450677 f380b1052f2a5a312dd7d8820e3910e7fffce99f
parent 450676 d12e6223b584951daba323e02572852d1520ef3f
child 450678 a1a83bf44226e2c6602f347636e090525cef075b
push id38944
push userbmo:leftysolara@gmail.com
push dateSun, 18 Dec 2016 16:01:15 +0000
reviewerssmaug
bugs1323967
milestone53.0a1
Bug 1323967 - Improve how Worklet tests are written, r=smaug
dom/worklet/tests/common.js
dom/worklet/tests/file_audioWorklet.html
dom/worklet/tests/file_basic.html
dom/worklet/tests/file_console.html
dom/worklet/tests/file_dump.html
dom/worklet/tests/file_exception.html
dom/worklet/tests/file_import_with_cache.html
dom/worklet/tests/file_paintWorklet.html
dom/worklet/tests/mochitest.ini
dom/worklet/tests/test_audioWorklet.html
dom/worklet/tests/test_basic.html
dom/worklet/tests/test_console.html
dom/worklet/tests/test_dump.html
dom/worklet/tests/test_exception.html
dom/worklet/tests/test_import_with_cache.html
dom/worklet/tests/test_paintWorklet.html
dom/worklet/tests/worklet_audioWorklet.js
--- a/dom/worklet/tests/common.js
+++ b/dom/worklet/tests/common.js
@@ -1,14 +1,23 @@
-function loadTest(file) {
-  var iframe = document.createElement('iframe');
-  iframe.src = file;
+window.onload = function() {
+  // We are the parent. Let's load the test.
+  if (parent == this || location.search.indexOf("worklet_iframe") == -1) {
+    SimpleTest.waitForExplicitFinish();
 
-  document.body.appendChild(iframe);
-}
+    configureTest().then(() => {
+      var iframe = document.createElement('iframe');
+      iframe.src = location.href + "?worklet_iframe";
+      document.body.appendChild(iframe);
+    });
 
-function setupTest() {
+    return;
+  }
+
+  // Here we are in the iframe.
   window.SimpleTest = parent.SimpleTest;
   window.is = parent.is;
   window.isnot = parent.isnot;
   window.ok = parent.ok;
   window.info = parent.info;
+
+  runTestInIframe();
 }
deleted file mode 100644
--- a/dom/worklet/tests/file_audioWorklet.html
+++ /dev/null
@@ -1,17 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Test for AudioWorklet</title>
-  <script type="application/javascript" src="common.js"></script>
-</head>
-<body>
-
-<script type="application/javascript">
-
-setupTest();
-
-audioWorklet.import("worklet_audioWorklet.js")
-
-</script>
-</body>
-</html>
deleted file mode 100644
--- a/dom/worklet/tests/file_basic.html
+++ /dev/null
@@ -1,56 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Test for Worklet</title>
-  <script type="application/javascript" src="common.js"></script>
-</head>
-<body>
-
-<script type="application/javascript">
-
-setupTest();
-
-ok(!!audioWorklet, "audioWorklet exists");
-
-// First loading
-audioWorklet.import("common.js")
-.then(() => {
-  ok(true, "Import should load a resource.");
-})
-
-// Second loading - same file
-.then(() => {
-  return audioWorklet.import("common.js")
-})
-.then(() => {
-  ok(true, "Import should load a resource.");
-})
-
-// 3rd loading - a network error
-.then(() => {
-  return audioWorklet.import("404.js");
-})
-.then(() => {
-  ok(false, "The loading should fail.");
-}, () => {
-  ok(true, "The loading should fail.");
-})
-
-// 4th loading - a network error
-.then(() => {
-  return audioWorklet.import("404.js");
-})
-.then(() => {
-  ok(false, "The loading should fail.");
-}, () => {
-  ok(true, "The loading should fail.");
-})
-
-// done
-.then(() => {
-  SimpleTest.finish();
-});
-
-</script>
-</body>
-</html>
deleted file mode 100644
--- a/dom/worklet/tests/file_console.html
+++ /dev/null
@@ -1,17 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Test for Worklet</title>
-  <script type="application/javascript" src="common.js"></script>
-</head>
-<body>
-
-<script type="application/javascript">
-
-setupTest();
-
-audioWorklet.import("worklet_console.js");
-
-</script>
-</body>
-</html>
deleted file mode 100644
--- a/dom/worklet/tests/file_dump.html
+++ /dev/null
@@ -1,21 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Test for Worklet</title>
-  <script type="application/javascript" src="common.js"></script>
-</head>
-<body>
-
-<script type="application/javascript">
-
-setupTest();
-
-audioWorklet.import("worklet_dump.js")
-.then(() => {
-  ok(true, "All good!");
-  SimpleTest.finish();
-});
-
-</script>
-</body>
-</html>
deleted file mode 100644
--- a/dom/worklet/tests/file_exception.html
+++ /dev/null
@@ -1,37 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Test for Exceptions in Worklet</title>
-  <script type="application/javascript" src="common.js"></script>
-</head>
-<body>
-
-<script type="application/javascript">
-
-setupTest();
-
-// This loading should fail
-audioWorklet.import("404.js")
-.then(() => {
-  ok(false, "We should not be called!");
-}, () => {
-  ok(true, "The script thrown but we are still here.");
-})
-
-// This should throw from JS
-.then(() => {
-  return audioWorklet.import("worklet_exception.js")
-})
-.then(() => {
-  ok(false, "We should not be called!");
-}, () => {
-  ok(true, "The script thrown but we are still here.");
-})
-
-.then(() => {
-  SimpleTest.finish();
-});
-
-</script>
-</body>
-</html>
deleted file mode 100644
--- a/dom/worklet/tests/file_import_with_cache.html
+++ /dev/null
@@ -1,40 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Test for Worklet</title>
-  <script type="application/javascript" src="common.js"></script>
-</head>
-<body>
-
-<script type="application/javascript">
-
-setupTest();
-
-function loading() {
-  audioWorklet.import("server_import_with_cache.sjs")
-  .then(() => {
-    ok(true, "Import should load a resource.");
-  }, () => {
-    ok(false, "Import should load a resource.");
-  })
-  .then(() => {
-    done();
-  });
-}
-
-var count = 0;
-const MAX = 10;
-
-function done() {
-  if (++count == MAX) {
-    SimpleTest.finish();
-  }
-}
-
-for (var i = 0; i < MAX; ++i) {
-  loading();
-}
-
-</script>
-</body>
-</html>
deleted file mode 100644
--- a/dom/worklet/tests/file_paintWorklet.html
+++ /dev/null
@@ -1,17 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Test for PaintWorklet</title>
-  <script type="application/javascript" src="common.js"></script>
-</head>
-<body>
-
-<script type="application/javascript">
-
-setupTest();
-
-paintWorklet.import("worklet_paintWorklet.js")
-
-</script>
-</body>
-</html>
--- a/dom/worklet/tests/mochitest.ini
+++ b/dom/worklet/tests/mochitest.ini
@@ -1,19 +1,18 @@
 [DEFAULT]
 skip-if = release_or_beta
 support-files =
   common.js
 
 [test_basic.html]
-support-files=file_basic.html
 [test_console.html]
-support-files=file_console.html worklet_console.js
+support-files=worklet_console.js
 [test_import_with_cache.html]
-support-files=file_import_with_cache.html server_import_with_cache.sjs
+support-files=server_import_with_cache.sjs
 [test_dump.html]
-support-files=file_dump.html worklet_dump.js
+support-files=worklet_dump.js
 [test_audioWorklet.html]
-support-files=file_audioWorklet.html worklet_audioWorklet.js
+support-files=worklet_audioWorklet.js
 [test_exception.html]
-support-files=file_exception.html worklet_exception.js
+support-files=worklet_exception.js
 [test_paintWorklet.html]
-support-files=file_paintWorklet.html worklet_paintWorklet.js
+support-files=worklet_paintWorklet.js
--- a/dom/worklet/tests/test_audioWorklet.html
+++ b/dom/worklet/tests/test_audioWorklet.html
@@ -5,38 +5,42 @@
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="common.js"></script>
 </head>
 <body>
 
 <script type="application/javascript">
 
-function consoleListener() {
-  SpecialPowers.addObserver(this, "console-api-log-event", false);
-}
+function configureTest() {
+  function consoleListener() {
+    SpecialPowers.addObserver(this, "console-api-log-event", false);
+  }
 
-consoleListener.prototype  = {
-  observe: function(aSubject, aTopic, aData) {
-    if (aTopic == "console-api-log-event") {
-      var obj = aSubject.wrappedJSObject;
-      if (obj.arguments[0] == "So far so good") {
-        ok(true, "Message received \\o/");
+  consoleListener.prototype  = {
+    observe: function(aSubject, aTopic, aData) {
+      if (aTopic == "console-api-log-event") {
+        var obj = aSubject.wrappedJSObject;
+        if (obj.arguments[0] == "So far so good") {
+          ok(true, "Message received \\o/");
 
-        SpecialPowers.removeObserver(this, "console-api-log-event");
-        SimpleTest.finish();
-        return;
+          SpecialPowers.removeObserver(this, "console-api-log-event");
+          SimpleTest.finish();
+          return;
+        }
       }
     }
   }
+
+  var cl = new consoleListener();
+
+  return SpecialPowers.pushPrefEnv(
+    {"set": [["dom.audioWorklet.enabled", true],
+             ["dom.worklet.enabled", true]]});
 }
 
-var cl = new consoleListener();
-
-SimpleTest.waitForExplicitFinish();
-SpecialPowers.pushPrefEnv(
-  {"set": [["dom.audioWorklet.enabled", true],
-           ["dom.worklet.enabled", true]]},
-  function() { loadTest("file_audioWorklet.html"); });
-
+// This function is called into an iframe.
+function runTestInIframe() {
+  audioWorklet.import("worklet_audioWorklet.js")
+}
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_basic.html
+++ b/dom/worklet/tests/test_basic.html
@@ -5,17 +5,61 @@
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="common.js"></script>
 </head>
 <body>
 
 <script type="application/javascript">
 
-SimpleTest.waitForExplicitFinish();
-SpecialPowers.pushPrefEnv(
-  {"set": [["dom.audioWorklet.enabled", true],
-           ["dom.worklet.enabled", true]]},
-  function() { loadTest("file_basic.html"); });
+function configureTest() {
+  return SpecialPowers.pushPrefEnv(
+    {"set": [["dom.audioWorklet.enabled", true],
+             ["dom.worklet.enabled", true]]});
+}
+
+// This function is called into an iframe.
+function runTestInIframe() {
+  ok(!!audioWorklet, "audioWorklet exists");
+
+  // First loading
+  audioWorklet.import("common.js")
+  .then(() => {
+    ok(true, "Import should load a resource.");
+  })
+
+  // Second loading - same file
+  .then(() => {
+    return audioWorklet.import("common.js")
+  })
+  .then(() => {
+    ok(true, "Import should load a resource.");
+  })
+
+  // 3rd loading - a network error
+  .then(() => {
+    return audioWorklet.import("404.js");
+  })
+  .then(() => {
+    ok(false, "The loading should fail.");
+  }, () => {
+    ok(true, "The loading should fail.");
+  })
+
+  // 4th loading - a network error
+  .then(() => {
+    return audioWorklet.import("404.js");
+  })
+  .then(() => {
+    ok(false, "The loading should fail.");
+  }, () => {
+    ok(true, "The loading should fail.");
+  })
+
+  // done
+  .then(() => {
+    SimpleTest.finish();
+  });
+}
 
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_console.html
+++ b/dom/worklet/tests/test_console.html
@@ -5,38 +5,43 @@
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="common.js"></script>
 </head>
 <body>
 
 <script type="application/javascript">
 
-function consoleListener() {
-  SpecialPowers.addObserver(this, "console-api-log-event", false);
-}
+function configureTest() {
+  function consoleListener() {
+    SpecialPowers.addObserver(this, "console-api-log-event", false);
+  }
 
-consoleListener.prototype  = {
-  observe: function(aSubject, aTopic, aData) {
-    if (aTopic == "console-api-log-event") {
-      var obj = aSubject.wrappedJSObject;
-      if (obj.arguments[0] == "Hello world from a worklet") {
-        ok(true, "Message received \\o/");
+  consoleListener.prototype  = {
+    observe: function(aSubject, aTopic, aData) {
+      if (aTopic == "console-api-log-event") {
+        var obj = aSubject.wrappedJSObject;
+        if (obj.arguments[0] == "Hello world from a worklet") {
+          ok(true, "Message received \\o/");
 
-        SpecialPowers.removeObserver(this, "console-api-log-event");
-        SimpleTest.finish();
-        return;
+          SpecialPowers.removeObserver(this, "console-api-log-event");
+          SimpleTest.finish();
+          return;
+        }
       }
     }
   }
+
+  var cl = new consoleListener();
+
+  return SpecialPowers.pushPrefEnv(
+    {"set": [["dom.audioWorklet.enabled", true],
+             ["dom.worklet.enabled", true]]});
 }
 
-var cl = new consoleListener();
-
-SimpleTest.waitForExplicitFinish();
-SpecialPowers.pushPrefEnv(
-  {"set": [["dom.audioWorklet.enabled", true],
-           ["dom.worklet.enabled", true]]},
-  function() { loadTest("file_console.html"); });
+// This function is called into an iframe.
+function runTestInIframe() {
+  audioWorklet.import("worklet_console.js");
+}
 
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_dump.html
+++ b/dom/worklet/tests/test_dump.html
@@ -5,17 +5,25 @@
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="common.js"></script>
 </head>
 <body>
 
 <script type="application/javascript">
 
-SimpleTest.waitForExplicitFinish();
-SpecialPowers.pushPrefEnv(
-  {"set": [["dom.audioWorklet.enabled", true],
-           ["dom.worklet.enabled", true]]},
-  function() { loadTest("file_dump.html"); });
+function configureTest() {
+  return SpecialPowers.pushPrefEnv(
+    {"set": [["dom.audioWorklet.enabled", true],
+             ["dom.worklet.enabled", true]]});
+}
 
+// This function is called into an iframe.
+function runTestInIframe() {
+  audioWorklet.import("worklet_dump.js")
+  .then(() => {
+    ok(true, "All good!");
+    SimpleTest.finish();
+  });
+}
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_exception.html
+++ b/dom/worklet/tests/test_exception.html
@@ -5,17 +5,42 @@
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="common.js"></script>
 </head>
 <body>
 
 <script type="application/javascript">
 
-SimpleTest.waitForExplicitFinish();
-SpecialPowers.pushPrefEnv(
-  {"set": [["dom.audioWorklet.enabled", true],
-           ["dom.worklet.enabled", true]]},
-  function() { loadTest("file_exception.html"); });
+function configureTest() {
+  return SpecialPowers.pushPrefEnv(
+    {"set": [["dom.audioWorklet.enabled", true],
+             ["dom.worklet.enabled", true]]});
+}
+
+// This function is called into an iframe.
+function runTestInIframe() {
+  // This loading should fail
+  audioWorklet.import("404.js")
+  .then(() => {
+    ok(false, "We should not be called!");
+  }, () => {
+    ok(true, "The script thrown but we are still here.");
+  })
+
+  // This should throw from JS
+  .then(() => {
+    return audioWorklet.import("worklet_exception.js")
+  })
+  .then(() => {
+    ok(false, "We should not be called!");
+  }, () => {
+    ok(true, "The script thrown but we are still here.");
+  })
+
+  .then(() => {
+    SimpleTest.finish();
+  });
+}
 
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_import_with_cache.html
+++ b/dom/worklet/tests/test_import_with_cache.html
@@ -5,17 +5,44 @@
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="common.js"></script>
 </head>
 <body>
 
 <script type="application/javascript">
 
-SimpleTest.waitForExplicitFinish();
-SpecialPowers.pushPrefEnv(
-  {"set": [["dom.audioWorklet.enabled", true],
-           ["dom.worklet.enabled", true]]},
-  function() { loadTest("file_import_with_cache.html"); });
+function configureTest() {
+  return SpecialPowers.pushPrefEnv(
+    {"set": [["dom.audioWorklet.enabled", true],
+             ["dom.worklet.enabled", true]]});
+}
 
+// This function is called into an iframe.
+function runTestInIframe() {
+  function loading() {
+    audioWorklet.import("server_import_with_cache.sjs")
+    .then(() => {
+      ok(true, "Import should load a resource.");
+    }, () => {
+      ok(false, "Import should load a resource.");
+    })
+    .then(() => {
+      done();
+    });
+  }
+
+  var count = 0;
+  const MAX = 10;
+
+  function done() {
+    if (++count == MAX) {
+      SimpleTest.finish();
+    }
+  }
+
+  for (var i = 0; i < MAX; ++i) {
+    loading();
+  }
+}
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_paintWorklet.html
+++ b/dom/worklet/tests/test_paintWorklet.html
@@ -5,38 +5,42 @@
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="common.js"></script>
 </head>
 <body>
 
 <script type="application/javascript">
 
-function consoleListener() {
-  SpecialPowers.addObserver(this, "console-api-log-event", false);
-}
+function configureTest() {
+  function consoleListener() {
+    SpecialPowers.addObserver(this, "console-api-log-event", false);
+  }
 
-consoleListener.prototype  = {
-  observe: function(aSubject, aTopic, aData) {
-    if (aTopic == "console-api-log-event") {
-      var obj = aSubject.wrappedJSObject;
-      if (obj.arguments[0] == "So far so good") {
-        ok(true, "Message received \\o/");
+  consoleListener.prototype  = {
+    observe: function(aSubject, aTopic, aData) {
+      if (aTopic == "console-api-log-event") {
+        var obj = aSubject.wrappedJSObject;
+        if (obj.arguments[0] == "So far so good") {
+          ok(true, "Message received \\o/");
 
-        SpecialPowers.removeObserver(this, "console-api-log-event");
-        SimpleTest.finish();
-        return;
+          SpecialPowers.removeObserver(this, "console-api-log-event");
+          SimpleTest.finish();
+          return;
+        }
       }
     }
   }
+
+  var cl = new consoleListener();
+
+  return SpecialPowers.pushPrefEnv(
+    {"set": [["dom.paintWorklet.enabled", true],
+             ["dom.worklet.enabled", true]]});
 }
 
-var cl = new consoleListener();
-
-SimpleTest.waitForExplicitFinish();
-SpecialPowers.pushPrefEnv(
-  {"set": [["dom.paintWorklet.enabled", true],
-           ["dom.worklet.enabled", true]]},
-  function() { loadTest("file_paintWorklet.html"); });
-
+// This function is called into an iframe.
+function runTestInIframe() {
+  paintWorklet.import("worklet_paintWorklet.js")
+}
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/worklet_audioWorklet.js
+++ b/dom/worklet/tests/worklet_audioWorklet.js
@@ -1,3 +1,3 @@
 // This should work for real... at some point.
 registerProcessor("sure!", () => {});
-console.log("So far so good");
+console.log(this instanceof AudioWorkletGlobalScope ? "So far so good" : "error");