Skip to content

Commit

Permalink
fix: app termination behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dlenroc authored Jun 13, 2024
1 parent 7da8618 commit 262e5af
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
21 changes: 15 additions & 6 deletions src/commands/createSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ export async function createSession(
);

this.opts.context ??= 'ECP';
this.opts.arguments ??= {};

if (this.opts.entryPoint) {
this.opts.arguments['odc_entry_point'] = this.opts.entryPoint;
}

if (!this.opts.noReset) {
this.opts.arguments['odc_clear_registry'] = true;
}

if (this.opts.registry && Object.keys(this.opts.registry).length) {
this.opts.arguments['odc_registry'] = this.opts.registry;
}

this.sdk = new SDK({
ip: this.opts.ip,
username: this.opts.username ?? 'rokudev',
Expand All @@ -32,12 +46,7 @@ export async function createSession(

if (this.opts.app) {
await this.installApp(this.opts.app);
await this.activateApp('dev', {
odc_clear_registry: !this.opts.noReset,
...(this.opts.entryPoint && { odc_entry_point: this.opts.entryPoint }),
...(this.opts.registry && { odc_registry: this.opts.registry }),
...this.opts.arguments,
});
await this.activateApp('dev', this.opts.arguments);
}

return session;
Expand Down
20 changes: 8 additions & 12 deletions src/commands/deleteSession.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { BaseDriver, errors } from '@appium/base-driver';
import type { Driver } from '../Driver.ts';
import * as appiumUtils from '../helpers/appium.js';
import { Driver } from '../Driver.js';
import { SDK } from '../helpers/sdk.js';

export async function deleteSession(
this: Driver,
sessionId?: string | null
): Promise<void> {
try {
this.sdk.controller.abort(
new errors.UnknownError('The session was terminated!')
);
this.sdk.controller.abort(
new errors.UnknownError('The session was terminated!')
);

try {
const sdk = new SDK(this.sdk);
if (this.opts.shouldTerminateApp) {
await sdk.ecp.keypress({ key: 'Home' });
await appiumUtils.retrying({
timeout: 1e4,
validate: (state) => !!state && !!('id' in state.app),
command: () => sdk.ecp.queryActiveApp(),
});
const driver = new Driver(this.opts);
driver.sdk = sdk;
await driver.terminateApp('dev');
} else if (this.pressedKey) {
await sdk.ecp.keyup({ key: this.pressedKey });
}
Expand Down
19 changes: 3 additions & 16 deletions src/commands/installApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,18 @@ export async function installApp(this: Driver, appPath: string): Promise<void> {

if (alreadyInstalled) {
this.log.info('Channel is already installed');
await this.terminateApp('dev');
return;
}
}

this.log.info('Install channel');
const patchedApp = await odc.inject(source.buffer);

await this.sdk.developerServer.installChannel({
content: new Uint8Array(patchedApp),
});
const content = new Uint8Array(await odc.inject(source.buffer));
await this.sdk.developerServer.installChannel({ content });

await appiumUtils.retrying({
timeout: 1e4,
validate: (state) => state === 4,
command: () => this.queryAppState('dev'),
});

await this.performActions([
{
id: 'remote',
type: 'key',
actions: [
{ type: 'keyDown', value: 'Home' },
{ type: 'keyUp', value: 'Home' },
],
},
]);
}
21 changes: 16 additions & 5 deletions src/commands/terminateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,27 @@ export async function terminateApp(
timeout: 1e4,
validate: (state, error) => !error && state! <= 2,
command: async () => {
await this.sdk.ecp.exitApp({ appId: appId as AppId });
await this.sdk.ecp
.exitApp({ appId: appId as AppId })
.catch(async (error) => {
const state = await this.queryAppState(appId);
if (state === 4) {
await this.sdk.ecp.keypress({ key: 'Home' });
return;
}

return Promise.reject(error);
});

return this.queryAppState(appId);
},
});

await this.sdk.debugServer.clearLaunchCaches({
signal: AbortSignal.timeout(1e4),
});
if (state === 2) {
await this.sdk.debugServer.clearLaunchCaches({
signal: AbortSignal.timeout(1e4),
});

if (state == 2) {
state = await appiumUtils.retrying({
timeout: 1e4,
validate: (state, error) => !error && state! <= 1,
Expand Down

0 comments on commit 262e5af

Please sign in to comment.