From 2ef981814ab3f212bc62d8e32f64c3bc4ba401e2 Mon Sep 17 00:00:00 2001 From: yangbo Date: Tue, 4 Jul 2023 11:44:37 +0800 Subject: [PATCH] fix getPermission Signed-off-by: yangbo --- multimedia/medialibrary/common.js | 57 ++++++++++--------- .../main/ets/test/getExifNoLocation.test.ets | 53 ++++++++--------- .../main/ets/test/getExifLocation.test.ets | 56 +++++++++--------- 3 files changed, 85 insertions(+), 81 deletions(-) diff --git a/multimedia/medialibrary/common.js b/multimedia/medialibrary/common.js index 6b8f6bc38..42f526717 100644 --- a/multimedia/medialibrary/common.js +++ b/multimedia/medialibrary/common.js @@ -14,7 +14,7 @@ */ import mediaLibrary from "@ohos.multimedia.mediaLibrary"; import abilityAccessCtrl from "@ohos.abilityAccessCtrl"; -import bundle from "@ohos.bundle"; +import bundleManager from '@ohos.bundle.bundleManager'; import uitest from "@ohos.UiTest"; const presetsCount = { ActsMediaLibraryAlbumTest: { albumsCount: 15, assetsCount: 27 }, @@ -238,36 +238,39 @@ const getPermission = async function (name, context) { if (!name) { name = "ohos.acts.multimedia.mediaLibrary"; } - console.info("getPermission start", name); - let permissions = ["ohos.permission.MEDIA_LOCATION", "ohos.permission.READ_MEDIA", "ohos.permission.WRITE_MEDIA"]; - - let atManager = abilityAccessCtrl.createAtManager(); try { - atManager.requestPermissionsFromUser(context, permissions, (err, data) => { - console.info(`getPermission requestPermissionsFromUser ${JSON.stringify(data)}`); - }); - } catch (err) { - console.log(`get permission catch err -> ${JSON.stringify(err)}`); - } - await sleep(1000); - let driver = uitest.Driver.create(); - - await sleep(2000); - let button = await driver.findComponent(uitest.ON.text("允许")); - await button.click(); - await sleep(2000); + console.info('getPermission start', name); + let permissionState = new Map(); + const permissions = [ + 'ohos.permission.MEDIA_LOCATION', + 'ohos.permission.READ_MEDIA', + 'ohos.permission.WRITE_MEDIA', + ]; - let appInfo = await bundle.getApplicationInfo(name, 0, 100); - let tokenID = appInfo.accessTokenId; - - let isGranted1 = await atManager.verifyAccessToken(tokenID, "ohos.permission.MEDIA_LOCATION"); - let isGranted2 = await atManager.verifyAccessToken(tokenID, "ohos.permission.READ_MEDIA"); - let isGranted3 = await atManager.verifyAccessToken(tokenID, "ohos.permission.WRITE_MEDIA"); - if (!(isGranted1 == 0 && isGranted2 == 0 && isGranted3 == 0)) { - console.info("getPermission failed"); + const atManager = abilityAccessCtrl.createAtManager(); + const appFlags = bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT; + const userId = 100; + const appInfo = await bundleManager.getApplicationInfo(name, appFlags, userId); + const tokenID = appInfo.accessTokenId; + for (const permission of permissions) { + console.info('getPermission permission: ' + permission); + try { + await atManager.grantUserGrantedPermission(tokenID, permission, 1); + } catch (error) { + console.info(`getPermission ${permission} failed`); + } + permissionState.set(permission, await atManager.verifyAccessToken(tokenID, permission)); + } + permissionState.forEach((value, key, map) => { + if (value !== 0) { + console.info(`getPermission failed; permission: ${key}, state: ${value}`); + } + }); + console.info('getPermission end'); + } catch (error) { + console.info(`getPermission failed, error: ${error}`); } - console.info("getPermission end"); }; const MODIFY_ERROR_CODE_01 = "-1000"; diff --git a/multimedia/medialibrary/mediaLibrary_getExifNoLocation/entry/src/main/ets/test/getExifNoLocation.test.ets b/multimedia/medialibrary/mediaLibrary_getExifNoLocation/entry/src/main/ets/test/getExifNoLocation.test.ets index eb1e72f94..a3b4fce8d 100644 --- a/multimedia/medialibrary/mediaLibrary_getExifNoLocation/entry/src/main/ets/test/getExifNoLocation.test.ets +++ b/multimedia/medialibrary/mediaLibrary_getExifNoLocation/entry/src/main/ets/test/getExifNoLocation.test.ets @@ -16,39 +16,40 @@ import image from "@ohos.multimedia.image"; import mediaLibrary from "@ohos.multimedia.mediaLibrary"; import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "deccjsunit/index"; import abilityAccessCtrl from "@ohos.abilityAccessCtrl"; -import bundle from "@ohos.bundle"; -import uitest from "@ohos.UiTest"; - -import { sleep } from "../../../../../../common"; +import bundleManager from '@ohos.bundle.bundleManager'; export default function getExifNoLocation(abilityContext) { describe("getExifNoLocation", function () { beforeAll(async function () { console.info("beforeAll case"); - let atManager = abilityAccessCtrl.createAtManager(); - let permissions = ["ohos.permission.READ_MEDIA"]; - try { - atManager.requestPermissionsFromUser(abilityContext, permissions, (err, data) => { - console.info(`getPermission requestPermissionsFromUser ${JSON.stringify(data)}`); - }); - } catch (err) { - console.log(`get permission catch err -> ${JSON.stringify(err)}`); - } - await sleep(1000); + let permissionState = new Map(); + const permissions = [ + 'ohos.permission.READ_MEDIA', + ]; - let driver = uitest.Driver.create(); - await sleep(2000); - - let button = await driver.findComponent(uitest.ON.text("允许")); - await button.click(); - await sleep(2000); - let appInfo = await bundle.getApplicationInfo("ohos.acts.multimedia.mediaLibrary", 0, 100); - let tokenID = appInfo.accessTokenId; - - let isGranted = await atManager.verifyAccessToken(tokenID, "ohos.permission.READ_MEDIA"); - if (isGranted != 0) { - console.info("getpermission failed"); + const atManager = abilityAccessCtrl.createAtManager(); + const appFlags = bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT; + const userId = 100; + const appInfo = await bundleManager.getApplicationInfo('ohos.acts.multimedia.mediaLibrary', appFlags, userId); + const tokenID = appInfo.accessTokenId; + for (const permission of permissions) { + console.info('getPermission permission: ' + permission); + try { + await atManager.grantUserGrantedPermission(tokenID, permission, 1); + } catch (error) { + console.info(`getPermission ${permission} failed`); + } + permissionState.set(permission, await atManager.verifyAccessToken(tokenID, permission)); + } + permissionState.forEach((value, key, map) => { + if (value !== 0) { + console.info(`getPermission failed; permission: ${key}, state: ${value}`); + } + }); + console.info('getPermission end'); + } catch (error) { + console.info(`getPermission failed, error: ${error}`); } }); beforeEach(function () { diff --git a/multimedia/medialibrary/mediaLibrary_getExiflocation/entry/src/main/ets/test/getExifLocation.test.ets b/multimedia/medialibrary/mediaLibrary_getExiflocation/entry/src/main/ets/test/getExifLocation.test.ets index 97db4f2e0..9328fede6 100644 --- a/multimedia/medialibrary/mediaLibrary_getExiflocation/entry/src/main/ets/test/getExifLocation.test.ets +++ b/multimedia/medialibrary/mediaLibrary_getExiflocation/entry/src/main/ets/test/getExifLocation.test.ets @@ -16,41 +16,41 @@ import image from "@ohos.multimedia.image"; import mediaLibrary from "@ohos.multimedia.mediaLibrary"; import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "deccjsunit/index"; import abilityAccessCtrl from "@ohos.abilityAccessCtrl"; -import bundle from "@ohos.bundle"; -import uitest from "@ohos.UiTest"; - -import { sleep } from "../../../../../../common"; +import bundleManager from '@ohos.bundle.bundleManager'; export default function getExifLocation(abilityContext) { describe("getExifLocation", function () { beforeAll(async function () { console.info("beforeAll case"); - let atManager = abilityAccessCtrl.createAtManager(); - - let permissions = ["ohos.permission.MEDIA_LOCATION", "ohos.permission.READ_MEDIA"]; try { - atManager.requestPermissionsFromUser(abilityContext, permissions, (err, data) => { - console.info(`getPermission requestPermissionsFromUser ${JSON.stringify(data)}`); - }); - } catch (err) { - console.log(`get permission catch err -> ${JSON.stringify(err)}`); - } - await sleep(1000); + let permissionState = new Map(); + const permissions = [ + 'ohos.permission.MEDIA_LOCATION', + 'ohos.permission.READ_MEDIA', + ]; - let driver = uitest.Driver.create(); - await sleep(2000); - - let button = await driver.findComponent(uitest.ON.text("允许")); - await button.click(); - await sleep(2000); - - let appInfo = await bundle.getApplicationInfo("ohos.acts.multimedia.mediaLibrary", 0, 100); - let tokenID = appInfo.accessTokenId; - - let isGranted1 = await atManager.verifyAccessToken(tokenID, "ohos.permission.MEDIA_LOCATION"); - let isGranted2 = await atManager.verifyAccessToken(tokenID, "ohos.permission.READ_MEDIA"); - if (!(isGranted1 == 0 && isGranted2 == 0)) { - console.info("getpermission failed"); + const atManager = abilityAccessCtrl.createAtManager(); + const appFlags = bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT; + const userId = 100; + const appInfo = await bundleManager.getApplicationInfo('ohos.acts.multimedia.mediaLibrary', appFlags, userId); + const tokenID = appInfo.accessTokenId; + for (const permission of permissions) { + console.info('getPermission permission: ' + permission); + try { + await atManager.grantUserGrantedPermission(tokenID, permission, 1); + } catch (error) { + console.info(`getPermission ${permission} failed`); + } + permissionState.set(permission, await atManager.verifyAccessToken(tokenID, permission)); + } + permissionState.forEach((value, key, map) => { + if (value !== 0) { + console.info(`getPermission failed; permission: ${key}, state: ${value}`); + } + }); + console.info('getPermission end'); + } catch (error) { + console.info(`getPermission failed, error: ${error}`); } }); beforeEach(function () { -- GitLab