WifiStationEvent.test.js 5.6 KB
Newer Older
Q
quanli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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'

Q
quanli 已提交
20

Q
quanli 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
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 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 actsWifiEventTest() {
    describe('actsWifiEventTest', function() {
        beforeEach(function () {
            checkWifiPowerOn();
        })
        afterEach(function () {
        })

        /**
        * @tc.number SUB_Communication_WiFi_Event_Test_0001
Q
quanli 已提交
78
        * @tc.name testWifiStateChange
Q
quanli 已提交
79 80 81 82 83
        * @tc.desc Test wifiStateChange callback
        * @tc.type Function
        * @tc.level Level 3
        */
        it('SUB_Communication_WiFi_Event_Test_0001', 0, async function (done) {
Q
quanli 已提交
84 85 86 87 88 89 90
            let wifiState = "wifiStateChange";
            let wifiStateChangeCallback = result => {
                console.info("[wifi_test]wifiStateChange callback, result: " + JSON.stringify(result));
            }
            wifi.on(wifiState, wifiStateChangeCallback);
            await sleep(3000);
            wifi.off(wifiState, wifiStateChangeCallback);
Q
quanli 已提交
91 92 93 94 95
            done();
        })

        /**
        * @tc.number SUB_Communication_WiFi_Event_Test_0002
Q
quanli 已提交
96
        * @tc.name testWifiConnectionChange
Q
quanli 已提交
97 98 99 100 101
        * @tc.desc Test wifiConnectionChange callback
        * @tc.type Function
        * @tc.level Level 3
        */
        it('SUB_Communication_WiFi_Event_Test_0002', 0, async function (done) {
Q
quanli 已提交
102 103 104 105 106 107 108
            let wifiConnectionState = "wifiConnectionChange";
            let wifiConnectionChangeCallback = result => {
                console.info("[wifi_test]wifiConnectionChange callback, result: " + JSON.stringify(result));
            }
            wifi.on(wifiConnectionState, wifiConnectionChangeCallback);
            await sleep(3000);
            wifi.off(wifiConnectionState, wifiConnectionChangeCallback);
Q
quanli 已提交
109 110 111 112 113
            done();
        })

        /**
        * @tc.number SUB_Communication_WiFi_Event_Test_0003
Q
quanli 已提交
114 115
        * @tc.name testWifiScanStateChange
        * @tc.desc Test wifiScanStateChange  callback
Q
quanli 已提交
116 117 118 119
        * @tc.type Function
        * @tc.level Level 3
        */
        it('SUB_Communication_WiFi_Event_Test_0003', 0, async function (done) {
Q
quanli 已提交
120 121 122 123 124
            let wifiScanState = "wifiScanStateChange";
            let wifiScanStateChangeCallback = result => {
                console.info("[wifi_test]wifiScanStateChange callback, result: " + JSON.stringify(result));
            }
            wifi.on(wifiScanState, wifiScanStateChangeCallback);
Q
quanli 已提交
125 126
            let scanResult = wifi.scan();
            await sleep(3000);
Q
quanli 已提交
127
            wifi.off(wifiScanState, wifiScanStateChangeCallback);
Q
quanli 已提交
128 129 130 131 132
            done();
        })

        /**
        * @tc.number SUB_Communication_WiFi_Event_Test_0004
Q
quanli 已提交
133
        * @tc.name testWifiRssiChange
Q
quanli 已提交
134 135 136 137 138
        * @tc.desc Test wifiRssiChange callback
        * @tc.type Function
        * @tc.level Level 3
        */
        it('SUB_Communication_WiFi_Event_Test_0004', 0, async function (done) {
Q
quanli 已提交
139 140 141 142 143 144 145
            let wifiRssiState = "wifiRssiChange";
            let wifiRssiChangeCallback = result => {
                console.info("[wifi_test]wifiRssiChange callback, result: " + JSON.stringify(result));
            }
            wifi.on(wifiRssiState, wifiRssiChangeCallback);
            await sleep(3000);
            wifi.off(wifiRssiState, wifiRssiChangeCallback);
Q
quanli 已提交
146 147 148 149 150
            done();
        })

        /**
        * @tc.number SUB_Communication_WiFi_Event_Test_0005
Q
quanli 已提交
151
        * @tc.name testHotspotStateChange
Q
quanli 已提交
152 153 154 155 156
        * @tc.desc Test hotspotStateChange api.
        * @tc.type Function
        * @tc.level Level 3
        */
        it('SUB_Communication_WiFi_Event_Test_0005', 0, async function (done) {
Q
quanli 已提交
157 158 159
            let hotspotState = "hotspotStateChange";
            let hotspotStateChangeCallback = result => {
                console.info("[wifi_test]hotspotStateChange callback, result: " + JSON.stringify(result));
Q
quanli 已提交
160
            }
Q
quanli 已提交
161 162 163
            wifi.on(hotspotState, hotspotStateChangeCallback);
            await sleep(3000);
            wifi.off(hotspotState, hotspotStateChangeCallback);
Q
quanli 已提交
164 165 166 167 168 169
            done();
        })
        console.log("*************[wifi_test] start wifi js unit test end*************");
        })
}

Q
quanli 已提交
170