ExampleJsunit.test.js 12.4 KB
Newer Older
Z
zhaoyuan17 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * 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.
 */

import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'
import demo from '@ohos.bundle'
import featureAbility from '@ohos.ability.featureability'
import abilityManager from '@ohos.app.abilityManager'
import commonEvent from '@ohos.commonevent'

const PATH = "/data/"
const ERROR = "error.hap"
const BMSJSTEST1 = "bmsJstest1.hap"
const BMSJSTEST3 = "bmsJstest3.hap"
const BMSJSTEST4 = "bmsJstest4.hap"
const BMSJSTEST5 = "bmsJstest5.hap"
const BMSJSTEST6 = "bmsJstest6.hap"
const BMSJSTEST8 = "bmsJstest8.hap"
const NAME1 = "com.example.myapplication1"
const NAME2 = "com.example.myapplication2"
const NAME3 = "com.example.myapplication4"
const NAME4 = "com.example.myapplication5"
const NAME5 = "com.example.myapplication6"
const THIRD1 = "com.example.third1"
const LAUNCHER = "com.ohos.launcher"
const OBJECT = "object"
const SUCCESS = "SUCCESS"
Z
zhaoyuan17 已提交
39
const START_ABILITY_TIMEOUT = 3000;
S
shilei 已提交
40 41 42
const ERR_CODE = -1;
const STATUS_INSTALL_FAILURE_INVALID = 3;
const STATUS_INSTALL_FAILURE_CONFLICT = 4;
Z
zhaoyuan17 已提交
43 44 45 46
var subscriberInfoEvent_0100 = {
    events: ['Third1_Publish_CommonEvent'],
};

S
shilei 已提交
47
describe('ActsBundleManagerUninstall', function () {
Z
zhaoyuan17 已提交
48 49 50 51 52 53
    /**
     * @tc.number uninstall_0100
     * @tc.name BUNDLE::uninstall
     * @tc.desc Test uninstall interfaces.
     */
    it('uninstall_0100', 0, async function (done) {
Z
zhaoyuan17 已提交
54 55
        let installData = await demo.getBundleInstaller();
        installData.install([PATH + BMSJSTEST1], {
S
shilei 已提交
56 57 58
            userId: 0,
            installFlag: 1,
            isKeepData: false
Z
zhaoyuan17 已提交
59 60 61 62 63 64 65
        }, async (err, data) => {
            expect(err.code).assertEqual(0);
            expect(data.status).assertEqual(0);
            expect(data.statusMessage).assertEqual('SUCCESS');
            var datainfo1 = await demo.getBundleInfo(NAME1, 1);
            expect(datainfo1.name).assertEqual(NAME1);
            installData.uninstall(NAME1, {
S
shilei 已提交
66 67 68
                userId: 0,
                installFlag: 1,
                isKeepData: false
Z
zhaoyuan17 已提交
69 70 71 72 73 74
            }, async (err, data) => {
                var datainfo2 = await demo.getBundleInfo(NAME1, 1);
                expect(datainfo2.name).assertEqual('');
                done();
            })
        });
Z
zhaoyuan17 已提交
75 76 77 78 79 80 81 82
    })

    /**
     * @tc.number uninstall_0200
     * @tc.name BUNDLE::uninstall
     * @tc.desc Test uninstall interfaces.
     */
    it('uninstall_0200', 0, async function (done) {
Z
zhaoyuan17 已提交
83 84
        let installData = await demo.getBundleInstaller()
        installData.install([PATH + BMSJSTEST1, PATH + BMSJSTEST3], {
S
shilei 已提交
85 86 87
            userId: 0,
            installFlag: 1,
            isKeepData: false
Z
zhaoyuan17 已提交
88
        }, async (err, data) => {
S
shilei 已提交
89 90 91
            expect(err.code).assertEqual(ERR_CODE);
            expect(data.status).assertEqual(STATUS_INSTALL_FAILURE_CONFLICT);
            expect(data.statusMessage).assertEqual('STATUS_INSTALL_FAILURE_CONFLICT');
Z
zhaoyuan17 已提交
92 93
            var datainfo1 = await demo.getBundleInfo(NAME1, 1);
            var datainfo2 = await demo.getBundleInfo(NAME2, 1);
S
shilei 已提交
94 95
            expect(datainfo1.name).assertEqual('');
            expect(datainfo2.name).assertEqual('');
Z
zhaoyuan17 已提交
96
            installData.uninstall(NAME1, {
S
shilei 已提交
97 98 99
                userId: 0,
                installFlag: 1,
                isKeepData: false
Z
zhaoyuan17 已提交
100 101 102 103
            }, async(err, data) => {
                var datainfo3 = await demo.getBundleInfo(NAME1, 1);
                expect(datainfo3.name).assertEqual('');
                installData.uninstall(NAME2, {
S
shilei 已提交
104 105 106
                    userId: 0,
                    installFlag: 1,
                    isKeepData: false
Z
zhaoyuan17 已提交
107 108 109 110 111 112 113
                }, async(err, data) => {
                    var datainfo4 = await demo.getBundleInfo(NAME2, 1);
                    expect(datainfo4.name).assertEqual('');
                    done();
                })
            })
        });
Z
zhaoyuan17 已提交
114 115 116 117 118 119 120 121
    })

    /**
     * @tc.number uninstall_0300
     * @tc.name BUNDLE::uninstall
     * @tc.desc Test uninstall interfaces.
     */
    it('uninstall_0300', 0, async function (done) {
Z
zhaoyuan17 已提交
122 123
        let installData = await demo.getBundleInstaller()
        installData.install([PATH + BMSJSTEST4, PATH + BMSJSTEST5, PATH + BMSJSTEST6], {
S
shilei 已提交
124 125 126
            userId: 0,
            installFlag: 1,
            isKeepData: false
Z
zhaoyuan17 已提交
127
        }, async (err, data) => {
S
shilei 已提交
128 129 130
            expect(err.code).assertEqual(ERR_CODE);
            expect(data.status).assertEqual(STATUS_INSTALL_FAILURE_INVALID);
            expect(data.statusMessage).assertEqual('STATUS_INSTALL_FAILURE_INVALID');
Z
zhaoyuan17 已提交
131
            var datainfo1 = await demo.getBundleInfo(NAME3, 1);
S
shilei 已提交
132
            expect(datainfo1.name).assertEqual('');
Z
zhaoyuan17 已提交
133
            var datainfo2 = await demo.getBundleInfo(NAME4, 1);
S
shilei 已提交
134
            expect(datainfo2.name).assertEqual('');
Z
zhaoyuan17 已提交
135
            var datainfo3 = await demo.getBundleInfo(NAME5, 1);
S
shilei 已提交
136
            expect(datainfo3.name).assertEqual('');
Z
zhaoyuan17 已提交
137
            installData.uninstall(NAME3, {
S
shilei 已提交
138 139 140
                userId: 0,
                installFlag: 1,
                isKeepData: false
Z
zhaoyuan17 已提交
141 142 143 144
            }, async(err, data) => {
                var datainfo4 = await demo.getBundleInfo(NAME3, 1);
                expect(datainfo4.name).assertEqual('');
                installData.uninstall(NAME4, {
S
shilei 已提交
145 146 147
                    userId: 0,
                    installFlag: 1,
                    isKeepData: false
Z
zhaoyuan17 已提交
148 149 150 151
                }, async(err, data) => {
                    var datainfo5 = await demo.getBundleInfo(NAME4, 1);
                    expect(datainfo5.name).assertEqual('');
                    installData.uninstall(NAME5, {
S
shilei 已提交
152 153 154
                        userId: 0,
                        installFlag: 1,
                        isKeepData: false
Z
zhaoyuan17 已提交
155 156 157 158 159 160 161 162
                    }, async(err, data) => {
                        var datainfo6 = await demo.getBundleInfo(NAME5, 1);
                        expect(datainfo6.name).assertEqual('');
                        done();
                    });
                });
            });
        });
Z
zhaoyuan17 已提交
163 164 165 166 167 168 169 170 171 172
    });

    /**
     * @tc.number uninstall_0400
     * @tc.name BUNDLE::uninstall
     * @tc.desc Test uninstall interfaces.
     */
    it('uninstall_0400', 0, async function (done) {
        demo.getBundleInstaller().then((data) => {
            data.uninstall(ERROR, {
S
shilei 已提交
173 174 175
                userId: 0,
                installFlag: 1,
                isKeepData: false
Z
zhaoyuan17 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
            }, OnReceiveinstallEvent);

            function OnReceiveinstallEvent(err, data) {
                expect(typeof data).assertEqual(OBJECT);
                expect(data.statusMessage).assertEqual("STATUS_UNINSTALL_FAILURE");
            }
            done();
        });
    })

    /**
     * @tc.number uninstall_0500
     * @tc.name BUNDLE::uninstall
     * @tc.desc Test uninstall interfaces.
     */
    it('uninstall_0500', 0, async function (done) {
        demo.getBundleInstaller().then((data) => {
            data.uninstall('', {
S
shilei 已提交
194 195 196
                userId: 0,
                installFlag: 1,
                isKeepData: false
Z
zhaoyuan17 已提交
197 198 199 200 201
            }, OnReceiveinstallEvent);

            function OnReceiveinstallEvent(err, data) {
                expect(typeof data).assertEqual(OBJECT);
                console.info('======data.statusMessage=====' + JSON.stringify(data.statusMessage));
Z
zhaoyuan17 已提交
202
                expect(data.statusMessage).assertEqual("STATUS_UNINSTALL_FAILURE_ABORTED");
Z
zhaoyuan17 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215
                done();
            }
        });
    })

    /**
     * @tc.number uninstall_0600
     * @tc.name BUNDLE::uninstall
     * @tc.desc Test uninstall interfaces.
     */
    it('uninstall_0600', 0, async function (done) {
        demo.getBundleInstaller().then((data) => {
            data.uninstall(LAUNCHER, {
S
shilei 已提交
216 217 218
                userId: 0,
                installFlag: 1,
                isKeepData: false
Z
zhaoyuan17 已提交
219 220 221 222 223
            }, OnReceiveinstallEvent);

            function OnReceiveinstallEvent(err, data) {
                expect(typeof data).assertEqual(OBJECT);
                console.info('======data.statusMessage=====' + JSON.stringify(data.statusMessage));
Z
zhaoyuan17 已提交
224
                expect(data.statusMessage).assertEqual("STATUS_UNINSTALL_FAILURE_CONFLICT");
Z
zhaoyuan17 已提交
225 226 227 228 229 230 231 232 233 234 235 236
                done();
            }
        });
    })

    /**
     * @tc.number uninstall_0700
     * @tc.name BUNDLE::uninstall
     * @tc.desc Test uninstall interfaces.
     */
    it('uninstall_0700', 0, async function (done) {
        let result = await demo.getBundleInstaller();
Z
zhaoyuan17 已提交
237
        result.install([PATH + BMSJSTEST1], {
S
shilei 已提交
238 239 240
            userId: 0,
            installFlag: 1,
            isKeepData: true
Z
zhaoyuan17 已提交
241 242 243 244
        }, OnReceiveinstallEvent);
        async function OnReceiveinstallEvent(err, data) {
            expect(typeof data).assertEqual(OBJECT);
            expect(data.statusMessage).assertEqual(SUCCESS);
Z
zhaoyuan17 已提交
245 246 247
            var datainfo1 = await demo.getBundleInfo(NAME1, 1);
            expect(datainfo1.name).assertEqual(NAME1);
            result.uninstall(NAME1, {
S
shilei 已提交
248 249 250
                userId: 0,
                installFlag: 1,
                isKeepData: false
Z
zhaoyuan17 已提交
251 252 253 254 255
            }, async(err, data) => {
                var datainfo2 = await demo.getBundleInfo(NAME1, 1);
                expect(datainfo2.name).assertEqual('');
                done();
            });
Z
zhaoyuan17 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
        }
    })

    /**
     * @tc.number uninstall_0800
     * @tc.name BUNDLE::uninstall
     * @tc.desc Test uninstall interfaces.
     */
    it('uninstall_0800', 0, async function (done) {
        var Subscriber;
        let id;
        commonEvent.createSubscriber(subscriberInfoEvent_0100).then((data) => {
            console.debug('====>Create Subscriber====>');
            Subscriber = data;
            commonEvent.subscribe(Subscriber, SubscribeCallBack);
        })
        function UnSubscribeCallback() {
            console.debug('====>UnSubscribe CallBack====>');
Z
zhaoyuan17 已提交
274
            done();
Z
zhaoyuan17 已提交
275 276 277 278 279
        }
        function timeout() {
            expect().assertFail();
            console.debug('uninstall_0800=====timeout======');
            commonEvent.unsubscribe(Subscriber, UnSubscribeCallback)
Z
zhaoyuan17 已提交
280
            done();
Z
zhaoyuan17 已提交
281
        }
Z
zhaoyuan17 已提交
282
        let installData = await demo.getBundleInstaller();
Z
zhaoyuan17 已提交
283
        installData.install([PATH + BMSJSTEST8], {
S
shilei 已提交
284 285 286
            userId: 0,
            installFlag: 1,
            isKeepData: false
Z
zhaoyuan17 已提交
287 288 289 290
        }, async (err, data) => {
            id = setTimeout(timeout, START_ABILITY_TIMEOUT);
            console.debug('=======start ability========')
            let result = await featureAbility.startAbility(
Z
zhaoyuan17 已提交
291
                {
Z
zhaoyuan17 已提交
292 293 294 295 296
                    want:
                    {
                        bundleName: 'com.example.third1',
                        abilityName: 'com.example.third1.MainAbility'
                    }
Z
zhaoyuan17 已提交
297
                }
Z
zhaoyuan17 已提交
298 299
            )
        });
Z
zhaoyuan17 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
        async function SubscribeCallBack(err, data) {
            clearTimeout(id);
            expect(data.event).assertEqual('Third1_Publish_CommonEvent');
            console.debug('====>Subscribe CallBack data:====>' + JSON.stringify(data));
            let processInfos1 = await abilityManager.getActiveProcessInfos();
            expect(typeof processInfos1).assertEqual('object');
            let processMap1 = new Map();
            let processMap2 = new Map();
            for (var i = 0, len = processInfos1.length; i < len; i++) {
                console.debug('=======Active Process uid=====' + processInfos1[i].uid);
                processMap1.set(processInfos1[i].uid, 0);
            }
            let bundleInfo = await demo.getBundleInfo('com.example.third1', 1);
            let uid = bundleInfo.uid;
            expect(processMap1.has(uid)).assertTrue();
Z
zhaoyuan17 已提交
315
            installData.uninstall(THIRD1, {
S
shilei 已提交
316 317 318
                userId: 0,
                installFlag: 1,
                isKeepData: false
Z
zhaoyuan17 已提交
319 320 321 322 323 324 325 326 327 328
            }, async(err, data) => {
                let processInfos2 = await abilityManager.getActiveProcessInfos();
                for (var i = 0, len = processInfos2.length; i < len; i++) {
                    console.debug('=======Active Process uid=====' + processInfos1[i].uid);
                    processMap2.set(processInfos2[i].uid, 0);
                }
                expect(processMap2.has(uid)).assertFalse();
                commonEvent.unsubscribe(Subscriber, UnSubscribeCallback)
                done();
            });
Z
zhaoyuan17 已提交
329 330 331 332
        }
    })
})

Z
zhaoyuan17 已提交
333