michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const { Cc, Ci, Cu, Cm, components } = require("chrome"); michael@0: const xulApp = require("sdk/system/xul-app"); michael@0: const self = require("sdk/self"); michael@0: const { Loader, main, unload } = require("toolkit/loader"); michael@0: const loaderOptions = require("@loader/options"); michael@0: michael@0: const { AddonManager } = Cu.import("resource://gre/modules/AddonManager.jsm", {}); michael@0: michael@0: exports.testSelf = function(assert) { michael@0: // Likewise, we can't assert anything about the full URL, because that michael@0: // depends on self.id . We can only assert that it ends in the right michael@0: // thing. michael@0: var url = self.data.url("test-content-symbiont.js"); michael@0: assert.equal(typeof(url), "string", "self.data.url('x') returns string"); michael@0: assert.equal(/\/test-content-symbiont\.js$/.test(url), true); michael@0: michael@0: // Make sure 'undefined' is not in url when no string is provided. michael@0: url = self.data.url(); michael@0: assert.equal(typeof(url), "string", "self.data.url() returns string"); michael@0: assert.equal(/\/undefined$/.test(url), false); michael@0: michael@0: // When tests are run on just the api-utils package, self.name is michael@0: // api-utils. When they're run as 'cfx testall', self.name is testpkgs. michael@0: assert.equal(self.name, "addon-sdk", "self.name is addon-sdk"); michael@0: michael@0: // loadReason may change here, as we change the way tests addons are installed michael@0: // Bug 854937 fixed loadReason and is now install michael@0: let testLoadReason = xulApp.versionInRange(xulApp.platformVersion, michael@0: "23.0a1", "*") ? "install" michael@0: : "startup"; michael@0: assert.equal(self.loadReason, testLoadReason, michael@0: "self.loadReason is either startup or install on test runs"); michael@0: michael@0: assert.equal(self.isPrivateBrowsingSupported, false, michael@0: 'usePrivateBrowsing property is false by default'); michael@0: }; michael@0: michael@0: exports.testSelfID = function(assert, done) { michael@0: var self = require("sdk/self"); michael@0: // We can't assert anything about the ID inside the unit test right now, michael@0: // because the ID we get depends upon how the test was invoked. The idea michael@0: // is that it is supposed to come from the main top-level package's michael@0: // package.json file, from the "id" key. michael@0: assert.equal(typeof(self.id), "string", "self.id is a string"); michael@0: assert.ok(self.id.length > 0); michael@0: michael@0: AddonManager.getAddonByID(self.id, function(addon) { michael@0: if (!addon) { michael@0: assert.fail("did not find addon with self.id"); michael@0: } michael@0: else { michael@0: assert.pass("found addon with self.id"); michael@0: } michael@0: michael@0: done(); michael@0: }); michael@0: } michael@0: michael@0: exports.testSelfHandlesLackingLoaderOptions = function (assert) { michael@0: let root = module.uri.substr(0, module.uri.lastIndexOf('/')); michael@0: let uri = root + '/fixtures/loader/self/'; michael@0: let sdkPath = loaderOptions.paths[''] + 'sdk'; michael@0: let loader = Loader({ paths: { '': uri, 'sdk': sdkPath }}); michael@0: let program = main(loader, 'main'); michael@0: let self = program.self; michael@0: assert.pass("No errors thrown when including sdk/self without loader options"); michael@0: assert.equal(self.isPrivateBrowsingSupported, false, michael@0: "safely checks sdk/self.isPrivateBrowsingSupported"); michael@0: assert.equal(self.packed, false, michael@0: "safely checks sdk/self.packed"); michael@0: unload(loader); michael@0: }; michael@0: michael@0: require("sdk/test").run(exports);