107 lines
5.0 KiB
Plaintext
107 lines
5.0 KiB
Plaintext
/*
|
|
* Copyright (C) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
|
|
import { DownloadClient } from '@ohos/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient';
|
|
import common from '@ohos.app.ability.common';
|
|
import { GlobalContext } from '../testability/GlobalContext';
|
|
import { CustomDataFetchClient, DataFetchResult, ImageKnifeGlobal, RequestOption } from '@ohos/imageknife';
|
|
|
|
const BASE_COUNT: number = 2000;
|
|
|
|
export default function CustomDataFetchClientTest() {
|
|
describe('CustomDataFetchClientTest', () => {
|
|
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
|
beforeAll(() => {
|
|
// Presets an action, which is performed only once before all test cases of the test suite start.
|
|
// This API supports only one parameter: preset action function.
|
|
})
|
|
beforeEach(() => {
|
|
// Presets an action, which is performed before each unit test case starts.
|
|
// The number of execution times is the same as the number of test cases defined by **it**.
|
|
// This API supports only one parameter: preset action function.
|
|
})
|
|
afterEach(() => {
|
|
// Presets a clear action, which is performed after each unit test case ends.
|
|
// The number of execution times is the same as the number of test cases defined by **it**.
|
|
// This API supports only one parameter: clear action function.
|
|
})
|
|
afterAll(() => {
|
|
// Presets a clear action, which is performed after all test cases of the test suite end.
|
|
// This API supports only one parameter: clear action function.
|
|
})
|
|
it('TestIsLocalLoadSrc', 0, () => {
|
|
let path = 'invalid path';
|
|
let client = new DownloadClient()
|
|
expect(client.isLocalLoadSrc(undefined, path)).assertFalse();
|
|
let context: object | undefined = GlobalContext.getInstance().getObject("hapContext");
|
|
if (context != undefined) {
|
|
let loadSrc1 = (context as common.UIAbilityContext).filesDir + 'a.jpg';
|
|
let loadSrc2 = (context as common.UIAbilityContext).cacheDir + 'b.jpg';
|
|
expect(client.isLocalLoadSrc(context, loadSrc1)).assertTrue();
|
|
expect(client.isLocalLoadSrc(context, loadSrc2)).assertTrue();
|
|
}
|
|
})
|
|
it('TestLoadData', 1, async () => {
|
|
let client = new CustomDataFetchClient();
|
|
let request = new RequestOption();
|
|
request.loadSrc = $r('app.media.icon');
|
|
let error = (await client.loadData(request) as DataFetchResult).error as String;
|
|
expect(error).assertEqual('CustomDataFetchClient request or loadSrc error.');
|
|
})
|
|
it('TestLoadData_customGetImage', 2, async () => {
|
|
let client = new CustomDataFetchClient();
|
|
let request = new RequestOption();
|
|
request.loadSrc = 'http://e.hiphotos.baidu.com/image/pic/item/4e4a20a4462309f7e41f5cfe760e0cf3d6cad6ee.jpg';
|
|
request.customGetImage = (context: Context, src: string) => {
|
|
// 这里是模拟的customGetImage逻辑
|
|
return Promise.resolve(new DataFetchResult());
|
|
}
|
|
console.log('LXH', 'TestLoadData 2 --1 customGetImage is undefined ?' + (request.customGetImage == undefined));
|
|
let context: object | undefined = GlobalContext.getInstance().getObject("hapContext");
|
|
let result = await client.loadData(request);
|
|
if (context != undefined) {
|
|
console.log('LXH', 'TestLoadData 2 --2');
|
|
expect(typeof result)
|
|
.assertEqual(typeof (await request?.customGetImage(context as common.UIAbilityContext, request.loadSrc)));
|
|
}
|
|
})
|
|
it('TestLoadData_combineArrayBuffers', 3, () => {
|
|
// 创建几个ArrayBuffer作为测试数据
|
|
const arrayBuffer1 = new ArrayBuffer(4);
|
|
const uint8Array1 = new Uint8Array(arrayBuffer1);
|
|
uint8Array1[0] = 1;
|
|
uint8Array1[1] = 2;
|
|
uint8Array1[2] = 3;
|
|
uint8Array1[3] = 4;
|
|
|
|
const arrayBuffer2 = new ArrayBuffer(2);
|
|
const uint8Array2 = new Uint8Array(arrayBuffer2);
|
|
uint8Array2[0] = 5;
|
|
uint8Array2[1] = 6;
|
|
let client = new CustomDataFetchClient();
|
|
const combinedArrayBuffer = client.combineArrayBuffers([arrayBuffer1, arrayBuffer2]);
|
|
expect(combinedArrayBuffer.byteLength).assertEqual(6);
|
|
const combinedUint8Array = new Uint8Array(combinedArrayBuffer);
|
|
for (let i = 0; i < 4; i++) {
|
|
expect(combinedUint8Array[i]).assertEqual(uint8Array1[i]);
|
|
}
|
|
for (let i = 0; i < 2; i++) {
|
|
expect(combinedUint8Array[i + 4]).assertEqual(uint8Array2[i]);
|
|
}
|
|
});
|
|
})
|
|
}
|
|
|