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