Skip to content

Commit c604df2

Browse files
committed
fix(harmonyos): invalidate viewport on keyboard back
1 parent 52874bf commit c604df2

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/core/__tests__/harmonyos-interactor.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { expect, test, vi } from 'vitest';
22
import type { DeviceInfo } from '@agent-device/kernel/device';
33

4+
const { invalidateHarmonyGestureViewport, runHarmonyHdc } = vi.hoisted(() => ({
5+
invalidateHarmonyGestureViewport: vi.fn(),
6+
runHarmonyHdc: vi.fn(async () => ({ stdout: '', stderr: '', exitCode: 0 })),
7+
}));
8+
9+
vi.mock('../../platforms/harmonyos/hdc.ts', () => ({ runHarmonyHdc }));
410
vi.mock('../../platforms/harmonyos/snapshot.ts', () => ({
511
snapshotHarmony: vi.fn(async () => ({
612
nodes: [{ index: 0, type: 'Button', label: 'Continue' }],
713
truncated: false,
814
})),
915
readHarmonyGestureViewport: vi.fn(),
16+
invalidateHarmonyGestureViewport,
1017
}));
1118

1219
import { createHarmonyInteractor } from '../interactors/harmonyos.ts';
@@ -26,3 +33,18 @@ test('harmonyos snapshot stamps its channel and producer', async () => {
2633
expect(result.producer).toBe('harmonyos-uitest');
2734
expect(result.nodes).toEqual([{ index: 0, type: 'Button', label: 'Continue' }]);
2835
});
36+
37+
test('harmonyos keyboard dismiss invalidates gesture viewport through the Back route', async () => {
38+
await expect(createHarmonyInteractor(device).keyboardDismiss?.()).resolves.toEqual({
39+
kind: 'acknowledged',
40+
});
41+
42+
expect(runHarmonyHdc).toHaveBeenCalledWith(device, [
43+
'shell',
44+
'uitest',
45+
'uiInput',
46+
'keyEvent',
47+
'Back',
48+
]);
49+
expect(invalidateHarmonyGestureViewport).toHaveBeenCalledWith(device);
50+
});

src/platforms/harmonyos/__tests__/snapshot-viewport-cache.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'node:assert/strict';
22
import fs from 'node:fs';
3-
import { beforeEach, test, vi } from 'vitest';
3+
import { afterEach, beforeEach, test, vi } from 'vitest';
44
import type { DeviceInfo } from '@agent-device/kernel/device';
55

66
const { runHarmonyHdc } = vi.hoisted(() => ({ runHarmonyHdc: vi.fn() }));
@@ -33,6 +33,10 @@ beforeEach(() => {
3333
scriptHarmonyLayoutDump(TALL_LAYOUT);
3434
});
3535

36+
afterEach(() => {
37+
vi.useRealTimers();
38+
});
39+
3640
/** Scripts `uitest dumpLayout` + `file recv` so the pulled layout is `layout`. */
3741
function scriptHarmonyLayoutDump(layout: unknown): void {
3842
runHarmonyHdc.mockImplementation(async (_device: unknown, args: string[]) => {
@@ -56,6 +60,21 @@ test('repeated viewport reads within the TTL trigger a single layout dump', asyn
5660
assert.equal(dumpLayoutCallCount(), 1);
5761
});
5862

63+
test('viewport cache expires after two seconds without explicit invalidation', async () => {
64+
vi.useFakeTimers();
65+
vi.setSystemTime(0);
66+
67+
await readHarmonyGestureViewport(DEVICE_A);
68+
assert.equal(dumpLayoutCallCount(), 1);
69+
70+
scriptHarmonyLayoutDump(WIDE_LAYOUT);
71+
await vi.advanceTimersByTimeAsync(2_000);
72+
const refreshed = await readHarmonyGestureViewport(DEVICE_A);
73+
74+
assert.deepEqual(refreshed, { x: 0, y: 0, width: 2340, height: 1080 });
75+
assert.equal(dumpLayoutCallCount(), 2);
76+
});
77+
5978
test('invalidating the viewport cache forces a fresh layout dump', async () => {
6079
await readHarmonyGestureViewport(DEVICE_A);
6180
assert.equal(dumpLayoutCallCount(), 1);

src/platforms/harmonyos/input-actions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ export async function pressHarmonyKeyboardKey(
140140
device: DeviceInfo,
141141
key: 'Enter' | 'Back',
142142
): Promise<void> {
143+
if (key === 'Back') {
144+
await backHarmony(device);
145+
return;
146+
}
143147
await runHarmonyHdc(device, ['shell', 'uitest', 'uiInput', 'keyEvent', key]);
144148
}
145149

0 commit comments

Comments
 (0)