1f6603c60Sopenharmony_ci/* 2f6603c60Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License. 5f6603c60Sopenharmony_ci * You may obtain a copy of the License at 6f6603c60Sopenharmony_ci * 7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f6603c60Sopenharmony_ci * 9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and 13f6603c60Sopenharmony_ci * limitations under the License. 14f6603c60Sopenharmony_ci */ 15f6603c60Sopenharmony_ci 16f6603c60Sopenharmony_ciimport resourceManager from '@ohos.resourceManager'; 17f6603c60Sopenharmony_ciimport photoAccessHelper from '@ohos.file.photoAccessHelper'; 18f6603c60Sopenharmony_ciimport {expect} from 'deccjsunit/index' 19f6603c60Sopenharmony_ciimport router from '@system.router' 20f6603c60Sopenharmony_ciimport fs from '@ohos.file.fs'; 21f6603c60Sopenharmony_ciimport fileio from '@ohos.fileio' 22f6603c60Sopenharmony_ciimport featureAbility from '@ohos.ability.featureAbility' 23f6603c60Sopenharmony_ciimport { UiDriver, BY, PointerMatrix } from '@ohos.UiTest'; 24f6603c60Sopenharmony_ciimport abilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'; 25f6603c60Sopenharmony_ciconst CODECMIMEVALUE = ['video/avc', 'audio/mp4a-latm', 'audio/mpeg'] 26f6603c60Sopenharmony_ciconst delegator = abilityDelegatorRegistry.getAbilityDelegator(); 27f6603c60Sopenharmony_ciexport async function getPermission(permissionNames) { 28f6603c60Sopenharmony_ci featureAbility.getContext().requestPermissionsFromUser(permissionNames, 0, async (data) => { 29f6603c60Sopenharmony_ci console.info("case request success" + JSON.stringify(data)); 30f6603c60Sopenharmony_ci }) 31f6603c60Sopenharmony_ci} 32f6603c60Sopenharmony_ci 33f6603c60Sopenharmony_ciexport async function driveFn(num) { 34f6603c60Sopenharmony_ci console.info(`case come in driveFn 111`) 35f6603c60Sopenharmony_ci let driver = await UiDriver.create() 36f6603c60Sopenharmony_ci console.info(`case come in driveFn 222`) 37f6603c60Sopenharmony_ci console.info(`driver is ${JSON.stringify(driver)}`) 38f6603c60Sopenharmony_ci await msleepAsync(2000) 39f6603c60Sopenharmony_ci console.info(`UiDriver start`) 40f6603c60Sopenharmony_ci for (let i = 0; i < num; i++) { 41f6603c60Sopenharmony_ci let button = await driver.findComponent(BY.text('允许')) 42f6603c60Sopenharmony_ci if(button == null){ 43f6603c60Sopenharmony_ci let cmd = "hidumper -s WindowManagerService -a'-a'" 44f6603c60Sopenharmony_ci await delegator.executeShellCommand(cmd); 45f6603c60Sopenharmony_ci continue; 46f6603c60Sopenharmony_ci } 47f6603c60Sopenharmony_ci console.info(`button is ${JSON.stringify(button)}`) 48f6603c60Sopenharmony_ci await msleepAsync(2000) 49f6603c60Sopenharmony_ci if(await driver.findComponent(BY.text('允许')) != null){ 50f6603c60Sopenharmony_ci await button.click() 51f6603c60Sopenharmony_ci } 52f6603c60Sopenharmony_ci await msleepAsync(2000) 53f6603c60Sopenharmony_ci } 54f6603c60Sopenharmony_ci await msleepAsync(2000) 55f6603c60Sopenharmony_ci} 56f6603c60Sopenharmony_ci 57f6603c60Sopenharmony_ci// File operation 58f6603c60Sopenharmony_ciexport async function getFileDescriptor(fileName) { 59f6603c60Sopenharmony_ci let fileDescriptor = undefined; 60f6603c60Sopenharmony_ci await resourceManager.getResourceManager().then(async (mgr) => { 61f6603c60Sopenharmony_ci await mgr.getRawFileDescriptor(fileName).then(value => { 62f6603c60Sopenharmony_ci fileDescriptor = {fd: value.fd, offset: value.offset, length: value.length}; 63f6603c60Sopenharmony_ci console.log('case getRawFileDescriptor success fileName: ' + fileName); 64f6603c60Sopenharmony_ci }).catch(error => { 65f6603c60Sopenharmony_ci console.log('case getRawFileDescriptor err: ' + error); 66f6603c60Sopenharmony_ci }); 67f6603c60Sopenharmony_ci }); 68f6603c60Sopenharmony_ci return fileDescriptor; 69f6603c60Sopenharmony_ci} 70f6603c60Sopenharmony_ciexport async function getStageFileDescriptor(fileName) { 71f6603c60Sopenharmony_ci let fileDescriptor = undefined; 72f6603c60Sopenharmony_ci let mgr = globalThis.abilityContext.resourceManager 73f6603c60Sopenharmony_ci await mgr.getRawFileDescriptor(fileName).then(value => { 74f6603c60Sopenharmony_ci fileDescriptor = {fd: value.fd, offset: value.offset, length: value.length}; 75f6603c60Sopenharmony_ci console.log('case getRawFileDescriptor success fileName: ' + fileName); 76f6603c60Sopenharmony_ci }).catch(error => { 77f6603c60Sopenharmony_ci console.log('case getRawFileDescriptor err: ' + error); 78f6603c60Sopenharmony_ci }); 79f6603c60Sopenharmony_ci return fileDescriptor; 80f6603c60Sopenharmony_ci} 81f6603c60Sopenharmony_ciexport async function closeFileDescriptor(fileName) { 82f6603c60Sopenharmony_ci await resourceManager.getResourceManager().then(async (mgr) => { 83f6603c60Sopenharmony_ci await mgr.closeRawFileDescriptor(fileName).then(()=> { 84f6603c60Sopenharmony_ci console.log('case closeRawFileDescriptor ' + fileName); 85f6603c60Sopenharmony_ci }).catch(error => { 86f6603c60Sopenharmony_ci console.log('case closeRawFileDescriptor err: ' + error); 87f6603c60Sopenharmony_ci }); 88f6603c60Sopenharmony_ci }); 89f6603c60Sopenharmony_ci} 90f6603c60Sopenharmony_ci 91f6603c60Sopenharmony_ciexport async function getStageFd(fileName) { 92f6603c60Sopenharmony_ci let fileDescriptor = undefined; 93f6603c60Sopenharmony_ci let mgr = globalThis.abilityContext.resourceManager; 94f6603c60Sopenharmony_ci await mgr.getRawFd(fileName).then(value => { 95f6603c60Sopenharmony_ci fileDescriptor = {fd: value.fd, offset: value.offset, length: value.length}; 96f6603c60Sopenharmony_ci console.log('case getRawFd success fileName: ' + fileName); 97f6603c60Sopenharmony_ci }).catch(error => { 98f6603c60Sopenharmony_ci console.log('case getRawFd err: ' + fileName); 99f6603c60Sopenharmony_ci console.log('case getRawFd err: ' + error); 100f6603c60Sopenharmony_ci }); 101f6603c60Sopenharmony_ci return fileDescriptor; 102f6603c60Sopenharmony_ci} 103f6603c60Sopenharmony_ci 104f6603c60Sopenharmony_ciexport async function closeStageFd(fileName) { 105f6603c60Sopenharmony_ci await resourceManager.getResourceManager().then(async (mgr) => { 106f6603c60Sopenharmony_ci await mgr.closeRawFd(fileName).then(()=> { 107f6603c60Sopenharmony_ci console.log('case closeRawFd ' + fileName); 108f6603c60Sopenharmony_ci }).catch(error => { 109f6603c60Sopenharmony_ci console.log('case closeRawFd err: ' + error); 110f6603c60Sopenharmony_ci }); 111f6603c60Sopenharmony_ci }); 112f6603c60Sopenharmony_ci} 113f6603c60Sopenharmony_ci 114f6603c60Sopenharmony_ciexport function isFileOpen(fileDescriptor, done) { 115f6603c60Sopenharmony_ci if (fileDescriptor == undefined) { 116f6603c60Sopenharmony_ci expect().assertFail(); 117f6603c60Sopenharmony_ci console.info('case error fileDescriptor undefined, open file fail'); 118f6603c60Sopenharmony_ci done(); 119f6603c60Sopenharmony_ci } 120f6603c60Sopenharmony_ci} 121f6603c60Sopenharmony_ci 122f6603c60Sopenharmony_ciexport async function getFdRead(pathName, done) { 123f6603c60Sopenharmony_ci let fdReturn; 124f6603c60Sopenharmony_ci await featureAbility.getContext().getFilesDir().then((fileDir) => { 125f6603c60Sopenharmony_ci console.info("case file dir is" + JSON.stringify(fileDir)); 126f6603c60Sopenharmony_ci pathName = fileDir + '/' + pathName; 127f6603c60Sopenharmony_ci console.info("case pathName is" + pathName); 128f6603c60Sopenharmony_ci }); 129f6603c60Sopenharmony_ci await fileio.open(pathName).then((fdNumber) => { 130f6603c60Sopenharmony_ci isFileOpen(fdNumber, done) 131f6603c60Sopenharmony_ci fdReturn = fdNumber; 132f6603c60Sopenharmony_ci console.info('[fileio]case open fd success, fd is ' + fdReturn); 133f6603c60Sopenharmony_ci }) 134f6603c60Sopenharmony_ci return fdReturn; 135f6603c60Sopenharmony_ci} 136f6603c60Sopenharmony_ci 137f6603c60Sopenharmony_ciexport async function closeFdNumber(fdNumber) { 138f6603c60Sopenharmony_ci await fileio.close(fdNumber); 139f6603c60Sopenharmony_ci} 140f6603c60Sopenharmony_ci 141f6603c60Sopenharmony_ci// wait synchronously 142f6603c60Sopenharmony_ciexport function msleep(time) { 143f6603c60Sopenharmony_ci for(let t = Date.now();Date.now() - t <= time;); 144f6603c60Sopenharmony_ci} 145f6603c60Sopenharmony_ci 146f6603c60Sopenharmony_ci// wait asynchronously 147f6603c60Sopenharmony_ciexport async function msleepAsync(ms) { 148f6603c60Sopenharmony_ci return new Promise((resolve) => setTimeout(resolve, ms)); 149f6603c60Sopenharmony_ci} 150f6603c60Sopenharmony_ci 151f6603c60Sopenharmony_ciexport function printError(error, done) { 152f6603c60Sopenharmony_ci expect().assertFail(); 153f6603c60Sopenharmony_ci console.info(`case error called,errMessage is ${error.message}`); 154f6603c60Sopenharmony_ci done(); 155f6603c60Sopenharmony_ci} 156f6603c60Sopenharmony_ci 157f6603c60Sopenharmony_ciexport function assertErr(opera, err, done) { 158f6603c60Sopenharmony_ci console.info(`case ${opera} error,errMessage is ${err.message}`); 159f6603c60Sopenharmony_ci expect().assertFail(); 160f6603c60Sopenharmony_ci done(); 161f6603c60Sopenharmony_ci} 162f6603c60Sopenharmony_ci 163f6603c60Sopenharmony_ci// callback function for promise call back error 164f6603c60Sopenharmony_ciexport function failureCallback(error) { 165f6603c60Sopenharmony_ci expect().assertFail(); 166f6603c60Sopenharmony_ci console.info(`case error called,errMessage is ${error.message}`); 167f6603c60Sopenharmony_ci} 168f6603c60Sopenharmony_ci 169f6603c60Sopenharmony_ci// callback function for promise catch error 170f6603c60Sopenharmony_ciexport function catchCallback(error) { 171f6603c60Sopenharmony_ci expect().assertFail(); 172f6603c60Sopenharmony_ci console.info(`case error called,errMessage is ${error.message}`); 173f6603c60Sopenharmony_ci} 174f6603c60Sopenharmony_ci 175f6603c60Sopenharmony_ciexport function checkDescription(actualDescription, descriptionKey, descriptionValue) { 176f6603c60Sopenharmony_ci for (let i = 0; i < descriptionKey.length; i++) { 177f6603c60Sopenharmony_ci let property = actualDescription[descriptionKey[i]]; 178f6603c60Sopenharmony_ci console.info('case key is '+ descriptionKey[i]); 179f6603c60Sopenharmony_ci console.info('case actual value is '+ property); 180f6603c60Sopenharmony_ci console.info('case hope value is '+ descriptionValue[i]); 181f6603c60Sopenharmony_ci if (descriptionKey[i] == 'codec_mime') { 182f6603c60Sopenharmony_ci console.info('CODECMIMEVALUE[descriptionValue[i]] value is '+ CODECMIMEVALUE[descriptionValue[i]]); 183f6603c60Sopenharmony_ci if(property == "video/x-h264"){ 184f6603c60Sopenharmony_ci console.info('property attribute is:'+ property); 185f6603c60Sopenharmony_ci }else{ 186f6603c60Sopenharmony_ci expect(property).assertEqual(CODECMIMEVALUE[descriptionValue[i]]); 187f6603c60Sopenharmony_ci } 188f6603c60Sopenharmony_ci } else if (descriptionKey[i] == 'bitrate') { 189f6603c60Sopenharmony_ci expect(Math.abs(property - descriptionValue[i])).assertLess(500); 190f6603c60Sopenharmony_ci } else { 191f6603c60Sopenharmony_ci expect(property).assertEqual(descriptionValue[i]); 192f6603c60Sopenharmony_ci } 193f6603c60Sopenharmony_ci 194f6603c60Sopenharmony_ci } 195f6603c60Sopenharmony_ci} 196f6603c60Sopenharmony_ci 197f6603c60Sopenharmony_ciexport function checkPlaybackInfo(actualDescription, descriptionKey, descriptionValue) { 198f6603c60Sopenharmony_ci for (let i = 0; i < descriptionKey.length; i++) { 199f6603c60Sopenharmony_ci let property = actualDescription[descriptionKey[i]]; 200f6603c60Sopenharmony_ci console.info('case key is ' + descriptionKey[i]); 201f6603c60Sopenharmony_ci console.info('case actual value is ' + property); 202f6603c60Sopenharmony_ci console.info('case hope value is ' + descriptionValue[i]); 203f6603c60Sopenharmony_ci expect(property).assertEqual(descriptionValue[i]); 204f6603c60Sopenharmony_ci } 205f6603c60Sopenharmony_ci} 206f6603c60Sopenharmony_ci 207f6603c60Sopenharmony_ciexport function checkOldDescription(actualDescription, descriptionKey, descriptionValue) { 208f6603c60Sopenharmony_ci for (let i = 0; i < descriptionKey.length; i++) { 209f6603c60Sopenharmony_ci let property = actualDescription[descriptionKey[i]]; 210f6603c60Sopenharmony_ci console.info('case key is '+ descriptionKey[i]); 211f6603c60Sopenharmony_ci console.info('case actual value is '+ property); 212f6603c60Sopenharmony_ci console.info('case hope value is '+ descriptionValue[i]); 213f6603c60Sopenharmony_ci if (descriptionKey[i] === 'bitrate'){ 214f6603c60Sopenharmony_ci expect(Math.abs(property - descriptionValue[i])).assertLess(500); 215f6603c60Sopenharmony_ci return; 216f6603c60Sopenharmony_ci } 217f6603c60Sopenharmony_ci expect(property).assertEqual(descriptionValue[i]); 218f6603c60Sopenharmony_ci } 219f6603c60Sopenharmony_ci} 220f6603c60Sopenharmony_ci 221f6603c60Sopenharmony_ciexport function printDescription(obj) { 222f6603c60Sopenharmony_ci let description = ""; 223f6603c60Sopenharmony_ci for(let i in obj) { 224f6603c60Sopenharmony_ci let property = obj[i]; 225f6603c60Sopenharmony_ci console.info('case key is '+ i); 226f6603c60Sopenharmony_ci console.info('case value is '+ property); 227f6603c60Sopenharmony_ci description += i + " = " + property + "\n"; 228f6603c60Sopenharmony_ci } 229f6603c60Sopenharmony_ci} 230f6603c60Sopenharmony_ci 231f6603c60Sopenharmony_ciexport async function toNewPage(pagePath1, pagePath2, page) { 232f6603c60Sopenharmony_ci let path = ''; 233f6603c60Sopenharmony_ci if (page == 0) { 234f6603c60Sopenharmony_ci path = pagePath1; 235f6603c60Sopenharmony_ci } else { 236f6603c60Sopenharmony_ci path = pagePath2; 237f6603c60Sopenharmony_ci } 238f6603c60Sopenharmony_ci let options = { 239f6603c60Sopenharmony_ci uri: path, 240f6603c60Sopenharmony_ci } 241f6603c60Sopenharmony_ci try { 242f6603c60Sopenharmony_ci await router.push(options); 243f6603c60Sopenharmony_ci } catch { 244f6603c60Sopenharmony_ci console.info('case route failed'); 245f6603c60Sopenharmony_ci } 246f6603c60Sopenharmony_ci} 247f6603c60Sopenharmony_ci 248f6603c60Sopenharmony_ciexport async function clearRouter() { 249f6603c60Sopenharmony_ci await router.clear(); 250f6603c60Sopenharmony_ci} 251f6603c60Sopenharmony_ci 252f6603c60Sopenharmony_ciexport async function saveVideo(asset) { 253f6603c60Sopenharmony_ci console.info('case saveVideo start'); 254f6603c60Sopenharmony_ci try { 255f6603c60Sopenharmony_ci let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(featureAbility.getContext()); 256f6603c60Sopenharmony_ci let assetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(asset); 257f6603c60Sopenharmony_ci assetChangeRequest.saveCameraPhoto(); 258f6603c60Sopenharmony_ci await phAccessHelper.applyChanges(assetChangeRequest); 259f6603c60Sopenharmony_ci console.info('case saveVideo successfully'); 260f6603c60Sopenharmony_ci } catch (error) { 261f6603c60Sopenharmony_ci console.info(`case error called,errMessage is ${error.message}`); 262f6603c60Sopenharmony_ci } 263f6603c60Sopenharmony_ci console.info('case saveVideo end'); 264f6603c60Sopenharmony_ci} 265f6603c60Sopenharmony_ci 266f6603c60Sopenharmony_ciexport async function getFd(pathName) { 267f6603c60Sopenharmony_ci console.info('case come in getFd') 268f6603c60Sopenharmony_ci let fdObject = { 269f6603c60Sopenharmony_ci fileAsset : null, 270f6603c60Sopenharmony_ci fdNumber : null 271f6603c60Sopenharmony_ci } 272f6603c60Sopenharmony_ci 273f6603c60Sopenharmony_ci await featureAbility.getContext().getFilesDir().then((fileDir) => { 274f6603c60Sopenharmony_ci console.info("case file dir is" + JSON.stringify(fileDir)); 275f6603c60Sopenharmony_ci pathName = fileDir + '/' + pathName; 276f6603c60Sopenharmony_ci console.info("case pathName is" + pathName); 277f6603c60Sopenharmony_ci }); 278f6603c60Sopenharmony_ci 279f6603c60Sopenharmony_ci let file = fs.openSync(pathName, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 280f6603c60Sopenharmony_ci fdObject.fileAsset = file; 281f6603c60Sopenharmony_ci fdObject.fdNumber = file.fd; 282f6603c60Sopenharmony_ci console.info('case getFd number is: ' + fdObject.fdNumber); 283f6603c60Sopenharmony_ci 284f6603c60Sopenharmony_ci return fdObject; 285f6603c60Sopenharmony_ci} 286f6603c60Sopenharmony_ci 287f6603c60Sopenharmony_ciexport async function closeFd(fdNumber) { 288f6603c60Sopenharmony_ci console.info('case come in closeFd') 289f6603c60Sopenharmony_ci if (fdNumber != null) { 290f6603c60Sopenharmony_ci fs.closeSync(fdNumber); 291f6603c60Sopenharmony_ci } else { 292f6603c60Sopenharmony_ci console.info('[fs.closeSync] case fdNumber is null'); 293f6603c60Sopenharmony_ci } 294f6603c60Sopenharmony_ci} 295