From 9783c81c59b01a087c9d17209103f66d220a4c15 Mon Sep 17 00:00:00 2001 From: zhang_hanyong Date: Thu, 25 Apr 2024 14:39:49 +0800 Subject: [PATCH] =?UTF-8?q?ImageKnife=E5=8A=A0=E8=BD=BD=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E6=A0=88=E8=A1=A5=E5=85=85XTS=E7=94=A8=E4=BE=8B=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E4=BC=98=E5=8C=96=E6=8C=89=E9=92=AE=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhang_hanyong --- .../testCustomDataFetchClientWithPage.ets | 13 ++- entry/src/ohosTest/ets/test/List.test.ets | 2 + .../ets/test/customdatafetchclient.test.ets | 104 ++++++++++++++++++ 3 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 entry/src/ohosTest/ets/test/customdatafetchclient.test.ets diff --git a/entry/src/main/ets/pages/testCustomDataFetchClientWithPage.ets b/entry/src/main/ets/pages/testCustomDataFetchClientWithPage.ets index c2a82f0..1451414 100644 --- a/entry/src/main/ets/pages/testCustomDataFetchClientWithPage.ets +++ b/entry/src/main/ets/pages/testCustomDataFetchClientWithPage.ets @@ -79,6 +79,8 @@ struct TestCustomDataFetchClientWithPage { @State isSingleImageVisible: boolean = true; @State isAllImageVisible: boolean = false; @State isCustom: boolean = false; + private isAllImageAdded: boolean = false; + private isCancelImageAdded: boolean = false; private data: Array = [ "http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg", "http://a.hiphotos.baidu.com/image/pic/item/e824b899a9014c087eb617650e7b02087af4f464.jpg" @@ -122,8 +124,10 @@ struct TestCustomDataFetchClientWithPage { this.isAllImageVisible = true; ImageKnifeGlobal.getInstance().getImageKnife()?.replaceDataFetch(new CustomDataFetchClient()); - this.hotCommendList.addData(this.hotCommendList.totalCount(), this.addData) - + if (!this.isAllImageAdded) { + this.hotCommendList.addData(this.hotCommendList.totalCount(), this.addData); + this.isAllImageAdded = true; + } }) Button("取消自定义全部图片").margin(16).onClick(() => { @@ -132,7 +136,10 @@ struct TestCustomDataFetchClientWithPage { this.isAllImageVisible = true; ImageKnifeGlobal.getInstance().getImageKnife()?.replaceDataFetch(new DownloadClient()); - this.hotCommendList.addData(this.hotCommendList.totalCount(), this.cancelData) + if (!this.isCancelImageAdded) { + this.hotCommendList.addData(this.hotCommendList.totalCount(), this.cancelData); + this.isCancelImageAdded = true; + } }) } diff --git a/entry/src/ohosTest/ets/test/List.test.ets b/entry/src/ohosTest/ets/test/List.test.ets index 9d986c8..3fc9777 100644 --- a/entry/src/ohosTest/ets/test/List.test.ets +++ b/entry/src/ohosTest/ets/test/List.test.ets @@ -21,6 +21,7 @@ import ImageKnifeTest from './imageknife.test' import DiskLruCacheTest from './diskLruCache.test' import SendableDataTest from './SendableData.test' import DefaultJobQueueTest from './DefaultJobQueueTest.test'; +import CustomDataFetchClientTest from './customdatafetchclient.test' export default function testsuite() { abilityTest() @@ -32,4 +33,5 @@ export default function testsuite() { ImageKnifeTest(); SendableDataTest(); DefaultJobQueueTest(); + CustomDataFetchClientTest(); } \ No newline at end of file diff --git a/entry/src/ohosTest/ets/test/customdatafetchclient.test.ets b/entry/src/ohosTest/ets/test/customdatafetchclient.test.ets new file mode 100644 index 0000000..50fa88c --- /dev/null +++ b/entry/src/ohosTest/ets/test/customdatafetchclient.test.ets @@ -0,0 +1,104 @@ +/* + * 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()); + } + let context: object | undefined = GlobalContext.getInstance().getObject("hapContext"); + let result = await client.loadData(request); + if (context != undefined) { + 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]); + } + }); + }) +} +