diff --git a/wifi/test/wifi_testapp/build-profile.json5 b/wifi/test/wifi_testapp/build-profile.json5 index 0955cff2c2385a8850769f7942facdb0e8e843ed..4b06f0477394a5b23fbee31899e8d6f32b69688e 100644 --- a/wifi/test/wifi_testapp/build-profile.json5 +++ b/wifi/test/wifi_testapp/build-profile.json5 @@ -32,11 +32,11 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": 12, + "compileSdkVersion": 18, //指定OpenHarmony应用/服务编译时的版本 - "compatibleSdkVersion": 10, + "compatibleSdkVersion": 18, //指定OpenHarmony应用/服务兼容的最低版本。 - "targetSdkVersion": 10, + "targetSdkVersion": 18, //指定OpenHarmony应用/服务目标版本。若没有设置,默认为compatibleSdkVersion "runtimeOS": "OpenHarmony" //指定为OpenHarmony diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/Component/testList.ets b/wifi/test/wifi_testapp/entry/src/main/ets/Component/testList.ets index e223eda30a7011dcbc2f4c30378ec625bc3dc6de..6225e28819207fbfd5ad0fa737b7159fe4bf05c5 100644 --- a/wifi/test/wifi_testapp/entry/src/main/ets/Component/testList.ets +++ b/wifi/test/wifi_testapp/entry/src/main/ets/Component/testList.ets @@ -41,6 +41,8 @@ export struct TestList { } .height(ConfigData.WH_100_100) .backgroundColor($r("app.color.lead")) + .scrollBar(BarState.Auto) + .edgeEffect(EdgeEffect.Spring) } } } \ No newline at end of file diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/newWifiManagerTestData.ets b/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/newWifiManagerTestData.ets new file mode 100755 index 0000000000000000000000000000000000000000..a7d7d890eccbb8c3be9f58e8b0fe05468a932f1f --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/newWifiManagerTestData.ets @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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. + */ + +/** + * 新接口测试下的wifiManager测试数据 - 只包含几个主要的测试用例 + */ + +import { TestApi , CaseCategory } from './testData' +import { + testIsWifiActiveManager, + testEnableWifiManager, + testDisableWifiManager, +} from './wifiManagerInterface' + +interface WifiTest { + name: string, + detail: string, + method: string, + api: Function, + result: string, + category: CaseCategory +} + +export const NewWifiManagerTestCase: WifiTest[] = [ + { + 'name' : '是否已使能' , + 'detail' : '是否已使能' , + 'method' : 'isWifiActive' , + 'api' : testIsWifiActiveManager , + 'result' : 'None' , + 'category' : CaseCategory.WifiManager + }, + { + 'name' : '开Wifi' , + 'detail' : '使能Wifi' , + 'method' : 'enableWifi' , + 'api' : testEnableWifiManager , + 'result' : 'None' , + 'category' : CaseCategory.WifiManager + }, + { + 'name' : '关wifi' , + 'detail' : '去使能wifi' , + 'method' : 'disableWifi' , + 'api' : testDisableWifiManager , + 'result' : 'None' , + 'category' : CaseCategory.WifiManager + }, +] + +export function initNewWifiManagerApiData(): Array { + let TestApiArray: Array = [] + NewWifiManagerTestCase.forEach(item => { + TestApiArray.push(new TestApi(item.name , item.detail , item.method , item.api , item.result , item.category)); + }) + return TestApiArray; +} diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testData.ets b/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testData.ets index 79dfb275306b2eddcfa155a4bc224be10177b863..7bfb64e3f75b485441236cab7ca8516dcae32e79 100644 --- a/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testData.ets +++ b/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testData.ets @@ -26,7 +26,8 @@ export enum Category { Stress , App , Stability, - NewApi + NewApi, + Socket } let NextId = 0; @@ -56,7 +57,8 @@ export enum CaseCategory { Hotspot , HotspotManager , P2p , - P2pManager + P2pManager, + Socket } export class TestApi { diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testDataModels.ets b/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testDataModels.ets index 89405e7d13a06897a57d49da1d02d5fcb6955949..aca4e69330639a97b5602fcda3fd9a1ee70196f8 100644 --- a/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testDataModels.ets +++ b/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testDataModels.ets @@ -1461,6 +1461,13 @@ const TestComposition: WifiTestComposition[] = [ 'image': $r('app.media.wifi'), 'category': Category.NewApi }, + { + 'name': '快速验证测试', + 'detail': '快速验证WiFi相关功能', + 'url': 'pages/newApi/QuickVerifyTest', + 'image': $r('app.media.wifi'), + 'category': Category.NewApi + }, { 'name' : 'WIFI' , 'detail' : 'wifi发现' , @@ -1482,6 +1489,34 @@ const TestComposition: WifiTestComposition[] = [ 'image' : $r('app.media.WLAN') , 'category' : Category.App }, + { + 'name' : 'TCP Socket验证' , + 'detail' : 'TCP连接测试' , + 'url' : 'pages/subAppTest/tcpSocketTest' , + 'image' : $r('app.media.wifi') , + 'category' : Category.App + }, + { + 'name' : 'UDP Socket验证' , + 'detail' : 'UDP连接测试' , + 'url' : 'pages/subAppTest/udpSocketTest' , + 'image' : $r('app.media.wifi') , + 'category' : Category.App + }, + { + 'name' : 'HTTP验证' , + 'detail' : 'HTTP请求测试' , + 'url' : 'pages/subAppTest/httpSocketTest' , + 'image' : $r('app.media.wifi') , + 'category' : Category.App + }, + { + 'name' : 'HTTPS验证' , + 'detail' : 'HTTPS请求测试' , + 'url' : 'pages/subAppTest/httpsSocketTest' , + 'image' : $r('app.media.wifi') , + 'category' : Category.App + }, { 'name' : 'WIFI' , diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/wifiManagerInterface.ets b/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/wifiManagerInterface.ets index 56f226db95c7e99a95bd75faa97403980122fe85..bd1359fffcb22cbfd330f5d7bd5049a5cf6814e0 100644 --- a/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/wifiManagerInterface.ets +++ b/wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/wifiManagerInterface.ets @@ -1121,7 +1121,7 @@ export function testStartPortalCertification() { export function testStartWifiDetection() { try { let message = '' - wifiManager.StartWifiDetection() + // wifiManager.StartWifiDetection() message = "StartWifiDetection 调用成功" promptAction.showToast({ message : message }) console.log(TAG , LogDataManager.testRemoveDeviceManager_EndLog); diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/homePage.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/homePage.ets index b24aaa795324681793525eb84496bdb3ddb233a7..6db3c8a75d2483eef7317c452e7304bcb559f638 100644 --- a/wifi/test/wifi_testapp/entry/src/main/ets/pages/homePage.ets +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/homePage.ets @@ -42,6 +42,7 @@ struct TestCategory { TabContent() { NewApiTest({ testItems: this.testItems.filter(item => (item.category === Category.NewApi))}) }.tabBar('新接口测试').margin({ top : $r("app.float.distance_5") }) + TabContent() { AppTest({ testItems : this.testItems.filter(item => (item.category === Category.App)) }) }.tabBar($r("app.string.App")).margin({ top : $r("app.float.distance_5") }) diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/QuickVerifyTest.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/QuickVerifyTest.ets new file mode 100755 index 0000000000000000000000000000000000000000..4e327c984539c78cb48634876e82d549ce50b36a --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/QuickVerifyTest.ets @@ -0,0 +1,318 @@ +/** + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 { PageTitle } from '../../Component/pageTitle'; +import { TestImageDisplay } from '../../Component/testImageDisplay'; +import { ContentTable } from '../../Component/contentTable'; +import router from '@ohos.router'; +import ConfigData from '../../utils/ConfigData'; +import { initNewWifiManagerApiData } from '../../entryability/model/newWifiManagerTestData' +import { TestData } from '../../entryability/model/testData' +import wifiManager from '@ohos.wifiManager'; + +interface myParams extends Object { + testItem: TestData +} + +const TAG = 'wifiTestApp [QuickVerifyTest]' + +/** + * 快速验证测试页面 - 基于wifiManager接口 + */ +interface wifiDeviceConfigManager { + ssid: string, + bssid: string, + preSharedKey: string, + isHiddenSsid: boolean, + securityType: wifiManager.WifiSecurityType, + creatorUid: number, + disableReason: number, + netId: number, + randomMacType: number, + randomMacAddr: string, + ipType: wifiManager.IpType, + staticIp: wifiManager.IpConfig +} + +@Component +struct QuickVerifyDeviceSetting { + @StorageLink('isAllowedQuick') isAllowedQuick: number = 1; + @StorageLink('w_networkIdMQuick') w_networkIdMQuick: number = 0; + @StorageLink('w_featureIdMQuick') w_featureIdMQuick: number = 1; + @StorageLink('wifiDeviceConfigManagerQuick') wifiDeviceConfigManagerQuick: wifiManager.WifiDeviceConfig = { + ssid : "TP-LINK_6365" , + bssid : "6C:B1:58:75:63:65" , + preSharedKey : "12345678" , + isHiddenSsid : false , + securityType : 3 , + creatorUid : 1 , + disableReason : 0 , + netId : 1 , + randomMacType : 0 , + randomMacAddr : "08:fb:ea:1b:38:aa" , + ipType : 1 , + staticIp : { + ipAddress : 3232235880 , + gateway : 3232235777 , + prefixLength : 0 , + dnsServers : [ 3716386629 ] , + domains : [ "0", "1", "2" ] , + } + } + + aboutToAppear() { + } + + build() { + Column() { + Row() { + Text("SetScanAlwaysAllowed:").fontSize("17vp").width(200) + TextInput({ text : this.isAllowedQuick.toString() , placeholder : "1" }) + .fontSize("15vp") + .onChange((strInput: string) => { + if ( strInput.length >= 1 ) { + this.isAllowedQuick = Number(strInput); + } + }) + .width(ConfigData.WH_50_100) + .borderRadius(1) + } + .backgroundColor($r("app.color.moon")) + .padding(5) + .justifyContent(FlexAlign.Start) + .alignItems(VerticalAlign.Center) + + Row() { + Text("ssidM:").fontSize("17vp").width(70) + TextInput({ text : this.wifiDeviceConfigManagerQuick.ssid , placeholder : "TP-LINK_6365" }) + .fontSize("15vp") + .onChange((strInput: string) => { + if ( strInput.length >= 1 ) { + this.wifiDeviceConfigManagerQuick.ssid = strInput; + } + }) + .width(ConfigData.WH_80_100) + .borderRadius(1) + } + .backgroundColor($r("app.color.moon")) + .padding(5) + .justifyContent(FlexAlign.Start) + .alignItems(VerticalAlign.Center) + + Scroll() { + Column() { + Stack().height("0.25vp").backgroundColor("#000000"); + Column() { + Row() { + Text("bssidM:").fontSize("15vp").width(60); + TextInput({ text : this.wifiDeviceConfigManagerQuick.bssid , placeholder : "6c:b1:58:75:63:67" }) + .fontSize("15vp") + .onChange((strInput: string) => { + if ( strInput.length >= 1 ) { + this.wifiDeviceConfigManagerQuick!.bssid = strInput; + } + }) + .width(ConfigData.WH_80_100) + .borderRadius(1) + } + .padding(3) + .justifyContent(FlexAlign.Start) + .alignItems(VerticalAlign.Center) + .backgroundColor($r("app.color.spring")) + + Row() { + Text("preSharedKeyM:").fontSize("15vp").width(110); + TextInput({ text : this.wifiDeviceConfigManagerQuick.preSharedKey , placeholder : "12345678" }) + .fontSize("15vp") + .onChange((strInput: string) => { + if ( strInput.length >= 1 ) { + this.wifiDeviceConfigManagerQuick!.preSharedKey = strInput; + } + }) + .width(ConfigData.WH_80_100) + .borderRadius(1) + } + .backgroundColor($r("app.color.spring")) + .padding(5) + .justifyContent(FlexAlign.Start) + .alignItems(VerticalAlign.Center) + + Row() { + Text("securityTypeM:").fontSize(15).width(100); + Column() { + Select([ + { value : 'WIFI_SEC_TYPE_INVALID' }, + { value : 'WIFI_SEC_TYPE_OPEN' }, + { value : 'WIFI_SEC_TYPE_WEP' }, + { value : 'WIFI_SEC_TYPE_PSK' }, + { value : 'WIFI_SEC_TYPE_SAE' } + ]) + .selected(3) + .value('WIFI_SEC_TYPE_PSK') + .font({ size : 17 }) + .selectedOptionFont({ size : 17 }) + .optionFont({ size : 15 }) + .width(ConfigData.WH_80_100) + .onSelect((index: number) => { + console.log(TAG , "Select:" + index) + this.wifiDeviceConfigManagerQuick!.securityType = index; + }) + } + .width(ConfigData.WH_80_100) + .borderRadius(1) + } + .backgroundColor($r("app.color.spring")) + .padding(5) + .justifyContent(FlexAlign.Start) + .alignItems(VerticalAlign.Center) + } + + Stack().height("0.25vp").backgroundColor("#000000"); + } + } + .scrollBarWidth(10) + .scrollBar(BarState.On) + } + .height(ConfigData.WH_58_100) + } +} + +@Entry +@Component +struct QuickVerifyTest { + @State showList: boolean = false; + private testItem: TestData = (router.getParams() as myParams).testItem + @State message: string = ""; + @State deviceId: string = ''; + @State currentClick: number = - 1; + @StorageLink('isAllowedQuick') isAllowedQuick: number = 1; + @StorageLink('pairedSsidMQuick') pairedSsidMQuick: string = ""; + @StorageLink('w_networkIdMQuick') w_networkIdMQuick: number = 0; + @StorageLink('w_featureIdMQuick') w_featureIdMQuick: number = 1; + @StorageLink('ipInfoManagerMessageQuick') ipInfoManagerMessageQuick: string = ''; + @StorageLink('linkedInfoManagerMessageQuick') linkedInfoManagerMessageQuick: string = ''; + @StorageLink('getDeviceConfigsManagerMessageQuick') getDeviceConfigsManagerMessageQuick: string = ''; + @StorageLink('getScanInfoManagerMessageQuick') getScanInfoManagerMessageQuick: string = ''; + @StorageLink('wifiDeviceConfigManagerQuick') wifiDeviceConfigManagerQuick: wifiManager.WifiDeviceConfig = { + ssid : "TP-LINK_6365" , + bssid : "6C:B1:58:75:63:65" , + preSharedKey : "12345678" , + isHiddenSsid : false , + securityType : 3 , + creatorUid : 1 , + disableReason : 0 , + netId : 100 , + randomMacType : 0 , + randomMacAddr : "08:fb:ea:1b:38:aa" , + ipType : 1 , + staticIp : { + ipAddress : 3232235880 , + gateway : 3232235777 , + prefixLength : 0 , + dnsServers : [ 3716386629 ] , + domains : [ "0", "1", "2" ] , + } + } + + aboutToAppear() { + + } + + init() { + console.log("QuickVerifyTest init-----------") + } + + build() { + Column() { + Stack({ alignContent : Alignment.TopStart }) { + TestImageDisplay({ testItem : this.testItem }) + PageTitle({ testItem : this.testItem }) + } + + Stack().height("0.5vp").backgroundColor("#000000"); + Column() { + Row() { + Text("已连接的WiFi:") + .fontSize("17vp") + .height(40) + .width(ConfigData.WH_30_100) + .align(Alignment.Start) + + TextInput({ text : this.pairedSsidMQuick , placeholder : "请在右侧输入WiFi的配置" }) + .height(40) + .borderStyle(BorderStyle.Dashed) + .backgroundColor($r("app.color.white")) + .onChange((str) => { + this.deviceId = str; + }) + .width(ConfigData.WH_45_100) + .padding({ top : 5 , left : 5 }) + Image($r('app.media.Switch')) + .height(50) + .padding({ top : 5 , bottom : 5 , left : 10 }) + .width(ConfigData.WH_15_100) + .onClick(() => { + this.showList = !this.showList; + }) + } + } + .align(Alignment.Start) + + if ( this.showList ) { + QuickVerifyDeviceSetting() + } else { + Column() { + Scroll() { + Column() { + Text("WLAN连接信息:" + this.linkedInfoManagerMessageQuick) + .fontSize("17vp") + .margin({ top : "5vp" , left : "10vp" }) + .textAlign(TextAlign.Start) + .width(ConfigData.WH_90_100) + Text("IP信息:" + this.ipInfoManagerMessageQuick) + .fontSize("17vp") + .margin({ top : "5vp" , left : "10vp" }) + .textAlign(TextAlign.Start) + .width(ConfigData.WH_90_100) + Text("获取网络配置:" + this.getDeviceConfigsManagerMessageQuick) + .fontSize("17vp") + .margin({ top : "5vp" , left : "10vp" }) + .textAlign(TextAlign.Start) + .width(ConfigData.WH_90_100) + Text("扫描结果:" + this.getScanInfoManagerMessageQuick) + .fontSize("17vp") + .margin({ top : "5vp" , left : "10vp" }) + .textAlign(TextAlign.Start) + .width(ConfigData.WH_90_100) + } + .width(ConfigData.WH_100_100) + } + .scrollBarWidth(10) + .scrollBar(BarState.Auto) + } + .height(100) + .width(ConfigData.WH_95_100) + .backgroundColor($r("sys.color.ohos_id_color_foreground_contrary")) + + ContentTable({ + testItem : this.testItem , apiItems : initNewWifiManagerApiData() + }) + } + } + .alignItems(HorizontalAlign.Center) + .backgroundColor($r("app.color.lead")) + .height(ConfigData.WH_100_100) + } +} diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/httpSocketTest.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/httpSocketTest.ets new file mode 100755 index 0000000000000000000000000000000000000000..9b99fa8845bbe6285ee7db5370c390871a78c640 --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/httpSocketTest.ets @@ -0,0 +1,280 @@ +/** + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 http from '@ohos.net.http'; +import { TitleBar } from '../../Component/titleBar'; +import ConfigData from '../../utils/ConfigData'; +import { BusinessError } from '@ohos.base'; + +const TAG = 'wifiTestApp [httpSocketTest]'; + +/** + * HTTP Test Page Of Wifi test + */ +@Entry +@Component +struct HttpSocketTest { + @State selectIndex: number = 0; + @State selectValue: string = 'GET'; + @State typeList: Array = [ + { value: 'GET'}, + { value: 'POST'}, + { value: 'PUT'}, + { value: 'DELETE'}, + { value: 'TRACE'}, + { value: 'CONNECT'}, + { value: 'OPTIONS'} + ]; + @State urlValue: string = 'http://www.baidu.com'; + @State reqSelectIndex: number = 0; + @State reqSelectValue: string = 'none'; + @State reqTypeList: Array = [ + { value: 'none'}, + { value: 'raw'} + ]; + @State reqValue: string = ''; + @State resData: string = ''; + @State codeValue: string = ''; + @State isLoading: boolean = false; + + build() { + Column() { + Stack({ alignContent: Alignment.TopStart }) { + TitleBar({ detail: "HTTP验证" }) + } + + Column() { + // HTTP请求配置区域 + Text('HTTP请求配置') + .fontSize(18) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ top: 10, bottom: 10 }) + + Row() { + Select(this.typeList) + .selected(this.selectIndex) + .value(this.selectValue) + .font({ size: 12, weight: 300 }) + .fontColor('#182431') + .selectedOptionFont({ size: 16, weight: 400 }) + .optionFont({ size: 16, weight: 400 }) + .menuAlign(MenuAlignType.START, {dx:0, dy:0}) + .borderRadius('5') + .width("25%") + .optionHeight(300) + .onSelect((index:number, text?: string | undefined)=>{ + console.log(TAG, 'Select:' + index) + this.selectIndex = index; + if(text){ + this.selectValue = text; + if (this.selectValue == 'GET') { + this.urlValue = 'http://www.baidu.com'; + this.reqValue = ''; + } else if (this.selectValue == 'POST') { + this.urlValue = 'https://httpbin.org/post'; + this.reqValue = '{"username": "test","password": "123"}'; + } + } + }) + TextInput({text:this.urlValue}).width('75%').borderRadius('5') + .onChange((val: string) => { + this.urlValue = val; + }) + } + .width('100%') + .margin({bottom:10}) + + // 请求体配置 + Text('请求体配置') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ bottom: 5 }) + + Row() { + Select(this.reqTypeList) + .selected(this.reqSelectIndex) + .value(this.reqSelectValue) + .font({ size: 12, weight: 300 }) + .fontColor('#182431') + .selectedOptionFont({ size: 16, weight: 400 }) + .optionFont({ size: 16, weight: 400 }) + .menuAlign(MenuAlignType.START, {dx:0, dy:0}) + .borderRadius('5') + .width("25%") + .optionHeight(300) + .onSelect((index:number, text?: string | undefined)=>{ + console.log(TAG, 'Select:' + index) + this.reqSelectIndex = index; + if(text){ + this.reqSelectValue = text; + } + }) + TextInput({text:this.reqValue}).width('75%').borderRadius('5') + .onChange((val: string) => { + this.reqValue = val; + }) + } + .width('100%') + .margin({bottom:15}) + + // 发送按钮和清空按钮 + Row() { + Button('发送请求') + .width('45%') + .backgroundColor(this.isLoading ? Color.Gray : Color.Blue) + .enabled(!this.isLoading) + .onClick(() => { + this.sendHttpRequest(); + }) + + Button('清空响应') + .width('45%') + .backgroundColor(Color.Orange) + .onClick(() => { + this.clearResponse(); + }) + } + .width('100%') + .justifyContent(FlexAlign.SpaceBetween) + .margin({ bottom: 15 }) + + // 响应状态 + if (this.codeValue) { + Row() { + Text('响应状态: ') + .fontSize(16) + .fontWeight(FontWeight.Bold) + Text(this.codeValue) + .fontSize(16) + .fontColor(this.codeValue.startsWith('2') ? Color.Green : Color.Red) + } + .width('100%') + .margin({ bottom: 10 }) + } + + // 响应区域 + Text('响应数据') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ bottom: 5 }) + + TextArea({ text: this.resData }) + .width('100%') + .height('50%') + .borderRadius(5) + .backgroundColor(Color.White) + } + .width('95%') + .height('85%') + .padding(10) + } + .size({ width: ConfigData.WH_100_100, height: ConfigData.WH_100_100 }) + .backgroundColor('#F5F5F5') + } + + sendHttpRequest() { + this.isLoading = true; + this.codeValue = ''; + this.resData = ''; + + let httpRequest = http.createHttp(); + + // 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息 + httpRequest.on('headersReceive', (header: Object) => { + console.log(TAG, 'header: ' + JSON.stringify(header)); + }); + + class Header { + public contentType: string; + + constructor(contentType: string) { + this.contentType = contentType; + } + } + + let str2method = (value: string): http.RequestMethod => { + switch (value) { + case 'GET': + return http.RequestMethod.GET; + case 'HEAD': + return http.RequestMethod.HEAD; + case 'POST': + return http.RequestMethod.POST; + case 'PUT': + return http.RequestMethod.PUT; + case 'DELETE': + return http.RequestMethod.DELETE; + case 'TRACE': + return http.RequestMethod.TRACE; + case 'OPTIONS': + return http.RequestMethod.OPTIONS; + case 'CONNECT': + return http.RequestMethod.CONNECT; + } + return http.RequestMethod.GET; + } + + let str2head = (): Header => { + return new Header('application/json') + } + + let options: http.HttpRequestOptions = { + method: str2method(this.selectValue), + header: str2head(), + extraData: this.reqValue, + expectDataType: http.HttpDataType.STRING, + connectTimeout: 60000, + readTimeout: 60000 + } + + httpRequest.request(this.urlValue, options, (err: BusinessError, data: http.HttpResponse) => { + this.isLoading = false; + if (!err) { + // data.result为HTTP响应内容,可根据业务需要进行解析 + console.log(TAG, 'Result:' + JSON.stringify(data.result)); + this.resData = 'Result:\n' + JSON.stringify(data.result); + console.log(TAG, 'code:' + JSON.stringify(data.responseCode)); + this.resData += '\ncode:\t' + JSON.stringify(data.responseCode); + this.codeValue = JSON.stringify(data.responseCode); + console.log(TAG, 'type:' + JSON.stringify(data.resultType)); + this.resData += '\ntype:\t' + JSON.stringify(data.resultType); + // data.header为HTTP响应头,可根据业务需要进行解析 + console.log(TAG, 'header:' + JSON.stringify(data.header)); + this.resData += '\nheader:\n' + JSON.stringify(data.header); + console.log(TAG, 'cookies:' + JSON.stringify(data.cookies)); // 自API version 8开始支持cookie + // 取消订阅HTTP响应头事件 + httpRequest.off('headersReceive'); + // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 + httpRequest.destroy(); + } else { + console.log(TAG, 'response err: ' + JSON.stringify(err)); + this.resData = 'response err: ' + JSON.stringify(err); + this.codeValue = 'Error'; + // 取消订阅HTTP响应头事件 + httpRequest.off('headersReceive'); + // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 + httpRequest.destroy(); + } + }) + } + + clearResponse() { + this.resData = ''; + this.codeValue = ''; + } +} diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/httpsSocketTest.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/httpsSocketTest.ets new file mode 100755 index 0000000000000000000000000000000000000000..27df4a7053dcbe72b63b96b60e422260f90832b8 --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/httpsSocketTest.ets @@ -0,0 +1,318 @@ +/** + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 http from '@ohos.net.http'; +import { TitleBar } from '../../Component/titleBar'; +import ConfigData from '../../utils/ConfigData'; +import { BusinessError } from '@ohos.base'; + +const TAG = 'wifiTestApp [httpsSocketTest]'; + +/** + * HTTPS Test Page Of Wifi test + */ +@Entry +@Component +struct HttpsSocketTest { + @State selectIndex: number = 0; + @State selectValue: string = 'GET'; + @State typeList: Array = [ + { value: 'GET'}, + { value: 'POST'}, + { value: 'PUT'}, + { value: 'DELETE'}, + { value: 'TRACE'}, + { value: 'CONNECT'}, + { value: 'OPTIONS'} + ]; + @State urlValue: string = 'https://www.baidu.com'; + @State reqSelectIndex: number = 0; + @State reqSelectValue: string = 'none'; + @State reqTypeList: Array = [ + { value: 'none'}, + { value: 'raw'} + ]; + @State reqValue: string = ''; + @State resData: string = ''; + @State codeValue: string = ''; + @State isLoading: boolean = false; + @State sslVerify: boolean = true; + + build() { + Column() { + Stack({ alignContent: Alignment.TopStart }) { + TitleBar({ detail: "HTTPS验证" }) + } + + Column() { + // HTTPS请求配置区域 + Text('HTTPS请求配置') + .fontSize(18) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ top: 10, bottom: 10 }) + + Row() { + Select(this.typeList) + .selected(this.selectIndex) + .value(this.selectValue) + .font({ size: 12, weight: 300 }) + .fontColor('#182431') + .selectedOptionFont({ size: 16, weight: 400 }) + .optionFont({ size: 16, weight: 400 }) + .menuAlign(MenuAlignType.START, {dx:0, dy:0}) + .borderRadius('5') + .width("25%") + .optionHeight(300) + .onSelect((index:number, text?: string | undefined)=>{ + console.log(TAG, 'Select:' + index) + this.selectIndex = index; + if(text){ + this.selectValue = text; + if (this.selectValue == 'GET') { + this.urlValue = 'https://www.baidu.com'; + this.reqValue = ''; + } else if (this.selectValue == 'POST') { + this.urlValue = 'https://httpbin.org/post'; + this.reqValue = '{"username": "test","password": "123"}'; + } + } + }) + TextInput({text:this.urlValue}).width('75%').borderRadius('5') + .onChange((val: string) => { + this.urlValue = val; + }) + } + .width('100%') + .margin({bottom:10}) + + // SSL验证配置 + Row() { + Text('SSL证书验证: ') + .fontSize(16) + .width('40%') + Toggle({ type: ToggleType.Switch, isOn: this.sslVerify }) + .onChange((isOn: boolean) => { + this.sslVerify = isOn; + console.log(TAG, `SSL验证: ${isOn}`); + }) + .width('20%') + Text(this.sslVerify ? '开启' : '关闭') + .fontSize(14) + .fontColor(this.sslVerify ? Color.Green : Color.Red) + .width('40%') + } + .width('100%') + .margin({bottom:10}) + + // 请求体配置 + Text('请求体配置') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ bottom: 5 }) + + Row() { + Select(this.reqTypeList) + .selected(this.reqSelectIndex) + .value(this.reqSelectValue) + .font({ size: 12, weight: 300 }) + .fontColor('#182431') + .selectedOptionFont({ size: 16, weight: 400 }) + .optionFont({ size: 16, weight: 400 }) + .menuAlign(MenuAlignType.START, {dx:0, dy:0}) + .borderRadius('5') + .width("25%") + .optionHeight(300) + .onSelect((index:number, text?: string | undefined)=>{ + console.log(TAG, 'Select:' + index) + this.reqSelectIndex = index; + if(text){ + this.reqSelectValue = text; + } + }) + TextInput({text:this.reqValue}).width('75%').borderRadius('5') + .onChange((val: string) => { + this.reqValue = val; + }) + } + .width('100%') + .margin({bottom:15}) + + // 发送按钮和清空按钮 + Row() { + Button('发送HTTPS请求') + .width('45%') + .backgroundColor(this.isLoading ? Color.Gray : Color.Blue) + .enabled(!this.isLoading) + .onClick(() => { + this.sendHttpsRequest(); + }) + + Button('清空响应') + .width('45%') + .backgroundColor(Color.Orange) + .onClick(() => { + this.clearResponse(); + }) + } + .width('100%') + .justifyContent(FlexAlign.SpaceBetween) + .margin({ bottom: 15 }) + + // 响应状态 + if (this.codeValue) { + Row() { + Text('响应状态: ') + .fontSize(16) + .fontWeight(FontWeight.Bold) + Text(this.codeValue) + .fontSize(16) + .fontColor(this.codeValue.startsWith('2') ? Color.Green : Color.Red) + } + .width('100%') + .margin({ bottom: 10 }) + } + + // 响应区域 + Text('响应数据') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ bottom: 5 }) + + TextArea({ text: this.resData }) + .width('100%') + .height('45%') + .borderRadius(5) + .backgroundColor(Color.White) + } + .width('95%') + .height('85%') + .padding(10) + } + .size({ width: ConfigData.WH_100_100, height: ConfigData.WH_100_100 }) + .backgroundColor('#F5F5F5') + } + + sendHttpsRequest() { + this.isLoading = true; + this.codeValue = ''; + this.resData = ''; + + let httpRequest = http.createHttp(); + + // 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息 + httpRequest.on('headersReceive', (header: Object) => { + console.log(TAG, 'header: ' + JSON.stringify(header)); + }); + + class Header { + public contentType: string; + + constructor(contentType: string) { + this.contentType = contentType; + } + } + + let str2method = (value: string): http.RequestMethod => { + switch (value) { + case 'GET': + return http.RequestMethod.GET; + case 'HEAD': + return http.RequestMethod.HEAD; + case 'POST': + return http.RequestMethod.POST; + case 'PUT': + return http.RequestMethod.PUT; + case 'DELETE': + return http.RequestMethod.DELETE; + case 'TRACE': + return http.RequestMethod.TRACE; + case 'OPTIONS': + return http.RequestMethod.OPTIONS; + case 'CONNECT': + return http.RequestMethod.CONNECT; + } + return http.RequestMethod.GET; + } + + let str2head = (): Header => { + return new Header('application/json') + } + + let options: http.HttpRequestOptions = { + method: str2method(this.selectValue), + header: str2head(), + extraData: this.reqValue, + expectDataType: http.HttpDataType.STRING, + connectTimeout: 60000, + readTimeout: 60000, + // HTTPS特定配置 + usingCache: true, + priority: 1, + usingProtocol: http.HttpProtocol.HTTP2, + // SSL证书验证配置 + caPath: this.sslVerify ? undefined : "/system/etc/security/cacerts/" + } + + httpRequest.request(this.urlValue, options, (err: BusinessError, data: http.HttpResponse) => { + this.isLoading = false; + if (!err) { + // data.result为HTTPS响应内容,可根据业务需要进行解析 + console.log(TAG, 'Result:' + JSON.stringify(data.result)); + this.resData = 'Result:\n' + JSON.stringify(data.result); + console.log(TAG, 'code:' + JSON.stringify(data.responseCode)); + this.resData += '\ncode:\t' + JSON.stringify(data.responseCode); + this.codeValue = JSON.stringify(data.responseCode); + console.log(TAG, 'type:' + JSON.stringify(data.resultType)); + this.resData += '\ntype:\t' + JSON.stringify(data.resultType); + // data.header为HTTPS响应头,可根据业务需要进行解析 + console.log(TAG, 'header:' + JSON.stringify(data.header)); + this.resData += '\nheader:\n' + JSON.stringify(data.header); + console.log(TAG, 'cookies:' + JSON.stringify(data.cookies)); // 自API version 8开始支持cookie + + // 添加SSL相关信息 + if (data.header && data.header['server']) { + this.resData += '\nSSL连接成功'; + } + + // 取消订阅HTTPS响应头事件 + httpRequest.off('headersReceive'); + // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 + httpRequest.destroy(); + } else { + console.log(TAG, 'response err: ' + JSON.stringify(err)); + this.resData = 'response err: ' + JSON.stringify(err); + this.codeValue = 'Error'; + + // 检查是否是SSL相关错误 + if (err.message && err.message.includes('SSL')) { + this.resData += '\n\nSSL错误提示:请检查证书配置或尝试关闭SSL验证'; + } + + // 取消订阅HTTPS响应头事件 + httpRequest.off('headersReceive'); + // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 + httpRequest.destroy(); + } + }) + } + + clearResponse() { + this.resData = ''; + this.codeValue = ''; + } +} diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/p2pDiscoveryTest.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/p2pDiscoveryTest.ets index 477001d0495032f1c4c6af46a27d6f5f1ca2ea7e..e7db3e8cd3e61a8074b655300662d5c7a70cd5db 100644 --- a/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/p2pDiscoveryTest.ets +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/p2pDiscoveryTest.ets @@ -21,7 +21,7 @@ import router from '@ohos.router'; import { PageTitle } from '../../Component/pageTitle' import { TestImageDisplay } from '../../Component/testImageDisplay' import { P2pModel } from '../../entryability/model/p2pModel' -import ConfigData from '../../utils/ConfigData'; +import ConfigData from '../../Utils/ConfigData'; interface myParams extends Object { testItem: TestData diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/tcpSocketTest.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/tcpSocketTest.ets new file mode 100755 index 0000000000000000000000000000000000000000..725d5af8e9847f6841c5856a029404c3d4a20377 --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/tcpSocketTest.ets @@ -0,0 +1,290 @@ +/** + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 socket from '@ohos.net.socket'; +import { TitleBar } from '../../Component/titleBar'; +import ConfigData from '../../utils/ConfigData'; + +export interface NetAddress { + address: string; + family?: number; + port?: number; +} + +export interface TCPConnectOptions { + address: NetAddress; + timeout?: number; +} + +const TAG = 'wifiTestApp [tcpSocketTest]'; + +/** + * TCP Socket Test Page Of Wifi test + */ +@Entry +@Component +struct TcpSocketTest { + @State serverAddress: string = '192.168.1.100'; + @State serverPort: string = '8080'; + @State message: string = 'Hello TCP Server'; + @State responseData: string = ''; + @State isConnected: boolean = false; + @State connectionStatus: string = '未连接'; + private tcpSocket: socket.TCPSocket | null = null; + + aboutToAppear() { + this.initSocket(); + } + + initSocket() { + this.tcpSocket = socket.constructTCPSocketInstance(); + } + + async connectToServer() { + if (!this.tcpSocket) { + this.responseData = '错误: Socket未初始化'; + return; + } + + try { + this.connectionStatus = '连接中...'; + this.responseData = ''; + + // 绑定本地地址(可选) + const bindAddress: NetAddress = { address: '0.0.0.0', port: 0, family: 1 }; + await this.tcpSocket.bind(bindAddress); + + // 连接到服务器 + const serverAddress: NetAddress = { + address: this.serverAddress, + port: parseInt(this.serverPort), + family: 1 + }; + const connectOptions: TCPConnectOptions = { + address: serverAddress, + timeout: 60000 + }; + await this.tcpSocket.connect(connectOptions); + + this.isConnected = true; + this.connectionStatus = '已连接'; + this.responseData = `成功连接到 ${this.serverAddress}:${this.serverPort}`; + + // 设置消息接收回调 + this.tcpSocket.on('message', (data) => { + const receivedData = data.message.toString(); + console.log(TAG, `收到数据: ${receivedData}`); + this.responseData += `\n收到: ${receivedData}`; + }); + + // 设置错误回调 + this.tcpSocket.on('error', (err) => { + console.error(TAG, `Socket错误: ${JSON.stringify(err)}`); + this.responseData += `\n错误: ${JSON.stringify(err)}`; + this.isConnected = false; + this.connectionStatus = '连接错误'; + }); + + // 设置关闭回调 + this.tcpSocket.on('close', () => { + console.log(TAG, 'Socket连接已关闭'); + this.responseData += '\n连接已关闭'; + this.isConnected = false; + this.connectionStatus = '连接已关闭'; + }); + + } catch (error) { + console.error(TAG, `连接失败: ${JSON.stringify(error)}`); + this.responseData = `连接失败: ${JSON.stringify(error)}`; + this.isConnected = false; + this.connectionStatus = '连接失败'; + } + } + + async sendMessage() { + if (!this.tcpSocket || !this.isConnected) { + this.responseData += '\n错误: 未连接到服务器'; + return; + } + + try { + await this.tcpSocket.send({ + data: this.message, + encoding: 'utf8' + }); + this.responseData += `\n发送: ${this.message}`; + console.log(TAG, `发送消息: ${this.message}`); + } catch (error) { + console.error(TAG, `发送失败: ${JSON.stringify(error)}`); + this.responseData += `\n发送失败: ${JSON.stringify(error)}`; + } + } + + async disconnectFromServer() { + if (!this.tcpSocket) { + return; + } + + try { + await this.tcpSocket.close(); + this.isConnected = false; + this.connectionStatus = '已断开'; + this.responseData += '\n主动断开连接'; + } catch (error) { + console.error(TAG, `断开连接失败: ${JSON.stringify(error)}`); + this.responseData += `\n断开连接失败: ${JSON.stringify(error)}`; + } + } + + clearResponse() { + this.responseData = ''; + } + + build() { + Column() { + Stack({ alignContent: Alignment.TopStart }) { + TitleBar({ detail: "TCP Socket验证" }) + } + + Column() { + // 服务器配置区域 + Text('服务器配置') + .fontSize(18) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ top: 10, bottom: 10 }) + + Row() { + Text('服务器地址: ') + .fontSize(16) + .width('30%') + TextInput({ text: this.serverAddress }) + .width('70%') + .borderRadius(5) + .onChange((value: string) => { + this.serverAddress = value; + }) + } + .width('100%') + .margin({ bottom: 10 }) + + Row() { + Text('端口: ') + .fontSize(16) + .width('30%') + TextInput({ text: this.serverPort }) + .width('70%') + .borderRadius(5) + .type(InputType.Number) + .onChange((value: string) => { + this.serverPort = value; + }) + } + .width('100%') + .margin({ bottom: 10 }) + + // 连接状态 + Row() { + Text('连接状态: ') + .fontSize(16) + .width('30%') + Text(this.connectionStatus) + .fontSize(16) + .fontColor(this.isConnected ? Color.Green : Color.Red) + .width('70%') + } + .width('100%') + .margin({ bottom: 10 }) + + // 操作按钮 + Row() { + Button('连接') + .width('22%') + .backgroundColor(this.isConnected ? Color.Gray : Color.Blue) + .enabled(!this.isConnected) + .onClick(() => { + this.connectToServer(); + }) + + Button('断开') + .width('22%') + .backgroundColor(!this.isConnected ? Color.Gray : Color.Red) + .enabled(this.isConnected) + .onClick(() => { + this.disconnectFromServer(); + }) + + Button('清空') + .width('22%') + .backgroundColor(Color.Orange) + .onClick(() => { + this.clearResponse(); + }) + + Button('发送') + .width('22%') + .backgroundColor(!this.isConnected ? Color.Gray : Color.Green) + .enabled(this.isConnected) + .onClick(() => { + this.sendMessage(); + }) + } + .width('100%') + .justifyContent(FlexAlign.SpaceBetween) + .margin({ bottom: 15 }) + + // 消息输入区域 + Text('发送消息') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ bottom: 5 }) + + TextInput({ text: this.message }) + .width('100%') + .borderRadius(5) + .onChange((value: string) => { + this.message = value; + }) + .margin({ bottom: 15 }) + + // 响应区域 + Text('响应数据') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ bottom: 5 }) + + TextArea({ text: this.responseData }) + .width('100%') + .height('40%') + .borderRadius(5) + .backgroundColor(Color.White) + } + .width('95%') + .height('85%') + .padding(10) + } + .size({ width: ConfigData.WH_100_100, height: ConfigData.WH_100_100 }) + .backgroundColor('#F5F5F5') + } + + aboutToDisappear() { + if (this.tcpSocket) { + this.tcpSocket.close(); + this.tcpSocket = null; + } + } +} diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/udpSocketTest.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/udpSocketTest.ets new file mode 100755 index 0000000000000000000000000000000000000000..f8281f4a37ab9d8f977d0dd72691f33979853324 --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/udpSocketTest.ets @@ -0,0 +1,305 @@ +/** + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 socket from '@ohos.net.socket'; +import { TitleBar } from '../../Component/titleBar'; +import ConfigData from '../../utils/ConfigData'; + +export interface NetAddress { + address: string; + family?: number; + port?: number; +} + +const TAG = 'wifiTestApp [udpSocketTest]'; + +/** + * UDP Socket Test Page Of Wifi test + */ +@Entry +@Component +struct UdpSocketTest { + @State serverAddress: string = '192.168.1.100'; + @State serverPort: string = '8081'; + @State localPort: string = '9090'; + @State message: string = 'Hello UDP Server'; + @State responseData: string = ''; + @State isSocketBound: boolean = false; + @State socketStatus: string = '未绑定'; + private udpSocket: socket.UDPSocket | null = null; + + aboutToAppear() { + this.initSocket(); + } + + initSocket() { + this.udpSocket = socket.constructUDPSocketInstance(); + } + + async bindSocket() { + if (!this.udpSocket) { + this.responseData = '错误: Socket未初始化'; + return; + } + + try { + this.socketStatus = '绑定中...'; + this.responseData = ''; + + // 绑定本地端口 + const bindAddress: NetAddress = { + address: '0.0.0.0', + port: parseInt(this.localPort), + family: 1 + }; + await this.udpSocket.bind(bindAddress); + + this.isSocketBound = true; + this.socketStatus = '已绑定'; + this.responseData = `成功绑定到本地端口 ${this.localPort}`; + + // 设置消息接收回调 + this.udpSocket.on('message', (data) => { + const receivedData = data.message.toString(); + const remoteInfo = data.remoteInfo; + console.log(TAG, `收到数据: ${receivedData}, 来自: ${remoteInfo.address}:${remoteInfo.port}`); + this.responseData += `\n收到: ${receivedData} (来自: ${remoteInfo.address}:${remoteInfo.port})`; + }); + + // 设置错误回调 + this.udpSocket.on('error', (err) => { + console.error(TAG, `Socket错误: ${JSON.stringify(err)}`); + this.responseData += `\n错误: ${JSON.stringify(err)}`; + this.isSocketBound = false; + this.socketStatus = 'Socket错误'; + }); + + // 设置监听回调 + this.udpSocket.on('listening', () => { + console.log(TAG, 'UDP Socket开始监听'); + this.responseData += '\nUDP Socket开始监听'; + }); + + // 设置关闭回调 + this.udpSocket.on('close', () => { + console.log(TAG, 'UDP Socket已关闭'); + this.responseData += '\nUDP Socket已关闭'; + this.isSocketBound = false; + this.socketStatus = 'Socket已关闭'; + }); + + } catch (error) { + console.error(TAG, `绑定失败: ${JSON.stringify(error)}`); + this.responseData = `绑定失败: ${JSON.stringify(error)}`; + this.isSocketBound = false; + this.socketStatus = '绑定失败'; + } + } + + async sendMessage() { + if (!this.udpSocket || !this.isSocketBound) { + this.responseData += '\n错误: Socket未绑定'; + return; + } + + try { + const sendAddress: NetAddress = { + address: this.serverAddress, + port: parseInt(this.serverPort), + family: 1 + }; + await this.udpSocket.send({ + data: this.message, + address: sendAddress + }); + this.responseData += `\n发送到 ${this.serverAddress}:${this.serverPort}: ${this.message}`; + console.log(TAG, `发送消息到 ${this.serverAddress}:${this.serverPort}: ${this.message}`); + } catch (error) { + console.error(TAG, `发送失败: ${JSON.stringify(error)}`); + this.responseData += `\n发送失败: ${JSON.stringify(error)}`; + } + } + + async closeSocket() { + if (!this.udpSocket) { + return; + } + + try { + await this.udpSocket.close(); + this.isSocketBound = false; + this.socketStatus = '已关闭'; + this.responseData += '\n主动关闭Socket'; + } catch (error) { + console.error(TAG, `关闭Socket失败: ${JSON.stringify(error)}`); + this.responseData += `\n关闭Socket失败: ${JSON.stringify(error)}`; + } + } + + clearResponse() { + this.responseData = ''; + } + + build() { + Column() { + Stack({ alignContent: Alignment.TopStart }) { + TitleBar({ detail: "UDP Socket验证" }) + } + + Column() { + // Socket配置区域 + Text('Socket配置') + .fontSize(18) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ top: 10, bottom: 10 }) + + Row() { + Text('本地端口: ') + .fontSize(16) + .width('30%') + TextInput({ text: this.localPort }) + .width('70%') + .borderRadius(5) + .type(InputType.Number) + .onChange((value: string) => { + this.localPort = value; + }) + } + .width('100%') + .margin({ bottom: 10 }) + + Row() { + Text('目标地址: ') + .fontSize(16) + .width('30%') + TextInput({ text: this.serverAddress }) + .width('70%') + .borderRadius(5) + .onChange((value: string) => { + this.serverAddress = value; + }) + } + .width('100%') + .margin({ bottom: 10 }) + + Row() { + Text('目标端口: ') + .fontSize(16) + .width('30%') + TextInput({ text: this.serverPort }) + .width('70%') + .borderRadius(5) + .type(InputType.Number) + .onChange((value: string) => { + this.serverPort = value; + }) + } + .width('100%') + .margin({ bottom: 10 }) + + // Socket状态 + Row() { + Text('Socket状态: ') + .fontSize(16) + .width('30%') + Text(this.socketStatus) + .fontSize(16) + .fontColor(this.isSocketBound ? Color.Green : Color.Red) + .width('70%') + } + .width('100%') + .margin({ bottom: 10 }) + + // 操作按钮 + Row() { + Button('绑定') + .width('22%') + .backgroundColor(this.isSocketBound ? Color.Gray : Color.Blue) + .enabled(!this.isSocketBound) + .onClick(() => { + this.bindSocket(); + }) + + Button('关闭') + .width('22%') + .backgroundColor(!this.isSocketBound ? Color.Gray : Color.Red) + .enabled(this.isSocketBound) + .onClick(() => { + this.closeSocket(); + }) + + Button('清空') + .width('22%') + .backgroundColor(Color.Orange) + .onClick(() => { + this.clearResponse(); + }) + + Button('发送') + .width('22%') + .backgroundColor(!this.isSocketBound ? Color.Gray : Color.Green) + .enabled(this.isSocketBound) + .onClick(() => { + this.sendMessage(); + }) + } + .width('100%') + .justifyContent(FlexAlign.SpaceBetween) + .margin({ bottom: 15 }) + + // 消息输入区域 + Text('发送消息') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ bottom: 5 }) + + TextInput({ text: this.message }) + .width('100%') + .borderRadius(5) + .onChange((value: string) => { + this.message = value; + }) + .margin({ bottom: 15 }) + + // 响应区域 + Text('响应数据') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('100%') + .margin({ bottom: 5 }) + + TextArea({ text: this.responseData }) + .width('100%') + .height('40%') + .borderRadius(5) + .backgroundColor(Color.White) + } + .width('95%') + .height('85%') + .padding(10) + } + .size({ width: ConfigData.WH_100_100, height: ConfigData.WH_100_100 }) + .backgroundColor('#F5F5F5') + } + + aboutToDisappear() { + if (this.udpSocket) { + this.udpSocket.close(); + this.udpSocket = null; + } + } +} diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/wifiCandidateTest.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/wifiCandidateTest.ets index c0a3934310517672742030127860f7899d4fc5c1..afdc8558743016bbf34da6a458199010af580a9a 100644 --- a/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/wifiCandidateTest.ets +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/subAppTest/wifiCandidateTest.ets @@ -15,7 +15,7 @@ import { TestData } from '../../entryability/model/testData' import { WifiModel } from '../../entryability/model/wifiModel' -import ConfigData from '../../utils/ConfigData'; +import ConfigData from '../../Utils/ConfigData'; import wifiManager from '@ohos.wifiManager' import { TestImageDisplay } from '../../Component/testImageDisplay'; import { PageTitle } from '../../Component/pageTitle'; diff --git a/wifi/test/wifi_testapp/entry/src/main/module.json5 b/wifi/test/wifi_testapp/entry/src/main/module.json5 index 25b30392115b046e54b8c87fa83fd5ab0154de30..a3b6cbfa88e192b3b4862cbc7678251113b10b67 100644 --- a/wifi/test/wifi_testapp/entry/src/main/module.json5 +++ b/wifi/test/wifi_testapp/entry/src/main/module.json5 @@ -52,9 +52,6 @@ } ], "requestPermissions": [ - { - "name": "ohos.permission.CAPTURE_SCREEN" - }, { "name": "ohos.permission.WRITE_MEDIA", "reason": "$string:app_name", @@ -97,26 +94,11 @@ ] } }, - { - "name": "ohos.permission.GET_WIFI_PEERS_MAC" - }, { "name": "ohos.permission.MANAGE_WIFI_CONNECTION" }, { - "name": "ohos.permission.SET_WIFI_CONFIG" - }, - { - "name": "ohos.permission.GET_WIFI_CONFIG" - }, - { - "name": "ohos.permission.GET_WIFI_LOCAL_MAC" - }, - { - "name": "ohos.permission.MANAGE_WIFI_HOTSPOT" - }, - { - "name": "ohos.permission.GET_WIFI_INFO_INTERNAL" + "name": "ohos.permission.MANAGE_ENTERPRISE_WIFI_CONNECTION" }, { "name": "ohos.permission.INTERNET" diff --git a/wifi/test/wifi_testapp/entry/src/main/resources/base/profile/main_pages.json b/wifi/test/wifi_testapp/entry/src/main/resources/base/profile/main_pages.json index 11f8a4f21e1536b4d53a0e267f6795a91b8bc6d3..88e8f3823464e960bfdd28d340af16a887c64b76 100644 --- a/wifi/test/wifi_testapp/entry/src/main/resources/base/profile/main_pages.json +++ b/wifi/test/wifi_testapp/entry/src/main/resources/base/profile/main_pages.json @@ -41,11 +41,16 @@ "pages/subAppTest/wifiScanTest", "pages/subAppTest/wifiCandidateTest", "pages/subAppTest/p2pDiscoveryTest", + "pages/subAppTest/tcpSocketTest", + "pages/subAppTest/udpSocketTest", + "pages/subAppTest/httpSocketTest", + "pages/subAppTest/httpsSocketTest", "pages/subStabilityTest/wifiSwitchStabilityTest", "pages/subStabilityTest/wifiScanStabilityTest", "pages/subStabilityTest/wifiConnectStabilityTest", "pages/subStabilityTest/hotspotStabilityTest", "pages/subStabilityTest/webLatencyTest", - "pages/newApi/AllInOneView" + "pages/newApi/AllInOneView", + "pages/newApi/QuickVerifyTest" ] } diff --git a/wifi/test/wifi_testapp/oh-package.json5 b/wifi/test/wifi_testapp/oh-package.json5 index 5552c4ed3a6267ead890700a51fdcdb5aa64fb48..4fd9bc2591c5e08117f5bcc74ea7f096a865bdc8 100644 --- a/wifi/test/wifi_testapp/oh-package.json5 +++ b/wifi/test/wifi_testapp/oh-package.json5 @@ -14,6 +14,7 @@ */ { + "modelVersion": "5.1.0", "license": "ISC", "devDependencies": { "@ohos/hypium": "1.0.6"