diff --git a/lib/commands/memory.js b/lib/commands/memory.js new file mode 100644 index 00000000..f4b2b4a0 --- /dev/null +++ b/lib/commands/memory.js @@ -0,0 +1,39 @@ +import {errors} from 'appium/driver'; +import {mixin} from './mixins'; + +/** + * @type {import('./mixins').MemoryMixin & ThisType} + * @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 + */ diff --git a/lib/commands/mixins.ts b/lib/commands/mixins.ts index e735b363..1f447ba0 100644 --- a/lib/commands/mixins.ts +++ b/lib/commands/mixins.ts @@ -927,6 +927,10 @@ export interface DeviceidleMixin { mobileDeviceidle(opts: types.DeviceidleOpts): Promise; } +export interface MemoryMixin { + mobileSendTrimMemory(opts: types.SendTrimMemoryOpts): Promise; +} + declare module '../driver' { interface AndroidDriver extends ActionsMixin, @@ -953,6 +957,7 @@ declare module '../driver' { StreamScreenMixin, SystemBarsMixin, DeviceidleMixin, + MemoryMixin, TouchMixin {} } diff --git a/lib/commands/types.ts b/lib/commands/types.ts index 7eede173..c7b97306 100644 --- a/lib/commands/types.ts +++ b/lib/commands/types.ts @@ -1101,6 +1101,13 @@ export interface DeviceidleOpts { packages?: string|string[]; } +export interface SendTrimMemoryOpts { + /** The package name to send trimMemory event to */ + pkg: string; + /** The atual trim level */ + level: 'COMPLETE' | 'MODERATE' | 'BACKGROUND' | 'UI_HIDDEN' | 'RUNNING_CRITICAL' | 'RUNNING_LOW' | 'RUNNING_MODERATE'; +} + export interface SetUiModeOpts { /** * The UI mode to set the value for.