AudioTestBase.js 1.7 KB
Newer Older
L
lwx1121892 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (C) 2022 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 abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import bundle from '@ohos.bundle';
import account from '@ohos.account.osAccount';


// apply permission for test hap
export async function applyPermission(applictionName, permissionNames) {
    let userId = await account.getAccountManager().getOsAccountLocalIdFromProcess();
L
modify  
lwx1121892 已提交
24
    console.info(`userid is :${userId}`)
L
lwx1121892 已提交
25 26 27 28
    let appInfo = await bundle.getApplicationInfo(applictionName, 0, userId);
    let atManager = abilityAccessCtrl.createAtManager();
    if (atManager != null) {
        let tokenID = appInfo.accessTokenId;
L
modify  
lwx1121892 已提交
29
        console.info(`[permission] case accessTokenID is ${tokenID}`);
L
lwx1121892 已提交
30 31
        for (let i = 0; i < permissionNames.length; i++) {
            await atManager.grantUserGrantedPermission(tokenID, permissionNames[i], 1).then((result) => {
L
modify  
lwx1121892 已提交
32
                console.info(`[permission] case grantUserGrantedPermission success : ${result}`);
L
lwx1121892 已提交
33
            }).catch((err) => {
L
modify  
lwx1121892 已提交
34
                console.info(`[permission] case grantUserGrantedPermission failed : ${err}`);
L
lwx1121892 已提交
35 36 37
            });
        }
    } else {
L
modify  
lwx1121892 已提交
38
        console.info(`[permission] case apply permission failed, createAtManager failed`);
L
lwx1121892 已提交
39 40
    }
}