Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const { Cc, Ci, Cu } = require("chrome"); |
michael@0 | 7 | const AddonInstaller = require("sdk/addon/installer"); |
michael@0 | 8 | const { on, off } = require("sdk/system/events"); |
michael@0 | 9 | const { setTimeout } = require("sdk/timers"); |
michael@0 | 10 | const tmp = require("sdk/test/tmp-file"); |
michael@0 | 11 | const system = require("sdk/system"); |
michael@0 | 12 | const fixtures = require("./fixtures"); |
michael@0 | 13 | |
michael@0 | 14 | const testFolderURL = module.uri.split('test-addon-installer.js')[0]; |
michael@0 | 15 | const ADDON_URL = testFolderURL + "fixtures/addon-install-unit-test@mozilla.com.xpi"; |
michael@0 | 16 | const ADDON_PATH = tmp.createFromURL(ADDON_URL); |
michael@0 | 17 | |
michael@0 | 18 | exports["test Install"] = function (assert, done) { |
michael@0 | 19 | |
michael@0 | 20 | // Save all events distpatched by bootstrap.js of the installed addon |
michael@0 | 21 | let events = []; |
michael@0 | 22 | function eventsObserver({ data }) { |
michael@0 | 23 | events.push(data); |
michael@0 | 24 | } |
michael@0 | 25 | on("addon-install-unit-test", eventsObserver); |
michael@0 | 26 | |
michael@0 | 27 | // Install the test addon |
michael@0 | 28 | AddonInstaller.install(ADDON_PATH).then( |
michael@0 | 29 | function onInstalled(id) { |
michael@0 | 30 | assert.equal(id, "addon-install-unit-test@mozilla.com", "`id` is valid"); |
michael@0 | 31 | |
michael@0 | 32 | // Now uninstall it |
michael@0 | 33 | AddonInstaller.uninstall(id).then(function () { |
michael@0 | 34 | // Ensure that bootstrap.js methods of the addon have been called |
michael@0 | 35 | // successfully and in the right order |
michael@0 | 36 | let expectedEvents = ["install", "startup", "shutdown", "uninstall"]; |
michael@0 | 37 | assert.equal(JSON.stringify(events), |
michael@0 | 38 | JSON.stringify(expectedEvents), |
michael@0 | 39 | "addon's bootstrap.js functions have been called"); |
michael@0 | 40 | |
michael@0 | 41 | off("addon-install-unit-test", eventsObserver); |
michael@0 | 42 | done(); |
michael@0 | 43 | }); |
michael@0 | 44 | }, |
michael@0 | 45 | function onFailure(code) { |
michael@0 | 46 | assert.fail("Install failed: "+code); |
michael@0 | 47 | off("addon-install-unit-test", eventsObserver); |
michael@0 | 48 | done(); |
michael@0 | 49 | } |
michael@0 | 50 | ); |
michael@0 | 51 | }; |
michael@0 | 52 | |
michael@0 | 53 | exports["test Failing Install With Invalid Path"] = function (assert, done) { |
michael@0 | 54 | AddonInstaller.install("invalid-path").then( |
michael@0 | 55 | function onInstalled(id) { |
michael@0 | 56 | assert.fail("Unexpected success"); |
michael@0 | 57 | done(); |
michael@0 | 58 | }, |
michael@0 | 59 | function onFailure(code) { |
michael@0 | 60 | assert.equal(code, AddonInstaller.ERROR_FILE_ACCESS, |
michael@0 | 61 | "Got expected error code"); |
michael@0 | 62 | done(); |
michael@0 | 63 | } |
michael@0 | 64 | ); |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | exports["test Failing Install With Invalid File"] = function (assert, done) { |
michael@0 | 68 | let directory = system.pathFor("ProfD"); |
michael@0 | 69 | AddonInstaller.install(directory).then( |
michael@0 | 70 | function onInstalled(id) { |
michael@0 | 71 | assert.fail("Unexpected success"); |
michael@0 | 72 | done(); |
michael@0 | 73 | }, |
michael@0 | 74 | function onFailure(code) { |
michael@0 | 75 | assert.equal(code, AddonInstaller.ERROR_CORRUPT_FILE, |
michael@0 | 76 | "Got expected error code"); |
michael@0 | 77 | done(); |
michael@0 | 78 | } |
michael@0 | 79 | ); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | exports["test Update"] = function (assert, done) { |
michael@0 | 83 | // Save all events distpatched by bootstrap.js of the installed addon |
michael@0 | 84 | let events = []; |
michael@0 | 85 | let iteration = 1; |
michael@0 | 86 | let eventsObserver = ({data}) => events.push(data); |
michael@0 | 87 | on("addon-install-unit-test", eventsObserver); |
michael@0 | 88 | |
michael@0 | 89 | function onInstalled(id) { |
michael@0 | 90 | let prefix = "[" + iteration + "] "; |
michael@0 | 91 | assert.equal(id, "addon-install-unit-test@mozilla.com", |
michael@0 | 92 | prefix + "`id` is valid"); |
michael@0 | 93 | |
michael@0 | 94 | // On 2nd and 3rd iteration, we receive uninstall events from the last |
michael@0 | 95 | // previously installed addon |
michael@0 | 96 | let expectedEvents = |
michael@0 | 97 | iteration == 1 |
michael@0 | 98 | ? ["install", "startup"] |
michael@0 | 99 | : ["shutdown", "uninstall", "install", "startup"]; |
michael@0 | 100 | assert.equal(JSON.stringify(events), |
michael@0 | 101 | JSON.stringify(expectedEvents), |
michael@0 | 102 | prefix + "addon's bootstrap.js functions have been called"); |
michael@0 | 103 | |
michael@0 | 104 | if (iteration++ < 3) { |
michael@0 | 105 | next(); |
michael@0 | 106 | } |
michael@0 | 107 | else { |
michael@0 | 108 | events = []; |
michael@0 | 109 | AddonInstaller.uninstall(id).then(function() { |
michael@0 | 110 | let expectedEvents = ["shutdown", "uninstall"]; |
michael@0 | 111 | assert.equal(JSON.stringify(events), |
michael@0 | 112 | JSON.stringify(expectedEvents), |
michael@0 | 113 | prefix + "addon's bootstrap.js functions have been called"); |
michael@0 | 114 | |
michael@0 | 115 | off("addon-install-unit-test", eventsObserver); |
michael@0 | 116 | done(); |
michael@0 | 117 | }); |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | function onFailure(code) { |
michael@0 | 121 | assert.fail("Install failed: "+code); |
michael@0 | 122 | off("addon-install-unit-test", eventsObserver); |
michael@0 | 123 | done(); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | function next() { |
michael@0 | 127 | events = []; |
michael@0 | 128 | AddonInstaller.install(ADDON_PATH).then(onInstalled, onFailure); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | next(); |
michael@0 | 132 | }; |
michael@0 | 133 | |
michael@0 | 134 | exports['test Uninstall failure'] = function (assert, done) { |
michael@0 | 135 | AddonInstaller.uninstall('invalid-addon-path').then( |
michael@0 | 136 | () => assert.fail('Addon uninstall should not resolve successfully'), |
michael@0 | 137 | () => assert.pass('Addon correctly rejected invalid uninstall') |
michael@0 | 138 | ).then(done, assert.fail); |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | exports['test Addon Disable and Enable'] = function (assert, done) { |
michael@0 | 142 | let ensureActive = (addonId) => AddonInstaller.isActive(addonId).then(state => { |
michael@0 | 143 | assert.equal(state, true, 'Addon should be enabled by default'); |
michael@0 | 144 | return addonId; |
michael@0 | 145 | }); |
michael@0 | 146 | let ensureInactive = (addonId) => AddonInstaller.isActive(addonId).then(state => { |
michael@0 | 147 | assert.equal(state, false, 'Addon should be disabled after disabling'); |
michael@0 | 148 | return addonId; |
michael@0 | 149 | }); |
michael@0 | 150 | |
michael@0 | 151 | AddonInstaller.install(ADDON_PATH) |
michael@0 | 152 | .then(ensureActive) |
michael@0 | 153 | .then(AddonInstaller.enable) // should do nothing, yet not fail |
michael@0 | 154 | .then(ensureActive) |
michael@0 | 155 | .then(AddonInstaller.disable) |
michael@0 | 156 | .then(ensureInactive) |
michael@0 | 157 | .then(AddonInstaller.disable) // should do nothing, yet not fail |
michael@0 | 158 | .then(ensureInactive) |
michael@0 | 159 | .then(AddonInstaller.enable) |
michael@0 | 160 | .then(ensureActive) |
michael@0 | 161 | .then(AddonInstaller.uninstall) |
michael@0 | 162 | .then(done, assert.fail); |
michael@0 | 163 | }; |
michael@0 | 164 | |
michael@0 | 165 | exports['test Disable failure'] = function (assert, done) { |
michael@0 | 166 | AddonInstaller.disable('not-an-id').then( |
michael@0 | 167 | () => assert.fail('Addon disable should not resolve successfully'), |
michael@0 | 168 | () => assert.pass('Addon correctly rejected invalid disable') |
michael@0 | 169 | ).then(done, assert.fail); |
michael@0 | 170 | }; |
michael@0 | 171 | |
michael@0 | 172 | exports['test Enable failure'] = function (assert, done) { |
michael@0 | 173 | AddonInstaller.enable('not-an-id').then( |
michael@0 | 174 | () => assert.fail('Addon enable should not resolve successfully'), |
michael@0 | 175 | () => assert.pass('Addon correctly rejected invalid enable') |
michael@0 | 176 | ).then(done, assert.fail); |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | require("test").run(exports); |