WifiP2P.test.js 20.2 KB
Newer Older
J
jiyong_sd 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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.
 */

Q
quanli 已提交
16
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
J
jiyong_sd 已提交
17 18 19 20

import wifi from '@ohos.wifi'

function sleep(delay) {
Q
quanli 已提交
21 22 23 24
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start > delay) {
        break;
    }
J
jiyong_sd 已提交
25 26 27
}

function checkWifiPowerOn(){
Q
quanli 已提交
28
	console.info("Wifi_test/wifi status:" + wifi.isWifiActive());
J
jiyong_sd 已提交
29 30 31 32 33 34 35 36 37 38
}

let GroupOwnerBand = {
    GO_BAND_AUTO : 0,
    GO_BAND_2GHZ : 1,
    GO_BAND_5GHZ : 2,
}

describe('ACTS_WifiTest', function () {
    beforeEach(function () {
Q
quanli 已提交
39 40
        console.info("beforeEach start");
        checkWifiPowerOn();
J
jiyong_sd 已提交
41 42
    })

Q
quanli 已提交
43 44
    afterEach(async function () {
        console.info("afterEach start");
J
jiyong_sd 已提交
45 46 47 48 49 50 51 52 53 54 55 56
    })

    /**
    * @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",
Q
quanli 已提交
57
            groupName : "AAAZZZ123",
J
jiyong_sd 已提交
58 59 60 61 62 63
            goBand : 0
        };

        console.log("[wifi_test]  check the state of wifi: " + wifi.isWifiActive());
        expect(wifi.isWifiActive()).assertTrue();
        let addConfig = wifi.createGroup(WifiP2PConfig);
Q
quanli 已提交
64 65
        console.info("[wifi_test] test createGroup end."+ addConfig);
        sleep(2000);
J
jiyong_sd 已提交
66
        expect(addConfig).assertTrue();
Q
quanli 已提交
67 68
        wifi.getCurrentGroup()
            .then(data  => {
J
jiyong_sd 已提交
69 70 71
                let resultLength = Object.keys(data).length;
                console.info("[wifi_test] getCurrentGroup [promise] result -> " + JSON.stringify(data));
                expect(true).assertEqual(resultLength!=0);
Q
quanli 已提交
72 73 74 75
                let removeConfig = wifi.removeGroup();
                expect(removeConfig).assertTrue();
            });
        done()       
J
jiyong_sd 已提交
76 77 78 79 80 81 82 83 84 85 86 87
    })

    /**
    * @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",
Q
quanli 已提交
88
            groupName : "AAAZZZ123",
J
jiyong_sd 已提交
89 90 91 92 93 94
            goBand : 0
        };

        console.log("[wifi_test]  check the state of wifi: " + wifi.isWifiActive());
        expect(wifi.isWifiActive()).assertTrue();
        let addConfig = wifi.createGroup(WifiP2PConfig);
Q
quanli 已提交
95 96
        sleep(2000);
        console.log("[wifi_test] check the state of wifi:" + addConfig);
J
jiyong_sd 已提交
97 98
        expect(addConfig).assertTrue();
       
Q
quanli 已提交
99
        wifi.getCurrentGroup(
J
jiyong_sd 已提交
100 101 102
            (err, result) => {
                if (err) {
                    console.error('wifi_test / failed to get getCurrentGroup: ' + JSON.stringify(err));
Q
quanli 已提交
103
                    expect().assertFail();
J
jiyong_sd 已提交
104
                    return;
Q
quanli 已提交
105 106 107 108 109
                }else{
                    console.info("[wifi_test] getCurrentGroup [callback] -> " + JSON.stringify(result));
                    let removeConfig = wifi.removeGroup();
                    expect(removeConfig).assertTrue();
                }    
J
jiyong_sd 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123
                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",
Q
quanli 已提交
124
            groupName : "AAAZZZ123",
J
jiyong_sd 已提交
125 126 127 128 129 130
            goBand : 1
        };

        console.log("[wifi_test]  check the state of wifi: " + wifi.isWifiActive());
        expect(wifi.isWifiActive()).assertTrue();
        let addConfig = wifi.createGroup(WifiP2PConfig2);
Q
quanli 已提交
131 132
        sleep(2000);
        console.info("[wifi_test] test createGroup3 result." + addConfig)
J
jiyong_sd 已提交
133 134
        expect(addConfig).assertTrue();
        await wifi.getCurrentGroup()
Q
quanli 已提交
135
            .then(data  => {
J
jiyong_sd 已提交
136 137 138
                let resultLength = Object.keys(data).length;
                console.info("[wifi_test] getCurrentGroup  [promise] result -> " + JSON.stringify(data));
                expect(true).assertEqual(resultLength!=0);
Q
quanli 已提交
139 140 141 142
                let removeConfig = wifi.removeGroup();
                expect(removeConfig).assertTrue();
            });
        done()
J
jiyong_sd 已提交
143 144 145 146 147 148 149 150 151 152 153 154
    })

    /**
    * @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",
Q
quanli 已提交
155
            groupName : "AAAZZZ123",
J
jiyong_sd 已提交
156 157 158 159 160 161
            goBand : 0
        };
       
        console.log("[wifi_test]  check the state of wifi: " + wifi.isWifiActive());
        expect(wifi.isWifiActive()).assertTrue();
        let addConfig = wifi.createGroup(WifiP2PConfig);
Q
quanli 已提交
162 163
        sleep(2000);
        console.info("[wifi_test] test p2pConnect result." + addConfig);
J
jiyong_sd 已提交
164 165 166
        expect(addConfig).assertTrue();
        await wifi.getCurrentGroup()
            .then((data)  => {
Q
quanli 已提交
167 168 169
                let resultLength = Object.keys(data).length;    
                console.info("[wifi_test] getCurrentGroup  [promise] result -> " + JSON.stringify(data));
                expect(true).assertEqual(resultLength!=0);
J
jiyong_sd 已提交
170

Q
quanli 已提交
171 172 173
                let removeConfig = wifi.removeGroup();
                expect(removeConfig).assertTrue();
            });
J
jiyong_sd 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187
        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",
Q
quanli 已提交
188
            goBand : 2
J
jiyong_sd 已提交
189 190 191 192 193
        };
        
        console.log("[wifi_test]  check the state of wifi: " + wifi.isWifiActive());
        expect(wifi.isWifiActive()).assertTrue();
        let scanConfig = wifi.startDiscoverDevices();
Q
quanli 已提交
194
        sleep(2000);
J
jiyong_sd 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        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()
Q
quanli 已提交
212
            .then(data => {
J
jiyong_sd 已提交
213 214 215
                let resultLength = Object.keys(data).length; 
                console.info("[wifi_test] getP2pLinkedInfo  [promise] result -> " + JSON.stringify(data));
                expect(true).assertEqual(resultLength!=0);
Q
quanli 已提交
216 217
                done()
            });
J
jiyong_sd 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
      
        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,
        };
        
Q
quanli 已提交
233
        wifi.getP2pLinkedInfo((err, result) => {
J
jiyong_sd 已提交
234 235 236
            if (err) {
                console.error('failed to getP2pLinkedInfo callback  ' + JSON.stringify(err));
                return;
Q
quanli 已提交
237 238 239 240 241 242
            }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);
J
jiyong_sd 已提交
243
            }
Q
quanli 已提交
244
            done();
J
jiyong_sd 已提交
245 246 247 248 249 250 251 252 253 254
        });
    })

    /**
    * @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();
Q
quanli 已提交
255
        sleep(2000);
J
jiyong_sd 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        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();
Q
quanli 已提交
273
        sleep(2000);
J
jiyong_sd 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        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();
Q
quanli 已提交
305
        sleep(2000);
J
jiyong_sd 已提交
306 307 308 309 310
        expect(scanConfig).assertTrue();
        
        await wifi.getP2pPeerDevices((err, result) => {
            if (err) {
                console.error('failed to getP2pPeerDevices infos callback because ' + JSON.stringify(err));
Q
quanli 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
            }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);
                    }
J
jiyong_sd 已提交
335
                }
Q
quanli 已提交
336 337
                let stopScan = wifi.stopDiscoverDevices();
                expect(stopScan).assertTrue();
J
jiyong_sd 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
            }
            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
    */
Q
quanli 已提交
369
    it('SUB_Communication_WiFi_P2P_p2pConnectionChange_0002', 0, async function (done) {  
J
jiyong_sd 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        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) {
Q
quanli 已提交
417
                    console.error('wifi_test / failed to get getP2pDevices: ' + JSON.stringify(err));
J
jiyong_sd 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
                    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) {
Q
quanli 已提交
458 459
        let recvP2pPersistentGroupChangeFunc = () => {
            console.info("wifi_test / p2p persistent group change receive event");
J
jiyong_sd 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
            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));
            });
        };
Q
quanli 已提交
477 478
        wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);
        setTimeout(async function() {
J
jiyong_sd 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
            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));
Q
quanli 已提交
495
            expect(true).assertEqual((result !=null));
J
jiyong_sd 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508
            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*************");
})