1/*
2 * Copyright (c) 2022-2023 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 */
15
16import {TAG} from '../../Constant';
17import Core from '../../core.js';
18
19export default class SysTestKit {
20
21  static delegator = null;
22  static systemTime = null;
23  static workerPort = null;
24
25  constructor() {
26    this.id = 'sysTestKit';
27    this.index = 0;
28  }
29
30  static getDescribeName() {
31    return Core.getInstance().getDefaultService('suite').getCurrentRunningSuite().description;
32  }
33
34  static getItName() {
35    return Core.getInstance().getDefaultService('spec').getCurrentRunningSpec().description;
36  }
37
38  static getItAttribute() {
39    return Core.getInstance().getDefaultService('spec').getCurrentRunningSpec().fi;
40  }
41
42  static actionStart(tag) {
43    console.info(`${TAG}${JSON.stringify(tag)}`);
44    var message = '\n' + 'OHOS_REPORT_ACTIONSTART: ' + JSON.stringify(tag) + '\n';
45    SysTestKit.print(message);
46    console.info(`${TAG}${JSON.stringify(tag)} actionStart print success`);
47  }
48
49  static actionEnd(tag) {
50    console.info(`${TAG}${JSON.stringify(tag)}`);
51    var message = '\n' + 'OHOS_REPORT_ACTIONEND: ' + JSON.stringify(tag) + '\n';
52    SysTestKit.print(message);
53    console.info(`${TAG}${JSON.stringify(tag)}  actionEnd print success`);
54  }
55
56  static async existKeyword(keyword, timeout) {
57    let reg = new RegExp(/^[a-zA-Z0-9]{1,}$/);
58    if (!reg.test(keyword)) {
59      throw new Error('keyword must contain more than one string, and only letters and numbers are supported.');
60    }
61    timeout = timeout || 4;
62
63    let searchResult = false;
64    let cmd = 'hilog -x | grep -i \'' + keyword + '\' | wc -l';
65    await executePromise(cmd, timeout).then((data) => {
66      searchResult = data;
67    });
68    return searchResult;
69  }
70  static async print(message) {
71    if ('printSync' in SysTestKit.delegator) {
72      console.info(`${TAG}printSync called ...`);
73      SysTestKit.delegator.printSync(message);
74    } else {
75      await SysTestKit.delegator.print(message);
76    }
77  }
78
79  static async getRealTime() {
80    let currentTime = new Date().getTime();
81    if (SysTestKit.systemTime !== null && SysTestKit.systemTime !== undefined) {
82      await SysTestKit.systemTime.getRealTime().then((time) => {
83        console.info(`${TAG}systemTime.getRealTime success data: ${JSON.stringify(time)}`);
84        currentTime = time;
85      }).catch((error) => {
86        console.error(`${TAG}failed to systemTime.getRealTime because ${JSON.stringify(error)}`);
87      });
88    }
89    return currentTime;
90  }
91}
92
93function executePromise(cmd, timeout) {
94  return new Promise((resolve, reject) => {
95    SysTestKit.delegator.executeShellCommand(cmd, timeout,
96      (error, data) => {
97        console.info(`${TAG}existKeyword CallBack: err : ${JSON.stringify(error)}`);
98        console.info(`${TAG}existKeyword CallBack: data : ${JSON.stringify(data)}`);
99        resolve(parseInt(data.stdResult) > 3 ? true : false);
100      });
101  });
102}