• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
17import wifiMg from '@ohos.wifiManager'
18import osaccount from '@ohos.account.osAccount'
19import bundle from '@ohos.bundle'
20import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
21
22async function applyPermission() {
23    let osAccountManager = osaccount.getAccountManager();
24    console.info("=== getAccountManager finish");
25    let localId = await osAccountManager.getOsAccountLocalIdFromProcess();
26    console.info("LocalId is :" + localId);
27    let appInfo = await bundle.getApplicationInfo('ohos.acts.communication.wifi.wifidevice', 0, localId);
28    let atManager = abilityAccessCtrl.createAtManager();
29    if (atManager != null) {
30        let tokenID = appInfo.accessTokenId;
31        console.info('[permission] case accessTokenID is ' + tokenID);
32        let permissionName1 = 'ohos.permission.LOCATION';
33        await atManager.grantUserGrantedPermission(tokenID, permissionName1, 1).then((result) => {
34            console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result));
35        }).catch((err) => {
36            console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err));
37        });
38    } else {
39        console.info('[permission] case apply permission failed, createAtManager failed');
40    }
41}
42
43function sleep(delay) {
44    return new Promise(resovle => setTimeout(resovle, delay))
45}
46
47function checkWifiPowerOn(){
48    console.info("[wifi_test]wifi status:" + wifiMg.isWifiActive());
49}
50
51export default function actsWifiManagerEventTest() {
52    describe('actsWifiManagerEventTest', function () {
53        beforeAll(async function (done) {
54            console.info('beforeAll case');
55            await applyPermission();
56            done();
57        })
58
59        beforeEach(function () {
60            console.info("[wifi_test]beforeEach start" );
61            checkWifiPowerOn();
62        })
63        afterEach(async function () {
64            console.info("[wifi_test]afterEach start" );
65        })
66
67        /**
68        * @tc.number SUB_Communication_WiFi_Event_Test_0008
69        * @tc.name testp2pStateChange
70        * @tc.desc Test p2pStateChange callback
71        * @tc.type Function
72        * @tc.level Level 3
73        */
74        it('SUB_Communication_WiFi_Event_Test_0008', 0, async function (done) {
75            let p2pState = "p2pStateChange";
76            let p2pStateChangeCallback = result => {
77                console.info("[wifi_test]p2pStateChange callback, result: " + JSON.stringify(result));
78            }
79            wifiMg.on(p2pState, p2pStateChangeCallback);
80            await sleep(3000);
81            wifiMg.off(p2pState, p2pStateChangeCallback);
82            done();
83        })
84
85        /**
86        * @tc.number SUB_Communication_WiFi_Event_Test_0009
87        * @tc.name testp2pConnectionChange
88        * @tc.desc Test p2pConnectionChange callback
89        * @tc.type Function
90        * @tc.level Level 3
91        */
92        it('SUB_Communication_WiFi_Event_Test_0009', 0, async function (done) {
93            let p2pConnectionState = "p2pConnectionChange";
94            let p2pConnectionChangeCallback = result => {
95                console.info("[wifi_test]p2pConnectionChange callback, result: " + JSON.stringify(result));
96            }
97            wifiMg.on(p2pConnectionState, p2pConnectionChangeCallback);
98            let p2pConnectState = {
99                DISCONNECTED :0,
100                CONNECTED : 1,
101            };
102            let wifiP2PConfig = {
103                deviceAddress : "22:9b:e6:48:1f:5c",
104                deviceAddressType : 1,
105                netId : -1,
106                passphrase : "12345678",
107                groupName : "DIRECT-AAAZZZ456",
108                goBand : wifiMg.GroupOwnerBand.GO_BAND_AUTO,
109            };
110            let connectResult = wifiMg.p2pConnect(wifiP2PConfig);
111            let p2pCancelResult = wifiMg.p2pCancelConnect();
112            await sleep(2000);
113            console.info("[wifi_test]test p2pCancelConnect successful." );
114            await wifiMg.getP2pLinkedInfo()
115                .then(data => {
116                    let resultLength = Object.keys(data).length;
117                    console.info("[wifi_test]getP2pLinkedInfo  promise result : " + JSON.stringify(data));
118                    expect(true).assertEqual(resultLength!=0);
119                    done()
120                });
121            await sleep(2000);
122            wifiMg.off(p2pConnectionState, p2pConnectionChangeCallback);
123            done();
124        })
125
126        /**
127        * @tc.number SUB_Communication_WiFi_Event_Test_0012
128        * @tc.name testp2pDeviceChange
129        * @tc.desc Test p2pDeviceChange callback
130        * @tc.type Function
131        * @tc.level Level 3
132        */
133        it('SUB_Communication_WiFi_Event_Test_0012', 0, async function (done) {
134            let p2pDeviceState = "p2pDeviceChange";
135            let p2pDeviceChangeCallback = result => {
136                console.info("[wifi_test]p2pDeviceChange callback, result: " + JSON.stringify(result));
137            }
138            wifiMg.on(p2pDeviceState, p2pDeviceChangeCallback);
139            await sleep(3000);
140            wifiMg.off(p2pDeviceState, p2pDeviceChangeCallback);
141            done();
142        })
143
144        /**
145        * @tc.number SUB_Communication_WiFi_Event_Test_0010
146        * @tc.name testp2pPeerDeviceChange
147        * @tc.desc Test p2pPeerDeviceChange callback
148        * @tc.type Function
149        * @tc.level Level 3
150        */
151        it('SUB_Communication_WiFi_Event_Test_0010', 0, async function (done) {
152            let p2pPeerDeviceState = "p2pPeerDeviceChange";
153            let p2pPeerDeviceChangeCallback = result => {
154                console.info("[wifi_test]p2pPeerDeviceChange callback, result: " + JSON.stringify(result));
155            }
156            wifiMg.on(p2pPeerDeviceState, p2pPeerDeviceChangeCallback);
157            let startDiscover = wifiMg.startDiscoverP2pDevices();
158            await sleep(3000);
159            let stopDiscover = wifiMg.stopDiscoverP2pDevices();
160            wifiMg.off(p2pPeerDeviceState, p2pPeerDeviceChangeCallback);
161            done();
162        })
163
164        /**
165        * @tc.number SUB_Communication_WiFi_Event_Test_0013
166        * @tc.name testp2pPersistentGroupChange
167        * @tc.desc Test p2pPersistentGroupChange callback
168        * @tc.type Function
169        * @tc.level Level 3
170        */
171        it('SUB_Communication_WiFi_Event_Test_0013', 0, async function (done) {
172            let p2pGroupState = "p2pPersistentGroupChange";
173            let p2pPersistentGroupChangeCallback = () => {
174                console.info("[wifi_test]p2pPersistentGroupChange callback, result: " + JSON.stringify(result));
175            }
176            wifiMg.on(p2pGroupState, p2pPersistentGroupChangeCallback);
177            let WifiP2PConfig = {
178                deviceAddress : "22:9b:e6:48:1f:5c",
179                netId : -2,
180                passphrase : "12345678",
181                groupName : "DIRECT-AAAZZZ123",
182                goBand : wifiMg.GroupOwnerBand.GO_BAND_AUTO,
183            };
184            try {
185                await wifiMg.getCurrentP2pGroup()
186                    .then(data => {
187                        let resultLength = Object.keys(data).length;
188                        console.info("[wifi_test] getCurrentP2pGroup  promise result -> " + JSON.stringify(data));
189                        expect(true).assertEqual(resultLength!=0);
190                    }).catch((error) => {
191                        console.error('[wifi_test] getCurrentP2pGroup  promise failed :' + JSON.stringify(error));
192                        expect(true).assertEqual(error !=null);
193                    });
194            }catch(error){
195                console.info("[wifi_test]getCurrentP2pGroup promise error: " + JSON.stringify(error.message));
196                expect(true).assertEqual( (JSON.stringify(error.message)) !=null);
197            }
198            wifiMg.off(p2pGroupState, p2pPersistentGroupChangeCallback);
199            done();
200        })
201
202        /**
203        * @tc.number SUB_Communication_WiFi_Event_Test_0011
204        * @tc.name testpp2pDiscoveryChange
205        * @tc.desc Test p2pDiscoveryChange callback
206        * @tc.type Function
207        * @tc.level Level 3
208        */
209        it('SUB_Communication_WiFi_Event_Test_0011', 0, async function (done) {
210            let p2pPeerDeviceState = "p2pDiscoveryChange";
211            let p2pDiscoveryChangeCallback = result => {
212                console.info("[wifi_test]p2pDiscoveryChange callback, result: " + JSON.stringify(result));
213            }
214            wifiMg.on(p2pPeerDeviceState, p2pDiscoveryChangeCallback);
215            let startDiscover = wifiMg.startDiscoverP2pDevices();
216            await sleep(3000);
217            let stopDiscover = wifiMg.stopDiscoverP2pDevices();
218            wifiMg.off(p2pPeerDeviceState, p2pDiscoveryChangeCallback);
219            done();
220        })
221        console.log("*************[wifi_test] start wifi js unit test end*************");
222    })
223}
224
225