1886da342Sopenharmony_ci/*
2886da342Sopenharmony_ci * Copyright (c) 2023-2024 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 { abilityDelegatorRegistry, TestRunner } from '@kit.TestKit';
17886da342Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
18886da342Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
19886da342Sopenharmony_ciimport { resourceManager } from '@kit.LocalizationKit';
20886da342Sopenharmony_ciimport { util } from '@kit.ArkTS';
21886da342Sopenharmony_ciimport { Hypium } from '@ohos/hypium';
22886da342Sopenharmony_ciimport testsuite from '../test/List.test';
23886da342Sopenharmony_ci
24886da342Sopenharmony_cilet abilityDelegator: abilityDelegatorRegistry.AbilityDelegator;
25886da342Sopenharmony_cilet abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs;
26886da342Sopenharmony_cilet jsonPath: string = 'mock/mock-config.json';
27886da342Sopenharmony_cilet domain: number = 0x0000; //日志标识,0x0000作为测试框架的业务标识
28886da342Sopenharmony_cilet tag: string = 'testTag'; //日志标识字符串,作为tag标识当前runner类下的测试行为
29886da342Sopenharmony_ci
30886da342Sopenharmony_ciexport default class OpenHarmonyTestRunner implements TestRunner {
31886da342Sopenharmony_ci  constructor() {
32886da342Sopenharmony_ci  }
33886da342Sopenharmony_ci
34886da342Sopenharmony_ci  onPrepare() {
35886da342Sopenharmony_ci    hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner OnPrepare');
36886da342Sopenharmony_ci  }
37886da342Sopenharmony_ci
38886da342Sopenharmony_ci  async onRun() {
39886da342Sopenharmony_ci    hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner onRun run');
40886da342Sopenharmony_ci    abilityDelegatorArguments = abilityDelegatorRegistry.getArguments();
41886da342Sopenharmony_ci    abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator();
42886da342Sopenharmony_ci    let moduleName = abilityDelegatorArguments.parameters['-m'];
43886da342Sopenharmony_ci    let context = abilityDelegator.getAppContext().getApplicationContext().createModuleContext(moduleName);
44886da342Sopenharmony_ci    let mResourceManager = context.resourceManager;
45886da342Sopenharmony_ci    await checkMock(abilityDelegator, mResourceManager);
46886da342Sopenharmony_ci    hilog.info(domain, tag, '%{public}s', 'start run testcase!!!');
47886da342Sopenharmony_ci    Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite);
48886da342Sopenharmony_ci    hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner onRun end');
49886da342Sopenharmony_ci  }
50886da342Sopenharmony_ci}
51886da342Sopenharmony_ci
52886da342Sopenharmony_ciasync function checkMock(abilityDelegator: abilityDelegatorRegistry.AbilityDelegator, resourceManager: resourceManager.ResourceManager) {
53886da342Sopenharmony_ci  let rawFile: Uint8Array;
54886da342Sopenharmony_ci  try {
55886da342Sopenharmony_ci    rawFile = resourceManager.getRawFileContentSync(jsonPath);
56886da342Sopenharmony_ci    hilog.info(domain, tag, 'MockList file exists');
57886da342Sopenharmony_ci    let mockStr: string = util.TextDecoder.create("utf-8", { ignoreBOM: true }).decodeWithStream(rawFile);
58886da342Sopenharmony_ci    let mockMap: Record<string, string> = getMockList(mockStr);
59886da342Sopenharmony_ci    try {
60886da342Sopenharmony_ci      abilityDelegator.setMockList(mockMap);
61886da342Sopenharmony_ci    } catch (error) {
62886da342Sopenharmony_ci      let code = (error as BusinessError).code;
63886da342Sopenharmony_ci      let message = (error as BusinessError).message;
64886da342Sopenharmony_ci      hilog.error(domain, tag, `abilityDelegator.setMockList failed, error code: ${code}, message: ${message}.`);
65886da342Sopenharmony_ci    }
66886da342Sopenharmony_ci  } catch (error) {
67886da342Sopenharmony_ci    let code = (error as BusinessError).code;
68886da342Sopenharmony_ci    let message = (error as BusinessError).message;
69886da342Sopenharmony_ci    hilog.error(domain, tag, `ResourceManager:callback getRawFileContent failed, error code: ${code}, message: ${message}.`);
70886da342Sopenharmony_ci  }
71886da342Sopenharmony_ci}
72886da342Sopenharmony_ci
73886da342Sopenharmony_cifunction getMockList(jsonStr: string) {
74886da342Sopenharmony_ci  let jsonObj: Record<string, Object> = JSON.parse(jsonStr);
75886da342Sopenharmony_ci  let map: Map<string, object> = new Map<string, object>(Object.entries(jsonObj));
76886da342Sopenharmony_ci  let mockList: Record<string, string> = {};
77886da342Sopenharmony_ci  map.forEach((value: object, key: string) => {
78886da342Sopenharmony_ci    let realValue: string = value['source'].toString();
79886da342Sopenharmony_ci    mockList[key] = realValue;
80886da342Sopenharmony_ci  });
81886da342Sopenharmony_ci  hilog.info(domain, tag, '%{public}s', 'mock-json value:' + JSON.stringify(mockList) ?? '');
82886da342Sopenharmony_ci  return mockList;
83886da342Sopenharmony_ci}