Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(adb): fix the adb tests #4691

Merged
merged 1 commit into from
Dec 12, 2020
Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"roll-browser": "node utils/roll_browser.js",
"coverage": "node test/checkCoverage.js",
"check-deps": "node utils/check_deps.js",
"build-android-driver": "./utils/build_android_driver.sh"
"build-android-driver": "./utils/build_android_driver.sh",
"test-android-driver": "PW_ANDROID_TESTS=1 npx folio test/android -p browserName=chromium --workers=1"
},
"author": {
"name": "Microsoft Corporation"
Expand Down
10 changes: 8 additions & 2 deletions src/dispatchers/androidDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Dispatcher, DispatcherScope } from './dispatcher';
import { Dispatcher, DispatcherScope, existingDispatcher } from './dispatcher';
import { Android, AndroidDevice } from '../server/android/android';
import * as channels from '../protocol/channels';
import { BrowserContextDispatcher } from './browserContextDispatcher';
Expand All @@ -27,7 +27,7 @@ export class AndroidDispatcher extends Dispatcher<Android, channels.AndroidIniti
async devices(params: channels.AndroidDevicesParams): Promise<channels.AndroidDevicesResult> {
const devices = await this._object.devices();
return {
devices: devices.map(d => new AndroidDeviceDispatcher(this._scope, d))
devices: devices.map(d => AndroidDeviceDispatcher.from(this._scope, d))
};
}

Expand All @@ -37,6 +37,12 @@ export class AndroidDispatcher extends Dispatcher<Android, channels.AndroidIniti
}

export class AndroidDeviceDispatcher extends Dispatcher<AndroidDevice, channels.AndroidDeviceInitializer> implements channels.AndroidDeviceChannel {

static from(scope: DispatcherScope, device: AndroidDevice): AndroidDeviceDispatcher {
const result = existingDispatcher<AndroidDeviceDispatcher>(device);
return result || new AndroidDeviceDispatcher(scope, device);
}

constructor(scope: DispatcherScope, device: AndroidDevice) {
super(scope, device, 'AndroidDevice', {
model: device.model,
Expand Down
19 changes: 17 additions & 2 deletions src/server/android/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export class Android {
}
return [...this._devices.values()];
}

_deviceClosed(device: AndroidDevice) {
this._devices.delete(device.serial);
}
}

export class AndroidDevice extends EventEmitter {
Expand All @@ -98,12 +102,16 @@ export class AndroidDevice extends EventEmitter {
static Events = {
WebViewAdded: 'webViewAdded',
WebViewRemoved: 'webViewRemoved',
Closed: 'closed'
};

private _browserConnections = new Set<AndroidBrowser>();
private _android: Android;
private _isClosed = false;

constructor(android: Android, backend: DeviceBackend, model: string) {
super();
this._android = android;
this._backend = backend;
this.model = model;
this.serial = backend.serial;
Expand Down Expand Up @@ -202,6 +210,7 @@ export class AndroidDevice extends EventEmitter {
}

async close() {
this._isClosed = true;
if (this._pollingWebViews)
clearTimeout(this._pollingWebViews);
for (const connection of this._browserConnections)
Expand All @@ -211,6 +220,8 @@ export class AndroidDevice extends EventEmitter {
driver.close();
}
await this._backend.close();
this._android._deviceClosed(this);
this.emit(AndroidDevice.Events.Closed);
}

async launchBrowser(pkg: string = 'com.android.chrome', options: types.BrowserContextOptions = {}): Promise<BrowserContext> {
Expand All @@ -234,11 +245,11 @@ export class AndroidDevice extends EventEmitter {
return await this._connectToBrowser(socketName, options);
}

connectToWebView(pid: number): Promise<BrowserContext> {
async connectToWebView(pid: number): Promise<BrowserContext> {
const webView = this._webViews.get(pid);
if (!webView)
throw new Error('WebView has been closed');
return this._connectToBrowser(`webview_devtools_remote_${pid}`);
return await this._connectToBrowser(`webview_devtools_remote_${pid}`);
}

private async _connectToBrowser(socketName: string, options: types.BrowserContextOptions = {}): Promise<BrowserContext> {
Expand Down Expand Up @@ -272,6 +283,8 @@ export class AndroidDevice extends EventEmitter {

private async _refreshWebViews() {
const sockets = (await this._backend.runCommand(`shell:cat /proc/net/unix | grep webview_devtools_remote`)).split('\n');
if (this._isClosed)
return;

const newPids = new Set<number>();
for (const line of sockets) {
Expand All @@ -286,6 +299,8 @@ export class AndroidDevice extends EventEmitter {
continue;

const procs = (await this._backend.runCommand(`shell:ps -A | grep ${pid}`)).split('\n');
if (this._isClosed)
return;
let pkg = '';
for (const proc of procs) {
const match = proc.match(/[^\s]+\s+(\d+).*$/);
Expand Down
3 changes: 2 additions & 1 deletion test/android/webview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if (process.env.PW_ANDROID_TESTS) {
await device.shell('am start org.chromium.webview_shell/.WebViewBrowserActivity');
const webview = await device.webView({ pkg: 'org.chromium.webview_shell' });
expect(webview.pkg()).toBe('org.chromium.webview_shell');
expect(device.webViews().length).toBe(1);
});

it('should connect to page', async function({ device }) {
Expand All @@ -33,7 +34,7 @@ if (process.env.PW_ANDROID_TESTS) {
expect(page.url()).toBe('about:blank');
});

it('should navigate page externally', async function({ device, server }) {
it('should navigate page internally', async function({ device, server }) {
expect(device.webViews().length).toBe(0);
await device.shell('am start org.chromium.webview_shell/.WebViewBrowserActivity');
const webview = await device.webView({ pkg: 'org.chromium.webview_shell' });
Expand Down