-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wix#1989 from wix/break-emulator
Break emulator class into more contained sub-concern classes
- Loading branch information
Showing
13 changed files
with
202 additions
and
96 deletions.
There are no files selected for viewing
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
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
28 changes: 28 additions & 0 deletions
28
detox/src/devices/drivers/android/emulator/AVDValidator.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,28 @@ | ||
const _ = require('lodash'); | ||
const path = require('path'); | ||
const AVDsResolver = require('./AVDsResolver'); | ||
const environment = require('../../../../utils/environment'); | ||
|
||
class AVDValidator { | ||
constructor(emulatorExec) { | ||
this._avdsResolver = new AVDsResolver(emulatorExec); | ||
} | ||
|
||
async validate(avdName) { | ||
const avds = await this._avdsResolver.resolve(avdName); | ||
if (!avds) { | ||
const avdmanagerPath = path.join(environment.getAndroidSDKPath(), 'tools', 'bin', 'avdmanager'); | ||
|
||
throw new Error(`Could not find any configured Android Emulator.\n | ||
Try creating a device first, example: ${avdmanagerPath} create avd --force --name Pixel_API_28 --abi x86_64 --package "system-images;android-28;default;x86_64" --device "pixel" | ||
or go to https://developer.android.com/studio/run/managing-avds.html for details on how to create an Emulator.`); | ||
} | ||
|
||
if (_.indexOf(avds, avdName) === -1) { | ||
throw new Error(`Can not boot Android Emulator with the name: '${avdName}', | ||
make sure you choose one of the available emulators: ${avds.toString()}`); | ||
} | ||
} | ||
} | ||
|
||
module.exports = AVDValidator; |
53 changes: 53 additions & 0 deletions
53
detox/src/devices/drivers/android/emulator/AVDValidator.test.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,53 @@ | ||
describe('AVD validator', () => { | ||
let mockAvdsResolver; | ||
|
||
class MockAVDsResolver { | ||
constructor(...args) { | ||
mockAvdsResolver.ctor(...args); | ||
this.resolve = mockAvdsResolver.resolve; | ||
} | ||
} | ||
|
||
const emulatorExec = {}; | ||
let uut; | ||
beforeEach(() => { | ||
mockAvdsResolver = { | ||
ctor: jest.fn(), | ||
resolve: jest.fn().mockResolvedValue(['mock-avd-name']), | ||
}; | ||
jest.mock('./AVDsResolver', () => MockAVDsResolver); | ||
|
||
const AVDValidator = require('./AVDValidator'); | ||
uut = new AVDValidator(emulatorExec); | ||
}); | ||
|
||
it('should use an AVDs resolver', async () => { | ||
await uut.validate('mock-avd-name'); | ||
|
||
expect(mockAvdsResolver.ctor).toHaveBeenCalledWith(emulatorExec); | ||
expect(mockAvdsResolver.resolve).toHaveBeenCalledWith('mock-avd-name'); | ||
}); | ||
|
||
it('should return safely if AVD exists', async () => { | ||
mockAvdsResolver.resolve.mockResolvedValue(['mock-avd-name']); | ||
await uut.validate('mock-avd-name'); | ||
}); | ||
|
||
it('should throw if no AVDs found', async () => { | ||
mockAvdsResolver.resolve.mockResolvedValue(undefined); | ||
|
||
try { | ||
await uut.validate(); | ||
fail('expected to throw'); | ||
} catch (err) {} | ||
}); | ||
|
||
it('should throw if specific AVD not found', async () => { | ||
mockAvdsResolver.resolve.mockResolvedValue(['other-avd', 'yet-another']); | ||
|
||
try { | ||
await uut.validate(); | ||
fail('expected to throw'); | ||
} catch (err) {} | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
detox/src/devices/drivers/android/emulator/AVDsResolver.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,15 @@ | ||
const { ListAVDsCommand } = require('../tools/EmulatorExec'); | ||
|
||
class AVDsResolver { | ||
constructor(emulatorExec) { | ||
this._emulatorExec = emulatorExec; | ||
} | ||
|
||
async resolve() { | ||
const output = await this._emulatorExec.exec(new ListAVDsCommand()); | ||
const avds = output.trim().split('\n'); | ||
return avds; | ||
} | ||
} | ||
|
||
module.exports = AVDsResolver; |
28 changes: 28 additions & 0 deletions
28
detox/src/devices/drivers/android/emulator/AVDsResolver.test.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,28 @@ | ||
describe('AVDs resolver', () => { | ||
let MockListAVDsCommand; | ||
let emulatorExec; | ||
let uut; | ||
beforeEach(() => { | ||
MockListAVDsCommand = jest.genMockFromModule('../tools/EmulatorExec').ListAVDsCommand; | ||
jest.mock('../tools/EmulatorExec', () => ({ | ||
ListAVDsCommand: MockListAVDsCommand, | ||
})); | ||
|
||
emulatorExec = { | ||
exec: jest.fn().mockResolvedValue(''), | ||
}; | ||
|
||
const AVDsResolver = require('./AVDsResolver'); | ||
uut = new AVDsResolver(emulatorExec); | ||
}); | ||
|
||
it('should exec command', async () => { | ||
await uut.resolve(); | ||
expect(emulatorExec.exec).toHaveBeenCalledWith(expect.any(MockListAVDsCommand)); | ||
}); | ||
|
||
it('should parse emulators list given as text', async () => { | ||
emulatorExec.exec.mockResolvedValue('avd-device1\navd-device2\n'); | ||
expect(await uut.resolve()).toEqual(['avd-device1', 'avd-device2']); | ||
}); | ||
}); |
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
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
Oops, something went wrong.