• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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 resourceManager from '@ohos.resourceManager';
17import { expect } from 'deccjsunit/index'
18import router from '@system.router'
19import mediaLibrary from '@ohos.multimedia.mediaLibrary'
20import fileio from '@ohos.fileio'
21import featureAbility from '@ohos.ability.featureAbility'
22import { UiDriver, BY, PointerMatrix } from '@ohos.UiTest'
23const CODECMIMEVALUE = ['video/x-h264', 'audio/mpeg', 'video/avc', 'audio/mp4a-latm']
24const context = featureAbility.getContext();
25
26export async function getPermission(permissionNames) {
27    featureAbility.getContext().requestPermissionsFromUser(permissionNames, 0, async (data) => {
28        console.info("case request success" + JSON.stringify(data));
29    })
30}
31
32export async function driveFn(num) {
33    console.info(`case come in driveFn 111`)
34    try {
35        let driver = await UiDriver.create()
36        console.info(`case come in driveFn 222`)
37        console.info(`driver is ${JSON.stringify(driver)}`)
38        await msleepAsync(2000)
39        console.info(`UiDriver start`)
40        for (let i = 0; i < num; i++) {
41            let button = await driver.findComponent(BY.text('允许'))
42            console.info(`button is ${JSON.stringify(button)}`)
43            await msleepAsync(2000)
44            await button.click()
45        }
46    }
47    catch (err) {
48        console.info('err is ' + err);
49        return;
50    }
51}
52
53export async function getAvRecorderFd(pathName, fileType) {
54    console.info('case come in getAvRecorderFd')
55    let fdObject = {
56        fileAsset: null,
57        fdNumber: null
58    }
59    let displayName = pathName;
60    console.info('[mediaLibrary] displayName is ' + displayName);
61    console.info('[mediaLibrary] fileType is ' + fileType);
62    const mediaTest = mediaLibrary.getMediaLibrary();
63    let fileKeyObj = mediaLibrary.FileKey;
64    let mediaType;
65    let publicPath;
66    if (fileType == 'audio') {
67        mediaType = mediaLibrary.MediaType.AUDIO;
68        publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_AUDIO);
69    } else {
70        mediaType = mediaLibrary.MediaType.VIDEO;
71        publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
72    }
73    console.info('[mediaLibrary] publicPath is ' + publicPath);
74    let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
75    if (dataUri != undefined) {
76        let args = dataUri.id.toString();
77        let fetchOp = {
78            selections: fileKeyObj.ID + "=?",
79            selectionArgs: [args],
80        }
81        let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
82        fdObject.fileAsset = await fetchFileResult.getAllObject();
83        fdObject.fdNumber = await fdObject.fileAsset[0].open('rw');
84        console.info('case getFd number is: ' + fdObject.fdNumber);
85    }
86    return fdObject;
87}
88
89// File operation
90export async function getFileDescriptor(fileName) {
91    let fileDescriptor = undefined;
92    await resourceManager.getResourceManager().then(async (mgr) => {
93        await mgr.getRawFileDescriptor(fileName).then(value => {
94            fileDescriptor = { fd: value.fd, offset: value.offset, length: value.length };
95            console.log('case getRawFileDescriptor success fileName: ' + fileName);
96        }).catch(error => {
97            console.log('case getRawFileDescriptor err: ' + error);
98        });
99    });
100    return fileDescriptor;
101}
102export async function getStageFileDescriptor(fileName) {
103    let fileDescriptor = undefined;
104    let mgr = globalThis.abilityContext.resourceManager
105    await mgr.getRawFileDescriptor(fileName).then(value => {
106        fileDescriptor = { fd: value.fd, offset: value.offset, length: value.length };
107        console.log('case getRawFileDescriptor success fileName: ' + fileName);
108    }).catch(error => {
109        console.log('case getRawFileDescriptor err: ' + error);
110    });
111    return fileDescriptor;
112}
113export async function closeFileDescriptor(fileName) {
114    await resourceManager.getResourceManager().then(async (mgr) => {
115        await mgr.closeRawFileDescriptor(fileName).then(() => {
116            console.log('case closeRawFileDescriptor ' + fileName);
117        }).catch(error => {
118            console.log('case closeRawFileDescriptor err: ' + error);
119        });
120    });
121}
122
123export function isFileOpen(fileDescriptor, done) {
124    if (fileDescriptor == undefined) {
125        expect().assertFail();
126        console.info('case error fileDescriptor undefined, open file fail');
127        done();
128    }
129}
130
131export async function getFdRead(pathName, done) {
132    let fdReturn;
133    await context.getFilesDir().then((fileDir) => {
134        console.info("case file dir is" + JSON.stringify(fileDir));
135        pathName = fileDir + '/' + pathName;
136        console.info("case pathName is" + pathName);
137    });
138    await fileio.open(pathName).then((fdNumber) => {
139        isFileOpen(fdNumber, done)
140        fdReturn = fdNumber;
141        console.info('[fileio]case open fd success, fd is ' + fdReturn);
142    })
143    return fdReturn;
144}
145
146export async function closeFdNumber(fdNumber) {
147    await fileio.close(fdNumber);
148}
149
150// wait synchronously
151export function msleep(time) {
152    for (let t = Date.now(); Date.now() - t <= time;);
153}
154
155// wait asynchronously
156export async function msleepAsync(ms) {
157    return new Promise((resolve) => setTimeout(resolve, ms));
158}
159
160export function printError(error, done) {
161    expect().assertFail();
162    console.info(`case error called,errMessage is ${error.message}`);
163    done();
164}
165
166export function assertErr(opera, err, done) {
167    console.info(`case ${opera} error,errMessage is ${err.message}`);
168    expect().assertFail();
169    done();
170}
171
172// callback function for promise call back error
173export function failureCallback(error) {
174    expect().assertFail();
175    console.info(`case error called,errMessage is ${error.message}`);
176}
177
178// callback function for promise catch error
179export function catchCallback(error) {
180    expect().assertFail();
181    console.info(`case error called,errMessage is ${error.message}`);
182}
183
184export function checkDescription(actualDescription, descriptionKey, descriptionValue) {
185    for (let i = 0; i < descriptionKey.length; i++) {
186        let property = actualDescription[descriptionKey[i]];
187        console.info('case key is  ' + descriptionKey[i]);
188        console.info('case actual value is  ' + property);
189        console.info('case hope value is  ' + descriptionValue[i]);
190        if (descriptionKey[i] == 'codec_mime') {
191            expect(property).assertEqual(CODECMIMEVALUE[descriptionValue[i]]);
192        } else {
193            expect(property).assertEqual(descriptionValue[i]);
194        }
195
196    }
197}
198
199export function checkOldDescription(actualDescription, descriptionKey, descriptionValue) {
200    for (let i = 0; i < descriptionKey.length; i++) {
201        let property = actualDescription[descriptionKey[i]];
202        console.info('case key is  ' + descriptionKey[i]);
203        console.info('case actual value is  ' + property);
204        console.info('case hope value is  ' + descriptionValue[i]);
205        expect(property).assertEqual(descriptionValue[i]);
206    }
207}
208
209export function printDescription(obj) {
210    let description = "";
211    for (let i in obj) {
212        let property = obj[i];
213        console.info('case key is  ' + i);
214        console.info('case value is  ' + property);
215        description += i + " = " + property + "\n";
216    }
217}
218
219export async function toNewPage(pagePath1, pagePath2, page) {
220    let path = '';
221    if (page == 0) {
222        path = pagePath1;
223    } else {
224        path = pagePath2;
225    }
226    let options = {
227        uri: path,
228    }
229    try {
230        await router.push(options);
231    } catch {
232        console.info('case route failed');
233    }
234}
235
236export async function clearRouter() {
237    await router.clear();
238}
239
240export async function getFd(pathName) {
241    let fdObject = {
242        fileAsset: null,
243        fdNumber: null
244    }
245    let displayName = pathName;
246    const mediaTest = mediaLibrary.getMediaLibrary();
247    let fileKeyObj = mediaLibrary.FileKey;
248    let mediaType = mediaLibrary.MediaType.VIDEO;
249    let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
250    let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
251    if (dataUri != undefined) {
252        let args = dataUri.id.toString();
253        let fetchOp = {
254            selections: fileKeyObj.ID + "=?",
255            selectionArgs: [args],
256        }
257        let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
258        fdObject.fileAsset = await fetchFileResult.getAllObject();
259        fdObject.fdNumber = await fdObject.fileAsset[0].open('rw');
260        console.info('case getFd number is: ' + fdObject.fdNumber);
261    }
262    return fdObject;
263}
264
265export async function getAudioFd(pathName) {
266    let fdObject = {
267        fileAsset: null,
268        fdNumber: null
269    }
270    let displayName = pathName;
271    const mediaTest = mediaLibrary.getMediaLibrary();
272    let fileKeyObj = mediaLibrary.FileKey;
273    let mediaType = mediaLibrary.MediaType.AUDIO;
274    let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_AUDIO);
275    let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
276    if (dataUri != undefined) {
277        let args = dataUri.id.toString();
278        let fetchOp = {
279            selections: fileKeyObj.ID + "=?",
280            selectionArgs: [args],
281        }
282        let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
283        fdObject.fileAsset = await fetchFileResult.getAllObject();
284        fdObject.fdNumber = await fdObject.fileAsset[0].open('rw');
285        console.info('case getFd number is: ' + fdObject.fdNumber);
286    }
287    return fdObject;
288}
289
290export async function closeFd(fileAsset, fdNumber) {
291    if (fileAsset != null) {
292        await fileAsset[0].close(fdNumber).then(() => {
293            console.info('[mediaLibrary] case close fd success');
294        }).catch((err) => {
295            console.info('[mediaLibrary] case close fd failed');
296        });
297    } else {
298        console.info('[mediaLibrary] case fileAsset is null');
299    }
300}
301