1 /*
2 * Copyright (c) 2025 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 <cstring>
16 #include "audio_collaborative_service_unit_test.h"
17 #include "audio_errors.h"
18
19 using namespace testing::ext;
20 using namespace testing;
21
22 namespace OHOS {
23 namespace AudioStandard {
24 static std::unique_ptr<AudioCollaborativeService> audioCollaborativeServicePtr_ = nullptr;
SetUpTestCase(void)25 void AudioCollaborativeServiceUnitTest::SetUpTestCase(void) {}
TearDownTestCase(void)26 void AudioCollaborativeServiceUnitTest::TearDownTestCase(void) {}
SetUp(void)27 void AudioCollaborativeServiceUnitTest::SetUp(void)
28 {
29 audioCollaborativeServicePtr_ = std::make_unique<AudioCollaborativeService>();
30 }
TearDown(void)31 void AudioCollaborativeServiceUnitTest::TearDown(void)
32 {
33 audioCollaborativeServicePtr_.reset();
34 }
35 static std::string testAddr1 = "address1";
36 static std::string testAddr2 = "address2";
37 static std::string testAddr3 = "address3";
38 static const std::string TEST_NAME = "test_name";
39 static const std::string TEST_LABEL = "test_label";
40 static const std::string EMPTY_MAC_ADDR = "";
41 static const std::string TEST_MAC_ADDR = "8C-32-23-23-6C-12";
42 static const std::string AUDIO_COLLABORATIVE_SERVICE_LABEL = "COLLABORATIVE";
43 static const std::string BLUETOOTH_EFFECT_CHAIN_NAME = "EFFECTCHAIN_COLLABORATIVE";
44
45 /**
46 * @tc.name : Test Su.
47 * @tc.number: AudioSpatializationService_001
48 * @tc.desc : Test isCollaborativePlaybackSupported.
49 */
50 HWTEST_F(AudioCollaborativeServiceUnitTest, AudioCollaborativeService_001, TestSize.Level0)
51 {
52 bool isSupported = audioCollaborativeService_.IsCollaborativePlaybackSupported();
53 EXPECT_EQ(isSupported, false);
54 }
55
56 /**
57 * @tc.name : Test Su.
58 * @tc.number: AudioSpatializationService_002
59 * @tc.desc : Test SetCollaborativePlaybackEnabledForDevice.
60 */
61 HWTEST_F(AudioCollaborativeServiceUnitTest, AudioCollaborativeService_002, TestSize.Level0)
62 {
63 const std::shared_ptr<AudioDeviceDescriptor> audioDevice1 = std::make_shared<AudioDeviceDescriptor>();
64 audioDevice1->macAddress_ = testAddr1;
65 int32_t ret = audioCollaborativeService_.SetCollaborativePlaybackEnabledForDevice(audioDevice1, true);
66 EXPECT_EQ(ret, ERROR);
67 ret = audioCollaborativeService_.SetCollaborativePlaybackEnabledForDevice(audioDevice1, true);
68 EXPECT_EQ(ret, ERROR);
69 ret = audioCollaborativeService_.SetCollaborativePlaybackEnabledForDevice(audioDevice1, false);
70 EXPECT_EQ(ret, ERROR);
71 ret = audioCollaborativeService_.SetCollaborativePlaybackEnabledForDevice(audioDevice1, false);
72 EXPECT_EQ(ret, ERROR);
73 }
74
75 /**
76 * @tc.name : Test Su.
77 * @tc.number: AudioSpatializationService_003
78 * @tc.desc : Test IsCollaborativePlaybackEnabledForDevice.
79 */
80 HWTEST_F(AudioCollaborativeServiceUnitTest, AudioCollaborativeService_003, TestSize.Level0)
81 {
82 const std::shared_ptr<AudioDeviceDescriptor> AudioDevice1 = std::make_shared<AudioDeviceDescriptor>();
83 const std::shared_ptr<AudioDeviceDescriptor> AudioDevice2 = std::make_shared<AudioDeviceDescriptor>();
84 const std::shared_ptr<AudioDeviceDescriptor> AudioDevice3 = std::make_shared<AudioDeviceDescriptor>();
85 AudioDevice1->macAddress_ = testAddr1;
86 AudioDevice2->macAddress_ = testAddr2;
87 AudioDevice3->macAddress_ = testAddr3;
88 int32_t ret = audioCollaborativeService_.SetCollaborativePlaybackEnabledForDevice(AudioDevice1, true);
89 EXPECT_EQ(ret, ERROR);
90 ret = audioCollaborativeService_.SetCollaborativePlaybackEnabledForDevice(AudioDevice2, false);
91 EXPECT_EQ(ret, ERROR);
92 bool isEnabled = audioCollaborativeService_.IsCollaborativePlaybackEnabledForDevice(AudioDevice1);
93 EXPECT_EQ(isEnabled, true);
94 isEnabled = audioCollaborativeService_.IsCollaborativePlaybackEnabledForDevice(AudioDevice2);
95 EXPECT_EQ(isEnabled, false);
96 isEnabled = audioCollaborativeService_.IsCollaborativePlaybackEnabledForDevice(AudioDevice3);
97 EXPECT_EQ(isEnabled, false);
98 }
99
100 /**
101 * @tc.name : Test Su.
102 * @tc.number: AudioSpatializationService_004
103 * @tc.desc : Test UpdateCurrentDevice.
104 */
105 HWTEST_F(AudioCollaborativeServiceUnitTest, AudioCollaborativeService_004, TestSize.Level0)
106 {
107 const std::shared_ptr<AudioDeviceDescriptor> AudioDevice1 = std::make_shared<AudioDeviceDescriptor>();
108 const std::shared_ptr<AudioDeviceDescriptor> AudioDevice2 = std::make_shared<AudioDeviceDescriptor>();
109 const std::shared_ptr<AudioDeviceDescriptor> AudioDevice3 = std::make_shared<AudioDeviceDescriptor>();
110 AudioDevice1->macAddress_ = testAddr1;
111 AudioDevice1->deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
112 AudioDevice2->macAddress_ = testAddr2;
113 AudioDevice2->deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
114 AudioDevice3->macAddress_ = testAddr3;
115 AudioDevice3->deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
116 int32_t ret = audioCollaborativeService_.SetCollaborativePlaybackEnabledForDevice(AudioDevice1, true);
117 EXPECT_EQ(ret, ERROR);
118 ret = audioCollaborativeService_.SetCollaborativePlaybackEnabledForDevice(AudioDevice2, false);
119 EXPECT_EQ(ret, ERROR);
120 audioCollaborativeService_.UpdateCurrentDevice(*AudioDevice1);
121 audioCollaborativeService_.UpdateCurrentDevice(*AudioDevice2);
122 audioCollaborativeService_.UpdateCurrentDevice(*AudioDevice3);
123 }
124
125 /**
126 * @tc.name : Test Su.
127 * @tc.number: AudioCollaborativeService_005
128 * @tc.desc : Test Init.
129 */
130 HWTEST_F(AudioCollaborativeServiceUnitTest, AudioCollaborativeService_005, TestSize.Level0)
131 {
132 std::vector<std::string> applyVec;
133 EffectChain effectChain1(BLUETOOTH_EFFECT_CHAIN_NAME, applyVec, AUDIO_COLLABORATIVE_SERVICE_LABEL);
134 EffectChain effectChain2(BLUETOOTH_EFFECT_CHAIN_NAME, applyVec, TEST_LABEL);
135 EffectChain effectChain3(TEST_NAME, applyVec, TEST_LABEL);
136 std::vector<EffectChain> effectChains = { effectChain1, effectChain2, effectChain3 };
137 audioCollaborativeServicePtr_->Init(effectChains);
138 EXPECT_TRUE(audioCollaborativeServicePtr_->isCollaborativePlaybackSupported_);
139 }
140
141 /**
142 * @tc.name : Test Su.
143 * @tc.number: AudioCollaborativeService_006
144 * @tc.desc : Test UpdateCurrentDevice.
145 */
146 HWTEST_F(AudioCollaborativeServiceUnitTest, AudioCollaborativeService_006, TestSize.Level0)
147 {
148 AudioDeviceDescriptor descriptor1;
149 descriptor1.macAddress_ = TEST_MAC_ADDR;
150 descriptor1.deviceType_ = DEVICE_TYPE_EARPIECE;
151 AudioDeviceDescriptor descriptor2;
152 descriptor2.macAddress_ = EMPTY_MAC_ADDR;
153 descriptor2.deviceType_ = DEVICE_TYPE_EARPIECE;
154 audioCollaborativeServicePtr_->addressToCollaborativeEnabledMap_
155 .insert(std::pair<std::string, bool>(TEST_MAC_ADDR, true));
156
157 audioCollaborativeServicePtr_->UpdateCurrentDevice(descriptor1);
158 EXPECT_EQ(audioCollaborativeServicePtr_->addressToCollaborativeEnabledMap_.size(), 0);
159 descriptor1.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
160 audioCollaborativeServicePtr_->UpdateCurrentDevice(descriptor1);
161 EXPECT_EQ(audioCollaborativeServicePtr_->addressToCollaborativeEnabledMap_.size(), 1);
162 audioCollaborativeServicePtr_->UpdateCurrentDevice(descriptor2);
163 EXPECT_EQ(audioCollaborativeServicePtr_->addressToCollaborativeEnabledMap_.size(), 1);
164 descriptor1.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
165 audioCollaborativeServicePtr_->UpdateCurrentDevice(descriptor2);
166 EXPECT_EQ(audioCollaborativeServicePtr_->addressToCollaborativeEnabledMap_.size(), 1);
167 }
168
169 /**
170 * @tc.name : Test Su.
171 * @tc.number: AudioCollaborativeService_007
172 * @tc.desc : Test UpdateCollaborativeStateReal.
173 */
174 HWTEST_F(AudioCollaborativeServiceUnitTest, AudioCollaborativeService_007, TestSize.Level0)
175 {
176 audioCollaborativeServicePtr_->isCollaborativePlaybackSupported_ = false;
177 EXPECT_EQ(audioCollaborativeServicePtr_->UpdateCollaborativeStateReal(), ERROR);
178
179 audioCollaborativeServicePtr_->isCollaborativePlaybackSupported_ = true;
180 audioCollaborativeServicePtr_->isCollaborativeStateEnabled_ = true;
181 audioCollaborativeServicePtr_->addressToCollaborativeEnabledMap_
182 .insert(std::pair<std::string, bool>(TEST_MAC_ADDR, true));
183 audioCollaborativeServicePtr_->curDeviceAddress_ = EMPTY_MAC_ADDR;
184 EXPECT_EQ(audioCollaborativeServicePtr_->UpdateCollaborativeStateReal(),
185 ERR_OPERATION_FAILED);
186
187 audioCollaborativeServicePtr_->curDeviceAddress_ = TEST_MAC_ADDR;
188 EXPECT_EQ(audioCollaborativeServicePtr_->UpdateCollaborativeStateReal(), ERR_OPERATION_FAILED);
189
190 audioCollaborativeServicePtr_->isCollaborativeStateEnabled_ = true;
191 EXPECT_EQ(audioCollaborativeServicePtr_->UpdateCollaborativeStateReal(), SUCCESS);
192 EXPECT_EQ(audioCollaborativeServicePtr_->GetRealCollaborativeState(), true);
193 }
194 } // AudioStandard
195 } // OHOS