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 "bluetooth_avrcp_ct.h"
18#include "bluetooth_avrcp_tg.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 AvrcpTargetObserverCommon : public AvrcpTarget::IObserver {
30public:
31    AvrcpTargetObserverCommon() = default;
32    virtual ~AvrcpTargetObserverCommon() = default;
33
34    void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) {}
35private:
36};
37
38static std::shared_ptr<AvrcpTargetObserverCommon> observer_ = std::make_shared<AvrcpTargetObserverCommon>();
39static AvrcpTarget *profile_;
40
41class AvrcpTargetTest : public testing::Test {
42public:
43    AvrcpTargetTest()
44    {}
45    ~AvrcpTargetTest()
46    {}
47
48    static void SetUpTestCase(void);
49    static void TearDownTestCase(void);
50    void SetUp();
51    void TearDown();
52    BluetoothHost *host_;
53};
54
55
56void AvrcpTargetTest::SetUpTestCase(void)
57{}
58void AvrcpTargetTest::TearDownTestCase(void)
59{}
60void AvrcpTargetTest::SetUp()
61{
62    host_ = &BluetoothHost::GetDefaultHost();
63    host_->EnableBt();
64    host_->EnableBle();
65    sleep(TIME);
66}
67
68void AvrcpTargetTest::TearDown()
69{
70    host_->DisableBt();
71    host_->DisableBle();
72    host_ = nullptr;
73}
74
75/*
76 * @tc.number: AvrcpTarget001
77 * @tc.name: GetProfile
78 * @tc.desc: Gets the static instance of the <b>AvrcpTarget</b> class.
79*/
80HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_GetProfile, TestSize.Level1)
81{
82    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetProfile start";
83
84    profile_ = AvrcpTarget::GetProfile();
85
86    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetProfile end";
87}
88
89/*
90 * @tc.number: AvrcpTarget002
91 * @tc.name: GetConnectedDevices
92 * @tc.desc: Gets the connected devices.
93*/
94HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_GetConnectedDevices, TestSize.Level1)
95{
96    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConnectedDevices start";
97
98    profile_ = AvrcpTarget::GetProfile();
99    vector<BluetoothRemoteDevice> devices = profile_->GetConnectedDevices();
100
101    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConnectedDevices end";
102}
103
104/*
105 * @tc.number: AvrcpTarget003
106 * @tc.name: GetDeviceState
107 * @tc.desc: Gets the connection state of the specified bluetooth device.
108*/
109HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_GetDeviceState, TestSize.Level1)
110{
111    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetDeviceState start";
112
113    profile_ = AvrcpTarget::GetProfile();
114    BluetoothRemoteDevice device;
115    int state = profile_->GetDeviceState(device);
116    EXPECT_EQ(state, 0);
117    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetDeviceState end";
118}
119
120/*
121 * @tc.number: AvrcpTarget004
122 * @tc.name: RegisterObserver
123 * @tc.desc: Registers the observer.
124*/
125HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_RegisterObserver, TestSize.Level1)
126{
127    GTEST_LOG_(INFO) << "Avrcp_UnitTest_RegisterObserver start";
128
129    profile_ = AvrcpTarget::GetProfile();
130    profile_->RegisterObserver(observer_);
131    GTEST_LOG_(INFO) << "Avrcp_UnitTest_RegisterObserver end";
132}
133
134/*
135 * @tc.number: AvrcpTarget005
136 * @tc.name: UnregisterObserver
137 * @tc.desc: Unregisters the observer.
138*/
139HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_UnregisterObserver, TestSize.Level1)
140{
141    GTEST_LOG_(INFO) << "Avrcp_UnitTest_DeregisterObserver start";
142
143    profile_ = AvrcpTarget::GetProfile();
144    profile_->UnregisterObserver(observer_);
145    GTEST_LOG_(INFO) << "Avrcp_UnitTest_DeregisterObserver end";
146}
147
148/*
149 * @tc.number: AvrcpTarget006
150 * @tc.name: SetActiveDevice
151 * @tc.desc: Sets the active device.
152*/
153HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_SetActiveDevice, TestSize.Level1)
154{
155    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetActiveDevice start";
156
157    profile_ = AvrcpTarget::GetProfile();
158    BluetoothRemoteDevice device;
159    profile_->SetActiveDevice(device);
160
161    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetActiveDevice end";
162}
163
164/*
165 * @tc.number: AvrcpTarget007
166 * @tc.name: GetDevicesByStates
167 * @tc.desc: Gets the devices of the specified states.
168*/
169HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_GetDevicesByStates, TestSize.Level1)
170{
171    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetDevicesByStates start";
172
173    profile_ = AvrcpTarget::GetProfile();
174    vector<int> states = {static_cast<int>(BTConnectState::CONNECTED)};
175    vector<BluetoothRemoteDevice> devices = profile_->GetDevicesByStates(states);
176
177    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetDevicesByStates end";
178}
179
180/*
181 * @tc.number: AvrcpTarget008
182 * @tc.name: Connect
183 * @tc.desc: Connects to the AVRCP CT service.
184*/
185HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_Connect, TestSize.Level1)
186{
187    GTEST_LOG_(INFO) << "Avrcp_UnitTest_Connect start";
188
189    profile_ = AvrcpTarget::GetProfile();
190    BluetoothRemoteDevice device;
191    profile_->Connect(device);
192
193    GTEST_LOG_(INFO) << "Avrcp_UnitTest_Connect end";
194}
195
196/*
197 * @tc.number: AvrcpTarget009
198 * @tc.name: Disconnect
199 * @tc.desc: Disconnects from the AVRCP CT service.
200*/
201HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_Disconnect, TestSize.Level1)
202{
203    GTEST_LOG_(INFO) << "Avrcp_UnitTest_Disconnect start";
204
205    profile_ = AvrcpTarget::GetProfile();
206    BluetoothRemoteDevice device;
207    profile_->Disconnect(device);
208
209    GTEST_LOG_(INFO) << "Avrcp_UnitTest_Disconnect end";
210}
211
212/*
213 * @tc.number: AvrcpTarget010
214 * @tc.name: NotifyPlaybackStatusChanged
215 * @tc.desc: Notifies the playback status is changed.
216*/
217HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyPlaybackStatusChanged, TestSize.Level1)
218{
219    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyPlaybackStatusChanged start";
220
221    profile_ = AvrcpTarget::GetProfile();
222    profile_->NotifyPlaybackStatusChanged(0,0);
223
224    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyPlaybackStatusChanged end";
225}
226
227/*
228 * @tc.number: AvrcpTarget011
229 * @tc.name: NotifyTrackChanged
230 * @tc.desc: Notifies the track is changed.
231*/
232HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyTrackChanged, TestSize.Level1)
233{
234    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyTrackChanged start";
235
236    profile_ = AvrcpTarget::GetProfile();
237    profile_->NotifyTrackChanged(0,0);
238
239    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyTrackChanged end";
240}
241
242/*
243 * @tc.number: AvrcpTarget012
244 * @tc.name: NotifyTrackReachedEnd
245 * @tc.desc: Notifies the track reached end is changed.
246*/
247HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyTrackReachedEnd, TestSize.Level1)
248{
249    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyTrackReachedEnd start";
250
251    profile_ = AvrcpTarget::GetProfile();
252    profile_->NotifyTrackReachedEnd(0);
253
254    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyTrackReachedEnd end";
255}
256
257/*
258 * @tc.number: AvrcpTarget013
259 * @tc.name: NotifyTrackReachedStart
260 * @tc.desc: Notifies the track reached start is changed.
261*/
262HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyTrackReachedStart, TestSize.Level1)
263{
264    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyTrackReachedStart start";
265
266    profile_ = AvrcpTarget::GetProfile();
267    profile_->NotifyTrackReachedStart(0);
268
269    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyTrackReachedStart end";
270}
271
272/*
273 * @tc.number: AvrcpTarget014
274 * @tc.name: NotifyPlaybackPosChanged
275 * @tc.desc: Notifies the player application setting is changed.
276*/
277HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyPlaybackPosChanged, TestSize.Level1)
278{
279    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyPlaybackPosChanged start";
280
281    profile_ = AvrcpTarget::GetProfile();
282    profile_->NotifyPlaybackPosChanged(0);
283
284    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyPlaybackPosChanged end";
285}
286
287/*
288 * @tc.number: AvrcpTarget015
289 * @tc.name: NotifyPlayerAppSettingChanged
290 * @tc.desc: Notifies the player application setting is changed.
291*/
292HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyPlayerAppSettingChanged, TestSize.Level1)
293{
294    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyPlayerAppSettingChanged start";
295
296    profile_ = AvrcpTarget::GetProfile();
297    vector<uint8_t> attributes = {0};
298    vector<uint8_t> values = {0};
299    profile_->NotifyPlayerAppSettingChanged(attributes, values);
300
301    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyPlayerAppSettingChanged end";
302}
303
304/*
305 * @tc.number: AvrcpTarget016
306 * @tc.name: NotifyNowPlayingContentChanged
307 * @tc.desc: Notifies the addressed player is changed.
308*/
309HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyNowPlayingContentChanged, TestSize.Level1)
310{
311    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyNowPlayingContentChanged start";
312
313    profile_ = AvrcpTarget::GetProfile();
314
315    profile_->NotifyNowPlayingContentChanged();
316
317    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyNowPlayingContentChanged end";
318}
319
320/*
321 * @tc.number: AvrcpTarget017
322 * @tc.name: NotifyAvailablePlayersChanged
323 * @tc.desc: Notifies that has the available player.
324*/
325HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyAvailablePlayersChanged, TestSize.Level1)
326{
327    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyAvailablePlayersChanged start";
328
329    profile_ = AvrcpTarget::GetProfile();
330
331    profile_->NotifyAvailablePlayersChanged();
332
333    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyAvailablePlayersChanged end";
334}
335
336/*
337 * @tc.number: AvrcpTarget018
338 * @tc.name: NotifyAddressedPlayerChanged
339 * @tc.desc: Notifies the addressed player is changed.
340*/
341HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyAddressedPlayerChanged, TestSize.Level1)
342{
343    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyAddressedPlayerChanged start";
344
345    profile_ = AvrcpTarget::GetProfile();
346
347    profile_->NotifyAddressedPlayerChanged(0 ,0);
348
349    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyAddressedPlayerChanged end";
350}
351
352/*
353 * @tc.number: AvrcpTarget019
354 * @tc.name: NotifyUidChanged
355 * @tc.desc: Notifies the uids is changed.
356*/
357HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyUidChanged, TestSize.Level1)
358{
359    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyUidChanged start";
360
361    profile_ = AvrcpTarget::GetProfile();
362
363    profile_->NotifyUidChanged(0);
364
365    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyUidChanged end";
366}
367
368/*
369 * @tc.number: AvrcpTarget020
370 * @tc.name: NotifyVolumeChanged
371 * @tc.desc: Notifies the absolute volume is changed.
372*/
373HWTEST_F(AvrcpTargetTest, Avrcp_UnitTest_NotifyVolumeChanged, TestSize.Level1)
374{
375    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyVolumeChanged start";
376
377    profile_ = AvrcpTarget::GetProfile();
378
379    profile_->NotifyVolumeChanged(0);
380
381    GTEST_LOG_(INFO) << "Avrcp_UnitTest_NotifyVolumeChanged end";
382}
383}  // namespace Bluetooth
384}  // namespace OHOS