|
| 1 | +/* @flow */ |
| 2 | +import {describe, it} from 'mocha'; |
| 3 | +import {assert} from 'chai'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +import {WebExtError, onlyInstancesOf} from '../../src/errors'; |
| 7 | +import {makeSureItFails} from '../helpers'; |
| 8 | +import {default as defaultConnector, RemoteFirefox} |
| 9 | + from '../../src/firefox/remote'; |
| 10 | + |
| 11 | + |
| 12 | +describe('firefox.remote', () => { |
| 13 | + |
| 14 | + describe('connect', () => { |
| 15 | + |
| 16 | + function prepareConnection(port=undefined, options={}) { |
| 17 | + options = { |
| 18 | + connectToFirefox: sinon.spy(() => Promise.resolve({})), |
| 19 | + ...options, |
| 20 | + }; |
| 21 | + const connect = defaultConnector(port, options); |
| 22 | + return {options, connect}; |
| 23 | + } |
| 24 | + |
| 25 | + it('resolves with a RemoteFirefox instance', () => { |
| 26 | + return prepareConnection().connect.then((client) => { |
| 27 | + assert.instanceOf(client, RemoteFirefox); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('connects on the default port', () => { |
| 32 | + const {connect, options} = prepareConnection(); |
| 33 | + return connect.then(() => { |
| 34 | + assert.equal(options.connectToFirefox.firstCall.args[0], 6000); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('lets you configure the port', () => { |
| 39 | + const {connect, options} = prepareConnection(7000); |
| 40 | + return connect.then(() => { |
| 41 | + assert.equal(options.connectToFirefox.args[0], 7000); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + }); |
| 46 | + |
| 47 | + describe('RemoteFirefox', () => { |
| 48 | + |
| 49 | + function fakeClient( |
| 50 | + {requestResult={}, requestError=null, |
| 51 | + makeRequestResult={}, makeRequestError=null}: Object = {}) { |
| 52 | + return { |
| 53 | + disconnect: sinon.spy(() => {}), |
| 54 | + request: sinon.spy( |
| 55 | + (request, callback) => callback(requestError, requestResult)), |
| 56 | + // This is client.client, the actual underlying connection. |
| 57 | + client: { |
| 58 | + makeRequest: sinon.spy((request, callback) => { |
| 59 | + // |
| 60 | + // The real function returns a response object that you |
| 61 | + // use like this: |
| 62 | + // if (response.error) { |
| 63 | + // ... |
| 64 | + // } else { |
| 65 | + // response.something; // ... |
| 66 | + // } |
| 67 | + // |
| 68 | + if (makeRequestError) { |
| 69 | + callback({error: makeRequestError}); |
| 70 | + } else { |
| 71 | + callback(makeRequestResult); |
| 72 | + } |
| 73 | + }), |
| 74 | + }, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + function fakeAddon() { |
| 79 | + return {id: 'some-id', actor: 'serv1.localhost'}; |
| 80 | + } |
| 81 | + |
| 82 | + function makeInstance(client=fakeClient()) { |
| 83 | + return new RemoteFirefox(client); |
| 84 | + } |
| 85 | + |
| 86 | + describe('disconnect', () => { |
| 87 | + it('lets you disconnect', () => { |
| 88 | + const client = fakeClient(); |
| 89 | + const conn = makeInstance(client); |
| 90 | + conn.disconnect(); |
| 91 | + assert.equal(client.disconnect.called, true); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('addonRequest', () => { |
| 96 | + |
| 97 | + it('makes requests to an add-on actor', () => { |
| 98 | + const addon = fakeAddon(); |
| 99 | + const stubResponse = {requestTypes: ['reload']}; |
| 100 | + const client = fakeClient({ |
| 101 | + makeRequestResult: stubResponse, |
| 102 | + }); |
| 103 | + |
| 104 | + const conn = makeInstance(client); |
| 105 | + return conn.addonRequest(addon, 'requestTypes') |
| 106 | + .then((response) => { |
| 107 | + |
| 108 | + assert.equal(client.client.makeRequest.called, true); |
| 109 | + const args = client.client.makeRequest.firstCall.args; |
| 110 | + assert.equal(args[0].type, 'requestTypes'); |
| 111 | + assert.equal(args[0].to, 'serv1.localhost'); |
| 112 | + |
| 113 | + assert.deepEqual(response, stubResponse); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('throws when add-on actor requests fail', () => { |
| 118 | + const addon = fakeAddon(); |
| 119 | + const client = fakeClient({ |
| 120 | + makeRequestError: new Error('some actor request failure'), |
| 121 | + }); |
| 122 | + |
| 123 | + const conn = makeInstance(client); |
| 124 | + return conn.addonRequest(addon, 'requestTypes') |
| 125 | + .then(makeSureItFails()) |
| 126 | + .catch(onlyInstancesOf(WebExtError, (error) => { |
| 127 | + assert.equal( |
| 128 | + error.message, |
| 129 | + 'requestTypes response error: Error: some actor request failure'); |
| 130 | + })); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('getInstalledAddon', () => { |
| 135 | + |
| 136 | + it('gets an installed add-on by ID', () => { |
| 137 | + const someAddonId = 'some-id'; |
| 138 | + const client = fakeClient({ |
| 139 | + requestResult: { |
| 140 | + addons: [{id: 'another-id'}, {id: someAddonId}, {id: 'bazinga'}], |
| 141 | + }, |
| 142 | + }); |
| 143 | + const conn = makeInstance(client); |
| 144 | + return conn.getInstalledAddon(someAddonId) |
| 145 | + .then((addon) => { |
| 146 | + assert.equal(addon.id, someAddonId); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it('throws an error when the add-on is not installed', () => { |
| 151 | + const client = fakeClient({ |
| 152 | + requestResult: { |
| 153 | + addons: [{id: 'one-id'}, {id: 'other-id'}], |
| 154 | + }, |
| 155 | + }); |
| 156 | + const conn = makeInstance(client); |
| 157 | + return conn.getInstalledAddon('missing-id') |
| 158 | + .then(makeSureItFails()) |
| 159 | + .catch(onlyInstancesOf(WebExtError, (error) => { |
| 160 | + assert.match(error.message, |
| 161 | + /does not have your extension installed/); |
| 162 | + })); |
| 163 | + }); |
| 164 | + |
| 165 | + it('throws an error when listAddons() fails', () => { |
| 166 | + const client = fakeClient({ |
| 167 | + requestError: new Error('some internal error'), |
| 168 | + }); |
| 169 | + const conn = makeInstance(client); |
| 170 | + return conn.getInstalledAddon('some-id') |
| 171 | + .then(makeSureItFails()) |
| 172 | + .catch(onlyInstancesOf(WebExtError, (error) => { |
| 173 | + assert.equal( |
| 174 | + error.message, |
| 175 | + 'Remote Firefox: listAddons() error: Error: some internal error'); |
| 176 | + })); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + describe('checkForAddonReloading', () => { |
| 181 | + |
| 182 | + it('checks for reload requestType in remote debugger', () => { |
| 183 | + const addon = fakeAddon(); |
| 184 | + const stubResponse = {requestTypes: ['reload']}; |
| 185 | + |
| 186 | + const conn = makeInstance(); |
| 187 | + conn.addonRequest = sinon.spy(() => Promise.resolve(stubResponse)); |
| 188 | + |
| 189 | + return conn.checkForAddonReloading(addon) |
| 190 | + .then((returnedAddon) => { |
| 191 | + assert.equal(conn.addonRequest.called, true); |
| 192 | + const args = conn.addonRequest.firstCall.args; |
| 193 | + |
| 194 | + assert.equal(args[0].id, addon.id); |
| 195 | + assert.equal(args[1], 'requestTypes'); |
| 196 | + |
| 197 | + assert.deepEqual(returnedAddon, addon); |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + it('throws an error if reload is not supported', () => { |
| 202 | + const addon = fakeAddon(); |
| 203 | + const stubResponse = {requestTypes: ['install']}; |
| 204 | + const conn = makeInstance(); |
| 205 | + conn.addonRequest = () => Promise.resolve(stubResponse); |
| 206 | + |
| 207 | + return conn.checkForAddonReloading(addon) |
| 208 | + .then(makeSureItFails()) |
| 209 | + .catch(onlyInstancesOf(WebExtError, (error) => { |
| 210 | + assert.match(error.message, /does not support addon\.reload/); |
| 211 | + })); |
| 212 | + }); |
| 213 | + |
| 214 | + it('only checks for reloading once', () => { |
| 215 | + const addon = fakeAddon(); |
| 216 | + const conn = makeInstance(); |
| 217 | + conn.addonRequest = |
| 218 | + sinon.spy(() => Promise.resolve({requestTypes: ['reload']})); |
| 219 | + return conn.checkForAddonReloading(addon) |
| 220 | + .then((addon) => conn.checkForAddonReloading(addon)) |
| 221 | + .then((returnedAddon) => { |
| 222 | + // This should remember not to check a second time. |
| 223 | + assert.equal(conn.addonRequest.callCount, 1); |
| 224 | + assert.deepEqual(returnedAddon, addon); |
| 225 | + }); |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + describe('reloadAddon', () => { |
| 230 | + |
| 231 | + it('asks the actor to reload the add-on', () => { |
| 232 | + const addon = fakeAddon(); |
| 233 | + const conn = makeInstance(); |
| 234 | + conn.getInstalledAddon = sinon.spy(() => Promise.resolve(addon)); |
| 235 | + conn.checkForAddonReloading = (addon) => Promise.resolve(addon); |
| 236 | + conn.addonRequest = sinon.spy(() => Promise.resolve({})); |
| 237 | + |
| 238 | + return conn.reloadAddon('some-id') |
| 239 | + .then(() => { |
| 240 | + assert.equal(conn.getInstalledAddon.called, true); |
| 241 | + assert.equal(conn.getInstalledAddon.firstCall.args[0], 'some-id'); |
| 242 | + |
| 243 | + assert.equal(conn.addonRequest.called, true); |
| 244 | + const requestArgs = conn.addonRequest.firstCall.args; |
| 245 | + assert.deepEqual(requestArgs[0], addon); |
| 246 | + assert.equal(requestArgs[1], 'reload'); |
| 247 | + }); |
| 248 | + }); |
| 249 | + |
| 250 | + it('makes sure the addon can be reloaded', () => { |
| 251 | + const addon = fakeAddon(); |
| 252 | + const conn = makeInstance(); |
| 253 | + conn.getInstalledAddon = () => Promise.resolve(addon); |
| 254 | + conn.checkForAddonReloading = |
| 255 | + sinon.spy((addon) => Promise.resolve(addon)); |
| 256 | + |
| 257 | + return conn.reloadAddon(addon.id) |
| 258 | + .then(() => { |
| 259 | + assert.equal(conn.checkForAddonReloading.called, true); |
| 260 | + assert.deepEqual(conn.checkForAddonReloading.firstCall.args[0], |
| 261 | + addon); |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + }); |
| 266 | + |
| 267 | + }); |
| 268 | + |
| 269 | +}); |
0 commit comments