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

feat(android-setup): Add support to remove port forwarding #2870

Merged
merged 2 commits into from
Jun 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface AndroidServiceConfigurator {
installService(deviceId: string): Promise<void>;
uninstallService(deviceId: string): Promise<void>;
setTcpForwarding(deviceId: string): Promise<void>;
removeTcpForwarding(deviceId: string): Promise<void>;
}

export interface AndroidServiceConfiguratorFactory {
Expand Down
10 changes: 8 additions & 2 deletions src/electron/platform/android/appium-service-configurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type AdbDevice = {
const servicePackageName: string = 'com.microsoft.accessibilityinsightsforandroidservice';

export class AppiumServiceConfigurator implements AndroidServiceConfigurator {
private readonly portNumber: number = 62442;

constructor(private readonly adb: ADB) {}

public getConnectedDevices = async (): Promise<Array<DeviceInfo>> => {
Expand Down Expand Up @@ -81,8 +83,12 @@ export class AppiumServiceConfigurator implements AndroidServiceConfigurator {
};

public setTcpForwarding = async (deviceId: string): Promise<void> => {
const port: number = 62442;
this.adb.setDeviceId(deviceId);
await this.adb.forwardPort(port, port);
await this.adb.forwardPort(this.portNumber, this.portNumber);
};

public removeTcpForwarding = async (deviceId: string): Promise<void> => {
this.adb.setDeviceId(deviceId);
await this.adb.removePortForward(this.portNumber);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('AppiumServiceConfigurator tests', () => {
const dumpsysAccessibilitySnippetWithServiceRunning: string =
'Service[label=Accessibility Insights for';
const pathToApk: string = './ServiceForAndroid/AccessibilityInsightsforAndroidService.apk';
const expectedPortNumber: number = 62442;

beforeEach(() => {
adbMock = Mock.ofType<ADB>(undefined, MockBehavior.Strict);
Expand Down Expand Up @@ -333,14 +334,38 @@ describe('AppiumServiceConfigurator tests', () => {
});

it('setTcpForwarding, succeeds', async () => {
const expectedPort: number = 62442;
adbMock.setup(m => m.setDeviceId(emulatorId)).verifiable(Times.once());
adbMock.setup(m => m.forwardPort(expectedPort, expectedPort)).verifiable(Times.once());
adbMock
.setup(m => m.forwardPort(expectedPortNumber, expectedPortNumber))
.verifiable(Times.once());

await testSubject.setTcpForwarding(emulatorId);

adbMock.verifyAll();
});

it('removeTcpForwarding, propagates error', async () => {
const expectedError: string = 'Oops';
adbMock
.setup(m => m.setDeviceId(emulatorId))
.throws(new Error(expectedError))
.verifiable(Times.once());

await expect(testSubject.removeTcpForwarding(emulatorId)).rejects.toThrowError(
expectedError,
);

adbMock.verifyAll();
});

it('removeTcpForwarding, succeeds', async () => {
adbMock.setup(m => m.setDeviceId(emulatorId)).verifiable(Times.once());
adbMock.setup(m => m.removePortForward(expectedPortNumber)).verifiable(Times.once());

await testSubject.removeTcpForwarding(emulatorId);

adbMock.verifyAll();
});
/*
// For live testing, set ANDROID_HOME or ANDROID_SDK_ROOT to point
// to your local installation, then add this line just before
Expand Down