1 /*
2 * Copyright (c) 2024 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
16 #include <iostream>
17 #include <gmock/gmock.h>
18 #include "gtest/gtest.h"
19 #include "audio_errors.h"
20 #include "audio_pnp_server_unit_test.h"
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace AudioStandard {
25
SetUpTestCase(void)26 void AudioPnpServerTest::SetUpTestCase(void) {}
TearDownTestCase(void)27 void AudioPnpServerTest::TearDownTestCase(void) {}
SetUp(void)28 void AudioPnpServerTest::SetUp(void)
29 {
30 audioPnpServer_ = &AudioPnpServer::GetAudioPnpServer();
31 mockCallback_ = std::make_shared<MockAudioPnpDeviceChangeCallback>();
32 }
TearDown(void)33 void AudioPnpServerTest::TearDown(void)
34 {
35 if (mockCallback_ != nullptr) {
36 mockCallback_.reset();
37 }
38 if (audioPnpServer_ != nullptr) {
39 audioPnpServer_ = nullptr;
40 }
41 }
42
43 /**
44 * @tc.name : RegisterPnpStatusListener_Success
45 * @tc.number: AudioPnpServerTest_001
46 * @tc.desc : Test RegisterPnpStatusListener function when callback is successfully registered.
47 */
48 HWTEST_F(AudioPnpServerTest, RegisterPnpStatusListener_Success, testing::ext::TestSize.Level0)
49 {
50 EXPECT_NE(audioPnpServer_, nullptr);
51 EXPECT_NE(mockCallback_, nullptr);
52 int32_t ret = audioPnpServer_->RegisterPnpStatusListener(mockCallback_);
53 EXPECT_EQ(ret, SUCCESS);
54 }
55
56 /**
57 * @tc.name : RegisterPnpStatusListener_NullCallback
58 * @tc.number: AudioPnpServerTest_002
59 * @tc.desc : Test RegisterPnpStatusListener function when callback is null.
60 */
61 HWTEST_F(AudioPnpServerTest, RegisterPnpStatusListener_NullCallback, testing::ext::TestSize.Level0)
62 {
63 EXPECT_NE(audioPnpServer_, nullptr);
64 int32_t ret = audioPnpServer_->RegisterPnpStatusListener(nullptr);
65 EXPECT_EQ(ret, SUCCESS);
66 }
67
68 /**
69 * @tc.name : UnRegisterPnpStatusListener_ShouldReturnSuccess_WhenCalled
70 * @tc.number: AudioPnpServerTest_001
71 * @tc.desc : Test if UnRegisterPnpStatusListener returns success when called.
72 */
73 HWTEST_F(AudioPnpServerTest, UnRegisterPnpStatusListener_ShouldReturnSuccess_WhenCalled, TestSize.Level0)
74 {
75 EXPECT_NE(audioPnpServer_, nullptr);
76 int32_t result = audioPnpServer_->UnRegisterPnpStatusListener();
77 EXPECT_EQ(result, SUCCESS);
78 }
79
80 /**
81 * @tc.name : OnPnpDeviceStatusChanged_ShouldCallCallback_WhenCallbackIsNotNull
82 * @tc.number: AudioPnpServerTest_002
83 * @tc.desc : Test if OnPnpDeviceStatusChanged calls the callback when it is not null.
84 */
85 HWTEST_F(AudioPnpServerTest, OnPnpDeviceStatusChanged_ShouldCallCallback_WhenCallbackIsNotNull, TestSize.Level0)
86 {
87 EXPECT_NE(audioPnpServer_, nullptr);
88 EXPECT_NE(mockCallback_, nullptr);
89 audioPnpServer_->RegisterPnpStatusListener(mockCallback_);
90
91 std::string info = "test_info";
92
93 audioPnpServer_->OnPnpDeviceStatusChanged(info);
94 EXPECT_NE(audioPnpServer_->pnpCallback_, nullptr);
95 }
96
97 /**
98 * @tc.name : OnPnpDeviceStatusChanged_ShouldNotCallCallback_WhenCallbackIsNull
99 * @tc.number: AudioPnpServerTest_003
100 * @tc.desc : Test if OnPnpDeviceStatusChanged does not call the callback when it is null.
101 */
102 HWTEST_F(AudioPnpServerTest, OnPnpDeviceStatusChanged_ShouldNotCallCallback_WhenCallbackIsNull, TestSize.Level0)
103 {
104 EXPECT_NE(audioPnpServer_, nullptr);
105 audioPnpServer_->UnRegisterPnpStatusListener();
106
107 std::string info = "test_info";
108 audioPnpServer_->OnPnpDeviceStatusChanged(info);
109 EXPECT_EQ(audioPnpServer_->pnpCallback_, nullptr);
110 }
111
112 /**
113 * @tc.name : DetectAudioDevice_AnalogHeadsetStateAdded_Success
114 * @tc.number: AudioPnpServerTest_001
115 * @tc.desc : Test DetectAudioDevice when AnalogHeadsetState is added and DetectAnalogHeadsetState returns SUCCESS.
116 */
117 HWTEST_F(AudioPnpServerTest, DetectAudioDevice_AnalogHeadsetStateAdded_Success, TestSize.Level0)
118 {
119 EXPECT_NE(audioPnpServer_, nullptr);
120 audioPnpServer_->DetectAudioDevice();
121 EXPECT_EQ(audioPnpServer_->pnpCallback_, nullptr);
122 }
123
124 /**
125 * @tc.name : DetectAudioDpDevice_Success_Add
126 * @tc.number: AudioPnpServerTest_001
127 * @tc.desc : Test DetectAudioDpDevice function when DetectDPState
128 */
129 HWTEST_F(AudioPnpServerTest, DetectAudioDpDevice_Success_Add, TestSize.Level0)
130 {
131 EXPECT_NE(audioPnpServer_, nullptr);
132 // Arrange
133 AudioEvent audioEvent = {0};
134 // Act
135 audioPnpServer_->DetectAudioDpDevice();
136
137 // Assert
138 EXPECT_EQ(audioEvent.eventType, 0);
139 }
140
141 /**
142 * @tc.name : StopPnpServer
143 * @tc.number: AudioPnpServerTest_003
144 * @tc.desc : Test StopPnpServer function.
145 */
146 HWTEST_F(AudioPnpServerTest, StopPnpServer, TestSize.Level0)
147 {
148 EXPECT_NE(audioPnpServer_, nullptr);
149
150 // Act
151 audioPnpServer_->StopPnpServer();
152
153 EXPECT_EQ(audioPnpServer_->socketThread_, nullptr);
154 EXPECT_EQ(audioPnpServer_->inputThread_, nullptr);
155 }
156 } // namespace AudioStandard
157 } // namespace OHOS