MediaTestBase.js 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 resourceManager from '@ohos.resourceManager';
1
15754133731 已提交
17
import { expect } from 'deccjsunit/index'
18 19
import router from '@system.router'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
20 21
import fileio from '@ohos.fileio'
import featureAbility from '@ohos.ability.featureAbility'
22
import { UiDriver, BY, PointerMatrix } from '@ohos.UiTest'
1
15754133731 已提交
23
const CODECMIMEVALUE = ['video/x-h264', 'audio/mpeg', 'video/avc', 'audio/mp4a-latm']
24
const context = featureAbility.getContext();
25

26 27 28 29 30 31 32 33
export async function getPermission(permissionNames) {
    featureAbility.getContext().requestPermissionsFromUser(permissionNames, 0, async (data) => {
        console.info("case request success" + JSON.stringify(data));
    })
}

export async function driveFn(num) {
    console.info(`case come in driveFn 111`)
1
15754133731 已提交
34 35 36 37
    try {
        let driver = await UiDriver.create()
        console.info(`case come in driveFn 222`)
        console.info(`driver is ${JSON.stringify(driver)}`)
1
15754133731 已提交
38
        await msleepAsync(2000)
1
15754133731 已提交
39 40 41 42 43 44 45 46 47 48 49
        console.info(`UiDriver start`)
        for (let i = 0; i < num; i++) {
            let button = await driver.findComponent(BY.text('允许'))
            console.info(`button is ${JSON.stringify(button)}`)
            await msleepAsync(2000)
            await button.click()
        }
    }
    catch (err) {
        console.info('err is ' + err);
        return;
1
15754133731 已提交
50 51 52 53 54 55
    }
}

export async function getAvRecorderFd(pathName, fileType) {
    console.info('case come in getAvRecorderFd')
    let fdObject = {
1
15754133731 已提交
56 57
        fileAsset: null,
        fdNumber: null
1
15754133731 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    }
    let displayName = pathName;
    console.info('[mediaLibrary] displayName is ' + displayName);
    console.info('[mediaLibrary] fileType is ' + fileType);
    const mediaTest = mediaLibrary.getMediaLibrary();
    let fileKeyObj = mediaLibrary.FileKey;
    let mediaType;
    let publicPath;
    if (fileType == 'audio') {
        mediaType = mediaLibrary.MediaType.AUDIO;
        publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_AUDIO);
    } else {
        mediaType = mediaLibrary.MediaType.VIDEO;
        publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
    }
    console.info('[mediaLibrary] publicPath is ' + publicPath);
    let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
    if (dataUri != undefined) {
        let args = dataUri.id.toString();
        let fetchOp = {
1
15754133731 已提交
78 79
            selections: fileKeyObj.ID + "=?",
            selectionArgs: [args],
1
15754133731 已提交
80
        }
1
15754133731 已提交
81 82 83 84
        let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
        fdObject.fileAsset = await fetchFileResult.getAllObject();
        fdObject.fdNumber = await fdObject.fileAsset[0].open('rw');
        console.info('case getFd number is: ' + fdObject.fdNumber);
85
    }
1
15754133731 已提交
86
    return fdObject;
87 88
}

89
// File operation
90
export async function getFileDescriptor(fileName) {
91
    let fileDescriptor = undefined;
92 93
    await resourceManager.getResourceManager().then(async (mgr) => {
        await mgr.getRawFileDescriptor(fileName).then(value => {
1
15754133731 已提交
94
            fileDescriptor = { fd: value.fd, offset: value.offset, length: value.length };
95
            console.log('case getRawFileDescriptor success fileName: ' + fileName);
96 97 98 99 100 101
        }).catch(error => {
            console.log('case getRawFileDescriptor err: ' + error);
        });
    });
    return fileDescriptor;
}
1
15754133731 已提交
102 103 104 105
export async function getStageFileDescriptor(fileName) {
    let fileDescriptor = undefined;
    let mgr = globalThis.abilityContext.resourceManager
    await mgr.getRawFileDescriptor(fileName).then(value => {
1
15754133731 已提交
106
        fileDescriptor = { fd: value.fd, offset: value.offset, length: value.length };
1
15754133731 已提交
107 108 109 110 111 112
        console.log('case getRawFileDescriptor success fileName: ' + fileName);
    }).catch(error => {
        console.log('case getRawFileDescriptor err: ' + error);
    });
    return fileDescriptor;
}
113 114
export async function closeFileDescriptor(fileName) {
    await resourceManager.getResourceManager().then(async (mgr) => {
1
15754133731 已提交
115
        await mgr.closeRawFileDescriptor(fileName).then(() => {
116
            console.log('case closeRawFileDescriptor ' + fileName);
117 118 119 120 121
        }).catch(error => {
            console.log('case closeRawFileDescriptor err: ' + error);
        });
    });
}
122 123 124 125 126 127 128

export function isFileOpen(fileDescriptor, done) {
    if (fileDescriptor == undefined) {
        expect().assertFail();
        console.info('case error fileDescriptor undefined, open file fail');
        done();
    }
129 130
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
export async function getFdRead(pathName, done) {
    let fdReturn;
    await context.getFilesDir().then((fileDir) => {
        console.info("case file dir is" + JSON.stringify(fileDir));
        pathName = fileDir + '/' + pathName;
        console.info("case pathName is" + pathName);
    });
    await fileio.open(pathName).then((fdNumber) => {
        isFileOpen(fdNumber, done)
        fdReturn = fdNumber;
        console.info('[fileio]case open fd success, fd is ' + fdReturn);
    })
    return fdReturn;
}

146 147 148 149
export async function closeFdNumber(fdNumber) {
    await fileio.close(fdNumber);
}

150 151
// wait synchronously 
export function msleep(time) {
1
15754133731 已提交
152
    for (let t = Date.now(); Date.now() - t <= time;);
153 154 155 156 157 158 159 160 161 162 163 164 165
}

// wait asynchronously
export async function msleepAsync(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

export function printError(error, done) {
    expect().assertFail();
    console.info(`case error called,errMessage is ${error.message}`);
    done();
}

1
15754133731 已提交
166 167 168 169 170 171
export function assertErr(opera, err, done) {
    console.info(`case ${opera} error,errMessage is ${err.message}`);
    expect().assertFail();
    done();
}

172 173 174 175 176 177 178 179 180 181 182 183
// callback function for promise call back error
export function failureCallback(error) {
    expect().assertFail();
    console.info(`case error called,errMessage is ${error.message}`);
}

// callback function for promise catch error
export function catchCallback(error) {
    expect().assertFail();
    console.info(`case error called,errMessage is ${error.message}`);
}

184
export function checkDescription(actualDescription, descriptionKey, descriptionValue) {
1
15754133731 已提交
185 186
    for (let i = 0; i < descriptionKey.length; i++) {
        let property = actualDescription[descriptionKey[i]];
1
15754133731 已提交
187 188 189
        console.info('case key is  ' + descriptionKey[i]);
        console.info('case actual value is  ' + property);
        console.info('case hope value is  ' + descriptionValue[i]);
1
15754133731 已提交
190 191 192 193 194
        if (descriptionKey[i] == 'codec_mime') {
            expect(property).assertEqual(CODECMIMEVALUE[descriptionValue[i]]);
        } else {
            expect(property).assertEqual(descriptionValue[i]);
        }
1
15754133731 已提交
195

1
15754133731 已提交
196 197 198 199
    }
}

export function checkOldDescription(actualDescription, descriptionKey, descriptionValue) {
200 201
    for (let i = 0; i < descriptionKey.length; i++) {
        let property = actualDescription[descriptionKey[i]];
1
15754133731 已提交
202 203 204
        console.info('case key is  ' + descriptionKey[i]);
        console.info('case actual value is  ' + property);
        console.info('case hope value is  ' + descriptionValue[i]);
205 206 207 208
        expect(property).assertEqual(descriptionValue[i]);
    }
}

1
15754133731 已提交
209 210 211
export function printDescription(obj) {
    let description = "";
    for (let i in obj) {
212
        let property = obj[i];
1
15754133731 已提交
213 214 215 216
        console.info('case key is  ' + i);
        console.info('case value is  ' + property);
        description += i + " = " + property + "\n";
    }
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

export async function toNewPage(pagePath1, pagePath2, page) {
    let path = '';
    if (page == 0) {
        path = pagePath1;
    } else {
        path = pagePath2;
    }
    let options = {
        uri: path,
    }
    try {
        await router.push(options);
    } catch {
        console.info('case route failed');
    }
}

export async function clearRouter() {
    await router.clear();
}

export async function getFd(pathName) {
    let fdObject = {
1
15754133731 已提交
242 243
        fileAsset: null,
        fdNumber: null
244 245 246 247 248 249 250 251 252 253
    }
    let displayName = pathName;
    const mediaTest = mediaLibrary.getMediaLibrary();
    let fileKeyObj = mediaLibrary.FileKey;
    let mediaType = mediaLibrary.MediaType.VIDEO;
    let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
    let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
    if (dataUri != undefined) {
        let args = dataUri.id.toString();
        let fetchOp = {
1
15754133731 已提交
254 255
            selections: fileKeyObj.ID + "=?",
            selectionArgs: [args],
256 257 258
        }
        let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
        fdObject.fileAsset = await fetchFileResult.getAllObject();
259
        fdObject.fdNumber = await fdObject.fileAsset[0].open('rw');
260 261 262 263 264
        console.info('case getFd number is: ' + fdObject.fdNumber);
    }
    return fdObject;
}

265 266
export async function getAudioFd(pathName) {
    let fdObject = {
1
15754133731 已提交
267 268
        fileAsset: null,
        fdNumber: null
269 270 271 272 273 274 275 276 277 278
    }
    let displayName = pathName;
    const mediaTest = mediaLibrary.getMediaLibrary();
    let fileKeyObj = mediaLibrary.FileKey;
    let mediaType = mediaLibrary.MediaType.AUDIO;
    let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_AUDIO);
    let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
    if (dataUri != undefined) {
        let args = dataUri.id.toString();
        let fetchOp = {
1
15754133731 已提交
279 280
            selections: fileKeyObj.ID + "=?",
            selectionArgs: [args],
281 282 283 284 285 286 287 288 289
        }
        let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
        fdObject.fileAsset = await fetchFileResult.getAllObject();
        fdObject.fdNumber = await fdObject.fileAsset[0].open('rw');
        console.info('case getFd number is: ' + fdObject.fdNumber);
    }
    return fdObject;
}

290 291 292 293 294 295 296 297 298 299 300
export async function closeFd(fileAsset, fdNumber) {
    if (fileAsset != null) {
        await fileAsset[0].close(fdNumber).then(() => {
            console.info('[mediaLibrary] case close fd success');
        }).catch((err) => {
            console.info('[mediaLibrary] case close fd failed');
        });
    } else {
        console.info('[mediaLibrary] case fileAsset is null');
    }
}