From 60dead3accb10d0f9cf07b79360e1d04e9ac4de3 Mon Sep 17 00:00:00 2001 From: quanli Date: Thu, 17 Nov 2022 18:39:58 +0800 Subject: [PATCH] quanli1@huawei.com Signed-off-by: quanli --- .../wifi_p2p/src/main/js/test/List.test.js | 9 +- .../main/js/test/WifiManagerP2PEvent.test.js | 192 ++++++ .../js/test/WifiManagerP2PFunction.test.js | 561 +++++++++++++++++ .../src/main/js/test/List.test.js | 13 +- .../main/js/test/WifiCandidateNetWork.test.js | 23 +- .../test/WifiManagerCandidateNetWork.test.js | 376 +++++++++++ .../js/test/WifiManagerStationEvent.test.js | 583 ++++++++++++++++++ .../test/WifiManagerStationFunctions.test.js | 331 ++++++++++ 8 files changed, 2075 insertions(+), 13 deletions(-) create mode 100644 communication/wifi_p2p/src/main/js/test/WifiManagerP2PEvent.test.js create mode 100644 communication/wifi_p2p/src/main/js/test/WifiManagerP2PFunction.test.js create mode 100644 communication/wifi_standard/src/main/js/test/WifiManagerCandidateNetWork.test.js create mode 100644 communication/wifi_standard/src/main/js/test/WifiManagerStationEvent.test.js create mode 100644 communication/wifi_standard/src/main/js/test/WifiManagerStationFunctions.test.js diff --git a/communication/wifi_p2p/src/main/js/test/List.test.js b/communication/wifi_p2p/src/main/js/test/List.test.js index d5770730c..f8f1acb84 100644 --- a/communication/wifi_p2p/src/main/js/test/List.test.js +++ b/communication/wifi_p2p/src/main/js/test/List.test.js @@ -13,8 +13,13 @@ * limitations under the License. */ import actsWifiFunctionTest from './WifiP2PFunction.test.js' +import actsWifiManagerFunctionTest from './WifiManagerP2PFunction.test.js' import actsWifiEventTest from './WifiP2PEvent.test.js' +import actsWifiManagerEventTest from './WifiManagerP2PEvent.test.js' export default function testsuite() { -actsWifiFunctionTest() -actsWifiEventTest() + actsWifiFunctionTest() + actsWifiManagerFunctionTest() + actsWifiEventTest() + actsWifiManagerEventTest() } + diff --git a/communication/wifi_p2p/src/main/js/test/WifiManagerP2PEvent.test.js b/communication/wifi_p2p/src/main/js/test/WifiManagerP2PEvent.test.js new file mode 100644 index 000000000..5fe3c6162 --- /dev/null +++ b/communication/wifi_p2p/src/main/js/test/WifiManagerP2PEvent.test.js @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' + +import wifiMg from '@ohos.wifiManager' + +function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) +} + +function checkWifiPowerOn(){ + console.info("[wifi_test]wifi status:" + wifiMg.isWifiActive()); +} + +let groupOwnerBand = { + GO_BAND_AUTO : 0, + GO_BAND_2GHZ : 1, + GO_BAND_5GHZ : 2, +} + +export default function actsWifiManagerEventTest() { + describe('actsWifiManagerEventTest', function () { + beforeEach(function () { + console.info("[wifi_test]beforeEach start" ); + checkWifiPowerOn(); + }) + afterEach(async function () { + console.info("[wifi_test]afterEach start" ); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0008 + * @tc.name testp2pStateChange + * @tc.desc Test p2pStateChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0008', 0, async function (done) { + let p2pState = "p2pStateChange"; + let p2pStateChangeCallback = result => { + console.info("[wifi_test]p2pStateChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(p2pState, p2pStateChangeCallback); + await sleep(3000); + wifiMg.off(p2pState, p2pStateChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0009 + * @tc.name testp2pConnectionChange + * @tc.desc Test p2pConnectionChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0009', 0, async function (done) { + let p2pConnectionState = "p2pConnectionChange"; + let p2pConnectionChangeCallback = result => { + console.info("[wifi_test]p2pConnectionChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(p2pConnectionState, p2pConnectionChangeCallback); + let p2pConnectState = { + DISCONNECTED :0, + CONNECTED : 1, + }; + let wifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "AAAZZZ456", + goBand : 0 + }; + let connectResult = wifiMg.p2pConnect(wifiP2PConfig); + await wifiMg.getP2pLinkedInfo() + .then(data => { + let resultLength = Object.keys(data).length; + console.info("[wifi_test]getP2pLinkedInfo promise result : " + JSON.stringify(data)); + expect(true).assertEqual(resultLength!=0); + done() + }); + await sleep(2000); + wifiMg.off(p2pConnectionState, p2pConnectionChangeCallback); + let removeGroupResult = wifiMg.removeGroup(); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0012 + * @tc.name testp2pDeviceChange + * @tc.desc Test p2pDeviceChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0012', 0, async function (done) { + let p2pDeviceState = "p2pDeviceChange"; + let p2pDeviceChangeCallback = result => { + console.info("[wifi_test]p2pDeviceChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(p2pDeviceState, p2pDeviceChangeCallback); + await sleep(3000); + wifiMg.off(p2pDeviceState, p2pDeviceChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0010 + * @tc.name testp2pPeerDeviceChange + * @tc.desc Test p2pPeerDeviceChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0010', 0, async function (done) { + let p2pPeerDeviceState = "p2pPeerDeviceChange"; + let p2pPeerDeviceChangeCallback = result => { + console.info("[wifi_test]p2pPeerDeviceChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(p2pPeerDeviceState, p2pPeerDeviceChangeCallback); + let startDiscover = wifiMg.startDiscoverDevices(); + await sleep(3000); + let stopDiscover = wifiMg.stopDiscoverDevices(); + wifiMg.off(p2pPeerDeviceState, p2pPeerDeviceChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0013 + * @tc.name testp2pPersistentGroupChange + * @tc.desc Test p2pPersistentGroupChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0013', 0, async function (done) { + let p2pGroupState = "p2pPersistentGroupChange"; + let p2pPersistentGroupChangeCallback = () => { + console.info("[wifi_test]p2pPersistentGroupChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(p2pGroupState, p2pPersistentGroupChangeCallback); + let WifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -2, + passphrase : "12345678", + groupName : "AAAZZZ123", + goBand : 0, + }; + let createGroupResult = wifiMg.createGroup(WifiP2PConfig); + await (2000); + await wifiMg.getCurrentGroup() + .then(data => { + let resultLength = Object.keys(data).length; + console.info("[wifi_test] getCurrentGroup promise result -> " + JSON.stringify(data)); + expect(true).assertEqual(resultLength!=0); + }); + wifiMg.off(p2pGroupState, p2pPersistentGroupChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0011 + * @tc.name testpp2pDiscoveryChange + * @tc.desc Test p2pDiscoveryChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0011', 0, async function (done) { + let p2pPeerDeviceState = "p2pDiscoveryChange"; + let p2pDiscoveryChangeCallback = result => { + console.info("[wifi_test]p2pDiscoveryChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(p2pPeerDeviceState, p2pDiscoveryChangeCallback); + let startDiscover = wifiMg.startDiscoverDevices(); + await sleep(3000); + let stopDiscover = wifiMg.stopDiscoverDevices(); + wifiMg.off(p2pPeerDeviceState, p2pDiscoveryChangeCallback); + done(); + }) + console.log("*************[wifi_test] start wifi js unit test end*************"); + }) +} + diff --git a/communication/wifi_p2p/src/main/js/test/WifiManagerP2PFunction.test.js b/communication/wifi_p2p/src/main/js/test/WifiManagerP2PFunction.test.js new file mode 100644 index 000000000..b64bc9484 --- /dev/null +++ b/communication/wifi_p2p/src/main/js/test/WifiManagerP2PFunction.test.js @@ -0,0 +1,561 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' + +import wifiMg from '@ohos.wifiManager' + +function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) +} + +function checkWifiPowerOn(){ + console.info("[wifi_test]/wifi status:" + wifiMg.isWifiActive()); +} + +let groupOwnerBand = { + GO_BAND_AUTO : 0, + GO_BAND_2GHZ : 1, + GO_BAND_5GHZ : 2, +} + +export default function actsWifiManagerFunctionTest() { + describe('actsWifiManagerFunctionTest', function () { + beforeEach(function () { + console.info("[wifi_test]beforeEach start" ); + checkWifiPowerOn(); + }) + afterEach(async function () { + console.info("[wifi_test]afterEach start" ); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0003 + * @tc.name testCreateGroup + * @tc.desc Test createGroup and getCurrentGroup API Function + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_P2P_0003', 0, async function(done) { + let wifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "AAAZZZ123", + goBand : groupOwnerBand.GO_BAND_2GHZ, + }; + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + let createGroupResult = wifiMg.createGroup(wifiP2PConfig); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test]getCurrentGroup promise result -> " + JSON.stringify(data)); + expect(true).assertEqual(data.groupName == wifiP2PConfig.groupName); + }); + function getCurrentGroupResult(){ + return new Promise((resolve, reject) => { + wifiMg.getCurrentGroup( + (err, result) => { + if(err) { + console.info("[wifi_test]failed to get getCurrentGroup:" + JSON.stringify(err)); + expect().assertFail(); + } + console.info("[wifi_test]getCurrentGroup callback:" + JSON.stringify(result)); + console.info("isP2pGo: " + result.isP2pGo + + "deviceName: " + result.ownerInfo.deviceName + + "deviceAddress: " + result.ownerInfo.deviceAddress + + "primaryDeviceType: " + result.ownerInfo.primaryDeviceType + + "deviceStatus: " + result.ownerInfo.deviceStatus + + "groupCapabilitys: " + result.ownerInfo.groupCapabilitys + + "passphrase: " + result.passphrase + "interface: "+ result.interface + + "groupName: " + result.groupName + + "frequency: " + result.frequency + "goIpAddress: " + result.goIpAddress); + console.info("[wifi_test] clientDevices:" + JSON.stringify(result.clientDevices)); + console.info("[wifi_test] ownerInfo:" + JSON.stringify(result.WifiP2pDevice)); + resolve(); + }); + }); + } + await getCurrentGroupResult(); + let removeGroupResult = wifiMg.removeGroup(); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0004 + * @tc.name testCreateGroup + * @tc.desc Test createGroup-Setting a 7-bit Key Function. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_P2P_0004', 0, async function (done) { + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + let wifiP2PConfig = { + deviceAddress: "00:00:00:00:00:00", + netId: -1, + passphrase: "1234567", + groupName: "test_pass", + goBand: groupOwnerBand.GO_BAND_2GHZ, + }; + let createGroupResult = wifiMg.createGroup(wifiP2PConfig); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result :" + JSON.stringify(data)); + expect(true).assertEqual(data.networkId == -999); + }); + let removeGroupResult = wifiMg.removeGroup(); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0104 + * @tc.name testCreateGroup + * @tc.desc Test createGroup-Key setting: Chinese, English, and characters Function. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_P2P_0104', 0, async function (done) { + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + let wifiP2PConfig = { + deviceAddress: "00:00:00:00:00:00", + netId: -1, + passphrase: "123@%abcD", + groupName: "test_pass1", + goBand: groupOwnerBand.GO_BAND_2GHZ, + }; + let createGroupResult = wifiMg.createGroup(wifiP2PConfig); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test]getCurrentGroup promise result : " + JSON.stringify(data)); + expect(true).assertEqual(data.passphrase == wifiP2PConfig.passphrase); + }); + let removeGroupResult = wifiMg.removeGroup(); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0204 + * @tc.name testCreateGroup + * @tc.desc Test createGroup-Key setting 64 bit Function. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_P2P_0204', 0, async function (done) { + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + let wifiP2PConfig = { + deviceAddress: "00:00:00:00:00:00", + netId: -1, + passphrase: "abc345678901234567890123456789012345678901234567890123456789012", + groupName: "test_pass2", + goBand: groupOwnerBand.GO_BAND_2GHZ, + }; + let createGroupResult = wifiMg.createGroup(wifiP2PConfig); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test]getCurrentGroup promise result : " + JSON.stringify(data)); + expect(true).assertEqual(data.passphrase == wifiP2PConfig.passphrase); + }); + let removeGroupResult = wifiMg.removeGroup(); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0304 + * @tc.name testCreateGroup + * @tc.desc Test createGroup-Key setting 65 bitsFunction. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_P2P_0304', 0, async function (done) { + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + let wifiP2PConfig = { + deviceAddress: "00:00:00:00:00:00", + netId: -1, + passphrase: "abc3456789012345678901234567890123456789012345678901234567890123", + groupName: "test_pass3", + goBand: groupOwnerBand.GO_BAND_2GHZ, + }; + let createGroupResult = wifiMg.createGroup(wifiP2PConfig); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test]getCurrentGroup promise result :" + JSON.stringify(data)); + expect(true).assertEqual(data.passphrase != wifiP2PConfig.passphrase); + }); + let removeGroupResult = wifiMg.removeGroup(); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0007 + * @tc.name testCreateGroup + * @tc.desc Test createGroup-2.4 GHz frequency band setting Function + * @tc.type Function + * @tc.level Level 0 + */ + it('SUB_Communication_WiFi_XTS_P2P_0007', 0, async function(done) { + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + let wifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "test_band1", + goBand : groupOwnerBand.GO_BAND_2GHZ, + }; + let createGroupResult = wifiMg.createGroup(wifiP2PConfig); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test]getCurrentGroup promise result :" + JSON.stringify(data)); + expect(true).assertEqual(2412 < data.frequency < 2484 ); + }); + let removeGroupResult = wifiMg.removeGroup(); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0107 + * @tc.name testCreateGroup + * @tc.desc Test createGroup-5 GHz frequency band setting Function + * @tc.type Function + * @tc.level Level 0 + */ + it('SUB_Communication_WiFi_XTS_P2P_0107', 0, async function(done) { + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + try { + let wifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "test_band2", + goBand : groupOwnerBand.GO_BAND_5GHZ, + }; + let createGroupResult = wifiMg.createGroup(wifiP2PConfig); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result :" + JSON.stringify(data)); + expect(true).assertEqual(5160 < data.frequency < 5865); + }); + let removeGroupResult = await wifiMg.removeGroup(); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + }catch(error){ + console.info("[wifi_test]createGroup 5G goBand result : " + JSON.stringify(error.message)); + expect(true).assertEqual( (JSON.stringify(error.message)) !=null); + } + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0207 + * @tc.name testCreateGroup + * @tc.desc Test createGroup-Auto frequency band setting Function + * @tc.type Function + * @tc.level Level 0 + */ + it('SUB_Communication_WiFi_XTS_P2P_0207', 0, async function(done) { + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + try { + let wifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "test_band3", + goBand : groupOwnerBand.GO_BAND_AUTO, + }; + let createGroupResult = wifiMg.createGroup(wifiP2PConfig); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test]getCurrentGroup promise result : " + JSON.stringify(data)); + expect(true).assertEqual(data.frequency != null ); + }); + let removeGroupResult = await wifiMg.removeGroup(); + await sleep(2000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + }catch(error){ + console.info("[wifi_test]createGroup auto goBand result : " + JSON.stringify(error.message)); + expect(true).assertEqual( (JSON.stringify(error.message)) !=null); + } + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0009 + * @tc.name testP2pCancelConnect + * @tc.desc Test p2pCancelConnect Group API functionality. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_P2P_0009', 0, async function (done) { + let wifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "AAAZZZ456", + goBand : groupOwnerBand.GO_BAND_2GHZ, + }; + let p2pConnectResult = wifiMg.p2pConnect(wifiP2PConfig); + console.info("[wifi_test]test p2pConnect successful." ); + let p2pCancelResult = wifiMg.p2pDisonnect(); + await sleep(2000); + console.info("[wifi_test]test p2pDisonnect successful." ); + let removeGroupResult = wifiMg.removeGroup(); + console.info("[wifi_test]test removeGroup successful " ); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0011 + * @tc.name testRemoveGroup + * @tc.desc Test remove a nonexistent group. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_P2P_0011', 0, async function (done) { + let removeGroupResult = wifiMg.removeGroup(10000); + await wifiMg.getCurrentGroup() + .then(data => { + console.info("[wifi_test] getCurrentGroup promise result1 :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName == null); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0002 + * @tc.name testP2pLocalDevice + * @tc.desc Test get P2pLocalDevice API functionality. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_P2P_0002', 0, async function (done) { + await wifiMg.getP2pLocalDevice() + .then(data => { + console.info("[wifi_test]getP2pLocalDevice promise result :" + JSON.stringify(data)); + expect(true).assertEqual(data.deviceName !=null); + }).catch((error) => { + console.info("[wifi_test]getP2pLocalDevice promise error." + JSON.stringify(error)); + expect().assertFail(); + }); + function getP2pLocal(){ + return new Promise((resolve, reject) => { + wifiMg.getP2pLocalDevice( + (err, ret) => { + if(err) { + console.info("[wifi_test]getP2pLocalDevice callback failed : " + JSON.stringify(err)); + return; + } + console.info("[wifi_test]getP2pLocalDevice callback result: " + JSON.stringify(ret)); + console.info("deviceName: " + ret.deviceName + "deviceAddress: " + + ret.deviceAddress + "primaryDeviceType: " + ret.primaryDeviceType + + "deviceStatus: " + ret.deviceStatus + "groupCapabilitys: " + + ret.groupCapabilitys ); + resolve(); + }); + }); + } + await getP2pLocal(); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0010 + * @tc.name testGetP2pLinkedInfo + * @tc.desc Test getP2pLinkedInfo API functionality + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_Communication_WiFi_XTS_P2P_0010', 0, async function(done) { + let p2pConnectState = { + DISCONNECTED :0, + CONNECTED : 1, + }; + await wifiMg.getP2pLinkedInfo() + .then(data => { + let resultLength = Object.keys(data).length; + console.info("[wifi_test]getP2pLinkedInfo promise result :" + JSON.stringify(data)); + expect(true).assertEqual(resultLength!=0); + done() + }); + function getP2pLinkedInfoResult(){ + return new Promise((resolve, reject) => { + wifiMg.getP2pLinkedInfo( + (err, result) => { + if(err) { + console.info("[wifi_test]failed to getP2pLinkedInfo callback:" + JSON.stringify(err)); + return; + } + let resultLength = Object.keys(result).length; + console.info("[wifi_test]getP2pLinkedInfo callback:" + JSON.stringify(resultLength)); + console.info("connectState: " + result.connectState + + "isGroupOwner: " + result.isGroupOwner + + "groupOwnerAddr: " + result.groupOwnerAddr); + expect(true).assertEqual(resultLength!=0); + resolve(); + }); + }); + } + await getP2pLinkedInfoResult(); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0001 + * @tc.name testGetP2pPeerDevices + * @tc.desc Test getP2pPeerDevices promise API functionality + * @tc.type Function + * @tc.level Level 0 + */ + it('SUB_Communication_WiFi_XTS_P2P_0001', 0, async function(done){ + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + let startDiscover = wifiMg.startDiscoverDevices(); + await sleep(2000); + await wifiMg.getP2pPeerDevices() + .then((data) => { + let resultLength = Object.keys(data).length; + console.info("[wifi_test]getP2pPeerDevices promise result -> " + JSON.stringify(data)); + expect(true).assertEqual(resultLength >= 0); + }).catch((error) => { + console.info("[wifi_test]getP2pPeerDevices promise then error." + JSON.stringify(error)); + expect().assertFail(); + }); + let stopDiscover = wifiMg.stopDiscoverDevices(); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_P2P_0101 + * @tc.name testGetP2pPeerDevices + * @tc.desc Test getP2pPeerDevices callback API functionality + * @tc.type Function + * @tc.level Level 0 + */ + it('SUB_Communication_WiFi_XTS_P2P_0101', 0, async function(done){ + let p2pDeviceStatus = { + CONNECTED : 0, + INVITED : 1, + FAILED : 2, + AVAILABLE : 3, + UNAVAILABLE : 4, + }; + console.log("[wifi_test]check the state of wifi: " + wifiMg.isWifiActive()); + expect(wifiMg.isWifiActive()).assertTrue(); + let startDiscover = wifiMg.startDiscoverDevices(); + await sleep(2000); + function getP2pPeerDevicesResult(){ + return new Promise((resolve, reject) => { + wifiMg.getP2pPeerDevices( + (err, result) => { + if(err) { + console.error('[wifi_test]failed to getP2pPeerDevices :' + JSON.stringify(err)); + } + console.info("[wifi_test] getP2pPeerDevices callback result :" + JSON.stringify(result)); + let len = Object.keys(result).length; + for (let j = 0; j < len; ++j) { + console.info("deviceName: " + result[j].deviceName + + "deviceAddress: " + result[j].deviceAddress + + "primaryDeviceType: " + result[j].primaryDeviceType + + "deviceStatus: " + result[j].deviceStatus + + "groupCapabilitys: " + result[j].groupCapabilitys ); + if(result[j].deviceStatus ==p2pDeviceStatus.UNAVAILABLE){ + console.info("deviceStatus: " + result[j].deviceStatus); + } + if(result[j].deviceStatus ==p2pDeviceStatus.CONNECTED){ + console.info("deviceStatus: " + result[j].deviceStatus); + } + if(result[j].deviceStatus ==p2pDeviceStatus.INVITED){ + console.info("deviceStatus: " + result[j].deviceStatus); + } + if(result[j].deviceStatus ==p2pDeviceStatus.FAILED){ + console.info("deviceStatus: " + result[j].deviceStatus); + } + if(result[j].deviceStatus ==p2pDeviceStatus.AVAILABLE){ + console.info("deviceStatus: " + result[j].deviceStatus); + } + } + resolve(); + }); + }); + } + await getP2pPeerDevicesResult(); + done(); + }); + console.log("*************[wifi_test] start wifi js unit test end*************"); + }) +} + diff --git a/communication/wifi_standard/src/main/js/test/List.test.js b/communication/wifi_standard/src/main/js/test/List.test.js index 1e7e3fd6c..3c360b682 100644 --- a/communication/wifi_standard/src/main/js/test/List.test.js +++ b/communication/wifi_standard/src/main/js/test/List.test.js @@ -13,12 +13,17 @@ * limitations under the License. */ import actsWifiCandidateNetWorkTest from './WifiCandidateNetWork.test.js' +import actsWifiManagerCandidateNetWorkTest from './WifiManagerCandidateNetWork.test.js' import actsWifiFunctionsTest from './WifiStationFunctions.test.js' +import actsWifiManagerFunctionsTest from './WifiManagerStationFunctions.test.js' import actsWifiEventTest from './WifiStationEvent.test.js' +import actsWifiManagerEventTest from './WifiManagerStationEvent.test.js' export default function testsuite() { -actsWifiCandidateNetWorkTest() -actsWifiFunctionsTest() -actsWifiEventTest() + actsWifiCandidateNetWorkTest() + actsWifiManagerCandidateNetWorkTest() + actsWifiFunctionsTest() + actsWifiManagerFunctionsTest() + actsWifiEventTest() + actsWifiManagerEventTest() } - diff --git a/communication/wifi_standard/src/main/js/test/WifiCandidateNetWork.test.js b/communication/wifi_standard/src/main/js/test/WifiCandidateNetWork.test.js index 1bd3c945f..af4d65193 100644 --- a/communication/wifi_standard/src/main/js/test/WifiCandidateNetWork.test.js +++ b/communication/wifi_standard/src/main/js/test/WifiCandidateNetWork.test.js @@ -93,10 +93,8 @@ export default function actsWifiCandidateNetWorkTest() { console.error('[wifi_test]add OPEN CandidateConfig promise failed -> ' + JSON.stringify(error)); expect(false).assertFalse(); }); - let getconfig = wifi.getCandidateConfigs(); console.info("[wifi_test]wifi get OPEN CandidateConfigs result : " + JSON.stringify(getconfig)); - let wifiDeviceConfig1 = { "ssid": "TEST_WEP", "bssid": "", @@ -104,7 +102,6 @@ export default function actsWifiCandidateNetWorkTest() { "isHiddenSsid": false, "securityType": wifiSecurityType.WIFI_SEC_TYPE_WEP, }; - await wifi.addCandidateConfig(wifiDeviceConfig1) .then(netWorkId => { console.info("[wifi_test]add WEP CandidateConfig promise : " + JSON.stringify(netWorkId)); @@ -154,7 +151,6 @@ export default function actsWifiCandidateNetWorkTest() { await wifi.removeCandidateConfig(networkId) .then(ret => { console.info("[wifi_test]remove CandidateConfig promise:" + JSON.stringify(ret)); - expect(false).assertFalse(); let getCandidate = wifi.getCandidateConfigs(); console.info("[wifi_test]wifi get CandidateConfigs result : " + JSON.stringify(getCandidate)); console.info("[wifi_test]wifi getconfig.length result : " + JSON.stringify(getCandidate.length)); @@ -195,7 +191,6 @@ export default function actsWifiCandidateNetWorkTest() { await wifi.removeCandidateConfig(networkId) .then(ret => { console.info("[wifi_test]remove CandidateConfig promise" + JSON.stringify(ret)); - expect(false).assertFalse(); let getconfig1 = wifi.getCandidateConfigs(); console.info("[wifi_test]wifi get CandidateConfigs result : " + JSON.stringify(getconfig1)); console.info("[wifi_test]wifi getconfig.length result : " + JSON.stringify(getconfig1.length)); @@ -339,7 +334,7 @@ export default function actsWifiCandidateNetWorkTest() { WIFI_SEC_TYPE_SAE: 4, } let wifiDeviceConfig = { - "ssid": "HONOR 3000", + "ssid": "TEST_connect", "bssid": "", "preSharedKey": "12345678", "isHiddenSsid": false, @@ -354,7 +349,7 @@ export default function actsWifiCandidateNetWorkTest() { expect().assertFail(); }); let getCandidateResult = wifi.getCandidateConfigs(); - console.info("[wifi_test]wifi get CandidateConfigs result : " + JSON.stringify(getCandidateResult)); + console.info("[wifi_test]wifi get CandidateConfigs result : " + JSON.stringify(getCandidateResult)); let connectToCandidateResult = wifi.connectToCandidateConfig(getCandidateResult[0].netId); console.info("[wifi_test]connect To CandidateConfig result : " + JSON.stringify(connectToCandidateResult)); await sleep(3000); @@ -366,6 +361,20 @@ export default function actsWifiCandidateNetWorkTest() { console.info("[wifi_test]promise then error." + JSON.stringify(error)); expect().assertFail(); }); + let getCandidateResult1 = wifi.getCandidateConfigs(); + console.info("[wifi_test]wifi get CandidateConfigs result1 : " + JSON.stringify(getCandidateResult1)); + var networkId = getCandidateResult1[0].netId; + console.info("[wifi_test]wifi get networkId result : " + JSON.stringify(networkId)); + await wifi.removeCandidateConfig(networkId) + .then(ret => { + let getconfig1 = wifi.getCandidateConfigs(); + console.info("[wifi_test]wifi get CandidateConfigs result2 : " + JSON.stringify(getconfig1)); + console.info("[wifi_test]wifi getconfig.length result : " + JSON.stringify(getconfig1.length)); + expect(true).assertEqual(getconfig1.length == 0); + }).catch((error) => { + console.error('[wifi_test]remove CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect().assertFail(); + }); done(); }) diff --git a/communication/wifi_standard/src/main/js/test/WifiManagerCandidateNetWork.test.js b/communication/wifi_standard/src/main/js/test/WifiManagerCandidateNetWork.test.js new file mode 100644 index 000000000..757d479f7 --- /dev/null +++ b/communication/wifi_standard/src/main/js/test/WifiManagerCandidateNetWork.test.js @@ -0,0 +1,376 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' + +import wifiMg from '@ohos.wifiManager' + +function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) +} + +function checkWifiPowerOn(){ + console.info("[wifi_test]wifi status:" + wifiMg.isWifiActive()); +} + +function resolveIP(ip) { + return (ip>>24 & 0xFF) + "." + (ip>>16 & 0xFF) + "." + (ip>>8 & 0xFF) + "." + (ip & 0xFF); +} + +let wifiSecurityType = { + WIFI_SEC_TYPE_INVALID: 0, + WIFI_SEC_TYPE_OPEN: 1, + WIFI_SEC_TYPE_WEP: 2, + WIFI_SEC_TYPE_PSK: 3, + WIFI_SEC_TYPE_SAE: 4, + WIFI_SEC_TYPE_EAP: 5, + WIFI_SEC_TYPE_EAP_SUITE_B: 6, + WIFI_SEC_TYPE_OWE:7 , + WIFI_SEC_TYPE_WAPI_CERT:8 , + WIFI_SEC_TYPE_WAPI_PSK:9 , +} + +let connState = { + SCANNING: 0, + CONNECTING: 1, + AUTHENTICATING: 2, + OBTAINING_IPADDR: 3, + CONNECTED: 4, + DISCONNECTING: 5, + DISCONNECTED: 6, + UNKNOWN: 7, +} + +let wifiChannelWidth = { + WIDTH_20MHZ : 0, + WIDTH_40MHZ : 1, + WIDTH_80MHZ : 2, + WIDTH_160MHZ : 3, + WIDTH_80MHZ_PLUS : 4, + WIDTH_INVALID:null, +} + +export default function actsWifiManagerCandidateNetWorkTest() { + describe('actsWifiManagerCandidateNetWorkTest', function () { + beforeEach(function () { + checkWifiPowerOn(); + }) + afterEach(function () { + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_CandidateNetWork_0001 + * @tc.name testaddCandidateConfig + * @tc.desc Test add OPEN and WEP CandidateConfig Promise API functionality. + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_Communication_WiFi_XTS_CandidateNetWork_0001', 0, async function (done) { + let wifiDeviceConfig = { + "ssid": "TEST_OPEN", + "bssid": "", + "preSharedKey": "", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_OPEN, + }; + await wifiMg.addCandidateConfig(wifiDeviceConfig) + .then(netWorkId => { + console.info("[wifi_test]add OPEN CandidateConfig promise : " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId = -1); + }).catch((error) => { + console.error('[wifi_test]add OPEN CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect(false).assertFalse(); + }); + let getconfig = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get OPEN CandidateConfigs result : " + JSON.stringify(getconfig)); + let wifiDeviceConfig1 = { + "ssid": "TEST_WEP", + "bssid": "", + "preSharedKey": "ABCDEF1234", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_WEP, + }; + + await wifiMg.addCandidateConfig(wifiDeviceConfig1) + .then(netWorkId => { + console.info("[wifi_test]add WEP CandidateConfig promise : " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId = -1); + }).catch((error) => { + console.error('[wifi_test]add WEP CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect(false).assertFalse(); + }); + console.info("[wifi_test]wifi get WEP CandidateConfigs result : " + JSON.stringify(getconfig)); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_CandidateNetWork_0002 + * @tc.name testaddCandidateConfig + * @tc.desc Test add PSK CandidateConfig and removeCandidateConfig Promise API functionality. + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_Communication_WiFi_XTS_CandidateNetWork_0002', 0, async function (done) { + let wifiDeviceConfig = { + "ssid": "TEST_PSK", + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_PSK, + "netId": -1, + "ipType": 1, + "creatorUid": 7, + "disableReason": 0, + "randomMacType": 0, + "randomMacAddr": "11:22:33:44:55:66", + "staticIp": {"ipAddress": 1284752956,"gateway": 1284752936}, + }; + await wifiMg.addCandidateConfig(wifiDeviceConfig) + .then(netWorkId => { + console.info("[wifi_test]add PSK CandidateConfig promise : " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId != -1); + }).catch((error) => { + console.error('[wifi_test]add PSK CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect().assertFail(); + }); + let getCandidateResult = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get PSK CandidateConfigs result : " + JSON.stringify(getCandidateResult)); + var networkId = getCandidateResult[0].netId; + console.info("[wifi_test]wifi get networkId result : " + JSON.stringify(networkId)); + await wifiMg.removeCandidateConfig(networkId) + .then(ret => { + let getCandidate = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get CandidateConfigs result : " + JSON.stringify(getCandidate)); + console.info("[wifi_test]wifi getconfig.length result : " + JSON.stringify(getCandidate.length)); + expect(true).assertEqual(getCandidate.length == 0); + }).catch((error) => { + console.error('[wifi_test]remove CandidateConfig promise failed : ' + JSON.stringify(error)); + expect().assertFail(); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_CandidateNetWork_0003 + * @tc.name testaddCandidateConfig + * @tc.desc Test add SAE CandidateConfig Promise API functionality. + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_Communication_WiFi_XTS_CandidateNetWork_0003', 0, async function (done) { + let wifiDeviceConfig = { + "ssid": "TEST_SAE", + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_SAE, + }; + await wifiMg.addCandidateConfig(wifiDeviceConfig) + .then(netWorkId => { + console.info("[wifi_test]add SAE CandidateConfig promise : " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId != -1); + }).catch((error) => { + console.error('[wifi_js]add SAE CandidateConfig promise failed -> ' + JSON.stringify(error)); + }); + let getCandidateResult = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get SAE CandidateConfigs result : " + JSON.stringify(getCandidateResult)); + var networkId = getCandidateResult[0].netId; + console.info("[wifi_test]wifi get networkId result : " + JSON.stringify(networkId)); + await wifiMg.removeCandidateConfig(networkId) + .then(ret => { + let getconfig1 = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get CandidateConfigs result : " + JSON.stringify(getconfig1)); + console.info("[wifi_test]wifi getconfig.length result : " + JSON.stringify(getconfig1.length)); + expect(true).assertEqual(getconfig1.length == 0); + }).catch((error) => { + console.error('[wifi_test]remove CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect().assertFail(); + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_CandidateNetWork_0004 + * @tc.name testaddCandidateConfig + * @tc.desc Test add MAX CandidateConfig and removeall CandidateConfig. + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_Communication_WiFi_XTS_CandidateNetWork_0004', 0, async function (done) { + let SSID = "TYPE_PSK" + for (let i = 0; i < 16; i++) { + SSID = "TYPE_PSK" + i + console.info("[wifi_test] get canshu result : "); + let wifiDeviceConfig = { + "ssid": SSID, + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_PSK, + }; + console.info("[wifi_test]get wifiDeviceConfig ssid result : " + JSON.stringify(wifiDeviceConfig.ssid)); + await wifiMg.addCandidateConfig(wifiDeviceConfig) + .then(netWorkId => { + console.info("[wifi_test]add 16th CandidateConfig promise : " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId != -1); + }).catch((error) => { + console.error('[wifi_test]add 16th CandidateConfig promise failed :' + JSON.stringify(error)); + expect().assertFail(); + }); + } + let wifiDeviceConfig1 = { + "ssid": "TYPE_17", + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_PSK, + }; + await wifiMg.addCandidateConfig(wifiDeviceConfig1) + .then(netWorkId => { + console.info("[wifi_test]add 17th CandidateConfig promise : " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId != -1); + }).catch((error) => { + console.error(`[wifi_test]add 17th failed, code is ${error.code}, message is ${error.message}`); + expect(true).assertEqual( (JSON.stringify(error.message)) !=null); + }); + let getCandidateResult = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get 16 CandidateConfigs result : " + JSON.stringify(getCandidateResult)); + for (let i = 0; i < 16; i++) { + var networkId = getCandidateResult[i].netId; + console.info("[wifi_test]wifi get networkId result : " + JSON.stringify(networkId)); + await wifiMg.removeCandidateConfig(networkId) + .then(ret => { + let getconfig1 = wifiMg.getCandidateConfigs(); + console.info("[wifi_test] wifi get CandidateConfigs result : " + JSON.stringify(getconfig1)); + console.info("[wifi_test] wifi getconfiglength result : " + JSON.stringify(getconfig1.length)); + }).catch((error) => { + console.error('[wifi_test]remove CandidateConfig promise failed -> ' + JSON.stringify(error)); + }); + } + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_CandidateNetWork_0005 + * @tc.name testaddCandidateConfig + * @tc.desc Test add CandidateConfig and removeCandidateConfig callback API functionality. + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_Communication_WiFi_XTS_CandidateNetWork_0005', 0, async function (done) { + let wifiDeviceConfig = { + "ssid": "TEST_connect", + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_PSK, + } + function addCandidate() { + return new Promise((resolve, reject) => { + wifiMg.addCandidateConfig(wifiDeviceConfig, + (err, netWorkId) => { + if (err) { + console.info("[wifi_test]add CandidateConfig callback failed : " + JSON.stringify(err)); + } + console.info("[wifi_test]addCandidateConfig callback result: " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId != -1); + resolve(); + }); + }); + } + await addCandidate(); + let getCandidateResult = wifiMg.getCandidateConfigs(); + console.info("[wifi_test] wifi getCandidateConfigs result : " + JSON.stringify(getCandidateResult)); + var networkId = getCandidateResult[0].netId; + function removeCandidate() { + return new Promise((resolve, reject) => { + wifiMg.removeCandidateConfig(networkId, + (err, ret) => { + if (err) { + console.info("[wifi_test]removeCandidate callback failed : " + JSON.stringify(err)); + } + let configs1 = wifiMg.getCandidateConfigs(); + console.info("[wifi_test] wifi get CandidateConfigs result : " + JSON.stringify(configs1)); + console.info("[wifi_test] getconfig.length result : " + JSON.stringify(configs1.length)); + expect(true).assertEqual(configs1.length == 0); + resolve(); + }); + }); + } + await removeCandidate(); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_CandidateNetWork_0006 + * @tc.name testaddCandidateConfig + * @tc.desc Test connect To CandidateConfig API functionality. + * @tc.type Function + * @tc.level Level 2 + */ + it('SUB_Communication_WiFi_XTS_CandidateNetWork_0006', 0, async function (done) { + let wifiSecurityType = { + WIFI_SEC_TYPE_INVALID: 0, + WIFI_SEC_TYPE_OPEN: 1, + WIFI_SEC_TYPE_WEP: 2, + WIFI_SEC_TYPE_PSK: 3, + WIFI_SEC_TYPE_SAE: 4, + } + let wifiDeviceConfig = { + "ssid": "HONOR 3000", + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_PSK, + }; + await wifiMg.addCandidateConfig(wifiDeviceConfig) + .then(netWorkId => { + console.info("[wifi_test]add CandidateConfig promise : " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId != -1); + }).catch((error) => { + console.error('[wifi_test]add CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect().assertFail(); + }); + let getCandidateResult = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get CandidateConfigs result : " + JSON.stringify(getCandidateResult)); + let connectToCandidateResult = wifiMg.connectToCandidateConfig(getCandidateResult[0].netId); + await sleep(3000); + await wifiMg.getLinkedInfo() + .then((result) => { + console.info("[wifi_test]get wifi link [promise] -> " + JSON.stringify(result)); + done(); + }).catch((error) => { + console.info("[wifi_test]promise then error." + JSON.stringify(error)); + expect().assertFail(); + }); + let getCandidateResult1 = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get CandidateConfigs result1 : " + JSON.stringify(getCandidateResult1)); + var networkId = getCandidateResult1[0].netId; + console.info("[wifi_test]wifi get networkId result : " + JSON.stringify(networkId)); + await wifiMg.removeCandidateConfig(networkId) + .then(ret => { + let getconfig1 = wifiMg.getCandidateConfigs(); + console.info("[wifi_test]wifi get CandidateConfigs result : " + JSON.stringify(getconfig1)); + console.info("[wifi_test]wifi getconfig.length result : " + JSON.stringify(getconfig1.length)); + expect(true).assertEqual(getconfig1.length == 0); + }).catch((error) => { + console.error('[wifi_test]remove CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect().assertFail(); + }); + done(); + }) + }) +} + diff --git a/communication/wifi_standard/src/main/js/test/WifiManagerStationEvent.test.js b/communication/wifi_standard/src/main/js/test/WifiManagerStationEvent.test.js new file mode 100644 index 000000000..b5dab1ef7 --- /dev/null +++ b/communication/wifi_standard/src/main/js/test/WifiManagerStationEvent.test.js @@ -0,0 +1,583 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' + +import wifiMg from '@ohos.wifiManager' + +import wifiext from '@ohos.wifiext' + +import wifiManagerExt from '@ohos.wifiManagerExt' + +function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) +} + +function checkWifiPowerOn(){ + console.info("[wifi_test]wifi status:" + wifiMg.isWifiActive()); +} +function resolveIP(ip) { + return (ip>>24 & 0xFF) + "." + (ip>>16 & 0xFF) + "." + (ip>>8 & 0xFF) + "." + (ip & 0xFF); +} + +let wifiSecurityType = { + WIFI_SEC_TYPE_INVALID: 0, + WIFI_SEC_TYPE_OPEN: 1, + WIFI_SEC_TYPE_WEP: 2, + WIFI_SEC_TYPE_PSK: 3, + WIFI_SEC_TYPE_SAE: 4, + WIFI_SEC_TYPE_EAP: 5, + WIFI_SEC_TYPE_EAP_SUITE_B: 6, + WIFI_SEC_TYPE_OWE:7 , + WIFI_SEC_TYPE_WAPI_CERT:8 , + WIFI_SEC_TYPE_WAPI_PSK:9 , +} + +let connState = { + SCANNING: 0, + CONNECTING: 1, + AUTHENTICATING: 2, + OBTAINING_IPADDR: 3, + CONNECTED: 4, + DISCONNECTING: 5, + DISCONNECTED: 6, + UNKNOWN: 7, +} + +let untrustedDeviceConfig = { + "ssid": "untrusted_ssid", + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_PSK +} + +let wifiChannelWidth = { + WIDTH_20MHZ : 0, + WIDTH_40MHZ : 1, + WIDTH_80MHZ : 2, + WIDTH_160MHZ : 3, + WIDTH_80MHZ_PLUS : 4, + WIDTH_INVALID:null, +} + +let groupOwnerBand = { + GO_BAND_AUTO : 0, + GO_BAND_2GHZ : 1, + GO_BAND_5GHZ : 2, +} + +export default function actsWifiManagerEventTest() { + describe('actsWifiManagerEventTest', function() { + beforeEach(function () { + checkWifiPowerOn(); + }) + afterEach(function () { + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0001 + * @tc.name testWifiStateChange + * @tc.desc Test wifiStateChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0001', 0, async function (done) { + let wifiState = "wifiStateChange"; + let wifiStateChangeCallback = result => { + console.info("[wifi_test]wifiStateChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(wifiState, wifiStateChangeCallback); + await sleep(3000); + wifiMg.off(wifiState, wifiStateChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0002 + * @tc.name testWifiConnectionChange + * @tc.desc Test wifiConnectionChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0002', 0, async function (done) { + let wifiConnectionState = "wifiConnectionChange"; + let wifiConnectionChangeCallback = result => { + console.info("[wifi_test]wifiConnectionChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(wifiConnectionState, wifiConnectionChangeCallback); + await sleep(3000); + wifiMg.off(wifiConnectionState, wifiConnectionChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0003 + * @tc.name testWifiScanStateChange + * @tc.desc Test wifiScanStateChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0003', 0, async function (done) { + let wifiScanState = "wifiScanStateChange"; + let wifiScanStateChangeCallback = result => { + console.info("[wifi_test]wifiScanStateChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(wifiScanState, wifiScanStateChangeCallback); + let scanResult = wifiMg.scan(); + await sleep(3000); + wifiMg.off(wifiScanState, wifiScanStateChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0004 + * @tc.name testWifiRssiChange + * @tc.desc Test wifiRssiChange callback + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0004', 0, async function (done) { + let wifiRssiState = "wifiRssiChange"; + let wifiRssiChangeCallback = result => { + console.info("[wifi_test]wifiRssiChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(wifiRssiState, wifiRssiChangeCallback); + await sleep(3000); + wifiMg.off(wifiRssiState, wifiRssiChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Event_Test_0005 + * @tc.name testHotspotStateChange + * @tc.desc Test hotspotStateChange api. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Event_Test_0005', 0, async function (done) { + let hotspotState = "hotspotStateChange"; + let hotspotStateChangeCallback = result => { + console.info("[wifi_test]hotspotStateChange callback, result: " + JSON.stringify(result)); + } + wifiMg.on(hotspotState, hotspotStateChangeCallback); + await sleep(3000); + wifiMg.off(hotspotState, hotspotStateChangeCallback); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0001 + * @tc.name testenableHotspot + * @tc.desc Test enableHotspot api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0001', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0001 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0001 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + console.info("[wifi_test] enableHotspot: " + wifiext.enableHotspot()) + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0001 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0001 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0002 + * @tc.name testdisableHotspot + * @tc.desc Test disableHotspot api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0002', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0002 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0002 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + console.info("[wifi_test] disableHotspot: " + wifiext.disableHotspot()) + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0002 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0002 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0003 + * @tc.name testgetSupportedPowerModel + * @tc.desc Test getSupportedPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0003', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0003 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0003 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + await wifiext.getSupportedPowerModel() + .then(data => { + console.info("[wifi_test]getSupportedPowerModel promise result -> " + JSON.stringify(data)); + }); + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0003 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0003 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0004 + * @tc.name testgetSupportedPowerModel + * @tc.desc Test getSupportedPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0004', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0004 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0004 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + function getSupportedPowerModelResult(){ + return new Promise((resolve, reject) => { + wifiext.getSupportedPowerModel( + (err, result) => { + if(err) { + console.info("[wifi_test]failed to getSupportedPowerModel:" + JSON.stringify(err)); + expect(true).assertTrue(); + } + console.info("[wifi_test]getSupportedPowerModel callback:" + JSON.stringify(result)); + resolve(); + }); + }); + } + await getSupportedPowerModelResult(); + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0004 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0004 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0005 + * @tc.name testgetPowerModel + * @tc.desc Test getPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0005', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0005 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0005 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + await wifiext.getPowerModel() + .then(data => { + console.info("[wifi_test]getPowerModel promise result -> " + JSON.stringify(data)); + }); + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0005 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0005 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0006 + * @tc.name testgetPowerModel + * @tc.desc Test getPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0006', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0006 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0006 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + function getPowerModelResult(){ + return new Promise((resolve, reject) => { + wifiext.getPowerModel( + (err, result) => { + if(err) { + console.info("[wifi_test]failed to getPowerModel:" + JSON.stringify(err)); + expect(true).assertTrue(); + } + console.info("[wifi_test]getPowerModel callback:" + JSON.stringify(result)); + resolve(); + }); + }); + } + await getPowerModelResult(); + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0006 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0006 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0007 + * @tc.name testsetPowerModel + * @tc.desc Test setPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0007', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0007 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0007 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + console.info("[wifi_test] setPowerModel: " + wifiext.setPowerModel(PowerModel)) + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0007 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0007 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0008 + * @tc.name testenableHotspot + * @tc.desc Test enableHotspot api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0008', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0008 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0008 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + console.info("[wifi_test] enableHotspot: " + wifiManagerExt.enableHotspot()) + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0008 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0008 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0009 + * @tc.name testdisableHotspot + * @tc.desc Test disableHotspot api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0009', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0009 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0009 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + console.info("[wifi_test] disableHotspot: " + wifiManagerExt.disableHotspot()) + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0009 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0009 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0010 + * @tc.name testgetSupportedPowerModel + * @tc.desc Test getSupportedPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0010', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0010 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0010 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + await wifiManagerExt.getSupportedPowerModel() + .then(data => { + console.info("[wifi_test]getSupportedPowerModel promise result -> " + JSON.stringify(data)); + }); + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0010 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0010 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0011 + * @tc.name testgetSupportedPowerModel + * @tc.desc Test getSupportedPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0011', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0011 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0011 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + function getSupportedPowerModelResult(){ + return new Promise((resolve, reject) => { + wifiManagerExt.getSupportedPowerModel( + (err, result) => { + if(err) { + console.info("[wifi_test]failed to getSupportedPowerModel:" + JSON.stringify(err)); + expect(true).assertTrue(); + } + console.info("[wifi_test]getSupportedPowerModel callback:" + JSON.stringify(result)); + resolve(); + }); + }); + } + await getSupportedPowerModelResult(); + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0011 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0011 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0012 + * @tc.name testgetPowerModel + * @tc.desc Test getPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0012', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0012 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0012 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + await wifiManagerExt.getPowerModel() + .then(data => { + console.info("[wifi_test]getPowerModel promise result -> " + JSON.stringify(data)); + }); + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0012 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0012 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0013 + * @tc.name testgetPowerModel + * @tc.desc Test getPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0013', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0013 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0013 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + function getPowerModelResult(){ + return new Promise((resolve, reject) => { + wifiManagerExt.getPowerModel( + (err, result) => { + if(err) { + console.info("[wifi_test]failed to getPowerModel:" + JSON.stringify(err)); + expect(true).assertTrue(); + } + console.info("[wifi_test]getPowerModel callback:" + JSON.stringify(result)); + resolve(); + }); + }); + } + await getPowerModelResult(); + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0013 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0013 end'); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_SysCaps_Test_0014 + * @tc.name testsetPowerModel + * @tc.desc Test setPowerModel api. + * @tc.type Function + */ + it('SUB_Communication_WiFi_SysCaps_Test_0014', 0, async function (done) { + console.info('SUB_Communication_WiFi_SysCaps_Test_0014 start'); + let ret = false; + try { + var isAccessToken = canIUse("SystemCapability.Communication.wifiMg.AP.Extension"); + console.info("SUB_Communication_WiFi_SysCaps_Test_0014 test.syscap.param.001 : " + isAccessToken); + if (isAccessToken) { + console.info("[wifi_test] setPowerModel: " + wifiManagerExt.setPowerModel(PowerModel)) + done(); + } + expect(isAccessToken).assertFalse(); + } catch (e) { + console.info("SUB_Communication_WiFi_SysCaps_Test_0014 canIUse isAccessToken error: " + e); + } + console.info('SUB_Communication_WiFi_SysCaps_Test_0014 end'); + done(); + }) + console.log("*************[wifi_test] start wifi js unit test end*************"); + }) +} + diff --git a/communication/wifi_standard/src/main/js/test/WifiManagerStationFunctions.test.js b/communication/wifi_standard/src/main/js/test/WifiManagerStationFunctions.test.js new file mode 100644 index 000000000..0b9d4cae8 --- /dev/null +++ b/communication/wifi_standard/src/main/js/test/WifiManagerStationFunctions.test.js @@ -0,0 +1,331 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' + +import wifiMg from '@ohos.wifiManager' + +function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) +} + +function checkWifiPowerOn(){ + console.info("[wifi_test]wifi status:" + wifiMg.isWifiActive()); +} + +function resolveIP(ip) { + return (ip>>24 & 0xFF) + "." + (ip>>16 & 0xFF) + "." + (ip>>8 & 0xFF) + "." + (ip & 0xFF); +} + +let wifiSecurityType = { + WIFI_SEC_TYPE_INVALID: 0, + WIFI_SEC_TYPE_OPEN: 1, + WIFI_SEC_TYPE_WEP: 2, + WIFI_SEC_TYPE_PSK: 3, + WIFI_SEC_TYPE_SAE: 4, + WIFI_SEC_TYPE_EAP: 5, + WIFI_SEC_TYPE_EAP_SUITE_B: 6, + WIFI_SEC_TYPE_OWE:7 , + WIFI_SEC_TYPE_WAPI_CERT:8 , + WIFI_SEC_TYPE_WAPI_PSK:9 , +} + +let connState = { + SCANNING: 0, + CONNECTING: 1, + AUTHENTICATING: 2, + OBTAINING_IPADDR: 3, + CONNECTED: 4, + DISCONNECTING: 5, + DISCONNECTED: 6, + UNKNOWN: 7, +} + +let untrustedDeviceConfig = { + "ssid": "untrusted_ssid", + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": wifiSecurityType.WIFI_SEC_TYPE_PSK +} + +let wifiChannelWidth = { + WIDTH_20MHZ : 0, + WIDTH_40MHZ : 1, + WIDTH_80MHZ : 2, + WIDTH_160MHZ : 3, + WIDTH_80MHZ_PLUS : 4, + WIDTH_INVALID:null, +} + +export default function actsWifiManagerFunctionsTest() { + describe('actsWifiManagerFunctionsTest', function() { + beforeEach(function () { + checkWifiPowerOn(); + }) + afterEach(function () { + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_Sta_0002 + * @tc.name testgetScanResults + * @tc.desc Test getScanResults promise and callback API functionality. + * @tc.type Function + * @tc.level Level 0 + */ + it('SUB_Communication_WiFi_XTS_Sta_0002', 0, async function (done) { + let scanResult = wifiMg.scan(); + await sleep(3000); + await wifiMg.getScanResults() + .then(result => { + let clen = Object.keys(result).length; + expect(true).assertEqual(clen >= 0); + console.info("[wifi_test]getScanInfos promise result:" + JSON.stringify(result)); + }); + function getScan() { + return new Promise((resolve, reject) => { + wifiMg.getScanResults( + (err, result) => { + if (err) { + console.log("[wifi_test] wifi getScanInfos failed:" + err); + } + let clen = Object.keys(result).length; + if (!(clen == 0)) { + expect(clen).assertLarger(0); + console.info("[wifi_test] getScanInfos callback result: " + JSON.stringify(result)); + for (let j = 0; j < clen; ++j) { + console.info("ssid: " + result[j].ssid + "bssid: " + result[j].bssid + + "securityType: " + result[j].securityType + + "rssi: " + result[j].rssi + "band: " + result[j].band + + "frequency: " + result[j].frequency + "channelWidth: " + result[j].channelWidth + + "timestamp" + result[j].timestamp + "capabilities" + result[j].capabilities + + "centerFrequency0: " + result[j].centerFrequency0 + + "centerFrequency1: " + result[j].centerFrequency1 + + "eid: " + result[j].infoElems.eid + "content: " + result[j].infoElems.content); + } + } + resolve(); + }); + }); + } + await getScan(); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_Sta_0021 + * @tc.name testGetSignalLevel + * @tc.desc Test getSignalLevel API functionality.. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_Sta_0021', 0, function () { + console.info("[wifi_test] check the 2.4G rssi assgined to level test."); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-65, 1)); + expect(wifiMg.getSignalLevel(-65, 1)).assertEqual(4); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-66, 1)); + expect(wifiMg.getSignalLevel(-66, 1)).assertEqual(3); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-75, 1)); + expect(wifiMg.getSignalLevel(-75, 1)).assertEqual(3); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-76, 1)); + expect(wifiMg.getSignalLevel(-76, 1)).assertEqual(2); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-82, 1)); + expect(wifiMg.getSignalLevel(-82, 1)).assertEqual(2); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-83, 1)); + expect(wifiMg.getSignalLevel(-83, 1)).assertEqual(1); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-88, 1)); + expect(wifiMg.getSignalLevel(-88, 1)).assertEqual(1); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-89, 1)); + expect(wifiMg.getSignalLevel(-89, 1)).assertEqual(0); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-127, 1)); + expect(wifiMg.getSignalLevel(-127, 1)).assertEqual(0); + + console.info("[wifi_test] check the 5G rssi assgined to level test."); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-65, 2)); + expect(wifiMg.getSignalLevel(-65, 2)).assertEqual(4); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-66, 2)); + expect(wifiMg.getSignalLevel(-66, 2)).assertEqual(3); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-72, 2)); + expect(wifiMg.getSignalLevel(-72, 2)).assertEqual(3); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-73, 2)); + expect(wifiMg.getSignalLevel(-73, 2)).assertEqual(2); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-79, 2)); + expect(wifiMg.getSignalLevel(-79, 2)).assertEqual(2); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-80, 2)); + expect(wifiMg.getSignalLevel(-80, 2)).assertEqual(1); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-85, 2)); + expect(wifiMg.getSignalLevel(-85, 2)).assertEqual(1); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-86, 2)); + expect(wifiMg.getSignalLevel(-86, 2)).assertEqual(0); + console.info("[wifi_test] getSignalLevel " + wifiMg.getSignalLevel(-127, 2)); + expect(wifiMg.getSignalLevel(-127, 2)).assertEqual(0); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_Sta_0017 + * @tc.name testgetCountryCode + * @tc.desc Test getCountryCode API function. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_Sta_0017', 0, function () { + expect(wifiMg.isWifiActive()).assertTrue(); + let getCountryCodeResult = wifiMg.getCountryCode(); + console.info("[wifi_test]getCountryCode :" + JSON.stringify(getCountryCodeResult)); + let countrylenth = getCountryCodeResult.length; + console.info("[wifi_test]getCountryCode.length :" + JSON.stringify(countrylenth)); + expect(true).assertEqual(countrylenth == 2); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_Sta_0020 + * @tc.name testFeatureSupported + * @tc.desc Test FeatureSupported API function. + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_XTS_Sta_0020', 0, function () { + expect(wifiMg.isWifiActive()).assertTrue(); + let wifiUtils = { + WIFI_FEATURE_INFRA: 0x0001, + WIFI_FEATURE_INFRA_5G: 0x0002, + WIFI_GAS_ANQP: 0x0004, + WIFI_WIFI_DIRECT: 0x0008, + WIFI_FEATURE_MOBILE_HOTSPOT: 0x0010, + WIFI_FEATURE_AWARE: 0x0040, + WIFI_FEATURE_AP_STA: 0x8000, + WIFI_FEATURE_WPA3_SAE: 0x8000000, + WIFI_FEATURE_WPA3_SUITE_B: 0x10000000, + WIFI_FEATURE_OWE: 0x20000000 + } + let isSupport1 = wifiMg.isFeatureSupported(wifiUtils.WIFI_FEATURE_INFRA); + console.info("[wifi_test] isFeatureSupported -> " + isSupport1); + let isSupport2 = wifiMg.isFeatureSupported(wifiUtils.WIFI_FEATURE_INFRA_5G); + console.info("[wifi_test] isFeatureSupported2 -> " + isSupport2); + let isSupport3 = wifiMg.isFeatureSupported(wifiUtils.WIFI_GAS_ANQP); + console.info("[wifi_test] isFeatureSupported3 -> " + isSupport3); + let isSupport4 = wifiMg.isFeatureSupported(wifiUtils.WIFI_WIFI_DIRECT); + console.info("[wifi_test] isFeatureSupported4 -> " + isSupport4); + let isSupport5 = wifiMg.isFeatureSupported(wifiUtils.WIFI_FEATURE_MOBILE_HOTSPOT); + console.info("[wifi_test] isFeatureSupported5 -> " + isSupport5); + let isSupport6 = wifiMg.isFeatureSupported(wifiUtils.WIFI_FEATURE_AWARE); + console.info("[wifi_test] isFeatureSupported6 -> " + isSupport6); + let isSupport7 = wifiMg.isFeatureSupported(wifiUtils.WIFI_FEATURE_AP_STA); + console.info("[wifi_test] isFeatureSupported7 -> " + isSupport7); + let isSupport8 = wifiMg.isFeatureSupported(wifiUtils.WIFI_FEATURE_WPA3_SAE); + console.info("[wifi_test] isFeatureSupported8 -> " + isSupport8); + let isSupport9 = wifiMg.isFeatureSupported(wifiUtils.WIFI_FEATURE_WPA3_SUITE_B); + console.info("[wifi_test] isFeatureSupported9 -> " + isSupport9); + let isSupport = wifiMg.isFeatureSupported(wifiUtils.WIFI_FEATURE_OWE); + console.info("[wifi_test] isFeatureSupported -> " + isSupport); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_Sta_0004 + * @tc.name testGetLinkedInfo + * @tc.desc Test Test getLinkedInfo and getIpInfo information. + * @tc.type Function + * @tc.level Level 1 + */ + it('SUB_Communication_WiFi_XTS_Sta_0004', 0, async function (done) { + let isConnectedResult = wifiMg.isConnected(); + let ipInfoResult = wifiMg.getIpInfo(); + expect(JSON.stringify(ipInfoResult)).assertContain("gateway"); + let ipAddress = resolveIP(ipInfoResult.ipAddress); + console.info("[wifi_test]ipAddress result: " + ipAddress); + console.info("gateway: " + ipInfoResult.gateway + "ipAddress: " + ipInfoResult.ipAddress + + "leaseDuration: " + ipInfoResult.leaseDuration + + "leaseDuration: " + ipInfoResult.leaseDuration + + "netmask: " + ipInfoResult.netmask + "primaryDns:" + ipInfoResult.primaryDns + + "secondDns: " + ipInfoResult.secondDns + "serverIp: " + ipInfoResult.serverIp); + await wifiMg.getLinkedInfo() + .then((result) => { + console.info("[wifi_test]get wifi link promise:" + JSON.stringify(result)); + done(); + }).catch((error) => { + console.info("[wifi_test]promise then error." + JSON.stringify(error)); + expect().assertFail(); + }); + + function getLinked(){ + return new Promise((resolve, reject) => { + wifiMg.getLinkedInfo( + (err, result) => { + if(err) { + console.log("[wifi_test]wifi getLinkedInfo failed " + err); + } + let clen = Object.keys(result).length; + expect(clen).assertLarger(0); + console.info("[wifi_test]getLinkedInfo callback result: " + JSON.stringify(result)); + console.info("ssid: " + result.ssid + "bssid:" + result.bssid + "band: " + result.band + + "isHidden: " + result.isHidden + "isRestricted: " + result.isRestricted + + "chload: " + result.chload + "rssi " + result.rssi + "netWorkId: " + result.netWorkId + + "linkSpeed: " + result.linkSpeed + "frequency:" + + result.frequency + "snr:" + result.snr + + "macAddress: " + result.macAddress + "ipAddress: " + result.ipAddress + + "suppState: " + result.suppState + "connState: " + result.connState + + "macType: " + result.macType); + let state = wifiMg.getLinkedInfo().ConnState; + if (state == connState.SCANNING) { + expect(true).assertEqual(state == 0); + } + if (state == connState.CONNECTING) { + expect(true).assertEqual(state == 1); + } + if (state == connState.AUTHENTICATING) { + expect(true).assertEqual(state == 2); + } + if (state == connState.OBTAINING_IPADDR) { + expect(true).assertEqual(state == 3); + } + if (state == connState.CONNECTED) { + expect(true).assertEqual(state == 4); + } + if (state == connState.DISCONNECTING) { + expect(true).assertEqual(state == 5); + } + if (state == connState.DISCONNECTED) { + expect(true).assertEqual(state == 6); + } + if (state == connState.UNKNOWN) { + expect(true).assertEqual(state == 7); + } + resolve(); + }); + }); + } + await getLinked(); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_XTS_Sta_0034 + * @tc.name testgetScanResultsSync + * @tc.desc Test getScanResults Sync API functionality. + * @tc.type Function + * @tc.level Level 0 + */ + it('SUB_Communication_WiFi_XTS_Sta_0034', 0, async function (done) { + let getScanResultsResult = wifiMg.getScanResultsSync(); + console.info("[wifi_test]wifi getScanResultsSync result : " + JSON.stringify(getScanResultsResult)); + let scanInfolenth = Object.keys(getScanResultsResult).length; + console.info("[wifi_test]wifi ScanInfosSync length result : " + JSON.stringify(scanInfolenth)); + expect(true).assertEqual(scanInfolenth >= 0); + done(); + }) + console.log("*************[wifi_test] start wifi js unit test end*************"); + }) +} + -- GitLab