• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 account from '@ohos.account.osAccount'
18import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit'
19
20const BUNDLE_NAME1 = 'com.example.third1';
21const BUNDLE_NAME2 = 'com.example.third2';
22const BUNDLE_NAME4 = 'com.example.third4';
23const BUNDLE_NAME5 = 'com.example.third5';
24const BUNDLE_NAME6 = 'com.example.third6';
25const ABILITIY_NAME1 = "com.example.third5.MainAbilityA";
26const ABILITIY_NAME2 = "com.example.third5.MainAbilityB";
27const ABILITIY_NAME3 = "com.example.third2.MainAbilityA";
28const ABILITIY_NAME4 = "com.example.third4.MainAbility";
29const ABILITIY_NAME5 = "com.example.third1.MainAbilityA";
30const ABILITIY_NAME6 = "com.example.third3.MainAbilityA";
31const NUM_TWO = 2;
32const NUM_THREE = 3;
33const NUM_FOUR = 4;
34const NUM_NINE = 9;
35const DATATRANSFER = 1;
36const AUDIOPLAYBACK = 2;
37const AUDIORECORDING = 4;
38const LOCATION = 8;
39const BLUETOOTHINTERACTION = 16;
40const MULTIDEVICECONNECTION = 32;
41const WIFIINTERACTION = 64;
42const VOIP = 128;
43const TASKKEEPING = 256;
44let userId = 0;
45
46describe('ActsBmsGetBackGroundModes', function () {
47
48    beforeAll(async function (done) {
49        await account.getAccountManager().getOsAccountLocalIdFromProcess().then(account => {
50            console.info("getOsAccountLocalIdFromProcess userid  ==========" + account);
51            userId = account;
52            done();
53        }).catch(err => {
54            console.info("getOsAccountLocalIdFromProcess err ==========" + JSON.stringify(err));
55            done();
56        })
57    });
58
59    /*
60    * @tc.number: bms_backGroundModes_0100
61    * @tc.name: Get the backgroundModes information of the application through queryAbilityByWant
62    * @tc.desc: Get the information of the background modes from multi-ability application
63    */
64    it('bms_backGroundModes_0100', 0, async function (done) {
65        let dataInfos = await bundle.queryAbilityByWant({
66            action: 'action.system.home',
67            entities: ['entity.system.home'],
68            deviceId: '0',
69            bundleName: BUNDLE_NAME5,
70            abilityName: ''
71        }, bundle.BundleFlag.GET_BUNDLE_DEFAULT, userId);
72        expect(dataInfos.length).assertEqual(NUM_FOUR);
73        if (dataInfos.length == NUM_FOUR) {
74            expect(dataInfos[NUM_TWO].name).assertEqual(ABILITIY_NAME1);
75            expect(dataInfos[NUM_TWO].backgroundModes).assertEqual(DATATRANSFER | AUDIOPLAYBACK | AUDIORECORDING |
76                LOCATION | BLUETOOTHINTERACTION | MULTIDEVICECONNECTION | WIFIINTERACTION | VOIP | TASKKEEPING);
77            expect(dataInfos[NUM_THREE].name).assertEqual(ABILITIY_NAME2);
78            expect(dataInfos[NUM_THREE].backgroundModes).assertEqual(DATATRANSFER | VOIP);
79        }
80        let bundleInfos = await bundle.getAllBundleInfo(bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES, userId);
81        for (let i = 0; i < bundleInfos.length; i++) {
82            if (bundleInfos[i].name == BUNDLE_NAME5) {
83                for (let j = 0; j < bundleInfos[i].abilityInfos.length; j++) {
84                    if (bundleInfos[i].abilityInfos[j].name == ABILITIY_NAME1) {
85                        expect(bundleInfos[i].abilityInfos[j].backgroundModes).assertEqual(DATATRANSFER |
86                            AUDIOPLAYBACK | AUDIORECORDING | LOCATION | BLUETOOTHINTERACTION |
87                            MULTIDEVICECONNECTION | WIFIINTERACTION | VOIP | TASKKEEPING);
88                    } else if (bundleInfos[i].abilityInfos[j].name == ABILITIY_NAME2) {
89                        expect(bundleInfos[i].abilityInfos[j].backgroundModes).assertEqual(DATATRANSFER | VOIP);
90                    }
91                }
92            }
93        }
94        let data3 = await bundle.getBundleInfo(BUNDLE_NAME5, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES);
95        expect(data3.abilityInfos[2].backgroundModes).assertEqual(DATATRANSFER | AUDIOPLAYBACK | AUDIORECORDING |
96            LOCATION | BLUETOOTHINTERACTION | MULTIDEVICECONNECTION | WIFIINTERACTION | VOIP | TASKKEEPING);
97        expect(data3.abilityInfos[3].backgroundModes).assertEqual(DATATRANSFER | VOIP);
98        done();
99    });
100
101    /*
102    * @tc.number: bms_backGroundModes_0200
103    * @tc.name: Get the backgroundModes information of the application through queryAbilityByWant
104    * @tc.desc: Get all background modes information, and each ability of the application
105    *               contains one of the background mode
106    */
107    it('bms_backGroundModes_0200', 0, async function (done) {
108        let dataInfos = await bundle.queryAbilityByWant({
109            action: 'action.system.home',
110            entities: ['entity.system.home'],
111            deviceId: '0',
112            bundleName: BUNDLE_NAME6,
113            abilityName: ''
114        }, bundle.BundleFlag.GET_BUNDLE_DEFAULT, userId);
115        expect(dataInfos.length).assertEqual(NUM_NINE);
116        for (let i = 0, len = dataInfos.length; i < len; i++) {
117            expect(dataInfos[i].backgroundModes).assertEqual(1 << i);
118        }
119        done();
120    });
121
122    /*
123    * @tc.number: bms_backGroundModes_0300
124    * @tc.name: Get the backgroundModes information of the application through queryAbilityByWant
125    * @tc.desc: Read the backgroundModes information of the app's ability and replace invalid attributes
126    */
127    it('bms_backGroundModes_0300', 0, async function (done) {
128        let dataInfos = await bundle.queryAbilityByWant({
129            action: 'action.system.home',
130            entities: ['entity.system.home'],
131            deviceId: '0',
132            bundleName: BUNDLE_NAME2,
133            abilityName: ''
134        }, bundle.BundleFlag.GET_BUNDLE_DEFAULT, userId);
135        expect(dataInfos.length).assertEqual(NUM_TWO);
136        if (dataInfos.length == NUM_TWO) {
137            expect(dataInfos[1].name).assertEqual(ABILITIY_NAME3)
138            expect(dataInfos[1].backgroundModes).assertEqual(AUDIOPLAYBACK | AUDIORECORDING | LOCATION
139                | BLUETOOTHINTERACTION | MULTIDEVICECONNECTION | WIFIINTERACTION | VOIP | TASKKEEPING)
140        }
141        bundle.getAllBundleInfo(bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES, userId, (err, bundleInfos) => {
142            for (let i = 0; i < bundleInfos.length; i++) {
143                if (bundleInfos[i].name == BUNDLE_NAME2) {
144                    for (let j = 0; j < bundleInfos[i].abilityInfos.length; j++) {
145                        if (bundleInfos[i].abilityInfos[j].name == ABILITIY_NAME3) {
146                            expect(bundleInfos[i].abilityInfos[j].backgroundModes).assertEqual(
147                                AUDIOPLAYBACK | AUDIORECORDING | LOCATION | BLUETOOTHINTERACTION
148                                | MULTIDEVICECONNECTION | WIFIINTERACTION | VOIP | TASKKEEPING);
149                        }
150                    }
151                }
152            }
153            bundle.getBundleInfo(BUNDLE_NAME2, bundle.BundleFlag.GET_BUNDLE_WITH_ABILITIES, (err, data3) => {
154                expect(data3.abilityInfos[1].backgroundModes).assertEqual(AUDIOPLAYBACK | AUDIORECORDING | LOCATION
155                    | BLUETOOTHINTERACTION | MULTIDEVICECONNECTION | WIFIINTERACTION | VOIP | TASKKEEPING);
156                done();
157            });
158        });
159    });
160
161    /*
162    * @tc.number: bms_backGroundModes_0400
163    * @tc.name: Get the backgroundModes information of the application through queryAbilityByWant
164    * @tc.desc: Read the backgroundModes information of the app's ability and replace invalid attributes
165    */
166    it('bms_backGroundModes_0400', 0, async function (done) {
167        let dataInfos = await bundle.queryAbilityByWant({
168            action: 'action.system.home',
169            entities: ['entity.system.home'],
170            deviceId: '0',
171            bundleName: BUNDLE_NAME4,
172            abilityName: ''
173        }, bundle.BundleFlag.GET_BUNDLE_DEFAULT, userId);
174        expect(dataInfos.length).assertEqual(1);
175        if (dataInfos.length == 1) {
176            expect(dataInfos[0].name).assertEqual(ABILITIY_NAME4)
177            expect(dataInfos[0].backgroundModes).assertEqual(0)
178        }
179        done();
180    });
181
182    /*
183    * @tc.number: bms_backGroundModes_0500
184    * @tc.name: Get the backgroundModes information of the application through queryAbilityByWant
185    * @tc.desc: Get the backgroundModes information of the multi-hap package of the application
186    */
187    it('bms_backGroundModes_0500', 0, async function (done) {
188        let dataInfos = await bundle.queryAbilityByWant({
189            action: 'action.system.home',
190            entities: ['entity.system.home'],
191            deviceId: '0',
192            bundleName: BUNDLE_NAME1,
193            abilityName: ''
194        }, bundle.BundleFlag.GET_BUNDLE_DEFAULT, userId);
195        expect(dataInfos.length).assertEqual(NUM_FOUR);
196        if (dataInfos.length == NUM_FOUR) {
197            expect(dataInfos[1].name).assertEqual(ABILITIY_NAME5)
198            expect(dataInfos[1].backgroundModes).assertEqual(DATATRANSFER | AUDIOPLAYBACK | AUDIORECORDING |
199                LOCATION | BLUETOOTHINTERACTION | MULTIDEVICECONNECTION | WIFIINTERACTION | VOIP | TASKKEEPING)
200            expect(dataInfos[3].name).assertEqual(ABILITIY_NAME6)
201            expect(dataInfos[3].backgroundModes).assertEqual(DATATRANSFER | AUDIOPLAYBACK | AUDIORECORDING |
202                LOCATION | BLUETOOTHINTERACTION | MULTIDEVICECONNECTION | WIFIINTERACTION | VOIP | TASKKEEPING)
203        }
204        done();
205    });
206
207})