-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.ts
258 lines (210 loc) · 8.09 KB
/
api.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { EventEmitter } from 'events';
import { KeyCode } from '../util/keycode';
import { MonkeyCallback } from '../util';
// reference https://android.googlesource.com/platform/development/+/master/cmds/monkey/src/com/android/commands/monkey/MonkeySourceNetworkVars.java
export default abstract class Api extends EventEmitter {
public abstract send<T>(command: string, cb?: MonkeyCallback<T>): this;
public abstract sendAndParse<T>(
command: string,
cb?: MonkeyCallback<T>,
parser?: (data: string | null) => T | null
): this;
private getAndParse(
command: string,
parser: { type: 'number' },
cb?: MonkeyCallback<number | null>
): this;
private getAndParse(
command: string,
parser: {
type: 'stringArray';
splitter: string | RegExp;
},
cb?: MonkeyCallback<string[] | null>
): this;
private getAndParse(
command: string,
parser:
| {
type: 'stringArray';
splitter: string | RegExp;
}
| { type: 'number' },
cb?: MonkeyCallback<number | null> | MonkeyCallback<string[] | null>
): this {
return this.sendAndParse(
'getvar ' + command,
cb as MonkeyCallback<number | string[] | null>,
(data) => {
if (!data) {
return null;
}
if (parser.type === 'number') {
return parseFloat(data);
}
return data.split(parser.splitter);
}
);
}
public keyDown(keyCode: KeyCode | number, cb?: MonkeyCallback): this {
return this.send('key down ' + keyCode, cb);
}
public keyUp(keyCode: KeyCode | number, cb?: MonkeyCallback): this {
return this.send('key up ' + keyCode, cb);
}
public touchDown(x: number, y: number, cb?: MonkeyCallback): this {
return this.send('touch down ' + x + ' ' + y, cb);
}
public touchUp(x: number, y: number, cb?: MonkeyCallback): this {
return this.send('touch up ' + x + ' ' + y, cb);
}
public touchMove(x: number, y: number, cb?: MonkeyCallback): this {
return this.send('touch move ' + x + ' ' + y, cb);
}
public trackball(dx: number, dy: number, cb?: MonkeyCallback): this {
return this.send('trackball ' + dx + ' ' + dy, cb);
}
public flipOpen(cb?: MonkeyCallback): this {
return this.send('flip open', cb);
}
public flipClose(cb?: MonkeyCallback): this {
return this.send('flip close', cb);
}
public wake(cb?: MonkeyCallback): this {
return this.send('wake', cb);
}
public tap(x: number, y: number, cb?: MonkeyCallback): this {
return this.send('tap ' + x + ' ' + y, cb);
}
public press(keyCode: KeyCode | number, cb?: MonkeyCallback): this {
return this.send('press ' + keyCode, cb);
}
public type(str: string, cb?: MonkeyCallback): this {
str = str.replace(/"/g, '\\"');
if (str.indexOf(' ') === -1) {
return this.send('type ' + str, cb);
} else {
return this.send('type "' + str + '"', cb);
}
}
public list(cb?: MonkeyCallback<string[] | null>): this {
return this.sendAndParse('listvar', cb, (vars) => {
return vars?.trim().split(/\s+/g) || null;
});
}
public get(name: string, cb?: MonkeyCallback<string | null>): this {
return this.send('getvar ' + name, cb);
}
public sleep(ms: number, cb?: MonkeyCallback): this {
return this.send('sleep ' + ms, cb);
}
public quit(cb?: MonkeyCallback): this {
return this.send('quit', cb);
}
public done(cb?: MonkeyCallback): this {
return this.send('done', cb);
}
// am. note that the current activity information isn't valid
// until the first activity gets launched after the monkey has
// been started.
public getAmCurrentAction(cb?: MonkeyCallback<string | null>): this {
return this.get('am.current.action', cb);
}
public getAmCurrentCategories(cb?: MonkeyCallback<string[] | null>): this {
return this.getAndParse(
'am.current.categories',
{ type: 'stringArray', splitter: /\s+/g },
cb
);
}
public getAmCurrentCompClass(cb?: MonkeyCallback<string | null>): this {
return this.get('am.current.comp.class', cb);
}
public getAmCurrentCompPackage(cb?: MonkeyCallback<string | null>): this {
return this.get('am.current.comp.package', cb);
}
public getAmCurrentData(cb?: MonkeyCallback<string | null>): this {
return this.get('am.current.data', cb);
}
public getAmCurrentPackage(cb?: MonkeyCallback<string | null>): this {
return this.get('am.current.package', cb);
}
public getBuildBoard(cb?: MonkeyCallback<string | null>): this {
return this.get('build.board', cb);
}
public getBuildBrand(cb?: MonkeyCallback<string | null>): this {
return this.get('build.brand', cb);
}
public getBuildCpuAbi(cb?: MonkeyCallback<string | null>): this {
return this.get('build.cpu_abi', cb);
}
public getBuildDevice(cb?: MonkeyCallback<string | null>): this {
return this.get('build.device', cb);
}
public getBuildDisplay(cb?: MonkeyCallback<string | null>): this {
return this.get('build.display', cb);
}
public getBuildFingerprint(cb?: MonkeyCallback<string | null>): this {
return this.get('build.fingerprint', cb);
}
public getBuildHost(cb?: MonkeyCallback<string | null>): this {
return this.get('build.host', cb);
}
public getBuildId(cb?: MonkeyCallback<string | null>): this {
return this.get('build.id', cb);
}
public getBuildManufacturer(cb?: MonkeyCallback<string | null>): this {
return this.get('build.manufacturer', cb);
}
public getBuildModel(cb?: MonkeyCallback<string | null>): this {
return this.get('build.model', cb);
}
public getBuildProduct(cb?: MonkeyCallback<string | null>): this {
return this.get('build.product', cb);
}
public getBuildTags(cb?: MonkeyCallback<string[] | null>): this {
return this.getAndParse(
'build.tags',
{ type: 'stringArray', splitter: ',' },
cb
);
}
public getBuildType(cb?: MonkeyCallback<string | null>): this {
return this.get('build.type', cb);
}
public getBuildUser(cb?: MonkeyCallback<string | null>): this {
return this.get('build.user', cb);
}
public getBuildVersionCodename(cb?: MonkeyCallback<string | null>): this {
return this.get('build.version.codename', cb);
}
public getBuildVersionIncremental(
cb?: MonkeyCallback<string | null>
): this {
return this.get('build.version.incremental', cb);
}
public getBuildVersionRelease(cb?: MonkeyCallback<string | null>): this {
return this.get('build.version.release', cb);
}
public getBuildVersionSdk(cb?: MonkeyCallback<number | null>): this {
return this.getAndParse('build.version.sdk', { type: 'number' }, cb);
}
public getClockMillis(cb?: MonkeyCallback<number | null>): this {
return this.getAndParse('clock.millis', { type: 'number' }, cb);
}
public getClockRealtime(cb?: MonkeyCallback<number | null>): this {
return this.getAndParse('clock.realtime', { type: 'number' }, cb);
}
public getClockUptime(cb?: MonkeyCallback<number | null>): this {
return this.getAndParse('clock.uptime', { type: 'number' }, cb);
}
public getDisplayDensity(cb?: MonkeyCallback<number | null>): this {
return this.getAndParse('display.density', { type: 'number' }, cb);
}
public getDisplayHeight(cb?: MonkeyCallback<number | null>): this {
return this.getAndParse('display.height', { type: 'number' }, cb);
}
public getDisplayWidth(cb?: MonkeyCallback<number | null>): this {
return this.getAndParse('display.width', { type: 'number' }, cb);
}
}