addon-sdk/source/test/test-cuddlefish.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-cuddlefish.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,47 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +'use strict';
     1.9 +
    1.10 +const { Loader, Require, unload, override } = require('sdk/loader/cuddlefish');
    1.11 +const packaging = require('@loader/options');
    1.12 +
    1.13 +exports['test loader'] = function(assert) {
    1.14 +  var prints = [];
    1.15 +  function print(message) {
    1.16 +    prints.push(message);
    1.17 +  }
    1.18 +
    1.19 +  let loader = Loader(override(packaging, {
    1.20 +    globals: {
    1.21 +      print: print,
    1.22 +      foo: 1
    1.23 +    }
    1.24 +  }));
    1.25 +  let require = Require(loader, module);
    1.26 +
    1.27 +  var fixture = require('./loader/fixture');
    1.28 +
    1.29 +  assert.equal(fixture.foo, 1, 'custom globals must work.');
    1.30 +  assert.equal(fixture.bar, 2, 'exports are set');
    1.31 +
    1.32 +  assert.equal(prints[0], 'testing', 'global print must be injected.');
    1.33 +
    1.34 +  var unloadsCalled = '';
    1.35 +
    1.36 +  require("sdk/system/unload").when(function(reason) {
    1.37 +    assert.equal(reason, 'test', 'unload reason is passed');
    1.38 +    unloadsCalled += 'a';
    1.39 +  });
    1.40 +  require('sdk/system/unload.js').when(function() {
    1.41 +    unloadsCalled += 'b';
    1.42 +  });
    1.43 +
    1.44 +  unload(loader, 'test');
    1.45 +
    1.46 +  assert.equal(unloadsCalled, 'ba',
    1.47 +               'loader.unload() must call listeners in LIFO order.');
    1.48 +};
    1.49 +
    1.50 +require('test').run(exports);

mercurial