toolkit/components/workerloader/tests/worker_test_loading.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 importScripts("utils_worker.js"); // Test suite code
michael@0 7 info("Test suite configured");
michael@0 8
michael@0 9 importScripts("resource://gre/modules/workers/require.js");
michael@0 10 info("Loader imported");
michael@0 11
michael@0 12 let PATH = "chrome://mochitests/content/chrome/toolkit/components/workerloader/tests/";
michael@0 13 let tests = [];
michael@0 14 let add_test = function(test) {
michael@0 15 tests.push(test);
michael@0 16 };
michael@0 17
michael@0 18 add_test(function test_setup() {
michael@0 19 ok(typeof require != "undefined", "Function |require| is defined");
michael@0 20 });
michael@0 21
michael@0 22 // Test simple loading (moduleA-depends.js requires moduleB-dependency.js)
michael@0 23 add_test(function test_load() {
michael@0 24 let A = require(PATH + "moduleA-depends.js");
michael@0 25 ok(true, "Opened module A");
michael@0 26
michael@0 27 is(A.A, true, "Module A exported value A");
michael@0 28 ok(!("B" in A), "Module A did not export value B");
michael@0 29 is(A.importedFoo, "foo", "Module A re-exported B.foo");
michael@0 30
michael@0 31 // re-evaluating moduleB-dependency.js would cause an error, but re-requiring it shouldn't
michael@0 32 let B = require(PATH + "moduleB-dependency.js");
michael@0 33 ok(true, "Managed to re-require module B");
michael@0 34 is(B.B, true, "Module B exported value B");
michael@0 35 is(B.foo, "foo", "Module B exported value foo");
michael@0 36 });
michael@0 37
michael@0 38 // Test simple circular loading (moduleC-circular.js and moduleD-circular.js require each other)
michael@0 39 add_test(function test_circular() {
michael@0 40 let C = require(PATH + "moduleC-circular.js");
michael@0 41 ok(true, "Loaded circular modules C and D");
michael@0 42 is(C.copiedFromD.copiedFromC.enteredC, true, "Properties exported by C before requiring D can be seen by D immediately");
michael@0 43
michael@0 44 let D = require(PATH + "moduleD-circular.js");
michael@0 45 is(D.exportedFromC.finishedC, true, "Properties exported by C after requiring D can be seen by D eventually");
michael@0 46 });
michael@0 47
michael@0 48 // Testing error cases
michael@0 49 add_test(function test_exceptions() {
michael@0 50 let should_throw = function(f) {
michael@0 51 try {
michael@0 52 f();
michael@0 53 return null;
michael@0 54 } catch (ex) {
michael@0 55 return ex;
michael@0 56 }
michael@0 57 };
michael@0 58
michael@0 59 let exn = should_throw(() => require(PATH + "this module doesn't exist"));
michael@0 60 ok(!!exn, "Attempting to load a module that doesn't exist raises an error");
michael@0 61
michael@0 62 exn = should_throw(() => require(PATH + "moduleE-throws-during-require.js"));
michael@0 63 ok(!!exn, "Attempting to load a module that throws at toplevel raises an error");
michael@0 64 is(exn.moduleName, PATH + "moduleE-throws-during-require.js",
michael@0 65 "moduleName is correct");
michael@0 66 isnot(exn.moduleStack.indexOf("moduleE-throws-during-require.js"), -1,
michael@0 67 "moduleStack contains the name of the module");
michael@0 68 is(exn.lineNumber, 10, "The error comes with the right line number");
michael@0 69
michael@0 70 exn = should_throw(() => require(PATH + "moduleF-syntaxerror.xml"));
michael@0 71 ok(!!exn, "Attempting to load a non-well formatted module raises an error");
michael@0 72
michael@0 73 exn = should_throw(() => require(PATH + "moduleG-throws-later.js").doThrow());
michael@0 74 ok(!!exn, "G.doThrow() has raised an error");
michael@0 75 info(exn);
michael@0 76 ok(exn.toString().startsWith("TypeError"), "The exception is a TypeError.");
michael@0 77 is(exn.moduleName, PATH + "moduleG-throws-later.js", "The name of the module is correct");
michael@0 78 isnot(exn.moduleStack.indexOf("moduleG-throws-later.js"), -1,
michael@0 79 "The name of the right file appears somewhere in the stack");
michael@0 80 is(exn.lineNumber, 11, "The error comes with the right line number");
michael@0 81 });
michael@0 82
michael@0 83 function get_exn(f) {
michael@0 84 try {
michael@0 85 f();
michael@0 86 return undefined;
michael@0 87 } catch (ex) {
michael@0 88 return ex;
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 // Test module.exports
michael@0 93 add_test(function test_module_dot_exports() {
michael@0 94 let H = require(PATH + "moduleH-module-dot-exports.js");
michael@0 95 is(H.key, "value", "module.exports worked");
michael@0 96 let H2 = require(PATH + "moduleH-module-dot-exports.js");
michael@0 97 is(H2.key, "value", "module.exports returned the same key");
michael@0 98 ok(H2 === H, "module.exports returned the same module the second time");
michael@0 99 let exn = get_exn(() => H.key = "this should not be accepted");
michael@0 100 ok(exn instanceof TypeError, "Cannot alter value in module.exports after export");
michael@0 101 exn = get_exn(() => H.key2 = "this should not be accepted, either");
michael@0 102 ok(exn instanceof TypeError, "Cannot add value to module.exports after export");
michael@0 103 });
michael@0 104
michael@0 105 self.onmessage = function(message) {
michael@0 106 for (let test of tests) {
michael@0 107 info("Entering " + test.name);
michael@0 108 try {
michael@0 109 test();
michael@0 110 } catch (ex) {
michael@0 111 ok(false, "Test " + test.name + " failed");
michael@0 112 info(ex);
michael@0 113 info(ex.stack);
michael@0 114 }
michael@0 115 info("Leaving " + test.name);
michael@0 116 }
michael@0 117 finish();
michael@0 118 };
michael@0 119
michael@0 120
michael@0 121

mercurial