1 /*
2 * Copyright (c) 2021-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 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 #include <memory>
18 #include "ap_define.h"
19 #include "ap_state_machine.h"
20 #include "wifi_log.h"
21 #include "mock_wifi_config_center.h"
22 #include "mock_wifi_settings.h"
23 #include "mock_pendant.h"
24 #include "mock_wifi_ap_hal_interface.h"
25 #include "operator_overload.h"
26
27 using ::testing::_;
28 using ::testing::AtLeast;
29 using ::testing::Eq;
30 using ::testing::Return;
31 namespace OHOS {
32 namespace Wifi {
33 class ApStateMachine_test : public testing::Test {
34 public:
SetUpTestCase()35 static void SetUpTestCase() {}
TearDownTestCase()36 static void TearDownTestCase() {}
SetUp()37 virtual void SetUp()
38 {
39 const int SLEEP_TIME = 20;
40 EXPECT_CALL(WifiConfigCenter::GetInstance(), SetThreadStatusFlag(_)).Times(AtLeast(0));
41 pMockPendant = new MockPendant();
42 pMockApStationsManager = &(pMockPendant->GetMockApStationsManager());
43 pMockApRootState = &(pMockPendant->GetMockApRootState());
44 pMockApIdleState = &(pMockPendant->GetMockApIdleState());
45 pMockApStartedState = &(pMockPendant->GetMockApStartedState());
46 pMockApMonitor = &(pMockPendant->GetMockApMonitor());
47
48 pApStateMachine = new ApStateMachine(*pMockApStationsManager, *pMockApRootState, *pMockApIdleState,
49 *pMockApStartedState, *pMockApMonitor);
50
51 RegisterApServiceCallbacks();
52 EXPECT_CALL(WifiApHalInterface::GetInstance(), RegisterApEvent(_, 0))
53 .WillOnce(Return(WifiErrorNo::WIFI_HAL_OPT_OK));
54 usleep(SLEEP_TIME);
55 }
TearDown()56 virtual void TearDown()
57 {
58 delete pApStateMachine;
59 delete pMockPendant;
60 pMockPendant = nullptr;
61 pApStateMachine = nullptr;
62 pMockApStationsManager = nullptr;
63 pMockApRootState = nullptr;
64 pMockApIdleState = nullptr;
65 pMockApStartedState = nullptr;
66 pMockApMonitor = nullptr;
67 }
68
69 public:
RegisterApServiceCallbacks()70 ErrCode RegisterApServiceCallbacks()
71 {
72 std::function<void(const StationInfo &, int)> OnStationEvent =
73 [&](const StationInfo &sta, int id) { m_sta = sta; };
74
75 IApServiceCallbacks callbacks = {"", [&](ApState state, int id) { mBState = state; },
76 OnStationEvent, OnStationEvent};
77 return pApStateMachine->RegisterApServiceCallbacks(callbacks);
78 }
UnRegisterApServiceCallbacks()79 ErrCode UnRegisterApServiceCallbacks()
80 {
81 IApServiceCallbacks callbacks;
82 return pApStateMachine->RegisterApServiceCallbacks(callbacks);
83 }
84
85 public:
WrapOnApStateChange(ApState state)86 void WrapOnApStateChange(ApState state)
87 {
88 EXPECT_CALL(WifiConfigCenter::GetInstance(), SetHotspotState(Eq(static_cast<int>(state)), 0)).WillOnce(
89 Return(0));
90 pApStateMachine->OnApStateChange(state);
91 }
WrapOnApStateChangeFail(ApState state)92 void WrapOnApStateChangeFail(ApState state)
93 {
94 EXPECT_CALL(WifiConfigCenter::GetInstance(), SetHotspotState(Eq(static_cast<int>(state)), 0)).WillOnce(
95 Return(1));
96 pApStateMachine->OnApStateChange(state);
97 }
98
WrapBroadCastStationJoin(const StationInfo & staInfo)99 void WrapBroadCastStationJoin(const StationInfo &staInfo)
100 {
101 pApStateMachine->BroadCastStationChange(staInfo, ApStatemachineEvent::CMD_STATION_JOIN);
102 }
103
WrapBroadCastStationLeave(const StationInfo & staInfo)104 void WrapBroadCastStationLeave(const StationInfo &staInfo)
105 {
106 pApStateMachine->BroadCastStationChange(staInfo, ApStatemachineEvent::CMD_STATION_LEAVE);
107 }
WrapBroadCastStationChangeDefult(const StationInfo & staInfo)108 void WrapBroadCastStationChangeDefult(const StationInfo &staInfo)
109 {
110 pApStateMachine->BroadCastStationChange(staInfo, ApStatemachineEvent::CMD_START_HOTSPOT);
111 }
WarpRegisterEventHandler() const112 void WarpRegisterEventHandler() const
113 {
114 pApStateMachine->RegisterEventHandler();
115 }
116
117 public:
118 MockPendant *pMockPendant;
119 MockApStationsManager *pMockApStationsManager;
120 MockApRootState *pMockApRootState;
121 MockApIdleState *pMockApIdleState;
122 MockApStartedState *pMockApStartedState;
123 MockApMonitor *pMockApMonitor;
124
125 ApStateMachine *pApStateMachine;
126 ApState mBState;
127 StationInfo m_sta;
128 };
TEST_F(ApStateMachine_test,OnApStateChange)129 TEST_F(ApStateMachine_test, OnApStateChange)
130 {
131 ApState BroadState = ApState::AP_STATE_IDLE;
132 WrapOnApStateChange(BroadState);
133
134 BroadState = ApState::AP_STATE_STARTING;
135 WrapOnApStateChange(BroadState);
136
137 BroadState = ApState::AP_STATE_STARTED;
138 WrapOnApStateChange(BroadState);
139
140 BroadState = ApState::AP_STATE_CLOSING;
141 WrapOnApStateChange(BroadState);
142
143 BroadState = ApState::AP_STATE_CLOSED;
144 WrapOnApStateChange(BroadState);
145
146 BroadState = ApState::AP_STATE_CLOSED;
147 WrapOnApStateChangeFail(BroadState);
148 }
149
TEST_F(ApStateMachine_test,BroadCastStationJoin)150 TEST_F(ApStateMachine_test, BroadCastStationJoin)
151 {
152 const StationInfo BroadCastStation = {"test1", "aa:bb:cc:dd:ee:ff", 1, "127.0.0.1"};
153 WrapBroadCastStationJoin(BroadCastStation);
154 EXPECT_EQ(BroadCastStation, m_sta);
155 }
156
TEST_F(ApStateMachine_test,BroadCastStationLeave)157 TEST_F(ApStateMachine_test, BroadCastStationLeave)
158 {
159 const StationInfo BroadCastStation = {"test1", "aa:bb:cc:dd:ee:ff", 1, "127.0.0.1"};
160 WrapBroadCastStationChangeDefult(BroadCastStation);
161 WrapBroadCastStationLeave(BroadCastStation);
162 EXPECT_EQ(BroadCastStation, m_sta);
163 }
164
TEST_F(ApStateMachine_test,RegisterEventHandler)165 TEST_F(ApStateMachine_test, RegisterEventHandler)
166 {
167 WarpRegisterEventHandler();
168 }
169 } // namespace Wifi
170 } // namespace OHOS
171