main.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. exports["test local vs sdk module"] = function (assert) {
  6. assert.notEqual(require("memory"),
  7. require("sdk/deprecated/memory"),
  8. "Local module takes the priority over sdk modules");
  9. assert.ok(require("memory").local,
  10. "this module is really the local one");
  11. }
  12. exports["test 3rd party vs sdk module"] = function (assert) {
  13. // We are testing with a 3rd party package called `tabs` with 3 modules
  14. // main, page-mod and third-party
  15. // the only way to require 3rd party package modules are to use absolute paths
  16. // require("tabs/main"), require("tabs/page-mod"),
  17. // require("tabs/third-party") and also require("tabs") which will refer
  18. // to tabs's main package module.
  19. // So require(page-mod) shouldn't map the 3rd party
  20. assert.equal(require("page-mod"),
  21. require("sdk/page-mod"),
  22. "Third party modules don't overload sdk modules");
  23. assert.ok(require("page-mod").PageMod,
  24. "page-mod module is really the sdk one");
  25. assert.equal(require("tabs/page-mod").id, "page-mod",
  26. "tabs/page-mod is the 3rd party");
  27. // But require(tabs) will map to 3rd party main module
  28. // *and* overload the sdk module
  29. // and also any local module with the same name
  30. assert.equal(require("tabs").id, "tabs-main",
  31. "Third party main module overload sdk modules");
  32. assert.equal(require("tabs"),
  33. require("tabs/main"),
  34. "require(tabs) maps to require(tabs/main)");
  35. // So that you have to use relative path to ensure getting the local module
  36. assert.equal(require("./tabs").id,
  37. "local-tabs",
  38. "require(./tabs) maps to the local module");
  39. // It should still be possible to require sdk module with absolute path
  40. assert.ok(require("sdk/tabs").open,
  41. "We can bypass this overloading with absolute path to sdk modules");
  42. assert.equal(require("sdk/tabs"),
  43. require("addon-kit/tabs"),
  44. "Old and new layout both work");
  45. }
  46. // /!\ Always use distinct module for each test.
  47. // Otherwise, the linker can correctly parse and allow the first usage of it
  48. // but still silently fail on the second.
  49. exports.testRelativeRequire = function (assert) {
  50. assert.equal(require('./same-folder').id, 'same-folder');
  51. }
  52. exports.testRelativeSubFolderRequire = function (assert) {
  53. assert.equal(require('./sub-folder/module').id, 'sub-folder');
  54. }
  55. exports.testMultipleRequirePerLine = function (assert) {
  56. var a=require('./multiple/a'),b=require('./multiple/b');
  57. assert.equal(a.id, 'a');
  58. assert.equal(b.id, 'b');
  59. }
  60. exports.testSDKRequire = function (assert) {
  61. assert.deepEqual(Object.keys(require('sdk/page-worker')), ['Page']);
  62. assert.equal(require('page-worker'), require('sdk/page-worker'));
  63. }
  64. require("sdk/test/runner").runTestsFromModule(module);