1 /*
2 * Copyright (c) 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 <gtest/gtest.h>
17 #include "background_audio_controller.h"
18 #include "avsession_log.h"
19 #include "audio_info.h"
20 #include "running_process_info.h"
21 #include "app_manager_adapter.h"
22 #include "bundle_mgr_client.h"
23 #include "avsession_descriptor.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::AVSession;
27
28 class BkGrAudioControllerTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase()36 void BkGrAudioControllerTest::SetUpTestCase()
37 {}
38
TearDownTestCase()39 void BkGrAudioControllerTest::TearDownTestCase()
40 {}
41
SetUp()42 void BkGrAudioControllerTest::SetUp()
43 {}
44
TearDown()45 void BkGrAudioControllerTest::TearDown()
46 {}
47
48 /**
49 * @tc.name: OnSessionRelease001
50 * @tc.desc: descriptor.isThirdPartyApp_ = false
51 * @tc.type: FUNC
52 * @tc.require: #I62OZV
53 */
HWTEST(BkGrAudioControllerTest, OnSessionRelease001, TestSize.Level1)54 static HWTEST(BkGrAudioControllerTest, OnSessionRelease001, TestSize.Level1)
55 {
56 SLOGI("OnSessionRelease001 begin!");
57 AVSessionDescriptor descriptor;
58 descriptor.isThirdPartyApp_ = false;
59 BackgroundAudioController bkgraudiocontroller;
60 bkgraudiocontroller.OnSessionRelease(descriptor);
61 EXPECT_EQ(descriptor.isThirdPartyApp_, false);
62 SLOGI("OnSessionRelease001 end!");
63 }
64
65 /**
66 * @tc.name: OnSessionRelease002
67 * @tc.desc: descriptor.isThirdPartyApp_ = true&&descriptor.uid_ = -1
68 * @tc.type: FUNC
69 * @tc.require: #I62OZV
70 */
HWTEST(BkGrAudioControllerTest, OnSessionRelease002, TestSize.Level1)71 static HWTEST(BkGrAudioControllerTest, OnSessionRelease002, TestSize.Level1)
72 {
73 SLOGI("OnSessionRelease002 begin!");
74 AVSessionDescriptor descriptor;
75 descriptor.isThirdPartyApp_ = true;
76 descriptor.uid_ = -1;
77 BackgroundAudioController bkgraudiocontroller;
78 bkgraudiocontroller.OnSessionRelease(descriptor);
79 EXPECT_EQ(descriptor.isThirdPartyApp_, true);
80 SLOGI("OnSessionRelease002 end!");
81 }
82
83 /**
84 * @tc.name: OnSessionRelease003
85 * @tc.desc: descriptor.isThirdPartyApp_ = true&&descriptor.uid_ = 100
86 * @tc.type: FUNC
87 * @tc.require: #I62OZV
88 */
HWTEST(BkGrAudioControllerTest, OnSessionRelease003, TestSize.Level1)89 static HWTEST(BkGrAudioControllerTest, OnSessionRelease003, TestSize.Level1)
90 {
91 SLOGI("OnSessionRelease003 begin!");
92 AVSessionDescriptor descriptor;
93 descriptor.isThirdPartyApp_ = true;
94 descriptor.uid_ = 100;
95 BackgroundAudioController bkgraudiocontroller;
96 bkgraudiocontroller.OnSessionRelease(descriptor);
97 EXPECT_EQ(descriptor.isThirdPartyApp_, true);
98 SLOGI("OnSessionRelease032 end!");
99 }
100
101 /**
102 * @tc.name: HandleAudioStreamRendererStateChange001
103 * @tc.desc: info->rendererState != AudioStandard::RENDERER_RUNNING
104 * @tc.type: FUNC
105 * @tc.require: #I62OZV
106 */
HWTEST(BkGrAudioControllerTest, HandleAudioStreamRendererStateChange001, TestSize.Level1)107 static HWTEST(BkGrAudioControllerTest, HandleAudioStreamRendererStateChange001, TestSize.Level1)
108 {
109 SLOGI("HandleAudioStreamRendererStateChange001 begin!");
110 std::shared_ptr<OHOS::AudioStandard::AudioRendererChangeInfo> info =
111 std::make_shared<OHOS::AudioStandard::AudioRendererChangeInfo>();
112 std::vector<std::shared_ptr<OHOS::AudioStandard::AudioRendererChangeInfo>> audioRendererChangeInfos;
113 info->rendererState = OHOS::AudioStandard::RENDERER_NEW;
114 audioRendererChangeInfos.push_back(move(info));
115 BackgroundAudioController backgroundaudiocontroller;
116 backgroundaudiocontroller.HandleAudioStreamRendererStateChange(audioRendererChangeInfos);
117 EXPECT_EQ(audioRendererChangeInfos[0]->rendererState, OHOS::AudioStandard::RENDERER_NEW);
118 }
119
120 /**
121 * @tc.name: HandleAudioStreamRendererStateChange002
122 * @tc.desc: !AppManagerAdapter::GetInstance().IsAppBackground(info->clientUID)
123 * @tc.type: FUNC
124 * @tc.require: #I62OZV
125 */
HWTEST(BkGrAudioControllerTest, HandleAudioStreamRendererStateChange002, TestSize.Level1)126 static HWTEST(BkGrAudioControllerTest, HandleAudioStreamRendererStateChange002, TestSize.Level1)
127 {
128 SLOGI("HandleAudioStreamRendererStateChange002 begin!");
129 std::shared_ptr<OHOS::AudioStandard::AudioRendererChangeInfo> info =
130 std::make_shared<OHOS::AudioStandard::AudioRendererChangeInfo>();
131 std::vector<std::shared_ptr<OHOS::AudioStandard::AudioRendererChangeInfo>> audioRendererChangeInfos;
132 info->rendererState = OHOS::AudioStandard::RENDERER_RUNNING;
133 info->clientUID = -1;
134 info->clientPid = -1;
135 audioRendererChangeInfos.push_back(move(info));
136 BackgroundAudioController backgroundaudiocontroller;
137 backgroundaudiocontroller.HandleAudioStreamRendererStateChange(audioRendererChangeInfos);
138 EXPECT_EQ(audioRendererChangeInfos[0]->clientUID, -1);
139 }
140
141 /**
142 * @tc.name: HandleAppMuteState001
143 * @tc.desc: int32_t uid = -1;
144 * @tc.type: FUNC
145 * @tc.require: #I62OZV
146 */
HWTEST(BkGrAudioControllerTest, HandleAppMuteState001, TestSize.Level1)147 static HWTEST(BkGrAudioControllerTest, HandleAppMuteState001, TestSize.Level1)
148 {
149 SLOGI("HandleAppMuteState001 begin!");
150 int32_t uid = -1;
151 int32_t pid = -1;
152 BackgroundAudioController backgroundaudiocontroller;
153 backgroundaudiocontroller.HandleAppMuteState(uid, pid, true);
154 EXPECT_EQ(uid, -1);
155 }
156
157 /**
158 * @tc.name: HandleAppMuteState002
159 * @tc.desc: int32_t uid = -1;
160 * @tc.type: FUNC
161 * @tc.require: #I62OZV
162 */
HWTEST(BkGrAudioControllerTest, HandleAppMuteState002, TestSize.Level1)163 static HWTEST(BkGrAudioControllerTest, HandleAppMuteState002, TestSize.Level1)
164 {
165 SLOGI("HandleAppMuteState001 begin!");
166 int32_t uid = -1;
167 int32_t pid = -1;
168 BackgroundAudioController backgroundaudiocontroller;
169 backgroundaudiocontroller.HandleAppMuteState(uid, pid, false);
170 EXPECT_EQ(uid, -1);
171 }
172
173 /**
174 * @tc.name: IsBackgroundMode001
175 * @tc.desc: test IsBackgroundMode
176 * @tc.type: FUNC
177 * @tc.require: #I62OZV
178 */
HWTEST(BkGrAudioControllerTest, IsBackgroundMode001, TestSize.Level1)179 static HWTEST(BkGrAudioControllerTest, IsBackgroundMode001, TestSize.Level1)
180 {
181 SLOGI("IsBackgroundMode001 begin!");
182 int32_t creatorUid = 1;
183 OHOS::AppExecFwk::BackgroundMode backgroundMode = OHOS::AppExecFwk::BackgroundMode::DATA_TRANSFER;
184 BackgroundAudioController backgroundaudiocontroller;
185 bool ret = backgroundaudiocontroller.IsBackgroundMode(creatorUid, backgroundMode);
186 EXPECT_EQ(ret, false);
187 }
188
189 /**
190 * @tc.name: HasAVSession001
191 * @tc.desc: test HasAVSession
192 * @tc.type: FUNC
193 * @tc.require: #I62OZV
194 */
HWTEST(BkGrAudioControllerTest, HasAVSession001, TestSize.Level1)195 static HWTEST(BkGrAudioControllerTest, HasAVSession001, TestSize.Level1)
196 {
197 SLOGI("HasAVSession001 begin!");
198 int32_t uid = 1000;
199 BackgroundAudioController backgroundaudiocontroller;
200 bool ret = backgroundaudiocontroller.HasAVSession(uid);
201 EXPECT_EQ(ret, false);
202 }
203
204 /**
205 * @tc.name: HasAVSession002
206 * @tc.desc: test HasAVSession
207 * @tc.type: FUNC
208 * @tc.require: #I62OZV
209 */
HWTEST(BkGrAudioControllerTest, HasAVSession002, TestSize.Level1)210 static HWTEST(BkGrAudioControllerTest, HasAVSession002, TestSize.Level1)
211 {
212 SLOGI("HasAVSession002 begin!");
213 int32_t uid = 1000;
214 BackgroundAudioController backgroundaudiocontroller;
215 backgroundaudiocontroller.sessionUIDs_.insert(uid);
216 bool ret = backgroundaudiocontroller.HasAVSession(uid);
217 EXPECT_EQ(ret, true);
218 }