-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ability.test.ets
452 lines (414 loc) · 18.9 KB
/
Ability.test.ets
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import { describe, it, expect } from '@ohos/hypium';
import abilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
import { Driver, ON } from '@ohos.UiTest'
import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import PTGJson from './PTG';
import router from '@ohos.router';
const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator();
const bundleName = abilityDelegatorRegistry.getArguments().bundleName;
function sleep(time: number) {
return new Promise<void>((resolve: Function) => setTimeout(resolve, time));
}
class ModelTest {
driver: Driver
PTG: Map<string, Array<{
component: { type },
event,
target
}>>
vis: Set<string>
count: number
constructor(driver: Driver, PTG: Map<string, Array<{
component: { type },
event,
target
}>>) {
this.driver = driver;
this.PTG = PTG;
this.vis = new Set<string>();
this.count = 0;
}
async test() {
let curPage = router.getState().path + router.getState().name;
if (this.vis.has(curPage)) {
await this.driver.delayMs(2000);
await this.driver.pressBack();
await this.driver.delayMs(2000);
return;
}
this.vis.add(curPage);
let curLength = router.getLength();
hilog.info(0x0000, 'testTag', '%{public}s', `currentPage: ${curPage}`);
// this.vis.add(curPage);
hilog.info(0x0000, 'testTag', '%{public}s', `pages vis: ${this.vis.size}}`);
// let x = Math.random();
// if (x <= 0.2) {
// await this.componentScrollToBottom();
// } else if (x <= 0.4) {
// await this.componentScrollToTop();
// }
const children = this.PTG.get(curPage);
if (!children) {
return;
}
const doAction = async () => {
// for (let [type, event, childPage] of children) {
for (let obj of children) {
for (let [cur, { component: { type }, event, target }] of Object.entries(obj)) {
let originPage = router.getState().path + router.getState().name;
let components = await this.driver.findComponents(ON.type(type));
if (components) {
if (type === 'ListItem' || type === 'GridItem') {
components = [components[0]];
}
for (let component of components) {
let page = router.getState().path + router.getState().name;
while (page != originPage) {
await this.test();
page = router.getState().path + router.getState().name;
}
if (event === 'onClick') {
await component.click();
this.count++;
hilog.info(0x0000, 'testTag', '%{public}s', `action count: ${this.count}}`);
}
await this.driver.delayMs(2000);
page = router.getState().path + router.getState().name;
if (page === target) {
hilog.info(0x0000, 'testTag', '%{public}s', `currentPage: ${router.getState().path + router.getState()
.name}`);
await this.test();
} else if (page === curPage) {
await this.driver.delayMs(0);
} else {
hilog.info(0x0000, 'testTag', '%{public}s', `currentPage: ${router.getState().path + router.getState()
.name}`);
await this.test();
}
}
}
}
}
}
const randomAction = async () => {
for (let i = 0; i < 10; i++) {
// let x = Math.random();
// if (x <= 0.5) {
// await this.componentScrollToBottom();
// } else {
// await this.componentScrollToTop();
// }
await this.componentClick();
await this.driver.delayMs(2000);
let page = router.getState().path + router.getState().name;
if (!this.vis.has(page) && page !== curPage) {
await this.test();
}
}
}
let tabBars = await this.driver.findComponents(ON.type('TabBar'));
if (tabBars) {
for (let tabBar of tabBars) {
let tabBarNum = 4;
let bounds = await tabBar.getBounds();
let left = bounds.left, right = bounds.right, top = bounds.top, bottom = bounds.bottom;
let d = Math.round((right - left) / tabBarNum);
for (let i = 1; i <= tabBarNum; i++) {
// let cs = await this.driver.findComponents(ON.enabled(true));
// while (true) {
// await this.driver.click(i * d - 20, top);
// let ncs = await this.driver.findComponents(ON.enabled(true));
// if (cs.length !== ncs.length) {
// break;
// }
// }
// for (let j = 0; j < 5; j++) {
await this.driver.click(i * d - 20, top);
// await this.driver.delayMs(1000);
// }
// 594, 2790
await this.driver.delayMs(2000);
await doAction();
if (i == 4) {
// await randomAction();
}
}
}
} else {
await doAction();
// await randomAction();
}
await this.driver.delayMs(2000);
await this.driver.pressBack();
await this.driver.delayMs(2000);
}
async componentClick() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (enabledComponents && Array.isArray(enabledComponents) && enabledComponents.length > 0) {
while (true) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let bound = await component.getBounds();
let type = await component.getType();
if (type !== 'Image') {
hilog.info(0x0000, 'testTag', '%{public}s', `action count: ${this.count}}`);
await component.click();
this.count++;
hilog.info(0x0000, 'testTag', '%{public}s', `action count: ${this.count}}`);
hilog.info(0x0000, 'testTag', '%{public}s', `click ${bound.left}, ${bound.top}, ${bound.right}, ${bound.bottom}`);
break;
}
}
}
}
async componentScrollToTop() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (enabledComponents && Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let speed = Math.floor(Math.random() * (15000 - 200) + 200);
await component.scrollToTop(speed);
hilog.info(0x0000, 'testTag', '%{public}s', `scrollToTop speed: ${speed}`);
}
}
async componentScrollToBottom() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (enabledComponents && Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let speed = Math.floor(Math.random() * (15000 - 200) + 200);
await component.scrollToBottom(speed);
hilog.info(0x0000, 'testTag', '%{public}s', `scrollToBottom speed: ${speed}`);
}
}
}
class RandomTest {
driver: Driver
leftBound: number
rightBound: number
topBound: number
bottomBound: number
// randomActions = [this.componentClick.bind(this), this.componentDoubleClick.bind(this), this.componentLongClick.bind(this), this.componentScrollToTop.bind(this), this.componentScrollToBottom.bind(this), this.componentDragTo.bind(this), this.componentPinchIn.bind(this), this.componentPinchOut.bind(this), this.componentInputText.bind(this), this.componentClearText.bind(this), this.driverClick.bind(this), this.driverDoubleClick.bind(this), this.driverLongClick.bind(this), this.driverSwipe.bind(this), this.driverDrag.bind(this), this.driverFling.bind(this)];
randomActions = [this.componentClick.bind(this), this.driverClick.bind(this)];
constructor(driver: Driver, leftBound: number, rightBound: number, topBound: number, bottomBound: number) {
this.driver = driver;
this.leftBound = leftBound;
this.rightBound = rightBound;
this.topBound = topBound;
this.bottomBound = bottomBound;
}
async test() {
let vis = new Set();
let count = 0;
while (true) {
let curPage = router.getState().path + router.getState().name;
vis.add(curPage);
await this.action();
count++;
await this.driver.delayMs(2000);
hilog.info(0x0000, 'testTag', '%{public}s', `pages vis: ${vis.size}}`);
hilog.info(0x0000, 'testTag', '%{public}s', `count: ${count}}`);
}
}
async action() {
let randomAction: Function = this.randomActions[Math.floor(Math.random() * this.randomActions.length)];
hilog.info(0x0000, 'testTag', '%{public}s', `randomAction: ${randomAction.name}`);
await randomAction();
}
async componentClick() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let bound = await component.getBounds();
await component.click();
hilog.info(0x0000, 'testTag', '%{public}s', `click ${bound.left}, ${bound.top}, ${bound.right}, ${bound.bottom}`);
}
}
async componentDoubleClick() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let bound = await component.getBounds();
await component.doubleClick();
hilog.info(0x0000, 'testTag', '%{public}s', `doubleClick ${bound.left}, ${bound.top}, ${bound.right}, ${bound.bottom}`);
}
}
async componentLongClick() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let bound = await component.getBounds();
await component.longClick();
hilog.info(0x0000, 'testTag', '%{public}s', `longClick ${bound.left}, ${bound.top}, ${bound.right}, ${bound.bottom}`);
}
}
async componentScrollToTop() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let speed = Math.floor(Math.random() * (15000 - 200) + 200);
await component.scrollToTop(speed);
hilog.info(0x0000, 'testTag', '%{public}s', `scrollToTop speed: ${speed}`);
}
}
async componentScrollToBottom() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let speed = Math.floor(Math.random() * (15000 - 200) + 200);
await component.scrollToBottom(speed);
hilog.info(0x0000, 'testTag', '%{public}s', `scrollToBottom speed: ${speed}`);
}
}
async componentDragTo() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (Array.isArray(enabledComponents) && enabledComponents.length > 1) {
let i1 = Math.floor(Math.random() * enabledComponents.length);
let i2 = i1;
while (i1 == i2) {
i2 = Math.floor(Math.random() * enabledComponents.length);
}
let c1 = enabledComponents[i1];
let c2 = enabledComponents[i2];
if (Math.random() < 0.5) {
await c1.dragTo(c2);
} else {
await c2.dragTo(c1);
}
hilog.info(0x0000, 'testTag', '%{public}s', 'dragTo');
}
}
async componentPinchIn() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let scale = Math.random() * 10;
await component.pinchIn(scale);
hilog.info(0x0000, 'testTag', '%{public}s', `pinchIn: ${scale}`);
}
}
async componentPinchOut() {
let enabledComponents = await this.driver.findComponents(ON.enabled(true));
if (Array.isArray(enabledComponents) && enabledComponents.length > 0) {
let component = enabledComponents[Math.floor(Math.random() * enabledComponents.length)];
let scale = Math.random() * 10;
await component.pinchOut(scale);
hilog.info(0x0000, 'testTag', '%{public}s', `pinchOut: ${scale}`);
}
}
async componentInputText() {
function getRandomChineseWord() {
let _rsl = "";
let _randomUniCode = Math.floor(Math.random() * (40870 - 19968) + 19968).toString(16);
eval("_rsl=" + '"\\u' + _randomUniCode + '"');
return _rsl;
}
let inputTextComponents = await this.driver.findComponents(ON.type('TextInput'));
if (Array.isArray(inputTextComponents) && inputTextComponents.length > 0) {
let component = inputTextComponents[Math.floor(Math.random() * inputTextComponents.length)];
let inputText;
if (Math.random() < 0.5) {
inputText = Math.random().toString(36).slice(-8);
} else {
inputText = getRandomChineseWord();
}
await component.inputText(inputText);
hilog.info(0x0000, 'testTag', '%{public}s', `inputText: ${inputText}`);
}
}
async componentClearText() {
let inputTextComponents = await this.driver.findComponents(ON.type('TextInput'));
if (Array.isArray(inputTextComponents) && inputTextComponents.length > 0) {
let component = inputTextComponents[Math.floor(Math.random() * inputTextComponents.length)];
let text = await component.getText();
await component.clearText();
hilog.info(0x0000, 'testTag', '%{public}s', `clearText: ${text}`);
}
}
async driverClick() {
let x = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let y = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
await this.driver.click(x, y);
hilog.info(0x0000, 'testTag', '%{public}s', `driverClick: ${x}, ${y}`);
}
async driverDoubleClick() {
let x = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let y = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
await this.driver.doubleClick(x, y);
hilog.info(0x0000, 'testTag', '%{public}s', `driverDoubleClick: ${x}, ${y}`);
}
async driverLongClick() {
let x = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let y = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
await this.driver.longClick(x, y);
hilog.info(0x0000, 'testTag', '%{public}s', `driverLongClick: ${x}, ${y}`);
}
async driverSwipe() {
let x1 = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let x2 = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let y1 = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
let y2 = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
let speed = Math.floor(Math.random() * (15000 - 200) + 200);
await this.driver.swipe(x1, y1, x2, y2, speed);
hilog.info(0x0000, 'testTag', '%{public}s', `driverSwipe: (${x1}, ${y1} -> ${x2}, ${y2})`);
}
async driverDrag() {
let x1 = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let x2 = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let y1 = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
let y2 = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
let speed = Math.floor(Math.random() * (15000 - 200) + 200);
await this.driver.drag(x1, y1, x2, y2, speed);
hilog.info(0x0000, 'testTag', '%{public}s', `driverDrag: (${x1}, ${y1} -> ${x2}, ${y2})`);
}
async driverFling() {
let x1 = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let x2 = Math.floor(Math.random() * (this.rightBound - this.leftBound) + this.leftBound);
let y1 = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
let y2 = Math.floor(Math.random() * (this.bottomBound - this.topBound) + this.topBound);
let stepLen = Math.min(Math.abs(x1 - x2), Math.abs(y1 - y2));
let speed = Math.floor(Math.random() * (15000 - 200) + 200);
await this.driver.fling({ x: x1, y: y1 }, { x: x2, y: y2 }, stepLen, speed);
hilog.info(0x0000, 'testTag', '%{public}s', `driverDrag: (${x1}, ${y1} -> ${x2}, ${y2})`);
}
}
export default function abilityTest() {
describe('ActsAbilityTest', () => {
it('testUiExample', 0, async (done: Function) => {
console.info("uitest: TestUiExample begin");
// start tested ability
await delegator.executeShellCommand(`aa start -d 0 -a EntryAbility -b ${bundleName}`)
.then((result: abilityDelegatorRegistry.ShellCmdResult) => {
console.info('Uitest, start ability finished:' + result)
})
.catch((err) => {
console.info('Uitest, start ability failed: ' + err)
})
await sleep(3000);
// Check the top display ability.
await delegator.getCurrentTopAbility().then((Ability: UIAbility) => {
console.info("get top ability");
expect(Ability.context.abilityInfo.name).assertEqual('EntryAbility');
})
let ptg = JSON.parse(PTGJson);
const project = 'HarmoneyOpenEye';
const prefix = `D:/project/CS490/${project}/entry/build/default/cache/default/default@CompileArkTS/esmodule/debug/entry/src/main/ets/`;
const PTG = new Map();
for (let [pageName, obj] of Object.entries(ptg)) {
PTG.set(pageName, []);
// for (const {component:{type},event,targetPage}) {
PTG.get(pageName).push(obj);
// }
}
// UI test code
// Initialize the driver.
let driver = Driver.create();
await driver.delayMs(1000);
const root = await driver.findComponent(ON.type('root'));
const bound = await root.getBounds();
const randomTest: RandomTest = new RandomTest(driver, bound.left, bound.right, bound.top, bound.bottom);
const modelTest: ModelTest = new ModelTest(driver, PTG);
// await randomTest.test();
await modelTest.test();
done();
})
})
}