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 #include "scan_service.h"
16 #include <gtest/gtest.h>
17 #include "Mock/mock_wifi_manager.h"
18 #include "mock_wifi_config_center.h"
19 #include "Mock/mock_wifi_settings.h"
20 #include "Mock/mock_scan_state_machine.h"
21 
22 using ::testing::_;
23 using ::testing::AtLeast;
24 using ::testing::DoAll;
25 using ::testing::Eq;
26 using ::testing::Return;
27 using ::testing::SetArgReferee;
28 using ::testing::StrEq;
29 using ::testing::TypedEq;
30 using ::testing::ext::TestSize;
31 
32 constexpr int TWO = 2;
33 constexpr int FOUR = 4;
34 constexpr int STATUS = 17;
35 
36 namespace OHOS {
37 namespace Wifi {
38 
39 class ScanServiceTest : public testing::Test {
40 public:
SetUpTestCase()41     static void SetUpTestCase() {}
TearDownTestCase()42     static void TearDownTestCase() {}
SetUp()43     virtual void SetUp()
44     {
45         pScanService = std::make_unique<ScanService>();
46         pScanService->pScanStateMachine = new MockScanStateMachine();
47         pScanService->RegisterScanCallbacks(WifiManager::GetInstance().GetScanCallback());
48     }
TearDown()49     virtual void TearDown()
50     {
51         pScanService.reset();
52     }
53 
54 public:
55     std::unique_ptr<ScanService> pScanService;
56 
SystemScanByIntervalSuccess()57     void SystemScanByIntervalSuccess()
58     {
59         int expScanCount = 1;
60         int interval = 1;
61         const int constTest = 2;
62         int count = constTest;
63         EXPECT_EQ(pScanService->SystemScanByInterval(expScanCount, interval, count), true);
64     }
SetEnhanceServiceTest()65     void SetEnhanceServiceTest()
66     {
67         IEnhanceService* enhanceService = nullptr;
68         pScanService->SetEnhanceService(enhanceService);
69     }
70 
StopPnoScanTest()71     void StopPnoScanTest()
72     {
73         pScanService->isPnoScanBegined = true;
74         pScanService->StopPnoScan();
75     }
76 
GetScanControlInfoSuccess()77     void GetScanControlInfoSuccess()
78     {
79         EXPECT_CALL(WifiConfigCenter::GetInstance(), GetScanControlInfo(_, _)).WillRepeatedly(Return(0));
80         pScanService->GetScanControlInfo();
81     }
82 
GetScanControlInfoFail()83     void GetScanControlInfoFail()
84     {
85         EXPECT_CALL(WifiConfigCenter::GetInstance(), GetScanControlInfo(_, _)).WillRepeatedly(Return(-1));
86         pScanService->GetScanControlInfo();
87     }
88 
AllowExternScanSuccess()89     void AllowExternScanSuccess()
90     {
91         pScanService->AllowExternScan();
92     }
93 
AllowExternScanFail1()94     void AllowExternScanFail1()
95     {
96         int staScene = 0;
97         StoreScanConfig cfg;
98         pScanService->scanConfigMap.emplace(staScene, cfg);
99         ScanMode scanMode = ScanMode::SYS_FOREGROUND_SCAN;
100         ScanForbidMode forbidMode;
101         forbidMode.scanScene = SCAN_SCENE_SCANNING;
102         forbidMode.scanMode = scanMode;
103         pScanService->scanControlInfo.scanForbidList.push_back(forbidMode);
104 
105         pScanService->AllowExternScan();
106     }
107 
AllowExternScanFail2()108     void AllowExternScanFail2()
109     {
110         EXPECT_CALL(WifiConfigCenter::GetInstance(), GetAppRunningState())
111             .WillRepeatedly(Return(ScanMode::SYS_FOREGROUND_SCAN));
112         EXPECT_CALL(WifiConfigCenter::GetInstance(), GetThermalLevel()).WillRepeatedly(Return(FOUR));
113         EXPECT_EQ(pScanService->AllowExternScan(), WIFI_OPT_SUCCESS);
114     }
115 
AllowExternScanFail3()116     void AllowExternScanFail3()
117     {
118         ScanMode scanMode = ScanMode::SYS_FOREGROUND_SCAN;
119         ScanForbidMode forbidMode;
120         forbidMode.scanScene = SCAN_SCENE_CONNECTED;
121         forbidMode.scanMode = scanMode;
122         forbidMode.forbidTime = 0;
123         forbidMode.forbidCount = 0;
124         pScanService->scanControlInfo.scanForbidList.push_back(forbidMode);
125         pScanService->staStatus = STATUS;
126         EXPECT_CALL(WifiConfigCenter::GetInstance(), GetAppRunningState())
127             .WillRepeatedly(Return(ScanMode::SYS_FOREGROUND_SCAN));
128         EXPECT_CALL(WifiConfigCenter::GetInstance(), GetThermalLevel()).WillRepeatedly(Return(FOUR));
129         EXPECT_EQ(pScanService->AllowExternScan(), WIFI_OPT_SUCCESS);
130     }
131 
AllowExternScanFail4()132     void AllowExternScanFail4()
133     {
134         pScanService->disableScanFlag = true;
135         EXPECT_CALL(WifiConfigCenter::GetInstance(), SetThermalLevel(TWO)).Times(AtLeast(0));
136         EXPECT_EQ(pScanService->AllowExternScan(), WIFI_OPT_FAILED);
137     }
138 };
139 
140 HWTEST_F(ScanServiceTest, SystemScanByIntervalSuccess, TestSize.Level1)
141 {
142     SystemScanByIntervalSuccess();
143 }
144 
145 HWTEST_F(ScanServiceTest, SetEnhanceServiceTest, TestSize.Level1)
146 {
147     SetEnhanceServiceTest();
148 }
149 
150 HWTEST_F(ScanServiceTest, GetScanControlInfoSuccess, TestSize.Level1)
151 {
152     GetScanControlInfoSuccess();
153 }
154 
155 HWTEST_F(ScanServiceTest, GetScanControlInfoFail, TestSize.Level1)
156 {
157     GetScanControlInfoFail();
158 }
159 
160 HWTEST_F(ScanServiceTest, StopPnoScanTest, TestSize.Level1)
161 {
162     StopPnoScanTest();
163 }
164 
165 HWTEST_F(ScanServiceTest, AllowExternScanSuccess, TestSize.Level1)
166 {
167     AllowExternScanSuccess();
168 }
169 
170 HWTEST_F(ScanServiceTest, AllowExternScanFail1, TestSize.Level1)
171 {
172     AllowExternScanFail1();
173 }
174 
175 HWTEST_F(ScanServiceTest, AllowExternScanFail2, TestSize.Level1)
176 {
177     AllowExternScanFail2();
178 }
179 
180 HWTEST_F(ScanServiceTest, AllowExternScanFail3, TestSize.Level1)
181 {
182     AllowExternScanFail3();
183 }
184 
185 HWTEST_F(ScanServiceTest, AllowExternScanFail4, TestSize.Level1)
186 {
187     AllowExternScanFail4();
188 }
189 }  // namespace Wifi
190 }  // namespace OHOS