From bee291f605b6cf482bc54f5d9b8a994cae2c390c Mon Sep 17 00:00:00 2001 From: quanli Date: Fri, 12 Aug 2022 18:13:37 +0800 Subject: [PATCH] quanli1@huawei.com Signed-off-by: quanli --- .../wifi_p2p/src/main/js/test/List.test.js | 9 +- .../src/main/js/test/WifiP2P.test2.js | 80 ++ .../{WifiP2P.test.js => WifiP2P.testsame.js} | 1066 +++++++++-------- .../src/main/js/test/List.test.js | 9 +- .../src/main/js/test/WifiJsunit.test1.js | 363 ++++++ ...WifiSta.test.js => WifiJsunit.testsame.js} | 963 ++++++++------- 6 files changed, 1489 insertions(+), 1001 deletions(-) create mode 100644 communication/wifi_p2p/src/main/js/test/WifiP2P.test2.js rename communication/wifi_p2p/src/main/js/test/{WifiP2P.test.js => WifiP2P.testsame.js} (78%) create mode 100644 communication/wifi_standard/src/main/js/test/WifiJsunit.test1.js rename communication/wifi_standard/src/main/js/test/{WifiSta.test.js => WifiJsunit.testsame.js} (77%) 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 3452c272f..244af299c 100644 --- a/communication/wifi_p2p/src/main/js/test/List.test.js +++ b/communication/wifi_p2p/src/main/js/test/List.test.js @@ -12,9 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -//require('./WifiSta.test.js') -//require('./WifiSoftAP.test.js') -import ActsWifiTest from './WifiP2P.test.js' +import actsWifiTestNew from './WifiP2P.testsame.js' +import actsWifiTest from './WifiP2P.test2.js' export default function testsuite() { -ActsWifiTest() +actsWifiTestNew() +actsWifiTest() } + diff --git a/communication/wifi_p2p/src/main/js/test/WifiP2P.test2.js b/communication/wifi_p2p/src/main/js/test/WifiP2P.test2.js new file mode 100644 index 000000000..c41191179 --- /dev/null +++ b/communication/wifi_p2p/src/main/js/test/WifiP2P.test2.js @@ -0,0 +1,80 @@ +/* + * 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 wifi from '@ohos.wifi' + +function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) +} + +function checkWifiPowerOn(){ + console.info("wifi_test/wifi status:" + wifi.isWifiActive()); +} + +export default function actsWifiTestNew() { + describe('actsWifiTestNew', function () { + beforeEach(function () { + checkWifiPowerOn(); + }) + afterEach(function () { + }) + + /** + * @tc.number P2P_0009 + * @tc.name SUB_Communication_WiFi_XTS_P2P_0009 + * @since 8 + * @tc.desc Test p2pCancelConnect Group API functionality. + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + 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 : "AAAZZZ123", + goBand : 0 + }; + let addConfig = wifi.createGroup(WifiP2PConfig); + console.info("[wifi_test] test p2pConnect result." + addConfig); + let disConn = wifi.p2pCancelConnect(); + sleep(2000); + console.info("[wifi_test] test p2pCancelConnect result." + disConn); + expect(disConn).assertTrue(); + let removeConfig = wifi.removeGroup(); + console.info("[wifi_test] test start removeGroup" + removeConfig); + expect(removeConfig).assertTrue(); + done(); + }) + + /** + * @tc.number P2P_0011 + * @tc.name SUB_Communication_WiFi_XTS_P2P_0011 + * @since 8 + * @tc.desc Test remove error Group functionality. + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_XTS_P2P_0011', 0, async function (done) { + let isRemoved = wifi.removeGroup(10000); + console.info("[wifi_test]removeGroup(10000) result : " + JSON.stringify(isRemoved)); + expect(isRemoved).assertTrue(); + done(); + }) + + }) +} + diff --git a/communication/wifi_p2p/src/main/js/test/WifiP2P.test.js b/communication/wifi_p2p/src/main/js/test/WifiP2P.testsame.js similarity index 78% rename from communication/wifi_p2p/src/main/js/test/WifiP2P.test.js rename to communication/wifi_p2p/src/main/js/test/WifiP2P.testsame.js index 0bcd7ff78..633cf773d 100644 --- a/communication/wifi_p2p/src/main/js/test/WifiP2P.test.js +++ b/communication/wifi_p2p/src/main/js/test/WifiP2P.testsame.js @@ -1,509 +1,557 @@ -/* - * 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 wifi from '@ohos.wifi' - -function sleep(delay) { - var start = (new Date()).getTime(); - while ((new Date()).getTime() - start > delay) { - break; - } -} - -function checkWifiPowerOn(){ - console.info("Wifi_test/wifi status:" + wifi.isWifiActive()); -} - -let GroupOwnerBand = { - GO_BAND_AUTO : 0, - GO_BAND_2GHZ : 1, - GO_BAND_5GHZ : 2, -} -export default function ActsWifiTest() { -describe('ActsWifiTest', function () { - beforeEach(function () { - console.info("beforeEach start"); - checkWifiPowerOn(); - }) - - afterEach(async function () { - console.info("afterEach start"); - }) - - /** - * @tc.number config_0001 - * @tc.name SUB_Communication_WiFi_P2P_Config_0001 - * @tc.desc Test createGroup and getCurrentGroup promise infos - */ - it('SUB_Communication_WiFi_P2P_Config_0001', 0, async function(done) { - let WifiP2PConfig = { - deviceAddress : "00:00:00:00:00:00", - netId : -1, - passphrase : "12345678", - groupName : "AAAZZZ123", - goBand : 0 - }; - - console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); - expect(wifi.isWifiActive()).assertTrue(); - let addConfig = wifi.createGroup(WifiP2PConfig); - console.info("[wifi_test] test createGroup end."+ addConfig); - sleep(2000); - expect(addConfig).assertTrue(); - wifi.getCurrentGroup() - .then(data => { - let resultLength = Object.keys(data).length; - console.info("[wifi_test] getCurrentGroup [promise] result -> " + JSON.stringify(data)); - expect(true).assertEqual(resultLength!=0); - let removeConfig = wifi.removeGroup(); - expect(removeConfig).assertTrue(); - }); - done() - }) - - /** - * @tc.number config_0002 - * @tc.name SUB_Communication_WiFi_P2P_Config_0002 - * @tc.desc Test getCurrentGroup callback infos - */ - it('SUB_Communication_WiFi_P2P_Config_0002', 0, async function(done) { - let WifiP2PConfig = { - deviceAddress : "00:00:00:00:00:00", - netId : -1, - passphrase : "12345678", - groupName : "AAAZZZ123", - goBand : 0 - }; - - console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); - expect(wifi.isWifiActive()).assertTrue(); - let addConfig = wifi.createGroup(WifiP2PConfig); - sleep(2000); - console.log("[wifi_test] check the state of wifi:" + addConfig); - expect(addConfig).assertTrue(); - - wifi.getCurrentGroup( - (err, result) => { - if (err) { - console.error('wifi_test / failed to get getCurrentGroup: ' + JSON.stringify(err)); - expect().assertFail(); - return; - }else{ - console.info("[wifi_test] getCurrentGroup [callback] -> " + JSON.stringify(result)); - let removeConfig = wifi.removeGroup(); - expect(removeConfig).assertTrue(); - } - done(); - }); - }) - - /** - * @tc.number config_0003 - * @tc.name SUB_Communication_WiFi_P2P_Config_0003 - * @tc.desc Test createGroup 2.4G band and getCurrentGroup infos - */ - it('SUB_Communication_WiFi_P2P_Config_0003', 0, async function(done) { - let WifiP2PConfig2 = { - deviceAddress : "00:00:00:00:00:00", - netId : -1, - passphrase : "12345678", - groupName : "AAAZZZ123", - goBand : 1 - }; - - console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); - expect(wifi.isWifiActive()).assertTrue(); - let addConfig = wifi.createGroup(WifiP2PConfig2); - sleep(2000); - console.info("[wifi_test] test createGroup3 result." + addConfig) - expect(addConfig).assertTrue(); - await wifi.getCurrentGroup() - .then(data => { - let resultLength = Object.keys(data).length; - console.info("[wifi_test] getCurrentGroup [promise] result -> " + JSON.stringify(data)); - expect(true).assertEqual(resultLength!=0); - let removeConfig = wifi.removeGroup(); - expect(removeConfig).assertTrue(); - }); - done() - }) - - /** - * @tc.number config_0004 - * @tc.name SUB_Communication_WiFi_P2P_Config_0004 - * @tc.desc Test create PersistentGroup infos - */ - it('SUB_Communication_WiFi_P2P_Config_0004', 0, async function(done) { - let WifiP2PConfig = { - deviceAddress : "00:00:00:00:00:00", - netId : -2, - passphrase : "12345678", - groupName : "AAAZZZ123", - goBand : 0 - }; - - console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); - expect(wifi.isWifiActive()).assertTrue(); - let addConfig = wifi.createGroup(WifiP2PConfig); - sleep(2000); - console.info("[wifi_test] test p2pConnect result." + addConfig); - expect(addConfig).assertTrue(); - await wifi.getCurrentGroup() - .then((data) => { - let resultLength = Object.keys(data).length; - console.info("[wifi_test] getCurrentGroup [promise] result -> " + JSON.stringify(data)); - expect(true).assertEqual(resultLength!=0); - - let removeConfig = wifi.removeGroup(); - expect(removeConfig).assertTrue(); - }); - done(); - }) - - /** - * @tc.number config_0005 - * @tc.name SUB_Communication_WiFi_P2P_Config_0005 - * @tc.desc Test p2pConnect infos - */ - it('SUB_Communication_WiFi_P2P_Config_0005', 0, async function(done) { - let WifiP2PConfig3 = { - deviceAddress : "00:00:00:00:00:00", - netId : -2, - passphrase : "12345678", - groupName : "AAAZZZ", - goBand : 2 - }; - - console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); - expect(wifi.isWifiActive()).assertTrue(); - let scanConfig = wifi.startDiscoverDevices(); - sleep(2000); - expect(scanConfig).assertTrue(); - - let connConfig = wifi.p2pConnect(WifiP2PConfig3); - console.info("[wifi_test] test p2pConnect result." + connConfig); - expect(connConfig).assertTrue(); - let stopScan = wifi.stopDiscoverDevices(); - console.info("[wifi_test] test stopDiscoverDevices result." + stopScan); - done() - }) - - /** - * @tc.number config_0006 - * @tc.name SUB_Communication_WiFi_P2P_Config_0006 - * @tc.desc Test getP2pLinkedInfo promise infos - */ - it('SUB_Communication_WiFi_P2P_Config_0006', 0, async function(done) { - await wifi.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() - }); - - done(); - }) - -/** - * @tc.number config_0007 - * @tc.name SUB_Communication_WiFi_P2P_Config_0007 - * @tc.desc Test getP2pLinkedInfo callback infos - */ - it('SUB_Communication_WiFi_P2P_Config_0007', 0, async function(done) { - let P2pConnectState = { - DISCONNECTED :0, - CONNECTED : 1, - }; - - wifi.getP2pLinkedInfo((err, result) => { - if (err) { - console.error('failed to getP2pLinkedInfo callback ' + JSON.stringify(err)); - return; - }else{ - console.info("[wifi_test] getP2pLinkedInfo [callback] -> " + JSON.stringify(result)); - console.info("connectState: " + result.connectState + - "isGroupOwner: " + result.isGroupOwner + - "groupOwnerAddr: " + result.groupOwnerAddr); - expect(false).assertEqual(result.connectState ==P2pConnectState.CONNECTED); - } - done(); - }); - }) - - /** - * @tc.number config_0008 - * @tc.name SUB_Communication_WiFi_P2P_Config_0008 - * @tc.desc Test p2pCancelConnect infos - */ - it('SUB_Communication_WiFi_P2P_Config_0008', 0, async function(done) { - let disConn = wifi.p2pCancelConnect(); - sleep(2000); - console.info("[wifi_test] test p2pCancelConnect result." + disConn); - expect(disConn).assertTrue(); - let removeConfig = wifi.removeGroup(); - console.info("[wifi_test] test start removeGroup" + removeConfig); - expect(removeConfig).assertTrue(); - done(); - }) - - /** - * @tc.number config_0009 - * @tc.name SUB_Communication_WiFi_P2P_Config_0009 - * @tc.desc Test getP2pPeerDevices infos - */ - it('SUB_Communication_WiFi_P2P_Config_0009', 0, async function(done){ - console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); - expect(wifi.isWifiActive()).assertTrue(); - let scanConfig = wifi.startDiscoverDevices(); - sleep(2000); - expect(scanConfig).assertTrue(); - await wifi.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(); - }); - done(); - - }) - - /** - * @tc.number config_0010 - * @tc.name SUB_Communication_WiFi_P2P_Config_0010 - * @tc.desc Test getP2pPeerDevices infos - */ - it('SUB_Communication_WiFi_P2P_Config_0010', 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: " + wifi.isWifiActive()); - expect(wifi.isWifiActive()).assertTrue(); - let scanConfig = wifi.startDiscoverDevices(); - sleep(2000); - expect(scanConfig).assertTrue(); - - await wifi.getP2pPeerDevices((err, result) => { - if (err) { - console.error('failed to getP2pPeerDevices infos callback because ' + JSON.stringify(err)); - }else{ - console.info("[wifi_test] getP2pPeerDevices [callback] -> " + 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); - } - } - let stopScan = wifi.stopDiscoverDevices(); - expect(stopScan).assertTrue(); - } - done(); - }); - }) - - /** - * @tc.number p2pStateChange_0001 - * @tc.name SUB_Communication_WiFi_P2P_P2pStateChange_0001 - * @tc.desc Test p2pStateChange callback - */ - it('SUB_Communication_WiFi_P2P_P2pStateChange_0001', 0, async function (done) { - await wifi.on('p2pStateChange', result => { - console.info("onP2pStateChange callback, result:" + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - done(); - }); - setTimeout(function() { - - wifi.off('p2pStateChange', result => { - console.info("offP2pStateChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - }); - }, 1 * 1000); - done(); - }) - - /** - * @tc.number p2pConnectionChange_0002 - * @tc.name SUB_Communication_WiFi_P2P_p2pConnectionChange_0002 - * @tc.desc Test p2pConnectionChange callback - */ - it('SUB_Communication_WiFi_P2P_p2pConnectionChange_0002', 0, async function (done) { - await wifi.on('p2pConnectionChange', recvP2pConnectionChangeFunc => { - console.info("[wifi_test] p2pConnectionChange result -> " + recvP2pConnectionChangeFunc); - expect(true).assertEqual(recvP2pConnectionChangeFunc !=null); - done(); - }); - setTimeout(function() { - console.info('[wifi_test] offP2pStateChange test start ...'); - wifi.off('p2pConnectionChange', recvP2pConnectionChangeFunc => { - console.info("p2pConnectionChange callback" + JSON.stringify(recvP2pConnectionChangeFunc)); - expect(true).assertEqual(recvP2pConnectionChangeFunc !=null); - }); - }, 1 * 1000); - done(); - }) - - /** - * @tc.number p2pDeviceChange_0003 - * @tc.name SUB_Communication_WiFi_P2P_p2pDeviceChange_0003 - * @tc.desc Test p2pDeviceChange callback - */ - it('SUB_Communication_WiFi_P2P_p2pDeviceChange_0003', 0, async function (done) { - console.info('[wifi_test] Onp2pDeviceChange test start ...'); - await wifi.on('p2pDeviceChange', result => { - console.info("onP2pDeviceChange callback, result:" + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - done(); - }); - setTimeout(function() { - console.info('[wifi_test] offP2pDeviceChange test start ...'); - wifi.off('p2pDeviceChange', result => { - console.info("offP2pStateChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - }); - }, 1 * 1000); - done(); - }) - - /** - * @tc.number p2pPeerDeviceChange_0004 - * @tc.name SUB_Communication_WiFi_P2P_p2pPeerDeviceChange_0004 - * @tc.desc Test p2pPeerDeviceChange callback - */ - it('SUB_Communication_WiFi_P2P_p2pPeerDeviceChange_0004', 0, async function (done) { - let recvP2pPeerDeviceChangeFunc = result => { - console.info("wifi_test / p2p peer device change receive event: " + JSON.stringify(result)); - wifi.getP2pDevices((err, data) => { - if (err) { - console.error('wifi_test / failed to get getP2pDevices: ' + JSON.stringify(err)); - return; - } - console.info("wifi_test / getP2pDevices [callback] -> " + JSON.stringify(data)); - let len = Object.keys(data).length; - console.log("getP2pDevices number: " + len); - for (let i = 0; i < len; ++i) { - if (data[i].deviceName === "GRE") { - console.info("wifi_test / p2pConnect: -> " + data[i].deviceAddress); - let config = { - "deviceAddress":data[i].deviceAddress, - "netId":-1, - "passphrase":"", - "groupName":"", - "goBand":0, - } - wifi.p2pConnect(config); - } - } - }); - } - await wifi.on('p2pPeerDeviceChange', result => { - console.info("onP2pPeerDeviceChange callback, result:" + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - done(); - }); - setTimeout(function() { - wifi.off('p2pPeerDeviceChange', result => { - console.info("offP2pPeerDeviceChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - }); - }, 1 * 1000); - done(); - }) - - /** - * @tc.number p2pPersistentGroupChange_0005 - * @tc.name SUB_Communication_WiFi_P2P_p2pPersistentGroupChange_0005 - * @tc.desc Test p2pPersistentGroupChange callback - */ - it('SUB_Communication_WiFi_P2P_p2pPersistentGroupChange_0005', 0, async function (done) { - let recvP2pPersistentGroupChangeFunc = () => { - console.info("wifi_test / p2p persistent group change receive event"); - let config = { - "deviceAddress" : "02:11:65:f2:0d:6e", - "netId":-2, - "passphrase":"", - "groupName":"", - "goBand":0, - }; - let addConfig = wifi.createGroup(config); - expect(addConfig).assertTrue(); - wifi.getCurrentGroup((err, data) => { - if (err) { - console.error('wifi_test / failed to get getCurrentGroup: ' + JSON.stringify(err)); - return; - } - console.info("wifi_test / get getCurrentGroup [callback] -> " + JSON.stringify(data)); - }); - }; - wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); - setTimeout(async function() { - wifi.off('p2pPersistentGroupChange', result => { - console.info("offP2pPersistentGroupChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - }); - }, 1 * 1000); - done(); - }) - - /** - * @tc.number p2pDiscoveryChange_0006 - * @tc.name SUB_Communication_WiFi_P2P_p2pDiscoveryChange_0006 - * @tc.desc Test p2pDiscoveryChange callback - */ - it('SUB_Communication_WiFi_P2P_p2pDiscoveryChange_0006', 0, async function (done) { - await wifi.on('p2pDiscoveryChange', result => { - console.info("onp2pDiscoveryChange callback, result:" + JSON.stringify(result)); - expect(true).assertEqual((result !=null)); - done(); - }); - setTimeout(function() { - wifi.off('p2pDiscoveryChange', result => { - console.info("offp2pDiscoveryChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - }); - }, 1 * 1000); - done(); - }) - - console.log("*************[wifi_test] start wifi js unit test end*************"); -}) -} +/* + * 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 wifi from '@ohos.wifi' + +function sleep(delay) { // delay x ms + var start = (new Date()).getTime(); + while ((new Date()).getTime() - start > delay) { + break; + } +} + +function checkWifiPowerOn(){ + console.info("wifi_test/wifi status:" + wifi.isWifiActive()); +} + +let GroupOwnerBand = { + GO_BAND_AUTO : 0, + GO_BAND_2GHZ : 1, + GO_BAND_5GHZ : 2, +} + +export default function actsWifiTest() { +describe('actsWifiTest', function () { + beforeEach(function () { + console.info("beforeEach start" ); + checkWifiPowerOn(); + }) + afterEach(async function () { + console.info("afterEach start" ); + }) + + /** + * @tc.number P2P_Config_0001 + * @tc.name SUB_Communication_WiFi_P2P_Config_0001 + * @tc.desc Test createGroup and getCurrentGroup promise infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_Config_0001', 0, async function(done) { + let WifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "AAAZZZ123", + goBand : 0 + }; + console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); + expect(wifi.isWifiActive()).assertTrue(); + let addConfig = wifi.createGroup(WifiP2PConfig); + console.info("[wifi_test] test createGroup end." + addConfig); + sleep(2000); + expect(addConfig).assertTrue(); + wifi.getCurrentGroup() + .then(data => { + let resultLength = Object.keys(data).length; + console.info("[wifi_test] getCurrentGroup [promise] result -> " + JSON.stringify(data)); + expect(true).assertEqual(resultLength!=0); + let removeConfig = wifi.removeGroup(); + expect(removeConfig).assertTrue(); + }); + done() + }) + + /** + * @tc.number P2P_Config_0002 + * @tc.name SUB_Communication_WiFi_P2P_Config_0002 + * @tc.desc Test getCurrentGroup callback infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION + */ + it('SUB_Communication_WiFi_P2P_Config_0002', 0, async function(done) { + let WifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "AAAZZZ123", + goBand : 0 + }; + + console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); + expect(wifi.isWifiActive()).assertTrue(); + let addConfig = wifi.createGroup(WifiP2PConfig); + sleep(2000); + console.log("[wifi_test] check the state of wifi: " + addConfig); + expect(addConfig).assertTrue(); + wifi.getCurrentGroup( + (err, result) => { + if (err) { + console.error('wifi_test / failed to get getCurrentGroup: ' + JSON.stringify(err)); + expect().assertFail(); + }else{ + 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 + + // "clientDevices: " + result.clientDevices +"networkId: " + result.networkId + // + "frequency: " + result.frequency + "goIpAddress: " + result.goIpAddress); + let removeConfig = wifi.removeGroup(); + expect(removeConfig).assertTrue(); + } + done(); + }); + }) + + /** + * @tc.number P2P_Config_0003 + * @tc.name SUB_Communication_WiFi_P2P_Config_0003 + * @tc.desc Test createGroup 2.4G band and getCurrentGroup infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_Config_0003', 0, async function(done) { + let WifiP2PConfig2 = { + deviceAddress : "00:00:00:00:00:00", + netId : -1, + passphrase : "12345678", + groupName : "AAAZZZ123", + goBand : 1 + }; + console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); + expect(wifi.isWifiActive()).assertTrue(); + let addConfig = wifi.createGroup(WifiP2PConfig2); + sleep(2000); + console.info("[wifi_test] test createGroup3 result." + addConfig) + expect(addConfig).assertTrue(); + await wifi.getCurrentGroup() + .then(data => { + let resultLength = Object.keys(data).length; + console.info("[wifi_test] getCurrentGroup [promise] result -> " + JSON.stringify(data)); + expect(true).assertEqual(resultLength!=0); + let removeConfig = wifi.removeGroup(); + expect(removeConfig).assertTrue(); + }); + done() + }) + + /** + * @tc.number P2P_Config_0004 + * @tc.name SUB_Communication_WiFi_P2P_Config_0004 + * @tc.desc Test create PersistentGroup infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_Config_0004', 0, async function(done) { + let WifiP2PConfig = { + deviceAddress : "00:00:00:00:00:00", + netId : -2, + passphrase : "12345678", + groupName : "AAAZZZ123", + goBand : 0 + }; + console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); + expect(wifi.isWifiActive()).assertTrue(); + let addConfig = wifi.createGroup(WifiP2PConfig); + sleep(2000); + console.info("[wifi_test] test p2pConnect result." + addConfig); + expect(addConfig).assertTrue(); + await wifi.getCurrentGroup() + .then((data) => { + let resultLength = Object.keys(data).length; + console.info("[wifi_test] getCurrentGroup [promise] result -> " + JSON.stringify(data)); + expect(true).assertEqual(resultLength!=0); + let removeConfig = wifi.removeGroup(); + expect(removeConfig).assertTrue(); + }); + done(); + }) + + /** + * @tc.number P2P_Config_0005 + * @tc.name SUB_Communication_WiFi_P2P_Config_0005 + * @tc.desc Test p2pConnect infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION + */ + it('SUB_Communication_WiFi_P2P_Config_0005', 0, async function(done) { + let WifiP2PConfig3 = { + deviceAddress : "00:00:00:00:00:00", + netId : -2, + passphrase : "12345678", + groupName : "AAAZZZ", + goBand : 2 + }; + console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); + expect(wifi.isWifiActive()).assertTrue(); + let scanConfig = wifi.startDiscoverDevices(); + sleep(2000); + expect(scanConfig).assertTrue(); + + let connConfig = wifi.p2pConnect(WifiP2PConfig3); + console.info("[wifi_test] test p2pConnect result." + connConfig); + expect(connConfig).assertTrue(); + let stopScan = wifi.stopDiscoverDevices(); + console.info("[wifi_test] test stopDiscoverDevices result." + stopScan); + done() + }) + + /** + * @tc.number P2P_Config_0006 + * @tc.name SUB_Communication_WiFi_P2P_Config_0006 + * @tc.desc Test getP2pLinkedInfo promise infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_Config_0006', 0, async function(done) { + await wifi.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() + }); + }) + + /** + * @tc.number P2P_Config_0007 + * @tc.name SUB_Communication_WiFi_P2P_Config_0007 + * @tc.desc Test getP2pLinkedInfo callback infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_Config_0007', 0, async function(done) { + let P2pConnectState = { + DISCONNECTED :0, + CONNECTED : 1, + }; + wifi.getP2pLinkedInfo((err, result) => { + if (err) { + console.error('failed to getP2pLinkedInfo callback ' + JSON.stringify(err)); + return; + }else{ + console.info("[wifi_test] getP2pLinkedInfo [callback] -> " + JSON.stringify(result)); + console.info("connectState: " + result.connectState + + "isGroupOwner: " + result.isGroupOwner + + "groupOwnerAddr: " + result.groupOwnerAddr); + expect(false).assertEqual(result.connectState ==P2pConnectState.CONNECTED); + } + done(); + }); + + }) + + /** + * @tc.number P2P_Config_0008 + * @tc.name SUB_Communication_WiFi_P2P_Config_0008 + * @tc.desc Test p2pCancelConnect infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_Config_0008', 0, async function(done) { + let disConn = wifi.p2pCancelConnect(); + sleep(2000); + console.info("[wifi_test] test p2pCancelConnect result." + disConn); + expect(disConn).assertTrue(); + let removeConfig = wifi.removeGroup(); + console.info("[wifi_test] test start removeGroup" + removeConfig); + expect(removeConfig).assertTrue(); + done(); + }) + + /** + * @tc.number P2P_Config_0009 + * @tc.name SUB_Communication_WiFi_P2P_Config_0009 + * @tc.desc Test getP2pPeerDevices promise infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION + */ + it('SUB_Communication_WiFi_P2P_Config_0009', 0, async function(done){ + console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); + expect(wifi.isWifiActive()).assertTrue(); + let scanConfig = wifi.startDiscoverDevices(); + sleep(2000); + expect(scanConfig).assertTrue(); + await wifi.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(); + }); + done(); + }) + + /** + * @tc.number P2P_Config_0010 + * @tc.name SUB_Communication_WiFi_P2P_Config_0010 + * @tc.desc Test getP2pPeerDevices callback infos + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION + */ + it('SUB_Communication_WiFi_P2P_Config_0010', 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: " + wifi.isWifiActive()); + expect(wifi.isWifiActive()).assertTrue(); + let scanConfig = wifi.startDiscoverDevices(); + sleep(2000); + expect(scanConfig).assertTrue(); + await wifi.getP2pPeerDevices((err, result) => { + if (err) { + console.error('failed to getP2pPeerDevices infos callback because ' + JSON.stringify(err)); + }else{ + console.info("[wifi_test] getP2pPeerDevices [callback] -> " + 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); + } + } + let stopScan = wifi.stopDiscoverDevices(); + expect(stopScan).assertTrue(); + } + done(); + }); + }) + + /** + * @tc.number P2P_P2pStateChange_0001 + * @tc.name SUB_Communication_WiFi_P2P_P2pStateChange_0001 + * @tc.desc Test p2pStateChange callback + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_P2pStateChange_0001', 0, async function (done) { + await wifi.on('p2pStateChange', result => { + console.info("onP2pStateChange callback, result:" + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + done(); + }); + setTimeout(function() { + wifi.off('p2pStateChange', result => { + console.info("offP2pStateChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + }); + }, 1 * 1000); + done(); + }) + + /** + * @tc.number p2pConnectionChange_0002 + * @tc.name SUB_Communication_WiFi_P2P_p2pConnectionChange_0002 + * @tc.desc Test p2pConnectionChange callback + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_p2pConnectionChange_0002', 0, async function (done) { + await wifi.on('p2pConnectionChange', recvP2pConnectionChangeFunc => { + console.info("[wifi_test] p2pConnectionChange result -> " + recvP2pConnectionChangeFunc); + expect(true).assertEqual(recvP2pConnectionChangeFunc !=null); + done(); + }); + setTimeout(function() { + console.info('[wifi_test] offP2pStateChange test start ...'); + wifi.off('p2pConnectionChange', recvP2pConnectionChangeFunc => { + console.info("p2pConnectionChange callback" + JSON.stringify(recvP2pConnectionChangeFunc)); + expect(true).assertEqual(recvP2pConnectionChangeFunc !=null); + }); + }, 1 * 1000); + done(); + }) + + /** + * @tc.number P2P_p2pDeviceChange_0003 + * @tc.name SUB_Communication_WiFi_P2P_p2pDeviceChange_0003 + * @tc.desc Test p2pDeviceChange callback + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION + */ + it('SUB_Communication_WiFi_P2P_p2pDeviceChange_0003', 0, async function (done) { + console.info('[wifi_test] Onp2pDeviceChange test start ...'); + await wifi.on('p2pDeviceChange', result => { + console.info("onP2pDeviceChange callback, result:" + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + done(); + }); + setTimeout(function() { + console.info('[wifi_test] offP2pDeviceChange test start ...'); + wifi.off('p2pDeviceChange', result => { + console.info("offP2pStateChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + }); + }, 1 * 1000); + done(); + }) + + /** + * @tc.number P2P_p2pPeerDeviceChange_0004 + * @tc.name SUB_Communication_WiFi_P2P_p2pPeerDeviceChange_0004 + * @tc.desc Test p2pDeviceChange callback + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION + */ + it('SUB_Communication_WiFi_P2P_p2pPeerDeviceChange_0004', 0, async function (done) { + let recvP2pPeerDeviceChangeFunc = result => { + console.info("wifi_test / p2p peer device change receive event: " + JSON.stringify(result)); + wifi.getP2pDevices((err, data) => { + if (err) { + console.error('wifi_test / failed to get getP2pDevices: ' + JSON.stringify(err)); + return; + } + console.info("wifi_test / getP2pDevices [callback] -> " + JSON.stringify(data)); + let len = Object.keys(data).length; + console.log("getP2pDevices number: " + len); + for (let i = 0; i < len; ++i) { + if (data[i].deviceName === "GRE") { + console.info("wifi_test / p2pConnect: -> " + data[i].deviceAddress); + let config = { + "deviceAddress":data[i].deviceAddress, + "netId":-1, + "passphrase":"", + "groupName":"", + "goBand":0, + } + wifi.p2pConnect(config); + } + } + }); + } + await wifi.on('p2pPeerDeviceChange', result => { + console.info("onP2pPeerDeviceChange callback, result:" + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + done(); + }); + setTimeout(function() { + wifi.off('p2pPeerDeviceChange', result => { + console.info("offP2pPeerDeviceChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + }); + }, 1 * 1000); + done(); + }) + + /** + * @tc.number P2P_p2pPersistentGroupChange_0005 + * @tc.name SUB_Communication_WiFi_P2P_p2pPersistentGroupChange_0005 + * @tc.desc Test p2pPersistentGroupChange callback + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_p2pPersistentGroupChange_0005', 0, async function (done) { + let recvP2pPersistentGroupChangeFunc = () => { + console.info("wifi_test / p2p persistent group change receive event"); + let config = { + "deviceAddress" : "02:11:65:f2:0d:6e", + "netId":-2, + "passphrase":"", + "groupName":"", + "goBand":0, + }; + let addConfig = wifi.createGroup(config); + expect(addConfig).assertTrue(); + wifi.getCurrentGroup((err, data) => { + if (err) { + console.error('wifi_test / failed to get getCurrentGroup: ' + JSON.stringify(err)); + return; + } + console.info("wifi_test / get getCurrentGroup [callback] -> " + JSON.stringify(data)); + }); + }; + wifi.on("p2pPersistentGroupChange",recvP2pPersistentGroupChangeFunc); + setTimeout(async function() { + wifi.off('p2pPersistentGroupChange', result => { + console.info("offP2pPersistentGroupChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + }); + }, 1 * 1000); + done(); + }) + + /** + * @tc.number P2P_p2pDiscoveryChange_0006 + * @tc.name SUB_Communication_WiFi_P2P_p2pDiscoveryChange_0006 + * @tc.desc Test p2pDiscoveryChange callback + * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P + * @permission ohos.permission.GET_WIFI_INFO + */ + it('SUB_Communication_WiFi_P2P_p2pDiscoveryChange_0006', 0, async function (done) { + await wifi.on('p2pDiscoveryChange', result => { + console.info("onp2pDiscoveryChange callback, result:" + JSON.stringify(result)); + expect(true).assertEqual((result !=null)); + done(); + }); + setTimeout(function() { + wifi.off('p2pDiscoveryChange', result => { + console.info("offp2pDiscoveryChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + }); + }, 1 * 1000); + 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 c598763f2..a815a54b3 100644 --- a/communication/wifi_standard/src/main/js/test/List.test.js +++ b/communication/wifi_standard/src/main/js/test/List.test.js @@ -12,9 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -require('./WifiSta.test.js') -//require('./WifiSoftAP.test.js') -import ActsWifiTest from './WifiSta.test.js' +import actsWifiTestNew from './WifiJsunit.test1.js' +import actsWifiTest from './WifiJsunit.testsame.js' export default function testsuite() { -ActsWifiTest() +actsWifiTestNew() +actsWifiTest() } + diff --git a/communication/wifi_standard/src/main/js/test/WifiJsunit.test1.js b/communication/wifi_standard/src/main/js/test/WifiJsunit.test1.js new file mode 100644 index 000000000..912273fb4 --- /dev/null +++ b/communication/wifi_standard/src/main/js/test/WifiJsunit.test1.js @@ -0,0 +1,363 @@ +/* + * 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 wifi from '@ohos.wifi' + +function sleep(delay) { + return new Promise(resovle => setTimeout(resovle, delay)) +} + +function checkWifiPowerOn(){ + console.info("wifi_test/wifi status:" + wifi.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, +} + +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 actsWifiTestNew() { + describe('actsWifiTestNew', function () { + beforeEach(function () { + checkWifiPowerOn(); + }) + afterEach(function () { + }) + + /** + * @tc.number CandidateNetWork_0001 + * @tc.name SUB_Communication_WiFi_XTS_CandidateNetWork_0001 + * @since 9 + * @tc.desc Test add OPEN and WEP CandidateConfig Promise API functionality. + * @syscap SystemCapability.Communication.WiFi.STA + * @permission ohos.permission.SET_WIFI_INFO + */ + 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 wifi.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_js] 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": "", + "preSharedKey": "ABCDEF1234", + "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)); + expect(true).assertEqual(netWorkId = -1); + }).catch((error) => { + console.error('[wifi_js] 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 CandidateNetWork_0001 + * @tc.name SUB_Communication_WiFi_XTS_CandidateNetWork_0002 + * @since 8 + * @tc.desc Test add PSK CandidateConfig and removeCandidateConfig Promise API functionality. + * @syscap SystemCapability.Communication.WiFi.STA + * @permission ohos.permission.SET_WIFI_INFO + */ + 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": "", + }; + await wifi.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_js] add PSK CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect().assertFail(); + }); + let getconfig = wifi.getCandidateConfigs(); + console.info("[wifi_test] wifi get PSK CandidateConfigs result : " + JSON.stringify(getconfig)); + expect(true).assertEqual(getconfig[0].securityType == wifiDeviceConfig.securityType); + expect(true).assertEqual(getconfig[0].isHiddenSsid == wifiDeviceConfig.isHiddenSsid); + expect(true).assertEqual(getconfig[0].ssid == wifiDeviceConfig.ssid); + var networkId = getconfig[0].netId; + console.info("[wifi_test] wifi get networkId result : " + JSON.stringify(networkId)); + 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)); + expect(true).assertEqual(getconfig1.length == 0); + }).catch((error) => { + console.error('[wifi_js] remove CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect().assertFail(); + }); + done(); + }) + + /** + * @tc.number CandidateNetWork_0003 + * @tc.name SUB_Communication_WiFi_XTS_CandidateNetWork_0003 + * @since 8 + * @tc.desc Test add SAE CandidateConfig Promise API functionality. + * @syscap SystemCapability.Communication.WiFi.STA + * @permission ohos.permission.SET_WIFI_INFO + */ + 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 wifi.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 getconfig = wifi.getCandidateConfigs(); + console.info("[wifi_test] wifi get SAE CandidateConfigs result : " + JSON.stringify(getconfig)); + done(); + }) + + /** + * @tc.number CandidateNetWork_0005 + * @tc.name SUB_Communication_WiFi_XTS_CandidateNetWork_0005 + * @since 8 + * @tc.desc Test add CandidateConfig and removeCandidateConfig callback API functionality. + * @syscap SystemCapability.Communication.WiFi.STA + * @permission ohos.permission.SET_WIFI_INFO + */ + it('SUB_Communication_WiFi_XTS_CandidateNetWork_0005', 0, async function (done) { + let wifiDeviceConfig = { + "ssid": "TYPE_PSK1", + "bssid": "", + "preSharedKey": "12345678", + "isHiddenSsid": false, + "securityType": WifiSecurityType.WIFI_SEC_TYPE_PSK, + } + function addCandidate() { + return new Promise((resolve, reject) => { + wifi.addCandidateConfig(wifiDeviceConfig, + (err, netWorkId) => { + if (err) { + console.info("[wifi_test]add CandidateConfig callback failed : " + JSON.stringify(err)); + return; + } + console.info("[wifi_test]addCandidateConfig callback result: " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId != -1); + resolve(); + }); + }); + } + await addCandidate(); + let configs = wifi.getCandidateConfigs(); + console.info("[wifi_test] wifi getCandidateConfigs result : " + JSON.stringify(configs)); + expect(true).assertEqual(configs[0].securityType == wifiDeviceConfig.securityType); + expect(true).assertEqual(configs[0].isHiddenSsid == wifiDeviceConfig.isHiddenSsid); + expect(true).assertEqual(configs[0].ssid == wifiDeviceConfig.ssid); + var networkId = configs[0].netId; + function removeCandidate() { + return new Promise((resolve, reject) => { + wifi.removeCandidateConfig(networkId, + (err, ret) => { + if (err) { + console.info("[wifi_test]removeCandidate callback failed : " + JSON.stringify(err)); + return; + } + console.info("[wifi_test] removeCandidateConfig callback result:" + JSON.stringify(ret)); + expect(ret).assertTrue(); + let configs1 = wifi.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 CandidateNetWork_0006 + * @tc.name SUB_Communication_WiFi_XTS_SUB_Communication_WiFi_XTS_CandidateNetWork_0006 + * @tc.desc Test connect To CandidateConfig API functionality. + * @since 9 + * @syscap SystemCapability.Communication.WiFi.STA + * @permission ohos.permission.SET_WIFI_INFO + */ + 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 wifi.addCandidateConfig(wifiDeviceConfig) + .then(netWorkId => { + console.info("[wifi_test]add CandidateConfig promise : " + JSON.stringify(netWorkId)); + expect(true).assertEqual(netWorkId != -1); + }).catch((error) => { + console.error('[wifi_js] add CandidateConfig promise failed -> ' + JSON.stringify(error)); + expect().assertFail(); + }); + let getconfig = wifi.getCandidateConfigs(); + console.info("[wifi_test] wifi get CandidateConfigs result : " + JSON.stringify(getconfig)); + let connectToCandidate = wifi.connectToCandidateConfig(getconfig[0].netId); + console.info("[wifi_test] connect To CandidateConfig result : " + JSON.stringify(connectToCandidate)); + await sleep(3000); + await wifi.getLinkedInfo() + .then((result) => { + console.info("[wifi_test] get wifi link [promise] -> " + JSON.stringify(result)); + expect(JSON.stringify(result)).assertContain('band'); + done(); + }).catch((error) => { + console.info("[wifi_test] promise then error." + JSON.stringify(error)); + expect().assertFail(); + }); + done(); + }) + + + /** + * @tc.number Sta_0002 + * @tc.name SUB_Communication_WiFi_XTS_Sta_0002 + * @tc.desc Test get to ScanInfos promise and callback API functionality. + * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA + * @permission ohos.permission.GET_WIFI_INFO and (ohos.permission.GET_WIFI_PEERS_MAC or ohos.permission.LOCATION) + */ + it('SUB_Communication_WiFi_XTS_Sta_0002', 0, async function (done) { + await wifi.getScanInfos() + .then(result => { + let clen = Object.keys(result).length; + expect(clen).assertLarger(0); + console.info("[wifi_test] getScanInfos promise result " + JSON.stringify(result)); + }); + function getScanInfos() { + return new Promise((resolve, reject) => { + wifi.getScanInfos( + (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 + + "infoElems: " + result[j].infoElems); + } + } + resolve(); + }); + }); + } + await getScanInfos(); + done(); + }) + + /** + * @tc.number Sta_0034 + * @tc.name SUB_Communication_WiFi_XTS_Sta_0034 + * @tc.desc Test get to ScanInfos Sync API functionality. + * @since 9 + * @syscap SystemCapability.Communication.WiFi.STA + * @permission ohos.permission.GET_WIFI_INFO and (ohos.permission.GET_WIFI_PEERS_MAC or ohos.permission.LOCATION) + */ + it('SUB_Communication_WiFi_XTS_Sta_0034', 0, async function (done) { + let getScanInfos = wifi.getScanInfosSync(); + console.info("[wifi_test] wifi get to ScanInfosSync result : " + JSON.stringify(getScanInfos)); + let lenth = Object.keys(getScanInfos).length; + console.info("[wifi_test] wifi ScanInfosSync length result : " + JSON.stringify(lenth)); + expect(lenth).assertLarger(0); + done(); + }) + }) +} + + diff --git a/communication/wifi_standard/src/main/js/test/WifiSta.test.js b/communication/wifi_standard/src/main/js/test/WifiJsunit.testsame.js similarity index 77% rename from communication/wifi_standard/src/main/js/test/WifiSta.test.js rename to communication/wifi_standard/src/main/js/test/WifiJsunit.testsame.js index 1bdec2e3d..a94f3499e 100644 --- a/communication/wifi_standard/src/main/js/test/WifiSta.test.js +++ b/communication/wifi_standard/src/main/js/test/WifiJsunit.testsame.js @@ -1,484 +1,479 @@ -/* - * 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 wifi from '@ohos.wifi' - -function sleep(delay) { - var start = (new Date()).getTime(); - while ((new Date()).getTime() - start > delay) { - break; - } -} - -function checkWifiPowerOn(){ - console.info("Wifi_test/wifi status:" + wifi.isWifiActive()); -} - -function resolve(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, -} - -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_280MHZ_PLUS : 4, - WIDTH_INVALID : null, -} - -export default function ActsWifiTest() { -describe('ActsWifiTest', function() { - - beforeEach(function() { - checkWifiPowerOn(); - }) - - afterEach(function() {}) - - - /** - * @tc.number open_0001 - * @tc.name SUB_Communication_WiFi_Sta_Open_0001 - * @since 6 - * @tc.desc Test wifi.isWifiActive API functionality. - */ - it('SUB_Communication_WiFi_Sta_WifiActive_0001', 0, function() { - sleep(3000); - console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); - expect(wifi.isWifiActive()).assertTrue(); - }) - - /** - * @tc.number Scan_0001 - * @tc.name SUB_Communication_WiFi_Sta_Scan_0001 - * @tc.desc Test get ScanInfos callback API functionality. - */ - it('SUB_Communication_WiFi_Sta_Scan_0001', 0, async function(done) { - let scan = wifi.scan(); - sleep(3000); - console.log("[wifi_test] open wifi scan result: " + scan); - expect(scan).assertTrue(); - - await wifi.getScanInfos() - .then(result => { - let clen = Object.keys(result).length; - expect(clen).assertLarger(0); - console.info("[wifi_test] getScanInfos promise result " + JSON.stringify(result)); - }); - - wifi.getScanInfos( - (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 + - "timestamp" + result[j].timestamp + "capabilities" + result[j].capabilities - + "channelWidth: " + result[j].channelWidth+"centerFrequency0"+result[j].centerFrequency0 - +"centerFrequency1"+result[j].centerFrequency1+"infoElems"+result[j].infoElems - ); - } - } - done() - }); - }) - - /** - * @tc.number Scan_0004 - * @tc.name SUB_Communication_WiFi_Sta_Scan_0004 - * @since 7 - * @tc.desc Test wifi.getSignalLevel API functionality. - */ - it('SUB_Communication_WiFi_Sta_Scan_0004', 0, function() { - console.info("[wifi_test] check the 2.4G rssi assgined to level test."); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-65,1)); - expect(wifi.getSignalLevel(-65, 1)).assertEqual(4); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-66,1)); - expect(wifi.getSignalLevel(-66, 1)).assertEqual(3); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-75,1)); - expect(wifi.getSignalLevel(-75, 1)).assertEqual(3); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-76,1)); - expect(wifi.getSignalLevel(-76, 1)).assertEqual(2); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-82,1)); - expect(wifi.getSignalLevel(-82, 1)).assertEqual(2); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-83,1)); - expect(wifi.getSignalLevel(-83, 1)).assertEqual(1); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-88,1)); - expect(wifi.getSignalLevel(-88, 1)).assertEqual(1); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-89,1)); - expect(wifi.getSignalLevel(-89, 1)).assertEqual(0); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-127,1)); - expect(wifi.getSignalLevel(-127, 1)).assertEqual(0); - console.info("[wifi_test] check the 5G rssi assgined to level test."); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-65,2)); - expect(wifi.getSignalLevel(-65, 2)).assertEqual(4); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-66,2)); - expect(wifi.getSignalLevel(-66, 2)).assertEqual(3); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-72,2)); - expect(wifi.getSignalLevel(-72, 2)).assertEqual(3); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-73,2)); - expect(wifi.getSignalLevel(-73, 2)).assertEqual(2); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-79,2)); - expect(wifi.getSignalLevel(-79, 2)).assertEqual(2); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-80,2)); - expect(wifi.getSignalLevel(-80, 2)).assertEqual(1); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-85,2)); - expect(wifi.getSignalLevel(-85, 2)).assertEqual(1); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-86,2)); - expect(wifi.getSignalLevel(-86, 2)).assertEqual(0); - console.info("[wifi_test] getSignalLevel" + wifi.getSignalLevel(-127,2)); - expect(wifi.getSignalLevel(-127, 2)).assertEqual(0); - - }) - - - /** - * @tc.number SUB_Communication_WiFi_Sta_info_0002 - * @tc.name testgetCountryCode - * @tc.desc Test getCountryCode api. - * @since 7 - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 3 - */ - it('SUB_Communication_WiFi_Sta_Info_0002', 0, function() { - expect(wifi.isWifiActive()).assertTrue(); - let countryCode = wifi.getCountryCode(); - console.info("[wifi_test] getCountryCode -> " + JSON.stringify(countryCode)); - console.info("[wifi_test] getCountryCode.length -> " + JSON.stringify(countryCode.length)); - expect(true).assertEqual(countryCode.length == 2); - }) - - /** - * @tc.number SUB_Communication_WiFi_Sta_info_0004 - * @tc.name testFeatureSupported - * @tc.desc Test FeatureSupported api. - * @since 7 - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 3 - */ - - it('SUB_Communication_WiFi_Sta_info_0004', 0, function () { - expect(wifi.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 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_INFRA); - console.info("[wifi_test] isFeatureSupported -> " + isSupport1); - let isSupport2 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_INFRA_5G); - console.info("[wifi_test] isFeatureSupported2 -> " + isSupport2); - let isSupport3 = wifi.isFeatureSupported(WifiUtils.WIFI_GAS_ANQP); - console.info("[wifi_test] isFeatureSupported3 -> " + isSupport3); - let isSupport4 = wifi.isFeatureSupported(WifiUtils.WIFI_WIFI_DIRECT); - console.info("[wifi_test] isFeatureSupported4 -> " + isSupport4); - let isSupport5 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_MOBILE_HOTSPOT); - console.info("[wifi_test] isFeatureSupported5 -> " + isSupport5); - let isSupport6 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_AWARE); - console.info("[wifi_test] isFeatureSupported6 -> " + isSupport6); - let isSupport7 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_AP_STA); - console.info("[wifi_test] isFeatureSupported7 -> " + isSupport7); - let isSupport8 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_WPA3_SAE); - console.info("[wifi_test] isFeatureSupported8 -> " + isSupport8); - let isSupport9 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_WPA3_SUITE_B); - console.info("[wifi_test] isFeatureSupported9 -> " + isSupport9); - let isSupport = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_OWE); - console.info("[wifi_test] isFeatureSupported -> " + isSupport); - }) - - /** - * @tc.number conn_Config_0002 - * @tc.name SUB_Communication_WiFi_Sta_Conn_Info_0002 - * @since 7 - * @tc.desc Test getLinkedInfo information - */ - it('SUB_Communication_WiFi_Sta_Conn_Info_0002', 0, async function(done) { - let isConnected = wifi.isConnected(); - expect(isConnected).assertFalse(); - await wifi.getLinkedInfo() - .then((result) => { - console.info("[wifi_test] get wifi link [promise] -> " + JSON.stringify(result)); - expect(JSON.stringify(result)).assertContain('band'); - done(); - }).catch((error) => { - console.info("[wifi_test] promise then error." + JSON.stringify(error)); - expect().assertFail(); - }); - }) - - /** - * @tc.number conn_Config_0003 - * @tc.name SUB_Communication_WiFi_Sta_Conn_Info_0003 - * @since 7 - * @tc.desc Test getLinkedInfo callback information - */ - it('SUB_Communication_WiFi_Sta_Conn_Info_0003', 0, async function(done) { - wifi.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 = wifi.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); - } - done(); - }); - }) - - /** - * @tc.number Conn_Info_0001 - * @tc.name SUB_Communication_WiFi_Sta_Conn_Info_0003 - * @since 7 - * @tc.desc Test get IpInfo information - */ - it('SUB_Communication_WiFi_Sta_Conn_Info_0003', 0, function () { - let isConnected= wifi.isConnected(); - expect(isConnected).assertFalse(); - let ipInfo = wifi.getIpInfo(); - expect(JSON.stringify(ipInfo)).assertContain("gateway"); - let ipAddress = resolveIP(ipInfo.ipAddress); - console.info("ipAddress result: " + ipAddress); - console.info("gateway: " + ipInfo.gateway + "ipAddress: " + ipInfo.ipAddress - + "leaseDuration: " + ipInfo.leaseDuration + - "leaseDuration: " + ipInfo.leaseDuration + - "netmask: " + ipInfo.netmask + "primaryDns:" + ipInfo.primaryDns + - "secondDns: " + ipInfo.secondDns + "serverIp: " + ipInfo.serverIp ); - }) - - /** - * @tc.number wifiStateChange_0001 - * @tc.name SUB_Communication_WiFi_Sta_wifiStateChange_0001 - * @since 7 - * @tc.desc Test wifiStateChange callback - */ - it('SUB_Communication_WiFi_Sta_wifiStateChange_0001', 0, async function (done) { - wifi.on('wifiStateChange', async result => { - console.info("wifiStateChange callback, result:" + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - let promise = new Promise((resolve) => { - wifi.off('wifiStateChange', result => { - console.info("offwifiStateChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - resolve() - }); - }) - await promise.then(done) - }); - done(); - }) - - /** - * @tc.number wifiConnectionChange_0002 - * @tc.name SUB_Communication_WiFi_Sta_wifiConnectionChange_0002 - * @since 7 - * @tc.desc Test wifiStateChange callback - */ - it('SUB_Communication_WiFi_Sta_wifiConnectionChange_0002', 0, async function (done) { - wifi.on('wifiConnectionChange', async result => { - console.info("wifiConnectionChange callback, result:" + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - let promise = new Promise((resolve) => { - console.info('[wifi_test] offwifiConnectionChange test start ...'); - wifi.off('wifiConnectionChange', result => { - console.info("offwifiConnectionChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - resolve() - }); - }) - await promise.then(done) - }); - done(); - }) - - /** - * @tc.number wifiScanStateChange_0003 - * @tc.name SUB_Communication_WiFi_Sta_wifiScanStateChange_0003 - * @since 7 - * @tc.desc Test wifiScanStateChange callback - */ - it('SUB_Communication_WiFi_Sta_wifiScanStateChange_0003', 0, async function (done) { - wifi.on('wifiScanStateChange', async result => { - console.info("wifiScanStateChange callback, result:" + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - let promise = new Promise((resolve) => { - console.info('[wifi_test] offwifiScanStateChange test start ...'); - wifi.off('wifiScanStateChange', result => { - console.info("offwifiScanStateChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - resolve() - }); - }) - await promise.then(done) - }); - let scan = wifi.scan(); - sleep(3000); - done(); - - }) - - /** - * @tc.number wifiRssiChange_0004 - * @tc.name SUB_Communication_WiFi_Sta_wifiRssiChange_0004 - * @since 7 - * @tc.desc Test wifiRssiChange callback - */ - it('SUB_Communication_WiFi_Sta_wifiRssiChange_0004', 0, async function (done) { - wifi.on('wifiRssiChange', async result => { - console.info("wifiRssiChange callback, result:" + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - let promise = new Promise((resolve) => { - console.info('[wifi_test] offwifiRssiChange test start ...'); - wifi.off('wifiRssiChange', result => { - console.info("offwifiRssiChange callback, result: " + JSON.stringify(result)); - expect(true).assertEqual(result !=null); - resolve() - }); - }) - await promise.then(done) - }); - done(); - - }) - - /** - * @tc.number SUB_Communication_WiFi_Hotspot_ON_0001 - * @tc.name testhotspotStateChangeOn - * @tc.desc Test hotspotStateChangeOn api. - * @since 7 - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 3 - */ - it('SUB_Communication_WiFi_Hotspot_ON_0001', 0, async function (done) { - console.info("[wifi_test]hotspotStateChange On test"); - try { - await wifi.on('hotspotStateChange', (data) => { - console.info("[wifi_test] hotspotStateChange On ->" + data); - expect(true).assertEqual(data != null); - }); - - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - - /** - * @tc.number SUB_Communication_WiFi_Hotspot_Off_0002 - * @tc.name testhotspotStateChangeOff - * @tc.desc Test hotspotStateChange api. - * @since 7 - * @tc.size MEDIUM - * @tc.type Function - * @tc.level Level 3 - */ - it('SUB_Communication_WiFi_Hotspot_Off_0002', 0, async function (done) { - try { - await wifi.off('hotspotStateChange', (data) => { - console.info("[wifi_test] hotspotStateChange Off ->" + data); - expect(true).assertEqual(data != null); - console.info("[wifi_test] wifiRssiChange unregister end"); - }); - - }catch(e) { - expect(null).assertFail(); - } - done(); - }) - console.log("*************[wifi_test] start wifi js unit test end*************"); -}) -} - +/* + * 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 wifi from '@ohos.wifi' + +function sleep(delay) { // delay x ms + var start = (new Date()).getTime(); + while ((new Date()).getTime() - start > delay) { + break; + } +} +function checkWifiPowerOn(){ + console.info("wifi_test/wifi status:" + wifi.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, +} + +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 actsWifiTest() { +describe('actsWifiTest', function() { + beforeEach(function() { + checkWifiPowerOn(); + }) + afterEach(function() { + }) + + /** + * @tc.number open_0001 + * @tc.name SUB_Communication_WiFi_Sta_Open_0001 + * @since 6 + * @tc.desc Test wifi.isWifiActive API functionality. + */ + it('SUB_Communication_WiFi_Sta_WifiActive_0001', 0, function() { + sleep(3000); + console.log("[wifi_test] check the state of wifi: " + wifi.isWifiActive()); + expect(wifi.isWifiActive()).assertTrue(); + }) + + /** + * @tc.number Scan_0001 + * @tc.name SUB_Communication_WiFi_Sta_Scan_0001 + * @since 6 + * @tc.desc Test get ScanInfos callback API functionality. + */ + it('SUB_Communication_WiFi_Sta_Scan_0001', 0, async function(done) { + let scan = wifi.scan(); + sleep(3000); + console.log("[wifi_test] open wifi scan result: " + scan); + expect(scan).assertTrue(); + await wifi.getScanInfos() + .then(result => { + let clen = Object.keys(result).length; + expect(clen).assertLarger(0); + console.info("[wifi_test] getScanInfos promise result " + JSON.stringify(result)); + }); + wifi.getScanInfos( + (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 + + "timestamp" + result[j].timestamp + "capabilities" + result[j].capabilities + + "channelWidth: " + result[j].channelWidth+ "centerFrequency0: " + + result[j].centerFrequency0 + + "centerFrequency1: " + result[j].centerFrequency1 + +"infoElems: " + result[j].infoElems); + } + } + done() + }); + }) + + /** + * @tc.number Scan_0004 + * @tc.name SUB_Communication_WiFi_Sta_Scan_0004 + * @since 7 + * @tc.desc Test wifi.getSignalLevel API functionality. + */ + it('SUB_Communication_WiFi_Sta_Scan_0004', 0, function() { + console.info("[wifi_test] check the 2.4G rssi assgined to level test."); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-65, 1)); + expect(wifi.getSignalLevel(-65, 1)).assertEqual(4); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-66, 1)); + expect(wifi.getSignalLevel(-66, 1)).assertEqual(3); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-75, 1)); + expect(wifi.getSignalLevel(-75, 1)).assertEqual(3); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-76, 1)); + expect(wifi.getSignalLevel(-76, 1)).assertEqual(2); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-82, 1)); + expect(wifi.getSignalLevel(-82, 1)).assertEqual(2); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-83, 1)); + expect(wifi.getSignalLevel(-83, 1)).assertEqual(1); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-88, 1)); + expect(wifi.getSignalLevel(-88, 1)).assertEqual(1); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-89, 1)); + expect(wifi.getSignalLevel(-89, 1)).assertEqual(0); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-127, 1)); + expect(wifi.getSignalLevel(-127, 1)).assertEqual(0); + + console.info("[wifi_test] check the 5G rssi assgined to level test."); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-65, 2)); + expect(wifi.getSignalLevel(-65, 2)).assertEqual(4); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-66, 2)); + expect(wifi.getSignalLevel(-66, 2)).assertEqual(3); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-72, 2)); + expect(wifi.getSignalLevel(-72, 2)).assertEqual(3); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-73, 2)); + expect(wifi.getSignalLevel(-73, 2)).assertEqual(2); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-79, 2)); + expect(wifi.getSignalLevel(-79, 2)).assertEqual(2); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-80, 2)); + expect(wifi.getSignalLevel(-80, 2)).assertEqual(1); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-85, 2)); + expect(wifi.getSignalLevel(-85, 2)).assertEqual(1); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-86, 2)); + expect(wifi.getSignalLevel(-86, 2)).assertEqual(0); + console.info("[wifi_test] getSignalLevel " + wifi.getSignalLevel(-127, 2)); + expect(wifi.getSignalLevel(-127, 2)).assertEqual(0); + }) + + /** + * @tc.number SUB_Communication_WiFi_Sta_info_0002 + * @tc.name testgetCountryCode + * @tc.desc Test getCountryCode api. + * @since 7 + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Sta_Info_0002', 0, function() { + expect(wifi.isWifiActive()).assertTrue(); + let countryCode = wifi.getCountryCode(); + console.info("[wifi_test] getCountryCode -> " + JSON.stringify(countryCode)); + let countrylen = countryCode.length; + console.info("[wifi_test] getCountryCode.length -> " + JSON.stringify(countrylen)); + expect(true).assertEqual(countrylen ==2); + }) + + /** + * @tc.number SUB_Communication_WiFi_Sta_info_0004 + * @tc.name testFeatureSupported + * @tc.desc Test FeatureSupported api. + * @since 7 + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Sta_info_0004', 0, function () { + expect(wifi.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 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_INFRA); + console.info("[wifi_test] isFeatureSupported -> " + isSupport1); + let isSupport2 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_INFRA_5G); + console.info("[wifi_test] isFeatureSupported2 -> " + isSupport2); + let isSupport3 = wifi.isFeatureSupported(WifiUtils.WIFI_GAS_ANQP); + console.info("[wifi_test] isFeatureSupported3 -> " + isSupport3); + let isSupport4 = wifi.isFeatureSupported(WifiUtils.WIFI_WIFI_DIRECT); + console.info("[wifi_test] isFeatureSupported4 -> " + isSupport4); + let isSupport5 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_MOBILE_HOTSPOT); + console.info("[wifi_test] isFeatureSupported5 -> " + isSupport5); + let isSupport6 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_AWARE); + console.info("[wifi_test] isFeatureSupported6 -> " + isSupport6); + let isSupport7 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_AP_STA); + console.info("[wifi_test] isFeatureSupported7 -> " + isSupport7); + let isSupport8 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_WPA3_SAE); + console.info("[wifi_test] isFeatureSupported8 -> " + isSupport8); + let isSupport9 = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_WPA3_SUITE_B); + console.info("[wifi_test] isFeatureSupported9 -> " + isSupport9); + let isSupport = wifi.isFeatureSupported(WifiUtils.WIFI_FEATURE_OWE); + console.info("[wifi_test] isFeatureSupported -> " + isSupport); + }) + + /** + * @tc.number conn_Config_0002 + * @tc.name SUB_Communication_WiFi_Sta_Conn_Info_0002 + * @since 7 + * @tc.desc Test getLinkedInfo information + */ + it('SUB_Communication_WiFi_Sta_Conn_Info_0002', 0, async function(done) { + let isConnected= wifi.isConnected(); + expect(isConnected).assertFalse(); + await wifi.getLinkedInfo() + .then((result) => { + console.info("[wifi_test] get wifi link [promise] -> " + JSON.stringify(result)); + expect(JSON.stringify(result)).assertContain('band'); + done(); + }).catch((error) => { + console.info("[wifi_test] promise then error." + JSON.stringify(error)); + expect().assertFail(); + }); + }) + + /** + * @tc.number conn_Config_0003 + * @tc.name SUB_Communication_WiFi_Sta_Conn_Info_0003 + * @since 7 + * @tc.desc Test getLinkedInfo callback information + */ + it('SUB_Communication_WiFi_Sta_Conn_Info_0003', 0, async function(done) { + wifi.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 = wifi.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); + } + done(); + }); + }) + + /** + * @tc.number Conn_Info_0003 + * @tc.name SUB_Communication_WiFi_Sta_Conn_Info_0003 + * @since 7 + * @tc.desc Test get IpInfo information + */ + it('SUB_Communication_WiFi_Sta_Conn_Info_0001', 0, function () { + let isConnected= wifi.isConnected(); + expect(isConnected).assertFalse(); + let ipInfo = wifi.getIpInfo(); + expect(JSON.stringify(ipInfo)).assertContain("gateway"); + let ipAddress = resolveIP(ipInfo.ipAddress); + console.info("ipAddress result: " + ipAddress); + console.info("gateway: " + ipInfo.gateway + "ipAddress: " + ipInfo.ipAddress + + "leaseDuration: " + ipInfo.leaseDuration + + "leaseDuration: " + ipInfo.leaseDuration + + "netmask: " + ipInfo.netmask + "primaryDns:" + ipInfo.primaryDns + + "secondDns: " + ipInfo.secondDns + "serverIp: " + ipInfo.serverIp ); + }) + + /** + * @tc.number wifiStateChange_0001 + * @tc.name SUB_Communication_WiFi_Sta_wifiStateChange_0001 + * @since 7 + * @tc.desc Test wifiStateChange callback + */ + it('SUB_Communication_WiFi_Sta_wifiStateChange_0001', 0, async function (done) { + wifi.on('wifiStateChange', async result => { + console.info("wifiStateChange callback, result:" + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + let promise = new Promise((resolve) => { + wifi.off('wifiStateChange', result => { + console.info("offwifiStateChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + resolve() + }); + }) + await promise.then(done) + }); + done(); + + }) + + /** + * @tc.number wifiConnectionChange_0002 + * @tc.name SUB_Communication_WiFi_Sta_wifiConnectionChange_0002 + * @since 7 + * @tc.desc Test wifiStateChange callback + */ + it('SUB_Communication_WiFi_Sta_wifiConnectionChange_0002', 0, async function (done) { + wifi.on('wifiConnectionChange', async result => { + console.info("wifiConnectionChange callback, result:" + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + let promise = new Promise((resolve) => { + console.info('[wifi_test] offwifiConnectionChange test start ...'); + wifi.off('wifiConnectionChange', result => { + console.info("offwifiConnectionChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + resolve() + }); + }) + await promise.then(done) + }); + done(); + + }) + + /** + * @tc.number wifiScanStateChange_0003 + * @tc.name SUB_Communication_WiFi_Sta_wifiScanStateChange_0003 + * @since 7 + * @tc.desc Test wifiScanStateChange callback + */ + it('SUB_Communication_WiFi_Sta_wifiScanStateChange_0003', 0, async function (done) { + wifi.on('wifiScanStateChange', async result => { + console.info("wifiScanStateChange callback, result:" + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + let promise = new Promise((resolve) => { + console.info('[wifi_test] offwifiScanStateChange test start ...'); + wifi.off('wifiScanStateChange', result => { + console.info("offwifiScanStateChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + resolve() + }); + }) + await promise.then(done) + }); + let scan = wifi.scan(); + sleep(3000); + done(); + + }) + + /** + * @tc.number wifiRssiChange_0004 + * @tc.name SUB_Communication_WiFi_Sta_wifiRssiChange_0004 + * @since 7 + * @tc.desc Test wifiRssiChange callback + */ + it('SUB_Communication_WiFi_Sta_wifiRssiChange_0004', 0, async function (done) { + wifi.on('wifiRssiChange', async result => { + console.info("wifiRssiChange callback, result:" + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + let promise = new Promise((resolve) => { + console.info('[wifi_test] offwifiRssiChange test start ...'); + wifi.off('wifiRssiChange', result => { + console.info("offwifiRssiChange callback, result: " + JSON.stringify(result)); + expect(true).assertEqual(result !=null); + resolve() + }); + }) + await promise.then(done) + }); + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Hotspot_ON_0001 + * @tc.name testhotspotStateChangeOn + * @since 7 + * @tc.desc Test hotspotStateChangeOn api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Hotspot_ON_0001', 0, async function (done) { + console.info("[wifi_test]hotspotStateChange On test"); + try { + await wifi.on('hotspotStateChange', (data) => { + console.info("[wifi_test] hotspotStateChange On ->" + data); + expect(true).assertEqual(data != null); + }); + + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + + /** + * @tc.number SUB_Communication_WiFi_Hotspot_Off_0002 + * @tc.name testhotspotStateChangeOff + * @since 7 + * @tc.desc Test hotspotStateChange api. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + */ + it('SUB_Communication_WiFi_Hotspot_Off_0002', 0, async function (done) { + console.info("[wifi_test]hotspotStateChange Off test"); + try { + await wifi.off('hotspotStateChange', (data) => { + console.info("[wifi_test] hotspotStateChange Off ->" + data); + expect(true).assertEqual(data != null); + }); + }catch(e) { + expect(null).assertFail(); + } + done(); + }) + console.log("*************[wifi_test] start wifi js unit test end*************"); +}) +} + + -- GitLab