testing/xpcshell/example/unit/import_module.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/xpcshell/example/unit/import_module.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,34 @@
     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 +// Module used by test_import_module.js
     1.9 +
    1.10 +const EXPORTED_SYMBOLS = [ "MODULE_IMPORTED", "MODULE_URI", "SUBMODULE_IMPORTED", "same_scope", "SUBMODULE_IMPORTED_TO_SCOPE" ];
    1.11 +
    1.12 +const MODULE_IMPORTED = true;
    1.13 +const MODULE_URI = __URI__;
    1.14 +
    1.15 +// Will import SUBMODULE_IMPORTED into scope.
    1.16 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.17 +XPCOMUtils.importRelative(this, "import_sub_module.jsm");
    1.18 +
    1.19 +// Prepare two scopes that we can import the submodule into.
    1.20 +var scope1 = { __URI__: __URI__ };
    1.21 +var scope2 = { __URI__: __URI__ };
    1.22 +// First one is the regular path.
    1.23 +XPCOMUtils.importRelative(scope1, "import_sub_module.jsm");
    1.24 +scope1.test_obj.i++;
    1.25 +// Second one is with a different path (leads to the same file).
    1.26 +XPCOMUtils.importRelative(scope2, "duh/../import_sub_module.jsm");
    1.27 +// test_obj belongs to import_sub_module.jsm and has a mutable field name i, if
    1.28 +// the two modules are actually the same, then they'll share the same value.
    1.29 +// We'll leave it up to test_import_module.js to check that this variable is
    1.30 +// true.
    1.31 +var same_scope = (scope1.test_obj.i == scope2.test_obj.i);
    1.32 +
    1.33 +// Check that importRelative can also import into a given scope
    1.34 +var testScope = {};
    1.35 +XPCOMUtils.importRelative(this, "import_sub_module.jsm", testScope);
    1.36 +var SUBMODULE_IMPORTED_TO_SCOPE = testScope.SUBMODULE_IMPORTED;
    1.37 +

mercurial