1/**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15export default class Utils {
16  static rect_left;
17  static rect_top;
18  static rect_right;
19  static rect_bottom;
20  static rect_value;
21
22  static sleep(time) {
23    return new Promise((resolve, reject) => {
24      setTimeout(() => {
25        resolve()
26      }, time)
27    }).then(() => {
28      console.info(`sleep ${time} over...`)
29    })
30  }
31
32  static getComponentRect(key) {
33    let strJson = getInspectorByKey(key);
34    let obj = JSON.parse(strJson);
35    console.info("[getInspectorByKey] current component obj is: " + JSON.stringify(obj));
36    let rectInfo = JSON.parse('[' + obj.$rect + ']')
37    console.info("[getInspectorByKey] rectInfo is: " + rectInfo);
38    this.rect_left = JSON.parse('[' + rectInfo[0] + ']')[0]
39    this.rect_top = JSON.parse('[' + rectInfo[0] + ']')[1]
40    this.rect_right = JSON.parse('[' + rectInfo[1] + ']')[0]
41    this.rect_bottom = JSON.parse('[' + rectInfo[1] + ']')[1]
42    return this.rect_value = {
43      "left": this.rect_left, "top": this.rect_top, "right": this.rect_right, "bottom": this.rect_bottom
44    }
45  }
46
47  static async swipe(downX, downY, upX, upY, steps) {
48    console.info('start to swipe')
49    this.drags(downX, downY, upX, upY, steps, false)
50  }
51
52  static async drag(downX, downY, upX, upY, steps) {
53    console.info('start to drag')
54    this.drags(downX, downY, upX, upY, steps, true)
55  }
56
57  static async drags(downX, downY, upX, upY, steps, drag) {
58    var xStep;
59    var yStep;
60    var swipeSteps;
61    var ret;
62    xStep = 0;
63    yStep = 0;
64    ret = false;
65    swipeSteps = steps;
66    if (swipeSteps == 0) {
67      swipeSteps = 1;
68    }
69    xStep = (upX - downX) / swipeSteps;
70    yStep = (upY - downY) / swipeSteps;
71    console.info('move step is: ' + 'xStep: ' + xStep + '    yStep: ' + yStep)
72    var downPoint: TouchObject = {
73      id: 1,
74      x: downX,
75      y: downY,
76      type: TouchType.Down,
77    }
78    console.info('down touch started: ' + JSON.stringify(downPonit))
79    sendTouchEvent(downPoint);
80    console.info('start to move')
81    if (drag) {
82      await this.sleep(500)
83    }
84    for (var i = 1;i <= swipeSteps; i++) {
85      var movePoint: TouchObject = {
86        id: 1,
87        x: downX + (xStep * i),
88        y: downY + (yStep * i),
89        type: TouchType.Move,
90      }
91      console.info('move touch started: ' + JSON.stringify(movePoint))
92      ret = sendTouchEvent(movePoint)
93      if (ret == false) {
94        break;
95      }
96      await this.sleep(5)
97    }
98    console.info('start to up')
99    if (drag) {
100      await this.sleep(100)
101    }
102    var upPoint: TouchObject = {
103      id: 1,
104      x: upX,
105      y: upY,
106      type: TouchType.Up,
107    }
108    console.info('up touch started: ' + JSON.stringify(upPoint))
109    sendTouchEvent(upPoint)
110    await this.sleep(500)
111  }
112}
113
114
115
116
117