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
16 #include "audio_tone_manager_test.h"
17 #include <thread>
18 #include <string>
19 #include <memory>
20 #include <vector>
21 #include <sys/socket.h>
22 #include <cerrno>
23 #include <fstream>
24 #include <algorithm>
25 #include <unistd.h>
26 #include "audio_policy_log.h"
27 #include "audio_errors.h"
28 using namespace std;
29 using namespace std::chrono;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace AudioStandard {
34
SetUpTestCase(void)35 void AudioToneManagerUnitTest::SetUpTestCase(void) {}
TearDownTestCase(void)36 void AudioToneManagerUnitTest::TearDownTestCase(void) {}
SetUp(void)37 void AudioToneManagerUnitTest::SetUp(void) {}
TearDown(void)38 void AudioToneManagerUnitTest::TearDown(void) {}
39
40
41 #define PRINT_LINE printf("debug __LINE__:%d\n", __LINE__)
42
43 #ifdef FEATURE_DTMF_TONE
44
45 /**
46 * @tc.name : Test LoadToneDtmfConfig
47 * @tc.number: LoadToneDtmfConfig_Fail_001
48 * @tc.desc : Test LoadToneDtmfConfig return false when the profile content is illegal.
49 */
50 HWTEST_F(AudioToneManagerUnitTest, LoadToneDtmfConfig_LoadFail_001, TestSize.Level4)
51 {
52 AudioToneManager audioToneManager;
53
54 std::ofstream file("/system/etc/audio/audio_tone_dtmf_config.xml");
55 file << "<INVALID></INVALID>";
56 file.close();
57
58 bool ret = audioToneManager.LoadToneDtmfConfig();
59 EXPECT_EQ(ret, false);
60 }
61
62 /**
63 * @tc.name : Test LoadToneDtmfConfig
64 * @tc.number: LoadToneDtmfConfig_Success_002
65 * @tc.desc : Test LoadToneDtmfConfig return true when the profile content is legal.
66 */
67 HWTEST_F(AudioToneManagerUnitTest, LoadToneDtmfConfig_Success_002, TestSize.Level4)
68 {
69 std::string configPath = "/system/etc/audio/audio_tone_dtmf_config.xml";
70 std::ofstream file(configPath);
71 ASSERT_TRUE(file.is_open());
72
73 file << "<DTMF>"
74 << "<Default>"
75 << "<Tone name=\"tone1\" freq=\"440\" duration=\"100\" />"
76 << "</Default>"
77 << "</DTMF>";
78 file.close();
79
80 AudioToneManager manager;
81 bool result = manager.LoadToneDtmfConfig();
82 EXPECT_EQ(result, true);
83 }
84
85 /**
86 * @tc.name : Test GetSupportedTones
87 * @tc.number: GetSupportedTones_Empty_001
88 * @tc.desc : Test return empty list when both the default and custom tone are empty.
89 */
90 HWTEST_F(AudioToneManagerUnitTest, GetSupportedTones_Empty_001, TestSize.Level4)
91 {
92 AudioToneManager manager;
93
94 auto result = manager.GetSupportedTones("CN");
95 EXPECT_TRUE(result.empty());
96 }
97
98 /**
99 * @tc.name : Test GetSupportedTones
100 * @tc.number: GetSupportedTones_CustomFound_002
101 * @tc.desc : Test customToneDescriptorMap_ branch, return custom and default collection.
102 */
103 HWTEST_F(AudioToneManagerUnitTest, GetSupportedTones_CustomFound_002, TestSize.Level4)
104 {
105 AudioToneManager manager;
106
107 manager.toneDescriptorMap_[100] = std::make_shared<ToneInfo>();
108 manager.toneDescriptorMap_[200] = std::make_shared<ToneInfo>();
109
110 std::string country = "CN";
111 manager.customToneDescriptorMap_[country][200] = std::make_shared<ToneInfo>();
112 manager.customToneDescriptorMap_[country][300] = std::make_shared<ToneInfo>();
113
114 auto result = manager.GetSupportedTones(country);
115
116 std::set<int32_t> resultSet(result.begin(), result.end());
117 EXPECT_EQ(resultSet.size(), 3);
118 EXPECT_TRUE(resultSet.count(100));
119 EXPECT_TRUE(resultSet.count(200));
120 EXPECT_TRUE(resultSet.count(300));
121 }
122
123 /**
124 * @tc.name : Test GetSupportedTones
125 * @tc.number: GetSupportedTones_CustomNotFound_003
126 * @tc.desc : Test Input country code not in custom list, return default tone.
127 */
128 HWTEST_F(AudioToneManagerUnitTest, GetSupportedTones_CustomNotFound_002, TestSize.Level4)
129 {
130 AudioToneManager manager;
131
132 manager.toneDescriptorMap_[100] = std::make_shared<ToneInfo>();
133 manager.toneDescriptorMap_[200] = std::make_shared<ToneInfo>();
134
135 manager.customToneDescriptorMap_["US"][300] = std::make_shared<ToneInfo>();
136
137 auto result = manager.GetSupportedTones("FR");
138
139 std::set<int32_t> resultSet(result.begin(), result.end());
140 EXPECT_EQ(resultSet.size(), 2);
141 EXPECT_TRUE(resultSet.count(100));
142 EXPECT_TRUE(resultSet.count(200));
143 EXPECT_FALSE(resultSet.count(300));
144 }
145
146 /**
147 * @tc.name : Test GetToneConfig
148 * @tc.number : GetToneConfig_NotFound_001
149 * @tc.desc : Test return nullptr when toneType not found in either customToneDescriptorMap_ or toneDescriptorMap_.
150 */
151 HWTEST_F(AudioToneManagerUnitTest, GetToneConfig_NotFound_001, TestSize.Level4)
152 {
153 AudioToneManager manager;
154 std::string country = "FR";
155 int32_t toneType = 999;
156
157 auto result = manager.GetToneConfig(toneType, country);
158 EXPECT_EQ(result, nullptr);
159 }
160
161 /**
162 * @tc.name : Test GetToneConfig
163 * @tc.number : GetToneConfig_CustomHit_002
164 * @tc.desc : Test return custom tone config when both country code and toneType exist in customToneDescriptorMap_.
165 */
166 HWTEST_F(AudioToneManagerUnitTest, GetToneConfig_CustomHit_002, TestSize.Level4)
167 {
168 AudioToneManager manager;
169 std::string country = "CN";
170 int32_t toneType = 100;
171
172 auto toneInfo = std::make_shared<ToneInfo>();
173 manager.customToneDescriptorMap_[country][toneType] = toneInfo;
174
175 auto result = manager.GetToneConfig(toneType, country);
176 EXPECT_EQ(result, toneInfo);
177 }
178
179 /**
180 * @tc.name : Test GetToneConfig
181 * @tc.number : GetToneConfig_CustomMissDefaultHit_003
182 * @tc.desc : Test return default when country code exists in customToneDescriptorMap_ but toneType does not.
183 */
184 HWTEST_F(AudioToneManagerUnitTest, GetToneConfig_CustomMissDefaultHit_003, TestSize.Level4)
185 {
186 AudioToneManager manager;
187 std::string country = "CN";
188 int32_t toneType = 200;
189
190 manager.customToneDescriptorMap_[country][999] = std::make_shared<ToneInfo>();
191 auto defaultTone = std::make_shared<ToneInfo>();
192 manager.toneDescriptorMap_[toneType] = defaultTone;
193
194 auto result = manager.GetToneConfig(toneType, country);
195 EXPECT_EQ(result, defaultTone);
196 }
197
198 /**
199 * @tc.name : Test GetToneConfig
200 * @tc.number : GetToneConfig_DefaultOnly_004
201 * @tc.desc : Test return default tone config when customToneDescriptorMap_ does not contain the country code.
202 */
203 HWTEST_F(AudioToneManagerUnitTest, GetToneConfig_DefaultOnly_004, TestSize.Level4)
204 {
205 AudioToneManager manager;
206 std::string country = "US";
207 int32_t toneType = 300;
208
209 auto defaultTone = std::make_shared<ToneInfo>();
210 manager.toneDescriptorMap_[toneType] = defaultTone;
211
212 auto result = manager.GetToneConfig(toneType, country);
213 EXPECT_EQ(result, defaultTone);
214 }
215
216 #endif
217 }
218 }
219