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