• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 #include "bluetooth_hfp_hf.h"
18 #include "bluetooth_host.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Bluetooth {
25 using namespace std;
26 
27 constexpr int TIME = 2;
28 
29 class HandsFreeUnitObserverCommon : public HandsFreeUnitObserver {
30 public:
31     HandsFreeUnitObserverCommon() = default;
32     virtual ~HandsFreeUnitObserverCommon() = default;
33 
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state)34     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state) {}
OnScoStateChanged(const BluetoothRemoteDevice & device,int state)35     virtual void OnScoStateChanged(const BluetoothRemoteDevice &device, int state) {}
OnCallChanged(const BluetoothRemoteDevice & device,const HandsFreeUnitCall & call)36     virtual void OnCallChanged(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call) {}
OnSignalStrengthChanged(const BluetoothRemoteDevice & device,int signal)37     virtual void OnSignalStrengthChanged(const BluetoothRemoteDevice &device, int signal) {}
OnRegistrationStatusChanged(const BluetoothRemoteDevice & device,int status)38     virtual void OnRegistrationStatusChanged(const BluetoothRemoteDevice &device, int status) {}
OnRoamingStatusChanged(const BluetoothRemoteDevice & device,int status)39     virtual void OnRoamingStatusChanged(const BluetoothRemoteDevice &device, int status) {}
OnOperatorSelectionChanged(const BluetoothRemoteDevice & device,const std::string & name)40     virtual void OnOperatorSelectionChanged(const BluetoothRemoteDevice &device, const std::string &name) {}
OnSubscriberNumberChanged(const BluetoothRemoteDevice & device,const std::string & number)41     virtual void OnSubscriberNumberChanged(const BluetoothRemoteDevice &device, const std::string &number) {}
OnVoiceRecognitionStatusChanged(const BluetoothRemoteDevice & device,int status)42     virtual void OnVoiceRecognitionStatusChanged(const BluetoothRemoteDevice &device, int status) {}
OnInBandRingToneChanged(const BluetoothRemoteDevice & device,int status)43     virtual void OnInBandRingToneChanged(const BluetoothRemoteDevice &device, int status) {}
44 
45 private:
46 };
47 static HandsFreeUnitObserverCommon observer_;
48 static HandsFreeUnit *profile_;
49 static BluetoothHost *host_;
50 class HandsFreeUnitTest : public testing::Test {
51 public:
HandsFreeUnitTest()52     HandsFreeUnitTest()
53     {}
~HandsFreeUnitTest()54     ~HandsFreeUnitTest()
55     {}
56 
57     static void SetUpTestCase(void);
58     static void TearDownTestCase(void);
59     void SetUp();
60     void TearDown();
61 };
62 
63 
SetUpTestCase(void)64 void HandsFreeUnitTest::SetUpTestCase(void)
65 {
66 
67 }
TearDownTestCase(void)68 void HandsFreeUnitTest::TearDownTestCase(void)
69 {
70 
71 }
SetUp()72 void HandsFreeUnitTest::SetUp()
73 {
74     host_ = &BluetoothHost::GetDefaultHost();
75     host_->EnableBt();
76     host_->EnableBle();
77     sleep(TIME);
78 }
79 
TearDown()80 void HandsFreeUnitTest::TearDown()
81 {
82     host_->DisableBt();
83     host_->DisableBle();
84     host_ = nullptr;
85 }
86 
87 
88 /*
89  * @tc.number: HandsFreeUnit001
90  * @tc.name: GetProfile
91  * @tc.desc: Get the instance of HandsFreeAudioGateway object.
92 */
93 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_GetProfile, TestSize.Level1)
94 {
95     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetProfile start";
96 
97     profile_ = HandsFreeUnit::GetProfile();
98 
99     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetProfile end";
100 }
101 
102 /*
103  * @tc.number: HandsFreeUnit002
104  * @tc.name: ConnectSco
105  * @tc.desc:  Initiate the establishment of an audio connection to remote AG device.
106 */
107 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_ConnectSco, TestSize.Level1)
108 {
109     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ConnectSco start";
110 
111     profile_ = HandsFreeUnit::GetProfile();
112     BluetoothRemoteDevice device;
113     EXPECT_EQ(profile_->ConnectSco(device), true);
114 
115     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ConnectSco end";
116 }
117 
118 /*
119  * @tc.number: HandsFreeUnit003
120  * @tc.name: DisconnectSco
121  * @tc.desc: Release the audio connection from remote HF device.
122 */
123 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_DisconnectSco, TestSize.Level1)
124 {
125     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_DisconnectSco start";
126 
127     profile_ = HandsFreeUnit::GetProfile();
128     BluetoothRemoteDevice device;
129     EXPECT_EQ(profile_->DisconnectSco(device), true);
130 
131     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_DisconnectSco end";
132 }
133 
134 /*
135  * @tc.number: HandsFreeUnit004
136  * @tc.name: GetDevicesByStates
137  * @tc.desc: Get remote AG device list which are in the specified states.
138 */
139 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_GetDevicesByStates, TestSize.Level1)
140 {
141     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetDevicesByStates start";
142 
143     profile_ = HandsFreeUnit::GetProfile();
144     BluetoothRemoteDevice device;
145     vector<int> states = {static_cast<int>(BTConnectState::CONNECTED)};
146     vector<BluetoothRemoteDevice> devices = profile_->GetDevicesByStates(states);
147 
148     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetDevicesByStates end";
149 }
150 
151 /*
152  * @tc.number: HandsFreeUnit005
153  * @tc.name: GetDeviceState
154  * @tc.desc: Get the connection state of the specified remote AG device.
155 */
156 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_GetDeviceState, TestSize.Level1)
157 {
158     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetDeviceState start";
159 
160     profile_ = HandsFreeUnit::GetProfile();
161     BluetoothRemoteDevice device;
162     EXPECT_EQ(profile_->GetDeviceState(device), 0);
163 
164     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetDeviceState end";
165 }
166 
167 /*
168  * @tc.number: HandsFreeUnit006
169  * @tc.name: GetScoState
170  * @tc.desc: Get the Audio connection state of the specified remote AG device.
171 */
172 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_GetScoState, TestSize.Level1)
173 {
174     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetScoState start";
175 
176     profile_ = HandsFreeUnit::GetProfile();
177     BluetoothRemoteDevice device;
178     EXPECT_EQ(profile_->GetScoState(device), 0);
179 
180     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetScoState end";
181 }
182 
183 /*
184  * @tc.number: HandsFreeUnit007
185  * @tc.name: SendDTMFTone
186  * @tc.desc: Send DTMF tone code to remote AG device.
187 */
188 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_SendDTMFTone, TestSize.Level1)
189 {
190     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_SendDTMFTone start";
191 
192     profile_ = HandsFreeUnit::GetProfile();
193     BluetoothRemoteDevice device;
194     EXPECT_EQ(profile_->SendDTMFTone(device, 0), true);
195 
196     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_SendDTMFTone end";
197 }
198 
199 
200 /*
201  * @tc.number: HandsFreeUnit008
202  * @tc.name: Connect
203  * @tc.desc: Initiate the establishment of a service level connection to remote AG device.
204 */
205 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_Connect, TestSize.Level1)
206 {
207     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_Connect start";
208 
209     profile_ = HandsFreeUnit::GetProfile();
210     BluetoothRemoteDevice device;
211     EXPECT_EQ(profile_->Connect(device), true);
212 
213     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_Connect end";
214 }
215 
216 /*
217  * @tc.number: HandsFreeUnit009
218  * @tc.name: Disconnect
219  * @tc.desc: Release the audio connection from remote AG device.
220 */
221 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_Disconnect, TestSize.Level1)
222 {
223     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_Disconnect start";
224 
225     profile_ = HandsFreeUnit::GetProfile();
226     BluetoothRemoteDevice device;
227     EXPECT_EQ(profile_->Disconnect(device), true);
228 
229     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_Disconnect end";
230 }
231 
232 
233 /*
234  * @tc.number: HandsFreeUnit010
235  * @tc.name: OpenVoiceRecognition
236  * @tc.desc: Open voice recognition.
237 */
238 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_OpenVoiceRecognition, TestSize.Level1)
239 {
240     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_OpenVoiceRecognition start";
241 
242     profile_ = HandsFreeUnit::GetProfile();
243     BluetoothRemoteDevice device;
244     EXPECT_EQ(profile_->OpenVoiceRecognition(device), true);
245 
246     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_OpenVoiceRecognition end";
247 }
248 
249 /*
250  * @tc.number: HandsFreeUnit011
251  * @tc.name: CloseVoiceRecognition
252  * @tc.desc: Close voice recognition.
253 */
254 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_CloseVoiceRecognition, TestSize.Level1)
255 {
256     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_CloseVoiceRecognition start";
257 
258     profile_ = HandsFreeUnit::GetProfile();
259     BluetoothRemoteDevice device;
260     EXPECT_EQ(profile_->CloseVoiceRecognition(device), true);
261 
262     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_CloseVoiceRecognition end";
263 }
264 
265 /*
266  * @tc.number: HandsFreeUnit012
267  * @tc.name: GetExistingCalls
268  * @tc.desc: Get a list of all existing calls.
269 */
270 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_GetExistingCalls, TestSize.Level1)
271 {
272     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetExistingCalls start";
273 
274     profile_ = HandsFreeUnit::GetProfile();
275     BluetoothRemoteDevice device;
276     vector<HandsFreeUnitCall> calls = profile_->GetExistingCalls(device);
277 
278     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetExistingCalls end";
279 }
280 
281 /*
282  * @tc.number: HandsFreeUnit013
283  * @tc.name: AcceptIncomingCall
284  * @tc.desc: Accept an incoming call.
285 */
286 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_AcceptIncomingCall, TestSize.Level1)
287 {
288     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_AcceptIncomingCall start";
289 
290     profile_ = HandsFreeUnit::GetProfile();
291     BluetoothRemoteDevice device;
292     EXPECT_EQ(profile_->AcceptIncomingCall(device, 0), true);
293 
294     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_AcceptIncomingCall end";
295 }
296 
297 /*
298  * @tc.number: HandsFreeUnit014
299  * @tc.name: HoldActiveCall
300  * @tc.desc: Hold an active call.
301 */
302 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_HoldActiveCall, TestSize.Level1)
303 {
304     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_HoldActiveCall start";
305 
306     profile_ = HandsFreeUnit::GetProfile();
307     BluetoothRemoteDevice device;
308     EXPECT_EQ(profile_->HoldActiveCall(device), true);
309 
310     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_HoldActiveCall end";
311 }
312 
313 /*
314  * @tc.number: HandsFreeUnit015
315  * @tc.name: RejectIncomingCall
316  * @tc.desc: Reject an incoming call.
317 */
318 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_RejectIncomingCall, TestSize.Level1)
319 {
320     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_RejectIncomingCall start";
321 
322     profile_ = HandsFreeUnit::GetProfile();
323     BluetoothRemoteDevice device;
324     EXPECT_EQ(profile_->RejectIncomingCall(device), true);
325 
326     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_RejectIncomingCall end";
327 }
328 
329 /*
330  * @tc.number: HandsFreeUnit016
331  * @tc.name: FinishActiveCall
332  * @tc.desc: Start dial with specified call number.
333 */
334 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_FinishActiveCall, TestSize.Level1)
335 {
336     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_FinishActiveCall start";
337 
338     profile_ = HandsFreeUnit::GetProfile();
339     BluetoothRemoteDevice device;
340     HandsFreeUnitCall call;
341     EXPECT_EQ(profile_->FinishActiveCall(device, call), true);
342 
343     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_FinishActiveCall end";
344 }
345 
346 /*
347  * @tc.number: HandsFreeUnit017
348  * @tc.name: StartDial
349  * @tc.desc: Start dial with specified call number.
350 */
351 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_StartDial, TestSize.Level1)
352 {
353     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_StartDial start";
354 
355     BluetoothRemoteDevice device;
356     std::string number = "000";
357     profile_ = HandsFreeUnit::GetProfile();
358     std::optional<HandsFreeUnitCall> call = profile_->StartDial(device, number);
359 
360     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_StartDial end";
361 }
362 
363 /*
364  * @tc.number: HandsFreeUnit018
365  * @tc.name: RegisterObserver
366  * @tc.desc: Register HandsFree Unit observer instance.
367 */
368 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_RegisterObserver, TestSize.Level1)
369 {
370     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_RegisterObserver start";
371 
372     profile_ = HandsFreeUnit::GetProfile();
373     profile_->RegisterObserver(&observer_);
374 
375     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_RegisterObserver end";
376 }
377 
378 /*
379  * @tc.number: HandsFreeUnit019
380  * @tc.name: DeregisterObserver
381  * @tc.desc: Deregister HandsFree Unit observer instance.
382 */
383 HWTEST_F(HandsFreeUnitTest, HandsFreeUnit_UnitTest_DeregisterObserver, TestSize.Level1)
384 {
385     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_DeregisterObserver start";
386 
387     profile_ = HandsFreeUnit::GetProfile();
388     profile_->DeregisterObserver(&observer_);
389 
390     GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_DeregisterObserver end";
391 }
392 
393 }  // namespace Bluetooth
394 }  // namespace OHOS