diff --git a/CHANGELOG.md b/CHANGELOG.md index 3715d2f..2a5ad77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## 3.0.0-rc.5 - 图片加载事件增加请求开始的回调,以及修复有缓存时,没有回调的bug - 修复对已销毁组件不再下发请求的逻辑 +- 加载图片流程添加日志 +- 子线程写入文件缓存获取buffer优化 +- 成功回调增加返回图片分辨率宽高 ## 3.0.0-rc.4 - 支持hsp多包图片资源 diff --git a/build-profile.json5 b/build-profile.json5 index d2d4041..90f6b5d 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -7,8 +7,8 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "4.1.0(11)", - "compatibleSdkVersion": "4.1.0(11)", + "compileSdkVersion": "5.0.0(12)", + "compatibleSdkVersion": "5.0.0(12)", "runtimeOS": "HarmonyOS", } ], @@ -37,6 +37,18 @@ { "name": "library", "srcPath": "./library" + }, + { + "name": "sharedlibrary", + "srcPath": "./sharedlibrary", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] } ] } \ No newline at end of file diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 index 62b4158..c88bea1 100644 --- a/entry/oh-package.json5 +++ b/entry/oh-package.json5 @@ -6,7 +6,7 @@ "author": "", "license": "", "dependencies": { - "@ohos/imageknife": "file:../library" + "@ohos/libraryimageknife": "file:../sharedlibrary", } } diff --git a/entry/src/main/ets/common/CustomEngineKeyImpl.ets b/entry/src/main/ets/common/CustomEngineKeyImpl.ets index f780cd6..4354e1c 100644 --- a/entry/src/main/ets/common/CustomEngineKeyImpl.ets +++ b/entry/src/main/ets/common/CustomEngineKeyImpl.ets @@ -12,9 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { IEngineKey, ImageKnifeOption, PixelMapTransformation } from '@ohos/imageknife'; -import { SparkMD5 } from '@ohos/imageknife/src/main/ets/3rd_party/sparkmd5/spark-md5'; -import { ImageKnifeRequestSource } from '@ohos/imageknife/src/main/ets/model/ImageKnifeData'; +import { IEngineKey, ImageKnifeOption, PixelMapTransformation,SparkMD5 ,ImageKnifeRequestSource} from '@ohos/libraryimageknife'; //全局自定义key demo @Sendable diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index ee7fe45..9817874 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -17,7 +17,7 @@ import hilog from '@ohos.hilog'; import UIAbility from '@ohos.app.ability.UIAbility'; import Want from '@ohos.app.ability.Want'; import window from '@ohos.window'; -import { ImageKnife, LogUtil } from '@ohos/imageknife'; +import { ImageKnife, InitImageKnife, LogUtil } from '@ohos/libraryimageknife'; import { CustomEngineKeyImpl } from '../common/CustomEngineKeyImpl'; export default class EntryAbility extends UIAbility { @@ -35,11 +35,12 @@ export default class EntryAbility extends UIAbility { LogUtil.mLogLevel = LogUtil.ALL // 初始化ImageKnife的文件缓存 - await ImageKnife.getInstance().initFileCache(this.context, 256, 256 * 1024 * 1024) + await InitImageKnife.init(this.context) ImageKnife.getInstance().setEngineKeyImpl(new CustomEngineKeyImpl()) // 全局配置请求头 ImageKnife.getInstance().addHeader('refer', "http://1.94.37.200:7070/AntiTheftChain/downloadImage"); ImageKnife.getInstance().deleteHeader('refer'); + windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); diff --git a/entry/src/main/ets/pages/ImageTransformation.ets b/entry/src/main/ets/pages/ImageTransformation.ets index 4cbbcba..ed863ca 100644 --- a/entry/src/main/ets/pages/ImageTransformation.ets +++ b/entry/src/main/ets/pages/ImageTransformation.ets @@ -1,6 +1,6 @@ import { BlurTransformation, BrightnessTransformation, ImageKnifeComponent, ImageKnifeOption, MultiTransTransformation, - PixelMapTransformation } from '@ohos/imageknife' + PixelMapTransformation } from '@ohos/libraryimageknife' import { collections } from '@kit.ArkTS' @Entry diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 4593079..34d9e90 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -26,6 +26,12 @@ struct Index { build() { Column() { + Button("测试HSP场景预加载").onClick(()=>{ + router.push({ + uri: 'pages/TestHspPreLoadImage', + + }); + }) Button("单个图片使用").onClick(()=>{ router.push({ uri: 'pages/SingleImage', diff --git a/entry/src/main/ets/pages/ListPage.ets b/entry/src/main/ets/pages/ListPage.ets index b56932c..c604d97 100644 --- a/entry/src/main/ets/pages/ListPage.ets +++ b/entry/src/main/ets/pages/ListPage.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/imageknife'; +import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/libraryimageknife'; @Entry @Component diff --git a/entry/src/main/ets/pages/LoadStatePage.ets b/entry/src/main/ets/pages/LoadStatePage.ets index d12051d..0f7c2a1 100644 --- a/entry/src/main/ets/pages/LoadStatePage.ets +++ b/entry/src/main/ets/pages/LoadStatePage.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent, ImageKnifeOption } from "@ohos/imageknife" +import { ImageKnifeComponent, ImageKnifeOption } from "@ohos/libraryimageknife" import matrix4 from '@ohos.matrix4' @Entry diff --git a/entry/src/main/ets/pages/LongImagePage.ets b/entry/src/main/ets/pages/LongImagePage.ets index 930396b..c8f6e1f 100644 --- a/entry/src/main/ets/pages/LongImagePage.ets +++ b/entry/src/main/ets/pages/LongImagePage.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent } from '@ohos/imageknife' +import { ImageKnifeComponent } from '@ohos/libraryimageknife' @Entry @Component diff --git a/entry/src/main/ets/pages/ManyPhotoShowPage.ets b/entry/src/main/ets/pages/ManyPhotoShowPage.ets index 58ec187..0719141 100644 --- a/entry/src/main/ets/pages/ManyPhotoShowPage.ets +++ b/entry/src/main/ets/pages/ManyPhotoShowPage.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent } from '@ohos/imageknife'; +import { ImageKnifeComponent } from '@ohos/libraryimageknife'; diff --git a/entry/src/main/ets/pages/ObjectFitPage.ets b/entry/src/main/ets/pages/ObjectFitPage.ets index 22665b0..02f7f8c 100644 --- a/entry/src/main/ets/pages/ObjectFitPage.ets +++ b/entry/src/main/ets/pages/ObjectFitPage.ets @@ -1,4 +1,4 @@ -import { ImageKnife, ImageKnifeComponent, ImageKnifeOption } from '@ohos/imageknife' +import { ImageKnife, ImageKnifeComponent, ImageKnifeOption } from '@ohos/libraryimageknife' @Entry @Component diff --git a/entry/src/main/ets/pages/SignatureTestPage.ets b/entry/src/main/ets/pages/SignatureTestPage.ets index 445dc26..42f49e4 100644 --- a/entry/src/main/ets/pages/SignatureTestPage.ets +++ b/entry/src/main/ets/pages/SignatureTestPage.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/imageknife'; +import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/libraryimageknife'; @Entry diff --git a/entry/src/main/ets/pages/SingleImage.ets b/entry/src/main/ets/pages/SingleImage.ets index 80ab7c1..c71f7bf 100644 --- a/entry/src/main/ets/pages/SingleImage.ets +++ b/entry/src/main/ets/pages/SingleImage.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent,BlurTransformation } from '@ohos/imageknife'; +import { ImageKnifeComponent,BlurTransformation } from '@ohos/libraryimageknife'; import fs from '@ohos.file.fs'; import image from '@ohos.multimedia.image'; diff --git a/entry/src/main/ets/pages/TestHeader.ets b/entry/src/main/ets/pages/TestHeader.ets index 93670a6..9be198e 100644 --- a/entry/src/main/ets/pages/TestHeader.ets +++ b/entry/src/main/ets/pages/TestHeader.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent,ImageKnife,ImageKnifeOption } from '@ohos/imageknife' +import { ImageKnifeComponent,ImageKnifeOption } from '@ohos/libraryimageknife' @Entry @Component diff --git a/entry/src/main/ets/pages/TestHspPreLoadImage.ets b/entry/src/main/ets/pages/TestHspPreLoadImage.ets new file mode 100644 index 0000000..16ff464 --- /dev/null +++ b/entry/src/main/ets/pages/TestHspPreLoadImage.ets @@ -0,0 +1,25 @@ +/* + * 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 { IndexComponent } from "@ohos/libraryimageknife" + +@Entry +@Component +struct TestHspPreLoadImage { + build() { + Column() { + IndexComponent() + }.width("100%").height('100%') + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/TestImageFlash.ets b/entry/src/main/ets/pages/TestImageFlash.ets index cd1ab28..c192a2b 100644 --- a/entry/src/main/ets/pages/TestImageFlash.ets +++ b/entry/src/main/ets/pages/TestImageFlash.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent } from '@ohos/imageknife/src/main/ets/components/ImageKnifeComponent' +import { ImageKnifeComponent } from '@ohos/libraryimageknife' @Observed export class MsgModel { diff --git a/entry/src/main/ets/pages/TestIsUrlExist.ets b/entry/src/main/ets/pages/TestIsUrlExist.ets index 4efc4b8..1225edc 100644 --- a/entry/src/main/ets/pages/TestIsUrlExist.ets +++ b/entry/src/main/ets/pages/TestIsUrlExist.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent, ImageKnife, ImageKnifeOption, CacheStrategy } from '@ohos/imageknife' +import { ImageKnifeComponent, ImageKnife, ImageKnifeOption, CacheStrategy } from '@ohos/libraryimageknife' @Entry @Component diff --git a/entry/src/main/ets/pages/TestPrefetchToFileCache.ets b/entry/src/main/ets/pages/TestPrefetchToFileCache.ets index 9dfef1f..d6b9e23 100644 --- a/entry/src/main/ets/pages/TestPrefetchToFileCache.ets +++ b/entry/src/main/ets/pages/TestPrefetchToFileCache.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent,ImageKnife,ImageKnifeOption } from '@ohos/imageknife' +import { ImageKnifeComponent,ImageKnife,ImageKnifeOption } from '@ohos/libraryimageknife' @Entry @Component diff --git a/entry/src/main/ets/pages/TestWriteCacheStage.ets b/entry/src/main/ets/pages/TestWriteCacheStage.ets index 43cd10a..6b0b203 100644 --- a/entry/src/main/ets/pages/TestWriteCacheStage.ets +++ b/entry/src/main/ets/pages/TestWriteCacheStage.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent,CacheStrategy,ImageKnifeOption } from '@ohos/imageknife' +import { ImageKnifeComponent,CacheStrategy,ImageKnifeOption } from '@ohos/libraryimageknife' @Entry @Component diff --git a/entry/src/main/ets/pages/TransformPage.ets b/entry/src/main/ets/pages/TransformPage.ets index b462c4c..7674156 100644 --- a/entry/src/main/ets/pages/TransformPage.ets +++ b/entry/src/main/ets/pages/TransformPage.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ImageKnifeComponent, ImageKnifeOption } from "@ohos/imageknife" +import { ImageKnifeComponent, ImageKnifeOption } from "@ohos/libraryimageknife" import matrix4 from '@ohos.matrix4' diff --git a/entry/src/main/ets/pages/User.ets b/entry/src/main/ets/pages/User.ets index 9309648..02e5cc0 100644 --- a/entry/src/main/ets/pages/User.ets +++ b/entry/src/main/ets/pages/User.ets @@ -1,4 +1,4 @@ -import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/imageknife' +import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/libraryimageknife' // const logger = new imUtils.logger.IMLogger('Avatar') diff --git a/entry/src/main/resources/base/media/rabbit.gif b/entry/src/main/resources/base/media/rabbit.gif new file mode 100644 index 0000000..c2c1402 Binary files /dev/null and b/entry/src/main/resources/base/media/rabbit.gif differ diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index bfefb27..e6d7674 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -15,6 +15,7 @@ "pages/ImageTransformation", "pages/ObjectFitPage", "pages/TestWriteCacheStage", - "pages/LoadStatePage" + "pages/LoadStatePage", + "pages/TestHspPreLoadImage" ] } \ No newline at end of file diff --git a/library/index.ets b/library/index.ets index d0f552e..bbeed44 100644 --- a/library/index.ets +++ b/library/index.ets @@ -12,7 +12,7 @@ export { LogUtil } from './src/main/ets/utils/LogUtil' export { IEngineKey } from './src/main/ets/key/IEngineKey' -export { ImageKnifeData , CacheStrategy} from "./src/main/ets/model/ImageKnifeData" +export { ImageKnifeData , CacheStrategy , ImageKnifeRequestSource} from "./src/main/ets/model/ImageKnifeData" export { PixelMapTransformation } from './src/main/ets/transform/PixelMapTransformation' @@ -22,4 +22,6 @@ export { BrightnessTransformation } from './src/main/ets/transform/BrightnessTra export { BlurTransformation } from './src/main/ets/transform/BlurTransformation' +export { SparkMD5 } from "./src/main/ets/3rd_party/sparkmd5/spark-md5" + diff --git a/library/src/main/ets/ImageKnife.ets b/library/src/main/ets/ImageKnife.ets index 6e01ae1..3a3a372 100644 --- a/library/src/main/ets/ImageKnife.ets +++ b/library/src/main/ets/ImageKnife.ets @@ -25,6 +25,7 @@ import { FileTypeUtil } from './utils/FileTypeUtil'; import { util } from '@kit.ArkTS'; import { image } from '@kit.ImageKit'; import { common } from '@kit.AbilityKit'; +import { LogUtil } from './utils/LogUtil'; export class ImageKnife { @@ -145,6 +146,7 @@ export class ImageKnife { } else { imageKnifeOption.loadSrc = loadSrc; } + LogUtil.log("ImageKnife_DataTime_preLoadCache-imageKnifeOption:"+loadSrc) let fileKey = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature) let cachePath = ImageKnife.getInstance().getFileCache().getFileToPath(fileKey) if (cachePath == null || cachePath == "" || cachePath == undefined) { @@ -299,10 +301,12 @@ export class ImageKnife { } async execute(request: ImageKnifeRequest): Promise { + LogUtil.log("ImageKnife_DataTime_execute.start:"+request.imageKnifeOption.loadSrc) if (this.headerMap.size > 0) { request.addHeaderMap(this.headerMap) } this.dispatcher.enqueue(request) + LogUtil.log("ImageKnife_DataTime_execute.end:"+request.imageKnifeOption.loadSrc) } setMaxRequests(concurrency: number): void { diff --git a/library/src/main/ets/ImageKnifeDispatcher.ets b/library/src/main/ets/ImageKnifeDispatcher.ets index 34d5810..0e099ea 100644 --- a/library/src/main/ets/ImageKnifeDispatcher.ets +++ b/library/src/main/ets/ImageKnifeDispatcher.ets @@ -51,6 +51,7 @@ export class ImageKnifeDispatcher { private engineKey: IEngineKey = new DefaultEngineKey(); showFromMemomry(request: ImageKnifeRequest, imageSrc: string | PixelMap | Resource, requestSource: ImageKnifeRequestSource): boolean { + LogUtil.log("ImageKnife_DataTime_showFromMemomry.start:"+request.imageKnifeOption.loadSrc) let memoryCache: ImageKnifeData | undefined; if ((typeof (request.imageKnifeOption.loadSrc as image.PixelMap).isEditable) == 'boolean') { memoryCache = { @@ -70,22 +71,27 @@ export class ImageKnifeDispatcher { // 回调请求开始 if (requestSource === ImageKnifeRequestSource.SRC && request.imageKnifeOption.onLoadListener?.onLoadStart !== undefined) { request.imageKnifeOption.onLoadListener?.onLoadStart() + LogUtil.log("ImageKnife_DataTime_MemoryCache_onLoadStart:"+request.imageKnifeOption.loadSrc) } - + LogUtil.log("ImageKnife_DataTime_MemoryCache_showPixelMap.start:"+request.imageKnifeOption.loadSrc) request.ImageKnifeRequestCallback?.showPixelMap(request.componentVersion, memoryCache.source, requestSource) + LogUtil.log("ImageKnife_DataTime_MemoryCache_showPixelMap.end:"+request.imageKnifeOption.loadSrc) if (requestSource == ImageKnifeRequestSource.SRC) { request.requestState = ImageKnifeRequestState.COMPLETE // 回调请求开结束 if (request.imageKnifeOption.onLoadListener?.onLoadSuccess !== undefined) { - request.imageKnifeOption.onLoadListener?.onLoadSuccess(memoryCache.source) + request.imageKnifeOption.onLoadListener?.onLoadSuccess(memoryCache.source,memoryCache.imageWidth,memoryCache.imageHeight) + LogUtil.log("ImageKnife_DataTime_MemoryCache_onLoadSuccess:"+request.imageKnifeOption.loadSrc) } } else if (requestSource == ImageKnifeRequestSource.ERROR_HOLDER) { request.requestState = ImageKnifeRequestState.ERROR } } + LogUtil.log("ImageKnife_DataTime_showFromMemomry.end_true:"+request.imageKnifeOption.loadSrc) return true } + LogUtil.log("ImageKnife_DataTime_showFromMemomry.end_false:"+request.imageKnifeOption.loadSrc) return false } @@ -106,6 +112,7 @@ export class ImageKnifeDispatcher { } executeJob(request: ImageKnifeRequest): void { + LogUtil.log("ImageKnife_DataTime_executeJob.start:"+request.imageKnifeOption.loadSrc) // 加载占位符 if (request.imageKnifeOption.placeholderSrc !== undefined) { if (this.showFromMemomry(request, request.imageKnifeOption.placeholderSrc, ImageKnifeRequestSource.PLACE_HOLDER) === false) { @@ -115,12 +122,14 @@ export class ImageKnifeDispatcher { // 加载主图 this.getAndShowImage(request, request.imageKnifeOption.loadSrc, ImageKnifeRequestSource.SRC) + LogUtil.log("ImageKnife_DataTime_executeJob.end:"+request.imageKnifeOption.loadSrc) } /** * 获取和显示图片 */ getAndShowImage(currentRequest: ImageKnifeRequest, imageSrc: string | PixelMap | Resource, requestSource: ImageKnifeRequestSource): void { + LogUtil.log("ImageKnife_DataTime_getAndShowImage.start:"+currentRequest.imageKnifeOption.loadSrc) let memoryKey: string = this.engineKey.generateMemoryKey(imageSrc, requestSource, currentRequest.imageKnifeOption) let requestList: List | undefined = this.executingJobMap.get(memoryKey) if (requestList == undefined) { @@ -147,7 +156,9 @@ export class ImageKnifeDispatcher { requestSource } // 启动线程下载和解码主图 + LogUtil.log("ImageKnife_DataTime_getAndShowImage_Task.start:"+currentRequest.imageKnifeOption.loadSrc) let task = new taskpool.Task(requestJob, request) + LogUtil.log("ImageKnife_DataTime_getAndShowImage_Task.end:"+currentRequest.imageKnifeOption.loadSrc) // 监听网络回调事件 if (currentRequest.imageKnifeOption.progressListener !== undefined && requestSource === ImageKnifeRequestSource.SRC) { let progressCallBack = currentRequest.imageKnifeOption.progressListener @@ -160,11 +171,13 @@ export class ImageKnifeDispatcher { requestList.forEach((requestWithSource: ImageKnifeRequestWithSource) => { if (requestWithSource.source === ImageKnifeRequestSource.SRC && requestWithSource.request.imageKnifeOption.onLoadListener?.onLoadStart !== undefined) { requestWithSource.request.imageKnifeOption.onLoadListener?.onLoadStart() + LogUtil.log("ImageKnife_DataTime_getAndShowImage_onLoadStart:"+currentRequest.imageKnifeOption.loadSrc) } }); - + LogUtil.log("ImageKnife_DataTime_getAndShowImage_execute.start:"+currentRequest.imageKnifeOption.loadSrc) taskpool.execute(task).then((res: Object) => { + LogUtil.log("ImageKnife_DataTime_getAndShowImage_CallBack.start:"+currentRequest.imageKnifeOption.loadSrc) let requestJobResult = res as RequestJobResult let pixelmap = requestJobResult === undefined ? undefined : requestJobResult.pixelMap if (pixelmap === undefined) { @@ -173,6 +186,7 @@ export class ImageKnifeDispatcher { // 回调请求失败 if (requestWithSource.source === ImageKnifeRequestSource.SRC && requestWithSource.request.imageKnifeOption.onLoadListener?.onLoadFailed !== undefined && requestJobResult.loadFail){ requestWithSource.request.imageKnifeOption.onLoadListener.onLoadFailed(requestJobResult.loadFail); + LogUtil.log("ImageKnife_DataTime_getAndShowImage_onLoadFailed:"+currentRequest.imageKnifeOption.loadSrc) } if (requestWithSource.source === ImageKnifeRequestSource.SRC && requestWithSource.request.imageKnifeOption.errorholderSrc !== undefined) { @@ -190,22 +204,24 @@ export class ImageKnifeDispatcher { } // 保存文件缓存 if (requestJobResult.bufferSize > 0 && currentRequest.imageKnifeOption.writeCacheStrategy !== CacheStrategy.Memory) { - LogUtil.log("ImageKnifeComponent writeCacheStrategy File") + LogUtil.log("ImageKnife_DataTime_getAndShowImage_saveWithoutWriteFile.start:"+currentRequest.imageKnifeOption.loadSrc) ImageKnife.getInstance().saveWithoutWriteFile(requestJobResult.fileKey, requestJobResult.bufferSize) + LogUtil.log("ImageKnife_DataTime_getAndShowImage_saveWithoutWriteFile.end:"+currentRequest.imageKnifeOption.loadSrc) } let ImageKnifeData: ImageKnifeData = { source: pixelmap!, - imageWidth: 0, - imageHeight: 0 + imageWidth: requestJobResult.size == undefined ? 0 : requestJobResult.size.width, + imageHeight: requestJobResult.size == undefined ? 0 : requestJobResult.size.height } // 保存内存缓存 if (currentRequest.imageKnifeOption.writeCacheStrategy !== CacheStrategy.File) { - LogUtil.log("ImageKnifeComponent writeCacheStrategy Memory") + LogUtil.log("ImageKnife_DataTime_getAndShowImage_saveMemoryCache.start:"+currentRequest.imageKnifeOption.loadSrc) ImageKnife.getInstance() .saveMemoryCache(this.engineKey.generateMemoryKey(imageSrc, requestSource, currentRequest.imageKnifeOption), ImageKnifeData) + LogUtil.log("ImageKnife_DataTime_getAndShowImage_saveMemoryCache.end:"+currentRequest.imageKnifeOption.loadSrc) } if (requestList !== undefined) { @@ -218,15 +234,17 @@ export class ImageKnifeDispatcher { // 画主图 if (requestWithSource.source === ImageKnifeRequestSource.SRC || requestWithSource.source === ImageKnifeRequestSource.ERROR_HOLDER || (requestWithSource.source === ImageKnifeRequestSource.PLACE_HOLDER && requestWithSource.request.requestState === ImageKnifeRequestState.PROGRESS)) { - + LogUtil.log("ImageKnife_DataTime_getAndShowImage_showPixelMap.start:"+currentRequest.imageKnifeOption.loadSrc) requestWithSource.request.ImageKnifeRequestCallback.showPixelMap(requestWithSource.request.componentVersion, ImageKnifeData.source, requestWithSource.source) + LogUtil.log("ImageKnife_DataTime_getAndShowImage_showPixelMap.end:"+currentRequest.imageKnifeOption.loadSrc) } if (requestWithSource.source == ImageKnifeRequestSource.SRC) { requestWithSource.request.requestState = ImageKnifeRequestState.COMPLETE if (requestWithSource.request.imageKnifeOption.onLoadListener && requestWithSource.request.imageKnifeOption.onLoadListener.onLoadSuccess) { // 回调请求成功 - requestWithSource.request.imageKnifeOption.onLoadListener.onLoadSuccess(ImageKnifeData.source); + requestWithSource.request.imageKnifeOption.onLoadListener.onLoadSuccess(ImageKnifeData.source,ImageKnifeData.imageWidth,ImageKnifeData.imageHeight); + LogUtil.log("ImageKnife_DataTime_getAndShowImage_onLoadSuccess:"+currentRequest.imageKnifeOption.loadSrc) } } else if (requestWithSource.source == ImageKnifeRequestSource.ERROR_HOLDER) { requestWithSource.request.requestState = ImageKnifeRequestState.ERROR @@ -241,12 +259,14 @@ export class ImageKnifeDispatcher { LogUtil.log("error: no requestlist need to draw for key = " + memoryKey) } // }) - + LogUtil.log("ImageKnife_DataTime_getAndShowImage_CallBack.end:"+currentRequest.imageKnifeOption.loadSrc) }); + LogUtil.log("ImageKnife_DataTime_getAndShowImage_execute.end:"+currentRequest.imageKnifeOption.loadSrc) + LogUtil.log("ImageKnife_DataTime_getAndShowImage.end:"+currentRequest.imageKnifeOption.loadSrc) } dispatchNextJob() { - + LogUtil.log("ImageKnife_DataTime_dispatchNextJob.start") while (true) { let request = this.jobQueue.pop() if (request === undefined) { @@ -254,6 +274,7 @@ export class ImageKnifeDispatcher { } else if (request.requestState === ImageKnifeRequestState.PROGRESS) { this.executeJob(request) + LogUtil.log("ImageKnife_DataTime_dispatchNextJob.end:" + request.imageKnifeOption.loadSrc) break } } @@ -282,6 +303,7 @@ export class ImageKnifeDispatcher { */ @Concurrent async function requestJob(request: RequestJobRequest): Promise { + LogUtil.log("ImageKnife_DataTime_requestJob.start:"+request.src) let resBuf: ArrayBuffer | undefined let bufferSize: number = 0 let loadError: string = ''; @@ -315,6 +337,7 @@ async function requestJob(request: RequestJobRequest): Promise() @@ -359,12 +382,14 @@ async function requestJob(request: RequestJobRequest): Promise { resPixelmap = pixelmap @@ -443,11 +472,13 @@ async function requestJob(request: RequestJobRequest): Promise void; // 请求成功 - onLoadSuccess?: (data: string | PixelMap | undefined) => void; + onLoadSuccess?: (data: string | PixelMap | undefined,width?:number,height?:number) => void; // 请求结束 onLoadFailed?: (err: string) => void; } \ No newline at end of file diff --git a/library/src/main/ets/model/ImageKnifeData.ets b/library/src/main/ets/model/ImageKnifeData.ets index f2abd25..97cff7b 100644 --- a/library/src/main/ets/model/ImageKnifeData.ets +++ b/library/src/main/ets/model/ImageKnifeData.ets @@ -17,6 +17,7 @@ import { ImageKnifeRequest } from '../ImageKnifeRequest' import { IEngineKey } from '../key/IEngineKey' import { PixelMapTransformation } from '../transform/PixelMapTransformation' import common from '@ohos.app.ability.common'; +import { Size } from '@kit.ArkUI' export interface ImageKnifeData { source: PixelMap | string, @@ -59,6 +60,7 @@ export interface RequestJobResult { bufferSize: number fileKey: string loadFail?: string, + size?:Size } /** diff --git a/library/src/main/ets/utils/MemoryLruCache.ets b/library/src/main/ets/utils/MemoryLruCache.ets index fa0d513..50b78af 100644 --- a/library/src/main/ets/utils/MemoryLruCache.ets +++ b/library/src/main/ets/utils/MemoryLruCache.ets @@ -108,6 +108,7 @@ export class MemoryLruCache implements IMemoryCache { this.currentMemory -= value.source.length } else { this.currentMemory -= value.source.getPixelBytesNumber(); + value.source.release() } // LogUtil.info("MemoryCache removeMemorySize: " + value.source.getPixelBytesNumber() + " currentMemory:" + this.currentMemory) } diff --git a/sharedlibrary/.gitignore b/sharedlibrary/.gitignore new file mode 100644 index 0000000..e2713a2 --- /dev/null +++ b/sharedlibrary/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/sharedlibrary/Index.ets b/sharedlibrary/Index.ets new file mode 100644 index 0000000..b25707a --- /dev/null +++ b/sharedlibrary/Index.ets @@ -0,0 +1,33 @@ +export { add } from './src/main/ets/utils/Calc' + +export { InitImageKnife } from "./src/main/ets/pages/InitImageKnife" + +export { IndexComponent } from "./src/main/ets/pages/Index" + +export { ImageKnifeComponent } from '@ohos/imageknife' + +export { ImageKnife } from '@ohos/imageknife' + +export { ImageKnifeOption } from '@ohos/imageknife' + +export { ImageKnifeRequest } from '@ohos/imageknife' + +export { FileUtils } from '@ohos/imageknife' + +export { LogUtil } from '@ohos/imageknife' + +export { IEngineKey } from '@ohos/imageknife' + +export { ImageKnifeData , CacheStrategy} from "@ohos/imageknife" + +export { PixelMapTransformation } from '@ohos/imageknife' + +export { MultiTransTransformation } from '@ohos/imageknife' + +export { BrightnessTransformation } from '@ohos/imageknife' + +export { BlurTransformation } from '@ohos/imageknife' + +export { SparkMD5 } from "@ohos/imageknife" + +export { ImageKnifeRequestSource } from "@ohos/imageknife" \ No newline at end of file diff --git a/sharedlibrary/build-profile.json5 b/sharedlibrary/build-profile.json5 new file mode 100644 index 0000000..170d980 --- /dev/null +++ b/sharedlibrary/build-profile.json5 @@ -0,0 +1,28 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": true, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest" + } + ] +} \ No newline at end of file diff --git a/sharedlibrary/hvigorfile.ts b/sharedlibrary/hvigorfile.ts new file mode 100644 index 0000000..d993120 --- /dev/null +++ b/sharedlibrary/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hspTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hspTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/sharedlibrary/obfuscation-rules.txt b/sharedlibrary/obfuscation-rules.txt new file mode 100644 index 0000000..985b2ae --- /dev/null +++ b/sharedlibrary/obfuscation-rules.txt @@ -0,0 +1,18 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope \ No newline at end of file diff --git a/sharedlibrary/oh-package.json5 b/sharedlibrary/oh-package.json5 new file mode 100644 index 0000000..65115c7 --- /dev/null +++ b/sharedlibrary/oh-package.json5 @@ -0,0 +1,12 @@ +{ + "name": "sharedlibrary", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "Index.ets", + "author": "", + "license": "Apache-2.0", + "packageType": "InterfaceHar", + "dependencies": { + "@ohos/imageknife": "file:../library" + } +} \ No newline at end of file diff --git a/sharedlibrary/src/main/ets/pages/Index.ets b/sharedlibrary/src/main/ets/pages/Index.ets new file mode 100644 index 0000000..ad35007 --- /dev/null +++ b/sharedlibrary/src/main/ets/pages/Index.ets @@ -0,0 +1,39 @@ +/* + * 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 { ImageKnife , ImageKnifeComponent ,ImageKnifeOption } from "@ohos/imageknife" + +@Component +export struct IndexComponent { + @State imageKnifeOption: ImageKnifeOption = { + loadSrc: $r('app.media.startIcon') + } + build() { + Column() { + Button("预加载").onClick((event: ClickEvent) => { + ImageKnife.getInstance() + .preLoadCache('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp') + .then((data) => { + console.log("preLoadImage_FileCache:" + data) + this.imageKnifeOption.loadSrc = data + }) + }) + ImageKnifeComponent({ + imageKnifeOption:this.imageKnifeOption + }).width(300).height(300) + } + .width('100%') + .height('100%') + } +} \ No newline at end of file diff --git a/sharedlibrary/src/main/ets/pages/InitImageKnife.ets b/sharedlibrary/src/main/ets/pages/InitImageKnife.ets new file mode 100644 index 0000000..405914c --- /dev/null +++ b/sharedlibrary/src/main/ets/pages/InitImageKnife.ets @@ -0,0 +1,21 @@ +/* + * 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 { ImageKnife } from '@ohos/imageknife' +import common from '@ohos.app.ability.common' +export class InitImageKnife{ + static async init(entryContext:common.UIAbilityContext){ + await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024) + } +} \ No newline at end of file diff --git a/sharedlibrary/src/main/ets/utils/Calc.ets b/sharedlibrary/src/main/ets/utils/Calc.ets new file mode 100644 index 0000000..e6b7cf2 --- /dev/null +++ b/sharedlibrary/src/main/ets/utils/Calc.ets @@ -0,0 +1,3 @@ +export function add(a:number, b:number) { + return a + b; +} \ No newline at end of file diff --git a/sharedlibrary/src/main/module.json5 b/sharedlibrary/src/main/module.json5 new file mode 100644 index 0000000..5ff0ab4 --- /dev/null +++ b/sharedlibrary/src/main/module.json5 @@ -0,0 +1,14 @@ +{ + "module": { + "name": "sharedlibrary", + "type": "shared", + "description": "$string:shared_desc", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "pages": "$profile:main_pages" + } +} \ No newline at end of file diff --git a/sharedlibrary/src/main/resources/base/element/color.json b/sharedlibrary/src/main/resources/base/element/color.json new file mode 100644 index 0000000..1bbc9aa --- /dev/null +++ b/sharedlibrary/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "white", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/sharedlibrary/src/main/resources/base/element/string.json b/sharedlibrary/src/main/resources/base/element/string.json new file mode 100644 index 0000000..98e1d8a --- /dev/null +++ b/sharedlibrary/src/main/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "shared_desc", + "value": "description" + } + ] +} \ No newline at end of file diff --git a/sharedlibrary/src/main/resources/base/profile/main_pages.json b/sharedlibrary/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000..1898d94 --- /dev/null +++ b/sharedlibrary/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/sharedlibrary/src/mock/mock-config.json5 b/sharedlibrary/src/mock/mock-config.json5 new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/sharedlibrary/src/mock/mock-config.json5 @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/ets/test/Ability.test.ets b/sharedlibrary/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000..85c78f6 --- /dev/null +++ b/sharedlibrary/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,35 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // 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('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/ets/test/List.test.ets b/sharedlibrary/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000..794c7dc --- /dev/null +++ b/sharedlibrary/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,5 @@ +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/ets/testability/TestAbility.ets b/sharedlibrary/src/ohosTest/ets/testability/TestAbility.ets new file mode 100644 index 0000000..3e04349 --- /dev/null +++ b/sharedlibrary/src/ohosTest/ets/testability/TestAbility.ets @@ -0,0 +1,47 @@ +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { abilityDelegatorRegistry } from '@kit.TestKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; +import { Hypium } from '@ohos/hypium'; +import testsuite from '../test/List.test'; + +export default class TestAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); + hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? ''); + let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator; + abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); + let abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs; + abilityDelegatorArguments = abilityDelegatorRegistry.getArguments(); + hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite); + } + + onDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate'); + windowStage.loadContent('testability/pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy'); + } + + onForeground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground'); + } + + onBackground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground'); + } +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/ets/testability/pages/Index.ets b/sharedlibrary/src/ohosTest/ets/testability/pages/Index.ets new file mode 100644 index 0000000..423b427 --- /dev/null +++ b/sharedlibrary/src/ohosTest/ets/testability/pages/Index.ets @@ -0,0 +1,17 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ets b/sharedlibrary/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ets new file mode 100644 index 0000000..713592f --- /dev/null +++ b/sharedlibrary/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ets @@ -0,0 +1,90 @@ +import { abilityDelegatorRegistry, TestRunner } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; +import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { resourceManager } from '@kit.LocalizationKit'; +import { util } from '@kit.ArkTS'; + +let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator; +let abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs; +let jsonPath: string = 'mock/mock-config.json'; +let tag: string = 'testTag'; + +async function onAbilityCreateCallback(data: UIAbility) { + hilog.info(0x0000, 'testTag', 'onAbilityCreateCallback, data: ${}', JSON.stringify(data)); +} + +async function addAbilityMonitorCallback(err: BusinessError) { + hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); +} + +export default class OpenHarmonyTestRunner implements TestRunner { + constructor() { + } + + onPrepare() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare'); + } + + async onRun() { + let tag = 'testTag'; + hilog.info(0x0000, tag, '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = abilityDelegatorRegistry.getArguments() + abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator() + let moduleName = abilityDelegatorArguments.parameters['-m']; + let context = abilityDelegator.getAppContext().getApplicationContext().createModuleContext(moduleName); + let mResourceManager = context.resourceManager; + await checkMock(abilityDelegator, mResourceManager); + const bundleName = abilityDelegatorArguments.bundleName; + const testAbilityName: string = 'TestAbility'; + let lMonitor: abilityDelegatorRegistry.AbilityMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + moduleName: moduleName + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) + const want: Want = { + bundleName: bundleName, + abilityName: testAbilityName, + moduleName: moduleName + }; + abilityDelegator.startAbility(want, (err: BusinessError, data: void) => { + hilog.info(0x0000, tag, 'startAbility : err : %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, tag, 'startAbility : data : %{public}s', JSON.stringify(data) ?? ''); + }) + hilog.info(0x0000, tag, '%{public}s', 'OpenHarmonyTestRunner onRun end'); + } +} + +async function checkMock(abilityDelegator: abilityDelegatorRegistry.AbilityDelegator, resourceManager: resourceManager.ResourceManager) { + let rawFile: Uint8Array; + try { + rawFile = resourceManager.getRawFileContentSync(jsonPath); + hilog.info(0x0000, tag, 'MockList file exists'); + let mockStr: string = util.TextDecoder.create('utf-8', { ignoreBOM: true }).decodeWithStream(rawFile); + let mockMap: Record = getMockList(mockStr); + try { + abilityDelegator.setMockList(mockMap) + } catch (error) { + let code = (error as BusinessError).code; + let message = (error as BusinessError).message; + hilog.error(0x0000, tag, `abilityDelegator.setMockList failed, error code: ${code}, message: ${message}.`); + } + } catch (error) { + let code = (error as BusinessError).code; + let message = (error as BusinessError).message; + hilog.error(0x0000, tag, `ResourceManager:callback getRawFileContent failed, error code: ${code}, message: ${message}.`); + } +} + +function getMockList(jsonStr: string) { + let jsonObj: Record = JSON.parse(jsonStr); + let map: Map = new Map(Object.entries(jsonObj)); + let mockList: Record = {}; + map.forEach((value: object, key: string) => { + let realValue: string = value['source'].toString(); + mockList[key] = realValue; + }); + hilog.info(0x0000, tag, '%{public}s', 'mock-json value:' + JSON.stringify(mockList) ?? ''); + return mockList; +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/module.json5 b/sharedlibrary/src/ohosTest/module.json5 new file mode 100644 index 0000000..4cca4a0 --- /dev/null +++ b/sharedlibrary/src/ohosTest/module.json5 @@ -0,0 +1,38 @@ +{ + "module": { + "name": "sharedlibrary_test", + "type": "feature", + "description": "$string:module_test_desc", + "mainElement": "TestAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:test_pages", + "abilities": [ + { + "name": "TestAbility", + "srcEntry": "./ets/testability/TestAbility.ets", + "description": "$string:TestAbility_desc", + "icon": "$media:icon", + "label": "$string:TestAbility_label", + "exported": true, + "startWindowIcon": "$media:icon", + "startWindowBackground": "$color:start_window_background", + "skills": [ + { + "actions": [ + "action.system.home" + ], + "entities": [ + "entity.system.home" + ] + } + ] + } + ] + } +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/resources/base/element/color.json b/sharedlibrary/src/ohosTest/resources/base/element/color.json new file mode 100644 index 0000000..3c71296 --- /dev/null +++ b/sharedlibrary/src/ohosTest/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/resources/base/element/string.json b/sharedlibrary/src/ohosTest/resources/base/element/string.json new file mode 100644 index 0000000..65d8fa5 --- /dev/null +++ b/sharedlibrary/src/ohosTest/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_test_desc", + "value": "test ability description" + }, + { + "name": "TestAbility_desc", + "value": "the test ability" + }, + { + "name": "TestAbility_label", + "value": "test label" + } + ] +} \ No newline at end of file diff --git a/sharedlibrary/src/ohosTest/resources/base/media/icon.png b/sharedlibrary/src/ohosTest/resources/base/media/icon.png new file mode 100644 index 0000000..a39445d Binary files /dev/null and b/sharedlibrary/src/ohosTest/resources/base/media/icon.png differ diff --git a/sharedlibrary/src/ohosTest/resources/base/profile/test_pages.json b/sharedlibrary/src/ohosTest/resources/base/profile/test_pages.json new file mode 100644 index 0000000..b7e7343 --- /dev/null +++ b/sharedlibrary/src/ohosTest/resources/base/profile/test_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "testability/pages/Index" + ] +} diff --git a/sharedlibrary/src/test/List.test.ets b/sharedlibrary/src/test/List.test.ets new file mode 100644 index 0000000..bb5b5c3 --- /dev/null +++ b/sharedlibrary/src/test/List.test.ets @@ -0,0 +1,5 @@ +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/sharedlibrary/src/test/LocalUnit.test.ets b/sharedlibrary/src/test/LocalUnit.test.ets new file mode 100644 index 0000000..165fc16 --- /dev/null +++ b/sharedlibrary/src/test/LocalUnit.test.ets @@ -0,0 +1,33 @@ +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // 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('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file