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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-addon-installer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,179 @@
     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 } = require("chrome");
    1.10 +const AddonInstaller = require("sdk/addon/installer");
    1.11 +const { on, off } = require("sdk/system/events");
    1.12 +const { setTimeout } = require("sdk/timers");
    1.13 +const tmp = require("sdk/test/tmp-file");
    1.14 +const system = require("sdk/system");
    1.15 +const fixtures = require("./fixtures");
    1.16 +
    1.17 +const testFolderURL = module.uri.split('test-addon-installer.js')[0];
    1.18 +const ADDON_URL = testFolderURL + "fixtures/addon-install-unit-test@mozilla.com.xpi";
    1.19 +const ADDON_PATH = tmp.createFromURL(ADDON_URL);
    1.20 +
    1.21 +exports["test Install"] = function (assert, done) {
    1.22 +
    1.23 +  // Save all events distpatched by bootstrap.js of the installed addon
    1.24 +  let events = [];
    1.25 +  function eventsObserver({ data }) {
    1.26 +    events.push(data);
    1.27 +  }
    1.28 +  on("addon-install-unit-test", eventsObserver);
    1.29 +
    1.30 +  // Install the test addon
    1.31 +  AddonInstaller.install(ADDON_PATH).then(
    1.32 +    function onInstalled(id) {
    1.33 +      assert.equal(id, "addon-install-unit-test@mozilla.com", "`id` is valid");
    1.34 +
    1.35 +      // Now uninstall it
    1.36 +      AddonInstaller.uninstall(id).then(function () {
    1.37 +        // Ensure that bootstrap.js methods of the addon have been called
    1.38 +        // successfully and in the right order
    1.39 +        let expectedEvents = ["install", "startup", "shutdown", "uninstall"];
    1.40 +        assert.equal(JSON.stringify(events),
    1.41 +                         JSON.stringify(expectedEvents),
    1.42 +                         "addon's bootstrap.js functions have been called");
    1.43 +
    1.44 +        off("addon-install-unit-test", eventsObserver);
    1.45 +        done();
    1.46 +      });
    1.47 +    },
    1.48 +    function onFailure(code) {
    1.49 +      assert.fail("Install failed: "+code);
    1.50 +      off("addon-install-unit-test", eventsObserver);
    1.51 +      done();
    1.52 +    }
    1.53 +  );
    1.54 +};
    1.55 +
    1.56 +exports["test Failing Install With Invalid Path"] = function (assert, done) {
    1.57 +  AddonInstaller.install("invalid-path").then(
    1.58 +    function onInstalled(id) {
    1.59 +      assert.fail("Unexpected success");
    1.60 +      done();
    1.61 +    },
    1.62 +    function onFailure(code) {
    1.63 +      assert.equal(code, AddonInstaller.ERROR_FILE_ACCESS,
    1.64 +                       "Got expected error code");
    1.65 +      done();
    1.66 +    }
    1.67 +  );
    1.68 +};
    1.69 +
    1.70 +exports["test Failing Install With Invalid File"] = function (assert, done) {
    1.71 +  let directory = system.pathFor("ProfD");
    1.72 +  AddonInstaller.install(directory).then(
    1.73 +    function onInstalled(id) {
    1.74 +      assert.fail("Unexpected success");
    1.75 +      done();
    1.76 +    },
    1.77 +    function onFailure(code) {
    1.78 +      assert.equal(code, AddonInstaller.ERROR_CORRUPT_FILE,
    1.79 +                       "Got expected error code");
    1.80 +      done();
    1.81 +    }
    1.82 +  );
    1.83 +}
    1.84 +
    1.85 +exports["test Update"] = function (assert, done) {
    1.86 +  // Save all events distpatched by bootstrap.js of the installed addon
    1.87 +  let events = [];
    1.88 +  let iteration = 1;
    1.89 +  let eventsObserver = ({data}) => events.push(data);
    1.90 +  on("addon-install-unit-test", eventsObserver);
    1.91 +
    1.92 +  function onInstalled(id) {
    1.93 +    let prefix = "[" + iteration + "] ";
    1.94 +    assert.equal(id, "addon-install-unit-test@mozilla.com",
    1.95 +                     prefix + "`id` is valid");
    1.96 +
    1.97 +    // On 2nd and 3rd iteration, we receive uninstall events from the last
    1.98 +    // previously installed addon
    1.99 +    let expectedEvents =
   1.100 +      iteration == 1
   1.101 +      ? ["install", "startup"]
   1.102 +      : ["shutdown", "uninstall", "install", "startup"];
   1.103 +    assert.equal(JSON.stringify(events),
   1.104 +                     JSON.stringify(expectedEvents),
   1.105 +                     prefix + "addon's bootstrap.js functions have been called");
   1.106 +
   1.107 +    if (iteration++ < 3) {
   1.108 +      next();
   1.109 +    }
   1.110 +    else {
   1.111 +      events = [];
   1.112 +      AddonInstaller.uninstall(id).then(function() {
   1.113 +        let expectedEvents = ["shutdown", "uninstall"];
   1.114 +        assert.equal(JSON.stringify(events),
   1.115 +                     JSON.stringify(expectedEvents),
   1.116 +                     prefix + "addon's bootstrap.js functions have been called");
   1.117 +
   1.118 +        off("addon-install-unit-test", eventsObserver);
   1.119 +        done();
   1.120 +      });
   1.121 +    }
   1.122 +  }
   1.123 +  function onFailure(code) {
   1.124 +    assert.fail("Install failed: "+code);
   1.125 +    off("addon-install-unit-test", eventsObserver);
   1.126 +    done();
   1.127 +  }
   1.128 +
   1.129 +  function next() {
   1.130 +    events = [];
   1.131 +    AddonInstaller.install(ADDON_PATH).then(onInstalled, onFailure);
   1.132 +  }
   1.133 +
   1.134 +  next();
   1.135 +};
   1.136 +
   1.137 +exports['test Uninstall failure'] = function (assert, done) {
   1.138 +  AddonInstaller.uninstall('invalid-addon-path').then(
   1.139 +    () => assert.fail('Addon uninstall should not resolve successfully'),
   1.140 +    () => assert.pass('Addon correctly rejected invalid uninstall')
   1.141 +  ).then(done, assert.fail);
   1.142 +};
   1.143 +
   1.144 +exports['test Addon Disable and Enable'] = function (assert, done) {
   1.145 +  let ensureActive = (addonId) => AddonInstaller.isActive(addonId).then(state => {
   1.146 +    assert.equal(state, true, 'Addon should be enabled by default');
   1.147 +    return addonId;
   1.148 +  });
   1.149 +  let ensureInactive = (addonId) => AddonInstaller.isActive(addonId).then(state => {
   1.150 +    assert.equal(state, false, 'Addon should be disabled after disabling');
   1.151 +    return addonId;
   1.152 +  });
   1.153 +
   1.154 +  AddonInstaller.install(ADDON_PATH)
   1.155 +    .then(ensureActive)
   1.156 +    .then(AddonInstaller.enable) // should do nothing, yet not fail
   1.157 +    .then(ensureActive)
   1.158 +    .then(AddonInstaller.disable)
   1.159 +    .then(ensureInactive)
   1.160 +    .then(AddonInstaller.disable) // should do nothing, yet not fail
   1.161 +    .then(ensureInactive)
   1.162 +    .then(AddonInstaller.enable)
   1.163 +    .then(ensureActive)
   1.164 +    .then(AddonInstaller.uninstall)
   1.165 +    .then(done, assert.fail);
   1.166 +};
   1.167 +
   1.168 +exports['test Disable failure'] = function (assert, done) {
   1.169 +  AddonInstaller.disable('not-an-id').then(
   1.170 +    () => assert.fail('Addon disable should not resolve successfully'),
   1.171 +    () => assert.pass('Addon correctly rejected invalid disable')
   1.172 +  ).then(done, assert.fail);
   1.173 +};
   1.174 +
   1.175 +exports['test Enable failure'] = function (assert, done) {
   1.176 +  AddonInstaller.enable('not-an-id').then(
   1.177 +    () => assert.fail('Addon enable should not resolve successfully'),
   1.178 +    () => assert.pass('Addon correctly rejected invalid enable')
   1.179 +  ).then(done, assert.fail);
   1.180 +};
   1.181 +
   1.182 +require("test").run(exports);

mercurial