1/*
2 * Copyright (c) 2023-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 */
15
16import { abilityDelegatorRegistry, TestRunner } from '@kit.TestKit';
17import { BusinessError } from '@kit.BasicServicesKit';
18import { hilog } from '@kit.PerformanceAnalysisKit';
19import { resourceManager } from '@kit.LocalizationKit';
20import { util } from '@kit.ArkTS';
21import { Hypium } from '@ohos/hypium';
22import testsuite from '../test/List.test';
23
24let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator;
25let abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs;
26let jsonPath: string = 'mock/mock-config.json';
27let domain: number = 0x0000; //日志标识,0x0000作为测试框架的业务标识
28let tag: string = 'testTag'; //日志标识字符串,作为tag标识当前runner类下的测试行为
29
30export default class OpenHarmonyTestRunner implements TestRunner {
31  constructor() {
32  }
33
34  onPrepare() {
35    hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner OnPrepare');
36  }
37
38  async onRun() {
39    hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner onRun run');
40    abilityDelegatorArguments = abilityDelegatorRegistry.getArguments();
41    abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator();
42    let moduleName = abilityDelegatorArguments.parameters['-m'];
43    let context = abilityDelegator.getAppContext().getApplicationContext().createModuleContext(moduleName);
44    let mResourceManager = context.resourceManager;
45    await checkMock(abilityDelegator, mResourceManager);
46    hilog.info(domain, tag, '%{public}s', 'start run testcase!!!');
47    Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite);
48    hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner onRun end');
49  }
50}
51
52async function checkMock(abilityDelegator: abilityDelegatorRegistry.AbilityDelegator, resourceManager: resourceManager.ResourceManager) {
53  let rawFile: Uint8Array;
54  try {
55    rawFile = resourceManager.getRawFileContentSync(jsonPath);
56    hilog.info(domain, tag, 'MockList file exists');
57    let mockStr: string = util.TextDecoder.create("utf-8", { ignoreBOM: true }).decodeWithStream(rawFile);
58    let mockMap: Record<string, string> = getMockList(mockStr);
59    try {
60      abilityDelegator.setMockList(mockMap);
61    } catch (error) {
62      let code = (error as BusinessError).code;
63      let message = (error as BusinessError).message;
64      hilog.error(domain, tag, `abilityDelegator.setMockList failed, error code: ${code}, message: ${message}.`);
65    }
66  } catch (error) {
67    let code = (error as BusinessError).code;
68    let message = (error as BusinessError).message;
69    hilog.error(domain, tag, `ResourceManager:callback getRawFileContent failed, error code: ${code}, message: ${message}.`);
70  }
71}
72
73function getMockList(jsonStr: string) {
74  let jsonObj: Record<string, Object> = JSON.parse(jsonStr);
75  let map: Map<string, object> = new Map<string, object>(Object.entries(jsonObj));
76  let mockList: Record<string, string> = {};
77  map.forEach((value: object, key: string) => {
78    let realValue: string = value['source'].toString();
79    mockList[key] = realValue;
80  });
81  hilog.info(domain, tag, '%{public}s', 'mock-json value:' + JSON.stringify(mockList) ?? '');
82  return mockList;
83}