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_error_code.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
TearDownTestCase()38 void WifiSoftBusTest::TearDownTestCase() { }
39
SetUp()40 void WifiSoftBusTest::SetUp() { }
41
TearDown()42 void WifiSoftBusTest::TearDown() { }
43
44 static void OnWifiScanStateChangedHandler(int32_t state, int32_t size);
45 static bool g_stateScanSuccess = false;
46
47 constexpr int32_t DEF_TIMEOUT = 5;
48 constexpr int32_t ONE_SECOND = 1;
49
OnWifiScanStateChangedHandler(int32_t state,int32_t size)50 static void OnWifiScanStateChangedHandler(int32_t state, int32_t size)
51 {
52 if (size > 0) {
53 g_stateScanSuccess = true;
54 }
55 return;
56 }
57
58 static ISoftBusScanResult g_scanResultCb = { .onSoftBusWifiScanResult = OnWifiScanStateChangedHandler };
59
WaitSacnResult(void)60 static void WaitSacnResult(void)
61 {
62 int32_t scanTimeout = DEF_TIMEOUT;
63 while (scanTimeout > 0) {
64 sleep(ONE_SECOND);
65 scanTimeout--;
66 if (g_stateScanSuccess) {
67 break;
68 }
69 }
70 if (scanTimeout <= 0) {
71 printf("WaitSacnResult:timeout!\n");
72 }
73 }
74
75 HWTEST_F(WifiSoftBusTest, WifiSoftBusGetWifiScanListTest001, TestSize.Level0)
76 {
77 SoftBusWifiScanInfo *result = nullptr;
78 unsigned int size = WIFI_MAX_SCAN_HOTSPOT_LIMIT;
79 int32_t ret;
80
81 EXPECT_TRUE(SoftBusRegisterWifiEvent(&g_scanResultCb) == SOFTBUS_OK);
82
83 ret = IsWifiActive();
84 if (ret != WIFI_STA_ACTIVE) {
85 ret = EnableWifi();
86 if (ret != WIFI_SUCCESS) {
87 printf("SoftBus Enable Wifi failed.");
88 }
89 sleep(ONE_SECOND);
90 }
91
92 EXPECT_TRUE(IsWifiActive() == WIFI_STA_ACTIVE);
93
94 EXPECT_TRUE(SoftBusStartWifiScan() == SOFTBUS_OK);
95
96 WaitSacnResult();
97
98 EXPECT_TRUE(SoftBusGetWifiScanList(&result, &size) == SOFTBUS_OK);
99
100 EXPECT_TRUE(result != nullptr);
101 SoftBusFree(result);
102 EXPECT_TRUE(SoftBusUnRegisterWifiEvent(&g_scanResultCb) == SOFTBUS_OK);
103 }
104 } // namespace OHOS
105