diff --git a/Drawing/ArkTSDrawing/.gitignore b/Drawing/ArkTSDrawing/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/Drawing/ArkTSDrawing/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/AppScope/app.json5 b/Drawing/ArkTSDrawing/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8633c11d6b7325d2674c1ba106e36dd80f1ce199 --- /dev/null +++ b/Drawing/ArkTSDrawing/AppScope/app.json5 @@ -0,0 +1,24 @@ +/* + * 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. + */ +{ + "app": { + "bundleName": "com.samples.drawing", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/Drawing/ArkTSDrawing/AppScope/resources/base/element/string.json b/Drawing/ArkTSDrawing/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..ca0ae1186dd237222874b85a60877356922c7703 --- /dev/null +++ b/Drawing/ArkTSDrawing/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "drawing" + } + ] +} diff --git a/Drawing/ArkTSDrawing/AppScope/resources/base/media/app_icon.png b/Drawing/ArkTSDrawing/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/Drawing/ArkTSDrawing/AppScope/resources/base/media/app_icon.png differ diff --git a/Drawing/ArkTSDrawing/README.md b/Drawing/ArkTSDrawing/README.md new file mode 100644 index 0000000000000000000000000000000000000000..57cf9b535f2d96b6f698cfb018bd46ed5894fedf --- /dev/null +++ b/Drawing/ArkTSDrawing/README.md @@ -0,0 +1,72 @@ +# 使用Drawing实现图形绘制与显示(ArkTS) + +## 介绍 + +本工程主要实现了对以下指南文档中[访问控制概述](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/AccessToken/access-token-overview.md)示例代码片段的工程化,主要目标是实现指南中示例代码需要与sample工程文件同源。 + +## 效果预览 + +| ![](screenshot/path.png) | ![](screenshot/rect.png) | ![](screenshot/text.png) | +| ------------------------ | ------------------------ | ------------------------ | + +使用说明: +1. 该工程可以选择在模拟器和开发板上运行。 +2. 点击构建,即可在生成的应用中点击对应的按钮进行图案的绘制。 +3. 进入”DocsSample/Drawing/ArkTSDrawing/entry/src/ohosTest/ets/test/DrawingAbility.test.ets“文件,可以对本项目进行UI的自动化测试。 + +## 工程目录 + +``` +ArkTSDrawing +├──entry/src/main +│ ├──ets // ets代码区 +| | ├──common +| | | ├──logger.ts // 日志类 +| | | ├──MyNodeController.ets // 节点控制类 +| | | ├──PathRenderNode.ets // 绘制路径类 +| | | ├──RectRenderNode.ets // 绘制矩形类 +| | | └──TextRenderNode.ets // 绘制文本类 +│ │ ├──entryability +| | | └──EntryAbility.ets // 程序入口类 +| | ├──entrybackupability +│ │ │ └──EntryBackupAbility.ets +│ │ └──pages // 页面文件 +│ │ └──Index.ets // 主界面 +| ├──resources // 资源文件目录 +``` + +## 具体实现 + +1. 创建`NodeController`的子类`MyNodeController`,并在其中定义创建`FrameNode`的函数。`NodeController`定义了节点容器的控制器,控制着容器里在生命周期中的节点。`FrameNode`定义了节点的基本类型,并包含一个`RenderNode`。 +2. 创建`RenderNode`子类`PathRenderNode`、`RectRenderNode`、`TextRenderNode`并在其中定义绘图函数。`RenderNode`中包含树结构的操作,以及对绘制属性的操作,其中`draw`方法会在`RenderNode`进行绘制时被调用。使用每个子类的`draw`方法可以实现五角星、举行和文本文字的绘制。 + +## 相关权限 + +无。 + +## 依赖 + +不涉及。 + +## 约束和限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.3 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.3 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.3 Release及以上。 + +## 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/Drawing/ArkTSDrawing/ > .git/info/sparse-checkout +git remote add origin https://gitcode.com/openharmony/applications_app_samples.git +git pull origin master +``` + diff --git a/Drawing/ArkTSDrawing/build-profile.json5 b/Drawing/ArkTSDrawing/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..aee4474e210500fab68ac997b9a9f220f1e35ed3 --- /dev/null +++ b/Drawing/ArkTSDrawing/build-profile.json5 @@ -0,0 +1,55 @@ +/* + * 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. + */ +{ + "app": { + "products": [ + { + "name": "default", + "signingConfig": "default", + "targetSdkVersion": "5.0.3(15)", + "compatibleSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug" + }, + { + "name": "release" + } + ], + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/code-linter.json5 b/Drawing/ArkTSDrawing/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3311c9280cd317b17b4b2745922184ac805051d8 --- /dev/null +++ b/Drawing/ArkTSDrawing/code-linter.json5 @@ -0,0 +1,34 @@ +/* + * 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. + */ +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/.gitignore b/Drawing/ArkTSDrawing/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/build-profile.json5 b/Drawing/ArkTSDrawing/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7fb94a437710d01ce28c7a151604f2d0a3b3d850 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/build-profile.json5 @@ -0,0 +1,42 @@ +/* + * 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. + */ +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/hvigorfile.ts b/Drawing/ArkTSDrawing/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..8fe7819925bcfb8281ffffa757993ecade94bcbc --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/hvigorfile.ts @@ -0,0 +1,20 @@ +/* + * 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 { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/Drawing/ArkTSDrawing/entry/obfuscation-rules.txt b/Drawing/ArkTSDrawing/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# 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://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# 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 + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/oh-package.json5 b/Drawing/ArkTSDrawing/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c2964381037433502863a5e14ed1c9ad5e267297 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/oh-package.json5 @@ -0,0 +1,24 @@ +/* + * 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. + */ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/Drawing/ArkTSDrawing/entry/src/main/ets/common/Logger.ts b/Drawing/ArkTSDrawing/entry/src/main/ets/common/Logger.ts new file mode 100644 index 0000000000000000000000000000000000000000..d30e495c69e1c316c332e9edd7d18f15ace05daf --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/ets/common/Logger.ts @@ -0,0 +1,52 @@ +/* + * 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 hilog from '@ohos.hilog' + +class Logger { + private domain: number + private prefix: string + private format: string = '%{public}s, %{public}s' + + constructor(prefix: string) { + this.prefix = prefix + this.domain = 0x0001 + } + + debug(...args: string[]) { + hilog.debug(this.domain, this.prefix, this.format, args) + } + + info(...args: string[]) { + hilog.info(this.domain, this.prefix, this.format, args) + } + + warn(...args: string[]) { + hilog.warn(this.domain, this.prefix, this.format, args) + } + + error(...args: string[]) { + hilog.error(this.domain, this.prefix, this.format, args) + } + + fatal(...args: string[]) { + hilog.fatal(this.domain, this.prefix, this.format, args) + } + + isLoggable(level: number) { + hilog.isLoggable(this.domain, this.prefix, level) + } +} + +export default new Logger('[Sample_ArkTSDrawing]') diff --git a/Drawing/ArkTSDrawing/entry/src/main/ets/common/MyNodeController.ets b/Drawing/ArkTSDrawing/entry/src/main/ets/common/MyNodeController.ets new file mode 100644 index 0000000000000000000000000000000000000000..39394daa3636dd46d4c3be0621c2d9a0cfbc4923 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/ets/common/MyNodeController.ets @@ -0,0 +1,61 @@ +/* + * 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 { FrameNode, NodeController, RenderNode } from '@kit.ArkUI'; + +class MyNodeController extends NodeController { + private rootNode: FrameNode | null = null; + + makeNode(uiContext: UIContext): FrameNode { + this.rootNode = new FrameNode(uiContext); + if (this.rootNode == null) { + return this.rootNode; + } + const renderNode = this.rootNode.getRenderNode() + if (renderNode != null) { + renderNode.frame = { + x: 0, + y: 0, + width: 10, + height: 500 + } + renderNode.pivot = { x: 50, y: 50 } + } + return this.rootNode; + } + + addNode(node: RenderNode): void { + if (this.rootNode == null) { + return; + } + const renderNode = this.rootNode.getRenderNode() + if (renderNode != null) { + renderNode.appendChild(node); + } + } + + clearNodes(): void { + if (this.rootNode == null) { + return; + } + const renderNode = this.rootNode.getRenderNode() + if (renderNode != null) { + renderNode.clearChildren(); + } + } +} + +export default MyNodeController; diff --git a/Drawing/ArkTSDrawing/entry/src/main/ets/common/PathRenderNode.ets b/Drawing/ArkTSDrawing/entry/src/main/ets/common/PathRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..179571859a304a3637207e3b5d2aa12eb1a4acde --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/ets/common/PathRenderNode.ets @@ -0,0 +1,85 @@ +/* + * 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 { RenderNode } from '@kit.ArkUI' +import { common2D, drawing } from '@kit.ArkGraphics2D' + +class PathRenderNode extends RenderNode { + async draw(context: DrawContext) { + const canvas = context.canvas; + let height_ = 1200; + let width_ = 600; + let len = height_ / 4; + let aX = width_ / 2; + let aY = height_ / 4; + let dX = aX - len * Math.sin(18.0); + let dY = aY + len * Math.cos(18.0); + let cX = aX + len * Math.sin(18.0); + let cY = dY; + let bX = aX + (len / 2.0); + let bY = aY + Math.sqrt((cX - dX) * (cX - dX) + (len / 2.0) * (len / 2.0)); + let eX = aX - (len / 2.0); + let eY = bY; + + // 创建一个path对象,然后使用接口连接成一个五角星形状 + let path = new drawing.Path(); + + // 指定path的起始位置 + path.moveTo(aX, aY); + + // 用直线连接到目标点 + path.lineTo(bX, bY); + path.lineTo(cX, cY); + path.lineTo(dX, dY); + path.lineTo(eX, eY); + + // 闭合形状,path绘制完毕 + path.close(); + + // 创建一个画笔Pen对象,Pen对象用于形状的边框线绘制 + let pen = new drawing.Pen(); + pen.setAntiAlias(true); + let penColor: common2D.Color = { + alpha: 0xFF, + red: 0xFF, + green: 0x00, + blue: 0x00 + } + pen.setColor(penColor); + pen.setStrokeWidth(10.0); + + // 将Pen画笔设置到canvas中 + canvas.attachPen(pen); + + // 创建一个画刷Brush对象,Brush对象用于形状的填充 + let brush = new drawing.Brush(); + let brushColor: common2D.Color = { + alpha: 0xFF, + red: 0x00, + green: 0xFF, + blue: 0x00 + } + brush.setColor(brushColor); + + // 将Brush画刷设置到canvas中 + canvas.attachBrush(brush); + + // 绘制path + canvas.drawPath(path); + } +} + +export default PathRenderNode; diff --git a/Drawing/ArkTSDrawing/entry/src/main/ets/common/RectRenderNode.ets b/Drawing/ArkTSDrawing/entry/src/main/ets/common/RectRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..0019996a706ca9f3bafeb283d257b7318178dfad --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/ets/common/RectRenderNode.ets @@ -0,0 +1,41 @@ +/* + * 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 { RenderNode } from '@kit.ArkUI' +import { drawing } from '@kit.ArkGraphics2D' + +class RectRenderNode extends RenderNode { + async draw(context: DrawContext) { + const canvas = context.canvas; + const pen = new drawing.Pen(); + pen.setStrokeWidth(5); + pen.setColor({ + alpha: 255, + red: 255, + green: 0, + blue: 0 + }); + canvas.attachPen(pen); + canvas.drawRect({ + left: 200, + right: 500, + top: 300, + bottom: 900 + }); + } +} + +export default RectRenderNode; diff --git a/Drawing/ArkTSDrawing/entry/src/main/ets/common/TextRenderNode.ets b/Drawing/ArkTSDrawing/entry/src/main/ets/common/TextRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..18f946810b49504c3a448bb5cd8f7bf2479dc7c1 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/ets/common/TextRenderNode.ets @@ -0,0 +1,38 @@ +/* + * 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 { RenderNode } from '@kit.ArkUI' +import { drawing } from '@kit.ArkGraphics2D' + +class TextRenderNode extends RenderNode { + async draw(context: DrawContext) { + const canvas = context.canvas; + const brush = new drawing.Brush(); + brush.setColor({ + alpha: 255, + red: 255, + green: 0, + blue: 0 + }); + const font = new drawing.Font(); + font.setSize(100); + const textBlob = drawing.TextBlob.makeFromString('Hello World', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.attachBrush(brush); + canvas.drawTextBlob(textBlob, 90, 500); + } +} + +export default TextRenderNode; diff --git a/Drawing/ArkTSDrawing/entry/src/main/ets/entryability/EntryAbility.ets b/Drawing/ArkTSDrawing/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..4e3af23763f0e51777efa68f6b9e4e8598c8ebf8 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,56 @@ +/* + * 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 { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('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(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/Drawing/ArkTSDrawing/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/Drawing/ArkTSDrawing/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..be794f48ccffc46278e76ce3493088a647b0d470 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,26 @@ +/* + * 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/main/ets/pages/Index.ets b/Drawing/ArkTSDrawing/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..39dbefd2a61519e1e827f9c00d0b9e71128facd6 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,93 @@ +/* + * 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 MyNodeController from '../common/MyNodeController' +import PathRenderNode from '../common/PathRenderNode' +import RectRenderNode from '../common/RectRenderNode' +import TextRenderNode from '../common/TextRenderNode' + +// 创建一个PathRenderNode对象 +const PathNode = new PathRenderNode(); +// 定义PathNode的像素格式 +PathNode.frame = { + x: 100, + y: 100, + width: 200, + height: 800 +} +PathNode.pivot = { x: 0.2, y: 0.8 } +PathNode.scale = { x: 1, y: 1 } + +// 创建一个RectRenderNode对象 +const rectNode = new RectRenderNode(); +// 定义rectNode的像素格式 +rectNode.frame = { + x: 90, + y: 100, + width: 200, + height: 800 +} +rectNode.pivot = { x: 0.2, y: 0.8 } +rectNode.scale = { x: 1, y: 1 } + +// 创建一个TextRenderNode对象 +const textNode = new TextRenderNode(); +// 定义textNode的像素格式 +textNode.frame = { + x: 90, + y: 100, + width: 200, + height: 800 +} +textNode.pivot = { x: 0.2, y: 0.8 } +textNode.scale = { x: 1, y: 1 } + +@Entry +@Component +struct RenderTest { + private myNodeController: MyNodeController = new MyNodeController(); + + build() { + Column() { + Row() { + NodeContainer(this.myNodeController) + .height('100%') + Button("Draw Path") + .margin({ bottom: 200, right: 12 }) + .onClick(() => { + this.myNodeController.clearNodes() + this.myNodeController.addNode(PathNode) + }) + Button("Draw Rect") + .margin({ bottom: 200, right: 12 }) + .onClick(() => { + this.myNodeController.clearNodes() + this.myNodeController.addNode(rectNode) + }) + Button("Draw Text") + .margin({ bottom: 200, right: 12 }) + .onClick(() => { + this.myNodeController.clearNodes() + this.myNodeController.addNode(textNode) + }) + } + .width('100%') + .justifyContent(FlexAlign.Center) + .shadow(ShadowStyle.OUTER_DEFAULT_SM) + .alignItems(VerticalAlign.Bottom) + .layoutWeight(1) + } + } +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/main/module.json5 b/Drawing/ArkTSDrawing/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ca3c9ba3157fbb51f3b8cba57252aee35e6203d0 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/module.json5 @@ -0,0 +1,65 @@ +/* + * 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. + */ +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/base/element/color.json b/Drawing/ArkTSDrawing/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/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/Drawing/ArkTSDrawing/entry/src/main/resources/base/element/string.json b/Drawing/ArkTSDrawing/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..19986ebf7aa6c87ae52a11dcab6f252f52262b9c --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "ArkTSDrawing" + } + ] +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/background.png b/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/background.png differ diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/foreground.png b/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/foreground.png differ diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/layered_image.json b/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/startIcon.png b/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/Drawing/ArkTSDrawing/entry/src/main/resources/base/media/startIcon.png differ diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/base/profile/backup_config.json b/Drawing/ArkTSDrawing/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/base/profile/main_pages.json b/Drawing/ArkTSDrawing/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/en_US/element/string.json b/Drawing/ArkTSDrawing/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..19986ebf7aa6c87ae52a11dcab6f252f52262b9c --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "ArkTSDrawing" + } + ] +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/main/resources/zh_CN/element/string.json b/Drawing/ArkTSDrawing/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e250f3f7dbbd1d9a8032bb60057d400d6e82405f --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "模块描述" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "ArkTSDrawing" + } + ] +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/mock/mock-config.json5 b/Drawing/ArkTSDrawing/entry/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e4eb8bbe4e5836577a13067db5f0fede18aa5c8c --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/mock/mock-config.json5 @@ -0,0 +1,16 @@ +/* + * 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. + */ +{ +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/ohosTest/ets/test/DrawingAbility.test.ets b/Drawing/ArkTSDrawing/entry/src/ohosTest/ets/test/DrawingAbility.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..dea8391d8d869cf34027deab5875013e52903884 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/ohosTest/ets/test/DrawingAbility.test.ets @@ -0,0 +1,125 @@ +/* + * 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 AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import { describe, it, expect } from '@ohos/hypium'; +import { Driver, ON } from '@ohos.UiTest'; +import hilog from '@ohos.hilog'; + +const TAG = '[Sample_ArkTSDrawingAPI]'; +const DOMAIN = 0xF811 +const BUNDLE = 'ArkTSDrawingAPI_' + +export default function abilityTest() { + + describe('ActsAbilityTest', () => { + /** + * 打开应用 + */ + it(BUNDLE + 'StartAbility_001', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + "StartAbility_001, begin") + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.drawing', + abilityName: 'EntryAbility' + }); + } catch (exception) { + hilog.info(DOMAIN, TAG, BUNDLE + `StartAbility_001 exception = ${JSON.stringify(exception)}`) + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Draw Path')); + done(); + hilog.info(DOMAIN, TAG, BUNDLE + 'StartAbility_001 end') + }) + + /** + * 点击按钮,绘制图形 + */ + it(BUNDLE + 'DrawPath_001', 2, async () => { + hilog.info(DOMAIN, TAG, BUNDLE + "DrawPath_001, begin") + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.drawing', + abilityName: 'EntryAbility' + }); + } catch (exception) { + hilog.info(DOMAIN, TAG, BUNDLE + `DrawPath_001 exception = ${JSON.stringify(exception)}`) + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Draw Path')); + let drawPathBtn = await driver.findComponent(ON.text('Draw Path')); + // 点击'Draw Path'按钮 + await drawPathBtn.click(); + await driver.delayMs(1000); + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawPath_001 end') + }) + + /** + * 点击按钮,绘制矩形 + */ + it(BUNDLE + 'DrawRect_001', 2, async () => { + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawRect_001 begin') + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.drawing', + abilityName: 'EntryAbility' + }); + } catch (exception) { + hilog.info(DOMAIN, TAG, BUNDLE + `DrawRect_001 exception = ${JSON.stringify(exception)}`) + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Draw Rect')); + let drawPathBtn = await driver.findComponent(ON.text('Draw Rect')); + // 点击'Draw Rect'按钮 + await drawPathBtn.click(); + await driver.delayMs(1000); + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawRect_001 end') + }) + + /** + * 点击按钮,绘制文字 + */ + it(BUNDLE + 'DrawText_001', 2, async () => { + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawText_001 begin') + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.drawing', + abilityName: 'EntryAbility' + }); + } catch (exception) { + hilog.info(DOMAIN, TAG, BUNDLE + `DrawText_001 exception = ${JSON.stringify(exception)}`) + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Draw Text')); + let drawPathBtn = await driver.findComponent(ON.text('Draw Text')); + // 点击'Draw Text'按钮 + await drawPathBtn.click(); + await driver.delayMs(1000); + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawText_001 end') + }) + }) +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/ohosTest/ets/test/List.test.ets b/Drawing/ArkTSDrawing/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c6a406e9eb09b5d547967f9e78fd51d0697ba79e --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,19 @@ +/* + * 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 abilityTest from './DrawingAbility.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/ohosTest/module.json5 b/Drawing/ArkTSDrawing/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..944ed99859bbc89eac2d57af2b3600881bb5a47c --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/ohosTest/module.json5 @@ -0,0 +1,26 @@ +/* + * 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. + */ +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/Drawing/ArkTSDrawing/entry/src/test/List.test.ets b/Drawing/ArkTSDrawing/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..3bc7c739d1957b3a778de67db8992240584b6692 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/test/List.test.ets @@ -0,0 +1,19 @@ +/* + * 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 localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/Drawing/ArkTSDrawing/entry/src/test/LocalUnit.test.ets b/Drawing/ArkTSDrawing/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..4ab69eca22877101c08c26f8a92322edddb739f8 --- /dev/null +++ b/Drawing/ArkTSDrawing/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,47 @@ +/* + * 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'; + +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 diff --git a/Drawing/ArkTSDrawing/hvigor/hvigor-config.json5 b/Drawing/ArkTSDrawing/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..87990affc4f62f7c76c246428c40272d3be0a7c7 --- /dev/null +++ b/Drawing/ArkTSDrawing/hvigor/hvigor-config.json5 @@ -0,0 +1,36 @@ +/* + * 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. + */ +{ + "modelVersion": "5.0.0", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/Drawing/ArkTSDrawing/hvigorfile.ts b/Drawing/ArkTSDrawing/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2e7b775e53d303786e9503843d93a55e59c7f4f2 --- /dev/null +++ b/Drawing/ArkTSDrawing/hvigorfile.ts @@ -0,0 +1,20 @@ +/* + * 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 { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/Drawing/ArkTSDrawing/oh-package.json5 b/Drawing/ArkTSDrawing/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7a7fe7781e61280b6e0a6f437d4a7a6a41b2afcd --- /dev/null +++ b/Drawing/ArkTSDrawing/oh-package.json5 @@ -0,0 +1,24 @@ +/* + * 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. + */ +{ + "modelVersion": "5.0.0", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/Drawing/ArkTSDrawing/ohosTest.md b/Drawing/ArkTSDrawing/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..407090b8ef06a0302479d0387357d6a05838d614 --- /dev/null +++ b/Drawing/ArkTSDrawing/ohosTest.md @@ -0,0 +1,11 @@ +# ArkTSDrawing测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| -------- | ------------ | ----------------- | --------------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 绘制路径 | 位于首页 | 点击**Draw Path** | 页面显示出一个五角星 | 是 | Pass | +| 绘制矩形 | 位于首页 | 点击**Draw Rect** | 页面显示出一个矩形 | 是 | Pass | +| 绘制文字 | 位于首页 | 点击**Draw Text** | 页面显示出一个“Hello World” | 是 | Pass | + diff --git a/Drawing/ArkTSDrawing/screenshot/path.png b/Drawing/ArkTSDrawing/screenshot/path.png new file mode 100644 index 0000000000000000000000000000000000000000..73831e09b0584af5d9eb03602ef7b86435619e5b Binary files /dev/null and b/Drawing/ArkTSDrawing/screenshot/path.png differ diff --git a/Drawing/ArkTSDrawing/screenshot/rect.png b/Drawing/ArkTSDrawing/screenshot/rect.png new file mode 100644 index 0000000000000000000000000000000000000000..562ede3997239af1e610dd5eb0f6e629ba3a235b Binary files /dev/null and b/Drawing/ArkTSDrawing/screenshot/rect.png differ diff --git a/Drawing/ArkTSDrawing/screenshot/text.png b/Drawing/ArkTSDrawing/screenshot/text.png new file mode 100644 index 0000000000000000000000000000000000000000..246a6e31cd0e20b2ab96714d24044602babffc6c Binary files /dev/null and b/Drawing/ArkTSDrawing/screenshot/text.png differ diff --git a/Drawing/NDKDrawing/.gitignore b/Drawing/NDKDrawing/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/Drawing/NDKDrawing/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/Drawing/NDKDrawing/AppScope/app.json5 b/Drawing/NDKDrawing/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..762e8a88679fa327308732e56868807915292031 --- /dev/null +++ b/Drawing/NDKDrawing/AppScope/app.json5 @@ -0,0 +1,24 @@ +/* + * 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. + */ +{ + "app": { + "bundleName": "com.samples.cdrawing", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/Drawing/NDKDrawing/AppScope/resources/base/element/string.json b/Drawing/NDKDrawing/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..068485e2f19c028edfdb1314fa39213254e14f3f --- /dev/null +++ b/Drawing/NDKDrawing/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "Cdrawing" + } + ] +} diff --git a/Drawing/NDKDrawing/AppScope/resources/base/media/app_icon.png b/Drawing/NDKDrawing/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/Drawing/NDKDrawing/AppScope/resources/base/media/app_icon.png differ diff --git a/Drawing/NDKDrawing/README.md b/Drawing/NDKDrawing/README.md new file mode 100644 index 0000000000000000000000000000000000000000..eaa1cc988f8455ac5bd10ad2c4bea4feeb1b22b6 --- /dev/null +++ b/Drawing/NDKDrawing/README.md @@ -0,0 +1,80 @@ +# 使用Drawing实现图形绘制与显示(C++) + +## 介绍 + +本工程主要实现了对以下指南文档中 https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/graphics/drawing-guidelines.md 示例代码片段的工程化,主要目标是实现指南中示例代码需要与sample工程文件同源。 + +## 效果预览 + +| ![](screenshot/path.png) | ![](screenshot/text.png) | +|--------------------------| ------------------------ | + +使用说明 + +1. 该工程可以选择在模拟器和开发板上运行。 +2. 点击构建,即可在生成的应用中点击对应的按钮进行图案的绘制。 +3. 进入”DocsSample/Drawing/NDKDrawing/entry/src/ohosTest/ets/test/DrawingAbility.test.ets“文件,可以对本项目进行UI的自动化测试。 + +## 工程目录 + +``` +NDKDrawing +├──entry/src/main +│ ├──cpp // C++代码区 +│ │ ├──CMakeLists.txt // CMake配置文件 +│ │ ├──hello.cpp // Napi模块注册 +│ │ ├──common +│ │ │ └──log_common.h // 日志封装定义文件 +│ │ ├──plugin // 生命周期管理模块 +│ │ │ ├──plugin_manager.cpp +│ │ │ └──plugin_manager.h +│ │ ├──samples // samples渲染模块 +│ │ │ ├──sample_bitmap.cpp +│ │ │ └──sample_bitmap.h +│ ├──ets // ets代码区 +│ │ ├──entryability +│ │ │ ├──EntryAbility.ts // 程序入口类 +| | | └──EntryAbility.ets +| | ├──interface +│ │ │ └──XComponentContext.ts // XComponentContext +│ │ └──pages // 页面文件 +│ │ └──Index.ets // 主界面 +| ├──resources // 资源文件目录 +``` + +## 具体实现 + +1. 利用Native XComponent来获取NativeWindow实例、获取布局/事件信息、注册事件回调并通过Drawing API实现在页面上绘制形状。功能主要包括点击按钮绘制一个五角星和“Hello World Drawing”文字。 +2. 通过在IDE中创建Native c++ 工程,在c++代码中定义对外接口为drawPattern和drawText,在js侧调用该接口可在页面上绘制出一个五角星和“Hello World Drawing”文字。 +3. 在XComponent的OnSurfaceCreated回调中获取NativeWindow实例并初始化NativeWindow环境。调用OH_NativeXComponent_GetXComponentSize接口获取XComponent的宽高,并以此为输入调用Drawing相关的绘制接口在NativeWindow上绘制出一个五角星和文字。 + +## 相关权限 + +无。 + +## 依赖 + +不涉及。 + +## 约束和限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.3 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.3 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.3 Release及以上。 + +## 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/Drawing/NDKDrawing/ > .git/info/sparse-checkout +git remote add origin https://gitcode.com/openharmony/applications_app_samples.git +git pull origin master +``` + diff --git a/Drawing/NDKDrawing/build-profile.json5 b/Drawing/NDKDrawing/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..41564fbe641371100ed1748efacd104b019a75ea --- /dev/null +++ b/Drawing/NDKDrawing/build-profile.json5 @@ -0,0 +1,66 @@ +/* + * 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. + */ +{ + "app": { + "products": [ + { + "name": "default", + "signingConfig": "default", + //指定OpenHarmony应用/服务编译时的版本 + "compatibleSdkVersion": "5.0.3(15)", + //指定OpenHarmony应用/服务兼容的最低版本。 + "targetSdkVersion": "5.0.3(15)", + //指定OpenHarmony应用/服务目标版本。若没有设置,默认为compatibleSdkVersion + "runtimeOS": "HarmonyOS", + //指定为OpenHarmony + "buildOption": { + "externalNativeOptions": { + //指定ABI配置 + "abiFilters": [ + "arm64-v8a", + "x86_64", + ] + }, + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug" + }, + { + "name": "release" + } + ], + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/code-linter.json5 b/Drawing/NDKDrawing/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3311c9280cd317b17b4b2745922184ac805051d8 --- /dev/null +++ b/Drawing/NDKDrawing/code-linter.json5 @@ -0,0 +1,34 @@ +/* + * 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. + */ +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/.gitignore b/Drawing/NDKDrawing/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/Drawing/NDKDrawing/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/build-profile.json5 b/Drawing/NDKDrawing/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..13781d517fa6d16ad7a8348345c632ce2d2d1d61 --- /dev/null +++ b/Drawing/NDKDrawing/entry/build-profile.json5 @@ -0,0 +1,53 @@ +/* + * 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. + */ +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/hvigorfile.ts b/Drawing/NDKDrawing/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..8fe7819925bcfb8281ffffa757993ecade94bcbc --- /dev/null +++ b/Drawing/NDKDrawing/entry/hvigorfile.ts @@ -0,0 +1,20 @@ +/* + * 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 { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/Drawing/NDKDrawing/entry/obfuscation-rules.txt b/Drawing/NDKDrawing/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/Drawing/NDKDrawing/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# 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://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# 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 + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/oh-package-lock.json5 b/Drawing/NDKDrawing/entry/oh-package-lock.json5 new file mode 100644 index 0000000000000000000000000000000000000000..de121d113e3d7f54ecd3d75b4fa018d088895081 --- /dev/null +++ b/Drawing/NDKDrawing/entry/oh-package-lock.json5 @@ -0,0 +1,18 @@ +{ + "meta": { + "stableOrder": true + }, + "lockfileVersion": 3, + "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", + "specifiers": { + "libentry.so@src/main/cpp/types/libentry": "libentry.so@src/main/cpp/types/libentry" + }, + "packages": { + "libentry.so@src/main/cpp/types/libentry": { + "name": "libentry.so", + "version": "12", + "resolved": "src/main/cpp/types/libentry", + "registryType": "local" + } + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/oh-package.json5 b/Drawing/NDKDrawing/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1182eecc31bffd54beb78b9aee733f9df9aa735f --- /dev/null +++ b/Drawing/NDKDrawing/entry/oh-package.json5 @@ -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. + */ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/CMakeLists.txt b/Drawing/NDKDrawing/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..0276845717c0d4e5da1e6898b0f6377cfc61bfd9 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,27 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.4.1) +project(drawing_test) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED + napi_init.cpp + samples/sample_bitmap.cpp + plugin/plugin_manager.cpp +) +find_library( + # Sets the name of the path variable. + hilog-lib + # Specifies the name of the NDK library that + # you want CMake to locate. + hilog_ndk.z +) +message("hilog path is :" ${hilog-lib}) +target_link_libraries(entry PUBLIC ${hilog-lib}) +target_link_libraries(entry PUBLIC libace_napi.z.so) +target_link_libraries(entry PUBLIC libace_ndk.z.so) +target_link_libraries(entry PUBLIC libnative_window.so) +target_link_libraries(entry PUBLIC libnative_drawing.so) \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/common/log_common.h b/Drawing/NDKDrawing/entry/src/main/cpp/common/log_common.h new file mode 100644 index 0000000000000000000000000000000000000000..1f0a8d768e8244fd57b30525db42002cfd762885 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/common/log_common.h @@ -0,0 +1,26 @@ +/* + * 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. + */ +#ifndef LOG_COMMON_H +#define LOG_COMMON_H +#include +#define LOG_PRINT_DOMAIN 0xFF00 +#define APP_LOG_DOMAIN 0x0001 +constexpr const char *APP_LOG_TAG = "DrawingSample"; +#define DRAWING_LOGI(...) ((void)OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, APP_LOG_TAG, __VA_ARGS__)) +#define DRAWING_LOGD(...) ((void)OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_DOMAIN, APP_LOG_TAG, __VA_ARGS__)) +#define DRAWING_LOGW(...) ((void)OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, APP_LOG_TAG, __VA_ARGS__)) +#define DRAWING_LOGE(...) ((void)OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_DOMAIN, APP_LOG_TAG, __VA_ARGS__)) + +#endif // LOG_COMMON_H diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/hello.cpp b/Drawing/NDKDrawing/entry/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af797b0ae85199cd333f2929630110b7e0851ef2 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/hello.cpp @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#include +#include "napi/native_api.h" +#include "common/log_common.h" +#include "plugin/plugin_manager.h" + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + DRAWING_LOGI("napi init"); + PluginManager::GetInstance()->Export(env, exports); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) +{ + napi_module_register(&demoModule); +} diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/napi_init.cpp b/Drawing/NDKDrawing/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7853a653d3987855862da4b50d2cce782507329f --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,42 @@ +/* + * 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. + */ +#include +#include "napi/native_api.h" +#include "common/log_common.h" +#include "plugin/plugin_manager.h" + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + DRAWING_LOGI("napi init"); + PluginManager::GetInstance()->Export(env, exports); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) +{ + napi_module_register(&demoModule); +} diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/plugin/plugin_manager.cpp b/Drawing/NDKDrawing/entry/src/main/cpp/plugin/plugin_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a6cf871cb6634824296193dd8ebec5f916aa264 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/plugin/plugin_manager.cpp @@ -0,0 +1,126 @@ +/* + * 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. + */ +#include +#include +#include +#include +#include "common/log_common.h" +#include "plugin_manager.h" + +PluginManager *PluginManager::GetInstance() +{ + static PluginManager pluginManager; + return &pluginManager; +} + +PluginManager::~PluginManager() +{ + DRAWING_LOGI("~PluginManager"); + for (auto iter = nativeXComponentMap_.begin(); iter != nativeXComponentMap_.end(); ++iter) { + if (iter->second != nullptr) { + delete iter->second; + iter->second = nullptr; + } + } + nativeXComponentMap_.clear(); + + for (auto iter = pluginRenderMap_.begin(); iter != pluginRenderMap_.end(); ++iter) { + if (iter->second != nullptr) { + delete iter->second; + iter->second = nullptr; + } + } + pluginRenderMap_.clear(); +} + +void PluginManager::Export(napi_env env, napi_value exports) +{ + if ((env == nullptr) || (exports == nullptr)) { + DRAWING_LOGE("Export: env or exports is null"); + return; + } + + napi_value exportInstance = nullptr; + if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) { + DRAWING_LOGE("Export: napi_get_named_property fail"); + return; + } + + OH_NativeXComponent *nativeXComponent = nullptr; + if (napi_unwrap(env, exportInstance, reinterpret_cast(&nativeXComponent)) != napi_ok) { + DRAWING_LOGE("Export: napi_unwrap fail"); + return; + } + + char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'}; + uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; + if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + DRAWING_LOGE("Export: OH_NativeXComponent_GetXComponentId fail"); + return; + } + + std::string id(idStr); + auto context = PluginManager::GetInstance(); + if ((context != nullptr) && (nativeXComponent != nullptr)) { + context->SetNativeXComponent(id, nativeXComponent); + auto render = context->GetRender(id); + if (render != nullptr) { + render->RegisterCallback(nativeXComponent); + render->Export(env, exports); + } else { + DRAWING_LOGE("render is nullptr"); + } + } +} + +void PluginManager::SetNativeXComponent(std::string &id, OH_NativeXComponent *nativeXComponent) +{ + DRAWING_LOGI("set native xComponent, ID = %{public}s.", id.c_str()); + if (nativeXComponent == nullptr) { + DRAWING_LOGE("xcomponent null"); + return; + } + + if (nativeXComponentMap_.find(id) == nativeXComponentMap_.end()) { + nativeXComponentMap_[id] = nativeXComponent; + return; + } + + if (nativeXComponentMap_[id] != nativeXComponent) { + OH_NativeXComponent *tmp = nativeXComponentMap_[id]; + delete tmp; + tmp = nullptr; + nativeXComponentMap_[id] = nativeXComponent; + } +} + +SampleBitMap *PluginManager::GetRender(std::string &id) +{ + if (pluginRenderMap_.find(id) == pluginRenderMap_.end()) { + SampleBitMap *instance = SampleBitMap::GetInstance(id); + pluginRenderMap_[id] = instance; + return instance; + } + return pluginRenderMap_[id]; +} + +void PluginManager::ReleaseRender(std::string &id) +{ + auto map = pluginRenderMap_.find(id); + if (map == pluginRenderMap_.end()) { + return; + } + pluginRenderMap_.erase(map); +} diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/plugin/plugin_manager.h b/Drawing/NDKDrawing/entry/src/main/cpp/plugin/plugin_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..37c84abc31db4a312d9b08369004edc687d72aae --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/plugin/plugin_manager.h @@ -0,0 +1,40 @@ +/* + * 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. + */ +#ifndef PLUGIN_MANAGER_H +#define PLUGIN_MANAGER_H + +#include +#include +#include +#include +#include +#include "samples/sample_bitmap.h" + +class PluginManager { +public: + ~PluginManager(); + + static PluginManager *GetInstance(); + + void SetNativeXComponent(std::string &id, OH_NativeXComponent *nativeXComponent); + SampleBitMap *GetRender(std::string &id); + void ReleaseRender(std::string &id); + void Export(napi_env env, napi_value exports); +private: + + std::unordered_map nativeXComponentMap_; + std::unordered_map pluginRenderMap_; +}; +#endif // PLUGIN_MANAGER_H diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/samples/sample_bitmap.cpp b/Drawing/NDKDrawing/entry/src/main/cpp/samples/sample_bitmap.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb52ec6b19abdb28466018080c925a7746eef526 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/samples/sample_bitmap.cpp @@ -0,0 +1,537 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include + + +#include "common/log_common.h" +#include "sample_bitmap.h" +#include "plugin/plugin_manager.h" + +static void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window) +{ + DRAWING_LOGI("OnSurfaceCreatedCB"); + if ((component == nullptr) || (window == nullptr)) { + DRAWING_LOGE("OnSurfaceCreatedCB: component or window is null"); + return; + } + char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'}; + uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; + if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + DRAWING_LOGE("OnSurfaceCreatedCB: Unable to get XComponent id"); + return; + } + std::string id(idStr); + auto render = SampleBitMap::GetInstance(id); + OHNativeWindow *nativeWindow = static_cast(window); + render->SetNativeWindow(nativeWindow); + + uint64_t width; + uint64_t height; + int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height); + if ((xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) && (render != nullptr)) { + render->SetHeight(height); + render->SetWidth(width); + DRAWING_LOGI("xComponent width = %{public}llu, height = %{public}llu", width, height); + } +} + +static void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) +{ + // 可获取 OHNativeWindow 实例 + OHNativeWindow* nativeWindow = static_cast(window); + char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'}; + uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; + if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + DRAWING_LOGE("OnSurfaceChangedCB: Unable to get XComponent id"); + return; + } + std::string id(idStr); + auto render = SampleBitMap::GetInstance(id); + + uint64_t width; + uint64_t height; + int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height); + if ((xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) && (render != nullptr)) { + render->SetHeight(height); + render->SetWidth(width); + DRAWING_LOGI("Surface Changed : xComponent width = %{public}llu, height = %{public}llu", width, height); + } +} + +static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) +{ + DRAWING_LOGI("OnSurfaceDestroyedCB"); + OHNativeWindow* nativeWindow = static_cast(window); + if ((component == nullptr) || (window == nullptr)) { + DRAWING_LOGE("OnSurfaceDestroyedCB: component or window is null"); + return; + } + char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'}; + uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; + if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + DRAWING_LOGE("OnSurfaceDestroyedCB: Unable to get XComponent id"); + return; + } + std::string id(idStr); + SampleBitMap::Release(id); +} + +static void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) +{ + // 可获取 OHNativeWindow 实例 + OHNativeWindow* nativeWindow = static_cast(window); + // ... +} + +static std::unordered_map g_instance; + +void SampleBitMap::SetWidth(uint64_t width) +{ + width_ = width; +} + +void SampleBitMap::SetHeight(uint64_t height) +{ + height_ = height; +} + +void SampleBitMap::SetNativeWindow(OHNativeWindow *nativeWindow) +{ + nativeWindow_ = nativeWindow; +} + +void SampleBitMap::Prepare() +{ + if (nativeWindow_ == nullptr) { + DRAWING_LOGE("nativeWindow_ is nullptr"); + return; + } + // 这里的nativeWindow是从上一步骤中的回调函数中获得的 + int32_t usage = NATIVEBUFFER_USAGE_CPU_READ | NATIVEBUFFER_USAGE_CPU_WRITE | NATIVEBUFFER_USAGE_MEM_DMA; + int ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow_, SET_USAGE, usage); + if (ret != 0) { + DRAWING_LOGE("failed to OH_NativeWindow_NativeWindowHandleOpt"); + return; + } + // 通过 OH_NativeWindow_NativeWindowRequestBuffer 获取 OHNativeWindowBuffer 实例 + ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer_, &fenceFd_); + DRAWING_LOGI("request buffer ret = %{public}d", ret); + // 通过 OH_NativeWindow_GetBufferHandleFromNative 获取 buffer 的 handle + bufferHandle_ = OH_NativeWindow_GetBufferHandleFromNative(buffer_); + // 使用系统mmap接口拿到bufferHandle的内存虚拟地址 + mappedAddr_ = static_cast( + mmap(bufferHandle_->virAddr, bufferHandle_->size, PROT_READ | PROT_WRITE, MAP_SHARED, bufferHandle_->fd, 0)); + if (mappedAddr_ == MAP_FAILED) { + DRAWING_LOGE("mmap failed"); + } +} + +void SampleBitMap::DisPlay() +{ + // 画完后获取像素地址,地址指向的内存包含画布画的像素数据 + void *bitmapAddr = OH_Drawing_BitmapGetPixels(cBitmap_); + uint32_t *value = static_cast(bitmapAddr); + + uint32_t *pixel = static_cast(mappedAddr_); // 使用mmap获取到的地址来访问内存 + if (pixel == nullptr) { + DRAWING_LOGE("pixel is null"); + return; + } + if (value == nullptr) { + DRAWING_LOGE("value is null"); + return; + } + // 使用mmap获取到的地址来访问内存 + uint32_t width = static_cast(bufferHandle_->stride / 4); + for (uint32_t x = 0; x < width; x++) { + for (uint32_t y = 0; y < height_; y++) { + *pixel++ = *value++; + } + } + // 设置刷新区域,如果Region中的Rect为nullptr,或者rectNumber为0,则认为OHNativeWindowBuffer全部有内容更改。 + Region region {nullptr, 0}; + // 通过OH_NativeWindow_NativeWindowFlushBuffer 提交给消费者使用,例如:显示在屏幕上。 + OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd_, region); + // 内存使用完记得去掉内存映射 + int result = munmap(mappedAddr_, bufferHandle_->size); + if (result == -1) { + DRAWING_LOGE("munmap failed!"); + } +} + +void SampleBitMap::Create() +{ + uint32_t width = static_cast(bufferHandle_->stride / 4); + // 创建一个bitmap对象 + cBitmap_ = OH_Drawing_BitmapCreate(); + // 定义bitmap的像素格式 + OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; + // 构造对应格式的bitmap + OH_Drawing_BitmapBuild(cBitmap_, width, height_, &cFormat); + + // 创建一个canvas对象 + cCanvas_ = OH_Drawing_CanvasCreate(); + // 将画布与bitmap绑定,画布画的内容会输出到绑定的bitmap内存中 + OH_Drawing_CanvasBind(cCanvas_, cBitmap_); + // 使用白色清除画布内容 + OH_Drawing_CanvasClear(cCanvas_, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF)); +} + +void SampleBitMap::ConstructPath() +{ + int len = height_ / 4; + float aX = width_ / 2; + float aY = height_ / 4; + float dX = aX - len * std::sin(18.0f); + float dY = aY + len * std::cos(18.0f); + float cX = aX + len * std::sin(18.0f); + float cY = dY; + float bX = aX + (len / 2.0); + float bY = aY + std::sqrt((cX - dX) * (cX - dX) + (len / 2.0) * (len / 2.0)); + float eX = aX - (len / 2.0); + float eY = bY; + // 创建一个path对象,然后使用接口连接成一个五角星形状 + cPath_ = OH_Drawing_PathCreate(); + // 指定path的起始位置 + OH_Drawing_PathMoveTo(cPath_, aX, aY); + // 用直线连接到目标点 + OH_Drawing_PathLineTo(cPath_, bX, bY); + OH_Drawing_PathLineTo(cPath_, cX, cY); + OH_Drawing_PathLineTo(cPath_, dX, dY); + OH_Drawing_PathLineTo(cPath_, eX, eY); + // 闭合形状,path绘制完毕 + OH_Drawing_PathClose(cPath_); +} + +void SampleBitMap::SetPenAndBrush() +{ + constexpr float penWidth = 10.0f; // pen width 10 + // 创建一个画笔Pen对象,Pen对象用于形状的边框线绘制 + cPen_ = OH_Drawing_PenCreate(); + OH_Drawing_PenSetAntiAlias(cPen_, true); + OH_Drawing_PenSetColor(cPen_, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00)); + OH_Drawing_PenSetWidth(cPen_, penWidth); + OH_Drawing_PenSetJoin(cPen_, LINE_ROUND_JOIN); + // 将Pen画笔设置到canvas中 + OH_Drawing_CanvasAttachPen(cCanvas_, cPen_); + + // 创建一个画刷Brush对象,Brush对象用于形状的填充 + cBrush_ = OH_Drawing_BrushCreate(); + OH_Drawing_BrushSetColor(cBrush_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0x00)); + + // 将Brush画刷设置到canvas中 + OH_Drawing_CanvasAttachBrush(cCanvas_, cBrush_); +} + +void SampleBitMap::DrawPath() +{ + // 在画布上画path的形状,五角星的边框样式为pen设置,颜色填充为Brush设置 + OH_Drawing_CanvasDrawPath(cCanvas_, cPath_); +} + +void SampleBitMap::DrawText() +{ + // 选择从左到右/左对齐等排版属性 + OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle(); + OH_Drawing_SetTypographyTextDirection(typoStyle, TEXT_DIRECTION_LTR); + OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_JUSTIFY); + + // TEXT_ALIGN_JUSTIFY + // 设置文字颜色,例如黑色 + OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle(); + OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00)); + // 设置文字大小、字重等属性 + double fontSize = width_ / 15; + OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize); + OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400); + OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC); + OH_Drawing_SetTextStyleFontHeight(txtStyle, 1); + // 如果需要多次测量,建议fontCollection作为全局变量使用,可以显著减少内存占用 + OH_Drawing_FontCollection* fontCollection = OH_Drawing_CreateSharedFontCollection(); + // 设置系统字体类型 + const char *fontFamilies[] = {"Roboto"}; + OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies); + OH_Drawing_SetTextStyleFontStyle(txtStyle, FONT_STYLE_NORMAL); + OH_Drawing_SetTextStyleLocale(txtStyle, "en"); + OH_Drawing_TypographyCreate *handler = + OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection()); + OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle); + // 设置文字内容 + const char *text = "Hello World Drawing\n"; + OH_Drawing_TypographyHandlerAddText(handler, text); + OH_Drawing_TypographyHandlerPopTextStyle(handler); + OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler); + // 设置页面最大宽度 + double maxWidth = width_; + OH_Drawing_TypographyLayout(typography, maxWidth); + // 设置文本在画布上绘制的起始位置 + double position[2] = {width_ / 5.0, height_ / 2.0}; + // 将文本绘制到画布上 + OH_Drawing_TypographyPaint(typography, cCanvas_, position[0], position[1]); + + // 确认typography不再使用时销毁 + OH_Drawing_DestroyTypography(typography); + // 确认已经完成layout时销毁 + OH_Drawing_DestroyTypographyHandler(handler); + // 确认不需要使用字体绘制功能时销毁 + OH_Drawing_DestroyFontCollection(fontCollection); + // 确认已经完成layout时销毁 + OH_Drawing_DestroyTextStyle(txtStyle); + // 确认已经完成layout时销毁 + OH_Drawing_DestroyTypographyStyle(typoStyle); +} + +void SampleBitMap::DrawTextNAbility() +{ + // 创建字体,并设置文字大小 + OH_Drawing_Font* font = OH_Drawing_FontCreate(); + int textSize = 40; + OH_Drawing_FontSetTextSize(font, textSize); + + /* 面向应用具备自排版能力的文本绘制场景 */ + // 创建文本构造器 + OH_Drawing_TextBlobBuilder* builder = OH_Drawing_TextBlobBuilderCreate(); + // count 需要用户自己定义 + int32_t count = 0; + // glyphs、posX和posY是开发者自排版产生的数据,使用该数据填充内存 + uint16_t* glyphs = nullptr; + float* posX = nullptr; + float* posY = nullptr; + int two = 2; + int one = 1; + const OH_Drawing_RunBuffer* runBuffer = OH_Drawing_TextBlobBuilderAllocRunPos(builder, font, count, nullptr); + for (int idx = 0; idx < count; idx++) { + runBuffer->glyphs[idx] = glyphs[idx]; + runBuffer->pos[idx * two] = posX[idx]; + runBuffer->pos[idx * two + one] = posY[idx]; + } + // 通过文本构造器创建文本 + OH_Drawing_TextBlob* textBlob = OH_Drawing_TextBlobBuilderMake(builder); + // 释放内存 + OH_Drawing_TextBlobBuilderDestroy(builder); + + /* 面向应用使用默认排版能力的文本绘制场景 */ + // 创建要显示的字符 + size_t size = 19; + const char16_t* buffer = u"Hello World Drawing"; + // 通过字符和对应的编码格式创建默认排版的文本 + OH_Drawing_TextEncoding type = OH_Drawing_TextEncoding::TEXT_ENCODING_UTF16; + OH_Drawing_TextBlob* textBlob2 = OH_Drawing_TextBlobCreateFromText(buffer, size * sizeof(char16_t), font, type); + // 创建一个画刷Brush对象,Brush对象用于形状的填充 + cBrush_ = OH_Drawing_BrushCreate(); + OH_Drawing_BrushSetColor(cBrush_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00)); + + // 将Brush画刷设置到canvas中 + OH_Drawing_CanvasAttachBrush(cCanvas_, cBrush_); + // 设置文本在画布上绘制的起始位置 + double position[2] = {width_ / 5.0, height_ / 2.0}; + // 将文本绘制到画布上 + OH_Drawing_CanvasDrawTextBlob(cCanvas_, textBlob, position[0], position[1]); + // 释放内存 + OH_Drawing_TextBlobDestroy(textBlob); + OH_Drawing_FontDestroy(font); +} + +napi_value SampleBitMap::NapiDrawPattern(napi_env env, napi_callback_info info) +{ + DRAWING_LOGI("NapiDrawPattern"); + if ((env == nullptr) || (info == nullptr)) { + DRAWING_LOGE("NapiDrawPattern: env or info is null"); + return nullptr; + } + + napi_value thisArg; + if (napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr) != napi_ok) { + DRAWING_LOGE("NapiDrawPattern: napi_get_cb_info fail"); + return nullptr; + } + + napi_value exportInstance; + if (napi_get_named_property(env, thisArg, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) { + DRAWING_LOGE("NapiDrawPattern: napi_get_named_property fail"); + return nullptr; + } + + OH_NativeXComponent *nativeXComponent = nullptr; + if (napi_unwrap(env, exportInstance, reinterpret_cast(&nativeXComponent)) != napi_ok) { + DRAWING_LOGE("NapiDrawPattern: napi_unwrap fail"); + return nullptr; + } + + char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'}; + uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; + if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + DRAWING_LOGE("NapiDrawPattern: Unable to get XComponent id"); + return nullptr; + } + DRAWING_LOGI("ID = %{public}s", idStr); + std::string id(idStr); + SampleBitMap *render = SampleBitMap().GetInstance(id); + if (render != nullptr) { + render->Prepare(); + render->Create(); + render->ConstructPath(); + render->SetPenAndBrush(); + render->DrawPath(); + render->DisPlay(); + render->Destroy(); + DRAWING_LOGI("DrawPath executed"); + } else { + DRAWING_LOGE("render is nullptr"); + } + return nullptr; +} + +napi_value SampleBitMap::NapiDrawText(napi_env env, napi_callback_info info) +{ + DRAWING_LOGI("NapiDrawText"); + if ((env == nullptr) || (info == nullptr)) { + DRAWING_LOGE("NapiDrawPattern: env or info is null"); + return nullptr; + } + + napi_value thisArg; + if (napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr) != napi_ok) { + DRAWING_LOGE("NapiDrawPattern: napi_get_cb_info fail"); + return nullptr; + } + + napi_value exportInstance; + if (napi_get_named_property(env, thisArg, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) { + DRAWING_LOGE("NapiDrawPattern: napi_get_named_property fail"); + return nullptr; + } + + OH_NativeXComponent *nativeXComponent = nullptr; + if (napi_unwrap(env, exportInstance, reinterpret_cast(&nativeXComponent)) != napi_ok) { + DRAWING_LOGE("NapiDrawPattern: napi_unwrap fail"); + return nullptr; + } + + char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'}; + uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; + if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + DRAWING_LOGE("NapiDrawPattern: Unable to get XComponent id"); + return nullptr; + } + DRAWING_LOGI("ID = %{public}s", idStr); + std::string id(idStr); + SampleBitMap *render = SampleBitMap().GetInstance(id); + if (render != nullptr) { + render->Prepare(); + render->Create(); + render->DrawText(); + render->DisPlay(); + render->Destroy(); + DRAWING_LOGI("DrawText executed"); + } else { + DRAWING_LOGE("render is nullptr"); + } + return nullptr; +} + +SampleBitMap::~SampleBitMap() +{ + // 销毁创建的对象 + OH_Drawing_BrushDestroy(cBrush_); + cBrush_ = nullptr; + OH_Drawing_PenDestroy(cPen_); + cPen_ = nullptr; + OH_Drawing_PathDestroy(cPath_); + cPath_ = nullptr; + // 销毁canvas对象 + OH_Drawing_CanvasDestroy(cCanvas_); + cCanvas_ = nullptr; + // 销毁bitmap对象 + OH_Drawing_BitmapDestroy(cBitmap_); + cBitmap_ = nullptr; + + buffer_ = nullptr; + bufferHandle_ = nullptr; + nativeWindow_ = nullptr; + mappedAddr_ = nullptr; +} + +void SampleBitMap::Destroy() +{ + // 销毁创建的对象 + OH_Drawing_BrushDestroy(cBrush_); + cBrush_ = nullptr; + OH_Drawing_PenDestroy(cPen_); + cPen_ = nullptr; + OH_Drawing_PathDestroy(cPath_); + cPath_ = nullptr; + // 销毁canvas对象 + OH_Drawing_CanvasDestroy(cCanvas_); + cCanvas_ = nullptr; + // 销毁bitmap对象 + OH_Drawing_BitmapDestroy(cBitmap_); +} + +void SampleBitMap::Release(std::string &id) +{ + PluginManager::GetInstance()->ReleaseRender(id); + SampleBitMap *render = SampleBitMap::GetInstance(id); + if (render != nullptr) { + delete render; + render = nullptr; + g_instance.erase(g_instance.find(id)); + } +} + +void SampleBitMap::Export(napi_env env, napi_value exports) +{ + if ((env == nullptr) || (exports == nullptr)) { + DRAWING_LOGE("Export: env or exports is null"); + return; + } + napi_property_descriptor desc[] = { + {"drawPattern", nullptr, SampleBitMap::NapiDrawPattern, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"drawText", nullptr, SampleBitMap::NapiDrawText, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + if (napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc) != napi_ok) { + DRAWING_LOGE("Export: napi_define_properties failed"); + } +} + +void SampleBitMap::RegisterCallback(OH_NativeXComponent *nativeXComponent) +{ + DRAWING_LOGI("register callback"); + renderCallback_.OnSurfaceCreated = OnSurfaceCreatedCB; + renderCallback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB; + // Callback must be initialized + renderCallback_.DispatchTouchEvent = nullptr; + renderCallback_.OnSurfaceChanged = nullptr; + OH_NativeXComponent_RegisterCallback(nativeXComponent, &renderCallback_); +} + +SampleBitMap *SampleBitMap::GetInstance(std::string &id) +{ + if (g_instance.find(id) == g_instance.end()) { + SampleBitMap *render = new SampleBitMap(id); + g_instance[id] = render; + return render; + } else { + return g_instance[id]; + } + return nullptr; +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/samples/sample_bitmap.h b/Drawing/NDKDrawing/entry/src/main/cpp/samples/sample_bitmap.h new file mode 100644 index 0000000000000000000000000000000000000000..b2d92d1dda2cac030f48fd4caf2783b28810ee7e --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/samples/sample_bitmap.h @@ -0,0 +1,77 @@ +/* + * 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. + */ +#ifndef SAMPLE_BITMAP_H +#define SAMPLE_BITMAP_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "napi/native_api.h" + +class SampleBitMap { +public: + SampleBitMap() = default; + ~SampleBitMap(); + explicit SampleBitMap(std::string id) : id_(id) {} + static napi_value NapiDrawPattern(napi_env env, napi_callback_info info); + static napi_value NapiDrawText(napi_env env, napi_callback_info info); + static void Release(std::string &id); + void DrawPath(); + void DrawText(); + void DrawTextNAbility(); + void SetWidth(uint64_t width); + void SetHeight(uint64_t height); + void SetNativeWindow(OHNativeWindow *nativeWindow); + void Prepare(); + void Create(); + void DisPlay(); + void ConstructPath(); + void SetPenAndBrush(); + void Export(napi_env env, napi_value exports); + void RegisterCallback(OH_NativeXComponent *nativeXComponent); + void Destroy(); + static SampleBitMap *GetInstance(std::string &id); + std::string id_; +private: + OH_NativeXComponent_Callback renderCallback_; + + uint64_t width_ = 0; + uint64_t height_ = 0; + + OH_Drawing_Bitmap *cBitmap_ = nullptr; + OH_Drawing_Canvas *cCanvas_ = nullptr; + + OH_Drawing_Path *cPath_ = nullptr; + OH_Drawing_Brush *cBrush_ = nullptr; + OH_Drawing_Pen *cPen_ = nullptr; + OHNativeWindow *nativeWindow_ = nullptr; + uint32_t *mappedAddr_ = nullptr; + BufferHandle *bufferHandle_ = nullptr; + struct NativeWindowBuffer *buffer_ = nullptr; + int fenceFd_ = 0; +}; + + +#endif diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/types/libentry/index.d.ts b/Drawing/NDKDrawing/entry/src/main/cpp/types/libentry/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..81c455d5358d1bce93a9e8c15f6800c7910c92b7 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/types/libentry/index.d.ts @@ -0,0 +1,14 @@ +/* + * 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. + */ diff --git a/Drawing/NDKDrawing/entry/src/main/cpp/types/libentry/oh-package.json5 b/Drawing/NDKDrawing/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2551436a2c7ce37d6d8d7cb7ffa81caa184944b0 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,20 @@ +/* + * 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. + */ +{ + "name": "libentry.so", + "types": "./index.d.ts", + "version": "12", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/ets/entryability/EntryAbility.ets b/Drawing/NDKDrawing/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..4e3af23763f0e51777efa68f6b9e4e8598c8ebf8 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,56 @@ +/* + * 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 { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('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(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/Drawing/NDKDrawing/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/Drawing/NDKDrawing/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..be794f48ccffc46278e76ce3493088a647b0d470 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,26 @@ +/* + * 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/ets/interface/XComponentContext.ts b/Drawing/NDKDrawing/entry/src/main/ets/interface/XComponentContext.ts new file mode 100644 index 0000000000000000000000000000000000000000..e463cb1f4af681201f63e5b5ecafffeaa9feac6c --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/ets/interface/XComponentContext.ts @@ -0,0 +1,19 @@ +/* + * 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. + */ +export default interface XComponentContext { + drawPattern(): void; + + drawText(): void; +}; diff --git a/Drawing/NDKDrawing/entry/src/main/ets/pages/Index.ets b/Drawing/NDKDrawing/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..46c39f33ec3f489de707fa4aab5922ef121d0cdf --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,71 @@ +/* + * 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 XComponentContext from "../interface/XComponentContext"; + +const TAG = '[Sample_DrawingAPI]'; + +@Entry +@Component +struct Index { + private xComponentContext: XComponentContext | undefined = undefined; + + build() { + Column() { + Row() { + XComponent({ id: 'xcomponentId', type: 'surface', libraryname: 'entry' }) + .onLoad((xComponentContext) => { + this.xComponentContext = xComponentContext as XComponentContext; + }).width('100%') // Multiples of 64 + }.height('88%') + .backgroundColor(Color.White) + Row() { + Button('Draw Path') + .fontSize('16fp') + .fontWeight(500) + .margin({ bottom: 24, right: 12 }) + .onClick(() => { + console.log(TAG, "Draw Path click"); + if (this.xComponentContext) { + console.log(TAG, "Draw Path"); + this.xComponentContext.drawPattern(); + } + }) + .width('33.6%') + .height(40) + .shadow(ShadowStyle.OUTER_DEFAULT_LG) + Button('Draw Text') + .fontSize('16fp') + .fontWeight(500) + .margin({ bottom: 24, left: 12 }) + .onClick(() => { + console.log(TAG, "draw text click"); + if (this.xComponentContext) { + console.log(TAG, "draw text"); + this.xComponentContext.drawText(); + } + }) + .width('33.6%') + .height(40) + .shadow(ShadowStyle.OUTER_DEFAULT_LG) + } + .width('100%') + .justifyContent(FlexAlign.Center) + .shadow(ShadowStyle.OUTER_DEFAULT_SM) + .alignItems(VerticalAlign.Bottom) + .layoutWeight(1) + } + } +} diff --git a/Drawing/NDKDrawing/entry/src/main/module.json5 b/Drawing/NDKDrawing/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8e24b46aa7aa5804bfdfa715477be1ff485c1b9d --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/module.json5 @@ -0,0 +1,51 @@ +/* + * 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. + */ +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:icon", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ] + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/resources/base/element/color.json b/Drawing/NDKDrawing/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/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/Drawing/NDKDrawing/entry/src/main/resources/base/element/string.json b/Drawing/NDKDrawing/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..b45ef5321b99d03ab0cf68f4b94c21171206b38b --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "NDKDrawing" + } + ] +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/resources/base/media/background.png b/Drawing/NDKDrawing/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/Drawing/NDKDrawing/entry/src/main/resources/base/media/background.png differ diff --git a/Drawing/NDKDrawing/entry/src/main/resources/base/media/foreground.png b/Drawing/NDKDrawing/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/Drawing/NDKDrawing/entry/src/main/resources/base/media/foreground.png differ diff --git a/Drawing/NDKDrawing/entry/src/main/resources/base/media/icon.png b/Drawing/NDKDrawing/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..cd45accb1dfd2fd0da16c732c72faa6e46b26521 Binary files /dev/null and b/Drawing/NDKDrawing/entry/src/main/resources/base/media/icon.png differ diff --git a/Drawing/NDKDrawing/entry/src/main/resources/base/media/layered_image.json b/Drawing/NDKDrawing/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/resources/base/media/startIcon.png b/Drawing/NDKDrawing/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..366f76459ffd4494ec40d0ddd5c59385b9c5da11 Binary files /dev/null and b/Drawing/NDKDrawing/entry/src/main/resources/base/media/startIcon.png differ diff --git a/Drawing/NDKDrawing/entry/src/main/resources/base/profile/backup_config.json b/Drawing/NDKDrawing/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/resources/base/profile/main_pages.json b/Drawing/NDKDrawing/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/Drawing/NDKDrawing/entry/src/main/resources/en_US/element/string.json b/Drawing/NDKDrawing/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..b45ef5321b99d03ab0cf68f4b94c21171206b38b --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "NDKDrawing" + } + ] +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/main/resources/zh_CN/element/string.json b/Drawing/NDKDrawing/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..2484373118b3f7d4589d509e889eb1b1aec38c3a --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "模块描述" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "NDKDrawing" + } + ] +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/mock/Libentry.mock.ets b/Drawing/NDKDrawing/entry/src/mock/Libentry.mock.ets new file mode 100644 index 0000000000000000000000000000000000000000..44d5c96308f6f1f6d4a0919017a153c648a1db8e --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/mock/Libentry.mock.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. + */ +const nativeMock: Record = { + 'add': (a: number, b: number) => { + return a + b; + }, +}; + +export default nativeMock; \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/mock/mock-config.json5 b/Drawing/NDKDrawing/entry/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a1e6f06e0cbdf8485bcab6838e9f88ea81ef5a2b --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/mock/mock-config.json5 @@ -0,0 +1,19 @@ +/* + * 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. + */ +{ + "libentry.so": { + "source": "src/mock/Libentry.mock.ets" + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/ohosTest/ets/test/DrawingAbility.test.ets b/Drawing/NDKDrawing/entry/src/ohosTest/ets/test/DrawingAbility.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..28c4ee4132b26468ff2b4444552d6650213d53b1 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/ohosTest/ets/test/DrawingAbility.test.ets @@ -0,0 +1,99 @@ +/* + * 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 AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import { describe, it, expect } from '@ohos/hypium'; +import { Driver, ON } from '@ohos.UiTest'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const TAG = '[Sample_NDKDrawingAPI]'; +const DOMAIN = 0xF811 +const BUNDLE = 'NDKDrawingAPI_' + +export default function abilityTest() { + + describe('ActsAbilityTest', () => { + /** + * 打开应用 + */ + it(BUNDLE + 'StartAbility_001', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'StartAbility_001 begin') + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.cdrawing', + abilityName: 'EntryAbility' + }); + } catch (exception) { + hilog.info(DOMAIN, TAG, BUNDLE + `StartAbility_001 exception = ${JSON.stringify(exception)}`) + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Draw Path')); + done(); + hilog.info(DOMAIN, TAG, BUNDLE + 'StartAbility_001 end') + }) + + /** + * 点击按钮,绘制图形 + */ + it(BUNDLE + 'DrawPath_001', 2, async () => { + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawPath_001 begin') + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.cdrawing', + abilityName: 'EntryAbility' + }); + } catch (exception) { + hilog.info(DOMAIN, TAG, BUNDLE + `DrawPath_001 exception = ${JSON.stringify(exception)}`) + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Draw Path')); + let drawPathBtn = await driver.findComponent(ON.text('Draw Path')); + // 点击'Draw Path'按钮 + await drawPathBtn.click(); + await driver.delayMs(1000); + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawPath_001 end') + }) + + /** + * 点击按钮,绘制文字 + */ + it(BUNDLE + 'DrawText_001', 2, async () => { + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawText_001 begin') + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.cdrawing', + abilityName: 'EntryAbility' + }); + } catch (exception) { + hilog.info(DOMAIN, TAG, BUNDLE + `DrawText_001 exception = ${JSON.stringify(exception)}`) + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Draw Text')); + let drawPathBtn = await driver.findComponent(ON.text('Draw Text')); + // 点击'Draw Text'按钮 + await drawPathBtn.click(); + await driver.delayMs(1000); + hilog.info(DOMAIN, TAG, BUNDLE + 'DrawText_001 end') + }) + }) +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/ohosTest/ets/test/List.test.ets b/Drawing/NDKDrawing/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c6a406e9eb09b5d547967f9e78fd51d0697ba79e --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,19 @@ +/* + * 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 abilityTest from './DrawingAbility.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/ohosTest/ets/testability/TestAbility.ets b/Drawing/NDKDrawing/entry/src/ohosTest/ets/testability/TestAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..7f2910251cff142ba3172279a1adf3cc2d105c71 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/ohosTest/ets/testability/TestAbility.ets @@ -0,0 +1,65 @@ +/* + * 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 UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import hilog from '@ohos.hilog'; +import { Hypium } from '@ohos/hypium'; +import testsuite from '../test/List.test'; +import window from '@ohos.window'; +import Want from '@ohos.app.ability.Want'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + +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, data) => { + 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. Data: %{public}s', + JSON.stringify(data) ?? ''); + }); + } + + 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/Drawing/NDKDrawing/entry/src/ohosTest/ets/testability/pages/Index.ets b/Drawing/NDKDrawing/entry/src/ohosTest/ets/testability/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8802c95f2d40f05b883be987f877930d4a5416a9 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/ohosTest/ets/testability/pages/Index.ets @@ -0,0 +1,31 @@ +/* + * 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. + */ +@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/Drawing/NDKDrawing/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ets b/Drawing/NDKDrawing/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ets new file mode 100644 index 0000000000000000000000000000000000000000..5515fee5e71eeddcd97d98a4b41d7c9f06baeea9 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ets @@ -0,0 +1,61 @@ +/* + * 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 hilog from '@ohos.hilog'; +import TestRunner from '@ohos.application.testRunner'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import Want from '@ohos.app.ability.Want'; + +let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator | undefined = undefined +let abilityDelegatorArguments: AbilityDelegatorRegistry.AbilityDelegatorArgs | undefined = undefined + +async function onAbilityCreateCallback() { + hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); +} + +async function addAbilityMonitorCallback(err : Error) { + 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() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + const bundleName = abilityDelegatorArguments.bundleName; + const testAbilityName = 'TestAbility'; + let lMonitor: AbilityDelegatorRegistry.AbilityMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) + const want: Want = { + bundleName: bundleName, + abilityName: testAbilityName + }; + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + abilityDelegator.startAbility(want, (err, data) => { + hilog.info(0x0000, 'testTag', 'startAbility : err : %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, 'testTag', 'startAbility : data : %{public}s',JSON.stringify(data) ?? ''); + }) + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ts b/Drawing/NDKDrawing/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ts new file mode 100644 index 0000000000000000000000000000000000000000..9c68c9e5fcfba563574459c2fa4f8c328d73590f --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ts @@ -0,0 +1,63 @@ +/* + * 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 hilog from '@ohos.hilog'; +import TestRunner from '@ohos.application.testRunner'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; + +var abilityDelegator = undefined +var abilityDelegatorArguments = undefined + +async function onAbilityCreateCallback() { + hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); +} + +async function addAbilityMonitorCallback(err: any) { + 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() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' + let lMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) + var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName + var debug = abilityDelegatorArguments.parameters['-D'] + if (debug == 'true') + { + cmd += ' -D' + } + hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd); + abilityDelegator.executeShellCommand(cmd, + (err: any, d: any) => { + hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? ''); + }) + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); + } +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/ohosTest/module.json5 b/Drawing/NDKDrawing/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7bfda1a783cf60783558deee959c7be643f7e9b2 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/ohosTest/module.json5 @@ -0,0 +1,51 @@ +/* + * 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. + */ +{ + "module": { + "name": "entry_test", + "type": "feature", + "description": "$string:module_test_desc", + "mainElement": "TestAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "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" + ] + } + ] + } + ] + } +} diff --git a/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/element/color.json b/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/Drawing/NDKDrawing/entry/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/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/element/string.json b/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..65d8fa5a7cf54aa3943dcd0214f58d1771bc1f6c --- /dev/null +++ b/Drawing/NDKDrawing/entry/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/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/media/icon.png b/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/media/icon.png differ diff --git a/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/profile/test_pages.json b/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/profile/test_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..b7e7343cacb32ce982a45e76daad86e435e054fe --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/ohosTest/resources/base/profile/test_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "testability/pages/Index" + ] +} diff --git a/Drawing/NDKDrawing/entry/src/test/List.test.ets b/Drawing/NDKDrawing/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..3bc7c739d1957b3a778de67db8992240584b6692 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/test/List.test.ets @@ -0,0 +1,19 @@ +/* + * 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 localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/Drawing/NDKDrawing/entry/src/test/LocalUnit.test.ets b/Drawing/NDKDrawing/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..4ab69eca22877101c08c26f8a92322edddb739f8 --- /dev/null +++ b/Drawing/NDKDrawing/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,47 @@ +/* + * 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'; + +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 diff --git a/Drawing/NDKDrawing/hvigor/hvigor-config.json5 b/Drawing/NDKDrawing/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..87990affc4f62f7c76c246428c40272d3be0a7c7 --- /dev/null +++ b/Drawing/NDKDrawing/hvigor/hvigor-config.json5 @@ -0,0 +1,36 @@ +/* + * 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. + */ +{ + "modelVersion": "5.0.0", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/Drawing/NDKDrawing/hvigorfile.ts b/Drawing/NDKDrawing/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2e7b775e53d303786e9503843d93a55e59c7f4f2 --- /dev/null +++ b/Drawing/NDKDrawing/hvigorfile.ts @@ -0,0 +1,20 @@ +/* + * 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 { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/Drawing/NDKDrawing/oh-package.json5 b/Drawing/NDKDrawing/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7a7fe7781e61280b6e0a6f437d4a7a6a41b2afcd --- /dev/null +++ b/Drawing/NDKDrawing/oh-package.json5 @@ -0,0 +1,24 @@ +/* + * 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. + */ +{ + "modelVersion": "5.0.0", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/Drawing/NDKDrawing/ohosTest.md b/Drawing/NDKDrawing/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..63edec9a5241021ab5f6ed31eea0e6bb6e1d2fcc --- /dev/null +++ b/Drawing/NDKDrawing/ohosTest.md @@ -0,0 +1,10 @@ +# NdkDrawing 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| -------- | ------------ | ----------------- | ----------------------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 绘制路径 | 位于首页 | 点击**Draw Path** | 页面显示出一个五角星 | 是 | Pass | +| 绘制文字 | 位于首页 | 点击**Draw Text** | 页面显示出一个“Hello World Drawing” | 是 | Pass | + diff --git a/Drawing/NDKDrawing/screenshot/path.png b/Drawing/NDKDrawing/screenshot/path.png new file mode 100644 index 0000000000000000000000000000000000000000..befd906dfc127cb4c7c114cd0b99bbf5534207f5 Binary files /dev/null and b/Drawing/NDKDrawing/screenshot/path.png differ diff --git a/Drawing/NDKDrawing/screenshot/text.png b/Drawing/NDKDrawing/screenshot/text.png new file mode 100644 index 0000000000000000000000000000000000000000..442d22a5894bfcb54e61e8a7cd2857e653b5973f Binary files /dev/null and b/Drawing/NDKDrawing/screenshot/text.png differ