• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 
18 #include "audio_service_log.h"
19 #include "audio_errors.h"
20 #include "audio_session_manager.h"
21 
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 
27 class AudioSessionManagerUnitTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 };
34 
35 /**
36  * @tc.name  : Test ActivateAudioSession API
37  * @tc.type  : FUNC
38  * @tc.number: ActivateAudioSession_001
39  * @tc.desc  : Test ActivateAudioSession interface.
40  */
41 HWTEST(AudioSessionManagerUnitTest, ActivateAudioSession_001, TestSize.Level1)
42 {
43     AudioSessionStrategy strategy;
44     strategy.concurrencyMode = AudioConcurrencyMode::DEFAULT;
45 
46     int32_t result = AudioSessionManager::GetInstance()->ActivateAudioSession(strategy);
47     EXPECT_NE(result, SUCCESS);
48 
49     result = AudioSessionManager::GetInstance()->SetDefaultOutputDevice(DEVICE_TYPE_INVALID);
50     EXPECT_NE(result, ERROR_INVALID_PARAM);
51 
52     result = AudioSessionManager::GetInstance()->ActivateAudioSession(strategy);
53     EXPECT_NE(result, SUCCESS);
54 }
55 
56 /**
57  * @tc.name  : Test UnsetAudioSessionStateChangeCallback API
58  * @tc.type  : FUNC
59  * @tc.number: UnsetAudioSessionStateChangeCallback_001
60  * @tc.desc  : Test UnsetAudioSessionStateChangeCallback interface.
61  */
62 HWTEST(AudioSessionManagerUnitTest, UnsetAudioSessionStateChangeCallback_001, TestSize.Level1)
63 {
64     int32_t result = AudioSessionManager::GetInstance()->UnsetAudioSessionStateChangeCallback();
65     EXPECT_NE(result, SUCCESS);
66 }
67 
68 /**
69  * @tc.name  : Test UnsetAudioSessionCurrentDeviceChangeCallback API
70  * @tc.type  : FUNC
71  * @tc.number: UnsetAudioSessionCurrentDeviceChangeCallback_001
72  * @tc.desc  : Test UnsetAudioSessionCurrentDeviceChangeCallback interface.
73  */
74 HWTEST(AudioSessionManagerUnitTest, UnsetAudioSessionCurrentDeviceChangeCallback_001, TestSize.Level1)
75 {
76     int32_t result = AudioSessionManager::GetInstance()->UnsetAudioSessionCurrentDeviceChangeCallback();
77     EXPECT_NE(result, SUCCESS);
78 }
79 
80 /**
81  * @tc.name  : Test AudioSessionRestoreParame class
82  * @tc.type  : FUNC
83  * @tc.number: AudioSessionRestoreParame_001
84  * @tc.desc  : Test AudioSessionRestoreParame class interface.
85  */
86 HWTEST(AudioSessionManagerUnitTest, AudioSessionRestoreParame_001, TestSize.Level1)
87 {
88     AudioSessionRestoreParame restoreParame_;
89     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_ACTIVATE, 0);
90     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_ACTIVATE, 1);
91     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_SET_SCENE, 1);
92     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_SET_SCENE, 0);
93     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_ACTIVATE, 1);
94     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_ACTIVATE, 1);
95     EXPECT_EQ(restoreParame_.actions_.size(), 2);
96     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_SET_SCENE, 1);
97     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_SET_SCENE, 1);
98     EXPECT_EQ(restoreParame_.actions_.size(), 3);
99     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_ACTIVATE, 0);
100     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_ACTIVATE, 0);
101     EXPECT_EQ(restoreParame_.actions_.size(), 2);
102 
103     restoreParame_.OnAudioSessionStateChanged(AudioSessionStateChangeHint::INVALID);
104     restoreParame_.OnAudioSessionStateChanged(AudioSessionStateChangeHint::STOP);
105     restoreParame_.OnAudioSessionStateChanged(AudioSessionStateChangeHint::TIME_OUT_STOP);
106     restoreParame_.OnAudioSessionStateChanged(AudioSessionStateChangeHint::PAUSE);
107     EXPECT_EQ(restoreParame_.actions_.size(), 0);
108 
109     restoreParame_.RecordAudioSessionOpt(AudioSessionRestoreParame::OperationType::AUDIO_SESSION_ACTIVATE, 0);
110     restoreParame_.OnAudioSessionDeactive();
111     EXPECT_EQ(restoreParame_.actions_.size(), 0);
112 }
113 
114 /**
115  * @tc.name  : Test OnAudioPolicyServiceDied api
116  * @tc.type  : FUNC
117  * @tc.number: AudioSessionManagerServiceDiedRestore_001
118  * @tc.desc  : Test OnAudioPolicyServiceDied interface.
119  */
120 HWTEST(AudioSessionManagerUnitTest, AudioSessionManagerServiceDiedRestore_001, TestSize.Level1)
121 {
122     AudioSessionManagerServiceDiedRestore restore;
123 
124     AudioSessionManager::GetInstance()->restoreParame_.actions_.clear();
125     AudioSessionManager::GetInstance()->setDefaultOutputDevice_ = false;
126     restore.OnAudioPolicyServiceDied();
127 
128     AudioSessionManager::GetInstance()->setDefaultOutputDevice_ = true;
129     AudioSessionManager::GetInstance()->setDeviceType_ = DEVICE_TYPE_DEFAULT;
130     AudioSessionManager::GetInstance()->restoreParame_.RecordAudioSessionOpt(
131         AudioSessionRestoreParame::OperationType::AUDIO_SESSION_ACTIVATE, 0);
132     restore.OnAudioPolicyServiceDied();
133     EXPECT_EQ(AudioSessionManager::GetInstance()->restoreParame_.actions_.size(), 1);
134 }
135 
136 } // namespace AudioStandard
137 } // namespace OHOS