1/* 2* Copyright (c) 2021 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 bundle from '@ohos.bundle' 17import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' 18 19const BUNDLE_NAME1 = 'com.example.bmsmainabilityfirstscene'; 20const BUNDLE_NAME2 = 'com.example.third2'; 21const BUNDLE_NAME3 = 'com.example.third5'; 22const BUNDLE_NAME5 = 'com.example.system1'; 23const FIRSTMAINABILITY = 'com.example.bmsmainabilityfirstscene.MainAbility'; 24const SECONDMAINABILITY = 'com.example.bmsmainabilitysecondscene.MainAbility'; 25const MODULE_NAME = 'entry'; 26const ICON = '$media:icon'; 27const ICON_ID = 16777218; 28const LABEL = '$string:app_name'; 29const LABEL_ID = 16777216; 30const DEVICE_TYPES = 'default'; 31const FIRST_SCENE_HAP_NAME = 'com.example.bmsmainabilityfirstscene.MyApplication'; 32const SECOND_SCENE_HAP_NAME = 'com.example.bmsmainabilitysecondscene.MyApplication'; 33const THIRD_TWO_HAP_NAME = 'com.example.third2.MyApplication'; 34const THIRD_FIVE_HAP_NAME = 'com.example.third5.BmsThirdBundle5'; 35const SYSTEM_ONE_HAP_NAME = 'com.example.system1.BmsSystemBundle1'; 36 37export default function ActsBmsHapModuleTest() { 38describe('ActsBmsHapModuleTest', function () { 39 40 /* 41 * @tc.number: bms_getHapModuleInfo_0100 42 * @tc.name: get hapModuleInfo from one app by getBundleInfo 43 * @tc.desc: get the module information of the hap with type of entry 44 */ 45 it('bms_getHapModuleInfo_0100', 0, async function (done) { 46 let dataInfo = await bundle.getBundleInfo(BUNDLE_NAME1, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES); 47 bundle.getBundleInfo(BUNDLE_NAME1, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES, callback); 48 async function callback(err, result) { 49 expect(JSON.stringify(result)).assertEqual(JSON.stringify(dataInfo)); 50 expect(result.hapModuleInfos.length).assertEqual(2); 51 if (result.hapModuleInfos.length > 0) { 52 let hapModuleInfo = result.hapModuleInfos[0]; 53 expect(hapModuleInfo.name).assertEqual(FIRST_SCENE_HAP_NAME); 54 expect(hapModuleInfo.moduleName).assertEqual(MODULE_NAME); 55 expect(hapModuleInfo.description).assertEqual(''); 56 expect(hapModuleInfo.descriptionId).assertEqual(0); 57 expect(hapModuleInfo.icon).assertEqual(ICON); 58 expect(hapModuleInfo.label).assertEqual(LABEL); 59 expect(hapModuleInfo.labelId).assertEqual(LABEL_ID); 60 expect(hapModuleInfo.iconId).assertEqual(ICON_ID); 61 expect(hapModuleInfo.backgroundImg).assertEqual(""); 62 expect(hapModuleInfo.supportedModes).assertEqual(0); 63 expect(hapModuleInfo.reqCapabilities.length).assertEqual(0); 64 expect(hapModuleInfo.deviceTypes[0]).assertEqual(DEVICE_TYPES); 65 expect(hapModuleInfo.mainAbilityName).assertEqual(FIRSTMAINABILITY); 66 expect(hapModuleInfo.abilityInfo.length).assertLarger(0); 67 expect(hapModuleInfo.installationFree).assertEqual(false); 68 done(); 69 } 70 } 71 }); 72 73 /* 74 * @tc.number: bms_getHapModuleInfo_0200 75 * @tc.name: get hapModuleInfo from two modules by getBundleInfo 76 * @tc.desc: get the module information of the hap with type of feature 77 */ 78 it('bms_getHapModuleInfo_0200', 0, async function (done) { 79 let dataInfo = await bundle.getBundleInfo(BUNDLE_NAME1, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES); 80 bundle.getBundleInfo(BUNDLE_NAME1, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES, async (err, result) => { 81 expect(JSON.stringify(result)).assertEqual(JSON.stringify(dataInfo)); 82 expect(result.hapModuleInfos.length).assertEqual(2); 83 if (result.hapModuleInfos.length == 2) { 84 let hapModuleInfo = result.hapModuleInfos[0]; 85 let hapModuleInfo1 = result.hapModuleInfos[1]; 86 checkHapMoudleInfos(hapModuleInfo); 87 checkHapMoudleInfos(hapModuleInfo1); 88 expect(hapModuleInfo.name).assertEqual(FIRST_SCENE_HAP_NAME); 89 expect(hapModuleInfo1.name).assertEqual(SECOND_SCENE_HAP_NAME); 90 expect(hapModuleInfo.moduleName).assertEqual(MODULE_NAME); 91 expect(hapModuleInfo.mainAbilityName).assertEqual(FIRSTMAINABILITY); 92 expect(hapModuleInfo1.moduleName).assertEqual('bmsmainabilitysecondscene'); 93 expect(hapModuleInfo1.mainAbilityName).assertEqual(SECONDMAINABILITY); 94 expect(hapModuleInfo.installationFree).assertEqual(false); 95 expect(hapModuleInfo1.installationFree).assertEqual(true); 96 done(); 97 } 98 }); 99 }); 100 101 /* 102 * @tc.number: bms_getHapModuleInfo_0300 103 * @tc.name: get hapModuleInfo one app without mainAbility by getBundleInfo 104 * @tc.desc: get the module information of the hap without mainAbility 105 */ 106 it('bms_getHapModuleInfo_0300', 0, async function (done) { 107 let result = await bundle.getBundleInfo(BUNDLE_NAME2, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES); 108 bundle.getBundleInfo(BUNDLE_NAME2, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES, (err, data) => { 109 expect(JSON.stringify(result)).assertEqual(JSON.stringify(data)); 110 expect(result.hapModuleInfos.length).assertEqual(1); 111 if (result.hapModuleInfos.length > 0) { 112 let hapModuleInfo = result.hapModuleInfos[0]; 113 expect(hapModuleInfo.name).assertEqual(THIRD_TWO_HAP_NAME); 114 expect(hapModuleInfo.moduleName).assertEqual(MODULE_NAME); 115 expect(hapModuleInfo.description).assertEqual(''); 116 expect(hapModuleInfo.descriptionId).assertEqual(0); 117 expect(hapModuleInfo.icon).assertEqual(''); 118 expect(hapModuleInfo.label).assertEqual(''); 119 expect(hapModuleInfo.labelId).assertEqual(0); 120 expect(hapModuleInfo.iconId).assertEqual(0); 121 expect(hapModuleInfo.backgroundImg).assertEqual(""); 122 expect(hapModuleInfo.supportedModes).assertEqual(0); 123 expect(hapModuleInfo.reqCapabilities.length).assertEqual(0); 124 expect(hapModuleInfo.deviceTypes[0]).assertEqual(DEVICE_TYPES); 125 expect(hapModuleInfo.mainAbilityName).assertEqual(""); 126 expect(hapModuleInfo.abilityInfo.length).assertLarger(0); 127 expect(hapModuleInfo.installationFree).assertEqual(false); 128 done(); 129 } 130 }); 131 }); 132 133 /* 134 * @tc.number: bms_getHapModuleInfo_0400 135 * @tc.name: get hapModuleInfo through getBundleInfo, an application that adds mainAbility 136 * @tc.desc: get the module information of the hap with the added field mainAbility 137 */ 138 it('bms_getHapModuleInfo_0400', 0, async function (done) { 139 let dataInfo = await bundle.getBundleInfo(BUNDLE_NAME3, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES); 140 bundle.getBundleInfo(BUNDLE_NAME3, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES, async (err, result) => { 141 expect(JSON.stringify(result)).assertEqual(JSON.stringify(dataInfo)); 142 expect(result.hapModuleInfos.length).assertEqual(1); 143 if (result.hapModuleInfos.length == 1) { 144 let hapModuleInfo = result.hapModuleInfos[0]; 145 expect(hapModuleInfo.name).assertEqual(THIRD_FIVE_HAP_NAME); 146 expect(hapModuleInfo.moduleName).assertEqual(MODULE_NAME); 147 expect(hapModuleInfo.description).assertEqual(''); 148 expect(hapModuleInfo.descriptionId).assertEqual(0); 149 expect(hapModuleInfo.icon).assertEqual(ICON); 150 expect(hapModuleInfo.label).assertEqual(LABEL); 151 expect(hapModuleInfo.labelId).assertEqual(LABEL_ID); 152 expect(hapModuleInfo.iconId).assertEqual(16777219); 153 expect(hapModuleInfo.backgroundImg).assertEqual(""); 154 expect(hapModuleInfo.supportedModes).assertEqual(0); 155 expect(hapModuleInfo.reqCapabilities.length).assertEqual(0); 156 expect(hapModuleInfo.deviceTypes[0]).assertEqual(DEVICE_TYPES); 157 expect(hapModuleInfo.mainAbilityName).assertEqual("com.example.third5.AMainAbility"); 158 expect(hapModuleInfo.abilityInfo.length).assertLarger(0); 159 expect(hapModuleInfo.installationFree).assertEqual(false); 160 done(); 161 } 162 }); 163 }); 164 165 /* 166 * @tc.number: bms_getHapModuleInfo_0600 167 * @tc.name: get hapModuleInfo from the system through getBundleInfo 168 * @tc.desc: get module information of mainAbility system application 169 */ 170 it('bms_getHapModuleInfo_0600', 0, async function (done) { 171 let dataInfo = await bundle.getBundleInfo(BUNDLE_NAME5, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES); 172 bundle.getBundleInfo(BUNDLE_NAME5, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES, callback); 173 function callback(err, data) { 174 expect(JSON.stringify(data)).assertEqual(JSON.stringify(dataInfo)); 175 expect(data.hapModuleInfos.length).assertEqual(1); 176 if (data.hapModuleInfos.length == 1) { 177 let hapModuleInfo = data.hapModuleInfos[0]; 178 expect(hapModuleInfo.name).assertEqual(SYSTEM_ONE_HAP_NAME); 179 expect(hapModuleInfo.moduleName).assertEqual(MODULE_NAME); 180 expect(hapModuleInfo.description).assertEqual(''); 181 expect(hapModuleInfo.descriptionId).assertEqual(0); 182 expect(hapModuleInfo.icon).assertEqual(ICON); 183 expect(hapModuleInfo.label).assertEqual(LABEL); 184 expect(hapModuleInfo.labelId).assertEqual(LABEL_ID); 185 expect(hapModuleInfo.iconId).assertEqual(16777219); 186 expect(hapModuleInfo.backgroundImg).assertEqual(""); 187 expect(hapModuleInfo.supportedModes).assertEqual(0); 188 expect(hapModuleInfo.reqCapabilities.length).assertEqual(0); 189 expect(hapModuleInfo.deviceTypes[0]).assertEqual(DEVICE_TYPES); 190 expect(hapModuleInfo.mainAbilityName).assertEqual("com.example.system1.MainAbility"); 191 expect(hapModuleInfo.abilityInfo.length).assertLarger(0); 192 expect(hapModuleInfo.installationFree).assertEqual(false); 193 } 194 done(); 195 } 196 }); 197 198 function checkHapMoudleInfos(info) { 199 expect(info.description).assertEqual(''); 200 expect(info.descriptionId).assertEqual(0); 201 expect(info.icon).assertEqual(ICON); 202 expect(info.labelId).assertEqual(LABEL_ID); 203 expect(info.iconId).assertEqual(ICON_ID); 204 expect(info.label).assertEqual(LABEL); 205 expect(info.backgroundImg).assertEqual(""); 206 expect(info.supportedModes).assertEqual(0); 207 expect(info.reqCapabilities.length).assertEqual(0); 208 expect(info.deviceTypes[0]).assertEqual(DEVICE_TYPES); 209 expect(info.abilityInfo.length).assertLarger(0); 210 } 211})} 212