WifiStationFunctions.test.js 15.2 KB
Newer Older
Q
quanli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import osaccount from '@ohos.account.osAccount'
import bundle from '@ohos.bundle'
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'

async function applyPermission() {
    let osAccountManager = osaccount.getAccountManager();
    console.info("=== getAccountManager finish");
    let localId = await osAccountManager.getOsAccountLocalIdFromProcess();
    console.info("LocalId is :" + localId);
    let appInfo = await bundle.getApplicationInfo('ohos.acts.communication.wifi.wifidevice', 0, localId);
    let atManager = abilityAccessCtrl.createAtManager();
    if (atManager != null) {
        let tokenID = appInfo.accessTokenId;
        console.info('[permission] case accessTokenID is ' + tokenID);
        let permissionName1 = 'ohos.permission.LOCATION';
        await atManager.grantUserGrantedPermission(tokenID, permissionName1, 1).then((result) => {
            console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result));
        }).catch((err) => {
            console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err));
        });
    } else {
        console.info('[permission] case apply permission failed, createAtManager failed');
    }
}
Q
quanli 已提交
43

Q
quanli 已提交
44 45
function sleep(delay) {
    return new Promise(resovle => setTimeout(resovle, delay))
Q
quanli 已提交
46
}
Q
quanli 已提交
47

Q
quanli 已提交
48 49 50 51 52 53 54 55 56
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);
}

export default function actsWifiFunctionsTest() {
    describe('actsWifiFunctionsTest', function() {
Q
quanli 已提交
57 58 59 60 61 62
        beforeAll(async function (done) {
            console.info('beforeAll case');
            await applyPermission();
            done();
        })

Q
quanli 已提交
63 64 65 66 67 68 69
        beforeEach(function () {
            checkWifiPowerOn();
        })
        afterEach(function () {
        })

        /**
Q
quanli 已提交
70
        * @tc.number Communication_WiFi_XTS_Sta_0002
Q
quanli 已提交
71
        * @tc.name testgetScanInfos
Q
quanli 已提交
72
        * @tc.desc Test getScanInfos promise and callback API functionality.
Q
quanli 已提交
73 74 75
        * @tc.type Function
        * @tc.level Level 0
        */
Q
quanli 已提交
76
        it('Communication_WiFi_XTS_Sta_0002', 0, async function (done) {
Q
quanli 已提交
77 78 79
            await wifi.getScanInfos()
                .then(result => {
                    let clen = Object.keys(result).length;
Q
quanli 已提交
80
                    expect(true).assertEqual(clen >= 0);
Q
quanli 已提交
81 82
                    console.info("[wifi_test]getScanInfos promise result:" + JSON.stringify(result));
                });
Q
quanli 已提交
83
            function getScan() {
Q
quanli 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
                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 +
Q
quanli 已提交
99
                                    "timestamp" + result[j].timestamp + "capabilities" + result[j].capabilities);
Q
quanli 已提交
100 101 102 103 104 105
                                }
                            }
                            resolve();
                        });
                });
            }
Q
quanli 已提交
106
            await getScan();
Q
quanli 已提交
107 108 109 110
            done();
        })

        /**
Q
quanli 已提交
111
        * @tc.number Communication_WiFi_XTS_Sta_0021
Q
quanli 已提交
112 113
        * @tc.name testGetSignalLevel
        * @tc.desc Test getSignalLevel API functionality..
Q
quanli 已提交
114 115 116
        * @tc.type Function
        * @tc.level Level 3
        */
Q
quanli 已提交
117
        it('Communication_WiFi_XTS_Sta_0021', 0, function () {
Q
quanli 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
            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);
        })

        /**
Q
quanli 已提交
160
        * @tc.number Communication_WiFi_XTS_Sta_0017
Q
quanli 已提交
161 162 163 164 165
        * @tc.name testgetCountryCode
        * @tc.desc Test getCountryCode API function.
        * @tc.type Function
        * @tc.level Level 3
        */
Q
quanli 已提交
166
        it('Communication_WiFi_XTS_Sta_0017', 0, function () {
Q
quanli 已提交
167 168 169 170 171 172 173 174 175
            expect(wifi.isWifiActive()).assertTrue();
            let getCountryCodeResult = wifi.getCountryCode();
            console.info("[wifi_test]getCountryCode :" + JSON.stringify(getCountryCodeResult));
            let countrylenth = getCountryCodeResult.length;
            console.info("[wifi_test]getCountryCode.length :" + JSON.stringify(countrylenth));
            expect(true).assertEqual(countrylenth == 2);
        })

        /**
Q
quanli 已提交
176
        * @tc.number Communication_WiFi_XTS_Sta_0020
Q
quanli 已提交
177 178 179 180 181
        * @tc.name testFeatureSupported
        * @tc.desc Test FeatureSupported API function.
        * @tc.type Function
        * @tc.level Level 3
        */
Q
quanli 已提交
182
        it('Communication_WiFi_XTS_Sta_0020', 0, function () {
Q
quanli 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            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);
        })

        /**
Q
quanli 已提交
219
        * @tc.number Communication_WiFi_XTS_Sta_0004
Q
quanli 已提交
220
        * @tc.name testGetLinkedInfo
Q
quanli 已提交
221 222 223 224
        * @tc.desc Test Test getLinkedInfo and getIpInfo  information.
        * @tc.type Function
        * @tc.level Level 1
        */
Q
quanli 已提交
225
        it('Communication_WiFi_XTS_Sta_0004', 0, async function (done) {
Q
quanli 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
            let isConnectedResult = wifi.isConnected();
            let ipInfoResult = wifi.getIpInfo();
            expect(JSON.stringify(ipInfoResult)).assertContain("gateway");
            let ipAddress = resolveIP(ipInfoResult.ipAddress);
            console.info("[wifi_test]ipAddress result: " + ipAddress);
            console.info("gateway: " + ipInfoResult.gateway + "ipAddress: " + ipInfoResult.ipAddress
            + "leaseDuration: " + ipInfoResult.leaseDuration +
            "leaseDuration: " + ipInfoResult.leaseDuration +
            "netmask: " + ipInfoResult.netmask + "primaryDns:" + ipInfoResult.primaryDns +
            "secondDns: " + ipInfoResult.secondDns + "serverIp: " + ipInfoResult.serverIp);
            await wifi.getLinkedInfo()
                .then((result) => {
                    console.info("[wifi_test]get wifi link promise:" + JSON.stringify(result));
                    done();
                }).catch((error) => {
                    console.info("[wifi_test]promise then error." + JSON.stringify(error));
                    expect().assertFail();
                });
Q
quanli 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

            function getLinked(){
                return new Promise((resolve, reject) => {
                    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 +
Q
quanli 已提交
261
                            "suppState: " + result.suppState + "ConnState: " + result.ConnState
Q
quanli 已提交
262 263
                            + "macType: " + result.macType);
                            let state = wifi.getLinkedInfo().ConnState;
Q
quanli 已提交
264
                            if (state == wifi.ConnState.SCANNING) {
Q
quanli 已提交
265 266
                                expect(true).assertEqual(state == 0);
                            }
Q
quanli 已提交
267
                            if (state == wifi.ConnState.CONNECTING) {
Q
quanli 已提交
268 269
                                expect(true).assertEqual(state == 1);
                            }
Q
quanli 已提交
270
                            if (state == wifi.ConnState.AUTHENTICATING) {
Q
quanli 已提交
271 272
                                expect(true).assertEqual(state == 2);
                            }
Q
quanli 已提交
273
                            if (state == wifi.ConnState.OBTAINING_IPADDR) {
Q
quanli 已提交
274 275
                                expect(true).assertEqual(state == 3);
                            }
Q
quanli 已提交
276
                            if (state == wifi.ConnState.CONNECTED) {
Q
quanli 已提交
277 278
                                expect(true).assertEqual(state == 4);
                            }
Q
quanli 已提交
279
                            if (state == wifi.ConnState.DISCONNECTING) {
Q
quanli 已提交
280 281
                                expect(true).assertEqual(state == 5);
                            }
Q
quanli 已提交
282
                            if (state == wifi.ConnState.DISCONNECTED) {
Q
quanli 已提交
283 284
                                expect(true).assertEqual(state == 6);
                            }
Q
quanli 已提交
285
                            if (state == wifi.ConnState.UNKNOWN) {
Q
quanli 已提交
286 287 288 289
                                expect(true).assertEqual(state == 7);
                            }
                            resolve();
                        });
Q
quanli 已提交
290
                });
Q
quanli 已提交
291 292 293 294
            }
            await getLinked();
            done();
        })
Q
quanli 已提交
295
        console.log("*************[wifi_test] start wifi js unit test end*************");
Q
quanli 已提交
296
    })
Q
quanli 已提交
297 298
}