worker_driver.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Any copyright is dedicated to the Public Domain.
  2. // http://creativecommons.org/publicdomain/zero/1.0/
  3. //
  4. // Utility script for writing worker tests. In your main document do:
  5. //
  6. // <script type="text/javascript" src="worker_driver.js"></script>
  7. // <script type="text/javascript">
  8. // workerTestExec('myWorkerTestCase.js')
  9. // </script>
  10. //
  11. // This will then spawn a worker, define some utility functions, and then
  12. // execute the code in myWorkerTestCase.js. You can then use these
  13. // functions in your worker-side test:
  14. //
  15. // ok() - like the SimpleTest assert
  16. // is() - like the SimpleTest assert
  17. // workerTestDone() - like SimpleTest.finish() indicating the test is complete
  18. //
  19. // There are also some functions for requesting information that requires
  20. // SpecialPowers or other main-thread-only resources:
  21. //
  22. // workerTestGetPrefs() - request an array of prefs value from the main thread
  23. // workerTestGetPermissions() - request an array permissions from the MT
  24. // workerTestGetVersion() - request the current version string from the MT
  25. // workerTestGetUserAgent() - request the user agent string from the MT
  26. //
  27. // For an example see test_worker_interfaces.html and test_worker_interfaces.js.
  28. function workerTestExec(script) {
  29. return new Promise(function(resolve, reject) {
  30. var worker = new Worker('worker_wrapper.js');
  31. worker.onmessage = function(event) {
  32. is(event.data.context, "Worker",
  33. "Correct context for messages received on the worker");
  34. if (event.data.type == 'finish') {
  35. worker.terminate();
  36. SpecialPowers.forceGC();
  37. resolve();
  38. } else if (event.data.type == 'status') {
  39. ok(event.data.status, event.data.context + ": " + event.data.msg);
  40. } else if (event.data.type == 'getPrefs') {
  41. var result = {};
  42. event.data.prefs.forEach(function(pref) {
  43. result[pref] = SpecialPowers.Services.prefs.getBoolPref(pref);
  44. });
  45. worker.postMessage({
  46. type: 'returnPrefs',
  47. prefs: event.data.prefs,
  48. result: result
  49. });
  50. } else if (event.data.type == 'getPermissions') {
  51. var result = {};
  52. event.data.permissions.forEach(function(permission) {
  53. result[permission] = SpecialPowers.hasPermission(permission, window.document);
  54. });
  55. worker.postMessage({
  56. type: 'returnPermissions',
  57. permissions: event.data.permissions,
  58. result: result
  59. });
  60. } else if (event.data.type == 'getVersion') {
  61. var result = SpecialPowers.Cc['@mozilla.org/xre/app-info;1'].getService(SpecialPowers.Ci.nsIXULAppInfo).version;
  62. worker.postMessage({
  63. type: 'returnVersion',
  64. result: result
  65. });
  66. } else if (event.data.type == 'getUserAgent') {
  67. worker.postMessage({
  68. type: 'returnUserAgent',
  69. result: navigator.userAgent
  70. });
  71. }
  72. }
  73. worker.onerror = function(event) {
  74. reject('Worker had an error: ' + event.data);
  75. };
  76. worker.postMessage({ script: script });
  77. });
  78. }