-
Notifications
You must be signed in to change notification settings - Fork 0
/
performActions.ts
169 lines (154 loc) · 4.35 KB
/
performActions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { errors } from '@appium/base-driver';
import { util } from '@appium/support';
import type {
ActionSequence,
KeyAction,
NullAction,
PointerAction,
} from '@appium/types';
import type { Key } from '@dlenroc/roku-ecp';
import { setTimeout as sleep } from 'node:timers/promises';
import type { Driver } from '../Driver.ts';
import { focus } from './internal/focus.js';
const Keys: Record<string, Key> = {
'\uE002': 'Info', // help
'\ue003': 'Backspace', // backspace
'\ue006': 'Enter', // return
'\ue007': 'Select', // enter
'\ue00b': 'Play', // pause
'\uE00C': 'Back', // escape
'\uE00E': 'ChannelUp', // page_up
'\uE00F': 'ChannelDown', // page_down
'\ue011': 'Home', // home
'\ue012': 'Left', // arrow_left
'\ue013': 'Up', // arrow_up
'\ue014': 'Right', // arrow_right
'\ue015': 'Down', // arrow_down
'\uE01A': 'InputAV1', // numpad_0
'\uE01B': 'InputHDMI1', // numpad_1
'\uE01C': 'InputHDMI2', // numpad_2
'\uE01D': 'InputHDMI3', // numpad_3
'\uE01E': 'InputHDMI4', // numpad_4
'\uE01F': 'InputTuner', // numpad_5
'\uE036': 'InstantReplay', // f6
'\uE037': 'Rev', // f7
'\uE038': 'Play', // f8
'\uE039': 'Fwd', // f9
'\uE03A': 'VolumeMute', // f10
'\uE03B': 'VolumeDown', // f11
'\uE03C': 'VolumeUp', // f12
// Search
// FindRemote
// PowerOff
};
export async function performActions(
this: Driver,
actions: ActionSequence[]
): Promise<void> {
for (const action of actions) {
switch (action.type) {
case 'none':
await performNoneActions.call(this, action.actions);
break;
case 'key':
await performKeyActions.call(this, action.actions);
break;
case 'pointer':
await performPointerActions.call(this, action.actions);
break;
default:
throw new errors.InvalidArgumentError(
`Unsupported action: ${JSON.stringify(action)}`
);
}
}
}
async function performNoneActions(
this: Driver,
actions: NullAction[]
): Promise<void> {
for (const action of actions) {
switch (action.type) {
case 'pause':
await sleep(action.duration);
break;
default:
throw new errors.InvalidArgumentError(
`Unsupported none action: ${JSON.stringify(action)}`
);
}
}
}
async function performKeyActions(
this: Driver,
actions: KeyAction[]
): Promise<void> {
for (let i = 0, n = actions.length; i < n; i++) {
const action = actions[i]!;
const nextAction = actions[i + 1];
if (action.type === 'pause') {
await performNoneActions.call(this, [action]);
continue;
}
let key = Keys[action.value] || action.value;
key = key?.length === 1 ? `LIT_${encodeURIComponent(key)}` : key;
if (
action.type === 'keyDown' &&
nextAction?.type === 'keyUp' &&
action.value === nextAction.value
) {
i++;
await this.sdk.ecp.keypress({ key });
this.pressedKey = undefined;
continue;
}
switch (action.type) {
case 'keyDown':
this.pressedKey = key;
await this.sdk.ecp.keydown({ key });
break;
case 'keyUp':
await this.sdk.ecp.keyup({ key });
this.pressedKey = undefined;
break;
default:
throw new errors.InvalidArgumentError(
`Unsupported key action: ${JSON.stringify(action)}`
);
}
}
}
async function performPointerActions(
this: Driver,
actions: PointerAction[]
): Promise<void> {
for (const action of actions) {
if (action.type === 'pause') {
await performNoneActions.call(this, [action]);
continue;
}
if (action.type === 'pointerMove') {
const origin = action.origin ?? '';
const elementId = util.unwrapElement(origin);
if (origin === elementId) {
throw new errors.InvalidArgumentError(
'"origin" property of pointerMove action must be an element ID: ' +
JSON.stringify(action)
);
}
if (action.x || action.y) {
throw new errors.InvalidArgumentError(
'"x" and "y" properties of pointerMove action are not supported: ' +
JSON.stringify(action)
);
}
await focus.call(this, elementId, {
timeout: Math.max(0, action.duration ?? 0) || 1e4,
});
continue;
}
throw new errors.InvalidArgumentError(
`Unsupported pointer action: ${JSON.stringify(action)}`
);
}
}