diff --git a/test/BUILD.gn b/test/BUILD.gn index 03d81d71a970cdc3c62123e4e0e0bbfc8e1ee376..48427768f323095a08b3ffc8d67fce1813abd570 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 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 @@ -26,5 +26,8 @@ group("moduletest") { group("unittest") { testonly = true - deps = [ "unittest/bm:unittest" ] + deps = [ + "sceneProject:test_hap", + "unittest/bm:unittest", + ] } diff --git a/test/mock/mock_bundle_installer_host.h b/test/mock/mock_bundle_installer_host.h index 3d3dc78dd6cccd76b4efcdc0ab960f6612990bb0..f691abbf7af8d7f18289dfd2c27d638d271a2481 100644 --- a/test/mock/mock_bundle_installer_host.h +++ b/test/mock/mock_bundle_installer_host.h @@ -33,6 +33,7 @@ const std::string STRING_OTHER_BUNDLE_PATH = "/data/test/bundle_test/othertestde const std::string STRING_BUNDLE_INSTALL_PATH1 = "../../test/bundle_test1/"; const std::string STRING_BUNDLE_INSTALL_PATH2 = "../../test/bundle_test2/"; const std::string STRING_BUNDLE_NAME = "name"; +const std::string TEST_BUNDLE_NAME = "com.query.test"; const std::string STRING_MODULE_NAME = "module"; const std::string STRING_ABILITY_NAME = "ability"; const std::string DEFAULT_USER_ID = "100"; diff --git a/test/sceneProject/BUILD.gn b/test/sceneProject/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e501acd4a656091fe1c88d7b26f4bcd85b3cc9e1 --- /dev/null +++ b/test/sceneProject/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (c) 2023 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. + +group("test_hap") { + testonly = true + deps = [] + + deps += [ + # deps file + "unittest:unittest_hap", + ] +} diff --git a/test/sceneProject/signature/com.query.test.p7b b/test/sceneProject/signature/com.query.test.p7b new file mode 100644 index 0000000000000000000000000000000000000000..4440231e360e150a9e2c02c2583da6c5feaf0f6d Binary files /dev/null and b/test/sceneProject/signature/com.query.test.p7b differ diff --git a/test/sceneProject/unittest/BUILD.gn b/test/sceneProject/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..2c628adced755144509a547ef1299b45be3212f5 --- /dev/null +++ b/test/sceneProject/unittest/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (c) 2023 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. + +group("unittest_hap") { + testonly = true + deps = [] + + deps += [ + # deps file + "system_module:distributed_system_module", + ] +} diff --git a/test/sceneProject/unittest/ohos_test/BUILD.gn b/test/sceneProject/unittest/ohos_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b432904f04e9c9a54feaf99a4d70f282fb1412d1 --- /dev/null +++ b/test/sceneProject/unittest/ohos_test/BUILD.gn @@ -0,0 +1,24 @@ +# Copyright (c) 2023 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("//build/ohos.gni") +import("../../../../bundletool.gni") + +ohos_copy("copy_ohos_test") { + part_name = "bundle_tool" + subsystem_name = "bundlemanager" + sources = [ "./ohos_test.xml" ] + outputs = [ + "$root_out_dir/tests/unittest/bundle_tool/tools/resource/ohos_test.xml", + ] +} diff --git a/test/sceneProject/unittest/ohos_test/ohos_test.xml b/test/sceneProject/unittest/ohos_test/ohos_test.xml new file mode 100644 index 0000000000000000000000000000000000000000..d20b3a3e74c59a642ddb10dd664281583c8fbe49 --- /dev/null +++ b/test/sceneProject/unittest/ohos_test/ohos_test.xml @@ -0,0 +1,22 @@ + + + + + + + + \ No newline at end of file diff --git a/test/sceneProject/unittest/system_module/AppScope/app.json b/test/sceneProject/unittest/system_module/AppScope/app.json new file mode 100644 index 0000000000000000000000000000000000000000..309d304163a6f0221d28b5f5bcf955eef1d7ed16 --- /dev/null +++ b/test/sceneProject/unittest/system_module/AppScope/app.json @@ -0,0 +1,23 @@ +{ + "app": { + "bundleName": "com.query.test", + "debug": true, + "icon": "$media:app_icon", + "iconId": 16777217, + "label": "$string:app_name", + "labelId": 16777216, + "description":"$string:description_application", + "descriptionId":1010, + "vendor": "huawei", + "versionCode": 1, + "versionName": "1.0", + "minCompatibleVersionCode":1, + "minAPIVersion": 8, + "targetAPIVersion":8, + "apiReleaseType":"Beta2", + "keepAlive":true, + "removable":false, + "userDataClearable":false, + "accessible":true + } +} diff --git a/test/sceneProject/unittest/system_module/AppScope/resources/base/element/string.json b/test/sceneProject/unittest/system_module/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8b99306e5e9a491d8d3ae5f43851155bd63e0808 --- /dev/null +++ b/test/sceneProject/unittest/system_module/AppScope/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string":[ + { + "name":"app_name", + "value":"16777216" + }, + { + "name":"description_application", + "value":"1010" + } + ] +} \ No newline at end of file diff --git a/test/sceneProject/unittest/system_module/BUILD.gn b/test/sceneProject/unittest/system_module/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..78d4f10198fe3cecc47de33860f4f3e410e380ba --- /dev/null +++ b/test/sceneProject/unittest/system_module/BUILD.gn @@ -0,0 +1,44 @@ +# Copyright (c) 2023 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("//build/ohos.gni") +import("../../../../bundletool.gni") + +ohos_hap("distributed_system_module") { + hap_profile = "entry/src/main/module.json" + hap_name = "distributed_system_module" + subsystem_name = "bundlemanager" + final_hap_path = "$root_out_dir/tests/unittest/bundle_tool/tools/resource/test_bundle/${hap_name}.hap" + testonly = true + deps = [ + ":hjs_demo_js_assets", + ":hjs_demo_resources", + ] + certificate_profile = + "${bundletool_test_path}/sceneProject/signature/com.query.test.p7b" +} + +ohos_app_scope("bmsstagedemoone_app_profile") { + app_profile = "AppScope/app.json" + sources = [ "AppScope/resources" ] +} + +ohos_js_assets("hjs_demo_js_assets") { + source_dir = "entry/src/main/ets" +} + +ohos_resources("hjs_demo_resources") { + sources = [ "entry/src/main/resources" ] + deps = [ ":bmsstagedemoone_app_profile" ] + hap_profile = "entry/src/main/module.json" +} diff --git a/test/sceneProject/unittest/system_module/entry/src/main/ets/MainAbility/GlobalContext.ts b/test/sceneProject/unittest/system_module/entry/src/main/ets/MainAbility/GlobalContext.ts new file mode 100644 index 0000000000000000000000000000000000000000..0a0a885f3e2610c79645c247880b0675dc905ad8 --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/ets/MainAbility/GlobalContext.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 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 {Core} from "deccjsunit/index.ets" +import testsuite from "../../test/List.test.ets" + +export default class GlobalContext { + constructor(){ + } + globalContext(core:Core) { + const configService = core.getDefaultService('config') + console.info('parameters---->' + JSON.stringify(globalThis.abilityWant.parameters)) + globalThis.abilityWant.parameters.timeout = 70000; + configService.setConfig(globalThis.abilityWant.parameters) + testsuite(globalThis.abilityContext) + } +} \ No newline at end of file diff --git a/test/sceneProject/unittest/system_module/entry/src/main/ets/MainAbility/MainAbility.ts b/test/sceneProject/unittest/system_module/entry/src/main/ets/MainAbility/MainAbility.ts new file mode 100644 index 0000000000000000000000000000000000000000..ab702584a26a5c89f994c0c5d757620ff33cc756 --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/ets/MainAbility/MainAbility.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 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 Ability from '@ohos.app.ability.UIAbility' + +export default class MainAbility extends Ability { + onCreate(want,launchParam){ + // Ability is creating, initialize resources for this ability + console.log("[Demo] MainAbility onCreate") + globalThis.abilityWant = want; + } + + onDestroy() { + // Ability is destroying, release resources for this ability + console.log("[Demo] MainAbility onDestroy") + } + + onWindowStageCreate(windowStage) { + // Main window is created, set main page for this ability + console.log("[Demo] MainAbility onWindowStageCreate") + globalThis.abilityContext = this.context + windowStage.setUIContent(this.context, "pages/index/index", null) + } + + onWindowStageDestroy() { + //Main window is destroyed, release UI related resources + console.log("[Demo] MainAbility onWindowStageDestroy") + } + + onForeground() { + // Ability has brought to foreground + console.log("[Demo] MainAbility onForeground") + } + + onBackground() { + // Ability has back to background + console.log("[Demo] MainAbility onBackground") + } +}; \ No newline at end of file diff --git a/test/sceneProject/unittest/system_module/entry/src/main/ets/pages/index/index.ets b/test/sceneProject/unittest/system_module/entry/src/main/ets/pages/index/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a07d7d96fc47571da6e55f34ebf286b9bcdb1e10 --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/ets/pages/index/index.ets @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 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 file from '@system.file'; + +import {Core, ExpectExtend, InstrumentLog, ReportExtend} from "deccjsunit/index.ets" +import GlobalContext from "../MainAbility/GlobalContext.ts" + + +@Entry +@Component +struct Index { + aboutToAppear(){ + console.info("start run testcase!!!!") + const core:Core = Core.getInstance() + const expectExtend:ExpectExtend = new ExpectExtend({ + id: 'extend' + }) + core.addService('expect', expectExtend) + const reportExtend:ReportExtend = new ReportExtend(file) + + core.addService('report', reportExtend) + core.init() + core.subscribeEvent('task', reportExtend) + + const globalContext:GlobalContext = new GlobalContext() + globalContext.globalContext(core) + core.execute() + } + + build() { + Flex({ direction:FlexDirection.Column, alignItems:ItemAlign.Center, justifyContent: FlexAlign.Center }) { + Text('Hello World') + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('next page') + .fontSize(25) + .fontWeight(FontWeight.Bold) + }.type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .onClick(() => { + + }) + } + .width('100%') + .height('100%') + } +} \ No newline at end of file diff --git a/test/sceneProject/unittest/system_module/entry/src/main/ets/test/List.test.ets b/test/sceneProject/unittest/system_module/entry/src/main/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..e1e14f881f2ce231f48f61f3f7fab1b65829140f --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/ets/test/List.test.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 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 function testsuite(context:Context) { +} \ No newline at end of file diff --git a/test/sceneProject/unittest/system_module/entry/src/main/module.json b/test/sceneProject/unittest/system_module/entry/src/main/module.json new file mode 100644 index 0000000000000000000000000000000000000000..390c6a2708a8201524c2fdb21cde908db7185fa4 --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/module.json @@ -0,0 +1,190 @@ +{ + "module": { + "name":"module01", + "type":"entry", + "srcEntry":"./MyAbilityStage.ts", + "description":"$string:description_application", + "descriptionId": 2020, + "process":"bba", + "mainElement":"MainAbility", + "deviceTypes":[ + "2in1", + "tablet", + "default", + "car", + "liteWearable", + "phone", + "smartVision", + "tablet", + "tv", + "wearable", + "router" + ], + "deliveryWithInstall":true, + "installationFree":false, + "virtualMachine":"ark", + "pages":"$profile:page_config", + "metadata":[ + { + "name":"string", + "value":"string", + "resource":"@profile:config_file" + }, + { + "name":"string", + "value":"string", + "resource":"@profile:config_file" + } + ], + "abilities": [ + { + "name": "MainAbility", + "srcEntry": "./login/MyLoginAbility.ts", + "description": "$string:description_main_ability", + "descriptionId": 3030, + "icon": "$media:icon", + "iconId": 4040, + "label": "Login", + "labelId": 16777216, + "permissions":["ext","sql"], + "metadata":[ + { + "name":"a01", + "value":"v01", + "resource":"hello" + }, + { + "name":"a02", + "value":"v02", + "resource":"world" + } + ], + "exported": true, + "skills": [ + { + "actions": [ + "action.system.home" + ], + "entities": [ + "entity.system.home" + ], + "uris":[ + { + "scheme":"content", + "host":"www.test.com", + "port":"8080", + "pathRegex":"query/.*/name", + "path":"hello", + "pathStartWith":"cc/jac" + }, + { + "scheme":"aaaaa", + "host":"bbbbb", + "port":"10", + "pathRegex":"ccc", + "path":"ddd", + "pathStartWith":"eee" + } + ] + } + ], + "backgroundModes":[ + "dataTransfer", + "audioPlayback", + "audioRecording", + "location", + "bluetoothInteraction", + "multDeviceConnection", + "wifiInteraction", + "voip", + "taskKeeping" + ] + } + ], + "extensionAbilities": [ + { + "name": "FormName", + "srcEntry": "./form/MyForm.ts", + "icon": "$media:icon", + "iconId": 16777223, + "label": "$string:extension_name", + "labelId": 6060, + "description": "$string:form_description", + "descriptionId": 7070, + "type": "form", + "readPermission":"readPermission---", + "writePermission":"writePermission---", + "permissions":["aaa","ccc"], + "exported": true, + "skills": [ + { + "actions": ["action001"], + "entities": ["entity001"], + "uris":[ + { + "scheme":"content", + "host":"www.test.com", + "pathRegex":"query/.*/name", + "port":"8080", + "path":"hello", + "pathStartWith":"cc/jac" + }, + { + "scheme":"aaaaa", + "host":"bbbbb", + "port":"10", + "pathRegex":"ccc", + "path":"ddd", + "pathStartWith":"eee" + } + ] + } + ], + "metadata": [ + { + "name": "ohos.extablility.form", + "resource": "$profile:form_config" + } + ] + } + ], + "requestPermissions": [ + { + "name": "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", + "reason": "$string:reason", + "usedScene": { + "abilities": ["FormAbility"], + "when": "inuse" + } + }, + { + "reason": "use ohos.permission.INSTALL_BUNDLE", + "name": "ohos.permission.INSTALL_BUNDLE" + } + ], + "definePermissions":[ + { + "name":"ohos.abilitydemo.permission.PROVIDER", + "grantMode":"system_grant", + "availableLevel":"system_basic", + "provisionEnable":true, + "distributedSceneEnable":true, + "label":"$string:permlab_provider", + "labelId":12, + "description":"$string:permdesc_provider", + "descriptionId":13 + }, + { + "name":"ohos.abilitydemo.permission.PROVIDER002", + "grantMode":"system_grant", + "availableLevel":"system_basic", + "provisionEnable":true, + "distributedSceneEnable":true, + "label":"$string:permlab_provider", + "labelId":112, + "description":"$string:permdesc_provider002", + "descriptionId":113 + } + ] + } +} \ No newline at end of file diff --git a/test/sceneProject/unittest/system_module/entry/src/main/resources/base/element/color.json b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..8ab41dcca1ad079e8d94f49627b9cdcdc650b0d8 --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "white", + "value": "#16777221" + } + ] +} diff --git a/test/sceneProject/unittest/system_module/entry/src/main/resources/base/element/string.json b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d005a7c3d91893013274b2c0897bde887f4d88a3 --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/element/string.json @@ -0,0 +1,40 @@ +{ + "string": [ + { + "name": "description_application", + "value": "description" + }, + { + "name": "extension_name", + "value": "label" + }, + { + "name": "description_main_ability", + "value": "description" + }, + { + "name": "permlab_provider", + "value": "label" + }, + { + "name": "form_description", + "value": "description" + }, + { + "name": "reason", + "value": "reason" + }, + { + "name": "permdesc_provider", + "value": "description" + }, + { + "name": "permdesc_provider002", + "value": "description" + }, + { + "name": "16777219", + "value": "description" + } + ] +} diff --git a/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/aa_icon.png b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/aa_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/aa_icon.png differ diff --git a/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/app_icon.png b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/app_icon.png differ diff --git a/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/icon.png b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/media/icon.png differ diff --git a/test/sceneProject/unittest/system_module/entry/src/main/resources/base/profile/form_config.json b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/profile/form_config.json new file mode 100644 index 0000000000000000000000000000000000000000..3fd6a0513f497250c799ae10559bc550a00c0b6b --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/profile/form_config.json @@ -0,0 +1,24 @@ +{ + "forms": [ + { + "colorMode": "auto", + "defaultDimension": "2*2", + "description": "$string:16777219", + "formConfigAbility": "ability://xxxxx", + "formVisibleNotify": false, + "isDefault": true, + "name": "Form_Js", + "scheduledUpdateTime": "10:30", + "src": "", + "supportDimensions": [ + "2*2" + ], + "updateDuration": 1, + "updateEnabled": true, + "window": { + "autoDesignWidth": true, + "designWidth": 720 + } + } + ] +} \ No newline at end of file diff --git a/test/sceneProject/unittest/system_module/entry/src/main/resources/base/profile/main_pages.json b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..96b478210df9884592229ae2db6f6bb7f86c14f4 --- /dev/null +++ b/test/sceneProject/unittest/system_module/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,6 @@ +{ + "src": [ + "pages/index/index", + "pages/second/second" + ] +} \ No newline at end of file diff --git a/test/unittest/bm/BUILD.gn b/test/unittest/bm/BUILD.gn index 1a691abd03573f3ba3e521dcd3ab429034980e15..3ae56f8d612de82a9ee835380af0ff52476cabd6 100644 --- a/test/unittest/bm/BUILD.gn +++ b/test/unittest/bm/BUILD.gn @@ -204,7 +204,7 @@ ohos_unittest("bm_command_install_test") { ohos_unittest("bm_command_test") { module_out_path = module_output_path - include_dirs = [] + include_dirs = [ "${bundle_framework_path}/services/bundlemgr/include" ] sources = [ "${bundletool_path}/src/bundle_command.cpp", @@ -229,6 +229,8 @@ ohos_unittest("bm_command_test") { deps = [ "${bundle_framework_path}/services/bundlemgr:libbms", + "${bundletool_test_path}/sceneProject/unittest/ohos_test:copy_ohos_test", + "${bundletool_test_path}/sceneProject/unittest/system_module:distributed_system_module", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] diff --git a/test/unittest/bm/bm_command_test.cpp b/test/unittest/bm/bm_command_test.cpp index 3f205bbe45d01e5ca80e1c260f806d2df522cb2d..e1363a228ce098419315870c8082832cd8d136f6 100644 --- a/test/unittest/bm/bm_command_test.cpp +++ b/test/unittest/bm/bm_command_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -14,6 +14,7 @@ */ #include +#include #define private public #include "bundle_command.h" @@ -24,17 +25,88 @@ #include "mock_bundle_installer_host.h" #include "mock_bundle_mgr_host.h" +#include "bundle_installer_proxy.h" +#include "bundle_mgr_proxy.h" +#include "iservice_registry.h" +#include "scope_guard.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" + using namespace testing::ext; using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; namespace OHOS { +namespace { +const std::string BUNDLE_NAME = "com.query.test"; +const std::string MODULE_NAME = "module01"; +const std::string ABILITY_NAME = "MainAbility"; +const std::string SYSTEM_HAP_FILE_PATH = "/data/test/resource/bms/install_bundle/distributed_system_module.hap"; +const int32_t USERID = 100; +const std::string MSG_SUCCESS = "[SUCCESS]"; +const std::string OPERATION_FAILED = "Failure"; +const std::string OPERATION_SUCCESS = "Success"; +} + +class StatusReceiverImpl : public StatusReceiverHost { +public: + StatusReceiverImpl(); + virtual ~StatusReceiverImpl(); + virtual void OnStatusNotify(const int progress); + virtual void OnFinished(const int32_t resultCode, const std::string &resultMsg); + std::string GetResultMsg() const; + +private: + mutable std::promise resultMsgSignal_; + int iProgress_ = 0; + + DISALLOW_COPY_AND_MOVE(StatusReceiverImpl); +}; + +StatusReceiverImpl::StatusReceiverImpl() +{ + APP_LOGI("create status receiver instance"); +} + +StatusReceiverImpl::~StatusReceiverImpl() +{ + APP_LOGI("destroy status receiver instance"); +} + +void StatusReceiverImpl::OnFinished(const int32_t resultCode, const std::string &resultMsg) +{ + APP_LOGD("OnFinished result is %{public}d, %{public}s", resultCode, resultMsg.c_str()); + resultMsgSignal_.set_value(resultMsg); +} +void StatusReceiverImpl::OnStatusNotify(const int progress) +{ + EXPECT_GT(progress, iProgress_); + iProgress_ = progress; + APP_LOGI("OnStatusNotify progress:%{public}d", progress); +} + +std::string StatusReceiverImpl::GetResultMsg() const +{ + auto future = resultMsgSignal_.get_future(); + future.wait(); + std::string resultMsg = future.get(); + if (resultMsg == MSG_SUCCESS) { + return OPERATION_SUCCESS; + } else { + return OPERATION_FAILED + resultMsg; + } +} + class BmCommandTest : public ::testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp() override; void TearDown() override; + static sptr GetBundleMgrProxy(); + static sptr GetInstallerProxy(); + bool InstallBundle(const std::string &bundlePath) const; + bool UninstallBundle(const std::string &bundleName) const; void MakeMockObjects(); void SetMockObjects(BundleManagerShellCommand &cmd) const; @@ -61,6 +133,81 @@ void BmCommandTest::SetUp() void BmCommandTest::TearDown() {} +sptr BmCommandTest::GetBundleMgrProxy() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + APP_LOGE("fail to get system ability mgr."); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + APP_LOGE("fail to get bundle manager proxy."); + return nullptr; + } + + APP_LOGI("get bundle manager proxy success."); + return iface_cast(remoteObject); +} + +sptr BmCommandTest::GetInstallerProxy() +{ + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + APP_LOGE("bundle mgr proxy is nullptr."); + return nullptr; + } + + sptr installerProxy = bundleMgrProxy->GetBundleInstaller(); + if (!installerProxy) { + APP_LOGE("fail to get bundle installer proxy."); + return nullptr; + } + + APP_LOGI("get bundle installer proxy success."); + return installerProxy; +} + +bool BmCommandTest::InstallBundle(const std::string &bundlePath) const +{ + setuid(Constants::FOUNDATION_UID); + ScopeGuard uidGuard([&] { setuid(Constants::ROOT_UID); }); + sptr installerProxy = GetInstallerProxy(); + if (!installerProxy) { + APP_LOGE("get bundle installer failed."); + return false; + } + InstallParam installParam; + installParam.installFlag = InstallFlag::REPLACE_EXISTING; + installParam.userId = USERID; + sptr statusReceiver = (new (std::nothrow) StatusReceiverImpl()); + EXPECT_NE(statusReceiver, nullptr); + bool result = installerProxy->Install(bundlePath, installParam, statusReceiver); + EXPECT_EQ(statusReceiver->GetResultMsg(), OPERATION_SUCCESS); + return result; +} + +bool BmCommandTest::UninstallBundle(const std::string &bundleName) const +{ + setuid(Constants::FOUNDATION_UID); + ScopeGuard uidGuard([&] { setuid(Constants::ROOT_UID); }); + sptr installerProxy = GetInstallerProxy(); + if (!installerProxy) { + APP_LOGE("get bundle installer failed."); + return false; + } + InstallParam installParam; + installParam.installFlag = InstallFlag::NORMAL; + installParam.userId = USERID; + sptr statusReceiver = (new (std::nothrow) StatusReceiverImpl()); + EXPECT_NE(statusReceiver, nullptr); + bool result = installerProxy->Uninstall(bundleName, installParam, statusReceiver); + EXPECT_EQ(statusReceiver->GetResultMsg(), OPERATION_SUCCESS); + return result; +} + void BmCommandTest::MakeMockObjects() { // mock a mgr host @@ -301,11 +448,14 @@ HWTEST_F(BmCommandTest, Bm_Command_Clean_0005, Function | MediumTest | Level1) */ HWTEST_F(BmCommandTest, Bm_Command_Clean_0006, Function | MediumTest | Level1) { + auto res = InstallBundle(SYSTEM_HAP_FILE_PATH); + EXPECT_TRUE(res); + char *argv[] = { const_cast(TOOL_NAME.c_str()), const_cast("clean"), const_cast("-n"), - const_cast(STRING_BUNDLE_NAME.c_str()), + const_cast(TEST_BUNDLE_NAME.c_str()), const_cast("-d"), const_cast(""), }; @@ -314,6 +464,9 @@ HWTEST_F(BmCommandTest, Bm_Command_Clean_0006, Function | MediumTest | Level1) // set the mock objects SetMockObjects(cmd); EXPECT_EQ(cmd.ExecCommand(), STRING_CLEAN_DATA_BUNDLE_OK + "\n"); + + res = UninstallBundle(BUNDLE_NAME); + EXPECT_TRUE(res); } /** @@ -434,11 +587,14 @@ HWTEST_F(BmCommandTest, Bm_Command_Clean_0011, Function | MediumTest | Level1) */ HWTEST_F(BmCommandTest, Bm_Command_Clean_0012, Function | MediumTest | Level1) { + auto res = InstallBundle(SYSTEM_HAP_FILE_PATH); + EXPECT_TRUE(res); + char *argv[] = { const_cast(TOOL_NAME.c_str()), const_cast("clean"), const_cast("-n"), - const_cast(STRING_BUNDLE_NAME.c_str()), + const_cast(TEST_BUNDLE_NAME.c_str()), const_cast("-d"), const_cast(" "), const_cast("-u"), @@ -450,6 +606,9 @@ HWTEST_F(BmCommandTest, Bm_Command_Clean_0012, Function | MediumTest | Level1) // set the mock objects SetMockObjects(cmd); EXPECT_EQ(cmd.ExecCommand(), STRING_CLEAN_DATA_BUNDLE_OK + "\n"); + + res = UninstallBundle(BUNDLE_NAME); + EXPECT_TRUE(res); } /**