browser/devtools/shared/test/browser_require_basic.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/shared/test/browser_require_basic.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,140 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Tests that source URLs in the Web Console can be clicked to display the
     1.8 +// standard View Source window.
     1.9 +
    1.10 +let [ define, require ] = (function() {
    1.11 +  let tempScope = {};
    1.12 +  Components.utils.import("resource://gre/modules/devtools/Require.jsm", tempScope);
    1.13 +  return [ tempScope.define, tempScope.require ];
    1.14 +})();
    1.15 +
    1.16 +function test() {
    1.17 +  addTab("about:blank", function() {
    1.18 +    info("Starting Require Tests");
    1.19 +    setup();
    1.20 +
    1.21 +    testWorking();
    1.22 +    testDomains();
    1.23 +    testLeakage();
    1.24 +    testMultiImport();
    1.25 +    testRecursive();
    1.26 +    testUncompilable();
    1.27 +    testFirebug();
    1.28 +
    1.29 +    shutdown();
    1.30 +  });
    1.31 +}
    1.32 +
    1.33 +function setup() {
    1.34 +  define('gclitest/requirable', [ 'require', 'exports', 'module' ], function(require, exports, module) {
    1.35 +    exports.thing1 = 'thing1';
    1.36 +    exports.thing2 = 2;
    1.37 +
    1.38 +    let status = 'initial';
    1.39 +    exports.setStatus = function(aStatus) { status = aStatus; };
    1.40 +    exports.getStatus = function() { return status; };
    1.41 +  });
    1.42 +
    1.43 +  define('gclitest/unrequirable', [ 'require', 'exports', 'module' ], function(require, exports, module) {
    1.44 +    null.throwNPE();
    1.45 +  });
    1.46 +
    1.47 +  define('gclitest/recurse', [ 'require', 'exports', 'module', 'gclitest/recurse' ], function(require, exports, module) {
    1.48 +    require('gclitest/recurse');
    1.49 +  });
    1.50 +
    1.51 +  define('gclitest/firebug', [ 'gclitest/requirable' ], function(requirable) {
    1.52 +    return { requirable: requirable, fb: true };
    1.53 +  });
    1.54 +}
    1.55 +
    1.56 +function shutdown() {
    1.57 +  delete define.modules['gclitest/requirable'];
    1.58 +  delete define.globalDomain.modules['gclitest/requirable'];
    1.59 +  delete define.modules['gclitest/unrequirable'];
    1.60 +  delete define.globalDomain.modules['gclitest/unrequirable'];
    1.61 +  delete define.modules['gclitest/recurse'];
    1.62 +  delete define.globalDomain.modules['gclitest/recurse'];
    1.63 +  delete define.modules['gclitest/firebug'];
    1.64 +  delete define.globalDomain.modules['gclitest/firebug'];
    1.65 +
    1.66 +  define = undefined;
    1.67 +  require = undefined;
    1.68 +
    1.69 +  finish();
    1.70 +}
    1.71 +
    1.72 +function testWorking() {
    1.73 +  // There are lots of requirement tests that we could be doing here
    1.74 +  // The fact that we can get anything at all working is a testament to
    1.75 +  // require doing what it should - we don't need to test the
    1.76 +  let requireable = require('gclitest/requirable');
    1.77 +  is('thing1', requireable.thing1, 'thing1 was required');
    1.78 +  is(2, requireable.thing2, 'thing2 was required');
    1.79 +  is(requireable.thing3, undefined, 'thing3 was not required');
    1.80 +}
    1.81 +
    1.82 +function testDomains() {
    1.83 +  let requireable = require('gclitest/requirable');
    1.84 +  is(requireable.status, undefined, 'requirable has no status');
    1.85 +  requireable.setStatus(null);
    1.86 +  is(null, requireable.getStatus(), 'requirable.getStatus changed to null');
    1.87 +  is(requireable.status, undefined, 'requirable still has no status');
    1.88 +  requireable.setStatus('42');
    1.89 +  is('42', requireable.getStatus(), 'requirable.getStatus changed to 42');
    1.90 +  is(requireable.status, undefined, 'requirable *still* has no status');
    1.91 +
    1.92 +  let domain = new define.Domain();
    1.93 +  let requireable2 = domain.require('gclitest/requirable');
    1.94 +  is(requireable2.status, undefined, 'requirable2 has no status');
    1.95 +  is('initial', requireable2.getStatus(), 'requirable2.getStatus is initial');
    1.96 +  requireable2.setStatus(999);
    1.97 +  is(999, requireable2.getStatus(), 'requirable2.getStatus changed to 999');
    1.98 +  is(requireable2.status, undefined, 'requirable2 still has no status');
    1.99 +
   1.100 +  is('42', requireable.getStatus(), 'status 42');
   1.101 +  ok(requireable.status === undefined, 'requirable has no status (as expected)');
   1.102 +
   1.103 +  delete domain.modules['gclitest/requirable'];
   1.104 +}
   1.105 +
   1.106 +function testLeakage() {
   1.107 +  let requireable = require('gclitest/requirable');
   1.108 +  is(requireable.setup, null, 'leakage of setup');
   1.109 +  is(requireable.shutdown, null, 'leakage of shutdown');
   1.110 +  is(requireable.testWorking, null, 'leakage of testWorking');
   1.111 +}
   1.112 +
   1.113 +function testMultiImport() {
   1.114 +  let r1 = require('gclitest/requirable');
   1.115 +  let r2 = require('gclitest/requirable');
   1.116 +  is(r1, r2, 'double require was strict equal');
   1.117 +}
   1.118 +
   1.119 +function testUncompilable() {
   1.120 +  // It's not totally clear how a module loader should perform with unusable
   1.121 +  // modules, however at least it should go into a flat spin ...
   1.122 +  // GCLI mini_require reports an error as it should
   1.123 +  try {
   1.124 +    let unrequireable = require('gclitest/unrequirable');
   1.125 +    fail();
   1.126 +  }
   1.127 +  catch (ex) {
   1.128 +    // an exception is expected
   1.129 +  }
   1.130 +}
   1.131 +
   1.132 +function testRecursive() {
   1.133 +  // See Bug 658583
   1.134 +  // require('gclitest/recurse');
   1.135 +  // Also see the comments in the testRecursive() function
   1.136 +}
   1.137 +
   1.138 +function testFirebug() {
   1.139 +  let requirable = require('gclitest/requirable');
   1.140 +  let firebug = require('gclitest/firebug');
   1.141 +  ok(firebug.fb, 'firebug.fb is true');
   1.142 +  is(requirable, firebug.requirable, 'requirable pass-through');
   1.143 +}

mercurial