From 7cd27c2f52fa957ea4773e744ad162e77f46fef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=96=8C?= Date: Sat, 2 Nov 2024 15:51:59 +0800 Subject: [PATCH] =?UTF-8?q?testapp=20=E6=96=B0=E5=A2=9E=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 罗斌 add wifi/test/wifi_testapp/entry/src/main/ets/Component. Signed-off-by: yiyajun update wifi/test/wifi_testapp/entry/src/main/ets/Component/NewApiTest.ets. Signed-off-by: yiyajun update wifi/test/wifi_testapp/entry/src/main/ets/pages/homePage.ets. Signed-off-by: yiyajun 新建 newApi Signed-off-by: yiyajun add wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi. Signed-off-by: yiyajun update wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/AllInOneView.ets. Signed-off-by: yiyajun add wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/ChangeNameView.ets. 修改本地设备名称页面 Signed-off-by: yiyajun 新建 components Signed-off-by: yiyajun add wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/components. Signed-off-by: yiyajun update wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/components/NormalNav.ets. 一般的导航条 Signed-off-by: yiyajun update wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testData.ets. Signed-off-by: yiyajun update wifi/test/wifi_testapp/entry/src/main/ets/entryability/model/testDataModels.ets. Signed-off-by: yiyajun update wifi/test/wifi_testapp/entry/src/main/resources/base/profile/main_pages.json. Signed-off-by: yiyajun 去掉暂时不需要的async关键字 Signed-off-by: yiyajun 删除文件 wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/.keep Signed-off-by: yiyajun 删除文件 wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/components/.keep Signed-off-by: yiyajun 去掉没用的测试代码 Signed-off-by: yiyajun 去掉无用代码 Signed-off-by: yiyajun 删除无用的本地设备状态变量 Signed-off-by: yiyajun --- .../src/main/ets/Component/NewApiTest.ets | 24 ++ .../main/ets/entryability/model/testData.ets | 3 +- .../ets/entryability/model/testDataModels.ets | 7 + .../entry/src/main/ets/pages/homePage.ets | 4 + .../main/ets/pages/newApi/AllInOneView.ets | 325 ++++++++++++++++++ .../main/ets/pages/newApi/ChangeNameView.ets | 42 +++ .../ets/pages/newApi/components/NormalNav.ets | 12 + .../resources/base/profile/main_pages.json | 3 +- 8 files changed, 418 insertions(+), 2 deletions(-) create mode 100644 wifi/test/wifi_testapp/entry/src/main/ets/Component/NewApiTest.ets create mode 100644 wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/AllInOneView.ets create mode 100644 wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/ChangeNameView.ets create mode 100644 wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/components/NormalNav.ets diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/Component/NewApiTest.ets b/wifi/test/wifi_testapp/entry/src/main/ets/Component/NewApiTest.ets new file mode 100644 index 000000000..c9e537ac8 --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/Component/NewApiTest.ets @@ -0,0 +1,24 @@ +import { TestData } from "../entryability/model/testData" +import { AllInOneView } from "../pages/newApi/AllInOneView" +import { TestList } from "./testList" + +@Component +export struct NewApiTest { + @Provide('newApiPathStack') pathStack: NavPathStack = new NavPathStack() + private testItems!: TestData[] + + build() { + Column() { + TestList({ + testItems : this.testItems + }) + } + } + + @Builder routeMap(name: string) { + if (name === 'allInOne') { + AllInOneView() + } + + } +} \ No newline at end of file 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 dd7c5fcfe..79dfb2753 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 @@ -25,7 +25,8 @@ export enum Category { BenchMark , Stress , App , - Stability + Stability, + NewApi } let NextId = 0; 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 6ce9e8415..3e30f0c4b 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 @@ -1445,6 +1445,13 @@ const p2pManagerTestCase: WifiTest[] = [ ] const TestComposition: WifiTestComposition[] = [ + { + 'name': 'new api', + 'detail': 'wifiManager测试', + 'url': 'pages/newApi/AllInOneView', + 'image': $r('app.media.wifi'), + 'category': Category.NewApi + }, { 'name' : 'WIFI' , 'detail' : 'wifi发现' , 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 c3b5d97bb..aa2ce6a10 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 @@ -24,6 +24,7 @@ import { StabilityTest } from "../Component/stabilityTest" import { BenchmarkTest } from '../Component/benchmarkTest'; import { StressTest } from "../Component/stressTest" import ConfigData from '../utils/ConfigData'; +import { NewApiTest } from '../Component/NewApiTest'; const TAG = '[homePage]' @@ -38,6 +39,9 @@ struct TestCategory { build() { Stack() { Tabs() { + 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/AllInOneView.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/AllInOneView.ets new file mode 100644 index 000000000..11076bfd0 --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/AllInOneView.ets @@ -0,0 +1,325 @@ +import { wifiManager } from '@kit.ConnectivityKit' +import { ChangeNameView } from './ChangeNameView' +import { NormalNav } from './Components/NormalNav' + + +const TAG = '[AllInOneView]' + +/*CONNECTED 0 连接状态。 +INVITED 1 邀请状态。 +FAILED 2 失败状态。 +AVAILABLE 3 可用状态。 +UNAVAILABLE 4 不可用状态。*/ +const P2PDeviceStatusInfo = ['连接状态','邀请状态', '失败状态', '可用状态', '不可用状态'] +@Observed +class P2PDevice { + name: string + address: string = '' + status: string = '' + + constructor(name: string) { + this.name = name + this.status = '' + } + + static from(d: wifiManager.WifiP2pDevice):P2PDevice { + let res = new P2PDevice(d.deviceName) + res.name = d.deviceName + res.address = d.deviceAddress + res.status = P2PDeviceStatusInfo[d.deviceStatus] + return res + } +} + +@Observed +class P2PGroup { + // 群组名称 + name: string + networkId: number + // 是否为群主 + /*isGo: boolean + // 群主设备信息 + ownerInfo: P2PDevice + // 密钥 + passphrase: string + // 接口名称 + interface: string + // 网络id + networkId: number + // 群组频率 + frequency: number + // 接入设备列表 + clientDevices: P2PDevice[] + // 群主ip地址 + goIpAddress: string*/ + + + constructor(group: wifiManager.WifiP2pGroupInfo) { + this.name = group.groupName + this.networkId = group.networkId + /*this.isGo = group.isP2pGo + this.ownerInfo = P2PDevice.from(group.ownerInfo) + this.passphrase = group.passphrase + this.interface = group.interface + this.networkId = group.networkId + this.frequency = group.frequency + this.clientDevices = group.clientDevices.map(P2PDevice.from) + this.goIpAddress = group.goIpAddress*/ + } + + static from(g: wifiManager.WifiP2pGroupInfo): P2PGroup { + let res = new P2PGroup(g) + return res + } +} + + +enum P2PConnectStatus { + Normal, + Connecting, + CancelConnecting, + Disconnecting +} + +@Entry +@Component +export struct AllInOneView { + + @Provide('p2pPathInfo') pathInfo:NavPathStack = new NavPathStack() + @State allDevice: P2PDevice[] = [] + @State localDevice: P2PDevice | null = null + @State allGroup: P2PGroup[] = [] + @State p2pIsLinked: boolean = false + @State isP2pDiscoverEnable:boolean = true + @State p2pLinkedInfo: wifiManager.WifiP2pLinkedInfo | null = null + connectStatus: P2PConnectStatus = P2PConnectStatus.Normal + + async aboutToAppear() { + this.addP2PListener() + try { + wifiManager.startDiscoverDevices() + } catch (e) { + JSON.stringify(e) + } + try { + this.p2pLinkedInfo = await wifiManager.getP2pLinkedInfo() + console.log(TAG, 'linkedInfo: ', JSON.stringify(this.p2pLinkedInfo)) + } catch (e) { + console.log(JSON.stringify(e)) + } + try { + let gps = await wifiManager.getP2pGroups() + console.log(TAG, 'Groups: ', JSON.stringify(gps)) + this.allGroup = gps.map(P2PGroup.from) + let ldv = await wifiManager.getP2pLocalDevice() + this.localDevice = P2PDevice.from(ldv) + console.log(TAG, JSON.stringify(this.localDevice)) + } catch (e) { + console.log(TAG, 'err getP2pLocalDevice', JSON.stringify(e)) + } + + } + + aboutToDisappear(): void { + this.removeP2PListener() + } + + addP2PListener() { + wifiManager.on('p2pPersistentGroupChange', async () => { + console.log(TAG, 'p2pPersistentGroupChange') + let gps = await wifiManager.getP2pGroups() + this.allGroup = gps.map(P2PGroup.from) + }) + wifiManager.on('p2pDiscoveryChange', (status) => { + console.log(TAG, `p2pDiscoveryChange: ${status}`) + this.isP2pDiscoverEnable = (status === 1) + }) + wifiManager.on('p2pPeerDeviceChange', (_: wifiManager.WifiP2pDevice[]) => { + console.log(TAG, 'p2pPeerDeviceChange:', JSON.stringify(_)) + this.allDevice = _.map(P2PDevice.from) + }) + wifiManager.on('p2pConnectionChange', async (result: wifiManager.WifiP2pLinkedInfo) => { + console.log(TAG, 'p2pConnectionChange', JSON.stringify(result)) + if (result.connectState === 0) { + wifiManager.startDiscoverDevices() + } else { + let gp = await wifiManager.getCurrentGroup() + console.log(TAG, 'currentGroup:', JSON.stringify(gp)) + } + }) + + } + + removeP2PListener() { + wifiManager.off('p2pPersistentGroupChange') + wifiManager.off('p2pDiscoveryChange') + wifiManager.off('p2pPeerDeviceChange') + wifiManager.off('p2pConnectionChange') + } + + @Builder localDeviceHeaer() { + Row() { + Text('我的设备') + .fontWeight(FontWeight.Bold) + }.height(60) + } + + @Builder deviceHeader() { + Row() { + Text('可用设备') + .fontWeight(FontWeight.Bold) + } + .height(60) + } + + @Builder groupHeader() { + Row() { + Text('已保存的群组') + .fontWeight(FontWeight.Bold) + } + .height(60) + } + + build() { + Navigation(this.pathInfo) { + Column() { + NormalNav({title: '新接口测试'}) + Button(this.isP2pDiscoverEnable ? '关闭扫描' : '开启扫描') + .onClick(() => { + if (this.isP2pDiscoverEnable) { + wifiManager.stopDiscoverDevices() + } else { + wifiManager.startDiscoverDevices() + } + }) + List({ space: 10 }) { + if (this.localDevice !== null) { + ListItemGroup({ header: this.localDeviceHeaer(), style: ListItemGroupStyle.CARD }) { + ListItem({style: ListItemStyle.CARD}) { + DeviceItemView({device: this.localDevice}) + }.height(110) + .onClick(() => { + this.pathInfo.pushPathByName('changeName', null, async () => { + console.log('changeNameBack') + let device = await wifiManager.getP2pLocalDevice() + this.localDevice = P2PDevice.from(device) + }, true) + }) + } + } + ListItemGroup({ header: this.deviceHeader, style: ListItemGroupStyle.CARD }) { + ForEach(this.allDevice, (device: P2PDevice) => { + ListItem({style: ListItemStyle.CARD}) { + DeviceItemView({device: device}) + } + .height(110) + .onClick(async () => { + if (this.p2pLinkedInfo && device.status === '连接状态') { + this.connectStatus = P2PConnectStatus.Disconnecting + try { + wifiManager.removeGroup() + } catch (e) { + console.log("removeGroup err: " + JSON.stringify(e)) + } + } else if (device.status === '邀请状态'){ + this.connectStatus = P2PConnectStatus.CancelConnecting + try { + wifiManager.p2pCancelConnect() + } catch (e) { + console.log("cancelConnect err: " + JSON.stringify(e)) + } + } else { + this.connectStatus = P2PConnectStatus.Connecting + await this.connectToDevice(device) + } + + }) + }) + }.divider({strokeWidth: 2, startMargin: 10, endMargin: 10}) + ListItemGroup({ header: this.groupHeader, style: ListItemGroupStyle.CARD }) { + ForEach(this.allGroup, (group: P2PGroup) => { + ListItem({ style: ListItemStyle.CARD }) { + GroupItemView({group: group}) + }.onClick(() => { + wifiManager.deletePersistentGroup(group.networkId) + }) + }) + } + } + .layoutWeight(1) + .margin({ top: 10 }) + } + .backgroundColor('#F2F2F2') + .width('100%') + .height('100%') + } + .hideTitleBar(true) + .navDestination(this.routeMap) + + } + + async connectToDevice(device: P2PDevice) { + console.log('connect to device:', JSON.stringify(device)) + let config:wifiManager.WifiP2PConfig = { + deviceAddress: device.address, + netId:-2, + passphrase:"", + groupName: "", + goBand:0, + } + + try { + wifiManager.p2pConnect(config) + wifiManager.startDiscoverDevices() + } catch (e) { + console.log(TAG, 'connect and create group error:', JSON.stringify(e)) + } + } + + @Builder routeMap(name: string) { + if (name === 'changeName') { + ChangeNameView() + } + + } +} + +@Component +struct DeviceItemView { + + @ObjectLink device: P2PDevice + + build() { + Row() { + Column({space: 10}) { + Text(`设备名称:${this.device.name}`) + Text(`设备地址:${this.device.address}`) + Text(`设备状态:${this.device.status}`) + }.alignItems(HorizontalAlign.Start) + Blank().layoutWeight(1) + if (this.device.status === '邀请状态') { + LoadingProgress() + .width(40) + .height(40) + .enableLoading(true) + } + } + .padding({left: 15, right: 15}) + } +} + +@Component +struct GroupItemView { + + @ObjectLink group: P2PGroup + + build() { + Row() { + Column({space: 10}) { + Text(`组名称:${this.group.name}`) + }.alignItems(HorizontalAlign.Start) + Blank().layoutWeight(1) + } + .padding({left: 15, right: 15}) + } +} \ No newline at end of file diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/ChangeNameView.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/ChangeNameView.ets new file mode 100644 index 000000000..52159a482 --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/ChangeNameView.ets @@ -0,0 +1,42 @@ +import { NormalNav } from "./Components/NormalNav" +import { wifiManager } from "@kit.ConnectivityKit" + +@Component +export struct ChangeNameView { + + @Consume('p2pPathInfo') pathInfo: NavPathStack + @State device: null | wifiManager.WifiP2pDevice = null + @State deviceStr: string = '' + + async aboutToAppear() { + this.device = await wifiManager.getP2pLocalDevice() + this.deviceStr = this.device.deviceName + } + + build() { + NavDestination() { + Column() { + NormalNav({title: '设备名称'}) + TextInput({text: $$this.deviceStr}) + Text('使用蓝牙互联,WLAN直连,热点共享和USB连接时,其他设备将看到此名称') + Blank() + .layoutWeight(1) + Row() { + Button('取消') + .onClick(() => { + this.pathInfo.pop() + }) + Button('确定') + .onClick(() => { + if (this.deviceStr.length > 0) { + wifiManager.setDeviceName(this.deviceStr) + this.pathInfo.pop(null, true) + } + }) + } + } + } + .hideTitleBar(true) + } + +} \ No newline at end of file diff --git a/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/components/NormalNav.ets b/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/components/NormalNav.ets new file mode 100644 index 000000000..ae4f8bccc --- /dev/null +++ b/wifi/test/wifi_testapp/entry/src/main/ets/pages/newApi/components/NormalNav.ets @@ -0,0 +1,12 @@ +@Component +export struct NormalNav { + title: string = '' + + build() { + Row() { + Text(this.title) + .margin({ left: 20 }) + } + .height(50) + } +} \ No newline at end of file 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 b1144b38d..11f8a4f21 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 @@ -45,6 +45,7 @@ "pages/subStabilityTest/wifiScanStabilityTest", "pages/subStabilityTest/wifiConnectStabilityTest", "pages/subStabilityTest/hotspotStabilityTest", - "pages/subStabilityTest/webLatencyTest" + "pages/subStabilityTest/webLatencyTest", + "pages/newApi/AllInOneView" ] }