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_errorcode.h"
18#include "bluetooth_hfp_ag.h"
19#include "bluetooth_host.h"
20
21using namespace testing;
22using namespace testing::ext;
23using namespace std;
24
25namespace OHOS {
26namespace Bluetooth {
27constexpr int TIME = 2;
28
29class HandsFreeAudioGatewayObserverCommon : public HandsFreeAudioGatewayObserver {
30public:
31    HandsFreeAudioGatewayObserverCommon() = default;
32    virtual ~HandsFreeAudioGatewayObserverCommon() = default;
33
34    void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) {}
35    void OnScoStateChanged(const BluetoothRemoteDevice &device, int state) {}
36    void OnActiveDeviceChanged(const BluetoothRemoteDevice &device) {}
37    void OnHfEnhancedDriverSafetyChanged(const BluetoothRemoteDevice &device, int indValue) {}
38
39private:
40};
41
42static std::shared_ptr<HandsFreeAudioGatewayObserverCommon> observer_ =
43    std::make_shared<HandsFreeAudioGatewayObserverCommon>();
44static HandsFreeAudioGateway *profile_;
45static BluetoothHost *host_;
46
47class HandsFreeAudioGatewayTest : public testing::Test {
48public:
49    HandsFreeAudioGatewayTest()
50    {}
51    ~HandsFreeAudioGatewayTest()
52    {}
53
54    static void SetUpTestCase(void);
55    static void TearDownTestCase(void);
56    void SetUp();
57    void TearDown();
58};
59
60
61void HandsFreeAudioGatewayTest::SetUpTestCase(void)
62{
63
64}
65void HandsFreeAudioGatewayTest::TearDownTestCase(void)
66{
67
68}
69void HandsFreeAudioGatewayTest::SetUp()
70{
71    host_ = &BluetoothHost::GetDefaultHost();
72    host_->EnableBt();
73    host_->EnableBle();
74    sleep(TIME);
75}
76
77void HandsFreeAudioGatewayTest::TearDown()
78{
79    host_->DisableBt();
80    host_->DisableBle();
81    host_ = nullptr;
82}
83
84
85/*
86 * @tc.number: HandsFreeAudioGateway001
87 * @tc.name: GetProfile
88 * @tc.desc: Get the instance of HandsFreeAudioGateway object
89*/
90HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_GetProfile, TestSize.Level1)
91{
92    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetProfile start";
93
94    profile_ = HandsFreeAudioGateway::GetProfile();
95
96    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetProfile end";
97}
98
99
100/*
101 * @tc.number: HandsFreeAudioGateway002
102 * @tc.name: GetConnectedDevices
103 * @tc.desc: Get remote HF device list which are in the connected state.
104*/
105HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_GetConnectedDevices, TestSize.Level1)
106{
107    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetConnectedDevices start";
108
109    profile_ = HandsFreeAudioGateway::GetProfile();
110    std::vector<BluetoothRemoteDevice> devices;
111    profile_->GetConnectedDevices(devices);
112
113    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetConnectedDevices end";
114}
115
116/*
117 * @tc.number: HandsFreeAudioGateway003
118 * @tc.name: GetDevicesByStates
119 * @tc.desc: Get remote HF device list which are in the specified states.
120*/
121HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_GetDevicesByStates, TestSize.Level1)
122{
123    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetDevicesByStates start";
124
125    profile_ = HandsFreeAudioGateway::GetProfile();
126    vector<int> states = {static_cast<int>(BTConnectState::CONNECTED)};
127    std::vector<BluetoothRemoteDevice> devices;
128    profile_->GetDevicesByStates(states, devices);
129    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetDevicesByStates end";
130}
131
132/*
133 * @tc.number: HandsFreeAudioGateway004
134 * @tc.name: GetDeviceState
135 * @tc.desc: Get the connection state of the specified remote HF device.
136*/
137HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_GetDeviceState, TestSize.Level1)
138{
139    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetDeviceState start";
140
141    profile_ = HandsFreeAudioGateway::GetProfile();
142    BluetoothRemoteDevice device;
143    int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
144    int state = profile_->GetDeviceState(device, state);
145    EXPECT_EQ(state, 0);
146    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetDeviceState end";
147}
148
149
150/*
151 * @tc.number: HandsFreeAudioGateway005
152 * @tc.name: Connect
153 * @tc.desc: Initiate the establishment of a service level connection to remote HF device.
154*/
155HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_Connect, TestSize.Level1)
156{
157    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_Connect start";
158
159    profile_ = HandsFreeAudioGateway::GetProfile();
160    BluetoothRemoteDevice device;
161    bool isOK = (profile_->Connect(device) == NO_ERROR ? true : false);
162    EXPECT_EQ(isOK, true);
163
164    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_Connect end";
165}
166
167
168/*
169 * @tc.number: HandsFreeAudioGateway006
170 * @tc.name: Disconnect
171 * @tc.desc: Release the audio connection from remote HF device.
172*/
173HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_Disconnect, TestSize.Level1)
174{
175    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ start";
176
177    profile_ = HandsFreeAudioGateway::GetProfile();
178    BluetoothRemoteDevice device;
179    bool isOK = (profile_->Disconnect(device) == NO_ERROR ? true : false);
180    EXPECT_EQ(isOK, true);
181    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_Disconnect end";
182}
183
184
185/*
186 * @tc.number: HandsFreeAudioGateway007
187 * @tc.name: GetScoState
188 * @tc.desc: Get the Audio connection state of the specified remote HF device.
189*/
190HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_GetScoState, TestSize.Level1)
191{
192    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ start";
193
194    profile_ = HandsFreeAudioGateway::GetProfile();
195    BluetoothRemoteDevice device;
196    int state = profile_->GetScoState(device);
197    EXPECT_EQ(state, 0);
198    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ end";
199}
200
201
202/*
203 * @tc.number: HandsFreeAudioGateway008
204 * @tc.name: ConnectSco
205 * @tc.desc: Initiate the establishment of an audio connection to remote active HF device.
206*/
207HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_ConnectSco, TestSize.Level1)
208{
209    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ConnectSco start";
210
211    profile_ = HandsFreeAudioGateway::GetProfile();
212    bool isOK = profile_->ConnectSco();
213    EXPECT_EQ(isOK, true);
214    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ConnectSco end";
215}
216
217
218/*
219 * @tc.number: HandsFreeAudioGateway009
220 * @tc.name: DisconnectSco
221 * @tc.desc: Release the audio connection from remote active HF device.
222*/
223HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_DisconnectSco, TestSize.Level1)
224{
225    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_DisconnectSco start";
226
227    profile_ = HandsFreeAudioGateway::GetProfile();
228    bool isOK = profile_->DisconnectSco();
229    EXPECT_EQ(isOK, true);
230
231    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_DisconnectSco end";
232}
233
234
235/*
236 * @tc.number: HandsFreeAudioGateway010
237 * @tc.name: PhoneStateChanged
238 * @tc.desc: This function used to Update changed phone call informations.
239*/
240HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_PhoneStateChanged, TestSize.Level1)
241{
242    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_PhoneStateChanged start";
243
244    profile_ = HandsFreeAudioGateway::GetProfile();
245    profile_->PhoneStateChanged(0, 0, 0, "123", 0, "");
246    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_PhoneStateChanged end";
247}
248
249
250/*
251 * @tc.number: HandsFreeAudioGateway011
252 * @tc.name: ClccResponse
253 * @tc.desc: Send response for querying standard list current calls by remote Hf device.
254*/
255HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_ClccResponse, TestSize.Level1)
256{
257    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ClccResponse start";
258
259    profile_ = HandsFreeAudioGateway::GetProfile();
260    profile_->ClccResponse(0, 0, 0, 0, false, "123", 0);
261
262    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_ClccResponse end";
263}
264
265
266/*
267 * @tc.number: HandsFreeAudioGateway012
268 * @tc.name: OpenVoiceRecognition
269 * @tc.desc: Open the voice recognition.
270*/
271HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_OpenVoiceRecognition, TestSize.Level1)
272{
273    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_OpenVoiceRecognition start";
274
275    profile_ = HandsFreeAudioGateway::GetProfile();
276    BluetoothRemoteDevice device;
277    bool isOK = profile_->OpenVoiceRecognition(device);
278    EXPECT_EQ(isOK, true);
279
280    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_OpenVoiceRecognition end";
281}
282
283
284/*
285 * @tc.number: HandsFreeAudioGateway013
286 * @tc.name: CloseVoiceRecognition
287 * @tc.desc: Close the voice recognition.
288*/
289HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_CloseVoiceRecognition, TestSize.Level1)
290{
291    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_CloseVoiceRecognition start";
292
293    profile_ = HandsFreeAudioGateway::GetProfile();
294    BluetoothRemoteDevice device;
295    bool isOK = profile_->CloseVoiceRecognition(device);
296    EXPECT_EQ(isOK, true);
297
298    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_CloseVoiceRecognition end";
299}
300
301
302/*
303 * @tc.number: HandsFreeAudioGateway014
304 * @tc.name: SetActiveDevice
305 * @tc.desc: Set the active device for audio connection.
306*/
307HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_SetActiveDevice, TestSize.Level1)
308{
309    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_SetActiveDevice start";
310
311    profile_ = HandsFreeAudioGateway::GetProfile();
312    BluetoothRemoteDevice device;
313    bool isOK = profile_->SetActiveDevice(device);
314    EXPECT_EQ(isOK, true);
315
316    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_SetActiveDevice end";
317}
318
319/*
320 * @tc.number: HandsFreeAudioGateway015
321 * @tc.name: GetActiveDevice
322 * @tc.desc: Set the active device for audio connection.
323*/
324HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_GetActiveDevice, TestSize.Level1)
325{
326    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetActiveDevice start";
327
328    profile_ = HandsFreeAudioGateway::GetProfile();
329    BluetoothRemoteDevice device = profile_->GetActiveDevice();
330
331    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_GetActiveDevice end";
332}
333
334/*
335 * @tc.number: HandsFreeAudioGateway016
336 * @tc.name: RegisterObserver
337 * @tc.desc: Register HandsFree AudioGateway observer instance.
338*/
339HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_RegisterObserver, TestSize.Level1)
340{
341    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_RegisterObserver start";
342
343    profile_ = HandsFreeAudioGateway::GetProfile();
344    profile_->RegisterObserver(observer_);
345    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_RegisterObserver end";
346}
347
348/*
349 * @tc.number: HandsFreeAudioGateway017
350 * @tc.name: DeregisterObserver
351 * @tc.desc: Deregister HandsFree AudioGateway observer instance.
352*/
353HWTEST_F(HandsFreeAudioGatewayTest, HandsFreeAudioGateway_UnitTest_DeregisterObserver, TestSize.Level1)
354{
355    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_DeregisterObserver start";
356
357    profile_ = HandsFreeAudioGateway::GetProfile();
358    profile_->DeregisterObserver(observer_);
359    GTEST_LOG_(INFO) << "HandsFreeAudioGateway_UnitTest_DeregisterObserver end";
360}
361
362
363}  // namespace Bluetooth
364}  // namespace OHOS