1886da342Sopenharmony_ci/*
2886da342Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3886da342Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4886da342Sopenharmony_ci * you may not use this file except in compliance with the License.
5886da342Sopenharmony_ci * You may obtain a copy of the License at
6886da342Sopenharmony_ci *
7886da342Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8886da342Sopenharmony_ci *
9886da342Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10886da342Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11886da342Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12886da342Sopenharmony_ci * See the License for the specific language governing permissions and
13886da342Sopenharmony_ci * limitations under the License.
14886da342Sopenharmony_ci */
15886da342Sopenharmony_ci
16886da342Sopenharmony_ciimport {TAG} from '../../Constant';
17886da342Sopenharmony_ciimport Core from '../../core.js';
18886da342Sopenharmony_ci
19886da342Sopenharmony_ciexport default class SysTestKit {
20886da342Sopenharmony_ci
21886da342Sopenharmony_ci  static delegator = null;
22886da342Sopenharmony_ci  static systemTime = null;
23886da342Sopenharmony_ci  static workerPort = null;
24886da342Sopenharmony_ci
25886da342Sopenharmony_ci  constructor() {
26886da342Sopenharmony_ci    this.id = 'sysTestKit';
27886da342Sopenharmony_ci    this.index = 0;
28886da342Sopenharmony_ci  }
29886da342Sopenharmony_ci
30886da342Sopenharmony_ci  static getDescribeName() {
31886da342Sopenharmony_ci    return Core.getInstance().getDefaultService('suite').getCurrentRunningSuite().description;
32886da342Sopenharmony_ci  }
33886da342Sopenharmony_ci
34886da342Sopenharmony_ci  static getItName() {
35886da342Sopenharmony_ci    return Core.getInstance().getDefaultService('spec').getCurrentRunningSpec().description;
36886da342Sopenharmony_ci  }
37886da342Sopenharmony_ci
38886da342Sopenharmony_ci  static getItAttribute() {
39886da342Sopenharmony_ci    return Core.getInstance().getDefaultService('spec').getCurrentRunningSpec().fi;
40886da342Sopenharmony_ci  }
41886da342Sopenharmony_ci
42886da342Sopenharmony_ci  static actionStart(tag) {
43886da342Sopenharmony_ci    console.info(`${TAG}${JSON.stringify(tag)}`);
44886da342Sopenharmony_ci    var message = '\n' + 'OHOS_REPORT_ACTIONSTART: ' + JSON.stringify(tag) + '\n';
45886da342Sopenharmony_ci    SysTestKit.print(message);
46886da342Sopenharmony_ci    console.info(`${TAG}${JSON.stringify(tag)} actionStart print success`);
47886da342Sopenharmony_ci  }
48886da342Sopenharmony_ci
49886da342Sopenharmony_ci  static actionEnd(tag) {
50886da342Sopenharmony_ci    console.info(`${TAG}${JSON.stringify(tag)}`);
51886da342Sopenharmony_ci    var message = '\n' + 'OHOS_REPORT_ACTIONEND: ' + JSON.stringify(tag) + '\n';
52886da342Sopenharmony_ci    SysTestKit.print(message);
53886da342Sopenharmony_ci    console.info(`${TAG}${JSON.stringify(tag)}  actionEnd print success`);
54886da342Sopenharmony_ci  }
55886da342Sopenharmony_ci
56886da342Sopenharmony_ci  static async existKeyword(keyword, timeout) {
57886da342Sopenharmony_ci    let reg = new RegExp(/^[a-zA-Z0-9]{1,}$/);
58886da342Sopenharmony_ci    if (!reg.test(keyword)) {
59886da342Sopenharmony_ci      throw new Error('keyword must contain more than one string, and only letters and numbers are supported.');
60886da342Sopenharmony_ci    }
61886da342Sopenharmony_ci    timeout = timeout || 4;
62886da342Sopenharmony_ci
63886da342Sopenharmony_ci    let searchResult = false;
64886da342Sopenharmony_ci    let cmd = 'hilog -x | grep -i \'' + keyword + '\' | wc -l';
65886da342Sopenharmony_ci    await executePromise(cmd, timeout).then((data) => {
66886da342Sopenharmony_ci      searchResult = data;
67886da342Sopenharmony_ci    });
68886da342Sopenharmony_ci    return searchResult;
69886da342Sopenharmony_ci  }
70886da342Sopenharmony_ci  static async print(message) {
71886da342Sopenharmony_ci    if ('printSync' in SysTestKit.delegator) {
72886da342Sopenharmony_ci      console.info(`${TAG}printSync called ...`);
73886da342Sopenharmony_ci      SysTestKit.delegator.printSync(message);
74886da342Sopenharmony_ci    } else {
75886da342Sopenharmony_ci      await SysTestKit.delegator.print(message);
76886da342Sopenharmony_ci    }
77886da342Sopenharmony_ci  }
78886da342Sopenharmony_ci
79886da342Sopenharmony_ci  static async getRealTime() {
80886da342Sopenharmony_ci    let currentTime = new Date().getTime();
81886da342Sopenharmony_ci    if (SysTestKit.systemTime !== null && SysTestKit.systemTime !== undefined) {
82886da342Sopenharmony_ci      await SysTestKit.systemTime.getRealTime().then((time) => {
83886da342Sopenharmony_ci        console.info(`${TAG}systemTime.getRealTime success data: ${JSON.stringify(time)}`);
84886da342Sopenharmony_ci        currentTime = time;
85886da342Sopenharmony_ci      }).catch((error) => {
86886da342Sopenharmony_ci        console.error(`${TAG}failed to systemTime.getRealTime because ${JSON.stringify(error)}`);
87886da342Sopenharmony_ci      });
88886da342Sopenharmony_ci    }
89886da342Sopenharmony_ci    return currentTime;
90886da342Sopenharmony_ci  }
91886da342Sopenharmony_ci}
92886da342Sopenharmony_ci
93886da342Sopenharmony_cifunction executePromise(cmd, timeout) {
94886da342Sopenharmony_ci  return new Promise((resolve, reject) => {
95886da342Sopenharmony_ci    SysTestKit.delegator.executeShellCommand(cmd, timeout,
96886da342Sopenharmony_ci      (error, data) => {
97886da342Sopenharmony_ci        console.info(`${TAG}existKeyword CallBack: err : ${JSON.stringify(error)}`);
98886da342Sopenharmony_ci        console.info(`${TAG}existKeyword CallBack: data : ${JSON.stringify(data)}`);
99886da342Sopenharmony_ci        resolve(parseInt(data.stdResult) > 3 ? true : false);
100886da342Sopenharmony_ci      });
101886da342Sopenharmony_ci  });
102886da342Sopenharmony_ci}