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.

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

mercurial