• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include "audio_info.h"
18 #include "audio_adapter.h"
19 #include "avsession_log.h"
20 #include "avsession_errors.h"
21 #include "focus_session_strategy.h"
22 
23 using namespace OHOS;
24 using namespace OHOS::AVSession;
25 using OHOS::AudioStandard::AudioRendererChangeInfo;
26 using OHOS::AudioStandard::RendererState;
27 
28 static std::string g_errLog;
29 
MyLogCallback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)30 static void MyLogCallback(const LogType type, const LogLevel level,
31     const unsigned int domain, const char *tag, const char *msg)
32 {
33     g_errLog = msg;
34 }
35 
36 class FocusSessionStrategyTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     void SetUp();
41     void TearDown();
42 };
43 
SetUpTestCase()44 void FocusSessionStrategyTest::SetUpTestCase()
45 {}
46 
TearDownTestCase()47 void FocusSessionStrategyTest::TearDownTestCase()
48 {}
49 
SetUp()50 void FocusSessionStrategyTest::SetUp()
51 {}
52 
TearDown()53 void FocusSessionStrategyTest::TearDown()
54 {}
55 
56 /**
57  * @tc.name: HandleAudioRenderStateChangeEvent001
58  * @tc.desc: Test IsCommandSendEnable
59  * @tc.type: FUNC
60  */
61 static HWTEST_F(FocusSessionStrategyTest, HandleAudioRenderStateChangeEvent001, testing::ext::TestSize.Level0)
62 {
63     SLOGD("HandleAudioRenderStateChangeEvent001 begin!");
64     LOG_SetCallback(MyLogCallback);
65     FocusSessionStrategy focusSessionStrategy;
66     std::shared_ptr<AudioRendererChangeInfo> info = std::make_shared<AudioRendererChangeInfo>();
67     info->clientUID = 1;
68     info->sessionId = 2;
69     info->rendererState = RendererState::RENDERER_RELEASED;
70     AudioRendererChangeInfos infos;
71     infos.push_back(std::move(info));
72 
73     focusSessionStrategy.HandleAudioRenderStateChangeEvent(infos);
74     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
75     SLOGD("HandleAudioRenderStateChangeEvent001 end!");
76 }
77 
78 /**
79  * @tc.name: HandleAudioRenderStateChangeEvent002
80  * @tc.desc: Test IsCommandSendEnable
81  * @tc.type: FUNC
82  */
83 static HWTEST_F(FocusSessionStrategyTest, HandleAudioRenderStateChangeEvent002, testing::ext::TestSize.Level0)
84 {
85     SLOGD("HandleAudioRenderStateChangeEvent002 begin!");
86     LOG_SetCallback(MyLogCallback);
87     FocusSessionStrategy focusSessionStrategy;
88     std::shared_ptr<AudioRendererChangeInfo> info = std::make_shared<AudioRendererChangeInfo>();
89     info->clientUID = 1;
90     info->sessionId = 2;
91     info->rendererState = RendererState::RENDERER_RELEASED;
92     AudioRendererChangeInfos infos;
93     infos.push_back(std::move(info));
94 
__anond168c3540102(const FocusSessionStrategy::FocusSessionChangeInfo&, bool) 95     auto func = [](const FocusSessionStrategy::FocusSessionChangeInfo&, bool) {};
96     focusSessionStrategy.RegisterFocusSessionChangeCallback(func);
97     focusSessionStrategy.HandleAudioRenderStateChangeEvent(infos);
98     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
99     SLOGD("HandleAudioRenderStateChangeEvent002 end!");
100 }
101 
102 /**
103  * @tc.name: HandleAudioRenderStateChangeEvent003
104  * @tc.desc: Test IsCommandSendEnable
105  * @tc.type: FUNC
106  */
107 static HWTEST_F(FocusSessionStrategyTest, HandleAudioRenderStateChangeEvent003, testing::ext::TestSize.Level0)
108 {
109     SLOGD("HandleAudioRenderStateChangeEvent003 begin!");
110     LOG_SetCallback(MyLogCallback);
111     FocusSessionStrategy focusSessionStrategy;
112     std::shared_ptr<AudioRendererChangeInfo> info = std::make_shared<AudioRendererChangeInfo>();
113     info->clientUID = 1;
114     info->sessionId = 2;
115     info->rendererState = RendererState::RENDERER_RUNNING;
116     AudioRendererChangeInfos infos;
117     infos.push_back(std::move(info));
118 
119     focusSessionStrategy.HandleAudioRenderStateChangeEvent(infos);
120     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
121     SLOGD("HandleAudioRenderStateChangeEvent003 end!");
122 }
123 
124 /**
125  * @tc.name: HandleAudioRenderStateChangeEvent004
126  * @tc.desc: Test IsCommandSendEnable
127  * @tc.type: FUNC
128  */
129 static HWTEST_F(FocusSessionStrategyTest, HandleAudioRenderStateChangeEvent004, testing::ext::TestSize.Level0)
130 {
131     SLOGD("HandleAudioRenderStateChangeEvent004 begin!");
132     LOG_SetCallback(MyLogCallback);
133     FocusSessionStrategy focusSessionStrategy;
134     std::shared_ptr<AudioRendererChangeInfo> info = std::make_shared<AudioRendererChangeInfo>();
135     info->clientUID = 1;
136     info->sessionId = 2;
137     info->rendererState = RendererState::RENDERER_RUNNING;
138     AudioRendererChangeInfos infos;
139     infos.push_back(std::move(info));
140 
__anond168c3540202(const FocusSessionStrategy::FocusSessionChangeInfo&, bool) 141     auto func = [](const FocusSessionStrategy::FocusSessionChangeInfo&, bool) {};
142     focusSessionStrategy.RegisterFocusSessionChangeCallback(func);
143     focusSessionStrategy.HandleAudioRenderStateChangeEvent(infos);
144     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
145     SLOGD("HandleAudioRenderStateChangeEvent004 end!");
146 }
147 
148 /**
149  * @tc.name: HandleAudioRenderStateChangeEvent005
150  * @tc.desc: Test IsCommandSendEnable
151  * @tc.type: FUNC
152  */
153 static HWTEST_F(FocusSessionStrategyTest, HandleAudioRenderStateChangeEvent005, testing::ext::TestSize.Level0)
154 {
155     SLOGD("HandleAudioRenderStateChangeEvent005 begin!");
156     LOG_SetCallback(MyLogCallback);
157     FocusSessionStrategy focusSessionStrategy;
158     std::shared_ptr<AudioRendererChangeInfo> info1 = std::make_shared<AudioRendererChangeInfo>();
159     info1->clientUID = 1;
160     info1->sessionId = 2;
161     info1->rendererState = RendererState::RENDERER_RELEASED;
162 
163     std::shared_ptr<AudioRendererChangeInfo> info2 = std::make_shared<AudioRendererChangeInfo>();
164     info2->clientUID = 3;
165     info2->sessionId = 4;
166     info2->rendererState = RendererState::RENDERER_RUNNING;
167 
168     AudioRendererChangeInfos infos;
169     infos.push_back(std::move(info1));
170     infos.push_back(std::move(info2));
171 
__anond168c3540302(const FocusSessionStrategy::FocusSessionChangeInfo&, bool) 172     auto func = [](const FocusSessionStrategy::FocusSessionChangeInfo&, bool) {};
173     focusSessionStrategy.RegisterFocusSessionChangeCallback(func);
174     focusSessionStrategy.HandleAudioRenderStateChangeEvent(infos);
175     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
176     SLOGD("HandleAudioRenderStateChangeEvent005 end!");
177 }
178 
179 /**
180  * @tc.name: IsFocusSession001
181  * @tc.desc: Test IsFocusSession
182  * @tc.type: FUNC
183  */
184 static HWTEST_F(FocusSessionStrategyTest, IsFocusSession001, testing::ext::TestSize.Level0)
185 {
186     SLOGD("IsFocusSession001 begin!");
187     std::shared_ptr<AudioRendererChangeInfo> info = std::make_shared<AudioRendererChangeInfo>();
188     info->clientUID = 1;
189     info->sessionId = 2;
190     info->rendererState = RendererState::RENDERER_RELEASED;
191     AudioRendererChangeInfos infos;
192     infos.push_back(std::move(info));
193     FocusSessionStrategy focusSessionStrategy;
__anond168c3540402(const OHOS::AVSession::FocusSessionStrategy::FocusSessionChangeInfo&) 194     auto func = [](const OHOS::AVSession::FocusSessionStrategy::FocusSessionChangeInfo&) {
195         return false;
196     };
197     focusSessionStrategy.RegisterFocusSessionSelector(func);
198     AudioRendererChangeInfo audioRendererChangeInfo;
199     audioRendererChangeInfo.clientUID = 1;
200     audioRendererChangeInfo.clientPid = 1;
201     std::pair<int32_t, int32_t> key = std::make_pair(audioRendererChangeInfo.clientUID,
202         audioRendererChangeInfo.clientPid);
203     audioRendererChangeInfo.sessionId = 2;
204     audioRendererChangeInfo.rendererState = RendererState::RENDERER_RELEASED;
205     bool ret = focusSessionStrategy.IsFocusSession(key);
206     EXPECT_EQ(ret, true);
207     SLOGD("IsFocusSession001 end!");
208 }
209 
210 /**
211  * @tc.name: IsFocusSession002
212  * @tc.desc: Test IsFocusSession
213  * @tc.type: FUNC
214  */
215 static HWTEST_F(FocusSessionStrategyTest, IsFocusSession002, testing::ext::TestSize.Level0)
216 {
217     SLOGD("IsFocusSession002 begin!");
218 
219     std::shared_ptr<AudioRendererChangeInfo> info = std::make_shared<AudioRendererChangeInfo>();
220     info->clientUID = 1;
221     info->sessionId = 2;
222     info->rendererState = RendererState::RENDERER_RELEASED;
223     AudioRendererChangeInfos infos;
224     infos.push_back(std::move(info));
225     FocusSessionStrategy focusSessionStrategy;
__anond168c3540502(const OHOS::AVSession::FocusSessionStrategy::FocusSessionChangeInfo&) 226     auto func = [](const OHOS::AVSession::FocusSessionStrategy::FocusSessionChangeInfo&) {
227         return true;
228     };
229     focusSessionStrategy.RegisterFocusSessionSelector(func);
230     AudioRendererChangeInfo audioRendererChangeInfo;
231     audioRendererChangeInfo.clientUID = 1;
232     audioRendererChangeInfo.clientPid = 1;
233     std::pair<int32_t, int32_t> key = std::make_pair(audioRendererChangeInfo.clientUID,
234         audioRendererChangeInfo.clientPid);
235     audioRendererChangeInfo.sessionId = 2;
236     audioRendererChangeInfo.rendererState = RendererState::RENDERER_RUNNING;
237     bool ret = focusSessionStrategy.IsFocusSession(key);
238     EXPECT_EQ(ret, true);
239     SLOGD("IsFocusSession002 end!");
240 }
241 
242 /**
243 * @tc.name: HandleAudioRenderStateChangeEvent006
244 * @tc.desc: Test HandleAudioRenderStateChangeEvent
245 * @tc.type: FUNC
246 */
247 static HWTEST_F(FocusSessionStrategyTest, HandleAudioRenderStateChangeEvent006, testing::ext::TestSize.Level0)
248 {
249     SLOGD("HandleAudioRenderStateChangeEvent006 begin!");
250     LOG_SetCallback(MyLogCallback);
251     FocusSessionStrategy focusSessionStrategy;
252     std::shared_ptr<AudioRendererChangeInfo> info1 = std::make_shared<AudioRendererChangeInfo>();
253     info1->clientUID = 1;
254     info1->sessionId = 2;
255     info1->rendererState = RendererState::RENDERER_RELEASED;
256 
257     std::shared_ptr<AudioRendererChangeInfo> info2 = std::make_shared<AudioRendererChangeInfo>();
258     info2->clientUID = 3;
259     info2->sessionId = 4;
260     info2->rendererState = RendererState::RENDERER_RUNNING;
261 
262     AudioRendererChangeInfos infos;
263     infos.push_back(std::move(info1));
264     infos.push_back(std::move(info2));
265 
266     focusSessionStrategy.currentStates_.insert({std::make_pair(1, 1), 1});
267 
__anond168c3540602(const FocusSessionStrategy::FocusSessionChangeInfo&, bool) 268     auto func = [](const FocusSessionStrategy::FocusSessionChangeInfo&, bool) {};
269     focusSessionStrategy.RegisterFocusSessionChangeCallback(func);
270     focusSessionStrategy.HandleAudioRenderStateChangeEvent(infos);
271     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
272     SLOGD("HandleAudioRenderStateChangeEvent006 end!");
273 }
274 
275 /**
276 * @tc.name: UpdateFocusSession001
277 * @tc.desc: Test UpdateFocusSession
278 * @tc.type: FUNC
279 */
280 static HWTEST_F(FocusSessionStrategyTest, UpdateFocusSession001, testing::ext::TestSize.Level0)
281 {
282     SLOGD("UpdateFocusSession001 begin!");
283     LOG_SetCallback(MyLogCallback);
284     FocusSessionStrategy focusSessionStrategy;
__anond168c3540702(const OHOS::AVSession::FocusSessionStrategy::FocusSessionChangeInfo&) 285     auto func = [](const OHOS::AVSession::FocusSessionStrategy::FocusSessionChangeInfo&) {
286         return false;
287     };
288     focusSessionStrategy.RegisterFocusSessionSelector(func);
289     std::pair<int32_t, int32_t> key = std::make_pair(1, 1);
290     focusSessionStrategy.UpdateFocusSession(key);
291     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
292     SLOGD("UpdateFocusSession001 end!");
293 }
294 
295 /**
296 * @tc.name: IsFocusSession003
297 * @tc.desc: Test IsFocusSession
298 * @tc.type: FUNC
299 */
300 static HWTEST_F(FocusSessionStrategyTest, IsFocusSession003, testing::ext::TestSize.Level0)
301 {
302     SLOGD("IsFocusSession003 begin!");
303     FocusSessionStrategy focusSessionStrategy;
304     focusSessionStrategy.lastStates_.insert({std::make_pair(1, 1), 2});
305     std::pair<int32_t, int32_t> key = std::make_pair(1, 1);
306     bool ret = focusSessionStrategy.IsFocusSession(key);
307     EXPECT_EQ(ret, false);
308     SLOGD("IsFocusSession003 end!");
309 }
310 
311 /**
312 * @tc.name: CheckFocusSessionStop001
313 * @tc.desc: Test CheckFocusSessionStop
314 * @tc.type: FUNC
315 */
316 static HWTEST_F(FocusSessionStrategyTest, CheckFocusSessionStop001, testing::ext::TestSize.Level0)
317 {
318     SLOGD("CheckFocusSessionStop001 begin!");
319     FocusSessionStrategy focusSessionStrategy;
320     focusSessionStrategy.lastStates_.insert({std::make_pair(1, 1), 2});
321     std::pair<int32_t, int32_t> key = std::make_pair(1, 1);
322     bool ret = focusSessionStrategy.CheckFocusSessionStop(key);
323     EXPECT_EQ(ret, true);
324     SLOGD("CheckFocusSessionStop001 end!");
325 }
326 
327 /**
328 * @tc.name: DelayStopFocusSession001
329 * @tc.desc: Test DelayStopFocusSession
330 * @tc.type: FUNC
331 */
332 static HWTEST_F(FocusSessionStrategyTest, DelayStopFocusSession001, testing::ext::TestSize.Level0)
333 {
334     SLOGD("DelayStopFocusSession001 begin!");
335     FocusSessionStrategy focusSessionStrategy;
336     focusSessionStrategy.lastStates_.insert({std::make_pair(1, 1), 1});
337     std::pair<int32_t, int32_t> key = std::make_pair(1, 1);
338     focusSessionStrategy.DelayStopFocusSession(key);
339     sleep(6);
340     SLOGD("DelayStopFocusSession001 end!");
341 }
342 
343 /**
344 * @tc.name: DelayStopFocusSession002
345 * @tc.desc: Test DelayStopFocusSession
346 * @tc.type: FUNC
347 */
348 static HWTEST_F(FocusSessionStrategyTest, DelayStopFocusSession002, testing::ext::TestSize.Level0)
349 {
350     SLOGD("DelayStopFocusSession002 begin!");
351     FocusSessionStrategy focusSessionStrategy;
__anond168c3540802(const FocusSessionStrategy::FocusSessionChangeInfo&, bool) 352     auto func = [](const FocusSessionStrategy::FocusSessionChangeInfo&, bool) {};
353     focusSessionStrategy.RegisterFocusSessionChangeCallback(func);
354     focusSessionStrategy.lastStates_.insert({std::make_pair(1, 1), 2});
355     std::pair<int32_t, int32_t> key = std::make_pair(1, 1);
356     focusSessionStrategy.DelayStopFocusSession(key);
357     sleep(6);
358     SLOGD("DelayStopFocusSession002 end!");
359 }
360 
361 /**
362 * @tc.name: DelayStopFocusSession003
363 * @tc.desc: Test DelayStopFocusSession
364 * @tc.type: FUNC
365 */
366 static HWTEST_F(FocusSessionStrategyTest, DelayStopFocusSession003, testing::ext::TestSize.Level0)
367 {
368     SLOGD("DelayStopFocusSession003 begin!");
369     FocusSessionStrategy focusSessionStrategy;
__anond168c3540902(const FocusSessionStrategy::FocusSessionChangeInfo&, bool) 370     auto func = [](const FocusSessionStrategy::FocusSessionChangeInfo&, bool) {};
371     focusSessionStrategy.RegisterFocusSessionChangeCallback(func);
372     focusSessionStrategy.lastStates_.insert({std::make_pair(1, 1), 1});
373     std::pair<int32_t, int32_t> key = std::make_pair(1, 1);
374     focusSessionStrategy.DelayStopFocusSession(key);
375     sleep(6);
376     SLOGD("DelayStopFocusSession003 end!");
377 }
378 
379 /**
380 * @tc.name: ProcAudioRenderChange001
381 * @tc.desc: Test ProcAudioRenderChange
382 * @tc.type: FUNC
383 */
384 static HWTEST_F(FocusSessionStrategyTest, ProcAudioRenderChange001, testing::ext::TestSize.Level0)
385 {
386     SLOGI("ProcAudioRenderChange001 begin!");
387     std::shared_ptr<AudioRendererChangeInfo> infoA = std::make_shared<AudioRendererChangeInfo>();
388     infoA->clientUID = 1;
389     infoA->sessionId = 1;
390     infoA->rendererState = RendererState::RENDERER_RUNNING;
391     infoA->rendererInfo.streamUsage = AudioStandard::STREAM_USAGE_MUSIC;
392 
393     std::shared_ptr<AudioRendererChangeInfo> infoB = std::make_shared<AudioRendererChangeInfo>();
394     infoB->clientUID = 1;
395     infoB->sessionId = 2;
396     infoB->rendererState = RendererState::RENDERER_RUNNING;
397     infoB->rendererInfo.streamUsage = AudioStandard::STREAM_USAGE_MUSIC;
398 
399     AudioRendererChangeInfos infos;
400     infos.push_back(std::move(infoA));
401     infos.push_back(std::move(infoB));
402     FocusSessionStrategy focusSessionStrategy;
403     focusSessionStrategy.audioPlayingUids_.clear();
404     focusSessionStrategy.ProcAudioRenderChange(infos);
405     SLOGE("ProcAudioRenderChange001 num:%{public}d aft ProcAudioRenderChange",
406         static_cast<int>(focusSessionStrategy.audioPlayingUids_.size()));
407     EXPECT_TRUE(focusSessionStrategy.audioPlayingUids_.size() == 1);
408     SLOGI("ProcAudioRenderChange001 end!");
409 }
410 
411 /**
412 * @tc.name: ProcAudioRenderChange002
413 * @tc.desc: Test ProcAudioRenderChange
414 * @tc.type: FUNC
415 */
416 static HWTEST_F(FocusSessionStrategyTest, ProcAudioRenderChange002, testing::ext::TestSize.Level0)
417 {
418     SLOGI("ProcAudioRenderChange002 begin!");
419     std::shared_ptr<AudioRendererChangeInfo> infoA = std::make_shared<AudioRendererChangeInfo>();
420     infoA->clientUID = 2;
421     infoA->sessionId = 1;
422     infoA->rendererState = RendererState::RENDERER_RELEASED;
423     infoA->rendererInfo.streamUsage = AudioStandard::STREAM_USAGE_MUSIC;
424 
425     std::shared_ptr<AudioRendererChangeInfo> infoB = std::make_shared<AudioRendererChangeInfo>();
426     infoB->clientUID = 2;
427     infoB->sessionId = 2;
428     infoB->rendererState = RendererState::RENDERER_RUNNING;
429     infoB->rendererInfo.streamUsage = AudioStandard::STREAM_USAGE_GAME;
430 
431     AudioRendererChangeInfos infos;
432     infos.push_back(std::move(infoA));
433     infos.push_back(std::move(infoB));
434     FocusSessionStrategy focusSessionStrategy;
435     focusSessionStrategy.audioPlayingUids_.clear();
436     focusSessionStrategy.ProcAudioRenderChange(infos);
437     SLOGE("ProcAudioRenderChange002 num:%{public}d aft ProcAudioRenderChange",
438         static_cast<int>(focusSessionStrategy.audioPlayingUids_.size()));
439     EXPECT_TRUE(focusSessionStrategy.audioPlayingUids_.size() == 0);
440     SLOGI("ProcAudioRenderChange002 end!");
441 }