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 */
15
16import { abilityDelegatorRegistry, TestRunner } from '@kit.TestKit';
17import { UIAbility, Want } from '@kit.AbilityKit';
18import { BusinessError } from '@kit.BasicServicesKit';
19import { hilog } from '@kit.PerformanceAnalysisKit';
20import { resourceManager } from '@kit.LocalizationKit';
21import { util } from '@kit.ArkTS';
22
23let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator;
24let abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs;
25let jsonPath: string = 'mock/mock-config.json';
26let tag: string = 'testTag';
27
28async function onAbilityCreateCallback(data: UIAbility) {
29  hilog.info(0x0000, 'testTag', 'onAbilityCreateCallback, data: ${}', JSON.stringify(data));
30}
31
32async function addAbilityMonitorCallback(err: BusinessError) {
33  hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? '');
34}
35
36export default class OpenHarmonyTestRunner implements TestRunner {
37  constructor() {
38  }
39
40  onPrepare() {
41    hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare');
42  }
43
44  async onRun() {
45    let tag = 'testTag';
46    hilog.info(0x0000, tag, '%{public}s', 'OpenHarmonyTestRunner onRun run');
47    abilityDelegatorArguments = abilityDelegatorRegistry.getArguments()
48    abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator()
49    let moduleName = abilityDelegatorArguments.parameters['-m'];
50    let context = abilityDelegator.getAppContext().getApplicationContext().createModuleContext(moduleName);
51    let mResourceManager = context.resourceManager;
52    await checkMock(abilityDelegator, mResourceManager);
53    const bundleName = abilityDelegatorArguments.bundleName;
54    const testAbilityName: string = 'TestAbility';
55    let lMonitor: abilityDelegatorRegistry.AbilityMonitor = {
56      abilityName: testAbilityName,
57      onAbilityCreate: onAbilityCreateCallback,
58      moduleName: moduleName
59    };
60    abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
61    const want: Want = {
62      bundleName: bundleName,
63      abilityName: testAbilityName,
64      moduleName: moduleName
65    };
66    abilityDelegator.startAbility(want, (err: BusinessError, data: void) => {
67      hilog.info(0x0000, tag, 'startAbility : err : %{public}s', JSON.stringify(err) ?? '');
68      hilog.info(0x0000, tag, 'startAbility : data : %{public}s', JSON.stringify(data) ?? '');
69    })
70    hilog.info(0x0000, tag, '%{public}s', 'OpenHarmonyTestRunner onRun end');
71  }
72}
73
74async function checkMock(abilityDelegator: abilityDelegatorRegistry.AbilityDelegator, resourceManager: resourceManager.ResourceManager) {
75  let rawFile: Uint8Array;
76  try {
77    rawFile = resourceManager.getRawFileContentSync(jsonPath);
78    hilog.info(0x0000, tag, 'MockList file exists');
79    let mockStr: string = util.TextDecoder.create("utf-8", { ignoreBOM: true }).decodeWithStream(rawFile);
80    let mockMap: Record<string, string> = getMockList(mockStr);
81    try {
82      abilityDelegator.setMockList(mockMap)
83    } catch (error) {
84      let code = (error as BusinessError).code;
85      let message = (error as BusinessError).message;
86      hilog.error(0x0000, tag, `abilityDelegator.setMockList failed, error code: ${code}, message: ${message}.`);
87    }
88  } catch (error) {
89    let code = (error as BusinessError).code;
90    let message = (error as BusinessError).message;
91    hilog.error(0x0000, tag, `ResourceManager:callback getRawFileContent failed, error code: ${code}, message: ${message}.`);
92  }
93}
94
95function getMockList(jsonStr: string) {
96  let jsonObj: Record<string, Object> = JSON.parse(jsonStr);
97  let map: Map<string, object> = new Map<string, object>(Object.entries(jsonObj));
98  let mockList: Record<string, string> = {};
99  map.forEach((value: object, key: string) => {
100    let realValue: string = value['source'].toString();
101    mockList[key] = realValue;
102  });
103  hilog.info(0x0000, tag, '%{public}s', 'mock-json value:' + JSON.stringify(mockList) ?? '');
104  return mockList;
105}