-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Added new tests for the FirefoxAndroidExtensionRunner
- Loading branch information
Showing
1 changed file
with
175 additions
and
0 deletions.
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
tests/unit/test-extension-runners/test.firefox-android.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* @flow */ | ||
|
||
import {assert} from 'chai'; | ||
import {describe, it} from 'mocha'; | ||
import deepcopy from 'deepcopy'; | ||
import sinon from 'sinon'; | ||
|
||
import { | ||
FirefoxAndroidExtensionRunner, | ||
} from '../../../src/extension-runners/firefox-android'; | ||
import type { | ||
FirefoxAndroidExtensionRunnerParams, | ||
} from '../../../src/extension-runners/firefox-android'; | ||
import { | ||
UsageError, | ||
} from '../../../src/errors'; | ||
import { | ||
basicManifest, | ||
getFakeFirefox, | ||
getFakeRemoteFirefox, | ||
// makeSureItFails, | ||
StubChildProcess, | ||
} from '../helpers'; | ||
|
||
// Fake result for client.installTemporaryAddon().then(installResult => ...) | ||
const tempInstallResult = { | ||
addon: {id: 'some-addon@test-suite'}, | ||
}; | ||
|
||
function getFakeADBKit(implementations = {}) { | ||
const fakeADBClient = { | ||
listDevices: sinon.spy(() => { | ||
return []; | ||
}), | ||
...implementations, | ||
}; | ||
|
||
return { | ||
createClient: sinon.spy(() => { | ||
return fakeADBClient; | ||
}), | ||
fakeADBClient, | ||
util: { | ||
readAll: sinon.spy(() => ''), | ||
}, | ||
}; | ||
} | ||
|
||
type PrepareParams = { | ||
params?: Object, | ||
deps?: Object, | ||
fakeFirefoxApp?: Object, | ||
fakeRemoteFirefox?: Object, | ||
fakeADBClient?: Object, | ||
debuggerPort?: number, | ||
} | ||
|
||
function prepareExtensionRunnerParams({ | ||
params, fakeFirefoxApp, fakeRemoteFirefox, fakeADBClient, debuggerPort, | ||
}: PrepareParams = {}) { | ||
const remoteFirefox = getFakeRemoteFirefox({ | ||
installTemporaryAddon: sinon.spy( | ||
() => Promise.resolve(tempInstallResult) | ||
), | ||
...fakeRemoteFirefox, | ||
}); | ||
const firefoxProcess = new StubChildProcess(); | ||
|
||
// $FLOW_IGNORE: allow overriden params for testing purpose. | ||
const runnerParams: FirefoxAndroidExtensionRunnerParams = { | ||
extensions: [{ | ||
sourceDir: '/fake/sourceDir', | ||
manifestData: deepcopy(basicManifest), | ||
}], | ||
keepProfileChanges: false, | ||
browserConsole: false, | ||
startUrl: undefined, | ||
firefoxBinary: 'firefox', | ||
preInstall: false, | ||
firefoxApp: getFakeFirefox({ | ||
run: sinon.spy(() => { | ||
return Promise.resolve({ | ||
debuggerPort, | ||
firefox: firefoxProcess, | ||
}); | ||
}), | ||
...fakeFirefoxApp, | ||
}, debuggerPort), | ||
adb: getFakeADBKit(fakeADBClient), | ||
firefoxClient: () => { | ||
return Promise.resolve(remoteFirefox); | ||
}, | ||
...(params || {}), | ||
}; | ||
|
||
return { | ||
remoteFirefox, | ||
firefoxProcess, | ||
params: runnerParams, | ||
}; | ||
} | ||
|
||
describe('util/extension-runners/firefox-android', () => { | ||
|
||
describe('raises an UsageError when', () => { | ||
|
||
async function testUsageError(prepareTestParams, testExceptionCallback) { | ||
const {params} = prepareExtensionRunnerParams(prepareTestParams); | ||
const runnerInstance = new FirefoxAndroidExtensionRunner(params); | ||
|
||
let actualException; | ||
|
||
try { | ||
await runnerInstance.run(); | ||
} catch (error) { | ||
actualException = error; | ||
} | ||
|
||
const {adb} = params; | ||
return testExceptionCallback({adb, actualException}); | ||
} | ||
|
||
it('does not find an adb binary', async () => { | ||
await testUsageError({ | ||
fakeADBClient: { | ||
listDevices: sinon.spy(() => { | ||
const fakeADBError = new Error('spawn adb'); | ||
// $FLOW_FIXME: reuse ErrorWithCode from other tests | ||
fakeADBError.code = 'ENOENT'; | ||
return Promise.reject(fakeADBError); | ||
}), | ||
}, | ||
}, ({adb, actualException}) => { | ||
sinon.assert.calledOnce(adb.createClient); | ||
sinon.assert.calledOnce(adb.fakeADBClient.listDevices); | ||
|
||
assert.ok(actualException instanceof UsageError); | ||
assert.match(actualException && actualException.message, | ||
/No adb executable has been found/); | ||
}); | ||
}); | ||
|
||
it('does not find an android device', async () => { | ||
await testUsageError({}, ({adb, actualException}) => { | ||
sinon.assert.calledOnce(adb.createClient); | ||
sinon.assert.calledOnce(adb.fakeADBClient.listDevices); | ||
|
||
assert.ok(actualException instanceof UsageError); | ||
assert.match(actualException && actualException.message, | ||
/No Android device found/); | ||
}); | ||
}); | ||
|
||
it('does not know which is the selected android device', async () => { | ||
await testUsageError({ | ||
fakeADBClient: { | ||
listDevices: sinon.spy(() => { | ||
return Promise.resolve([ | ||
{id: 'emulator-1'}, {id: 'emulator-2'}, | ||
]); | ||
}), | ||
}, | ||
}, ({adb, actualException}) => { | ||
sinon.assert.calledOnce(adb.createClient); | ||
sinon.assert.calledOnce(adb.fakeADBClient.listDevices); | ||
|
||
assert.ok(actualException instanceof UsageError); | ||
assert.match(actualException && actualException.message, | ||
/Select an android device using --android-device/); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
}); |