diff --git a/BackGroundTasksKit/NativeTransientTask/.gitignore b/BackGroundTasksKit/NativeTransientTask/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/.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/BackGroundTasksKit/NativeTransientTask/AppScope/app.json5 b/BackGroundTasksKit/NativeTransientTask/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b2995904199d43e94ba26b67669023cc937a2c76 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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": "ohos.samples.nativetransienttask", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:layered_image", + "label": "$string:app_name" + } +} diff --git a/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/element/string.json b/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d71e5ead1ce20b4ac3384d18e6d104f1ea4d83b4 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "MyApplication" + } + ] +} diff --git a/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/media/background.png b/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/media/background.png differ diff --git a/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/media/foreground.png b/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..eb9427585b36d14b12477435b6419d1f07b3e0bb Binary files /dev/null and b/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/media/foreground.png differ diff --git a/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/media/layered_image.json b/BackGroundTasksKit/NativeTransientTask/AppScope/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/AppScope/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/BackGroundTasksKit/NativeTransientTask/README_zh.md b/BackGroundTasksKit/NativeTransientTask/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..0f2e199c1b007715c08853d252dba121e4e188dc --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/README_zh.md @@ -0,0 +1,62 @@ +# 短时任务 + +### 介绍 + +本示例主要展示后台任务中的短时任务。 + +通过[transient_task_api](https://gitcode.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-backgroundtasks-kit/capi-transient-task-api-h.md)接口,实现应用申请短时任务的功能。 + +### 效果预览 + +|退后台前 |退后台一段时间在进前台 | +|---------------------------------------|-------------------------------------| +|![image](screenshots/device/before.png) |![image](screenshots/device/after.png)| + +使用说明 + +1.进入应用,点击申请短时任务按钮; + +2.短时任务Id有变化; + +### 工程目录 +``` +entry/src/main +|---cpp +| |---CMakeLists.txt // 配置库依赖 +| |---napi_init.cpp // 封装接口并注册模块 +|---ets/page +| |---Index.ets // 首页 +``` +### 具体实现 + +* 该示例使用requestSuspendDelay方法申请短时任务,延迟挂起,setInterval方法进行计数,实现应用退后台仍然可以计数的功能。 +* 源码链接:[CMakeLists.txt](entry/src/main/cpp/CMakeLists.txt),[napi_init.cpp](entry/src/main/cpp/napi_init.cpp), +* 接口参考:[transient_task_api](https://gitcode.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-backgroundtasks-kit/capi-transient-task-api-h.md) + +### 相关权限 + +不涉及 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行,支持设备:RK3568; + +2.本示例已适配API version 9版本SDK,版本号:3.2.11.9; + +3.本示例需要使用DevEco Studio 6.0.1 Beta1 (Build Version: 6.0.1.246, built on October 31, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: +``` +git init +git config core.sparsecheckout true +echo code/BasicFeature/TaskManagement/NativeTransientTask > .git/info/sparse-checkout +git remote add origin https://gitcode.com/openharmony/applications_app_samples.git +git pull origin master + +``` \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/build-profile.json5 b/BackGroundTasksKit/NativeTransientTask/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4cc3a52a1cf647348548f8ec539bfc974fad8858 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/build-profile.json5 @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 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": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "targetSdkVersion": "6.0.1(21)", + "compatibleSdkVersion": "6.0.1(21)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "nativeCompiler": "BiSheng", + "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/BackGroundTasksKit/NativeTransientTask/code-linter.json5 b/BackGroundTasksKit/NativeTransientTask/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ed05653cca31b61d64cf6471529eaf50d4f70709 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/code-linter.json5 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2025 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": { + "@security/no-unsafe-aes": "error", + "@security/no-unsafe-hash": "error", + "@security/no-unsafe-mac": "warn", + "@security/no-unsafe-dh": "error", + "@security/no-unsafe-dsa": "error", + "@security/no-unsafe-ecdsa": "error", + "@security/no-unsafe-rsa-encrypt": "error", + "@security/no-unsafe-rsa-sign": "error", + "@security/no-unsafe-rsa-key": "error", + "@security/no-unsafe-dsa-key": "error", + "@security/no-unsafe-dh-key": "error", + "@security/no-unsafe-3des": "error" + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/.gitignore b/BackGroundTasksKit/NativeTransientTask/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/build-profile.json5 b/BackGroundTasksKit/NativeTransientTask/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f3d112306bd8c6ec733f59e86a9dd7450a60a8a8 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/build-profile.json5 @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2025 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": { + "resOptions": { + "copyCodeResource": { + "enable": false + } + }, + "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/BackGroundTasksKit/NativeTransientTask/entry/hvigorfile.ts b/BackGroundTasksKit/NativeTransientTask/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..cfa8a00f74f409d9647f55cdf270ab6aec69fe41 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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. */ +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/obfuscation-rules.txt b/BackGroundTasksKit/NativeTransientTask/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/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/BackGroundTasksKit/NativeTransientTask/entry/oh-package.json5 b/BackGroundTasksKit/NativeTransientTask/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5d993e5251fd56950970aa593aefef1b8d71e976 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/CMakeLists.txt b/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..3cefc6412aa7bfd7c5e585b2cf67af18095e9310 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,16 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(NativeTransientTask) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so) +target_link_libraries(entry PUBLIC libace_napi.z.so libtransient_task.so) \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/napi_init.cpp b/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c0d93d3e3032457fe98eb33bbfdcebe539f5a92 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2025 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. + */ + +// [Start encapsulation_function] +#include "napi/native_api.h" +#include "transient_task/transient_task_api.h" + +TransientTask_DelaySuspendInfo delaySuspendInfo; +const int32_t TransientTask_TIMER = 3; +static void Callback(void) +{ + // 短时任务即将结束,业务在这里取消短时任务 + OH_BackgroundTaskManager_CancelSuspendDelay(delaySuspendInfo.requestId); +} + +// 申请短时任务 +static napi_value RequestSuspendDelay(napi_env env, napi_callback_info info) +{ + napi_value result; + int32_t res = OH_BackgroundTaskManager_RequestSuspendDelay("test", Callback, &delaySuspendInfo); + if (res == 0) { + napi_create_int32(env, delaySuspendInfo.requestId, &result); + } else { + napi_create_int32(env, -1, &result); + } + return result; +} + +// 获取剩余时间 +static napi_value GetRemainingDelayTime(napi_env env, napi_callback_info info) +{ + napi_value result; + int32_t delayTime = 0; + int32_t res = OH_BackgroundTaskManager_GetRemainingDelayTime(delaySuspendInfo.requestId, &delayTime); + if (res == 0) { + napi_create_int32(env, delayTime, &result); + } else { + napi_create_int32(env, -1, &result); + } + return result; +} + +// 取消短时任务 +static napi_value CancelSuspendDelay(napi_env env, napi_callback_info info) +{ + napi_value result; + int32_t res = OH_BackgroundTaskManager_CancelSuspendDelay(delaySuspendInfo.requestId); + napi_create_int32(env, res, &result); + return result; +} + +// 获取所有短时任务信息 +TransientTask_TransientTaskInfo transientTaskInfo; + +static napi_value GetTransientTaskInfo(napi_env env, napi_callback_info info) +{ + napi_value result; + napi_create_object(env, &result); + int32_t res = OH_BackgroundTaskManager_GetTransientTaskInfo(&transientTaskInfo); + napi_value napiRemainingQuota = nullptr; + // 获取成功,格式化数据并返回给接口 + if (res == 0) { + napi_create_int32(env, transientTaskInfo.remainingQuota, &napiRemainingQuota); + napi_set_named_property(env, result, "remainingQuota", napiRemainingQuota); // 格式化当日总配额 + + napi_value info {nullptr}; + napi_create_array(env, &info); + uint32_t count = 0; + // 格式化所有已申请的短时任务信息 + for (int index = 0; index < TransientTask_TIMER; index++) { + if (transientTaskInfo.transientTasks[index].requestId == 0) { + continue; + } + + napi_value napiWork = nullptr; + napi_create_object(env, &napiWork); + + napi_value napiRequestId = nullptr; + napi_create_int32(env, transientTaskInfo.transientTasks[index].requestId, &napiRequestId); + napi_set_named_property(env, napiWork, "requestId", napiRequestId); + + napi_value napiActualDelayTime = nullptr; + napi_create_int32(env, transientTaskInfo.transientTasks[index].actualDelayTime, &napiActualDelayTime); + napi_set_named_property(env, napiWork, "actualDelayTime", napiActualDelayTime); + + napi_set_element(env, info, count, napiWork); + count++; + } + napi_set_named_property(env, result, "transientTasks", info); + } else { + napi_create_int32(env, 0, &napiRemainingQuota); + napi_set_named_property(env, result, "remainingQuota", napiRemainingQuota); + napi_value info {nullptr}; + napi_create_array(env, &info); + napi_set_named_property(env, result, "transientTasks", info); + } + return result; +} + +// [End encapsulation_function] + +// [Start registration_function] +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + {"RequestSuspendDelay", nullptr, RequestSuspendDelay, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"GetRemainingDelayTime", nullptr, GetRemainingDelayTime, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"CancelSuspendDelay", nullptr, CancelSuspendDelay, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"GetTransientTaskInfo", nullptr, GetTransientTaskInfo, nullptr, nullptr, nullptr, napi_default, nullptr }, + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END +// [End registration_function] + +// [Start registration_module] +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); +} +// [End registration_module] \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/types/libentry/Index.d.ts b/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..b8ee4ca7592ac02af1c96ea31e7e14f4ef2d0883 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 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. + */ + +// [Start declaration_function] +import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager'; + +export const RequestSuspendDelay: () => number; +export const GetRemainingDelayTime: () => number; +export const CancelSuspendDelay: () => number; +export const GetTransientTaskInfo: () => backgroundTaskManager.TransientTaskInfo; +// [End declaration_function] \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/types/libentry/oh-package.json5 b/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..846e4c7e13ead48abe6019bd40f3a13bf8f9c083 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/entryability/EntryAbility.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..6a95f638674014a6d6088fb8a25f00854a309e53 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2025 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, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + try { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + } catch (err) { + hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err)); + } + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..1fca37ecc34e1079aaf6557f1272cd694aa0068c --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 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'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/pages/Index.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8d280a948623176ce954aef5331430ca83e17201 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2025 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. + */ + +// [Start native_transient_task] +import testTransientTask from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = ''; + @State requestId: number = 0; + + build() { + Row() { + Column() { + // [StartExclude native_transient_task] + Row(){ + Text($r('app.string.transient_task_id')).fontSize(20); + Text(this.requestId.toString()).fontSize(20).id('transient_task_id'); + } + // [EndExclude native_transient_task] + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button($r('app.string.request_suspend_delay')) + .onClick(event => { + this.RequestSuspendDelay(); + }) + // [StartExclude native_transient_task] + .id('request_suspend_delay') + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + // [EndExclude native_transient_task] + Button($r('app.string.get_remaining_delay_time')) + .onClick(event =>{ + this.GetRemainingDelayTime(); + }) + // [StartExclude native_transient_task] + .id('get_remaining_delay_time') + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + // [EndExclude native_transient_task] + Button($r('app.string.cancel_suspend_delay')) + .onClick(event =>{ + this.CancelSuspendDelay(); + }) + // [StartExclude native_transient_task] + .id('cancel_suspend_delay') + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + // [EndExclude native_transient_task] + Button($r('app.string.get_transient_task_info')) + .onClick(event =>{ + this.GetTransientTaskInfo(); + }) + // [StartExclude native_transient_task] + .id('get_transient_task_info') + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + // [EndExclude native_transient_task] + } + .width('100%') + } + .height('100%') + } + + RequestSuspendDelay() { + let requestId = testTransientTask.RequestSuspendDelay(); + // [StartExclude native_transient_task] + this.requestId = requestId; + // [EndExclude native_transient_task] + console.log('The return requestId is ' + requestId); + } + + GetRemainingDelayTime() { + let time = testTransientTask.GetRemainingDelayTime(); + console.log('The time is ' + time); + } + + CancelSuspendDelay() { + let ret = testTransientTask.CancelSuspendDelay(); + console.log('The ret is ' + ret); + } + + GetTransientTaskInfo() { + let ret = testTransientTask.GetTransientTaskInfo(); + console.log('The ret is ' + JSON.stringify(ret)); + } +} +// [End native_transient_task] diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/module.json5 b/BackGroundTasksKit/NativeTransientTask/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e93dadfa773b13cd6a84d2a3916544f4e6e22766 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/module.json5 @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2025 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": [ + "phone" + ], + "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": [ + "ohos.want.action.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/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/element/color.json b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..d66f9a7d4ac61fb8d215239ab3620b7bcd77bf33 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/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/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/element/float.json b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..a8a5d404dcd8b0466194afc3aa25d90a8a327470 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/element/string.json b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..67d5bb24eb88d609f247423673152552d05d76db --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/element/string.json @@ -0,0 +1,36 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "label" + }, + { + "name": "request_suspend_delay", + "value": "申请短时任务" + }, + { + "name": "get_remaining_delay_time", + "value": "获取剩余时间" + }, + { + "name": "cancel_suspend_delay", + "value": "取消短时任务" + }, + { + "name": "get_transient_task_info", + "value": "获取所有短时任务信息" + }, + { + "name": "transient_task_id", + "value": "短时任务Id:" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/background.png b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/background.png differ diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/foreground.png b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/foreground.png differ diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/layered_image.json b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/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/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/startIcon.png b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/media/startIcon.png differ diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/profile/backup_config.json b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..d742c2f96e7dd0f406f499941f3147345e998f95 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/profile/main_pages.json b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/dark/element/color.json b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..438d5bc43bb23c59c210d586b96635a72da5b64a --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/TestRunner/OpenHarmonyTestRunner.ts b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/TestRunner/OpenHarmonyTestRunner.ts new file mode 100644 index 0000000000000000000000000000000000000000..b4fdba969e254b0e6cec7e7c3391e107bc90b42d --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/TestRunner/OpenHarmonyTestRunner.ts @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2025 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'; + +let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator | undefined = undefined; +let abilityDelegatorArguments: AbilityDelegatorRegistry.AbilityDelegatorArgs | undefined = undefined; + +async function onAbilityCreateCallback(): Promise { + hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); +} + +async function addAbilityMonitorCallback(err: any): Promise { + hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); +} + +export default class OpenHarmonyTestRunner implements TestRunner { + constructor() { + } + + onPrepare(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare '); + } + + async onRun(): Promise { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments(); + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + let testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'; + let lMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback); + let cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName; + let 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/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/test/Ability.test.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7ec458d91a71e8b1ad4e55b8d99b008fd1c0f8dc --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2025 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; +import { Driver, ON } from '@kit.TestKit'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; + +const BUNDLE = 'NativeTransientTaskTest_'; +const TAG = '[Sample_NativeTransientTask]'; +const DOMAIN = 0xF811; + +export default function abilityTest() { + let driver: Driver = Driver.create(); + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + + /** + * 拉起应用 ohos.samples.nativetransienttask + */ + it(BUNDLE + 'StartAbility_001', 0, async (done: Function) => { + console.info(TAG, `${BUNDLE}CameraAbility_StartAbility_001 start`); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'ohos.samples.nativetransienttask', + abilityName: 'EntryAbility' + }) + expect(true).assertTrue(); + } catch (exception) { + console.info(TAG, `${BUNDLE}Failed to start ability, error info: ${JSON.stringify(exception)}`); + expect().assertFail(); + } + await driver.delayMs(1000); + console.info(TAG, `${BUNDLE}StartAbility_001 end`); + done(); + }) + + /** + * 判断起应用后,判断控件是否存在 + */ + it(BUNDLE + 'ComponentIfExist_001', 0, async (done: Function) => { + console.info(TAG, `${BUNDLE}ComponentIfExist_001 start`); + await driver.delayMs(1000); + // 测试控件是否存在 + await driver.assertComponentExist(ON.id('transient_task_id')); + await driver.assertComponentExist(ON.id('request_suspend_delay')); + await driver.assertComponentExist(ON.id('get_remaining_delay_time')); + await driver.assertComponentExist(ON.id('cancel_suspend_delay')); + await driver.assertComponentExist(ON.id('get_transient_task_info')); + expect(true).assertTrue(); + console.info(TAG, `${BUNDLE}ComponentIfExist_001 end`); + done(); + }) + + /** + * 测试控件点击, 判断短时任务是否申请成功 + */ + it(BUNDLE + 'requestSuspendDelay_003', 0, async (done: Function) => { + console.info(TAG, `${BUNDLE}requestSuspendDelay_003 start`); + await driver.delayMs(1000); + let taskIdAfter = ''; + let requestBtn = await driver.findComponent(ON.id('request_suspend_delay')); + let taskId = await driver.findComponent(ON.id('transient_task_id')); + await requestBtn.click(); + await driver.delayMs(1000); + await taskId.getText().then(value => { + taskIdAfter = value; + console.info(TAG, `${BUNDLE}requestSuspendDelay_003 after requestBtn: ` + taskIdAfter); + }) + if (taskIdAfter === '0') { + expect(false).assertTrue(); + } + expect(true).assertTrue(); + await driver.delayMs(1000); + console.info(TAG, `${BUNDLE}requestSuspendDelay_003 end`); + done(); + }) + }) +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/test/List.test.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 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 './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/testability/TestAbility.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/testability/TestAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..85019b83838505a520a6ccaac08997394e70caae --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/testability/TestAbility.ets @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2025 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): void { + 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.getAbilityDelegator(); + let abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments(); + hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + 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(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy'); + } + + onForeground(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground'); + } + + onBackground(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground'); + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/testability/pages/Index.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/testability/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..807149e3e16c9a4b0a3dd1648d13f73bd9c00b51 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/ets/testability/pages/Index.ets @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 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'; + +@Entry +@Component +struct Index { + aboutToAppear() { + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear'); + } + + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('next page') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(() => { + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/module.json5 b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0acc0d50227727d0c325963ee3ed28c730678277 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/module.json5 @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 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:entry_test_desc", + "mainElement": "TestAbility", + "deviceTypes": [ + "default", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:test_pages", + "abilities": [ + { + "name": "TestAbility", + "srcEntry": "./ets/testability/TestAbility.ets", + "description": "$string:TestAbility_desc", + "icon": "$media:icon", + "label": "$string:TestAbility_label", + "visible": true, + "startWindowIcon": "$media:icon", + "startWindowBackground": "$color:white", + "skills": [ + { + "actions": [ + "action.system.home" + ], + "entities": [ + "entity.system.home" + ] + } + ], + } + ] + } +} diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/element/color.json b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..1bbc9aa9617e97c45440e1d3d66afc1154837012 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "white", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/element/string.json b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..36d4230c53e9f5a07ae343ad8dc9808341975e3b --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "entry_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/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/media/icon.png b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/media/icon.png differ diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/profile/test_pages.json b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/profile/test_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..b7e7343cacb32ce982a45e76daad86e435e054fe --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/ohosTest/resources/base/profile/test_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "testability/pages/Index" + ] +} diff --git a/BackGroundTasksKit/NativeTransientTask/entry/src/test/List.test.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 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/BackGroundTasksKit/NativeTransientTask/entry/src/test/LocalUnit.test.ets b/BackGroundTasksKit/NativeTransientTask/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 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/BackGroundTasksKit/NativeTransientTask/hvigor/hvigor-config.json5 b/BackGroundTasksKit/NativeTransientTask/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..80c167a1299082893434056345158f142c6ab06a --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/hvigor/hvigor-config.json5 @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 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": "6.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | "ultrafine" | 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 */ + // "optimizationStrategy": "memory" /* Define the optimization strategy. Value: [ "memory" | "performance" ]. Default: "memory" */ + }, + "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/BackGroundTasksKit/NativeTransientTask/hvigorfile.ts b/BackGroundTasksKit/NativeTransientTask/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e3340f07e45ddc5dcadbb87012668555def2e6e0 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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. */ +} \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/oh-package.json5 b/BackGroundTasksKit/NativeTransientTask/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b94b14436026cef4113ab5e0f93df09d3af69b0e --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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": "6.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.24", + "@ohos/hamock": "1.0.0" + } +} diff --git a/BackGroundTasksKit/NativeTransientTask/ohosTest.md b/BackGroundTasksKit/NativeTransientTask/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..3dbf127f7a711a16bdbd04dc1b66e39f5df632b7 --- /dev/null +++ b/BackGroundTasksKit/NativeTransientTask/ohosTest.md @@ -0,0 +1,13 @@ +# NativeTransientTask 测试用例归档 + +## 用例表 + +|测试功能|预置条件| 输入 | 预期输出 |测试结果| +|--------------------------------|--------------------------------|--------------|-----------|--------------------------------| +|拉起应用| 设备正常运行| | 成功拉起应用 |Pass| +|短时任务开启功能| 位于主页| 点击申请短时任务 | 申请短时任务成功 |Pass| +|短时任务最多申请3次功能| 位于主页| 点击3次申请短时任务 | 申请短时任务成功 |Pass| +|短时任务最多申请3次功能| 位于主页| 点击第4申请次短时任务 | 申请短时任务失败 |Pass| +|获取剩余时间功能| 位于主页| 点击获取剩余时间 | 获取剩余时间成功 |Pass| +|获取所有短时任务信息功能| 位于主页| 点击获取所有短时任务信息 | 获取所有短时任务信息成功 |Pass| +|取消短时任务功能| 位于桌面| 点击取消短时任务 | 取消短时任务成功 |Pass| \ No newline at end of file diff --git a/BackGroundTasksKit/NativeTransientTask/screenshots/device/after.png b/BackGroundTasksKit/NativeTransientTask/screenshots/device/after.png new file mode 100644 index 0000000000000000000000000000000000000000..2d202eac039f863338f72be07d44d0a0dadc2266 Binary files /dev/null and b/BackGroundTasksKit/NativeTransientTask/screenshots/device/after.png differ diff --git a/BackGroundTasksKit/NativeTransientTask/screenshots/device/before.png b/BackGroundTasksKit/NativeTransientTask/screenshots/device/before.png new file mode 100644 index 0000000000000000000000000000000000000000..23f2b523704dc2bb278cd590d9706074a28116b6 Binary files /dev/null and b/BackGroundTasksKit/NativeTransientTask/screenshots/device/before.png differ