Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {execFileSync, execSync} from 'child_process';
import execa from 'execa';
import tryLaunchEmulator from '../tryLaunchEmulator';

jest.mock('child_process');
jest.mock('execa');

const flushPromises = () => Promise.resolve().then(() => Promise.resolve());

describe('tryLaunchEmulator', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.clearAllMocks();

(execa.sync as jest.Mock).mockReturnValue({stdout: 'Pixel_9'});
(execa as unknown as jest.Mock).mockReturnValue({
on: jest.fn(),
unref: jest.fn(),
});
(execSync as jest.Mock).mockReturnValue(
Buffer.from('List of devices attached\nemulator-5554\tdevice\n'),
);
(execFileSync as jest.Mock).mockReturnValue(Buffer.from(''));
});

afterEach(() => {
jest.useRealTimers();
});

test('waits for Android framework boot completion after adb connects', async () => {
const resultPromise = tryLaunchEmulator('/path/to/adb');
const onResult = jest.fn();
resultPromise.then(onResult);

jest.advanceTimersByTime(1000);
await flushPromises();

expect(onResult).not.toHaveBeenCalled();
expect(execFileSync).toHaveBeenCalledWith('/path/to/adb', [
'-s',
'emulator-5554',
'shell',
'getprop',
'sys.boot_completed',
]);

(execFileSync as jest.Mock).mockReturnValue(Buffer.from('1\n'));
jest.advanceTimersByTime(1000);

await expect(resultPromise).resolves.toEqual({success: true});
});

test('allows up to two minutes for a cold boot to complete', async () => {
const resultPromise = tryLaunchEmulator('/path/to/adb');
const onResult = jest.fn();
resultPromise.then(onResult);

jest.advanceTimersByTime(119_000);
await flushPromises();
expect(onResult).not.toHaveBeenCalled();

jest.advanceTimersByTime(1000);

await expect(resultPromise).resolves.toEqual({
success: false,
error: expect.stringContaining('It took too long'),
});
});
});
22 changes: 22 additions & 0 deletions packages/cli-platform-android/src/commands/runAndroid/adb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ function getDevices(adbPath: string): Array<string> {
}
}

/**
* Checks whether Android has finished booting on a connected device.
*/
function isDeviceBooted(adbPath: string, device: string): boolean {
try {
return (
execFileSync(adbPath, [
'-s',
device,
'shell',
'getprop',
'sys.boot_completed',
])
.toString()
.trim() === '1'
);
} catch (e) {
return false;
}
}

/**
* Gets available CPUs of devices from ADB
*/
Expand Down Expand Up @@ -90,6 +111,7 @@ function getCPU(adbPath: string, device: string): string | null {

export default {
getDevices,
isDeviceBooted,
getAvailableCPUs,
getCPU,
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ const launchEmulator = async (
},
);
cp.unref();
const timeout = 30;
const timeout = 120;

return new Promise<boolean>((resolve, reject) => {
const bootCheckInterval = setInterval(async () => {
const bootCheckInterval = setInterval(() => {
const devices = adb.getDevices(adbPath);
const connected = port
const connectedDevice = port
? devices.find((d) => d.includes(`${port}`))
: devices.length > 0;
if (connected) {
: devices[0];
if (connectedDevice && adb.isDeviceBooted(adbPath, connectedDevice)) {
cleanup();
resolve(true);
}
Expand Down
Loading