• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, TestType } from '@ohos/hypium';
17import { AppInfo } from '../../../main/ets/common/model/typedef';
18import { Permission } from '../../../main/ets/common/model/definition';
19import Constants from '../../../main/ets/common/utils/constant';
20import {
21  Log,
22  verifyAccessToken,
23  titleTrim,
24  addLocalTag,
25  getPermissionLabel,
26  getPermissionGroup,
27  getPermissionGroupByName,
28  getGroupIdByPermission,
29  checkPermissionGroup,
30  getFontSizeScale
31} from '../../../main/ets/common/utils/utils';
32import { abilityAccessCtrl, bundleManager } from '@kit.AbilityKit';
33
34export default function utilsTest() {
35  describe('utilsTest', () => {
36    // Defines a test suite. Two parameters are supported: test suite name and test suite function.
37    beforeAll(() => {
38      // Presets an action, which is performed only once before all test cases of the test suite start.
39      // This API supports only one parameter: preset action function.
40    })
41    beforeEach(() => {
42      // Presets an action, which is performed before each unit test case starts.
43      // The number of execution times is the same as the number of test cases defined by **it**.
44      // This API supports only one parameter: preset action function.
45    })
46    afterEach(() => {
47      // Presets a clear action, which is performed after each unit test case ends.
48      // The number of execution times is the same as the number of test cases defined by **it**.
49      // This API supports only one parameter: clear action function.
50    })
51    afterAll(() => {
52      // Presets a clear action, which is performed after all test cases of the test suite end.
53      // This API supports only one parameter: clear action function.
54    })
55
56    it('UtilsTest_000', TestType.FUNCTION, async () => {
57      Log.info(`UtilsTest_000 begin`);
58      let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
59      let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag);
60      let status = await verifyAccessToken(bundleInfo.appInfo.accessTokenId, 'ohos.permission.MICROPHONE');
61      expect(status).assertEqual(abilityAccessCtrl.GrantStatus.PERMISSION_DENIED);
62    })
63
64    it('UtilsTest_001', TestType.FUNCTION, () => {
65      Log.info(`UtilsTest_001 begin`);
66      let testUri = 'testTitle';
67      let title = titleTrim(testUri);
68      expect(title).assertEqual(testUri);
69    })
70
71    it('UtilsTest_002', TestType.FUNCTION, async () => {
72      Log.info(`UtilsTest_002 begin`);
73      let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
74      let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag);
75      let reqPermissions: Array<Permission> = [];
76      bundleInfo.reqPermissionDetails.forEach(item => {
77        reqPermissions.push(item.name as Permission);
78      });
79      let info = new AppInfo(
80        bundleInfo.name,
81        bundleInfo.targetVersion,
82        bundleInfo.appInfo.accessTokenId,
83        '',
84        bundleInfo.appInfo.iconId,
85        bundleInfo.appInfo.iconResource,
86        '',
87        bundleInfo.appInfo.labelId,
88        bundleInfo.appInfo.labelResource,
89        reqPermissions,
90        [],
91        '',
92        '',
93        ''
94      );
95      addLocalTag(info);
96    })
97
98    it('UtilsTest_004', TestType.FUNCTION, () => {
99      Log.info(`UtilsTest_004 begin`);
100      let testUri = Permission.MICROPHONE;
101      let label = getPermissionLabel(testUri);
102      expect(JSON.stringify(label)).assertEqual(JSON.stringify($r('sys.string.ohos_lab_microphone')));
103    })
104
105    it('UtilsTest_005', TestType.FUNCTION, () => {
106      Log.info(`UtilsTest_005 begin`);
107      let testUri = Permission.MICROPHONE;
108      let groupInfo = getPermissionGroup(testUri);
109      expect(groupInfo.permissions).assertContain(testUri);
110    })
111
112    it('UtilsTest_006', TestType.FUNCTION, () => {
113      Log.info(`UtilsTest_006 begin`);
114      let testGroup = 'MICROPHONE';
115      let groupInfo = getPermissionGroupByName(testGroup);
116      expect(groupInfo.name).assertEqual(testGroup);
117    })
118
119    it('UtilsTest_007', TestType.FUNCTION, () => {
120      Log.info(`UtilsTest_007 begin`);
121      let testUri = Permission.MICROPHONE;
122      let groupId = getGroupIdByPermission(testUri);
123      expect(groupId).assertEqual(2);
124    })
125
126    it('UtilsTest_010', TestType.FUNCTION, () => {
127      Log.info(`UtilsTest_010 begin`);
128      let testUri = [Permission.MICROPHONE];
129      let groupName = checkPermissionGroup(testUri);
130      expect(groupName).assertEqual('MICROPHONE');
131    })
132
133    it('UtilsTest_012', TestType.FUNCTION, () => {
134      Log.info(`UtilsTest_012 begin`);
135      let scale = getFontSizeScale();
136      expect(scale).assertEqual(false);
137    })
138
139    it('UtilsTest_013', TestType.FUNCTION, () => {
140      Log.info(`UtilsTest_013 begin`);
141      Log.warn(`UtilsTest_013 begin`);
142      Log.debug(`UtilsTest_013 begin`);
143      Log.error(`UtilsTest_013 begin`);
144    })
145  })
146}