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
16 #include <sys/stat.h>
17
18 #include "audio_errors.h"
19 #include "audio_info.h"
20 #include "audio_system_manager.h"
21 #include "hilog/log.h"
22
23 #include "audio_volume_change_unit_test.h"
24
25 using OHOS::HiviewDFX::HiLog;
26 using OHOS::HiviewDFX::HiLogLabel;
27 using namespace std;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace AudioStandard {
32 namespace {
33 constexpr HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "VolumeChangeGtest"};
34 AudioSystemManager *g_audioManagerInstance = nullptr;
35
36 int32_t g_streamType(0);
37 int32_t g_volumeLevel(0);
38 bool g_isUpdateUi(false);
39 bool g_isCallbackReceived(false);
40
41 std::string g_callbackName("");
42 std::mutex g_mutex;
43 std::condition_variable g_condVar;
44 } // namespace
45
ApplicationCallback(const std::string & testCaseName)46 ApplicationCallback::ApplicationCallback(const std::string &testCaseName) : testCaseName_(testCaseName) {}
47
OnVolumeKeyEvent(AudioStreamType streamType,int32_t volumeLevel,bool isUpdateUi)48 void ApplicationCallback::OnVolumeKeyEvent(AudioStreamType streamType, int32_t volumeLevel, bool isUpdateUi)
49 {
50 g_isCallbackReceived = true;
51 g_streamType = streamType;
52 g_volumeLevel = volumeLevel;
53 g_callbackName = testCaseName_;
54 g_isUpdateUi = isUpdateUi;
55 g_condVar.notify_all();
56 }
57
WaitForCallback()58 void AudioVolumeChangeUnitTest::WaitForCallback()
59 {
60 std::unique_lock<std::mutex> lock(g_mutex);
61 g_condVar.wait_until(lock, std::chrono::system_clock::now() + std::chrono::minutes(1),
62 []() { return g_isCallbackReceived == true; });
63 }
64
SetUpTestCase(void)65 void AudioVolumeChangeUnitTest::SetUpTestCase(void)
66 {
67 g_audioManagerInstance = AudioSystemManager::GetInstance();
68 if (g_audioManagerInstance == nullptr) {
69 HiLog::Error(LABEL, "AudioSystemManager instance not available");
70 return;
71 }
72 }
73
TearDownTestCase(void)74 void AudioVolumeChangeUnitTest::TearDownTestCase(void)
75 {
76 g_audioManagerInstance = nullptr;
77 }
78
79 // SetUp:Execute before each test case
SetUp()80 void AudioVolumeChangeUnitTest::SetUp() {}
81
TearDown(void)82 void AudioVolumeChangeUnitTest::TearDown(void) {}
83
84 /*
85 * Feature: AudioVolumeChangeUnitTest
86 * Function: Set volume for AudioStreamType::STREAM_MUSIC
87 * SubFunction: NA
88 * FunctionPoints: NA
89 * EnvConditions: NA
90 * CaseDescription:
91 */
92 HWTEST_F(AudioVolumeChangeUnitTest, volumeChange_test_001, TestSize.Level1)
93 {
94 int result;
95 int callBackSetResult;
96 std::string testCaseName("volumeChange_test_001");
97 g_isCallbackReceived = false;
98 AudioStreamType streamType = AudioStreamType::STREAM_MUSIC;
99 AudioSystemManager::AudioVolumeType volumeType
100 = static_cast<AudioSystemManager::AudioVolumeType>(streamType);
101 int volume = 10;
102 g_callbackName = testCaseName;
103 bool isUpdateUi = false;
104 auto appCallback = make_shared<ApplicationCallback>(testCaseName);
105 callBackSetResult = g_audioManagerInstance->RegisterVolumeKeyEventCallback(getpid(), appCallback);
106 result = g_audioManagerInstance->SetVolume(volumeType, volume);
107 EXPECT_EQ(result, SUCCESS);
108 EXPECT_EQ(callBackSetResult, SUCCESS);
109 if (result == SUCCESS) {
110 // Wait here for callback. If not callback for 2 mintues, will skip this step
111 AudioVolumeChangeUnitTest::WaitForCallback();
112 EXPECT_EQ(streamType, g_streamType);
113 EXPECT_EQ(volume, g_volumeLevel);
114 EXPECT_EQ(isUpdateUi, g_isUpdateUi);
115 EXPECT_STREQ(g_callbackName.c_str(), testCaseName.c_str());
116 }
117 g_audioManagerInstance->UnregisterVolumeKeyEventCallback(getpid());
118 }
119
120 /*
121 * Feature: AudioVolumeChangeUnitTest
122 * Function: Set volume for AudioStreamType::STREAM_RING
123 * SubFunction: NA
124 * FunctionPoints: NA
125 * EnvConditions: NA
126 * CaseDescription:
127 */
128 HWTEST_F(AudioVolumeChangeUnitTest, volumeChange_test_002, TestSize.Level1)
129 {
130 int result;
131 int callBackSetResult;
132 std::string testCaseName("volumeChange_test_002");
133 g_isCallbackReceived = false;
134 AudioStreamType streamType = AudioStreamType::STREAM_RING;
135 AudioSystemManager::AudioVolumeType volumeType
136 = static_cast<AudioSystemManager::AudioVolumeType>(streamType);
137 int volume = 10;
138 g_callbackName = testCaseName;
139 bool isUpdateUi = false;
140 auto appCallback = make_shared<ApplicationCallback>(testCaseName);
141 callBackSetResult = g_audioManagerInstance->RegisterVolumeKeyEventCallback(getpid(), appCallback);
142 result = g_audioManagerInstance->SetVolume(volumeType, volume);
143 EXPECT_EQ(result, SUCCESS);
144 EXPECT_EQ(callBackSetResult, SUCCESS);
145 if (result == SUCCESS) {
146 // Wait here for callback. If not callback for 2 mintues, will skip this step
147 AudioVolumeChangeUnitTest::WaitForCallback();
148 EXPECT_EQ(streamType, g_streamType);
149 EXPECT_EQ(volume, g_volumeLevel);
150 EXPECT_EQ(isUpdateUi, g_isUpdateUi);
151 EXPECT_STREQ(g_callbackName.c_str(), testCaseName.c_str());
152 }
153 g_audioManagerInstance->UnregisterVolumeKeyEventCallback(getpid());
154 }
155
156 /*
157 * Feature: AudioVolumeChangeUnitTest
158 * Function: Set volume for AudioStreamType::STREAM_VOICE_CALL
159 * SubFunction: NA
160 * FunctionPoints: NA
161 * EnvConditions: NA
162 * CaseDescription:
163 */
164 HWTEST_F(AudioVolumeChangeUnitTest, volumeChange_test_003, TestSize.Level1)
165 {
166 int result;
167 int callBackSetResult;
168 std::string testCaseName("volumeChange_test_003");
169 g_isCallbackReceived = false;
170 AudioStreamType streamType = AudioStreamType::STREAM_VOICE_CALL;
171 AudioSystemManager::AudioVolumeType volumeType
172 = static_cast<AudioSystemManager::AudioVolumeType>(streamType);
173 int volume = 10;
174 g_callbackName = testCaseName;
175 bool isUpdateUi = false;
176 auto appCallback = make_shared<ApplicationCallback>(testCaseName);
177 callBackSetResult = g_audioManagerInstance->RegisterVolumeKeyEventCallback(getpid(), appCallback);
178 result = g_audioManagerInstance->SetVolume(volumeType, volume);
179 EXPECT_EQ(result, SUCCESS);
180 EXPECT_EQ(callBackSetResult, SUCCESS);
181 if (result == SUCCESS) {
182 // Wait here for callback. If not callback for 2 mintues, will skip this step
183 AudioVolumeChangeUnitTest::WaitForCallback();
184 EXPECT_EQ(streamType, g_streamType);
185 EXPECT_EQ(volume, g_volumeLevel);
186 EXPECT_EQ(isUpdateUi, g_isUpdateUi);
187 EXPECT_STREQ(g_callbackName.c_str(), testCaseName.c_str());
188 }
189 g_audioManagerInstance->UnregisterVolumeKeyEventCallback(getpid());
190 }
191 } // namespace AudioStandard
192 } // namespace OHOS
193