test-chrome.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. let chrome = require('chrome');
  6. const FIXTURES_URL = module.uri.substr(0, module.uri.lastIndexOf('/') + 1) +
  7. 'fixtures/chrome-worker/'
  8. exports['test addEventListener'] = function(assert, done) {
  9. let uri = FIXTURES_URL + 'addEventListener.js';
  10. let worker = new chrome.ChromeWorker(uri);
  11. worker.addEventListener('message', function(event) {
  12. assert.equal(event.data, 'Hello', 'message received');
  13. worker.terminate();
  14. done();
  15. });
  16. };
  17. exports['test onmessage'] = function(assert, done) {
  18. let uri = FIXTURES_URL + 'onmessage.js';
  19. let worker = new chrome.ChromeWorker(uri);
  20. worker.onmessage = function(event) {
  21. assert.equal(event.data, 'ok', 'message received');
  22. worker.terminate();
  23. done();
  24. };
  25. worker.postMessage('ok');
  26. };
  27. exports['test setTimeout'] = function(assert, done) {
  28. let uri = FIXTURES_URL + 'setTimeout.js';
  29. let worker = new chrome.ChromeWorker(uri);
  30. worker.onmessage = function(event) {
  31. assert.equal(event.data, 'ok', 'setTimeout fired');
  32. worker.terminate();
  33. done();
  34. };
  35. };
  36. exports['test jsctypes'] = function(assert, done) {
  37. let uri = FIXTURES_URL + 'jsctypes.js';
  38. let worker = new chrome.ChromeWorker(uri);
  39. worker.onmessage = function(event) {
  40. assert.equal(event.data, 'function', 'ctypes.open is a function');
  41. worker.terminate();
  42. done();
  43. };
  44. };
  45. exports['test XMLHttpRequest'] = function(assert, done) {
  46. let uri = FIXTURES_URL + 'xhr.js';
  47. let worker = new chrome.ChromeWorker(uri);
  48. worker.onmessage = function(event) {
  49. assert.equal(event.data, 'ok', 'XMLHttpRequest works');
  50. worker.terminate();
  51. done();
  52. };
  53. };
  54. exports['test onerror'] = function(assert, done) {
  55. let uri = FIXTURES_URL + 'onerror.js';
  56. let worker = new chrome.ChromeWorker(uri);
  57. worker.onerror = function(event) {
  58. assert.equal(event.filename, uri, 'event reports the correct uri');
  59. assert.equal(event.lineno, 6, 'event reports the correct line number');
  60. assert.equal(event.target, worker, 'event reports the correct worker');
  61. assert.ok(event.message.match(/ok/),
  62. 'event contains the exception message');
  63. // Call preventDefault in order to avoid being displayed in JS console.
  64. event.preventDefault();
  65. worker.terminate();
  66. done();
  67. };
  68. };
  69. require('sdk/test').run(exports);