diff --git a/communication/bluetooth_ble/BUILD.gn b/communication/bluetooth_ble/BUILD.gn index e8c142a4d2b90d823533b673906c7187845a6ed9..76822f7d21a8467cd6c5b7bf5ca2b495d114d277 100644 --- a/communication/bluetooth_ble/BUILD.gn +++ b/communication/bluetooth_ble/BUILD.gn @@ -9,7 +9,7 @@ # 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. +# limitations under the License. import("//test/xts/tools/build/suite.gni") ohos_js_hap_suite("ActsBluetoothBleJsTest") { @@ -23,7 +23,9 @@ ohos_js_hap_suite("ActsBluetoothBleJsTest") { hap_name = "ActsBluetoothBleHapTest" } ohos_js_assets("bluetooth_js_assets") { - source_dir = "./src/main/js/default" + js2abc = true + hap_profile = "./src/main/config.json" + source_dir = "./src/main/js" } ohos_resources("bluetooth_resources") { sources = [ "./src/main/resources" ] diff --git a/communication/bluetooth_ble/Test.json b/communication/bluetooth_ble/Test.json index d3f0292e79097cab7d5b25518908cc0ccb749ec7..0ab8b66bc991faf64a936381f06aad8b10364936 100644 --- a/communication/bluetooth_ble/Test.json +++ b/communication/bluetooth_ble/Test.json @@ -1,18 +1,20 @@ -{ - "description": "Configuration for bluetooth js api Tests", - "driver": { - "type": "JSUnitTest", - "test-timeout": "600000", - "package": "ohos.acts.communication.bluetooth.bluetoothhost", - "shell-timeout": "60000" - }, - "kits": [ - { - "test-file-name": [ - "ActsBluetoothBleHapTest.hap" - ], - "type": "AppInstallKit", - "cleanup-apps": true - } - ] -} +{ + "description": "Configuration for bluetooth js api Tests", + "driver": { + "type": "OHJSUnitTest", + "test-timeout": "600000", + "shell-timeout": "600000", + "bundle-name": "ohos.acts.communication.bluetooth.bluetoothhost", + "package-name": "ohos.acts.communication.bluetooth.bluetoothhost", + "testcase-timeout": 70000 + }, + "kits": [ + { + "test-file-name": [ + "ActsBluetoothBleHapTest.hap" + ], + "type": "AppInstallKit", + "cleanup-apps": true + } + ] +} \ No newline at end of file diff --git a/communication/bluetooth_ble/src/main/config.json b/communication/bluetooth_ble/src/main/config.json index b56e3760ba9b0eb7eebf5af55ad6b00e856a4e7c..9175146c5f6ad96816a2fd65537b4d78a755b4e7 100644 --- a/communication/bluetooth_ble/src/main/config.json +++ b/communication/bluetooth_ble/src/main/config.json @@ -1,104 +1,135 @@ { - "app": { - "bundleName": "ohos.acts.communication.bluetooth.bluetoothhost", - "vendor": "acts", - "version": { - "code": 1000000, - "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5 - } - }, - "deviceConfig": {}, - "module": { - "package": "ohos.acts.communication.bluetooth.bluetoothhost", - "name": ".MyApplication", - "deviceType": [ - "phone" - ], - "distro": { - "deliveryWithInstall": true, - "moduleName": "entry", - "moduleType": "entry" + "app": { + "bundleName": "ohos.acts.communication.bluetooth.bluetoothhost", + "vendor": "acts", + "version": { + "code": 1000000, + "name": "1.0.0" + }, + "apiVersion": { + "compatible": 4, + "target": 5 + } }, - "abilities": [ - { - "visible": true, - "skills": [ - { - "entities": [ - "entity.system.home" - ], - "actions": [ - "action.system.home" - ] - } + "deviceConfig": {}, + "module": { + "package": "ohos.acts.communication.bluetooth.bluetoothhost", + "name": ".entry", + "deviceType": [ + "phone" ], - "name": "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility", - "icon": "$media:icon", - "description": "$string:mainability_description", - "label": "$string:app_name", - "type": "page", - "launchType": "standard" - } - ], - "js": [ - { - "pages": [ - "pages/index/index" + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "formsEnabled": false, + "name": ".MainAbility", + "srcLanguage": "js", + "srcPath": "MainAbility", + "icon": "$media:icon", + "description": "$string:MainAbility_desc", + "label": "$string:MainAbility_label", + "type": "page", + "visible": true, + "launchType": "standard" + }, + { + "orientation": "unspecified", + "formsEnabled": false, + "name": ".TestAbility", + "srcLanguage": "js", + "srcPath": "TestAbility", + "icon": "$media:icon", + "description": "$string:TestAbility_desc", + "label": "$string:TestAbility_label", + "type": "page", + "visible": true, + "launchType": "standard" + } ], - "name": "default", - "window": { - "designWidth": 720, - "autoDesignWidth": false - } - } - ], - "reqPermissions": [ - { - "name": "ohos.permission.USE_BLUETOOTH", - "reason": "", - "usedScene": { - "ability": [ - "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" - ], - "when": "inuse" - } - }, - { - "name": "ohos.permission.MANAGE_BLUETOOTH", - "reason": "", - "usedScene": { - "ability": [ - "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" - ], - "when": "inuse" - } - }, - { - "name": "ohos.permission.DISCOVER_BLUETOOTH", - "reason": "", - "usedScene": { - "ability": [ - "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" - ], - "when": "inuse" - } - }, - { - "name": "ohos.permission.LOCATION", - "reason": "", - "usedScene": { - "ability": [ - "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" - ], - "when": "inuse" - } - } - ] - - } -} - + "js": [ + { + "pages": [ + "pages/index/index" + ], + "name": "default", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + }, + { + "pages": [ + "pages/index/index" + ], + "name": ".TestAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.USE_BLUETOOTH", + "reason": "", + "usedScene": { + "ability": [ + "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.MANAGE_BLUETOOTH", + "reason": "", + "usedScene": { + "ability": [ + "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.DISCOVER_BLUETOOTH", + "reason": "", + "usedScene": { + "ability": [ + "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.LOCATION", + "reason": "", + "usedScene": { + "ability": [ + "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" + ], + "when": "inuse" + } + } + ], + "testRunner": { + "name": "OpenHarmonyTestRunner", + "srcPath": "TestRunner" + }, + "mainAbility": ".MainAbility", + "srcPath": "" + } +} \ No newline at end of file diff --git a/communication/bluetooth_ble/src/main/js/default/app.js b/communication/bluetooth_ble/src/main/js/MainAbility/app.js similarity index 100% rename from communication/bluetooth_ble/src/main/js/default/app.js rename to communication/bluetooth_ble/src/main/js/MainAbility/app.js diff --git a/communication/bluetooth_on/src/main/js/default/i18n/en-US.json b/communication/bluetooth_ble/src/main/js/MainAbility/i18n/en-US.json similarity index 91% rename from communication/bluetooth_on/src/main/js/default/i18n/en-US.json rename to communication/bluetooth_ble/src/main/js/MainAbility/i18n/en-US.json index e63c70d978a3a53be988388c87182f81785e170c..a4c13dcbdc39c537073f638393d7726ac9a5cdc4 100644 --- a/communication/bluetooth_on/src/main/js/default/i18n/en-US.json +++ b/communication/bluetooth_ble/src/main/js/MainAbility/i18n/en-US.json @@ -1,6 +1,6 @@ -{ - "strings": { - "hello": "Hello", - "world": "World" - } +{ + "strings": { + "hello": "Hello", + "world": "World" + } } \ No newline at end of file diff --git a/communication/bluetooth_on/src/main/js/default/i18n/zh-CN.json b/communication/bluetooth_ble/src/main/js/MainAbility/i18n/zh-CN.json similarity index 91% rename from communication/bluetooth_on/src/main/js/default/i18n/zh-CN.json rename to communication/bluetooth_ble/src/main/js/MainAbility/i18n/zh-CN.json index de6ee5748322f44942c1b003319d8e66c837675f..b1c02368f72f929e4375a43170444de95dcc5984 100644 --- a/communication/bluetooth_on/src/main/js/default/i18n/zh-CN.json +++ b/communication/bluetooth_ble/src/main/js/MainAbility/i18n/zh-CN.json @@ -1,6 +1,6 @@ -{ - "strings": { - "hello": "您好", - "world": "世界" - } +{ + "strings": { + "hello": "您好", + "world": "世界" + } } \ No newline at end of file diff --git a/communication/bluetooth_on/src/main/js/default/pages/index/index.css b/communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.css similarity index 96% rename from communication/bluetooth_on/src/main/js/default/pages/index/index.css rename to communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.css index 72baaffcaa675bfe9f3d3d1974690ae7d05c1a4b..e84b0d6300d7a299aed5376be2cba12327f9d9d2 100644 --- a/communication/bluetooth_on/src/main/js/default/pages/index/index.css +++ b/communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.css @@ -1,24 +1,24 @@ -/* - * Copyright (C) 2022 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. - */ - -.container { - flex-direction: column; - justify-content: center; - align-items: center; -} - -.title { - font-size: 100px; -} +/* + * Copyright (C) 2022 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. + */ + +.container { + flex-direction: column; + justify-content: center; + align-items: center; +} + +.title { + font-size: 100px; +} diff --git a/communication/bluetooth_on/src/main/js/default/pages/index/index.hml b/communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.hml similarity index 97% rename from communication/bluetooth_on/src/main/js/default/pages/index/index.hml rename to communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.hml index 546cd8296b727c1639d551d2301a0bca055c1457..43848d302f5c5cd40b7aad5e19f6c7ea15373d91 100644 --- a/communication/bluetooth_on/src/main/js/default/pages/index/index.hml +++ b/communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.hml @@ -1,20 +1,20 @@ -/* - * Copyright (C) 2022 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. - */ - -
- - {{ $t('strings.hello') }} {{ title }} - -
+/* + * Copyright (C) 2022 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. + */ + +
+ + {{ $t('strings.hello') }} {{ title }} + +
diff --git a/communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.js b/communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.js new file mode 100644 index 0000000000000000000000000000000000000000..014e63a263c208b0afc5eb3809a206d5b879f038 --- /dev/null +++ b/communication/bluetooth_ble/src/main/js/MainAbility/pages/index/index.js @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2022 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 { + data: { + title: "" + }, + onInit() { + this.title = this.$t('strings.world'); + }, + onShow() { + console.info('onShow finish') + }, + onReady() { + console.info('onReady finish') + }, +} + diff --git a/communication/bluetooth_ble/src/main/js/TestAbility/app.js b/communication/bluetooth_ble/src/main/js/TestAbility/app.js new file mode 100644 index 0000000000000000000000000000000000000000..cdc31f3dcf031e2f6a7665d9653e53bb649e21c5 --- /dev/null +++ b/communication/bluetooth_ble/src/main/js/TestAbility/app.js @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' +import { Hypium } from '@ohos/hypium' +import testsuite from '../test/List.test' + +export default { + onCreate() { + console.info('TestApplication onCreate') + var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + console.info('start run testcase!!!') + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) + }, + onDestroy() { + console.info("TestApplication onDestroy"); + } +}; diff --git a/communication/bluetooth_ble/src/main/js/TestAbility/i18n/en-US.json b/communication/bluetooth_ble/src/main/js/TestAbility/i18n/en-US.json new file mode 100644 index 0000000000000000000000000000000000000000..3cb24b374b1d919ca8eac0638f361692b603a900 --- /dev/null +++ b/communication/bluetooth_ble/src/main/js/TestAbility/i18n/en-US.json @@ -0,0 +1,8 @@ +{ + "strings": { + "hello": "Hello", + "world": "World" + }, + "Files": { + } +} \ No newline at end of file diff --git a/communication/bluetooth_ble/src/main/js/TestAbility/i18n/zh-CN.json b/communication/bluetooth_ble/src/main/js/TestAbility/i18n/zh-CN.json new file mode 100644 index 0000000000000000000000000000000000000000..c804e32c0c3103929baca5617cdac70be11fdba1 --- /dev/null +++ b/communication/bluetooth_ble/src/main/js/TestAbility/i18n/zh-CN.json @@ -0,0 +1,8 @@ +{ + "strings": { + "hello": "您好", + "world": "世界" + }, + "Files": { + } +} \ No newline at end of file diff --git a/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.css b/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.css new file mode 100644 index 0000000000000000000000000000000000000000..b1bcd43387ba131cc1d30975ff7508a6f8084a4b --- /dev/null +++ b/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.css @@ -0,0 +1,30 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 60px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} + +@media screen and (device-type: phone) and (orientation: landscape) { + .title { + font-size: 60px; + } +} + +@media screen and (device-type: tablet) and (orientation: landscape) { + .title { + font-size: 100px; + } +} \ No newline at end of file diff --git a/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.hml b/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.hml new file mode 100644 index 0000000000000000000000000000000000000000..f629c71a9be857db6cdf94149652a191b9b272ea --- /dev/null +++ b/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.hml @@ -0,0 +1,5 @@ +
+ + {{ $t('strings.hello') }} {{ title }} + +
diff --git a/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.js b/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.js new file mode 100644 index 0000000000000000000000000000000000000000..88b083a7f6b979019d6a2c5ad20b19c5fd43286b --- /dev/null +++ b/communication/bluetooth_ble/src/main/js/TestAbility/pages/index/index.js @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 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 { + data: { + title: "" + }, + onInit() { + this.title = this.$t('strings.world'); + } +} + + + diff --git a/communication/bluetooth_ble/src/main/js/TestRunner/OpenHarmonyTestRunner.js b/communication/bluetooth_ble/src/main/js/TestRunner/OpenHarmonyTestRunner.js new file mode 100644 index 0000000000000000000000000000000000000000..c5fa8620ca77d381f20b65a903b833e6e3378c97 --- /dev/null +++ b/communication/bluetooth_ble/src/main/js/TestRunner/OpenHarmonyTestRunner.js @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' + +function translateParamsToString(parameters) { + const keySet = new Set([ + '-s class', '-s notClass', '-s suite', '-s itName', + '-s level', '-s testType', '-s size', '-s timeout', + '-s package', '-s dryRun' + ]) + let targetParams = ''; + for (const key in parameters) { + if (keySet.has(key)) { + targetParams += ' ' + key + ' ' + parameters[key] + } + } + return targetParams.trim() +} + + export default { + onPrepare() { + console.info('OpenHarmonyTestRunner OnPrepare') + }, + onRun() { + console.log('OpenHarmonyTestRunner onRun run') + var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + + var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility' + + var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName + cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters) + var debug = abilityDelegatorArguments.parameters["-D"] + console.info('debug value : '+debug) + if (debug == 'true') + { + cmd += ' -D' + } + console.info('cmd : '+cmd) + abilityDelegator.executeShellCommand(cmd, (err, data) => { + console.info('executeShellCommand : err : ' + JSON.stringify(err)); + console.info('executeShellCommand : data : ' + data.stdResult); + console.info('executeShellCommand : data : ' + data.exitCode); + }) + } +}; diff --git a/communication/bluetooth_ble/src/main/js/default/pages/index/index.js b/communication/bluetooth_ble/src/main/js/default/pages/index/index.js deleted file mode 100644 index 8b0cf348f983cc45123e7bbdc1d59cd3f182e399..0000000000000000000000000000000000000000 --- a/communication/bluetooth_ble/src/main/js/default/pages/index/index.js +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2022 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, ExpectExtend} from 'deccjsunit/index' - -export default { - data: { - title: "", - myTimeout:70000 - }, - onInit() { - this.title = this.$t('strings.world'); - }, - onShow() { - console.info('onShow finish') - const core = Core.getInstance() - const expectExtend = new ExpectExtend({ - 'id': 'extend' - }) - core.addService('expect', expectExtend) - core.init() - - const configService = core.getDefaultService('config') - this.timeout = this.myTimeout - configService.setConfig(this) - - require('../../test/List.test') - core.execute() - - }, - onReady() { - console.info('onReady finish') - }, -} - diff --git a/communication/bluetooth_ble/src/main/js/default/test/BluetoothBle.test.js b/communication/bluetooth_ble/src/main/js/test/BluetoothBle.test.js similarity index 97% rename from communication/bluetooth_ble/src/main/js/default/test/BluetoothBle.test.js rename to communication/bluetooth_ble/src/main/js/test/BluetoothBle.test.js index 265ea631299971ca852ae1d593991bc53e05a7ad..23b55ca45e6724fc454b48d94c37d4dfe7beeb33 100644 --- a/communication/bluetooth_ble/src/main/js/default/test/BluetoothBle.test.js +++ b/communication/bluetooth_ble/src/main/js/test/BluetoothBle.test.js @@ -1,842 +1,844 @@ -/* - * Copyright (C) 2022 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 bluetooth from '@ohos.bluetooth'; -import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' - - -let SppType = { - SPP_RFCOMM : 0 -} - -let MatchMode = { - MATCH_MODE_AGGRESSIVE : 1, - MATCH_MODE_STICKY : 2 -} - -let MajorClass = { - MAJOR_MISC : 0x0000, - MAJOR_COMPUTER : 0x0100, - MAJOR_PHONE : 0x0200, - MAJOR_NETWORKING : 0x0300, - MAJOR_AUDIO_VIDEO : 0x0400, - MAJOR_PERIPHERAL : 0x0500, - MAJOR_IMAGING : 0x0600, - MAJOR_WEARABLE : 0x0700, - MAJOR_TOY : 0x0800, - MAJOR_HEALTH : 0x0900, - MAJOR_UNCATEGORIZED : 0x1F00 -} - - -describe('bluetoothhostTest', function() { - - let gattServer = null; - let gattClient = null; - beforeAll(function () { - console.info('beforeAll called') - gattServer = bluetooth.BLE.createGattServer(); - gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - }) - beforeEach(function () { - console.info('beforeEach called') - - }) - afterEach(function () { - console.info('afterEach called') - }) - afterAll(function () { - console.info('afterAll called') - }) - - function sleep(delay) { - return new Promise(resovle => setTimeout(resovle, delay)) - } - - async function tryToEnableBt() { - let sta = bluetooth.getState(); - switch(sta){ - case 0: - console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta)); - bluetooth.enableBluetooth(); - await sleep(3000); - break; - case 1: - console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta)); - await sleep(3000); - break; - case 2: - console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta)); - break; - case 3: - console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta)); - bluetooth.enableBluetooth(); - await sleep(3000); - break; - default: - console.info('[bluetooth_js] enable success'); - } - } - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_ENABLE_0001 - * @tc.name testEnableBluetooth - * @tc.desc Test EnableBluetooth api by promise. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_ENABLE_0001', 0, async function (done) { - console.info('[bluetooth_js] enable start'); - await tryToEnableBt(); - let state = bluetooth.getState(); - console.info('[bluetooth_js] getState On = '+ JSON.stringify(state)); - expect(state).assertEqual(2); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetoothble_GET_CONNECTED_BLEDEVICES_0001 - * @tc.name testGetConnectedBLEDevices - * @tc.desc Test getConnectedBLEDevices api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetoothble_GET_CONNECTED_BLEDEVICES_0001', 0, async function (done) { - console.info('[bluetooth_js] getConnectedBLEDevices test start ...'); - await tryToEnableBt(); - let result = bluetooth.BLE.getConnectedBLEDevices(); - console.info("[bluetooth_js] getConnDev:" + JSON.stringify(result)+ "length:" +result.length); - expect(result.length).assertEqual(0); - done(); - }) - - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_ADVERTISING_0001 - * @tc.name testStartAdvertising - * @tc.desc Test StartAdvertising api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_ADVERTISING_0001', 0, async function (done) { - console.info('[bluetooth_js] BLE advertising start'); - await tryToEnableBt(); - let manufactureValueBuffer = new Uint8Array(4); - manufactureValueBuffer[0] = 1; - manufactureValueBuffer[1] = 2; - manufactureValueBuffer[2] = 3; - manufactureValueBuffer[3] = 4; - let serviceValueBuffer = new Uint8Array(4); - serviceValueBuffer[0] = 4; - serviceValueBuffer[1] = 6; - serviceValueBuffer[2] = 7; - serviceValueBuffer[3] = 8; - let promise = new Promise((resolve) => { - let gattServer = bluetooth.BLE.createGattServer() - let start = gattServer.startAdvertising({ - interval:150, - txPower:60, - connectable:true, - },{ - serviceUuids:["12"], - manufactureData:[{ - manufactureId:4567, - manufactureValue:manufactureValueBuffer.buffer - }], - serviceData:[{ - serviceUuid:"1234", - serviceValue:serviceValueBuffer.buffer - }], - },{ - serviceUuids:["12"], - manufactureData:[{ - manufactureId:1789, - manufactureValue:manufactureValueBuffer.buffer - }], - serviceData:[{ - serviceUuid:"1794", - serviceValue:serviceValueBuffer.buffer - }], - }); - console.info('[bluetooth_js] startAdv res: ' + JSON.stringify(start)); - expect(start).assertNull(); - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_STOP_ADVERTISING_0001 - * @tc.name testStopAdvertising - * @tc.desc Test StopAdvertising api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_STOP_ADVERTISING_0001', 0, async function (done) { - console.info('[bluetooth_js] BLE stop advertising start'); - await tryToEnableBt(); - let gattServer = bluetooth.BLE.createGattServer(); - let stop = gattServer.stopAdvertising(); - expect(true).assertTrue(); - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_CONNRCT_0001 - * @tc.name testConnect - * @tc.desc Test Connect api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_CONNRCT_0001', 0, async function (done) { - console.info('[bluetooth_js] gatt connect start'); - await tryToEnableBt(); - gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - let ret = gattClient.connect(); - expect(ret).assertTrue(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GET_RSSI_VALUE_CALLBACK - * @tc.name testGetRssiValue - * @tc.desc Test GetRssiValue api by callback. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GET_RSSI_VALUE_CALLBACK', 0, async function (done) { - console.info('[bluetooth_js] BLE get rssi1 start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.getRssiValue((err, data)=> { - console.info('[bluetooth_js] rssi err:' + JSON.stringify(err)+'value:'+JSON.stringify(data)); - expect(data).assertNull(); - }); - resolve() - }) - await promise.then(done) - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GET_RSSI_VALUE_PROMISE_0001 - * @tc.name testGetRssiValue - * @tc.desc Test GetRssiValue api by promise. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GET_RSSI_VALUE_PROMISE_0001', 0, async function (done) { - console.info('[bluetooth_js] BLE get rssi start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.getRssiValue().then((data) => { - if (data != null) { - expect(true).assertEqual(true); - } else { - console.info('[bluetooth_js] BLE read rssi ' + JSON.stringify(data)); - let rssiLength = Object.keys(data).length; - expect(rssiLength).assertEqual(0); - } - }).catch(err => { - console.error(`bluetooth getRssiValue has error: ${err}`); - expect(true).assertEqual(true); - }); - resolve() - }) - await promise.then(done) - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GET_DEVICE_NAME_PROMISE_0001 - * @tc.name testGetDeviceName - * @tc.desc Test GetDeviceName api by promise. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GET_DEVICE_NAME_PROMISE_0001', 0, async function (done) { - console.info('[bluetooth_js] BLE get device name promise start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.getDeviceName().then((data) => { - console.info('[bluetooth_js] device name' + JSON.stringify(data)) - expect(data).assertNull(); - done(); - }) - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GET_DEVICE_NAME_CALLBACK_0001 - * @tc.name testGetDeviceName - * @tc.desc Test GetDeviceName api by callback. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GET_DEVICE_NAME_CALLBACK_0001', 0, async function (done) { - console.info('[bluetooth_js] BLE get device name callback start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.getDeviceName((err, data)=> { - console.info('[bluetooth_js] device name' + JSON.stringify(data)) - expect(data).assertNull(); - done(); - }) - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_GETSERVICES_CALLBACK_0001 - * @tc.name testGetServices - * @tc.desc Test GetServices api by callback. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_GETSERVICES_CALLBACK_0001', 0, async function (done) { - console.info('[bluetooth_js] getServices callback start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.getServices((err, data)=> { - console.info('[bluetooth_js] device name err1 ' + JSON.stringify(err) +'name1' + JSON.stringify(data)); - expect(true).assertEqual(err.code == -1); - done(); - }) - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_GETSERVICES_PROMISE_0001 - * @tc.name testGetServices - * @tc.desc Test GetServices api by promise. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_GETSERVICES_PROMISE_0001', 0, async function (done) { - console.info('[bluetooth_js] getServices promise start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.getServices().then((object) => { - if (object != null) { - expect(true).assertEqual(true); - } else { - console.info('[bluetooth_js] getServices successfully:'+JSON.stringify(object)); - expect(null).assertFail(); - } - done(); - }).catch(err => { - console.error(`bluetooth getServices has error: ${err}`); - expect(true).assertEqual(true); - done(); - }); - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_READ_CHARA_VALUE_0001 - * @tc.name testReadCharacteristicValue - * @tc.desc Test ReadCharacteristicValue api by promise. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_READ_CHARA_VALUE_0001', 0, async function (done) { - console.info('[bluetooth_js] readCharacteristicValue promise start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let descriptors = []; - let arrayBuffer = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBuffer); - desValue[0] = 11; - let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; - descriptors[0] = descriptor; - let arrayBufferCCC = new ArrayBuffer(8); - let cccValue = new Uint8Array(arrayBufferCCC); - cccValue[0] = 32; - let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', - characteristicValue: arrayBufferCCC, descriptors:descriptors}; - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.readCharacteristicValue(characteristic).then((object) => { - if (object != null) { - expect(true).assertEqual(true); - } else { - console.info('[bluetooth_js] readCharacValue promise data:' + JSON.stringify(data)); - expect(null).assertFail(); - } - done(); - }).catch(err => { - console.error(`bluetooth readCharacteristicValue promise has error: ${err}`); - expect(true).assertEqual(true); - done(); - }); - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_READ_CHARA_VALUE_0002 - * @tc.name testReadCharacteristicValue - * @tc.desc Test ReadCharacteristicValue api by callback. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_READ_CHARA_VALUE_0002', 0, async function (done) { - console.info('[bluetooth_js] readCharacteristicValue callback start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let descriptors = []; - let arrayBuffer = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBuffer); - desValue[0] = 11; - let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; - descriptors[0] = descriptor; - let arrayBufferCCC = new ArrayBuffer(8); - let cccValue = new Uint8Array(arrayBufferCCC); - cccValue[0] = 32; - let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', - characteristicValue: arrayBufferCCC, descriptors:descriptors}; - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.readCharacteristicValue(characteristic,(err, data)=> { - if (err) { - console.error(`bluetooth readCharacteristicValue callback has error: ${err}`); - expect(true).assertEqual(true); - done(); - return; - } - if (object != null) { - expect(true).assertEqual(true); - } else { - console.info('[bluetooth_js] readCharacValue callback data:' + JSON.stringify(data)); - expect(null).assertFail(); - } - done(); - }) - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_READ_DESCRI_VALUE_0001 - * @tc.name testReadDescriptorValue - * @tc.desc Test ReadDescriptorValue api by promise. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_READ_DESCRI_VALUE_0001', 0, async function (done) { - console.info('[bluetooth_js] readDescriptorValue promise start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let arrayBuffer = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBuffer); - desValue[0] = 11; - let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.readDescriptorValue(descriptor).then((object) => { - if (object != null) { - expect(true).assertEqual(true); - } else { - console.info('[bluetooth_js] BLEDescriptor promise data:' + JSON.stringify(object)); - expect(null).assertFail(); - } - done(); - }).catch(err => { - console.error(`bluetooth readDescriptorValue promise has error: ${err}`); - expect(true).assertEqual(true); - done(); - }); - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_READ_DESCRI_VALUE_0002 - * @tc.name testReadDescriptorValue - * @tc.desc Test ReadDescriptorValue api by callback. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_READ_DESCRI_VALUE_0002', 0, async function (done) { - console.info('[bluetooth_js] readDescriptorValue callback start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let arrayBuffer = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBuffer); - desValue[0] = 11; - let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - gattClient.readDescriptorValue(descriptor,(err, data)=> { - if (err) { - console.error(`bluetooth readDescriptorValue callback has error: ${err}`); - expect(true).assertEqual(true); - done(); - return; - } - if (object != null) { - expect(true).assertEqual(true); - } else { - console.info('[bluetooth_js] BLEDescriptor callback data:' + JSON.stringify(object)); - expect(null).assertFail(); - } - done(); - }) - resolve() - }) - await promise.then(done) - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_WRITE_CHARACT_VALUE_0001 - * @tc.name testWriteCharacteristicValue - * @tc.desc Test WriteCharacteristicValue api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_WRITE_CHARACT_VALUE_0001', 0, async function (done) { - console.info('[bluetooth_js] writeCharacteristicValue start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let descriptors = []; - let arrayBuffer = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBuffer); - desValue[0] = 11; - let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; - descriptors[0] = descriptor; - let arrayBufferCCC = new ArrayBuffer(8); - let cccValue = new Uint8Array(arrayBufferCCC); - cccValue[0] = 32; - let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', - characteristicValue: arrayBufferCCC, descriptors:descriptors}; - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - let ret = gattClient.writeCharacteristicValue(characteristic); - console.info('[bluetooth_js] writeCharacteristicValue ret : ' + ret); - expect(ret).assertEqual(false); - resolve() - }) - await promise.then(done) - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_WRITE_DESC_VALUE_0001 - * @tc.name testWriteDescriptorValue - * @tc.desc Test WriteDescriptorValue api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_WRITE_DESC_VALUE_0001', 0, async function (done) { - console.info('[bluetooth_js] writeDescriptorValue start'); - await tryToEnableBt(); - let promise = new Promise((resolve) => { - let arrayBuffer = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBuffer); - desValue[0] = 11; - let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - let ret = gattClient.writeDescriptorValue(descriptor); - console.info('[bluetooth_js] bluetooth writeDescriptorValue ret : ' + ret); - expect(ret).assertEqual(false); - resolve() - }) - await promise.then(done) - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_SET_BLE_MTUSIZE_0001 - * @tc.name testSetBLEMtuSize - * @tc.desc Test SetBLEMtuSize api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_SET_BLE_MTUSIZE_0001', 0, async function (done) { - console.info('[bluetooth_js] setBLEMtuSize start'); - await tryToEnableBt(); - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - let ret = gattClient.setBLEMtuSize(128); - console.info('[bluetooth_js] bluetooth setBLEMtuSize ret:' + ret); - expect(ret).assertEqual(false); - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_SET_NOTIFY_CHARA_CHANGED_0001 - * @tc.name testSetNotifyCharacteristicChanged - * @tc.desc Test SetNotifyCharacteristicChanged api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_SET_NOTIFY_CHARA_CHANGED_0001', 0, async function (done) { - console.info('[bluetooth_js] setNotifyCharacteristicChanged start'); - await tryToEnableBt(); - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - let descriptors = []; - let arrayBuffer = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBuffer); - desValue[0] = 11; - let arrayBufferNotify = new ArrayBuffer(8); - let descNotifyValue = new Uint8Array(arrayBufferNotify); - descNotifyValue[0] = 1 - let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', - descriptorValue: arrayBuffer}; - let descriptorNotify = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', - descriptorValue: arrayBufferNotify}; - descriptors[0] = descriptor; - descriptors[1] = descriptorNotify; - let arrayBufferCCC = new ArrayBuffer(8); - let cccValue = new Uint8Array(arrayBufferCCC); - cccValue[0] = 1; - let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', - characteristicValue: arrayBufferCCC, descriptors:descriptors}; - let ret = gattClient.setNotifyCharacteristicChanged(characteristic, false); - console.info('[bluetooth_js] setNotifyCharacteristicChanged ret:' + ret); - expect(ret).assertEqual(false); - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_ADD_SERVICE_0001 - * @tc.name testAddService - * @tc.desc Test AddService api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_ADD_SERVICE_0001', 0, async function (done) { - console.info('[bluetooth_js] addService start'); - await tryToEnableBt(); - let gattServer = bluetooth.BLE.createGattServer(); - let descriptors = []; - let arrayBuffer = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBuffer); - desValue[0] = 11; - let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', - descriptorValue: arrayBuffer}; - descriptors[0] = descriptor; - let characteristics = []; - let arrayBufferCCC = new ArrayBuffer(8); - let cccValue = new Uint8Array(arrayBufferCCC); - cccValue[0] = 1; - let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', - characteristicValue: arrayBufferCCC, descriptors:descriptors}; - characteristics[0] = characteristic; - let service = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - isPrimary: true, characteristics: characteristics, includeServices: []}; - let ret = gattServer.addService(service); - console.info('[bluetooth_js] bluetooth addService ret : ' + ret); - expect(ret).assertTrue(); - await sleep(3500); - let ret1=gattServer.removeService('00001810-0000-1000-8000-00805F9B34FB'); - console.info('[bluetooth_js]removeService ret:'+ret1); - expect(ret1).assertTrue(); - done(); - }) - - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_NOTIFY_CHARA_CHANGED_0001 - * @tc.name testNotifyCharacteristicChanged - * @tc.desc Test NotifyCharacteristicChanged api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_NOTIFY_CHARA_CHANGED_0001', 0, async function (done) { - console.info('[bluetooth_js] notifyCharacteristicChanged start'); - await tryToEnableBt(); - let gattServer = bluetooth.BLE.createGattServer() - let descriptors = []; - let arrayBufferDesc = new ArrayBuffer(8); - let desValue = new Uint8Array(arrayBufferDesc); - desValue[0] = 11; - let arrayBufferNotifyDesc = new ArrayBuffer(8); - let descNotifyValue = new Uint8Array(arrayBufferNotifyDesc); - descNotifyValue[0] = 1 - let - descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: desValue}; - let descriptorNotify = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', - descriptorValue: descNotifyValue}; - descriptors[0] = descriptor; - descriptors[1] = descriptorNotify; - let arrayBufferCCC = new ArrayBuffer(8); - let cccValue = new Uint8Array(arrayBufferCCC); - cccValue[0] = 1; - let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', - characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', - characteristicValue: cccValue, - descriptors:descriptors,confirm:'false'}; - let ret = gattServer.notifyCharacteristicChanged('00:00:00:00:00:00', characteristic); - console.info('[bluetooth_js] notifyCharacteristicChanged ret : ' + ret); - expect(ret).assertEqual(false); - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_SEND_RESPONSE_0001 - * @tc.name testSendResponse - * @tc.desc Test SendResponse api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_SEND_RESPONSE_0001', 0, async function (done) { - console.info('[bluetooth_js] sendResponse start'); - await tryToEnableBt(); - let gattServer = bluetooth.BLE.createGattServer() - let arrayBuffer = new ArrayBuffer(8); - let value = new Uint8Array(arrayBuffer); - value[0] = 1; - let serverResponse = {deviceId: '00:00:00:00:00', transId: 1, - status: 0, offset: 0, value: arrayBuffer}; - let ret = gattServer.sendResponse(serverResponse); - console.info('[bluetooth_js] sendResponse ret : ' + ret); - - expect(ret).assertEqual(false); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetoothble_GATTSERVER_CLOSE_0001 - * @tc.name testBlePeripheralManagerClose - * @tc.desc Test BlePeripheralManagerClose api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetoothble_GATTSERVER_CLOSE_0001', 0, async function (done) { - console.info('[bluetooth_js] GattserverClose test start ...'); - await tryToEnableBt(); - try{ - let gattServer = bluetooth.BLE.createGattServer() - let result = gattServer.close(); - console.info("[bluetooth_js] GattserverClose res:"+ JSON.stringify(result)); - expect(result).assertTrue(); - }catch(error){ - console.info("[bluetooth_js] GattserverClose err:" + JSON.stringify(error)); - expect(null).assertFail(); - } - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_DISCONNRCT_0001 - * @tc.name testDisConnect - * @tc.desc Test DisConnect api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_DISCONNRCT_0001', 0, async function (done) { - console.info('[bluetooth_js] gatt disconnect start'); - await tryToEnableBt(); - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - let ret = gattClient.disconnect(); - console.info('[bluetooth_js] gatt disconnect ret:' + ret); - expect(ret).assertEqual(false); - done(); - }) - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_GATT_CLIENT_CLOSE_0001 - * @tc.name testGattClientClose - * @tc.desc Test GattClientClose api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_GATT_CLIENT_CLOSE_0001', 0, async function (done) { - console.info('[bluetooth_js] GattclientClose test start ...'); - await tryToEnableBt(); - try{ - let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - let result = gattClient.close(); - console.info("[bluetooth_js] GattclientClose result:" + JSON.stringify(result)); - expect(result).assertTrue(); - }catch(error){ - console.info("[bluetooth_js] GattclientClose err:" + JSON.stringify(error)); - expect(null).assertFail(); - } - done(); - }) - -}) +/* + * Copyright (C) 2022 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 bluetooth from '@ohos.bluetooth'; +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' + + +let SppType = { + SPP_RFCOMM : 0 +} + +let MatchMode = { + MATCH_MODE_AGGRESSIVE : 1, + MATCH_MODE_STICKY : 2 +} + +let MajorClass = { + MAJOR_MISC : 0x0000, + MAJOR_COMPUTER : 0x0100, + MAJOR_PHONE : 0x0200, + MAJOR_NETWORKING : 0x0300, + MAJOR_AUDIO_VIDEO : 0x0400, + MAJOR_PERIPHERAL : 0x0500, + MAJOR_IMAGING : 0x0600, + MAJOR_WEARABLE : 0x0700, + MAJOR_TOY : 0x0800, + MAJOR_HEALTH : 0x0900, + MAJOR_UNCATEGORIZED : 0x1F00 +} + + +export default function bluetoothhostTest() { +describe('bluetoothhostTest', function() { + + let gattServer = null; + let gattClient = null; + beforeAll(function () { + console.info('beforeAll called') + gattServer = bluetooth.BLE.createGattServer(); + gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + }) + beforeEach(function () { + console.info('beforeEach called') + + }) + afterEach(function () { + console.info('afterEach called') + }) + afterAll(function () { + console.info('afterAll called') + }) + + function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) + } + + async function tryToEnableBt() { + let sta = bluetooth.getState(); + switch(sta){ + case 0: + console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta)); + bluetooth.enableBluetooth(); + await sleep(3000); + break; + case 1: + console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta)); + await sleep(3000); + break; + case 2: + console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta)); + break; + case 3: + console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta)); + bluetooth.enableBluetooth(); + await sleep(3000); + break; + default: + console.info('[bluetooth_js] enable success'); + } + } + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_ENABLE_0001 + * @tc.name testEnableBluetooth + * @tc.desc Test EnableBluetooth api by promise. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_ENABLE_0001', 0, async function (done) { + console.info('[bluetooth_js] enable start'); + await tryToEnableBt(); + let state = bluetooth.getState(); + console.info('[bluetooth_js] getState On = '+ JSON.stringify(state)); + expect(state).assertEqual(2); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetoothble_GET_CONNECTED_BLEDEVICES_0001 + * @tc.name testGetConnectedBLEDevices + * @tc.desc Test getConnectedBLEDevices api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetoothble_GET_CONNECTED_BLEDEVICES_0001', 0, async function (done) { + console.info('[bluetooth_js] getConnectedBLEDevices test start ...'); + await tryToEnableBt(); + let result = bluetooth.BLE.getConnectedBLEDevices(); + console.info("[bluetooth_js] getConnDev:" + JSON.stringify(result)+ "length:" +result.length); + expect(result.length).assertEqual(0); + done(); + }) + + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_ADVERTISING_0001 + * @tc.name testStartAdvertising + * @tc.desc Test StartAdvertising api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_ADVERTISING_0001', 0, async function (done) { + console.info('[bluetooth_js] BLE advertising start'); + await tryToEnableBt(); + let manufactureValueBuffer = new Uint8Array(4); + manufactureValueBuffer[0] = 1; + manufactureValueBuffer[1] = 2; + manufactureValueBuffer[2] = 3; + manufactureValueBuffer[3] = 4; + let serviceValueBuffer = new Uint8Array(4); + serviceValueBuffer[0] = 4; + serviceValueBuffer[1] = 6; + serviceValueBuffer[2] = 7; + serviceValueBuffer[3] = 8; + let promise = new Promise((resolve) => { + let gattServer = bluetooth.BLE.createGattServer() + let start = gattServer.startAdvertising({ + interval:150, + txPower:60, + connectable:true, + },{ + serviceUuids:["12"], + manufactureData:[{ + manufactureId:4567, + manufactureValue:manufactureValueBuffer.buffer + }], + serviceData:[{ + serviceUuid:"1234", + serviceValue:serviceValueBuffer.buffer + }], + },{ + serviceUuids:["12"], + manufactureData:[{ + manufactureId:1789, + manufactureValue:manufactureValueBuffer.buffer + }], + serviceData:[{ + serviceUuid:"1794", + serviceValue:serviceValueBuffer.buffer + }], + }); + console.info('[bluetooth_js] startAdv res: ' + JSON.stringify(start)); + expect(start).assertNull(); + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_STOP_ADVERTISING_0001 + * @tc.name testStopAdvertising + * @tc.desc Test StopAdvertising api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_STOP_ADVERTISING_0001', 0, async function (done) { + console.info('[bluetooth_js] BLE stop advertising start'); + await tryToEnableBt(); + let gattServer = bluetooth.BLE.createGattServer(); + let stop = gattServer.stopAdvertising(); + expect(true).assertTrue(); + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_CONNRCT_0001 + * @tc.name testConnect + * @tc.desc Test Connect api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_CONNRCT_0001', 0, async function (done) { + console.info('[bluetooth_js] gatt connect start'); + await tryToEnableBt(); + gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + let ret = gattClient.connect(); + expect(ret).assertTrue(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GET_RSSI_VALUE_CALLBACK + * @tc.name testGetRssiValue + * @tc.desc Test GetRssiValue api by callback. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GET_RSSI_VALUE_CALLBACK', 0, async function (done) { + console.info('[bluetooth_js] BLE get rssi1 start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.getRssiValue((err, data)=> { + console.info('[bluetooth_js] rssi err:' + JSON.stringify(err)+'value:'+JSON.stringify(data)); + expect(data).assertNull(); + }); + resolve() + }) + await promise.then(done) + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GET_RSSI_VALUE_PROMISE_0001 + * @tc.name testGetRssiValue + * @tc.desc Test GetRssiValue api by promise. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GET_RSSI_VALUE_PROMISE_0001', 0, async function (done) { + console.info('[bluetooth_js] BLE get rssi start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.getRssiValue().then((data) => { + if (data != null) { + expect(true).assertEqual(true); + } else { + console.info('[bluetooth_js] BLE read rssi ' + JSON.stringify(data)); + let rssiLength = Object.keys(data).length; + expect(rssiLength).assertEqual(0); + } + }).catch(err => { + console.error(`bluetooth getRssiValue has error: ${err}`); + expect(true).assertEqual(true); + }); + resolve() + }) + await promise.then(done) + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GET_DEVICE_NAME_PROMISE_0001 + * @tc.name testGetDeviceName + * @tc.desc Test GetDeviceName api by promise. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GET_DEVICE_NAME_PROMISE_0001', 0, async function (done) { + console.info('[bluetooth_js] BLE get device name promise start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.getDeviceName().then((data) => { + console.info('[bluetooth_js] device name' + JSON.stringify(data)) + expect(data).assertNull(); + done(); + }) + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GET_DEVICE_NAME_CALLBACK_0001 + * @tc.name testGetDeviceName + * @tc.desc Test GetDeviceName api by callback. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GET_DEVICE_NAME_CALLBACK_0001', 0, async function (done) { + console.info('[bluetooth_js] BLE get device name callback start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.getDeviceName((err, data)=> { + console.info('[bluetooth_js] device name' + JSON.stringify(data)) + expect(data).assertNull(); + done(); + }) + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_GETSERVICES_CALLBACK_0001 + * @tc.name testGetServices + * @tc.desc Test GetServices api by callback. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_GETSERVICES_CALLBACK_0001', 0, async function (done) { + console.info('[bluetooth_js] getServices callback start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.getServices((err, data)=> { + console.info('[bluetooth_js] device name err1 ' + JSON.stringify(err) +'name1' + JSON.stringify(data)); + expect(true).assertEqual(err.code == -1); + done(); + }) + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_GETSERVICES_PROMISE_0001 + * @tc.name testGetServices + * @tc.desc Test GetServices api by promise. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_GETSERVICES_PROMISE_0001', 0, async function (done) { + console.info('[bluetooth_js] getServices promise start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.getServices().then((object) => { + if (object != null) { + expect(true).assertEqual(true); + } else { + console.info('[bluetooth_js] getServices successfully:'+JSON.stringify(object)); + expect(null).assertFail(); + } + done(); + }).catch(err => { + console.error(`bluetooth getServices has error: ${err}`); + expect(true).assertEqual(true); + done(); + }); + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_READ_CHARA_VALUE_0001 + * @tc.name testReadCharacteristicValue + * @tc.desc Test ReadCharacteristicValue api by promise. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_READ_CHARA_VALUE_0001', 0, async function (done) { + console.info('[bluetooth_js] readCharacteristicValue promise start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let descriptors = []; + let arrayBuffer = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBuffer); + desValue[0] = 11; + let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; + descriptors[0] = descriptor; + let arrayBufferCCC = new ArrayBuffer(8); + let cccValue = new Uint8Array(arrayBufferCCC); + cccValue[0] = 32; + let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', + characteristicValue: arrayBufferCCC, descriptors:descriptors}; + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.readCharacteristicValue(characteristic).then((object) => { + if (object != null) { + expect(true).assertEqual(true); + } else { + console.info('[bluetooth_js] readCharacValue promise data:' + JSON.stringify(data)); + expect(null).assertFail(); + } + done(); + }).catch(err => { + console.error(`bluetooth readCharacteristicValue promise has error: ${err}`); + expect(true).assertEqual(true); + done(); + }); + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_READ_CHARA_VALUE_0002 + * @tc.name testReadCharacteristicValue + * @tc.desc Test ReadCharacteristicValue api by callback. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_READ_CHARA_VALUE_0002', 0, async function (done) { + console.info('[bluetooth_js] readCharacteristicValue callback start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let descriptors = []; + let arrayBuffer = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBuffer); + desValue[0] = 11; + let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; + descriptors[0] = descriptor; + let arrayBufferCCC = new ArrayBuffer(8); + let cccValue = new Uint8Array(arrayBufferCCC); + cccValue[0] = 32; + let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', + characteristicValue: arrayBufferCCC, descriptors:descriptors}; + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.readCharacteristicValue(characteristic,(err, data)=> { + if (err) { + console.error(`bluetooth readCharacteristicValue callback has error: ${err}`); + expect(true).assertEqual(true); + done(); + return; + } + if (object != null) { + expect(true).assertEqual(true); + } else { + console.info('[bluetooth_js] readCharacValue callback data:' + JSON.stringify(data)); + expect(null).assertFail(); + } + done(); + }) + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_READ_DESCRI_VALUE_0001 + * @tc.name testReadDescriptorValue + * @tc.desc Test ReadDescriptorValue api by promise. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_READ_DESCRI_VALUE_0001', 0, async function (done) { + console.info('[bluetooth_js] readDescriptorValue promise start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let arrayBuffer = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBuffer); + desValue[0] = 11; + let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.readDescriptorValue(descriptor).then((object) => { + if (object != null) { + expect(true).assertEqual(true); + } else { + console.info('[bluetooth_js] BLEDescriptor promise data:' + JSON.stringify(object)); + expect(null).assertFail(); + } + done(); + }).catch(err => { + console.error(`bluetooth readDescriptorValue promise has error: ${err}`); + expect(true).assertEqual(true); + done(); + }); + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_READ_DESCRI_VALUE_0002 + * @tc.name testReadDescriptorValue + * @tc.desc Test ReadDescriptorValue api by callback. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_READ_DESCRI_VALUE_0002', 0, async function (done) { + console.info('[bluetooth_js] readDescriptorValue callback start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let arrayBuffer = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBuffer); + desValue[0] = 11; + let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + gattClient.readDescriptorValue(descriptor,(err, data)=> { + if (err) { + console.error(`bluetooth readDescriptorValue callback has error: ${err}`); + expect(true).assertEqual(true); + done(); + return; + } + if (object != null) { + expect(true).assertEqual(true); + } else { + console.info('[bluetooth_js] BLEDescriptor callback data:' + JSON.stringify(object)); + expect(null).assertFail(); + } + done(); + }) + resolve() + }) + await promise.then(done) + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_WRITE_CHARACT_VALUE_0001 + * @tc.name testWriteCharacteristicValue + * @tc.desc Test WriteCharacteristicValue api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_WRITE_CHARACT_VALUE_0001', 0, async function (done) { + console.info('[bluetooth_js] writeCharacteristicValue start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let descriptors = []; + let arrayBuffer = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBuffer); + desValue[0] = 11; + let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; + descriptors[0] = descriptor; + let arrayBufferCCC = new ArrayBuffer(8); + let cccValue = new Uint8Array(arrayBufferCCC); + cccValue[0] = 32; + let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', + characteristicValue: arrayBufferCCC, descriptors:descriptors}; + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + let ret = gattClient.writeCharacteristicValue(characteristic); + console.info('[bluetooth_js] writeCharacteristicValue ret : ' + ret); + expect(ret).assertEqual(false); + resolve() + }) + await promise.then(done) + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_WRITE_DESC_VALUE_0001 + * @tc.name testWriteDescriptorValue + * @tc.desc Test WriteDescriptorValue api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_WRITE_DESC_VALUE_0001', 0, async function (done) { + console.info('[bluetooth_js] writeDescriptorValue start'); + await tryToEnableBt(); + let promise = new Promise((resolve) => { + let arrayBuffer = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBuffer); + desValue[0] = 11; + let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + let ret = gattClient.writeDescriptorValue(descriptor); + console.info('[bluetooth_js] bluetooth writeDescriptorValue ret : ' + ret); + expect(ret).assertEqual(false); + resolve() + }) + await promise.then(done) + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_SET_BLE_MTUSIZE_0001 + * @tc.name testSetBLEMtuSize + * @tc.desc Test SetBLEMtuSize api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_SET_BLE_MTUSIZE_0001', 0, async function (done) { + console.info('[bluetooth_js] setBLEMtuSize start'); + await tryToEnableBt(); + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + let ret = gattClient.setBLEMtuSize(128); + console.info('[bluetooth_js] bluetooth setBLEMtuSize ret:' + ret); + expect(ret).assertEqual(false); + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_SET_NOTIFY_CHARA_CHANGED_0001 + * @tc.name testSetNotifyCharacteristicChanged + * @tc.desc Test SetNotifyCharacteristicChanged api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_SET_NOTIFY_CHARA_CHANGED_0001', 0, async function (done) { + console.info('[bluetooth_js] setNotifyCharacteristicChanged start'); + await tryToEnableBt(); + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + let descriptors = []; + let arrayBuffer = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBuffer); + desValue[0] = 11; + let arrayBufferNotify = new ArrayBuffer(8); + let descNotifyValue = new Uint8Array(arrayBufferNotify); + descNotifyValue[0] = 1 + let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', + descriptorValue: arrayBuffer}; + let descriptorNotify = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', + descriptorValue: arrayBufferNotify}; + descriptors[0] = descriptor; + descriptors[1] = descriptorNotify; + let arrayBufferCCC = new ArrayBuffer(8); + let cccValue = new Uint8Array(arrayBufferCCC); + cccValue[0] = 1; + let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', + characteristicValue: arrayBufferCCC, descriptors:descriptors}; + let ret = gattClient.setNotifyCharacteristicChanged(characteristic, false); + console.info('[bluetooth_js] setNotifyCharacteristicChanged ret:' + ret); + expect(ret).assertEqual(false); + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_ADD_SERVICE_0001 + * @tc.name testAddService + * @tc.desc Test AddService api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_ADD_SERVICE_0001', 0, async function (done) { + console.info('[bluetooth_js] addService start'); + await tryToEnableBt(); + let gattServer = bluetooth.BLE.createGattServer(); + let descriptors = []; + let arrayBuffer = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBuffer); + desValue[0] = 11; + let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', + descriptorValue: arrayBuffer}; + descriptors[0] = descriptor; + let characteristics = []; + let arrayBufferCCC = new ArrayBuffer(8); + let cccValue = new Uint8Array(arrayBufferCCC); + cccValue[0] = 1; + let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', + characteristicValue: arrayBufferCCC, descriptors:descriptors}; + characteristics[0] = characteristic; + let service = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + isPrimary: true, characteristics: characteristics, includeServices: []}; + let ret = gattServer.addService(service); + console.info('[bluetooth_js] bluetooth addService ret : ' + ret); + expect(ret).assertTrue(); + await sleep(3500); + let ret1=gattServer.removeService('00001810-0000-1000-8000-00805F9B34FB'); + console.info('[bluetooth_js]removeService ret:'+ret1); + expect(ret1).assertTrue(); + done(); + }) + + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_NOTIFY_CHARA_CHANGED_0001 + * @tc.name testNotifyCharacteristicChanged + * @tc.desc Test NotifyCharacteristicChanged api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_NOTIFY_CHARA_CHANGED_0001', 0, async function (done) { + console.info('[bluetooth_js] notifyCharacteristicChanged start'); + await tryToEnableBt(); + let gattServer = bluetooth.BLE.createGattServer() + let descriptors = []; + let arrayBufferDesc = new ArrayBuffer(8); + let desValue = new Uint8Array(arrayBufferDesc); + desValue[0] = 11; + let arrayBufferNotifyDesc = new ArrayBuffer(8); + let descNotifyValue = new Uint8Array(arrayBufferNotifyDesc); + descNotifyValue[0] = 1 + let + descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: desValue}; + let descriptorNotify = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', + descriptorValue: descNotifyValue}; + descriptors[0] = descriptor; + descriptors[1] = descriptorNotify; + let arrayBufferCCC = new ArrayBuffer(8); + let cccValue = new Uint8Array(arrayBufferCCC); + cccValue[0] = 1; + let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', + characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', + characteristicValue: cccValue, + descriptors:descriptors,confirm:'false'}; + let ret = gattServer.notifyCharacteristicChanged('00:00:00:00:00:00', characteristic); + console.info('[bluetooth_js] notifyCharacteristicChanged ret : ' + ret); + expect(ret).assertEqual(false); + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_SEND_RESPONSE_0001 + * @tc.name testSendResponse + * @tc.desc Test SendResponse api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_SEND_RESPONSE_0001', 0, async function (done) { + console.info('[bluetooth_js] sendResponse start'); + await tryToEnableBt(); + let gattServer = bluetooth.BLE.createGattServer() + let arrayBuffer = new ArrayBuffer(8); + let value = new Uint8Array(arrayBuffer); + value[0] = 1; + let serverResponse = {deviceId: '00:00:00:00:00', transId: 1, + status: 0, offset: 0, value: arrayBuffer}; + let ret = gattServer.sendResponse(serverResponse); + console.info('[bluetooth_js] sendResponse ret : ' + ret); + + expect(ret).assertEqual(false); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetoothble_GATTSERVER_CLOSE_0001 + * @tc.name testBlePeripheralManagerClose + * @tc.desc Test BlePeripheralManagerClose api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetoothble_GATTSERVER_CLOSE_0001', 0, async function (done) { + console.info('[bluetooth_js] GattserverClose test start ...'); + await tryToEnableBt(); + try{ + let gattServer = bluetooth.BLE.createGattServer() + let result = gattServer.close(); + console.info("[bluetooth_js] GattserverClose res:"+ JSON.stringify(result)); + expect(result).assertTrue(); + }catch(error){ + console.info("[bluetooth_js] GattserverClose err:" + JSON.stringify(error)); + expect(null).assertFail(); + } + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_DISCONNRCT_0001 + * @tc.name testDisConnect + * @tc.desc Test DisConnect api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_DISCONNRCT_0001', 0, async function (done) { + console.info('[bluetooth_js] gatt disconnect start'); + await tryToEnableBt(); + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + let ret = gattClient.disconnect(); + console.info('[bluetooth_js] gatt disconnect ret:' + ret); + expect(ret).assertEqual(false); + done(); + }) + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_GATT_CLIENT_CLOSE_0001 + * @tc.name testGattClientClose + * @tc.desc Test GattClientClose api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_GATT_CLIENT_CLOSE_0001', 0, async function (done) { + console.info('[bluetooth_js] GattclientClose test start ...'); + await tryToEnableBt(); + try{ + let gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + let result = gattClient.close(); + console.info("[bluetooth_js] GattclientClose result:" + JSON.stringify(result)); + expect(result).assertTrue(); + }catch(error){ + console.info("[bluetooth_js] GattclientClose err:" + JSON.stringify(error)); + expect(null).assertFail(); + } + done(); + }) + +}) +} diff --git a/communication/bluetooth_on/src/main/js/default/test/List.test.js b/communication/bluetooth_ble/src/main/js/test/List.test.js similarity index 81% rename from communication/bluetooth_on/src/main/js/default/test/List.test.js rename to communication/bluetooth_ble/src/main/js/test/List.test.js index bd39458f3a158c2195f8fb4188e526971f9833c7..1debd6f1948fc983e343bfa54298926a5d73686a 100644 --- a/communication/bluetooth_on/src/main/js/default/test/List.test.js +++ b/communication/bluetooth_ble/src/main/js/test/List.test.js @@ -13,4 +13,7 @@ * limitations under the License. */ -require('./BluetoothOn.test.js') +import bluetoothhostTest from './BluetoothBle.test.js' +export default function testsuite() { +bluetoothhostTest() +} diff --git a/communication/bluetooth_ble/src/main/resources/base/element/string.json b/communication/bluetooth_ble/src/main/resources/base/element/string.json index 14f9c3a9210f78b12caf3b945dc2b2c0f63f7d3d..d4b2a0a059d21198f9767cea679a5cc2bb8f6dad 100644 --- a/communication/bluetooth_ble/src/main/resources/base/element/string.json +++ b/communication/bluetooth_ble/src/main/resources/base/element/string.json @@ -7,6 +7,22 @@ { "name": "mainability_description", "value": "JS_Phone_Empty Feature Ability" - } + }, + { + "name": "MainAbility_desc", + "value": "description" + }, + { + "name": "MainAbility_label", + "value": "label" + }, + { + "name": "TestAbility_desc", + "value": "description" + }, + { + "name": "TestAbility_label", + "value": "label" + } ] } \ No newline at end of file diff --git a/communication/bluetooth_on/BUILD.gn b/communication/bluetooth_on/BUILD.gn index 6f41a315d7d13696addc2401b8e57877509a70e6..054097df174f6eed58d4b75b78e3f2f1e33d3222 100644 --- a/communication/bluetooth_on/BUILD.gn +++ b/communication/bluetooth_on/BUILD.gn @@ -9,7 +9,7 @@ # 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. +# limitations under the License. import("//test/xts/tools/build/suite.gni") ohos_js_hap_suite("ActsBluetoothOnJsTest") { @@ -23,7 +23,9 @@ ohos_js_hap_suite("ActsBluetoothOnJsTest") { hap_name = "ActsBluetoothOnHapTest" } ohos_js_assets("bluetooth_js_assets") { - source_dir = "./src/main/js/default" + js2abc = true + hap_profile = "./src/main/config.json" + source_dir = "./src/main/js" } ohos_resources("bluetooth_resources") { sources = [ "./src/main/resources" ] diff --git a/communication/bluetooth_on/Test.json b/communication/bluetooth_on/Test.json index eb1655d3ea1fda28eb3d5647ce9ec4c033f0de34..b6172f16b6e0d54c3bf5d40fcdc02ab1ff05d6a0 100644 --- a/communication/bluetooth_on/Test.json +++ b/communication/bluetooth_on/Test.json @@ -1,18 +1,20 @@ -{ - "description": "Configuration for bluetooth js api Tests", - "driver": { - "type": "JSUnitTest", - "test-timeout": "600000", - "package": "ohos.acts.communication.bluetooth.bluetoothhost", - "shell-timeout": "60000" - }, - "kits": [ - { - "test-file-name": [ - "ActsBluetoothOnHapTest.hap" - ], - "type": "AppInstallKit", - "cleanup-apps": true - } - ] -} +{ + "description": "Configuration for bluetooth js api Tests", + "driver": { + "type": "OHJSUnitTest", + "test-timeout": "600000", + "shell-timeout": "600000", + "bundle-name": "ohos.acts.communication.bluetooth.bluetoothhost", + "package-name": "ohos.acts.communication.bluetooth.bluetoothhost", + "testcase-timeout": 70000 + }, + "kits": [ + { + "test-file-name": [ + "ActsBluetoothOnHapTest.hap" + ], + "type": "AppInstallKit", + "cleanup-apps": true + } + ] +} \ No newline at end of file diff --git a/communication/bluetooth_on/src/main/config.json b/communication/bluetooth_on/src/main/config.json index b56e3760ba9b0eb7eebf5af55ad6b00e856a4e7c..9175146c5f6ad96816a2fd65537b4d78a755b4e7 100644 --- a/communication/bluetooth_on/src/main/config.json +++ b/communication/bluetooth_on/src/main/config.json @@ -1,104 +1,135 @@ { - "app": { - "bundleName": "ohos.acts.communication.bluetooth.bluetoothhost", - "vendor": "acts", - "version": { - "code": 1000000, - "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5 - } - }, - "deviceConfig": {}, - "module": { - "package": "ohos.acts.communication.bluetooth.bluetoothhost", - "name": ".MyApplication", - "deviceType": [ - "phone" - ], - "distro": { - "deliveryWithInstall": true, - "moduleName": "entry", - "moduleType": "entry" + "app": { + "bundleName": "ohos.acts.communication.bluetooth.bluetoothhost", + "vendor": "acts", + "version": { + "code": 1000000, + "name": "1.0.0" + }, + "apiVersion": { + "compatible": 4, + "target": 5 + } }, - "abilities": [ - { - "visible": true, - "skills": [ - { - "entities": [ - "entity.system.home" - ], - "actions": [ - "action.system.home" - ] - } + "deviceConfig": {}, + "module": { + "package": "ohos.acts.communication.bluetooth.bluetoothhost", + "name": ".entry", + "deviceType": [ + "phone" ], - "name": "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility", - "icon": "$media:icon", - "description": "$string:mainability_description", - "label": "$string:app_name", - "type": "page", - "launchType": "standard" - } - ], - "js": [ - { - "pages": [ - "pages/index/index" + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "formsEnabled": false, + "name": ".MainAbility", + "srcLanguage": "js", + "srcPath": "MainAbility", + "icon": "$media:icon", + "description": "$string:MainAbility_desc", + "label": "$string:MainAbility_label", + "type": "page", + "visible": true, + "launchType": "standard" + }, + { + "orientation": "unspecified", + "formsEnabled": false, + "name": ".TestAbility", + "srcLanguage": "js", + "srcPath": "TestAbility", + "icon": "$media:icon", + "description": "$string:TestAbility_desc", + "label": "$string:TestAbility_label", + "type": "page", + "visible": true, + "launchType": "standard" + } ], - "name": "default", - "window": { - "designWidth": 720, - "autoDesignWidth": false - } - } - ], - "reqPermissions": [ - { - "name": "ohos.permission.USE_BLUETOOTH", - "reason": "", - "usedScene": { - "ability": [ - "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" - ], - "when": "inuse" - } - }, - { - "name": "ohos.permission.MANAGE_BLUETOOTH", - "reason": "", - "usedScene": { - "ability": [ - "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" - ], - "when": "inuse" - } - }, - { - "name": "ohos.permission.DISCOVER_BLUETOOTH", - "reason": "", - "usedScene": { - "ability": [ - "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" - ], - "when": "inuse" - } - }, - { - "name": "ohos.permission.LOCATION", - "reason": "", - "usedScene": { - "ability": [ - "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" - ], - "when": "inuse" - } - } - ] - - } -} - + "js": [ + { + "pages": [ + "pages/index/index" + ], + "name": "default", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + }, + { + "pages": [ + "pages/index/index" + ], + "name": ".TestAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.USE_BLUETOOTH", + "reason": "", + "usedScene": { + "ability": [ + "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.MANAGE_BLUETOOTH", + "reason": "", + "usedScene": { + "ability": [ + "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.DISCOVER_BLUETOOTH", + "reason": "", + "usedScene": { + "ability": [ + "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.LOCATION", + "reason": "", + "usedScene": { + "ability": [ + "ohos.acts.communication.bluetooth.bluetoothhost.MainAbility" + ], + "when": "inuse" + } + } + ], + "testRunner": { + "name": "OpenHarmonyTestRunner", + "srcPath": "TestRunner" + }, + "mainAbility": ".MainAbility", + "srcPath": "" + } +} \ No newline at end of file diff --git a/communication/bluetooth_on/src/main/js/default/app.js b/communication/bluetooth_on/src/main/js/MainAbility/app.js similarity index 100% rename from communication/bluetooth_on/src/main/js/default/app.js rename to communication/bluetooth_on/src/main/js/MainAbility/app.js diff --git a/communication/bluetooth_ble/src/main/js/default/i18n/en-US.json b/communication/bluetooth_on/src/main/js/MainAbility/i18n/en-US.json similarity index 100% rename from communication/bluetooth_ble/src/main/js/default/i18n/en-US.json rename to communication/bluetooth_on/src/main/js/MainAbility/i18n/en-US.json diff --git a/communication/bluetooth_ble/src/main/js/default/i18n/zh-CN.json b/communication/bluetooth_on/src/main/js/MainAbility/i18n/zh-CN.json similarity index 100% rename from communication/bluetooth_ble/src/main/js/default/i18n/zh-CN.json rename to communication/bluetooth_on/src/main/js/MainAbility/i18n/zh-CN.json diff --git a/communication/bluetooth_ble/src/main/js/default/pages/index/index.css b/communication/bluetooth_on/src/main/js/MainAbility/pages/index/index.css similarity index 100% rename from communication/bluetooth_ble/src/main/js/default/pages/index/index.css rename to communication/bluetooth_on/src/main/js/MainAbility/pages/index/index.css diff --git a/communication/bluetooth_ble/src/main/js/default/pages/index/index.hml b/communication/bluetooth_on/src/main/js/MainAbility/pages/index/index.hml similarity index 100% rename from communication/bluetooth_ble/src/main/js/default/pages/index/index.hml rename to communication/bluetooth_on/src/main/js/MainAbility/pages/index/index.hml diff --git a/communication/bluetooth_on/src/main/js/MainAbility/pages/index/index.js b/communication/bluetooth_on/src/main/js/MainAbility/pages/index/index.js new file mode 100644 index 0000000000000000000000000000000000000000..95f0ced35911ffa1f0c03aec4e7da0ea5a474c66 --- /dev/null +++ b/communication/bluetooth_on/src/main/js/MainAbility/pages/index/index.js @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2022 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 { + data: { + title: "" + }, + onInit() { + this.title = this.$t('strings.world'); + }, + onShow() { + console.info('onShow finish') + }, + onReady() { + console.info('onReady finish') + }, +} + diff --git a/communication/bluetooth_on/src/main/js/TestAbility/app.js b/communication/bluetooth_on/src/main/js/TestAbility/app.js new file mode 100644 index 0000000000000000000000000000000000000000..cdc31f3dcf031e2f6a7665d9653e53bb649e21c5 --- /dev/null +++ b/communication/bluetooth_on/src/main/js/TestAbility/app.js @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' +import { Hypium } from '@ohos/hypium' +import testsuite from '../test/List.test' + +export default { + onCreate() { + console.info('TestApplication onCreate') + var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + console.info('start run testcase!!!') + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) + }, + onDestroy() { + console.info("TestApplication onDestroy"); + } +}; diff --git a/communication/bluetooth_on/src/main/js/TestAbility/i18n/en-US.json b/communication/bluetooth_on/src/main/js/TestAbility/i18n/en-US.json new file mode 100644 index 0000000000000000000000000000000000000000..3cb24b374b1d919ca8eac0638f361692b603a900 --- /dev/null +++ b/communication/bluetooth_on/src/main/js/TestAbility/i18n/en-US.json @@ -0,0 +1,8 @@ +{ + "strings": { + "hello": "Hello", + "world": "World" + }, + "Files": { + } +} \ No newline at end of file diff --git a/communication/bluetooth_on/src/main/js/TestAbility/i18n/zh-CN.json b/communication/bluetooth_on/src/main/js/TestAbility/i18n/zh-CN.json new file mode 100644 index 0000000000000000000000000000000000000000..c804e32c0c3103929baca5617cdac70be11fdba1 --- /dev/null +++ b/communication/bluetooth_on/src/main/js/TestAbility/i18n/zh-CN.json @@ -0,0 +1,8 @@ +{ + "strings": { + "hello": "您好", + "world": "世界" + }, + "Files": { + } +} \ No newline at end of file diff --git a/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.css b/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.css new file mode 100644 index 0000000000000000000000000000000000000000..b1bcd43387ba131cc1d30975ff7508a6f8084a4b --- /dev/null +++ b/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.css @@ -0,0 +1,30 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 60px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} + +@media screen and (device-type: phone) and (orientation: landscape) { + .title { + font-size: 60px; + } +} + +@media screen and (device-type: tablet) and (orientation: landscape) { + .title { + font-size: 100px; + } +} \ No newline at end of file diff --git a/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.hml b/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.hml new file mode 100644 index 0000000000000000000000000000000000000000..f629c71a9be857db6cdf94149652a191b9b272ea --- /dev/null +++ b/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.hml @@ -0,0 +1,5 @@ +
+ + {{ $t('strings.hello') }} {{ title }} + +
diff --git a/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.js b/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.js new file mode 100644 index 0000000000000000000000000000000000000000..88b083a7f6b979019d6a2c5ad20b19c5fd43286b --- /dev/null +++ b/communication/bluetooth_on/src/main/js/TestAbility/pages/index/index.js @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 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 { + data: { + title: "" + }, + onInit() { + this.title = this.$t('strings.world'); + } +} + + + diff --git a/communication/bluetooth_on/src/main/js/TestRunner/OpenHarmonyTestRunner.js b/communication/bluetooth_on/src/main/js/TestRunner/OpenHarmonyTestRunner.js new file mode 100644 index 0000000000000000000000000000000000000000..c5fa8620ca77d381f20b65a903b833e6e3378c97 --- /dev/null +++ b/communication/bluetooth_on/src/main/js/TestRunner/OpenHarmonyTestRunner.js @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' + +function translateParamsToString(parameters) { + const keySet = new Set([ + '-s class', '-s notClass', '-s suite', '-s itName', + '-s level', '-s testType', '-s size', '-s timeout', + '-s package', '-s dryRun' + ]) + let targetParams = ''; + for (const key in parameters) { + if (keySet.has(key)) { + targetParams += ' ' + key + ' ' + parameters[key] + } + } + return targetParams.trim() +} + + export default { + onPrepare() { + console.info('OpenHarmonyTestRunner OnPrepare') + }, + onRun() { + console.log('OpenHarmonyTestRunner onRun run') + var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + + var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility' + + var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName + cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters) + var debug = abilityDelegatorArguments.parameters["-D"] + console.info('debug value : '+debug) + if (debug == 'true') + { + cmd += ' -D' + } + console.info('cmd : '+cmd) + abilityDelegator.executeShellCommand(cmd, (err, data) => { + console.info('executeShellCommand : err : ' + JSON.stringify(err)); + console.info('executeShellCommand : data : ' + data.stdResult); + console.info('executeShellCommand : data : ' + data.exitCode); + }) + } +}; diff --git a/communication/bluetooth_on/src/main/js/default/pages/index/index.js b/communication/bluetooth_on/src/main/js/default/pages/index/index.js deleted file mode 100644 index 504efbf0411cd9dde3e56a610f12b0cb5b389629..0000000000000000000000000000000000000000 --- a/communication/bluetooth_on/src/main/js/default/pages/index/index.js +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2022 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, ExpectExtend} from 'deccjsunit/index' - -export default { - data: { - title: "", - myTimeout:70000 - }, - onInit() { - this.title = this.$t('strings.world'); - }, - onShow() { - console.info('onShow finish') - const core = Core.getInstance() - const expectExtend = new ExpectExtend({ - 'id': 'extend' - }) - core.addService('expect', expectExtend) - core.init() - - const configService = core.getDefaultService('config') - this.timeout = this.myTimeout - configService.setConfig(this) - - require('../../test/List.test') - core.execute() - - }, - onReady() { - console.info('onReady finish') - }, -} - diff --git a/communication/bluetooth_on/src/main/js/default/test/BluetoothOn.test.js b/communication/bluetooth_on/src/main/js/test/BluetoothOn.test.js similarity index 97% rename from communication/bluetooth_on/src/main/js/default/test/BluetoothOn.test.js rename to communication/bluetooth_on/src/main/js/test/BluetoothOn.test.js index a50738648190d728e3098bd5b020b58f5944fbb9..5d6cd426747b72c9a84c62c9c8134e4731ca2177 100644 --- a/communication/bluetooth_on/src/main/js/default/test/BluetoothOn.test.js +++ b/communication/bluetooth_on/src/main/js/test/BluetoothOn.test.js @@ -1,885 +1,887 @@ -/* - * Copyright (C) 2022 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 bluetooth from '@ohos.bluetooth'; -import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' - -let MajorMinorClass = { - COMPUTER_UNCATEGORIZED : 0x0100, - COMPUTER_DESKTOP : 0x0104, - COMPUTER_SERVER : 0x0108, - COMPUTER_LAPTOP : 0x010C, - COMPUTER_HANDHELD_PC_PDA : 0x0110, - COMPUTER_PALM_SIZE_PC_PDA : 0x0114, - COMPUTER_WEARABLE : 0x0118, - COMPUTER_TABLET : 0x011C, - PHONE_UNCATEGORIZED : 0x0200, - PHONE_CELLULAR : 0x0204, - PHONE_CORDLESS : 0x0208, - PHONE_SMART : 0x020C, - PHONE_MODEM_OR_GATEWAY : 0x0210, - PHONE_ISDN : 0x0214, - NETWORK_FULLY_AVAILABLE : 0x0300, - NETWORK_1_TO_17_UTILIZED : 0x0320, - NETWORK_17_TO_33_UTILIZED : 0x0340, - NETWORK_33_TO_50_UTILIZED : 0x0360, - NETWORK_60_TO_67_UTILIZED : 0x0380, - NETWORK_67_TO_83_UTILIZED : 0x03A0, - NETWORK_83_TO_99_UTILIZED : 0x03C0, - NETWORK_NO_SERVICE : 0x03E0, - AUDIO_VIDEO_UNCATEGORIZED : 0x0400, - AUDIO_VIDEO_WEARABLE_HEADSET : 0x0404, - AUDIO_VIDEO_HANDSFREE : 0x0408, - AUDIO_VIDEO_MICROPHONE : 0x0410, - AUDIO_VIDEO_LOUDSPEAKER : 0x0414, - AUDIO_VIDEO_HEADPHONES : 0x0418, - AUDIO_VIDEO_PORTABLE_AUDIO : 0x041C, - AUDIO_VIDEO_CAR_AUDIO : 0x0420, - AUDIO_VIDEO_SET_TOP_BOX : 0x0424, - AUDIO_VIDEO_HIFI_AUDIO : 0x0428, - AUDIO_VIDEO_VCR : 0x042C, - AUDIO_VIDEO_VIDEO_CAMERA : 0x0430, - AUDIO_VIDEO_CAMCORDER : 0x0434, - AUDIO_VIDEO_VIDEO_MONITOR : 0x0438, - AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER : 0x043C, - AUDIO_VIDEO_VIDEO_CONFERENCING : 0x0440, - AUDIO_VIDEO_VIDEO_GAMING_TOY : 0x0448, - PERIPHERAL_NON_KEYBOARD_NON_POINTING : 0x0500, - PERIPHERAL_KEYBOARD : 0x0540, - PERIPHERAL_POINTING_DEVICE : 0x0580, - PERIPHERAL_KEYBOARD_POINTING : 0x05C0, - PERIPHERAL_UNCATEGORIZED : 0x0500, - PERIPHERAL_JOYSTICK : 0x0504, - PERIPHERAL_GAMEPAD : 0x0508, - PERIPHERAL_REMOTE_CONTROL : 0x05C0, - PERIPHERAL_SENSING_DEVICE : 0x0510, - PERIPHERAL_DIGITIZER_TABLET : 0x0514, - PERIPHERAL_CARD_READER : 0x0518, - PERIPHERAL_DIGITAL_PEN : 0x051C, - PERIPHERAL_SCANNER_RFID : 0x0520, - PERIPHERAL_GESTURAL_INPUT : 0x0522, - IMAGING_UNCATEGORIZED : 0x0600, - IMAGING_DISPLAY : 0x0610, - IMAGING_CAMERA : 0x0620, - IMAGING_SCANNER : 0x0640, - IMAGING_PRINTER : 0x0680, - WEARABLE_UNCATEGORIZED : 0x0700, - WEARABLE_WRIST_WATCH : 0x0704, - WEARABLE_PAGER : 0x0708, - WEARABLE_JACKET : 0x070C, - WEARABLE_HELMET : 0x0710, - WEARABLE_GLASSES : 0x0714, - TOY_UNCATEGORIZED : 0x0800, - TOY_ROBOT : 0x0804, - TOY_VEHICLE : 0x0808, - TOY_DOLL_ACTION_FIGURE : 0x080C, - TOY_CONTROLLER : 0x0810, - TOY_GAME : 0x0814, - HEALTH_UNCATEGORIZED : 0x0900, - HEALTH_BLOOD_PRESSURE : 0x0904, - HEALTH_THERMOMETER : 0x0908, - HEALTH_WEIGHING : 0x090C, - HEALTH_GLUCOSE : 0x0910, - HEALTH_PULSE_OXIMETER : 0x0914, - HEALTH_PULSE_RATE : 0x0918, - HEALTH_DATA_DISPLAY : 0x091C, - HEALTH_STEP_COUNTER : 0x0920, - HEALTH_BODY_COMPOSITION_ANALYZER : 0x0924, - HEALTH_PEAK_FLOW_MOITOR : 0x0928, - HEALTH_MEDICATION_MONITOR : 0x092C, - HEALTH_KNEE_PROSTHESIS : 0x0930, - HEALTH_ANKLE_PROSTHESIS : 0x0934, - HEALTH_GENERIC_HEALTH_MANAGER : 0x0938, - HEALTH_PERSONAL_MOBILITY_DEVICE : 0x093C, - HEALTH_PERSONAL_MOBILITY_DEVICE : 0x093C -}; - -let ScanDuty= - { - SCAN_MODE_LOW_POWER : 0, - SCAN_MODE_BALANCED : 1, - SCAN_MODE_LOW_LATENCY : 2, - }; - -let MatchMode= - { - MATCH_MODE_AGGRESSIVE : 1, - MATCH_MODE_STICKY : 2, - }; - - -describe('bluetoothhostTest', function() { - - let gattServer = null; - let gattClient = null; - beforeAll(function () { - console.info('beforeAll called') - gattServer = bluetooth.BLE.createGattServer(); - gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); - }) - beforeEach(function () { - console.info('beforeEach called') - - }) - afterEach(function () { - console.info('afterEach called') - }) - afterAll(function () { - console.info('afterAll called') - }) - - function sleep(delay) { - return new Promise(resovle => setTimeout(resovle, delay)) - } - - async function tryToEnableBt() { - let sta = bluetooth.getState(); - switch(sta){ - case 0: - console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta)); - bluetooth.enableBluetooth(); - await sleep(3000); - break; - case 1: - console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta)); - await sleep(3000); - break; - case 2: - console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta)); - break; - case 3: - console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta)); - bluetooth.enableBluetooth(); - await sleep(3000); - break; - default: - console.info('[bluetooth_js] enable success'); - } - } - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_ENABLE_0001 - * @tc.name testEnableBluetooth - * @tc.desc Test EnableBluetooth api by promise. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_ENABLE_0001', 0, async function (done) { - console.info('[bluetooth_js] enable start'); - bluetooth.on("stateChange", onReceiveEvent); - function onReceiveEvent(data) { - if (data == bluetooth.BluetoothState.STATE_ON) { - console.info('enable bluetooth'); - bluetooth.off('stateChange', result => { - expect(true).assertEqual(result ==null); - done(); - }); - } - } - await tryToEnableBt(); - let state = bluetooth.getState(); - expect(state).assertEqual(2); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLUETOOTH_DISCOVERY_0001 - * @tc.name testClassicStartBluetoothDiscovery - * @tc.desc Test ClassicStartBluetoothDiscovery api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLUETOOTH_DISCOVERY_0001', 0, async function (done) { - console.info('[bluetooth_js] discovery1 start'); - await sleep(1000); - await tryToEnableBt(); - bluetooth.on("bluetoothDeviceFind", onReceiveEvent) - function onReceiveEvent(data) { - console.info('[bluetooth_js] Device' + JSON.stringify(data)+ 'length' + data.length) - expect(true).assertTrue(data.length >= 0); - bluetooth.off('bluetoothDeviceFind', result => { - console.info("[bluetooth_js] bluetoothDeviceFind off1:" + JSON.stringify(result)); - expect(true).assertEqual(true); - done(); - }) - } - bluetooth.startBluetoothDiscovery(); - }) - - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0001 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0001', 0, async function (done) { - console.info('[bluetooth_js] BLE scan1 test start'); - await sleep(1000); - await tryToEnableBt(); - function onReceiveEvent(data) - { - console.info('[bluetooth_js] BLE scan device find result1 = '+JSON.stringify(data)); - except(true).assertTrue(data.length>0); - } - bluetooth.BLE.on("BLEDeviceFind") - bluetooth.BLE.startBLEScan([{}]); - await sleep(1000); - console.info('[bluetooth_js] BLE SCAN OFF01'); - bluetooth.BLE.off('BLEDeviceFind'); - bluetooth.BLE.stopBLEScan(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0002 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0002', 0, async function (done) { - console.info('[bluetooth_js] BLE scan2 test start'); - await sleep(1000); - await tryToEnableBt(); - function onReceiveEvent(data) - { - console.info('[bluetooth_js] BLE scan device find result2 = '+JSON.stringify(data )); - except(true).assertTrue(data.length>0); - } - bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) - bluetooth.BLE.startBLEScan([{deviceId:"00:11:22:33:44:55"}]); - await sleep(1000); - console.info('[bluetooth_js] BLE scan off2'); - bluetooth.BLE.off('BLEDeviceFind'); - bluetooth.BLE.stopBLEScan(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0003 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0003', 0, async function (done) { - console.info('[bluetooth_js] BLE scan3 test start'); - await sleep(1000); - await tryToEnableBt(); - function onReceiveEvent(data) - { - console.info('[bluetooth_js] BLE scan device find result3 = '+JSON.stringify(data)); - except(true).assertTrue(data.length>0); - } - bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) - bluetooth.BLE.startBLEScan([{name:"blue_test"}]); - await sleep(1000); - console.info('[bluetooth_js] BLE scan off3'); - bluetooth.BLE.off('BLEDeviceFind'); - bluetooth.BLE.stopBLEScan(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0004 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0004', 0, async function (done) { - console.info('[bluetooth_js] BLE scan4 test start'); - await sleep(1000); - await tryToEnableBt(); - function onReceiveEvent(data) { - console.info('[bluetooth_js] BLE scan device find result4 = '+ JSON.stringify(data)); - expect(true).assertTrue(data.length >= 0);} - bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) - bluetooth.BLE.startBLEScan([{serviceUuid:"00001888-0000-1000-8000-00805f9b34fb"}]); - await sleep(1000); - console.info('[bluetooth_js] BLE scan device find off4:'); - bluetooth.BLE.off('BLEDeviceFind'); - bluetooth.BLE.stopBLEScan(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0005 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0005', 0, async function (done) { - console.info('[bluetooth_js] BLE scan5 test start'); - await sleep(1000); - await tryToEnableBt(); - function onReceiveEvent(data) - { - console.info('[bluetooth_js] BLE scan device find result5 = '+ JSON.stringify(data)); - expect(true).assertTrue(data.length > 0); - } - bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) - bluetooth.BLE.startBLEScan( - [{}], - { - interval: 500, - dutyMode: ScanDuty.SCAN_MODE_LOW_POWER, - matchMode: MatchMode.MATCH_MODE_AGGRESSIVE, - } - ); - await sleep(1000); - bluetooth.BLE.off("BLEDeviceFind"); - bluetooth.BLE.stopBLEScan(); - done(); - }) - - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0006 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0006', 0, async function (done) { - console.info('[bluetooth_js] BLE scan6 test start'); - await sleep(1000); - await tryToEnableBt(); - function onReceiveEvent(data) - { - consle.info('[bluetooth_js] BLE scan device find result6 = '+ JSON.stringify(data)); - expect(true).assertTrue(data.length > 0); - } - bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) - bluetooth.BLE.startBLEScan( - [{}], - { - interval: 500, - dutyMode: ScanDuty.SCAN_MODE_BALANCED, - matchMode: MatchMode.MATCH_MODE_AGGRESSIVE, - } - ); - await sleep(1000); - bluetooth.BLE.off('BLEDeviceFind'); - bluetooth.BLE.stopBLEScan(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0007 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0007', 0, async function (done) { - console.info('[bluetooth_js] BLE scan7 test start'); - await sleep(1000); - await tryToEnableBt(); - function onReceiveEvent(data) { - console.info('[bluetooth_js] BLE scan device find result7 = '+ JSON.stringify(data)); - expect(true).assertTrue(data.length > 0); - } - bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) - bluetooth.BLE.startBLEScan( - [{}], - { - interval: 500, - dutyMode: ScanDuty.SCAN_MODE_LOW_LATENCY, - matchMode: MatchMode.MATCH_MODE_AGGRESSIVE, - } - ); - await sleep(1000); - bluetooth.BLE.off('BLEDeviceFind'); - await sleep(1000); - bluetooth.BLE.stopBLEScan(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0008 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0008', 0, async function (done) { - console.info('[bluetooth_js] BLE scan8 test start'); - await sleep(1000); - await tryToEnableBt(); - function onReceiveEvent(data) - { - console.info('[bluetooth_js] BLE scan device find result8 = '+ JSON.stringify(data)); - expect(true).assertTrue(data.length >= 0); - } - bluetooth.BLE.on("BLEDeviceFind",onReceiveEvent) - bluetooth.BLE.startBLEScan( - [{}], - { - interval: 0, - dutyMode: ScanDuty.SCAN_MODE_LOW_POWER, - matchMode: MatchMode.MATCH_MODE_STICKY, - } - ); - await sleep(1000); - bluetooth.BLE.off('BLEDeviceFind'); - await sleep(1000); - bluetooth.BLE.stopBLEScan(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0009 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0009', 0, async function (done) { - console.info('[bluetooth_js] BLE scan9 test start'); - await sleep(2000); - await tryToEnableBt(); - let disable = bluetooth.disableBluetooth(); - console.info('[bluetooth_js] stopScan disable:' + JSON.stringify(disable)); - expect(disable).assertTrue(); - await sleep(3100); - let result1 = bluetooth.BLE.stopBLEScan(); - console.info("[bluetooth_js] onStopBLEScan9 -> " + JSON.stringify(result1)); - expect(result1).assertNull(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0010 - * @tc.name testClassicStartBLEScan - * @tc.desc Test ClassicStartBLEScan api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0010', 0, async function (done) { - console.info('[bluetooth_js] BLE scan10 test start'); - await sleep(1000); - await tryToEnableBt(); - let result1 = bluetooth.BLE.stopBLEScan(); - console.info("[bluetooth_js] onStopBLEScan9 -> " + JSON.stringify(result1)); - expect(result1).assertNull(); - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_PAIR_DEVICE_0001 - * @tc.name testClassicPairDevice - * @tc.desc Test ClassicPairDevice api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_PAIR_DEVICE_0001', 0, async function (done) { - console.info('[bluetooth_js] pair device start'); - await tryToEnableBt(); - bluetooth.BLE.on('pinRequired', result => { - console.info("[bluetooth_js] pinRequired on:" + JSON.stringify(result)); - bluetooth.setDevicePairingConfirmation(result,false); - expect(true).assertEqual(result !=null); - done(); - }); - let enable3 = bluetooth.pairDevice("00:00:00:00:00:00") - bluetooth.BLE.off('pinRequired', result => { - console.info("[bluetooth_js] pinRequired off:" + JSON.stringify(result)); - expect(true).assertEqual(true); - done(); - }); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_PAIR_DEVICE_0002 - * @tc.name testClassicPairDevice - * @tc.desc Test ClassicPairDevice api. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_PAIR_DEVICE_0002', 0, async function (done) { - console.info('[bluetooth_js] pair device start'); - await tryToEnableBt(); - await bluetooth.BLE.on('bondStateChange', result => { - console.info("[bluetooth_js] bondStateChange on:" + JSON.stringify(result) - +'bondStateChange deviceId:' + data.deviceId + 'bondStateChange state:' + data.state); - expect(true).assertEqual(result !=null); - done(); - }); - let BondState= - { - BOND_STATE_INVALID : 0, - BOND_STATE_BONDING : 1, - BOND_STATE_BONDED : 2 - }; - let enable4 = bluetooth.pairDevice("00:00:00:00:00:00") - expect(BondState.BOND_STATE_INVALID == 0).assertTrue(); - expect(BondState.BOND_STATE_BONDING == 1).assertTrue(); - expect(BondState.BOND_STATE_BONDED == 2).assertTrue(); - bluetooth.BLE.off('bondStateChange', result => { - expect(true).assertEqual(true); - done(); - }); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_SPP_LISTEN_0001 - * @tc.name testSppListen - * @tc.desc Test SppListen api by callback. - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_SPP_LISTEN_0001', 0, async function (done) { - console.log("[bluetooth_js]: spp listen start"); - await tryToEnableBt(); - let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', - secure: false, type: 0}; - bluetooth.sppListen('server1', sppOption, function(code, serverSocketNumber) { - console.info('[bluetooth_js] code is: ' + code.code); - if (code.code == 0) { - expect(true).assertEqual(true); - done(); - } else { - expect(true).assertEqual(false); - done(); - } - }); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetoothble_CHARAC_READ_ON_0001 - * @tc.name testonCharacteristicReadOn - * @tc.desc Test CharacteristicReadOn api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetoothble_CHARAC_READ_ON_0001', 0, async function (done) { - try { - await tryToEnableBt(); - console.info('[bluetooth_js] CharacteristicReadOn test start'); - gattServer.on('characteristicRead', function (data) { - console.info('[bluetooth_js] CharRedReq deviceId: ' + data.deviceId + - 'transId:' + data.transId + 'offset:' + data.offset + 'charUuid:' + - data.characteristicUuid + 'serviceUuid:' + data.serviceUuid); - let serverResponse = { - "deviceId": data.deviceId, - "transId": data.transId, - "status": 0, - "offset": data.offset, - "value": str2ab("characteristic read response", data.offset), - }; - let result = gattServer.sendResponse(serverResponse); - expect(JSON.stringify(result)).assertContain("true"); - }); - }catch(e) { - expect(null).assertFail(); - } - try { - console.info('[bluetooth_js] characteristicRead test1 start'); - gattServer.off('characteristicRead', function (data) { - console.info("[bluetooth_js] charaRead off data:" + JSON.stringify(data)); - expect(true).assertEqual(true); - }); - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetoothble_CHARAC_WRITE_ON_0001 - * @tc.name testonCharacteristicwriteOn - * @tc.desc Test CharacteristicwriteOn api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetoothble_CHARAC_WRITE_ON_0001', 0, async function (done) { - try { - await tryToEnableBt(); - console.info('[bluetooth_js] CharacteristicwriteOn test start'); - gattServer.on('characteristicWrite', function (data) { - console.info('[bluetooth_js] CharWriReq deviceId: ' + data.deviceId + - 'transId:' + data.transId + 'offset:' + data.offset + 'isPrep:' + data.isPrep + - 'charUuid:' + data.characteristicUuid + 'serviceUuid:' + data.serviceUuid + - 'value:' + data.value + 'needRsp' + data.needRsp); - if (data.value instanceof ArrayBuffer) { - console.log(`[bluetooth_js] value: ${ab2hex(data.value)}`) - } - if (data.needRsp == false) { - return; - } - console.log(`data.value is ArraryBuffer: ${ab2hex(data.value)}`) - let serverResponse = { - "deviceId": data.deviceId, - "transId": data.transId, - "status": 0, - "offset": data.offset, - "value": data.value, - }; - let result = gattServer.sendResponse(serverResponse); - expect(JSON.stringify(result)).assertContain("true"); - }); - }catch(e) { - expect(null).assertFail(); - } - try { - console.info('[bluetooth_js] characteristicWrite test1 start'); - gattServer.off('characteristicWrite', function (data) { - console.info("[bluetooth_js] charaWrite off data2:" + JSON.stringify(data)); - expect(true).assertEqual(true); - }); - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_DESC_READ_ON_0001 - * @tc.name testDescriptorReadOn - * @tc.desc Test DescriptorReadOn api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_DESC_READ_ON_0001', 0, async function (done) { - try { - await tryToEnableBt(); - console.info('[bluetooth_js] descriptorReadOn test start ...'); - gattServer.on('descriptorRead', function (data) { - console.info("[bluetooth_js] DesRedon jsondata:" + JSON.stringify(data) + - 'deviceId:' + data.deviceId + 'transId:' + data.transId + 'offset:' + - data.offset +'descriptorUuid:' + data.descriptorUuid + 'characteristicUuid:' + - data.characteristicUuid + 'serviceUuid:' + data.serviceUuid); - expect(true).assertEqual(data !=null); - }); - }catch(e) { - expect(null).assertFail(); - } - try { - console.info('[bluetooth_js] descriptorReadOff test start ...'); - gattServer.off('descriptorRead', function (data) { - expect(true).assertEqual(true); - }); - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_DESC_WRITE_ON_0001 - * @tc.name testDescriptorWriteOn - * @tc.desc Test DescriptorWriteOn api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_DESC_WRITE_ON_0001', 0, async function (done) { - try { - await tryToEnableBt(); - console.info('[bluetooth_js] descriptorWriteOn test start ...'); - gattServer.on('descriptorWrite', function (data) { - console.info("[bluetooth_js] desWriOn jsondata: " + JSON.stringify(data) + - 'deviceId: ' + data.deviceId + 'transId:' + data.transId + 'offset:' + - data.offset +'descriptorUuid:' + data.descriptorUuid + - 'charUuid:' + data.characteristicUuid +'serviceUuid:' + data.serviceUuid + - 'value:' + data.value + 'needRsp' + data.needRsp + 'isPrep:' + data.isPrep ); - expect(true).assertEqual(data !=null); - }); - }catch(e) { - expect(null).assertFail(); - } - try { - console.info('[bluetooth_js] descriptorWriteOff test start ...'); - gattServer.off('descriptorWrite', function (data) { - expect(true).assertTrue(); - }); - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_CONNE_STATE_CHANGE_ON_0001 - * @tc.name testConnectStateChangeOn - * @tc.desc Test ConnectStateChangeOn api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_CONNE_STATE_CHANGE_ON_0001', 0, async function (done) { - try { - await tryToEnableBt(); - console.info('[bluetooth_js] ConnectStateChangeOn test start ...'); - gattServer.on('connectStateChange', function (data) { - console.info("[bluetooth_js] connectStaOn jsonData -> " + JSON.stringify(data) + - 'deviceId: ' + data.deviceId + 'state:'+ data.state); - expect(true).assertEqual(data !=null); - }); - }catch(e) { - expect(null).assertFail(); - } - try { - console.info('[bluetooth_js] ConnectStateChangeOff test start ...'); - gattServer.off('connectStateChange', function (data) { - console.info("[bluetooth_js] connectStateChange_off Data:" + JSON.stringify(data)); - expect(true).assertTrue(); - }); - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_BLE_CHAR_CHANGE_ON_0001 - * @tc.name testBLECharacteristicChangeOn - * @tc.desc Test BLECharacteristicChangeOn api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_BLE_CHAR_CHANGE_ON_0001', 0, async function (done) { - try { - await tryToEnableBt(); - console.info('[bluetooth_js] BLECharacteristicChangeOn test start ...'); - gattClient.on('BLECharacteristicChange', function (data) { - console.info("[bluetooth_js] BLECharacteristicChange data " + JSON.stringify(data)); - expect(true).assertEqual(data !=null); - }); - }catch(e) { - expect(null).assertFail(); - } - try { - console.info('[bluetooth_js] BLECharacteristicChangeOff test start'); - gattClient.off('BLECharacteristicChange', function (data) { - console.info("[bluetooth_js] BLECharcChange_off data-> " + JSON.stringify(data)); - expect(true).assertTrue(); - }); - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetooth_BLE_CONNE_STATE_CHANGE_ON_0001 - * @tc.name testBLEConnectionStateChangeOn - * @tc.desc Test BLEConnectionStateChangeOn api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetooth_BLE_CONNE_STATE_CHANGE_ON_0001', 0, async function (done) { - try { - await tryToEnableBt(); - console.info('[bluetooth_js] BLEConnectionStateChangeOn test start'); - gattClient.on('BLEConnectionStateChange', function (data) { - console.info("[bluetooth_js] BLEConnecStateChange_on data " + JSON.stringify(data) - +'deviceId: ' + data.deviceId + 'state:'+ data.state); - expect(true).assertEqual(data !=null); - }); - }catch(e) { - expect(null).assertFail(); - } - try { - console.info('[bluetooth_js] BLEConnectionStateChangeOff test start'); - gattClient.off('BLEConnectionStateChange', function (data) { - console.info("[bluetooth_js] BLEConneStateChange_off data-> " + JSON.stringify(data)); - expect(true).assertEqual(true); - }); - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - - - /** - * @tc.number SUB_COMMUNACATION_bluetoothble_SPP_READ_ON_0001 - * @tc.name testonsppReadOn - * @tc.desc Test sppReadOn api . - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 2 - */ - it('SUB_COMMUNACATION_bluetoothble_SPP_READ_ON_0001', 0, async function (done) { - try { - await tryToEnableBt(); - console.info('[bluetooth_js] sppReadOn test start'); - bluetooth.on("sppRead",-1, (result) => { - console.info("[bluetooth_js] sppReadOn json_result -> " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - }); - }catch(e) { - expect(null).assertFail(); - } - try { - console.info('[bluetooth_js] sppReadOff test start ...'); - bluetooth.off("sppRead",-1, (result) => { - console.info("[bluetooth_js] sppReadOff json_result -> " + JSON.stringify(result)); - expect(true).assertEqual(true); - }); - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - -}) - +/* + * Copyright (C) 2022 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 bluetooth from '@ohos.bluetooth'; +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' + +let MajorMinorClass = { + COMPUTER_UNCATEGORIZED : 0x0100, + COMPUTER_DESKTOP : 0x0104, + COMPUTER_SERVER : 0x0108, + COMPUTER_LAPTOP : 0x010C, + COMPUTER_HANDHELD_PC_PDA : 0x0110, + COMPUTER_PALM_SIZE_PC_PDA : 0x0114, + COMPUTER_WEARABLE : 0x0118, + COMPUTER_TABLET : 0x011C, + PHONE_UNCATEGORIZED : 0x0200, + PHONE_CELLULAR : 0x0204, + PHONE_CORDLESS : 0x0208, + PHONE_SMART : 0x020C, + PHONE_MODEM_OR_GATEWAY : 0x0210, + PHONE_ISDN : 0x0214, + NETWORK_FULLY_AVAILABLE : 0x0300, + NETWORK_1_TO_17_UTILIZED : 0x0320, + NETWORK_17_TO_33_UTILIZED : 0x0340, + NETWORK_33_TO_50_UTILIZED : 0x0360, + NETWORK_60_TO_67_UTILIZED : 0x0380, + NETWORK_67_TO_83_UTILIZED : 0x03A0, + NETWORK_83_TO_99_UTILIZED : 0x03C0, + NETWORK_NO_SERVICE : 0x03E0, + AUDIO_VIDEO_UNCATEGORIZED : 0x0400, + AUDIO_VIDEO_WEARABLE_HEADSET : 0x0404, + AUDIO_VIDEO_HANDSFREE : 0x0408, + AUDIO_VIDEO_MICROPHONE : 0x0410, + AUDIO_VIDEO_LOUDSPEAKER : 0x0414, + AUDIO_VIDEO_HEADPHONES : 0x0418, + AUDIO_VIDEO_PORTABLE_AUDIO : 0x041C, + AUDIO_VIDEO_CAR_AUDIO : 0x0420, + AUDIO_VIDEO_SET_TOP_BOX : 0x0424, + AUDIO_VIDEO_HIFI_AUDIO : 0x0428, + AUDIO_VIDEO_VCR : 0x042C, + AUDIO_VIDEO_VIDEO_CAMERA : 0x0430, + AUDIO_VIDEO_CAMCORDER : 0x0434, + AUDIO_VIDEO_VIDEO_MONITOR : 0x0438, + AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER : 0x043C, + AUDIO_VIDEO_VIDEO_CONFERENCING : 0x0440, + AUDIO_VIDEO_VIDEO_GAMING_TOY : 0x0448, + PERIPHERAL_NON_KEYBOARD_NON_POINTING : 0x0500, + PERIPHERAL_KEYBOARD : 0x0540, + PERIPHERAL_POINTING_DEVICE : 0x0580, + PERIPHERAL_KEYBOARD_POINTING : 0x05C0, + PERIPHERAL_UNCATEGORIZED : 0x0500, + PERIPHERAL_JOYSTICK : 0x0504, + PERIPHERAL_GAMEPAD : 0x0508, + PERIPHERAL_REMOTE_CONTROL : 0x05C0, + PERIPHERAL_SENSING_DEVICE : 0x0510, + PERIPHERAL_DIGITIZER_TABLET : 0x0514, + PERIPHERAL_CARD_READER : 0x0518, + PERIPHERAL_DIGITAL_PEN : 0x051C, + PERIPHERAL_SCANNER_RFID : 0x0520, + PERIPHERAL_GESTURAL_INPUT : 0x0522, + IMAGING_UNCATEGORIZED : 0x0600, + IMAGING_DISPLAY : 0x0610, + IMAGING_CAMERA : 0x0620, + IMAGING_SCANNER : 0x0640, + IMAGING_PRINTER : 0x0680, + WEARABLE_UNCATEGORIZED : 0x0700, + WEARABLE_WRIST_WATCH : 0x0704, + WEARABLE_PAGER : 0x0708, + WEARABLE_JACKET : 0x070C, + WEARABLE_HELMET : 0x0710, + WEARABLE_GLASSES : 0x0714, + TOY_UNCATEGORIZED : 0x0800, + TOY_ROBOT : 0x0804, + TOY_VEHICLE : 0x0808, + TOY_DOLL_ACTION_FIGURE : 0x080C, + TOY_CONTROLLER : 0x0810, + TOY_GAME : 0x0814, + HEALTH_UNCATEGORIZED : 0x0900, + HEALTH_BLOOD_PRESSURE : 0x0904, + HEALTH_THERMOMETER : 0x0908, + HEALTH_WEIGHING : 0x090C, + HEALTH_GLUCOSE : 0x0910, + HEALTH_PULSE_OXIMETER : 0x0914, + HEALTH_PULSE_RATE : 0x0918, + HEALTH_DATA_DISPLAY : 0x091C, + HEALTH_STEP_COUNTER : 0x0920, + HEALTH_BODY_COMPOSITION_ANALYZER : 0x0924, + HEALTH_PEAK_FLOW_MOITOR : 0x0928, + HEALTH_MEDICATION_MONITOR : 0x092C, + HEALTH_KNEE_PROSTHESIS : 0x0930, + HEALTH_ANKLE_PROSTHESIS : 0x0934, + HEALTH_GENERIC_HEALTH_MANAGER : 0x0938, + HEALTH_PERSONAL_MOBILITY_DEVICE : 0x093C, + HEALTH_PERSONAL_MOBILITY_DEVICE : 0x093C +}; + +let ScanDuty= + { + SCAN_MODE_LOW_POWER : 0, + SCAN_MODE_BALANCED : 1, + SCAN_MODE_LOW_LATENCY : 2, + }; + +let MatchMode= + { + MATCH_MODE_AGGRESSIVE : 1, + MATCH_MODE_STICKY : 2, + }; + + +export default function bluetoothhostTest() { +describe('bluetoothhostTest', function() { + + let gattServer = null; + let gattClient = null; + beforeAll(function () { + console.info('beforeAll called') + gattServer = bluetooth.BLE.createGattServer(); + gattClient = bluetooth.BLE.createGattClientDevice("00:00:00:00:00:00"); + }) + beforeEach(function () { + console.info('beforeEach called') + + }) + afterEach(function () { + console.info('afterEach called') + }) + afterAll(function () { + console.info('afterAll called') + }) + + function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) + } + + async function tryToEnableBt() { + let sta = bluetooth.getState(); + switch(sta){ + case 0: + console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta)); + bluetooth.enableBluetooth(); + await sleep(3000); + break; + case 1: + console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta)); + await sleep(3000); + break; + case 2: + console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta)); + break; + case 3: + console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta)); + bluetooth.enableBluetooth(); + await sleep(3000); + break; + default: + console.info('[bluetooth_js] enable success'); + } + } + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_ENABLE_0001 + * @tc.name testEnableBluetooth + * @tc.desc Test EnableBluetooth api by promise. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_ENABLE_0001', 0, async function (done) { + console.info('[bluetooth_js] enable start'); + bluetooth.on("stateChange", onReceiveEvent); + function onReceiveEvent(data) { + if (data == bluetooth.BluetoothState.STATE_ON) { + console.info('enable bluetooth'); + bluetooth.off('stateChange', result => { + expect(true).assertEqual(result ==null); + done(); + }); + } + } + await tryToEnableBt(); + let state = bluetooth.getState(); + expect(state).assertEqual(2); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLUETOOTH_DISCOVERY_0001 + * @tc.name testClassicStartBluetoothDiscovery + * @tc.desc Test ClassicStartBluetoothDiscovery api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLUETOOTH_DISCOVERY_0001', 0, async function (done) { + console.info('[bluetooth_js] discovery1 start'); + await sleep(1000); + await tryToEnableBt(); + bluetooth.on("bluetoothDeviceFind", onReceiveEvent) + function onReceiveEvent(data) { + console.info('[bluetooth_js] Device' + JSON.stringify(data)+ 'length' + data.length) + expect(true).assertTrue(data.length >= 0); + bluetooth.off('bluetoothDeviceFind', result => { + console.info("[bluetooth_js] bluetoothDeviceFind off1:" + JSON.stringify(result)); + expect(true).assertEqual(true); + done(); + }) + } + bluetooth.startBluetoothDiscovery(); + }) + + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0001 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0001', 0, async function (done) { + console.info('[bluetooth_js] BLE scan1 test start'); + await sleep(1000); + await tryToEnableBt(); + function onReceiveEvent(data) + { + console.info('[bluetooth_js] BLE scan device find result1 = '+JSON.stringify(data)); + except(true).assertTrue(data.length>0); + } + bluetooth.BLE.on("BLEDeviceFind") + bluetooth.BLE.startBLEScan([{}]); + await sleep(1000); + console.info('[bluetooth_js] BLE SCAN OFF01'); + bluetooth.BLE.off('BLEDeviceFind'); + bluetooth.BLE.stopBLEScan(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0002 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0002', 0, async function (done) { + console.info('[bluetooth_js] BLE scan2 test start'); + await sleep(1000); + await tryToEnableBt(); + function onReceiveEvent(data) + { + console.info('[bluetooth_js] BLE scan device find result2 = '+JSON.stringify(data )); + except(true).assertTrue(data.length>0); + } + bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) + bluetooth.BLE.startBLEScan([{deviceId:"00:11:22:33:44:55"}]); + await sleep(1000); + console.info('[bluetooth_js] BLE scan off2'); + bluetooth.BLE.off('BLEDeviceFind'); + bluetooth.BLE.stopBLEScan(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0003 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0003', 0, async function (done) { + console.info('[bluetooth_js] BLE scan3 test start'); + await sleep(1000); + await tryToEnableBt(); + function onReceiveEvent(data) + { + console.info('[bluetooth_js] BLE scan device find result3 = '+JSON.stringify(data)); + except(true).assertTrue(data.length>0); + } + bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) + bluetooth.BLE.startBLEScan([{name:"blue_test"}]); + await sleep(1000); + console.info('[bluetooth_js] BLE scan off3'); + bluetooth.BLE.off('BLEDeviceFind'); + bluetooth.BLE.stopBLEScan(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0004 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0004', 0, async function (done) { + console.info('[bluetooth_js] BLE scan4 test start'); + await sleep(1000); + await tryToEnableBt(); + function onReceiveEvent(data) { + console.info('[bluetooth_js] BLE scan device find result4 = '+ JSON.stringify(data)); + expect(true).assertTrue(data.length >= 0);} + bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) + bluetooth.BLE.startBLEScan([{serviceUuid:"00001888-0000-1000-8000-00805f9b34fb"}]); + await sleep(1000); + console.info('[bluetooth_js] BLE scan device find off4:'); + bluetooth.BLE.off('BLEDeviceFind'); + bluetooth.BLE.stopBLEScan(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0005 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0005', 0, async function (done) { + console.info('[bluetooth_js] BLE scan5 test start'); + await sleep(1000); + await tryToEnableBt(); + function onReceiveEvent(data) + { + console.info('[bluetooth_js] BLE scan device find result5 = '+ JSON.stringify(data)); + expect(true).assertTrue(data.length > 0); + } + bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) + bluetooth.BLE.startBLEScan( + [{}], + { + interval: 500, + dutyMode: ScanDuty.SCAN_MODE_LOW_POWER, + matchMode: MatchMode.MATCH_MODE_AGGRESSIVE, + } + ); + await sleep(1000); + bluetooth.BLE.off("BLEDeviceFind"); + bluetooth.BLE.stopBLEScan(); + done(); + }) + + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0006 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0006', 0, async function (done) { + console.info('[bluetooth_js] BLE scan6 test start'); + await sleep(1000); + await tryToEnableBt(); + function onReceiveEvent(data) + { + consle.info('[bluetooth_js] BLE scan device find result6 = '+ JSON.stringify(data)); + expect(true).assertTrue(data.length > 0); + } + bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) + bluetooth.BLE.startBLEScan( + [{}], + { + interval: 500, + dutyMode: ScanDuty.SCAN_MODE_BALANCED, + matchMode: MatchMode.MATCH_MODE_AGGRESSIVE, + } + ); + await sleep(1000); + bluetooth.BLE.off('BLEDeviceFind'); + bluetooth.BLE.stopBLEScan(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0007 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0007', 0, async function (done) { + console.info('[bluetooth_js] BLE scan7 test start'); + await sleep(1000); + await tryToEnableBt(); + function onReceiveEvent(data) { + console.info('[bluetooth_js] BLE scan device find result7 = '+ JSON.stringify(data)); + expect(true).assertTrue(data.length > 0); + } + bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent) + bluetooth.BLE.startBLEScan( + [{}], + { + interval: 500, + dutyMode: ScanDuty.SCAN_MODE_LOW_LATENCY, + matchMode: MatchMode.MATCH_MODE_AGGRESSIVE, + } + ); + await sleep(1000); + bluetooth.BLE.off('BLEDeviceFind'); + await sleep(1000); + bluetooth.BLE.stopBLEScan(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0008 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0008', 0, async function (done) { + console.info('[bluetooth_js] BLE scan8 test start'); + await sleep(1000); + await tryToEnableBt(); + function onReceiveEvent(data) + { + console.info('[bluetooth_js] BLE scan device find result8 = '+ JSON.stringify(data)); + expect(true).assertTrue(data.length >= 0); + } + bluetooth.BLE.on("BLEDeviceFind",onReceiveEvent) + bluetooth.BLE.startBLEScan( + [{}], + { + interval: 0, + dutyMode: ScanDuty.SCAN_MODE_LOW_POWER, + matchMode: MatchMode.MATCH_MODE_STICKY, + } + ); + await sleep(1000); + bluetooth.BLE.off('BLEDeviceFind'); + await sleep(1000); + bluetooth.BLE.stopBLEScan(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0009 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0009', 0, async function (done) { + console.info('[bluetooth_js] BLE scan9 test start'); + await sleep(2000); + await tryToEnableBt(); + let disable = bluetooth.disableBluetooth(); + console.info('[bluetooth_js] stopScan disable:' + JSON.stringify(disable)); + expect(disable).assertTrue(); + await sleep(3100); + let result1 = bluetooth.BLE.stopBLEScan(); + console.info("[bluetooth_js] onStopBLEScan9 -> " + JSON.stringify(result1)); + expect(result1).assertNull(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0010 + * @tc.name testClassicStartBLEScan + * @tc.desc Test ClassicStartBLEScan api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_START_BLESCAN_WITHOUT_PARAM_0010', 0, async function (done) { + console.info('[bluetooth_js] BLE scan10 test start'); + await sleep(1000); + await tryToEnableBt(); + let result1 = bluetooth.BLE.stopBLEScan(); + console.info("[bluetooth_js] onStopBLEScan9 -> " + JSON.stringify(result1)); + expect(result1).assertNull(); + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_PAIR_DEVICE_0001 + * @tc.name testClassicPairDevice + * @tc.desc Test ClassicPairDevice api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_PAIR_DEVICE_0001', 0, async function (done) { + console.info('[bluetooth_js] pair device start'); + await tryToEnableBt(); + bluetooth.BLE.on('pinRequired', result => { + console.info("[bluetooth_js] pinRequired on:" + JSON.stringify(result)); + bluetooth.setDevicePairingConfirmation(result,false); + expect(true).assertEqual(result !=null); + done(); + }); + let enable3 = bluetooth.pairDevice("00:00:00:00:00:00") + bluetooth.BLE.off('pinRequired', result => { + console.info("[bluetooth_js] pinRequired off:" + JSON.stringify(result)); + expect(true).assertEqual(true); + done(); + }); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_PAIR_DEVICE_0002 + * @tc.name testClassicPairDevice + * @tc.desc Test ClassicPairDevice api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_PAIR_DEVICE_0002', 0, async function (done) { + console.info('[bluetooth_js] pair device start'); + await tryToEnableBt(); + await bluetooth.BLE.on('bondStateChange', result => { + console.info("[bluetooth_js] bondStateChange on:" + JSON.stringify(result) + +'bondStateChange deviceId:' + data.deviceId + 'bondStateChange state:' + data.state); + expect(true).assertEqual(result !=null); + done(); + }); + let BondState= + { + BOND_STATE_INVALID : 0, + BOND_STATE_BONDING : 1, + BOND_STATE_BONDED : 2 + }; + let enable4 = bluetooth.pairDevice("00:00:00:00:00:00") + expect(BondState.BOND_STATE_INVALID == 0).assertTrue(); + expect(BondState.BOND_STATE_BONDING == 1).assertTrue(); + expect(BondState.BOND_STATE_BONDED == 2).assertTrue(); + bluetooth.BLE.off('bondStateChange', result => { + expect(true).assertEqual(true); + done(); + }); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_SPP_LISTEN_0001 + * @tc.name testSppListen + * @tc.desc Test SppListen api by callback. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_SPP_LISTEN_0001', 0, async function (done) { + console.log("[bluetooth_js]: spp listen start"); + await tryToEnableBt(); + let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', + secure: false, type: 0}; + bluetooth.sppListen('server1', sppOption, function(code, serverSocketNumber) { + console.info('[bluetooth_js] code is: ' + code.code); + if (code.code == 0) { + expect(true).assertEqual(true); + done(); + } else { + expect(true).assertEqual(false); + done(); + } + }); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetoothble_CHARAC_READ_ON_0001 + * @tc.name testonCharacteristicReadOn + * @tc.desc Test CharacteristicReadOn api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetoothble_CHARAC_READ_ON_0001', 0, async function (done) { + try { + await tryToEnableBt(); + console.info('[bluetooth_js] CharacteristicReadOn test start'); + gattServer.on('characteristicRead', function (data) { + console.info('[bluetooth_js] CharRedReq deviceId: ' + data.deviceId + + 'transId:' + data.transId + 'offset:' + data.offset + 'charUuid:' + + data.characteristicUuid + 'serviceUuid:' + data.serviceUuid); + let serverResponse = { + "deviceId": data.deviceId, + "transId": data.transId, + "status": 0, + "offset": data.offset, + "value": str2ab("characteristic read response", data.offset), + }; + let result = gattServer.sendResponse(serverResponse); + expect(JSON.stringify(result)).assertContain("true"); + }); + }catch(e) { + expect(null).assertFail(); + } + try { + console.info('[bluetooth_js] characteristicRead test1 start'); + gattServer.off('characteristicRead', function (data) { + console.info("[bluetooth_js] charaRead off data:" + JSON.stringify(data)); + expect(true).assertEqual(true); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetoothble_CHARAC_WRITE_ON_0001 + * @tc.name testonCharacteristicwriteOn + * @tc.desc Test CharacteristicwriteOn api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetoothble_CHARAC_WRITE_ON_0001', 0, async function (done) { + try { + await tryToEnableBt(); + console.info('[bluetooth_js] CharacteristicwriteOn test start'); + gattServer.on('characteristicWrite', function (data) { + console.info('[bluetooth_js] CharWriReq deviceId: ' + data.deviceId + + 'transId:' + data.transId + 'offset:' + data.offset + 'isPrep:' + data.isPrep + + 'charUuid:' + data.characteristicUuid + 'serviceUuid:' + data.serviceUuid + + 'value:' + data.value + 'needRsp' + data.needRsp); + if (data.value instanceof ArrayBuffer) { + console.log(`[bluetooth_js] value: ${ab2hex(data.value)}`) + } + if (data.needRsp == false) { + return; + } + console.log(`data.value is ArraryBuffer: ${ab2hex(data.value)}`) + let serverResponse = { + "deviceId": data.deviceId, + "transId": data.transId, + "status": 0, + "offset": data.offset, + "value": data.value, + }; + let result = gattServer.sendResponse(serverResponse); + expect(JSON.stringify(result)).assertContain("true"); + }); + }catch(e) { + expect(null).assertFail(); + } + try { + console.info('[bluetooth_js] characteristicWrite test1 start'); + gattServer.off('characteristicWrite', function (data) { + console.info("[bluetooth_js] charaWrite off data2:" + JSON.stringify(data)); + expect(true).assertEqual(true); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_DESC_READ_ON_0001 + * @tc.name testDescriptorReadOn + * @tc.desc Test DescriptorReadOn api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_DESC_READ_ON_0001', 0, async function (done) { + try { + await tryToEnableBt(); + console.info('[bluetooth_js] descriptorReadOn test start ...'); + gattServer.on('descriptorRead', function (data) { + console.info("[bluetooth_js] DesRedon jsondata:" + JSON.stringify(data) + + 'deviceId:' + data.deviceId + 'transId:' + data.transId + 'offset:' + + data.offset +'descriptorUuid:' + data.descriptorUuid + 'characteristicUuid:' + + data.characteristicUuid + 'serviceUuid:' + data.serviceUuid); + expect(true).assertEqual(data !=null); + }); + }catch(e) { + expect(null).assertFail(); + } + try { + console.info('[bluetooth_js] descriptorReadOff test start ...'); + gattServer.off('descriptorRead', function (data) { + expect(true).assertEqual(true); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_DESC_WRITE_ON_0001 + * @tc.name testDescriptorWriteOn + * @tc.desc Test DescriptorWriteOn api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_DESC_WRITE_ON_0001', 0, async function (done) { + try { + await tryToEnableBt(); + console.info('[bluetooth_js] descriptorWriteOn test start ...'); + gattServer.on('descriptorWrite', function (data) { + console.info("[bluetooth_js] desWriOn jsondata: " + JSON.stringify(data) + + 'deviceId: ' + data.deviceId + 'transId:' + data.transId + 'offset:' + + data.offset +'descriptorUuid:' + data.descriptorUuid + + 'charUuid:' + data.characteristicUuid +'serviceUuid:' + data.serviceUuid + + 'value:' + data.value + 'needRsp' + data.needRsp + 'isPrep:' + data.isPrep ); + expect(true).assertEqual(data !=null); + }); + }catch(e) { + expect(null).assertFail(); + } + try { + console.info('[bluetooth_js] descriptorWriteOff test start ...'); + gattServer.off('descriptorWrite', function (data) { + expect(true).assertTrue(); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_CONNE_STATE_CHANGE_ON_0001 + * @tc.name testConnectStateChangeOn + * @tc.desc Test ConnectStateChangeOn api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_CONNE_STATE_CHANGE_ON_0001', 0, async function (done) { + try { + await tryToEnableBt(); + console.info('[bluetooth_js] ConnectStateChangeOn test start ...'); + gattServer.on('connectStateChange', function (data) { + console.info("[bluetooth_js] connectStaOn jsonData -> " + JSON.stringify(data) + + 'deviceId: ' + data.deviceId + 'state:'+ data.state); + expect(true).assertEqual(data !=null); + }); + }catch(e) { + expect(null).assertFail(); + } + try { + console.info('[bluetooth_js] ConnectStateChangeOff test start ...'); + gattServer.off('connectStateChange', function (data) { + console.info("[bluetooth_js] connectStateChange_off Data:" + JSON.stringify(data)); + expect(true).assertTrue(); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_BLE_CHAR_CHANGE_ON_0001 + * @tc.name testBLECharacteristicChangeOn + * @tc.desc Test BLECharacteristicChangeOn api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_BLE_CHAR_CHANGE_ON_0001', 0, async function (done) { + try { + await tryToEnableBt(); + console.info('[bluetooth_js] BLECharacteristicChangeOn test start ...'); + gattClient.on('BLECharacteristicChange', function (data) { + console.info("[bluetooth_js] BLECharacteristicChange data " + JSON.stringify(data)); + expect(true).assertEqual(data !=null); + }); + }catch(e) { + expect(null).assertFail(); + } + try { + console.info('[bluetooth_js] BLECharacteristicChangeOff test start'); + gattClient.off('BLECharacteristicChange', function (data) { + console.info("[bluetooth_js] BLECharcChange_off data-> " + JSON.stringify(data)); + expect(true).assertTrue(); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetooth_BLE_CONNE_STATE_CHANGE_ON_0001 + * @tc.name testBLEConnectionStateChangeOn + * @tc.desc Test BLEConnectionStateChangeOn api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetooth_BLE_CONNE_STATE_CHANGE_ON_0001', 0, async function (done) { + try { + await tryToEnableBt(); + console.info('[bluetooth_js] BLEConnectionStateChangeOn test start'); + gattClient.on('BLEConnectionStateChange', function (data) { + console.info("[bluetooth_js] BLEConnecStateChange_on data " + JSON.stringify(data) + +'deviceId: ' + data.deviceId + 'state:'+ data.state); + expect(true).assertEqual(data !=null); + }); + }catch(e) { + expect(null).assertFail(); + } + try { + console.info('[bluetooth_js] BLEConnectionStateChangeOff test start'); + gattClient.off('BLEConnectionStateChange', function (data) { + console.info("[bluetooth_js] BLEConneStateChange_off data-> " + JSON.stringify(data)); + expect(true).assertEqual(true); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + + + /** + * @tc.number SUB_COMMUNACATION_bluetoothble_SPP_READ_ON_0001 + * @tc.name testonsppReadOn + * @tc.desc Test sppReadOn api . + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_COMMUNACATION_bluetoothble_SPP_READ_ON_0001', 0, async function (done) { + try { + await tryToEnableBt(); + console.info('[bluetooth_js] sppReadOn test start'); + bluetooth.on("sppRead",-1, (result) => { + console.info("[bluetooth_js] sppReadOn json_result -> " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + }); + }catch(e) { + expect(null).assertFail(); + } + try { + console.info('[bluetooth_js] sppReadOff test start ...'); + bluetooth.off("sppRead",-1, (result) => { + console.info("[bluetooth_js] sppReadOff json_result -> " + JSON.stringify(result)); + expect(true).assertEqual(true); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + +}) + +} diff --git a/communication/bluetooth_ble/src/main/js/default/test/List.test.js b/communication/bluetooth_on/src/main/js/test/List.test.js similarity index 82% rename from communication/bluetooth_ble/src/main/js/default/test/List.test.js rename to communication/bluetooth_on/src/main/js/test/List.test.js index ce59fc6b44896f4684f01fbbf671845c7b721be3..45a3fe3fb9652159852a6f98df56c016b6775d65 100644 --- a/communication/bluetooth_ble/src/main/js/default/test/List.test.js +++ b/communication/bluetooth_on/src/main/js/test/List.test.js @@ -13,4 +13,7 @@ * limitations under the License. */ -require('./BluetoothBle.test.js') +import bluetoothhostTest from './BluetoothOn.test.js' +export default function testsuite() { +bluetoothhostTest() +} diff --git a/communication/bluetooth_on/src/main/resources/base/element/string.json b/communication/bluetooth_on/src/main/resources/base/element/string.json index 14f9c3a9210f78b12caf3b945dc2b2c0f63f7d3d..d4b2a0a059d21198f9767cea679a5cc2bb8f6dad 100644 --- a/communication/bluetooth_on/src/main/resources/base/element/string.json +++ b/communication/bluetooth_on/src/main/resources/base/element/string.json @@ -7,6 +7,22 @@ { "name": "mainability_description", "value": "JS_Phone_Empty Feature Ability" - } + }, + { + "name": "MainAbility_desc", + "value": "description" + }, + { + "name": "MainAbility_label", + "value": "label" + }, + { + "name": "TestAbility_desc", + "value": "description" + }, + { + "name": "TestAbility_label", + "value": "label" + } ] } \ No newline at end of file