diff --git a/telephony/BUILD.gn b/telephony/BUILD.gn index 4bc054cc32dbc0b8102f71097476bb1353f0cc8a..f81255a935b6ad8fdec22d636958dafe100077d2 100644 --- a/telephony/BUILD.gn +++ b/telephony/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2021 Huawei Device Co., Ltd. +# Copyright (C) 2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -14,6 +14,7 @@ group("telephony") { testonly = true deps = [ + "crossplatform:crossplatform", "crossplatform/telephony_http_test:ActsCrossplatformTelephonyHttpEtsTest", "telephonyjstest:telephonyjstest", ] diff --git a/telephony/crossplatform/BUILD.gn b/telephony/crossplatform/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ee0755c0b64c147f085bd3797afcc643a1abe16d --- /dev/null +++ b/telephony/crossplatform/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//test/xts/tools/build/suite.gni") + +group("crossplatform") { + testonly = true + if (is_standard_system) { + deps = [ + "telephonyConnectionetstest:ActsCrossplatformConnectionTest", + "telephonyWebSocketetstest:ActsCrossplatformWebSocketTest", + ] + } +} diff --git a/telephony/crossplatform/telephonyConnectionetstest/AppScope/app.json b/telephony/crossplatform/telephonyConnectionetstest/AppScope/app.json new file mode 100644 index 0000000000000000000000000000000000000000..8d2563afef305b950bcc5e365a46ae38bbb573bc --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/AppScope/app.json @@ -0,0 +1,16 @@ +{ + "app": { + "bundleName": "ohos.acts.telephony.crossplatform.Connection.normal", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name", + "minAPIVersion": 10, + "targetAPIVersion": 10, + "car": { + "apiCompatibleVersion": 10, + "singleUser": false + } + } +} diff --git a/telephony/crossplatform/telephonyConnectionetstest/AppScope/resources/base/element/string.json b/telephony/crossplatform/telephonyConnectionetstest/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3811bc441fc0f128eb4d14c53ed463ffb8dc3811 --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "xtsDemo1" + } + ] +} diff --git a/telephony/crossplatform/telephonyConnectionetstest/AppScope/resources/base/media/app_icon.png b/telephony/crossplatform/telephonyConnectionetstest/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/telephony/crossplatform/telephonyConnectionetstest/AppScope/resources/base/media/app_icon.png differ diff --git a/telephony/crossplatform/telephonyConnectionetstest/BUILD.gn b/telephony/crossplatform/telephonyConnectionetstest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..6390bc9a97218219610b8e63fd2f2fdffa5536b3 --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//test/xts/tools/build/suite.gni") + +ohos_js_hap_suite("ActsCrossplatformConnectionTest") { + hap_profile = "src/main/module.json" + deps = [ + ":windowStage_js_assets", + ":windowStage_resources", + ] + ets2abc = true + certificate_profile = "./signature/openharmony_sx.p7b" + + # hap_name: HAP的名字,可选,默认为目标名 + hap_name = "ActsCrossplatformConnectionTest" + subsystem_name = "communication" + part_name = "netstack" +} +ohos_app_scope("windowStage_app_profile") { + # app_profile: HAP的AppScope中的app.json,只在Stage模型下使用 + app_profile = "AppScope/app.json" + + # sources: 资源文件路径 + sources = [ "AppScope/resources" ] +} +ohos_js_assets("windowStage_js_assets") { + # source_dir: js或ets代码路径,兼容FA模型的单ability配置到ability目录 + source_dir = "src/main/ets" +} +ohos_resources("windowStage_resources") { + # sources: 资源文件路径 + sources = [ "src/main/resources" ] + + # deps: 当前目标的依赖 + deps = [ ":windowStage_app_profile" ] + + # hap_profile: HAP的config.json,Stage模型对应module.json + hap_profile = "src/main/module.json" +} diff --git a/telephony/crossplatform/telephonyConnectionetstest/Test.json b/telephony/crossplatform/telephonyConnectionetstest/Test.json new file mode 100644 index 0000000000000000000000000000000000000000..d3e9816dc90dd3676163ec651c1e75aaf51cf836 --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/Test.json @@ -0,0 +1,27 @@ +{ + "description": "Configuration for hjunit demo Tests", + // 指定设备执行的驱动 + "driver": { + "type": "OHJSUnitTest", + //test-timeout: 测试hap 整体执行用例的执行时长 + "test-timeout": "60000", + //shell-timeout: aa test 拉起命令执行时长 + "shell-timeout": "60000", + //bundle-name: 与HAP的app.json 中 bundleName 保持一致 + "bundle-name": "ohos.acts.telephony.crossplatform.Connection.normal", + //module-name: 与HAP的module.json 中"module"字段的 name 保持一致 + "module-name": "entry_test" + //testcase-timeout: 测试hap 单条用例的执行时长 + //"testcase-timeout": 5000 + }, + // kit的作用主要是为了支撑测试执行活动,在测试前执行预制操作(Setup),在测试后执行清理操作(Teardown) + "kits": [ + { + "test-file-name": [ + "ActsDemoStageTest.hap" + ], + "type": "AppInstallKit", + "cleanup-apps": true + } + ] +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyConnectionetstest/signature/openharmony_sx.p7b b/telephony/crossplatform/telephonyConnectionetstest/signature/openharmony_sx.p7b new file mode 100644 index 0000000000000000000000000000000000000000..dc89ce61ef5f75df79e5bbc7992581c2d59df372 Binary files /dev/null and b/telephony/crossplatform/telephonyConnectionetstest/signature/openharmony_sx.p7b differ diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestAbility/TestAbility.ets b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestAbility/TestAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..cb027d69256099790b95a32b446978bbc64b4ecb --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestAbility/TestAbility.ets @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import hilog from '@ohos.hilog'; +import { Hypium } from '@ohos/hypium'; +import testsuite from '../test/List.test'; +import window from '@ohos.window'; + +export default class TestAbility extends UIAbility { + onCreate(want, launchParam) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); + hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:'+ JSON.stringify(launchParam) ?? ''); + var abilityDelegator: any + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var abilityDelegatorArguments: any + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) + } + + onDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate'); + windowStage.loadContent('TestAbility/pages/Index', (err, data) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', + JSON.stringify(data) ?? ''); + }); + } + + onWindowStageDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy'); + } + + onForeground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground'); + } + + onBackground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground'); + } +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestAbility/pages/Index.ets b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestAbility/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8cbc4b3b96d8d38bc36fbd9a3173d3fd4f09db73 --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestAbility/pages/Index.ets @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import hilog from '@ohos.hilog'; + +@Entry +@Component +struct Index { + aboutToAppear() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear'); + } + @State message: string = 'Hello World' + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('next page') + .fontSize(20) + .fontWeight(FontWeight.Bold) + }.type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(()=>{ + }) + } + .width('100%') + } + .height('100%') + } + } \ No newline at end of file diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts new file mode 100644 index 0000000000000000000000000000000000000000..92a16d84e8870da219c51d9f1342c79203c1f42d --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts @@ -0,0 +1,49 @@ +import hilog from '@ohos.hilog'; +import TestRunner from '@ohos.application.testRunner'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; + +var abilityDelegator = undefined +var abilityDelegatorArguments = undefined + +async function onAbilityCreateCallback() { + hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); +} + +async function addAbilityMonitorCallback(err: any) { + hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); +} + +export default class OpenHarmonyTestRunner implements TestRunner { + constructor() { + } + + onPrepare() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare '); + } + + async onRun() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' + let lMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) + var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName + var debug = abilityDelegatorArguments.parameters['-D'] + if (debug == 'true') + { + cmd += ' -D' + } + hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd); + abilityDelegator.executeShellCommand(cmd, + (err: any, d: any) => { + hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? ''); + }) + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); + } +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/test/Connection.test.ets b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/test/Connection.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..63d36484359af5aac0c746d354c7c82c1f565979 --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/test/Connection.test.ets @@ -0,0 +1,307 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' +import connection from '@ohos.net.connection' + +export default function connectionTest() { + describe('connectionTest', function () { + const TIMEOUT = 100; + const DELAY = 1000; + const NETID_IVVALID = 99; + const NETID_IVVALID2 = 0; + let envReady = false; + + /* + * @tc.number: TEST_CREATE_NET_CONNECTION_001 + * @tc.name : testCreateNetConnection001 + * @tc.desc : Create a network connection with optional netSpecifier and timeout. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testCreateNetConnection001', 0, function (done) { + try { + connection.createNetConnection(); + console.log("create success11:" + connection.createNetConnection); + expect(true).assertTrue(); + done(); + } catch (error) { + expect().assertFail(); + done(); + } + }); + + /* + * @tc.number: TEST_HAS_DEFAULT_NET_001 + * @tc.name : testHasDefaultNet001 + * @tc.desc : Checks whether the default data network is activated. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testHasDefaultNet001', 0, function (done) { + console.log('testHasDefaultNet001 1'); + connection.hasDefaultNet((error, value) => { + console.log('testHasDefaultNet001 2'); + if (!error) { + console.log('testHasDefaultNet001 3'); + console.log(`testHasDefaultNet001 case success,data:` + value); + console.log('testHasDefaultNet001 4'); + expect(value).assertTrue(); + done(); + } else { + console.log('testHasDefaultNet001 6'); + console.log(`testHasDefaultNet001 error, case failed,errCode:` + error.code); + console.log('testHasDefaultNet001 7'); + expect().assertFail(); + done(); + } + }); + console.log('testHasDefaultNet001 9'); + }); + + /* + * @tc.number: TEST_HAS_DEFAULT_NET_002 + * @tc.name : testHasDefaultNet002 + * @tc.desc : Checks whether the default data network is activated. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testHasDefaultNet002', 0, function (done) { + connection.hasDefaultNet().then(value => { + console.log(`case success,data:` + value); + expect(value).assertTrue(); + done(); + }).catch((error) => { + console.log(`error, case failed,errCode:` + error.code); + expect().assertFail(); + done(); + }); + }); + + /** + * @tc.number: TEST_REGISTER_001 + * @tc.name : testRegister001 + * @tc.desc : Receives status change notifications of a specified network. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testRegister001', 0, function (done) { + let netConn = connection.createNetConnection(); + netConn.register((error) => { + if (!error) { + console.log(`register success11`); + netConn.unregister((error) => { + expect(true).assertTrue(); + done(); + }); + } else { + console.log(`register failed,errCode:` + error.code); + expect().assertFail(); + done(); + } + }); + }); + + /** + * @tc.number: TEST_UNREGISTER_001 + * @tc.name : testUnregister001 + * @tc.desc : Cancels listening for network status changes. test + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testUnregister001', 0, function (done) { + let netConn = connection.createNetConnection(); + netConn.register((error) => { + if (!error) { + console.log(`register success11`); + netConn.unregister((error) => { + if (!error) { + console.log(`unregister success11`); + expect(true).assertTrue(); + done(); + } else { + console.log(`register failed,errCode:` + error.code); + expect().assertFail(); + done(); + } + }); + } + }); + }); + + /** + * @tc.number: TEST_ON_NET_AVAILABLE_001 + * @tc.name : testOn_NetAvailable001 + * @tc.desc : Subscribe to network available events. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOn_NetAvailable001', 0, function (done) { + try { + let netConn = connection.createNetConnection(); + netConn.register((error) => { + if (!error) { + console.log("register success11:" + error); + netConn.on("netAvailable",(data) => { + console.log("netAvailable success11:" + data); + }); + netConn.unregister((error) => { + console.log("unregister success11:" + error); + }); + expect(true).assertTrue(); + done(); + } + }); + } catch (err) { + console.log(`register failed,errCode:` + err); + expect().assertFail(); + done(); + } + }); + + /** + * @tc.number: TEST_ON_NET_CAPABILITIES_CHANGE_001 + * @tc.name : testOn_NetCapabilitiesChange001 + * @tc.desc : Subscribe to network capability change events. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOn_NetCapabilitiesChange001', 0, function (done) { + let netConn = connection.createNetConnection(); + netConn.register((error) => { + if (!error) { + netConn.on('netCapabilitiesChange',(data) => { + console.log("success11:" + data); + expect(data.netCap.bearerTypes.length >= 0).assertTrue(); + }); + netConn.unregister((error) => { + console.log("result11:" + error); + }); + expect(true).assertTrue(); + done(); + } else { + console.log(`register failed,errCode:` + error.code); + expect().assertFail(); + done(); + } + }); + }); + + /** + * @tc.number: TEST_ON_NET_LOST_001 + * @tc.name : testOn_NetLost001 + * @tc.desc : Subscribe to network loss events. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOn_NetLost001', 0, function (done) { + let netConn = connection.createNetConnection(); + netConn.register((error) => { + if (!error) { + netConn.on('netLost',(data) => { + console.log("testOn_NetLost001 success11:" + data.netId); + }); + netConn.unregister((error) => { + console.log("testOn_NetLost001 result11:" + error); + }); + expect(true).assertTrue(); + done(); + } else { + console.log(`testOn_NetLost001 register failed,errCode:` + error.code); + expect().assertFail(); + done(); + } + }); + }); + + /** + * @tc.number: TEST_ON_NET_UNAVAILABLE_001 + * @tc.name : testOn_NetUnavailable001 + * @tc.desc : Subscribe to network loss events. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOn_NetUnavailable001', 0, function (done) { + let netConn = connection.createNetConnection(); + netConn.register((error) => { + if (!error) { + netConn.on('netUnavailable',(data) => { + console.log("on success11:" + data); + }); + netConn.unregister((error) => { + console.log("result11:" + error); + }); + expect(true).assertTrue(); + done(); + } else { + console.log(`register failed,errCode:` + error.code); + expect().assertFail(); + done(); + } + }); + }); + + /* + * @tc.number: TEST_NET_ID_001 + * @tc.name : testNetId001 + * @tc.desc : netId test. + * @tc.size : MediumTest + * @tc.type : Field + * @tc.level : Level 1 + */ + it('testNetId001', 0, function (done) { + let netConn = connection.createNetConnection(); + netConn.register((error) => { + if (!error) { + console.log(`register success11`); + netConn.on("netAvailable",(data) => { + console.log(`netId success11 data:` +data.netId); + expect(data.netId >= 100).assertTrue(); + }); + done(); + } else { + console.log(`register failed,errCode:` + error.code); + expect().assertFail(); + done(); + } + }); + }); + + /** + * @tc.number: TEST_NET_BEAR_TYPE_001 + * @tc.name : testNetBearType001 + * @tc.desc : NetBearType test + * @tc.size : MediumTest + * @tc.type : Enum + * @tc.level : Level 0 + */ + it('testNetBearType001', 0, function (done) { + let nbt_0 = connection.NetBearType.BEARER_CELLULAR; + let nbt_1 = connection.NetBearType.BEARER_WIFI; + expect(nbt_0).assertEqual(0); + expect(nbt_1).assertEqual(1); + done(); + }); + + }) +} diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/test/List.test.ets b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fb4af5e2c19335e6edcd4192881fc4386802a53 --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/ets/test/List.test.ets @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import connectionTest from './Connection.test' + +export default function testsuite() { + connectionTest() +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/module.json b/telephony/crossplatform/telephonyConnectionetstest/src/main/module.json new file mode 100644 index 0000000000000000000000000000000000000000..8e5df3d2cdabecbd3645bb7396822032a31f690c --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/module.json @@ -0,0 +1,44 @@ +{ + "module": { + "name": "entry_test", + "type": "feature", + "description": "$string:module_test_desc", + "mainElement": "TestAbility", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:test_pages", + "abilities": [ + { + "name": "TestAbility", + "srcEntry": "./ets/TestAbility/TestAbility.ets", + "description": "$string:TestAbility_desc", + "icon": "$media:icon", + "label": "$string:TestAbility_label", + "exported": true, + "startWindowIcon": "$media:icon", + "startWindowBackground": "$color:start_window_background", + "skills": [ + { + "actions": [ + "action.system.home" + ], + "entities": [ + "entity.system.home" + ] + } + ] + } + ], + "requestPermissions": [ + { + "name": "ohos.permission.INTERNET" + }, + { + "name": "ohos.permission.GET_NETWORK_INFO" + } + ] + } +} diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/element/color.json b/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/element/string.json b/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..65d8fa5a7cf54aa3943dcd0214f58d1771bc1f6c --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_test_desc", + "value": "test ability description" + }, + { + "name": "TestAbility_desc", + "value": "the test ability" + }, + { + "name": "TestAbility_label", + "value": "test label" + } + ] +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/media/icon.png b/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/media/icon.png differ diff --git a/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/profile/test_pages.json b/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/profile/test_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..77e90731b5a38d861663029b483df3d3ac9ec74b --- /dev/null +++ b/telephony/crossplatform/telephonyConnectionetstest/src/main/resources/base/profile/test_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "TestAbility/pages/Index" + ] +} diff --git a/telephony/crossplatform/telephonyWebSocketetstest/AppScope/app.json b/telephony/crossplatform/telephonyWebSocketetstest/AppScope/app.json new file mode 100644 index 0000000000000000000000000000000000000000..d7929e68d3c962db6fb672b3846efee21b47c7c8 --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/AppScope/app.json @@ -0,0 +1,16 @@ +{ + "app": { + "bundleName": "ohos.acts.telephony.crossplatform.WebSocket.normal", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name", + "minAPIVersion": 10, + "targetAPIVersion": 10, + "car": { + "apiCompatibleVersion": 10, + "singleUser": false + } + } +} diff --git a/telephony/crossplatform/telephonyWebSocketetstest/AppScope/resources/base/element/string.json b/telephony/crossplatform/telephonyWebSocketetstest/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3811bc441fc0f128eb4d14c53ed463ffb8dc3811 --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "xtsDemo1" + } + ] +} diff --git a/telephony/crossplatform/telephonyWebSocketetstest/AppScope/resources/base/media/app_icon.png b/telephony/crossplatform/telephonyWebSocketetstest/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/telephony/crossplatform/telephonyWebSocketetstest/AppScope/resources/base/media/app_icon.png differ diff --git a/telephony/crossplatform/telephonyWebSocketetstest/BUILD.gn b/telephony/crossplatform/telephonyWebSocketetstest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8eccd552b9db69ad6b2765effc335f6805101995 --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//test/xts/tools/build/suite.gni") + +ohos_js_hap_suite("ActsCrossplatformWebSocketTest") { + hap_profile = "src/main/module.json" + deps = [ + ":windowStage_js_assets", + ":windowStage_resources", + ] + ets2abc = true + certificate_profile = "./signature/openharmony_sx.p7b" + + # hap_name: HAP的名字,可选,默认为目标名 + hap_name = "ActsCrossplatformWebSocketTest" + subsystem_name = "communication" + part_name = "netstack" +} +ohos_app_scope("windowStage_app_profile") { + # app_profile: HAP的AppScope中的app.json,只在Stage模型下使用 + app_profile = "AppScope/app.json" + + # sources: 资源文件路径 + sources = [ "AppScope/resources" ] +} +ohos_js_assets("windowStage_js_assets") { + # source_dir: js或ets代码路径,兼容FA模型的单ability配置到ability目录 + source_dir = "src/main/ets" +} +ohos_resources("windowStage_resources") { + # sources: 资源文件路径 + sources = [ "src/main/resources" ] + + # deps: 当前目标的依赖 + deps = [ ":windowStage_app_profile" ] + + # hap_profile: HAP的config.json,Stage模型对应module.json + hap_profile = "src/main/module.json" +} diff --git a/telephony/crossplatform/telephonyWebSocketetstest/Test.json b/telephony/crossplatform/telephonyWebSocketetstest/Test.json new file mode 100644 index 0000000000000000000000000000000000000000..2542f970ed030638554d077897e5b7d733e7f234 --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/Test.json @@ -0,0 +1,27 @@ +{ + "description": "Configuration for hjunit demo Tests", + // 指定设备执行的驱动 + "driver": { + "type": "OHJSUnitTest", + //test-timeout: 测试hap 整体执行用例的执行时长 + "test-timeout": "60000", + //shell-timeout: aa test 拉起命令执行时长 + "shell-timeout": "60000", + //bundle-name: 与HAP的app.json 中 bundleName 保持一致 + "bundle-name": "ohos.acts.telephony.crossplatform.WebSocket.normal", + //module-name: 与HAP的module.json 中"module"字段的 name 保持一致 + "module-name": "entry_test" + //testcase-timeout: 测试hap 单条用例的执行时长 + //"testcase-timeout": 5000 + }, + // kit的作用主要是为了支撑测试执行活动,在测试前执行预制操作(Setup),在测试后执行清理操作(Teardown) + "kits": [ + { + "test-file-name": [ + "ActsDemoStageTest.hap" + ], + "type": "AppInstallKit", + "cleanup-apps": true + } + ] +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyWebSocketetstest/signature/openharmony_sx.p7b b/telephony/crossplatform/telephonyWebSocketetstest/signature/openharmony_sx.p7b new file mode 100644 index 0000000000000000000000000000000000000000..27e8339b624cd3c623227b801a9977510e03f0fa Binary files /dev/null and b/telephony/crossplatform/telephonyWebSocketetstest/signature/openharmony_sx.p7b differ diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestAbility/TestAbility.ets b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestAbility/TestAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..cb027d69256099790b95a32b446978bbc64b4ecb --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestAbility/TestAbility.ets @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import hilog from '@ohos.hilog'; +import { Hypium } from '@ohos/hypium'; +import testsuite from '../test/List.test'; +import window from '@ohos.window'; + +export default class TestAbility extends UIAbility { + onCreate(want, launchParam) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); + hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:'+ JSON.stringify(launchParam) ?? ''); + var abilityDelegator: any + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var abilityDelegatorArguments: any + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) + } + + onDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate'); + windowStage.loadContent('TestAbility/pages/Index', (err, data) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', + JSON.stringify(data) ?? ''); + }); + } + + onWindowStageDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy'); + } + + onForeground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground'); + } + + onBackground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground'); + } +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestAbility/pages/Index.ets b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestAbility/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8cbc4b3b96d8d38bc36fbd9a3173d3fd4f09db73 --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestAbility/pages/Index.ets @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import hilog from '@ohos.hilog'; + +@Entry +@Component +struct Index { + aboutToAppear() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear'); + } + @State message: string = 'Hello World' + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('next page') + .fontSize(20) + .fontWeight(FontWeight.Bold) + }.type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(()=>{ + }) + } + .width('100%') + } + .height('100%') + } + } \ No newline at end of file diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts new file mode 100644 index 0000000000000000000000000000000000000000..92a16d84e8870da219c51d9f1342c79203c1f42d --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts @@ -0,0 +1,49 @@ +import hilog from '@ohos.hilog'; +import TestRunner from '@ohos.application.testRunner'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; + +var abilityDelegator = undefined +var abilityDelegatorArguments = undefined + +async function onAbilityCreateCallback() { + hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); +} + +async function addAbilityMonitorCallback(err: any) { + hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); +} + +export default class OpenHarmonyTestRunner implements TestRunner { + constructor() { + } + + onPrepare() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare '); + } + + async onRun() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' + let lMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) + var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName + var debug = abilityDelegatorArguments.parameters['-D'] + if (debug == 'true') + { + cmd += ' -D' + } + hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd); + abilityDelegator.executeShellCommand(cmd, + (err: any, d: any) => { + hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? ''); + }) + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); + } +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/test/List.test.ets b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..a791911ca2dc11c741ac25154dd64242a0978229 --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/test/List.test.ets @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import webSocketTest from './WebSocket.test' + +export default function testsuite() { + webSocketTest() +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/test/WebSocket.test.ets b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/test/WebSocket.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7ead9be7cc756dc8f0178359539c49ea354de5ed --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/ets/test/WebSocket.test.ets @@ -0,0 +1,496 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' +import webSocket from '@ohos.net.webSocket' + +export default function webSocketTest() { + describe('webSocketTest', function () { + console.log("************* webSocket Test start*************"); + + /* + * @tc.number: TEST_CREATE_WEB_SOCKET_001 + * @tc.name : testCreateWebSocket001 + * @tc.desc : Creates a web socket connection. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testCreateWebSocket001', 0, async function (done) { + var mWebSocket = webSocket.createWebSocket(); + expect(mWebSocket != null).assertTrue(); + mWebSocket.close((error, value) => { + if (!error) { + console.log(`case success:` + value); + expect(value).assertTrue(); + done(); + } else { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(2302999); + done(); + } + }); + done(); + }); + + /* + * @tc.number: TEST_WEB_SOCKET_REQUEST_OPTIONS_HEADER_001 + * @tc.name : testWebSocketRequestOptions_header001 + * @tc.desc : webSocketRequestOptions.header + * @tc.size : MediumTest + * @tc.type : Field + * @tc.level : Level 1 + */ + it('testWebSocketRequestOptions_header001', 0, async function (done) { + let webSocketRequestOptions: webSocket.WebSocketRequestOptions = { + header: "test header" + } + expect(webSocketRequestOptions.header === "test header").assertTrue(); + done(); + }); + + /* + * @tc.number: TEST_WEB_SOCKET_CLOSE_OPTIONS_CODE_001 + * @tc.name : testWebSocketCloseOptions_code001 + * @tc.desc : webSocketCloseOptions.code + * @tc.size : MediumTest + * @tc.type : Field + * @tc.level : Level 1 + */ + it('testWebSocketCloseOptions_code001', 0, async function (done) { + let webSocketCloseOptions: webSocket.WebSocketCloseOptions = { + code: 1, + reason: "test reason" + } + expect(webSocketCloseOptions.code === 1).assertTrue(); + done(); + }); + + /* + * @tc.number: TEST_WEB_SOCKET_CLOSE_OPTIONS_001 + * @tc.name : testWebSocketCloseOptions_Reason001 + * @tc.desc : webSocketCloseOptions.reason + * @tc.size : MediumTest + * @tc.type : Field + * @tc.level : Level 1 + */ + it('testWebSocketCloseOptions_Reason001', 0, async function (done) { + let webSocketCloseOptions: webSocket.WebSocketCloseOptions = { + code: 1, + reason: "test reason" + } + expect(webSocketCloseOptions.reason).assertEqual("test reason"); + done(); + }); + + /* + * @tc.number: TEST_CONNECT_001 + * @tc.name : testConnect001 + * @tc.desc : Initiates a WebSocket request to establish a WebSocket connection to a given URL. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testConnect001', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + mWebSocket.connect(null, (error, value) => { + if (!error) { + console.log(`case success11:` + value); + expect(value).assertTrue(); + done(); + } else { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(401); + done(); + } + }); + }); + + /* + * @tc.number: TEST_CONNECT_002 + * @tc.name : testConnect002 + * @tc.desc : Initiates a WebSocket request to establish a WebSocket connection to a given URL. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testConnect002', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + let webSocketRequestOptions: webSocket.WebSocketRequestOptions = { + header: { + key: "value", + key2: "value2" + } + } + console.info('webSocket: start11'); + mWebSocket.connect("ws://19211.16811.1.100:8081/string", webSocketRequestOptions, (error, value) => { + if (!error) { + console.log(`case success11:` + value); + expect(value).assertTrue(); + done(); + } else { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(-1); + done(); + } + }); + }); + + /* + * @tc.number: TEST_CONNECT_003 + * @tc.name : testConnect003 + * @tc.desc : Initiates a WebSocket request to establish a WebSocket connection to a given URL. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testConnect003', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + mWebSocket.connect("", (error, value) => { + if (!error) { + console.log(`case success11:` + value); + expect(value).assertTrue(); + done(); + } else { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(-1); + done(); + } + }); + }); + + /* + * @tc.number: TEST_CONNECT_004 + * @tc.name : testConnect004 + * @tc.desc : Initiates a WebSocket request to establish a WebSocket connection to a given URL. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testConnect004', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + mWebSocket.connect(null).then(value => { + console.log(`case success11:` + value); + expect(value).assertTrue(); + done(); + }).catch((error) => { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(401); + done(); + }); + }); + + /* + * @tc.number: TEST_CONNECT_005 + * @tc.name : testConnect005 + * @tc.desc : Initiates a WebSocket request to establish a WebSocket connection to a given URL. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testConnect005', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + let webSocketRequestOptions: webSocket.WebSocketRequestOptions = { + header: { + key: "value", + key2: "value2" + } + } + mWebSocket.connect("ws://19211.16811.1.100:8081/string", webSocketRequestOptions).then(value => { + console.log(`case success11:` + value); + expect(value).assertTrue(); + done(); + }).catch((error) => { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(-1); + done(); + }); + }); + + /* + * @tc.number: TEST_SEND_001 + * @tc.name : testSend001 + * @tc.desc : Sends data through a WebSocket connection. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testSend001', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + mWebSocket.send("test data", (error, value) => { + if (!error) { + console.log(`case success,data:` + value); + expect(value).assertTrue(); + done(); + } else { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(2302999); + done(); + } + }); + }); + + /* + * @tc.number: TEST_SEND_002 + * @tc.name : testSend002 + * @tc.desc : Sends data through a WebSocket connection. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testSend002', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + let promise = mWebSocket.send("test data"); + promise.then(value => { + console.log(`case success,data:` + value); + expect(value).assertTrue(); + done(); + }).catch((error) => { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(2302999); + done(); + }); + }); + + /* + * @tc.number: TEST_SEND_003 + * @tc.name : testSend003 + * @tc.desc : Sends data through a WebSocket connection. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testSend003', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + var str = "Hello, server!(arrayBuffer promise mode)"; + var buf = new ArrayBuffer(str.length); + var message = new Uint8Array(buf); + for (var i = 0, strLen = str.length; i < strLen; i++) { + message[i] = str.charCodeAt(i); + } + let promise = mWebSocket.send(message.buffer); + promise.then(value => { + console.log(`case success,data:` + value); + expect(value).assertTrue(); + done(); + }).catch((error) => { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(2302999); + done(); + }); + }); + + /* + * @tc.number: TEST_SEND_004 + * @tc.name : testSend004 + * @tc.desc : Sends data through a WebSocket connection. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testSend004', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + var str = "Hello, server!(arrayBuffer promise mode)"; + var buf = new ArrayBuffer(str.length); + var message = new Uint8Array(buf); + for (var i = 0, strLen = str.length; i < strLen; i++) { + message[i] = str.charCodeAt(i); + } + mWebSocket.send(message.buffer, (error, value) => { + if (!error) { + console.log(`case success:` + value); + expect(value).assertTrue(); + done(); + } else { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(2302999); + done(); + } + }); + }); + + /* + * @tc.number: TEST_CLOSE_001 + * @tc.name : testClose001 + * @tc.desc : Closes a WebSocket connection. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testClose001', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + mWebSocket.close((error, value) => { + if (!error) { + console.log(`case success:` + value); + expect(value).assertTrue(); + done(); + } else { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(2302999); + done(); + } + }); + }); + + /* + * @tc.number: TEST_CLOSE_002 + * @tc.name : testClose002 + * @tc.desc : Closes a WebSocket connection. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testClose002', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + let webSocketCloseOptions: webSocket.WebSocketCloseOptions = { + code: 1, + reason: "test reason" + } + mWebSocket.close(webSocketCloseOptions, (error, value) => { + if (!error) { + console.log(`case success,data:` + value); + expect(value).assertTrue(); + done(); + } else { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(2302999); + done(); + } + }); + }); + + /* + * @tc.number: TEST_CLOSE_003 + * @tc.name : testClose003 + * @tc.desc : Closes a WebSocket connection. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testClose003', 0, async function (done) { + let mWebSocket = webSocket.createWebSocket(); + let webSocketCloseOptions: webSocket.WebSocketCloseOptions = { + code: 1, + reason: "test reason" + } + mWebSocket.close(webSocketCloseOptions).then(value => { + console.log(`success11`); + expect(true).assertTrue(); + done(); + }).catch((error) => { + console.log(`error, case failed,errCode:` + error.code); + expect(error.code).assertEqual(2302999); + done(); + }); + }); + + /* + * @tc.number: TEST_ON_OPEN_001 + * @tc.name : testOn_Open001 + * @tc.desc : Subscribe to WebSocket open events, using callback as an asynchronous method. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOn_Open001', 0, async function (done) { + let ws = webSocket.createWebSocket(); + done(); + console.log("testOn_Open001: 0"); + ws.on('open', (err, value) => { + console.log("testOn_Open001: 1"); + console.log("on open, status:" + value['status'] + ", message:" + value['message']); + console.log("testOn_Open001: 2"); + expect(true).assertTrue(); + done(); + }); + ws.off('open'); + console.log("testOn_Open001: 3"); + }); + + /* + * @tc.number: TEST_OFF_OPEN_001 + * @tc.name : testOff_Open001 + * @tc.desc : Unsubscribe from WebSocket open events, using callback as an asynchronous method. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOff_Open001', 0, async function (done) { + let ws = webSocket.createWebSocket(); + done(); + let callback1 = (err, value) => { + console.log("on open, status:" + value['status'] + ", message:" + value['message']); + } + ws.on('open', callback1); + ws.off('open', callback1); + expect(true).assertTrue(); + }); + + /* + * @tc.number: TEST_ON_OFF_MESSAGE_001 + * @tc.name : testOn_Off_Message001 + * @tc.desc : Subscribe to WebSocket received server message events, using callback as an asynchronous method. The maximum length of each message is 4K, and more than 4K automatic fragmentation. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOn_Off_Message001', 0, async function (done) { + let ws = webSocket.createWebSocket(); + done(); + ws.on('message', (err, value) => { + console.log("on message, message:" + value); + expect(true).assertTrue(); + }); + ws.off('message'); + }); + + /* + * @tc.number: TEST_On_Off_CLOSE_001 + * @tc.name : testOn_Off_Close001 + * @tc.desc : Subscribe to WebSocket shutdown events, using the callback method as an asynchronous method. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOn_Off_Close001', 0, async function (done) { + let ws = webSocket.createWebSocket(); + done(); + ws.on('close', (err, value) => { + console.log("on close, code is " + value.code + ", reason is " + value.reason); + }); + ws.off('close'); + expect(true).assertTrue(); + }); + + /* + * @tc.number: TEST_ON_OFF_ERROR_001 + * @tc.name : testOn_Off_Error001 + * @tc.desc : Unsubscribe to WebSocket shutdown events, using the callback method as an asynchronous method. + * @tc.size : MediumTest + * @tc.type : Method + * @tc.level : Level 2 + */ + it('testOn_Off_Error001', 0, async function (done) { + let ws = webSocket.createWebSocket(); + done(); + ws.on('error', (err) => { + console.log("on error, error:" + JSON.stringify(err)) + }); + ws.off('error'); + expect(true).assertTrue(); + }); + + + }) +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/module.json b/telephony/crossplatform/telephonyWebSocketetstest/src/main/module.json new file mode 100644 index 0000000000000000000000000000000000000000..8e5df3d2cdabecbd3645bb7396822032a31f690c --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/module.json @@ -0,0 +1,44 @@ +{ + "module": { + "name": "entry_test", + "type": "feature", + "description": "$string:module_test_desc", + "mainElement": "TestAbility", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:test_pages", + "abilities": [ + { + "name": "TestAbility", + "srcEntry": "./ets/TestAbility/TestAbility.ets", + "description": "$string:TestAbility_desc", + "icon": "$media:icon", + "label": "$string:TestAbility_label", + "exported": true, + "startWindowIcon": "$media:icon", + "startWindowBackground": "$color:start_window_background", + "skills": [ + { + "actions": [ + "action.system.home" + ], + "entities": [ + "entity.system.home" + ] + } + ] + } + ], + "requestPermissions": [ + { + "name": "ohos.permission.INTERNET" + }, + { + "name": "ohos.permission.GET_NETWORK_INFO" + } + ] + } +} diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/element/color.json b/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/element/string.json b/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..65d8fa5a7cf54aa3943dcd0214f58d1771bc1f6c --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_test_desc", + "value": "test ability description" + }, + { + "name": "TestAbility_desc", + "value": "the test ability" + }, + { + "name": "TestAbility_label", + "value": "test label" + } + ] +} \ No newline at end of file diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/media/icon.png b/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/media/icon.png differ diff --git a/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/profile/test_pages.json b/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/profile/test_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..77e90731b5a38d861663029b483df3d3ac9ec74b --- /dev/null +++ b/telephony/crossplatform/telephonyWebSocketetstest/src/main/resources/base/profile/test_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "TestAbility/pages/Index" + ] +}