browser/devtools/shared/test/browser_require_basic.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 // Tests that source URLs in the Web Console can be clicked to display the
michael@0 5 // standard View Source window.
michael@0 6
michael@0 7 let [ define, require ] = (function() {
michael@0 8 let tempScope = {};
michael@0 9 Components.utils.import("resource://gre/modules/devtools/Require.jsm", tempScope);
michael@0 10 return [ tempScope.define, tempScope.require ];
michael@0 11 })();
michael@0 12
michael@0 13 function test() {
michael@0 14 addTab("about:blank", function() {
michael@0 15 info("Starting Require Tests");
michael@0 16 setup();
michael@0 17
michael@0 18 testWorking();
michael@0 19 testDomains();
michael@0 20 testLeakage();
michael@0 21 testMultiImport();
michael@0 22 testRecursive();
michael@0 23 testUncompilable();
michael@0 24 testFirebug();
michael@0 25
michael@0 26 shutdown();
michael@0 27 });
michael@0 28 }
michael@0 29
michael@0 30 function setup() {
michael@0 31 define('gclitest/requirable', [ 'require', 'exports', 'module' ], function(require, exports, module) {
michael@0 32 exports.thing1 = 'thing1';
michael@0 33 exports.thing2 = 2;
michael@0 34
michael@0 35 let status = 'initial';
michael@0 36 exports.setStatus = function(aStatus) { status = aStatus; };
michael@0 37 exports.getStatus = function() { return status; };
michael@0 38 });
michael@0 39
michael@0 40 define('gclitest/unrequirable', [ 'require', 'exports', 'module' ], function(require, exports, module) {
michael@0 41 null.throwNPE();
michael@0 42 });
michael@0 43
michael@0 44 define('gclitest/recurse', [ 'require', 'exports', 'module', 'gclitest/recurse' ], function(require, exports, module) {
michael@0 45 require('gclitest/recurse');
michael@0 46 });
michael@0 47
michael@0 48 define('gclitest/firebug', [ 'gclitest/requirable' ], function(requirable) {
michael@0 49 return { requirable: requirable, fb: true };
michael@0 50 });
michael@0 51 }
michael@0 52
michael@0 53 function shutdown() {
michael@0 54 delete define.modules['gclitest/requirable'];
michael@0 55 delete define.globalDomain.modules['gclitest/requirable'];
michael@0 56 delete define.modules['gclitest/unrequirable'];
michael@0 57 delete define.globalDomain.modules['gclitest/unrequirable'];
michael@0 58 delete define.modules['gclitest/recurse'];
michael@0 59 delete define.globalDomain.modules['gclitest/recurse'];
michael@0 60 delete define.modules['gclitest/firebug'];
michael@0 61 delete define.globalDomain.modules['gclitest/firebug'];
michael@0 62
michael@0 63 define = undefined;
michael@0 64 require = undefined;
michael@0 65
michael@0 66 finish();
michael@0 67 }
michael@0 68
michael@0 69 function testWorking() {
michael@0 70 // There are lots of requirement tests that we could be doing here
michael@0 71 // The fact that we can get anything at all working is a testament to
michael@0 72 // require doing what it should - we don't need to test the
michael@0 73 let requireable = require('gclitest/requirable');
michael@0 74 is('thing1', requireable.thing1, 'thing1 was required');
michael@0 75 is(2, requireable.thing2, 'thing2 was required');
michael@0 76 is(requireable.thing3, undefined, 'thing3 was not required');
michael@0 77 }
michael@0 78
michael@0 79 function testDomains() {
michael@0 80 let requireable = require('gclitest/requirable');
michael@0 81 is(requireable.status, undefined, 'requirable has no status');
michael@0 82 requireable.setStatus(null);
michael@0 83 is(null, requireable.getStatus(), 'requirable.getStatus changed to null');
michael@0 84 is(requireable.status, undefined, 'requirable still has no status');
michael@0 85 requireable.setStatus('42');
michael@0 86 is('42', requireable.getStatus(), 'requirable.getStatus changed to 42');
michael@0 87 is(requireable.status, undefined, 'requirable *still* has no status');
michael@0 88
michael@0 89 let domain = new define.Domain();
michael@0 90 let requireable2 = domain.require('gclitest/requirable');
michael@0 91 is(requireable2.status, undefined, 'requirable2 has no status');
michael@0 92 is('initial', requireable2.getStatus(), 'requirable2.getStatus is initial');
michael@0 93 requireable2.setStatus(999);
michael@0 94 is(999, requireable2.getStatus(), 'requirable2.getStatus changed to 999');
michael@0 95 is(requireable2.status, undefined, 'requirable2 still has no status');
michael@0 96
michael@0 97 is('42', requireable.getStatus(), 'status 42');
michael@0 98 ok(requireable.status === undefined, 'requirable has no status (as expected)');
michael@0 99
michael@0 100 delete domain.modules['gclitest/requirable'];
michael@0 101 }
michael@0 102
michael@0 103 function testLeakage() {
michael@0 104 let requireable = require('gclitest/requirable');
michael@0 105 is(requireable.setup, null, 'leakage of setup');
michael@0 106 is(requireable.shutdown, null, 'leakage of shutdown');
michael@0 107 is(requireable.testWorking, null, 'leakage of testWorking');
michael@0 108 }
michael@0 109
michael@0 110 function testMultiImport() {
michael@0 111 let r1 = require('gclitest/requirable');
michael@0 112 let r2 = require('gclitest/requirable');
michael@0 113 is(r1, r2, 'double require was strict equal');
michael@0 114 }
michael@0 115
michael@0 116 function testUncompilable() {
michael@0 117 // It's not totally clear how a module loader should perform with unusable
michael@0 118 // modules, however at least it should go into a flat spin ...
michael@0 119 // GCLI mini_require reports an error as it should
michael@0 120 try {
michael@0 121 let unrequireable = require('gclitest/unrequirable');
michael@0 122 fail();
michael@0 123 }
michael@0 124 catch (ex) {
michael@0 125 // an exception is expected
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 function testRecursive() {
michael@0 130 // See Bug 658583
michael@0 131 // require('gclitest/recurse');
michael@0 132 // Also see the comments in the testRecursive() function
michael@0 133 }
michael@0 134
michael@0 135 function testFirebug() {
michael@0 136 let requirable = require('gclitest/requirable');
michael@0 137 let firebug = require('gclitest/firebug');
michael@0 138 ok(firebug.fb, 'firebug.fb is true');
michael@0 139 is(requirable, firebug.requirable, 'requirable pass-through');
michael@0 140 }

mercurial