• 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 { Permission } from '../../../main/ets/common/model/definition';
18import {
19  Log,
20  verifyAccessToken,
21  titleTrim,
22  getPermissionLabel,
23  getPermissionGroup,
24  getPermissionGroupByName,
25  getGroupIdByPermission,
26  checkPermissionGroup,
27  getFontSizeScale,
28  supportPermission
29} from '../../../main/ets/common/utils/utils';
30import { abilityAccessCtrl, bundleManager } from '@kit.AbilityKit';
31
32export default function utilsTest() {
33  describe('utilsTest', () => {
34    // Defines a test suite. Two parameters are supported: test suite name and test suite function.
35    beforeAll(() => {
36      // Presets an action, which is performed only once before all test cases of the test suite start.
37      // This API supports only one parameter: preset action function.
38    })
39    beforeEach(() => {
40      // Presets an action, which is performed before each unit test case starts.
41      // The number of execution times is the same as the number of test cases defined by **it**.
42      // This API supports only one parameter: preset action function.
43    })
44    afterEach(() => {
45      // Presets a clear action, which is performed after each unit test case ends.
46      // The number of execution times is the same as the number of test cases defined by **it**.
47      // This API supports only one parameter: clear action function.
48    })
49    afterAll(() => {
50      // Presets a clear action, which is performed after all test cases of the test suite end.
51      // This API supports only one parameter: clear action function.
52    })
53
54    it('UtilsTest_000', TestType.FUNCTION, async () => {
55      Log.info(`UtilsTest_000 begin`);
56      let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
57      let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag);
58      let status = await verifyAccessToken(bundleInfo.appInfo.accessTokenId, 'ohos.permission.MICROPHONE');
59      expect(status).assertEqual(abilityAccessCtrl.GrantStatus.PERMISSION_DENIED);
60      let status1 = await verifyAccessToken(bundleInfo.appInfo.accessTokenId, 'ohos.permission.GET_BUNDLE_INFO');
61      expect(status1).assertEqual(abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED);
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_004', TestType.FUNCTION, () => {
72      Log.info(`UtilsTest_004 begin`);
73      let testUri = Permission.MICROPHONE;
74      let label = getPermissionLabel(testUri);
75      expect(JSON.stringify(label)).assertEqual(JSON.stringify($r('sys.string.ohos_lab_microphone')));
76      let testUri1 = '';
77      let label1 = getPermissionLabel(testUri1 as Permission);
78      expect(label1).assertEqual('');
79    })
80
81    it('UtilsTest_005', TestType.FUNCTION, () => {
82      Log.info(`UtilsTest_005 begin`);
83      let testUri = Permission.MICROPHONE;
84      let groupInfo = getPermissionGroup(testUri);
85      expect(groupInfo.permissions).assertContain(testUri);
86      let testUri1 = '';
87      let groupInfo1 = getPermissionGroup(testUri1 as Permission);
88      expect(groupInfo1.label).assertEqual('');
89    })
90
91    it('UtilsTest_006', TestType.FUNCTION, () => {
92      Log.info(`UtilsTest_006 begin`);
93      let testGroup = 'MICROPHONE';
94      let groupInfo = getPermissionGroupByName(testGroup);
95      expect(groupInfo.name).assertEqual(testGroup);
96      let testGroup1 = '';
97      let groupInfo1 = getPermissionGroupByName(testGroup1);
98      expect(groupInfo1.name).assertEqual('LOCATION');
99    })
100
101    it('UtilsTest_007', TestType.FUNCTION, () => {
102      Log.info(`UtilsTest_007 begin`);
103      let testUri = Permission.MICROPHONE;
104      let groupId = getGroupIdByPermission(testUri);
105      expect(groupId).assertEqual(2);
106      let testUri1 = '';
107      let groupId1 = getGroupIdByPermission(testUri1 as Permission);
108      expect(groupId1).assertEqual(-1);
109    })
110
111    it('UtilsTest_010', TestType.FUNCTION, () => {
112      Log.info(`UtilsTest_010 begin`);
113      let testUri = [Permission.MICROPHONE];
114      let groupName = checkPermissionGroup(testUri);
115      expect(groupName).assertEqual('MICROPHONE');
116      let testUri1 = [];
117      let groupName1 = checkPermissionGroup(testUri1);
118      expect(groupName1).assertEqual(null);
119      let testUri2 = ['' as Permission];
120      let groupName2 = checkPermissionGroup(testUri2);
121      expect(groupName2).assertEqual(null);
122    })
123
124    it('UtilsTest_012', TestType.FUNCTION, () => {
125      Log.info(`UtilsTest_012 begin`);
126      let scale = getFontSizeScale();
127      expect(scale).assertEqual(false);
128    })
129
130    it('UtilsTest_013', TestType.FUNCTION, () => {
131      Log.info(`UtilsTest_013 begin`);
132      let support = supportPermission();
133      expect(support).assertContain(Permission.CAMERA);
134    })
135  })
136}