Skip to content

Commit

Permalink
feat: Add a helper method to simulate low-memory event
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jan 9, 2024
1 parent 6498a6f commit 3cf9cc5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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 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.
Expand Down

0 comments on commit 3cf9cc5

Please sign in to comment.