1 /*
2  * Copyright (c) 2021 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 
16 #include <gtest/gtest.h>
17 
18 #include <securec.h>
19 
20 #include "softbus_adapter_mem.h"
21 #include "softbus_errcode.h"
22 #include "softbus_utils.h"
23 #include "softbus_wifi_api_adapter.h"
24 
25 namespace OHOS {
26 using namespace testing::ext;
27 
28 class WifiSoftBusTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase()36 void WifiSoftBusTest::SetUpTestCase()
37 {
38 }
39 
TearDownTestCase()40 void WifiSoftBusTest::TearDownTestCase()
41 {
42 }
43 
SetUp()44 void WifiSoftBusTest::SetUp()
45 {
46 }
47 
TearDown()48 void WifiSoftBusTest::TearDown()
49 {
50 }
51 
52 static void OnWifiScanStateChangedHandler(int state, int size);
53 static bool g_stateScanSuccess = false;
54 
55 constexpr int32_t DEF_TIMEOUT = 5;
56 constexpr int32_t ONE_SECOND = 1;
57 
OnWifiScanStateChangedHandler(int state,int size)58 static void OnWifiScanStateChangedHandler(int state, int size)
59 {
60     if (size > 0) {
61         g_stateScanSuccess = true;
62     }
63     return;
64 }
65 
66 static ISoftBusScanResult g_scanResultCb = {
67     .onSoftBusWifiScanResult = OnWifiScanStateChangedHandler
68 };
69 
WaitSacnResult(void)70 static void WaitSacnResult(void)
71 {
72     int scanTimeout = DEF_TIMEOUT;
73     while (scanTimeout > 0) {
74         sleep(ONE_SECOND);
75         scanTimeout--;
76         if (g_stateScanSuccess) {
77             break;
78         }
79     }
80     if (scanTimeout <= 0) {
81         printf("WaitSacnResult:timeout!\n");
82     }
83 }
84 
85 HWTEST_F(WifiSoftBusTest, WifiSoftBusGetWifiScanListTest001, TestSize.Level0)
86 {
87     SoftBusWifiScanInfo* result = NULL;
88     unsigned int size = WIFI_MAX_SCAN_HOTSPOT_LIMIT;
89     int32_t ret;
90 
91     EXPECT_TRUE(SoftBusRegisterWifiEvent(&g_scanResultCb) == SOFTBUS_OK);
92 
93     ret = IsWifiActive();
94     if (ret != WIFI_STA_ACTIVE) {
95         ret = EnableWifi();
96         if (ret != WIFI_SUCCESS) {
97             printf("SoftBus Enable Wifi failed.");
98         }
99         sleep(ONE_SECOND);
100     }
101 
102     EXPECT_TRUE(IsWifiActive() == WIFI_STA_ACTIVE);
103 
104     EXPECT_TRUE(SoftBusStartWifiScan() == SOFTBUS_OK);
105 
106     WaitSacnResult();
107 
108     EXPECT_TRUE(SoftBusGetWifiScanList(&result, &size) == SOFTBUS_OK);
109 
110     EXPECT_TRUE(result != NULL);
111     SoftBusFree(result);
112     EXPECT_TRUE(SoftBusUnRegisterWifiEvent(&g_scanResultCb) == SOFTBUS_OK);
113 }
114 }
115