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: Add a helper method to simulate low-memory event #896

Merged
merged 2 commits into from
Jan 10, 2024
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
39 changes: 39 additions & 0 deletions lib/commands/memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {errors} from 'appium/driver';
import {mixin} from './mixins';

/**
* @type {import('./mixins').MemoryMixin & ThisType<import('../driver').AndroidDriver>}
* @satisfies {import('@appium/types').ExternalDriver}
*/
const MemoryMixin = {
/**
* Simulates the onTrimMemory() event for the given package.
* Read https://developer.android.com/topic/performance/memory
* for more details.
*
* @param {import('./types').SendTrimMemoryOpts} opts
*/
async mobileSendTrimMemory(opts) {
const {
pkg,
level,
} = opts;

if (!pkg) {
throw new errors.InvalidArgumentError(`The 'pkg' argument must be provided`);
}
if (!level) {
throw new errors.InvalidArgumentError(`The 'level' argument must be provided`);
}

await this.adb.shell(['am', 'send-trim-memory', pkg, level]);
},
};

mixin(MemoryMixin);

export default MemoryMixin;

/**
* @typedef {import('appium-adb').ADB} ADB
*/
5 changes: 5 additions & 0 deletions lib/commands/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,10 @@ export interface DeviceidleMixin {
mobileDeviceidle(opts: types.DeviceidleOpts): Promise<void>;
}

export interface MemoryMixin {
mobileSendTrimMemory(opts: types.SendTrimMemoryOpts): Promise<void>;
}

declare module '../driver' {
interface AndroidDriver
extends ActionsMixin,
Expand All @@ -953,6 +957,7 @@ declare module '../driver' {
StreamScreenMixin,
SystemBarsMixin,
DeviceidleMixin,
MemoryMixin,
TouchMixin {}
}

Expand Down
7 changes: 7 additions & 0 deletions lib/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,13 @@ export interface DeviceidleOpts {
packages?: string|string[];
}

export interface SendTrimMemoryOpts {
/** The package name to send the `trimMemory` event to */
pkg: string;
/** The actual memory trim level to be sent */
level: 'COMPLETE' | 'MODERATE' | 'BACKGROUND' | 'UI_HIDDEN' | 'RUNNING_CRITICAL' | 'RUNNING_LOW' | 'RUNNING_MODERATE';
}

export interface SetUiModeOpts {
/**
* The UI mode to set the value for.
Expand Down
Loading