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 <memory>
18 #include "background_audio_controller.h"
19 #include "avsession_log.h"
20 #include "audio_info.h"
21 #include "running_process_info.h"
22 #include "app_manager_adapter.h"
23 #include "bundle_mgr_client.h"
24 #include "avsession_descriptor.h"
25 #include "system_ability_definition.h"
26 #include "avsession_service.h"
27
28 using namespace testing::ext;
29 using namespace OHOS::AVSession;
30
31 class BkGrAudioControllerTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
SetUpTestCase()39 void BkGrAudioControllerTest::SetUpTestCase()
40 {}
41
TearDownTestCase()42 void BkGrAudioControllerTest::TearDownTestCase()
43 {}
44
SetUp()45 void BkGrAudioControllerTest::SetUp()
46 {}
47
TearDown()48 void BkGrAudioControllerTest::TearDown()
49 {}
50
51 /**
52 * @tc.name: OnSessionRelease001
53 * @tc.desc: descriptor.isThirdPartyApp_ = false
54 * @tc.type: FUNC
55 * @tc.require: #I62OZV
56 */
57 static HWTEST(BkGrAudioControllerTest, OnSessionRelease001, TestSize.Level0)
58 {
59 SLOGI("OnSessionRelease001 begin!");
60 AVSessionDescriptor descriptor;
61 descriptor.isThirdPartyApp_ = false;
62 BackgroundAudioController bkgraudiocontroller;
63 bkgraudiocontroller.OnSessionRelease(descriptor);
64 EXPECT_EQ(bkgraudiocontroller.ptr_, nullptr);
65 SLOGI("OnSessionRelease001 end!");
66 }
67
68 /**
69 * @tc.name: OnSessionRelease002
70 * @tc.desc: descriptor.isThirdPartyApp_ = true&&descriptor.uid_ = -1
71 * @tc.type: FUNC
72 * @tc.require: #I62OZV
73 */
74 static HWTEST(BkGrAudioControllerTest, OnSessionRelease002, TestSize.Level0)
75 {
76 SLOGI("OnSessionRelease002 begin!");
77 AVSessionDescriptor descriptor;
78 descriptor.isThirdPartyApp_ = true;
79 descriptor.uid_ = -1;
80 BackgroundAudioController bkgraudiocontroller;
81 bkgraudiocontroller.OnSessionRelease(descriptor);
82 EXPECT_EQ(bkgraudiocontroller.ptr_, nullptr);
83 SLOGI("OnSessionRelease002 end!");
84 }
85
86 /**
87 * @tc.name: OnSessionRelease003
88 * @tc.desc: descriptor.isThirdPartyApp_ = true&&descriptor.uid_ = 100
89 * @tc.type: FUNC
90 * @tc.require: #I62OZV
91 */
92 static HWTEST(BkGrAudioControllerTest, OnSessionRelease003, TestSize.Level0)
93 {
94 SLOGI("OnSessionRelease003 begin!");
95 AVSessionDescriptor descriptor;
96 descriptor.isThirdPartyApp_ = true;
97 descriptor.uid_ = 100;
98 BackgroundAudioController bkgraudiocontroller;
99 bkgraudiocontroller.OnSessionRelease(descriptor);
100 EXPECT_EQ(bkgraudiocontroller.ptr_, nullptr);
101 SLOGI("OnSessionRelease032 end!");
102 }
103
104 /**
105 * @tc.name: HandleAudioStreamRendererStateChange001
106 * @tc.desc: info->rendererState != AudioStandard::RENDERER_RUNNING
107 * @tc.type: FUNC
108 * @tc.require: #I62OZV
109 */
110 static HWTEST(BkGrAudioControllerTest, HandleAudioStreamRendererStateChange001, TestSize.Level0)
111 {
112 SLOGI("HandleAudioStreamRendererStateChange001 begin!");
113 std::shared_ptr<OHOS::AudioStandard::AudioRendererChangeInfo> info =
114 std::make_shared<OHOS::AudioStandard::AudioRendererChangeInfo>();
115 std::vector<std::shared_ptr<OHOS::AudioStandard::AudioRendererChangeInfo>> audioRendererChangeInfos;
116 info->rendererState = OHOS::AudioStandard::RENDERER_NEW;
117 audioRendererChangeInfos.push_back(move(info));
118 BackgroundAudioController backgroundaudiocontroller;
119 backgroundaudiocontroller.HandleAudioStreamRendererStateChange(audioRendererChangeInfos);
120 EXPECT_EQ(audioRendererChangeInfos[0]->rendererState, OHOS::AudioStandard::RENDERER_NEW);
121 }
122
123 /**
124 * @tc.name: HandleAudioStreamRendererStateChange002
125 * @tc.desc: !AppManagerAdapter::GetInstance().IsAppBackground(info->clientUID)
126 * @tc.type: FUNC
127 * @tc.require: #I62OZV
128 */
129 static HWTEST(BkGrAudioControllerTest, HandleAudioStreamRendererStateChange002, TestSize.Level0)
130 {
131 SLOGI("HandleAudioStreamRendererStateChange002 begin!");
132 std::shared_ptr<OHOS::AudioStandard::AudioRendererChangeInfo> info =
133 std::make_shared<OHOS::AudioStandard::AudioRendererChangeInfo>();
134 std::vector<std::shared_ptr<OHOS::AudioStandard::AudioRendererChangeInfo>> audioRendererChangeInfos;
135 info->rendererState = OHOS::AudioStandard::RENDERER_RUNNING;
136 info->clientUID = -1;
137 info->clientPid = -1;
138 audioRendererChangeInfos.push_back(move(info));
139 BackgroundAudioController backgroundaudiocontroller;
140 backgroundaudiocontroller.HandleAudioStreamRendererStateChange(audioRendererChangeInfos);
141 EXPECT_EQ(audioRendererChangeInfos[0]->clientUID, -1);
142 }
143
144 /**
145 * @tc.name: HandleAppMuteState001
146 * @tc.desc: int32_t uid = -1;
147 * @tc.type: FUNC
148 * @tc.require: #I62OZV
149 */
150 static HWTEST(BkGrAudioControllerTest, HandleAppMuteState001, TestSize.Level0)
151 {
152 SLOGI("HandleAppMuteState001 begin!");
153 int32_t uid = -1;
154 int32_t pid = -1;
155 BackgroundAudioController backgroundaudiocontroller;
156 backgroundaudiocontroller.HandleAppMuteState(uid, pid, true);
157 EXPECT_EQ(backgroundaudiocontroller.ptr_, nullptr);
158 }
159
160 /**
161 * @tc.name: HandleAppMuteState002
162 * @tc.desc: int32_t uid = -1;
163 * @tc.type: FUNC
164 * @tc.require: #I62OZV
165 */
166 static HWTEST(BkGrAudioControllerTest, HandleAppMuteState002, TestSize.Level0)
167 {
168 SLOGI("HandleAppMuteState001 begin!");
169 int32_t uid = -1;
170 int32_t pid = -1;
171 BackgroundAudioController backgroundaudiocontroller;
172 backgroundaudiocontroller.HandleAppMuteState(uid, pid, false);
173 EXPECT_EQ(backgroundaudiocontroller.ptr_, nullptr);
174 }
175
176 /**
177 * @tc.name: IsBackgroundMode001
178 * @tc.desc: test IsBackgroundMode
179 * @tc.type: FUNC
180 * @tc.require: #I62OZV
181 */
182 static HWTEST(BkGrAudioControllerTest, IsBackgroundMode001, TestSize.Level0)
183 {
184 SLOGI("IsBackgroundMode001 begin!");
185 int32_t creatorUid = 1;
186 OHOS::AppExecFwk::BackgroundMode backgroundMode = OHOS::AppExecFwk::BackgroundMode::DATA_TRANSFER;
187 BackgroundAudioController backgroundaudiocontroller;
188 bool ret = backgroundaudiocontroller.IsBackgroundMode(creatorUid, backgroundMode);
189 EXPECT_EQ(ret, false);
190 }
191
192 /**
193 * @tc.name: HasAVSession001
194 * @tc.desc: test HasAVSession
195 * @tc.type: FUNC
196 * @tc.require: #I62OZV
197 */
198 static HWTEST(BkGrAudioControllerTest, HasAVSession001, TestSize.Level0)
199 {
200 SLOGI("HasAVSession001 begin!");
201 int32_t uid = 1000;
202 BackgroundAudioController backgroundaudiocontroller;
203 bool ret = backgroundaudiocontroller.HasAVSession(uid);
204 EXPECT_EQ(ret, false);
205 }
206
207 /**
208 * @tc.name: HasAVSession002
209 * @tc.desc: test HasAVSession
210 * @tc.type: FUNC
211 * @tc.require: #I62OZV
212 */
213 static HWTEST(BkGrAudioControllerTest, HasAVSession002, TestSize.Level0)
214 {
215 SLOGI("HasAVSession002 begin!");
216 int32_t uid = 1000;
217 int32_t pid = 1000;
218 BackgroundAudioController backgroundaudiocontroller;
219 std::set<int32_t> pidSet;
220 pidSet.insert(pid);
221 backgroundaudiocontroller.sessionUIDs_.insert(std::make_pair(uid, pidSet));
222 bool ret = backgroundaudiocontroller.HasAVSession(uid);
223 EXPECT_EQ(ret, true);
224 }
225
226 /**
227 * @tc.name: OnSessionRelease004
228 * @tc.desc: sessionUIDs_ only contain descriptor.uid_
229 * @tc.type: FUNC
230 * @tc.require: #I62OZV
231 */
232 static HWTEST(BkGrAudioControllerTest, OnSessionRelease004, TestSize.Level0)
233 {
234 SLOGI("OnSessionRelease004 begin!");
235 AVSessionDescriptor descriptor;
236 descriptor.isThirdPartyApp_ = true;
237 descriptor.uid_ = 100;
238 BackgroundAudioController bkgraudiocontroller;
239 std::map<int32_t, std::set<int32_t>> sessionUID;
240 sessionUID.insert({descriptor.uid_, {descriptor.uid_}});
241 bkgraudiocontroller.sessionUIDs_ = sessionUID;
242 bkgraudiocontroller.OnSessionRelease(descriptor);
243 EXPECT_TRUE(!bkgraudiocontroller.sessionUIDs_.empty());
244 SLOGI("OnSessionRelease04 end!");
245 }
246
247 /**
248 * @tc.name: OnSessionRelease005
249 * @tc.desc: sessionUIDs_ not only contain descriptor.uid_
250 * @tc.type: FUNC
251 * @tc.require: #I62OZV
252 */
253 static HWTEST(BkGrAudioControllerTest, OnSessionRelease005, TestSize.Level0)
254 {
255 SLOGI("OnSessionRelease005 begin!");
256 AVSessionDescriptor descriptor;
257 descriptor.isThirdPartyApp_ = true;
258 descriptor.uid_ = 100;
259 BackgroundAudioController bkgraudiocontroller;
260 std::map<int32_t, std::set<int32_t>> sessionUID;
261 sessionUID.insert({descriptor.uid_, {descriptor.uid_, 0, 1, 2}});
262 bkgraudiocontroller.sessionUIDs_ = sessionUID;
263 bkgraudiocontroller.OnSessionRelease(descriptor);
264 EXPECT_TRUE(!bkgraudiocontroller.sessionUIDs_.empty());
265 SLOGI("OnSessionRelease05 end!");
266 }
267
268 /**
269 * @tc.name: OnSessionCreate001
270 * @tc.desc: test OnSessionCreate
271 * @tc.type: FUNC
272 * @tc.require: #I62OZV
273 */
274 static HWTEST(BkGrAudioControllerTest, OnSessionCreate001, TestSize.Level0)
275 {
276 SLOGI("OnSessionCreate001 begin!");
277 AVSessionDescriptor descriptor;
278 BackgroundAudioController bkgraudiocontroller;
279 bkgraudiocontroller.OnSessionCreate(descriptor);
280 auto it = bkgraudiocontroller.sessionUIDs_.find(descriptor.uid_);
281 EXPECT_NE(it, bkgraudiocontroller.sessionUIDs_.end());
282 SLOGI("OnSessionCreate001 end!");
283 }
284
285 /**
286 * @tc.name: OnSessionCreate002
287 * @tc.desc: test OnSessionCreate
288 * @tc.type: FUNC
289 * @tc.require: #I62OZV
290 */
291 static HWTEST(BkGrAudioControllerTest, OnSessionCreate002, TestSize.Level0)
292 {
293 SLOGI("OnSessionCreate002 begin!");
294 AVSessionDescriptor descriptor;
295 BackgroundAudioController bkgraudiocontroller;
296 bkgraudiocontroller.sessionUIDs_.insert(std::make_pair(descriptor.uid_, std::set<int32_t>()));
297 bkgraudiocontroller.OnSessionCreate(descriptor);
298 auto it = bkgraudiocontroller.sessionUIDs_.find(descriptor.uid_);
299 EXPECT_NE(it, bkgraudiocontroller.sessionUIDs_.end());
300 SLOGI("OnSessionCreate002 end!");
301 }
302
303 /**
304 * @tc.name: OnRemoteDistributedSessionChange001
305 * @tc.desc: test OnRemoteDistributedSessionChange
306 * @tc.type: FUNC
307 * @tc.require: #I62OZV
308 */
309 static HWTEST(BkGrAudioControllerTest, OnRemoteDistributedSessionChange001, TestSize.Level1)
310 {
311 SLOGI("OnRemoteDistributedSessionChange001 begin!");
312 DeviceLogEventCode eventCode = DeviceLogEventCode::DEVICE_LOG_FULL;
313 const int64_t param = 1000;
314 std::string deviceId = "test";
315 std::vector<OHOS::sptr<IRemoteObject>> sessionControllers;
316 BackgroundAudioController backgroundaudiocontroller;
317 backgroundaudiocontroller.OnDeviceLogEvent(eventCode, param);
318 int32_t uid = 1000;
319 int32_t pid = 1000;
320 std::set<int32_t> pidSet;
321 pidSet.insert(pid);
322 backgroundaudiocontroller.OnDeviceOffline(deviceId);
323 backgroundaudiocontroller.sessionUIDs_.insert(std::make_pair(uid, pidSet));
324 backgroundaudiocontroller.OnAudioSessionChecked(uid);
325 bool ret = backgroundaudiocontroller.HasAVSession(uid);
326 EXPECT_EQ(ret, true);
327 sessionControllers.push_back(nullptr);
328 backgroundaudiocontroller.OnRemoteDistributedSessionChange(sessionControllers);
329 SLOGI("OnRemoteDistributedSessionChange001 end!");
330 }
331
332 /**
333 * @tc.name: IsBackgroundMode002
334 * @tc.desc: test IsBackgroundMode
335 * @tc.type: FUNC
336 * @tc.require: #I62OZV
337 */
338 static HWTEST(BkGrAudioControllerTest, IsBackgroundMode002, TestSize.Level1)
339 {
340 SLOGI("IsBackgroundMode002 begin!");
341 int32_t creatorUid = 1;
342 OutputDeviceInfo castOutputDeviceInfo;
343 OHOS::AppExecFwk::BackgroundMode backgroundMode = OHOS::AppExecFwk::BackgroundMode::DATA_TRANSFER;
344 BackgroundAudioController backgroundaudiocontroller;
345 backgroundaudiocontroller.OnDeviceAvailable(castOutputDeviceInfo);
346 bool ret = backgroundaudiocontroller.IsBackgroundMode(creatorUid, backgroundMode);
347 EXPECT_EQ(ret, false);
348 SLOGI("IsBackgroundMode002 end!");
349 }
350