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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-self.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,79 @@
     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 +"use strict";
     1.8 +
     1.9 +const { Cc, Ci, Cu, Cm, components } = require("chrome");
    1.10 +const xulApp = require("sdk/system/xul-app");
    1.11 +const self = require("sdk/self");
    1.12 +const { Loader, main, unload } = require("toolkit/loader");
    1.13 +const loaderOptions = require("@loader/options");
    1.14 +
    1.15 +const { AddonManager } = Cu.import("resource://gre/modules/AddonManager.jsm", {});
    1.16 +
    1.17 +exports.testSelf = function(assert) {
    1.18 +  // Likewise, we can't assert anything about the full URL, because that
    1.19 +  // depends on self.id . We can only assert that it ends in the right
    1.20 +  // thing.
    1.21 +  var url = self.data.url("test-content-symbiont.js");
    1.22 +  assert.equal(typeof(url), "string", "self.data.url('x') returns string");
    1.23 +  assert.equal(/\/test-content-symbiont\.js$/.test(url), true);
    1.24 +
    1.25 +  // Make sure 'undefined' is not in url when no string is provided.
    1.26 +  url = self.data.url();
    1.27 +  assert.equal(typeof(url), "string", "self.data.url() returns string");
    1.28 +  assert.equal(/\/undefined$/.test(url), false);
    1.29 +
    1.30 +  // When tests are run on just the api-utils package, self.name is
    1.31 +  // api-utils. When they're run as 'cfx testall', self.name is testpkgs.
    1.32 +  assert.equal(self.name, "addon-sdk", "self.name is addon-sdk");
    1.33 +
    1.34 +  // loadReason may change here, as we change the way tests addons are installed
    1.35 +  // Bug 854937 fixed loadReason and is now install
    1.36 +  let testLoadReason = xulApp.versionInRange(xulApp.platformVersion,
    1.37 +                                             "23.0a1", "*") ? "install"
    1.38 +                                                            : "startup";
    1.39 +  assert.equal(self.loadReason, testLoadReason,
    1.40 +               "self.loadReason is either startup or install on test runs");
    1.41 +
    1.42 +  assert.equal(self.isPrivateBrowsingSupported, false,
    1.43 +               'usePrivateBrowsing property is false by default');
    1.44 +};
    1.45 +
    1.46 +exports.testSelfID = function(assert, done) {
    1.47 +  var self = require("sdk/self");
    1.48 +  // We can't assert anything about the ID inside the unit test right now,
    1.49 +  // because the ID we get depends upon how the test was invoked. The idea
    1.50 +  // is that it is supposed to come from the main top-level package's
    1.51 +  // package.json file, from the "id" key.
    1.52 +  assert.equal(typeof(self.id), "string", "self.id is a string");
    1.53 +  assert.ok(self.id.length > 0);
    1.54 +
    1.55 +  AddonManager.getAddonByID(self.id, function(addon) {
    1.56 +    if (!addon) {
    1.57 +      assert.fail("did not find addon with self.id");
    1.58 +    }
    1.59 +    else {
    1.60 +      assert.pass("found addon with self.id");
    1.61 +    }
    1.62 +
    1.63 +    done();
    1.64 +  });
    1.65 +}
    1.66 +
    1.67 +exports.testSelfHandlesLackingLoaderOptions = function (assert) {
    1.68 +  let root = module.uri.substr(0, module.uri.lastIndexOf('/'));
    1.69 +  let uri = root + '/fixtures/loader/self/';
    1.70 +  let sdkPath = loaderOptions.paths[''] + 'sdk';
    1.71 +  let loader = Loader({ paths: { '': uri, 'sdk': sdkPath }});
    1.72 +  let program = main(loader, 'main');
    1.73 +  let self = program.self;
    1.74 +  assert.pass("No errors thrown when including sdk/self without loader options");
    1.75 +  assert.equal(self.isPrivateBrowsingSupported, false,
    1.76 +    "safely checks sdk/self.isPrivateBrowsingSupported");
    1.77 +  assert.equal(self.packed, false,
    1.78 +    "safely checks sdk/self.packed");
    1.79 +  unload(loader);
    1.80 +};
    1.81 +
    1.82 +require("sdk/test").run(exports);

mercurial