Skip to content

Commit

Permalink
feat: add screenState=SCREEN_STATE_OFF as the screen locked (#791)
Browse files Browse the repository at this point in the history
* feat: add screenState=SCREEN_STATE_OFF as the screen locked

* tweak the naming

* remove unnecessary await
  • Loading branch information
KazuCocoa authored Jan 24, 2025
1 parent ea47b65 commit 6f6f45a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
14 changes: 13 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ export async function getApkanalyzerForOs (sysHelpers) {
return await sysHelpers.getBinaryFromSdkRoot('apkanalyzer');
}

/**
* Checks screenState has SCREEN_STATE_OFF in dumpsys output to determine
* possible lock screen.
*
* @param {string} dumpsys - The output of dumpsys window command.
* @return {boolean} True if lock screen is showing.
*/
export function isScreenStateOff(dumpsys) {
return /\s+screenState=SCREEN_STATE_OFF/i.test(dumpsys);
}

/**
* Checks mShowingLockscreen or mDreamingLockscreen in dumpsys output to determine
* if lock screen is showing
Expand Down Expand Up @@ -323,7 +334,8 @@ export function getSurfaceOrientation (dumpsys) {

/*
* Checks mScreenOnFully in dumpsys output to determine if screen is showing
* Default is true
* Default is true.
* Note: this key
*/
export function isScreenOnFully (dumpsys) {
let m = /mScreenOnFully=\w+/gi.exec(dumpsys);
Expand Down
5 changes: 3 additions & 2 deletions lib/tools/lockmgmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { log } from '../logger.js';
import _ from 'lodash';
import {
isShowingLockscreen, isCurrentFocusOnKeyguard, isScreenOnFully,
isInDozingMode,
isInDozingMode, isScreenStateOff,
} from '../helpers.js';
import B from 'bluebird';
import { waitForCondition } from 'asyncbox';
Expand Down Expand Up @@ -217,7 +217,8 @@ export async function isScreenLocked () {
return isShowingLockscreen(windowOutput)
|| isCurrentFocusOnKeyguard(windowOutput)
|| !isScreenOnFully(windowOutput)
|| isInDozingMode(powerOutput);
|| isInDozingMode(powerOutput)
|| isScreenStateOff(windowOutput);
}

/**
Expand Down
59 changes: 59 additions & 0 deletions test/unit/helper-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
buildStartCmd, isShowingLockscreen, getBuildToolsDirs,
parseAaptStrings, parseAapt2Strings,
extractMatchingPermissions, parseLaunchableActivityNames, matchComponentName,
isScreenStateOff,
} from '../../lib/helpers';
import { withMocks } from '@appium/test-support';
import { fs } from '@appium/support';
Expand Down Expand Up @@ -58,6 +59,64 @@ describe('helpers', withMocks({fs}, function (mocks) {
});
});

describe('isScreenStateOff', function () {
it('should return true if isScreenStateOff is off', async function () {
let dumpsys = `
KeyguardServiceDelegate
showing=false
showingAndNotOccluded=true
inputRestricted=false
occluded=false
secure=false
dreaming=false
systemIsReady=true
deviceHasKeyguard=true
enabled=true
offReason=OFF_BECAUSE_OF_USER
currentUser=-10000
bootCompleted=true
screenState=SCREEN_STATE_OFF
interactiveState=INTERACTIVE_STATE_SLEEP
KeyguardStateMonitor
mIsShowing=false
mSimSecure=false
mInputRestricted=false
mTrusted=false
mCurrentUserId=0
...
`;
isScreenStateOff(dumpsys).should.be.true;
});
it('should return true if isScreenStateOff is on', async function () {
let dumpsys = `
KeyguardServiceDelegate
showing=false
showingAndNotOccluded=true
inputRestricted=false
occluded=false
secure=false
dreaming=false
systemIsReady=true
deviceHasKeyguard=true
enabled=true
offReason=OFF_BECAUSE_OF_USER
currentUser=-10000
bootCompleted=true
screenState=SCREEN_STATE_ON
interactiveState=INTERACTIVE_STATE_AWAKE
KeyguardStateMonitor
mIsShowing=false
mSimSecure=false
mInputRestricted=false
mTrusted=false
mCurrentUserId=0
...
`;
isScreenStateOff(dumpsys).should.be.false;
});
});


describe('isShowingLockscreen', function () {
it('should return true if mShowingLockscreen is true', async function () {
let dumpsys = 'mShowingLockscreen=true mShowingDream=false mDreamingLockscreen=false mTopIsFullscreen=false';
Expand Down

0 comments on commit 6f6f45a

Please sign in to comment.