ActsBmsHapModuleTest.test.js 14.5 KB
Newer Older
Z
zhaoyuan17 已提交
1
/*
Z
zhaoyuan17 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Z
zhaoyuan17 已提交
15 16

import bundle from '@ohos.bundle'
Z
zhaoyuan17 已提交
17 18
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'

Z
zhaoyuan17 已提交
19
const TIMEOUT = 2000;
Z
zhaoyuan17 已提交
20 21

describe('ActsBmsHapModuleTest', function () {
Z
zhaoyuan17 已提交
22

Z
zhaoyuan17 已提交
23 24 25 26 27 28 29 30 31
    /*
     * @tc.number: bms_getHapModuleInfo_0100
     * @tc.name: get hapModuleInfo from one app by getBundleInfo
     * @tc.desc: get the module information of the hap with type of entry
     */
    it('bms_getHapModuleInfo_0100', 0, async function (done) {
        console.debug('===========begin bms_getHapModuleInfo_0100===========')
        await install(['/data/test/bmsMainAbilityFirstScene.hap']);
        let bundleName = 'com.example.bmsmainabilityfirstscene';
Z
zhaoyuan17 已提交
32
        let ret = false
Z
zhaoyuan17 已提交
33
        bundle.getBundleInfo(bundleName, 1, callback);
Z
zhaoyuan17 已提交
34
        async function callback(err, data) {
Z
zhaoyuan17 已提交
35
            console.debug('=======get bundle========' + JSON.stringify(data));
Z
zhaoyuan17 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            expect(data.hapModuleInfo.length).assertEqual(1);
            if (data.hapModuleInfo.length > 0) {
                let hapModuleInfo = data.hapModuleInfo[0];
                console.debug('=======get hapModule========' + JSON.stringify(hapModuleInfo))
                console.debug('=======get hapModule mainAbilityName========' + hapModuleInfo.mainAbilityName)
                expect(hapModuleInfo.moduleName).assertEqual('entry');
                expect(hapModuleInfo.mainAbilityName).assertEqual('com.example.bmsmainabilityfirstscene.MainAbility');
                expect(hapModuleInfo.name).assertEqual('com.example.bmsmainabilityfirstscene');
                expect(hapModuleInfo.description).assertEqual('');
                expect(hapModuleInfo.descriptionId).assertEqual(0);
                expect(hapModuleInfo.icon).assertEqual('');
                expect(hapModuleInfo.label).assertEqual('$string:app_name');
                expect(hapModuleInfo.labelId).assertEqual(0);
                expect(hapModuleInfo.iconId).assertEqual(0);
                expect(hapModuleInfo.backgroundImg).assertEqual('');
                expect(hapModuleInfo.supportedModes).assertEqual(0);
                console.info('===============hapModuleInfo.reqCapabilities==========' + JSON.stringify(hapModuleInfo.reqCapabilities))
                expect(typeof hapModuleInfo.reqCapabilities).assertEqual('object');
                expect(hapModuleInfo.deviceTypes).assertEqual('phone');
                console.info('===============hapModuleInfo.abilityInfo==========' + JSON.stringify(hapModuleInfo.abilityInfo))
                expect(typeof hapModuleInfo.abilityInfo).assertEqual('object');
                expect(hapModuleInfo.moduleName).assertEqual('entry');
                expect(hapModuleInfo.mainAbilityName).assertEqual('com.example.bmsmainabilityfirstscene.MainAbility');
                expect(hapModuleInfo.installationFree).assertEqual(false);
                for (let i = 0, len = hapModuleInfo.reqCapabilities.length; i < len; i++) {
                    console.debug('=======get reqCapabilities========' + JSON.stringify(hapModuleInfo.reqCapabilities[i]));
                    expect(hapModuleInfo.reqCapabilities[i]).assertEqual('');
                }
                for (let j = 0, len = hapModuleInfo.abilityInfo.length; j < len; j++) {
                    console.debug('=======get abilityInfo========' + JSON.stringify(hapModuleInfo.abilityInfo[j]))
                    expect(hapModuleInfo.abilityInfo[j].name).assertEqual('com.example.bmsmainabilityfirstscene.MainAbility');
                }
                ret = true;
            }
            await uninstall(bundleName);
Z
zhaoyuan17 已提交
71 72
            done();
        }
Z
zhaoyuan17 已提交
73 74 75
        setTimeout(function () {
            expect(ret).assertTrue();
        }, TIMEOUT);
Z
zhaoyuan17 已提交
76 77 78 79 80 81 82 83 84
    });

    /*
     * @tc.number: bms_getHapModuleInfo_0200
     * @tc.name: get hapModuleInfo from two modules by getBundleInfo
     * @tc.desc: get the module information of the hap with type of feature
     */
    it('bms_getHapModuleInfo_0200', 0, async function (done) {
        console.debug('===========begin bms_getHapModuleInfo_0200===========')
Z
zhaoyuan17 已提交
85
        await install(['/data/test/bmsMainAbilityFirstScene.hap']);
Z
zhaoyuan17 已提交
86
        await install(['/data/test/bmsMainAbilitySecondScene.hap']);
Z
zhaoyuan17 已提交
87
        let ret = false
Z
zhaoyuan17 已提交
88 89 90 91 92 93
        let bundleName = 'com.example.bmsmainabilityfirstscene';
        let firstMainAbility = 'com.example.bmsmainabilityfirstscene.MainAbility';
        let secondMainAbility = 'com.example.bmsmainabilitysecondscene.MainAbility';
        bundle.getBundleInfo(bundleName, 1, async (err, data) => {
            console.debug('=======hapModule length========' + data.hapModuleInfo.length);
            expect(data.hapModuleInfo.length).assertEqual(2);
Z
zhaoyuan17 已提交
94
            for (let i = 0, len = data.hapModuleInfo.length; i < len; i++) {
Z
zhaoyuan17 已提交
95 96 97 98
                console.debug('=======get hapModule========' + JSON.stringify(data.hapModuleInfo[i]))
                console.debug('=======get hapModule mainAbilityName========' + data.hapModuleInfo[i].mainAbilityName);
                checkHapModuleInfo(data.hapModuleInfo[i]);
            }
Z
zhaoyuan17 已提交
99 100 101 102 103 104
            if (data.hapModuleInfo.length == 2) {
                expect(data.hapModuleInfo[0].mainAbilityName).assertEqual(firstMainAbility);
                expect(data.hapModuleInfo[0].moduleName).assertEqual('entry');
                expect(data.hapModuleInfo[1].mainAbilityName).assertEqual(secondMainAbility);
                expect(data.hapModuleInfo[1].moduleName).assertEqual('bmsmainabilitysecondscene');
            }
Z
zhaoyuan17 已提交
105
            await uninstall(bundleName);
Z
zhaoyuan17 已提交
106
            ret = true;
Z
zhaoyuan17 已提交
107 108
            done();
        })
Z
zhaoyuan17 已提交
109 110 111
        setTimeout(function () {
            expect(ret).assertTrue();
        }, TIMEOUT);
Z
zhaoyuan17 已提交
112 113 114 115 116 117 118 119 120 121 122
    })

    /*
     * @tc.number: bms_getHapModuleInfo_0300
     * @tc.name: get hapModuleInfo one app without mainAbility by getBundleInfo
     * @tc.desc: get the module information of the hap without mainAbility
     */
    it('bms_getHapModuleInfo_0300', 0, async function (done) {
        console.debug('===========begin bms_getHapModuleInfo_0300===========')
        await install(['/data/test/bmsThirdBundleTest2.hap']);
        let bundleName = 'com.example.third2';
Z
zhaoyuan17 已提交
123
        let ret = false;
Z
zhaoyuan17 已提交
124
        bundle.getBundleInfo(bundleName, 1,).then(async (data) => {
Z
zhaoyuan17 已提交
125 126
            console.debug('=======get hapModule========' + JSON.stringify(data))
            expect(data.hapModuleInfo.length).assertEqual(1);
Z
zhaoyuan17 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
            if (data.hapModuleInfo.length > 0) {
                console.debug('=======get hapModule mainAbilityName========' + data.hapModuleInfo[0].mainAbilityName)
                expect(data.hapModuleInfo[0].mainAbilityName).assertEqual('');
                expect(data.hapModuleInfo[0].moduleName).assertEqual('entry');
                checkHapModuleInfo(data.hapModuleInfo[0]);
                ret = true;
            }
            await uninstall(bundleName);
            done();
        })
        setTimeout(function () {
            expect(ret).assertTrue();
        }, TIMEOUT);
    })

Z
zhaoyuan17 已提交
142 143 144 145 146
    /*
     * @tc.number: bms_getHapModuleInfo_0400
     * @tc.name: get hapModuleInfo through getBundleInfo, an application that adds mainAbility  
     * @tc.desc: get the module information of the hap with the added field mainAbility 
     */
Z
zhaoyuan17 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    it('bms_getHapModuleInfo_0400', 0, async function (done) {
        console.debug('===========begin bms_getHapModuleInfo_0400===========')
        await install(['/data/test/bmsThirdBundleTest5.hap']);
        let bundleName = 'com.example.third5';
        let ret = true;
        bundle.getBundleInfo(bundleName, 1, async (err, data) => {
            console.debug('=======get hapModule========' + JSON.stringify(data))
            expect(data.hapModuleInfo.length).assertEqual(1);
            if (data.hapModuleInfo.length == 1) {
                console.debug('=======get hapModule mainAbilityName========' + data.hapModuleInfo[0].mainAbilityName)
                expect(data.hapModuleInfo[0].mainAbilityName).assertEqual('com.example.third5.AMainAbility');
                expect(data.hapModuleInfo[0].moduleName).assertEqual('entry');
                checkHapModuleInfo(data.hapModuleInfo[0]);
                ret = true;
            }
Z
zhaoyuan17 已提交
162 163 164
            await uninstall(bundleName);
            done();
        })
Z
zhaoyuan17 已提交
165 166 167
        setTimeout(function () {
            expect(ret).assertTrue();
        }, TIMEOUT);
Z
zhaoyuan17 已提交
168 169
    })

Z
zhaoyuan17 已提交
170 171 172 173 174
     /*
     * @tc.number: bms_getHapModuleInfo_0500
     * @tc.name: get the hapModuleInfo of the upgraded hap package from an application through getBundleInfo 
     * @tc.desc: get the module information of the mainAbility upgrade hap  
     */
Z
zhaoyuan17 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    it('bms_getHapModuleInfo_0500', 0, async function (done) {
        console.debug('===========begin bms_getHapModuleInfo_0500===========')
        await install(['/data/test/bmsThirdBundleTest1.hap']);
        await install(['/data/test/bmsThirdBundleTestA1.hap']);
        let bundleName = 'com.example.third1';
        let ret = false
        bundle.getBundleInfo(bundleName, 1, callback);
        async function callback(err, data) {
            console.debug('=======get bundle========' + JSON.stringify(data));
            expect(data.hapModuleInfo.length).assertEqual(1);
            if (data.hapModuleInfo.length == 1) {
                let hapModuleInfo = data.hapModuleInfo[0];
                console.debug('=======get hapModule========' + JSON.stringify(hapModuleInfo))
                console.debug('=======get hapModule mainAbilityName========' + hapModuleInfo.mainAbilityName)
                expect(hapModuleInfo.moduleName).assertEqual('entry');
                expect(hapModuleInfo.mainAbilityName).assertEqual('com.example.third1.AMainAbility');
                checkHapModuleInfo(hapModuleInfo);
            }
            await uninstall(bundleName);
            ret = true;
            done();
        }
        setTimeout(function () {
            expect(ret).assertTrue();
        }, TIMEOUT);
    });

Z
zhaoyuan17 已提交
202 203 204 205 206
    /*
     * @tc.number: bms_getHapModuleInfo_0600
     * @tc.name: get hapModuleInfo from the system through getBundleInfo 
     * @tc.desc: get module information of mainAbility system application 
     */
Z
zhaoyuan17 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    it('bms_getHapModuleInfo_0600', 0, async function (done) {
        console.debug('===========begin bms_getHapModuleInfo_0600===========')
        let bundleName = 'com.example.system1';
        let ret = false
        bundle.getBundleInfo(bundleName, 1, callback);
        function callback(err, data) {
            console.debug('=======get bundle========' + JSON.stringify(data));
            console.debug('=======data.hapModuleInfo.length========' + data.hapModuleInfo.length);
            expect(data.hapModuleInfo.length).assertEqual(1);
            if (data.hapModuleInfo.length == 1) {
                let hapModuleInfo = data.hapModuleInfo[0];
                console.debug('=======get hapModule========' + JSON.stringify(hapModuleInfo))
                console.debug('=======get hapModule mainAbilityName========' + hapModuleInfo.mainAbilityName)
                expect(hapModuleInfo.moduleName).assertEqual('entry');
                expect(hapModuleInfo.mainAbilityName).assertEqual('com.example.system1.MainAbility');
                checkHapModuleInfo(hapModuleInfo);
                ret = true;
            }
            done();
        }
        setTimeout(function () {
            expect(ret).assertTrue();
        }, TIMEOUT);
    });

Z
zhaoyuan17 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    function checkHapModuleInfo(dataInfo) {
        console.debug('========begin check hapModuleInfo========')
        expect(typeof dataInfo.name).assertEqual('string');
        expect(typeof dataInfo.description).assertEqual('string');
        expect(typeof dataInfo.descriptionId).assertEqual('number');
        expect(typeof dataInfo.icon).assertEqual('string');
        expect(typeof dataInfo.label).assertEqual('string');
        expect(typeof dataInfo.labelId).assertEqual('number');
        expect(typeof dataInfo.iconId).assertEqual('number');
        expect(typeof dataInfo.backgroundImg).assertEqual('string');
        expect(typeof dataInfo.supportedModes).assertEqual('number');
        expect(typeof dataInfo.reqCapabilities).assertEqual('object');
        expect(typeof dataInfo.deviceTypes).assertEqual('object');
        expect(typeof dataInfo.abilityInfo).assertEqual('object');
        expect(typeof dataInfo.moduleName).assertEqual('string');
        expect(typeof dataInfo.mainAbilityName).assertEqual('string');
        expect(typeof dataInfo.installationFree).assertEqual('boolean');
    }

Z
zhaoyuan17 已提交
251
    async function install(bundlePath) {
Z
zhaoyuan17 已提交
252
        var installer = await bundle.getBundleInstaller();
Z
zhaoyuan17 已提交
253 254
        console.log('========install========' + typeof installer);
        installer.install(bundlePath, {
Z
zhaoyuan17 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
            param: {
                userId: 0,
                installFlag: 1,
                isKeepData: false
            }
        }, onReceiveInstallEvent);
        function onReceiveInstallEvent(err, data) {
            console.info('========install Finish========');
            expect(typeof err).assertEqual('object');
            expect(err.code).assertEqual(0);
            expect(typeof data).assertEqual('object');
            expect(data.status).assertEqual(0);
            expect(data.statusMessage).assertEqual('SUCCESS');
        }
    }

Z
zhaoyuan17 已提交
271
    async function uninstall(bundleName) {
Z
zhaoyuan17 已提交
272
        var installer = await bundle.getBundleInstaller();
Z
zhaoyuan17 已提交
273
        installer.uninstall(bundleName, {
Z
zhaoyuan17 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
            param: {
                userId: 0,
                installFlag: 1,
                isKeepData: false
            }
        }, onReceiveUninstallEvent);

        function onReceiveUninstallEvent(err, data) {
            console.info('========uninstall Finish========');
            expect(typeof err).assertEqual('object');
            expect(err.code).assertEqual(0);
            expect(typeof data).assertEqual('object');
            expect(data.status).assertEqual(0);
            expect(data.statusMessage).assertEqual('SUCCESS');
        }
    }
})