test-cuddlefish.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. const { Loader, Require, unload, override } = require('sdk/loader/cuddlefish');
  6. const packaging = require('@loader/options');
  7. exports['test loader'] = function(assert) {
  8. var prints = [];
  9. function print(message) {
  10. prints.push(message);
  11. }
  12. let loader = Loader(override(packaging, {
  13. globals: {
  14. print: print,
  15. foo: 1
  16. }
  17. }));
  18. let require = Require(loader, module);
  19. var fixture = require('./loader/fixture');
  20. assert.equal(fixture.foo, 1, 'custom globals must work.');
  21. assert.equal(fixture.bar, 2, 'exports are set');
  22. assert.equal(prints[0], 'testing', 'global print must be injected.');
  23. var unloadsCalled = '';
  24. require("sdk/system/unload").when(function(reason) {
  25. assert.equal(reason, 'test', 'unload reason is passed');
  26. unloadsCalled += 'a';
  27. });
  28. require('sdk/system/unload.js').when(function() {
  29. unloadsCalled += 'b';
  30. });
  31. unload(loader, 'test');
  32. assert.equal(unloadsCalled, 'ba',
  33. 'loader.unload() must call listeners in LIFO order.');
  34. };
  35. require('test').run(exports);