1 /*
2 * Copyright (c) 2023 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 #define private public
19 #include "avsession_dumper.h"
20 #undef private
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace AVSession {
26 class AVSessionDumperTest : public testing::Test {
27 public:
28 std::unique_ptr<AVSessionService> avSessionService_ = nullptr;
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp() override;
32 void TearDown() override;
33 };
34
SetUpTestCase()35 void AVSessionDumperTest::SetUpTestCase()
36 {}
37
TearDownTestCase()38 void AVSessionDumperTest::TearDownTestCase()
39 {}
40
SetUp()41 void AVSessionDumperTest::SetUp()
42 {
43 avSessionService_ = std::make_unique<AVSessionService>(1, true);
44 }
45
TearDown()46 void AVSessionDumperTest::TearDown()
47 {}
48
49 /**
50 * @tc.name: ShowHelp001
51 * @tc.desc: Test whether the string returned by showHelp is correct
52 * @tc.type: FUNC
53 * @tc.require: I6RW8M
54 */
55 HWTEST_F(AVSessionDumperTest, ShowHelp001, TestSize.Level1)
56 {
57 SLOGI("ShowHelp001 begin");
58 std::string argsHelp = "-h";
59 std::string expectedString;
60 std::string actualString;
61 expectedString.append("Usage:dump <command> [options]\n")
62 .append("Description:\n")
63 .append("-show_metadata :show all avsession metadata in the system\n")
64 .append("-show_session_info :show information of all sessions\n")
65 .append("-show_controller_info :show information of all controllers \n")
66 .append("-show_error_info :show error information about avsession\n")
67 .append("-show_trusted_devices_Info :show trusted devices Info\n");
68 std::vector<std::string> args;
69 args.push_back(argsHelp);
70 AVSessionDumper dumper;
71 dumper.Dump(args, actualString, *avSessionService_);
72 EXPECT_EQ(actualString, expectedString);
73 SLOGI("ShowHelp001 end");
74 }
75
76 /**
77 * @tc.name: ShowTrustedDevicesInfo001
78 * @tc.desc: Test whether the string returned by ShowTrustedDevicesInfo is correct
79 * @tc.type: FUNC
80 * @tc.require: I6RW8M
81 */
82 HWTEST_F(AVSessionDumperTest, ShowTrustedDevicesInfo001, TestSize.Level1)
83 {
84 SLOGI("ShowTrustedDevicesInfo001 begin");
85 std::string trustedDeviceInfo = "-show_trusted_devices_Info";
86 std::string actualString;
87 std::vector<std::string> args;
88 args.push_back(trustedDeviceInfo);
89 AVSessionDumper dumper;
90 dumper.Dump(args, actualString, *avSessionService_);
91 EXPECT_EQ(actualString.size() > 0, true);
92 SLOGI("ShowTrustedDevicesInfo001 end");
93 }
94
95 /**
96 * @tc.name: ShowSessionInfo001
97 * @tc.desc: Test whether the string returned by ShowSessionInfo is correct
98 * @tc.type: FUNC
99 * @tc.require: I6RW8M
100 */
101 HWTEST_F(AVSessionDumperTest, ShowSessionInfo001, TestSize.Level1)
102 {
103 SLOGI("ShowSessionInfo001 begin");
104 std::string showSessionInfo = "-show_session_info";
105 std::string actualString;
106 std::vector<std::string> args;
107 args.push_back(showSessionInfo);
108 AVSessionDumper dumper;
109 dumper.Dump(args, actualString, *avSessionService_);
110 EXPECT_EQ(actualString.size() > 0, true);
111 SLOGI("ShowSessionInfo001 end");
112 }
113
114 /**
115 * @tc.name: ShowControllerInfo001
116 * @tc.desc: Test whether the string returned by ShowControllerInfo is correct
117 * @tc.type: FUNC
118 * @tc.require: I6RW8M
119 */
120 HWTEST_F(AVSessionDumperTest, ShowControllerInfo, TestSize.Level1)
121 {
122 SLOGI("ShowControllerInfo begin");
123 std::string showControllerInfo = "-show_controller_info";
124 std::string actualString;
125 std::vector<std::string> args;
126 args.push_back(showControllerInfo);
127 AVSessionDumper dumper;
128 dumper.Dump(args, actualString, *avSessionService_);
129 EXPECT_EQ(actualString.size() > 0, true);
130 SLOGI("ShowControllerInfo end");
131 }
132
133 /**
134 * @tc.name: ShowErrorInfo001
135 * @tc.desc: Test whether errorInfo can be obtained
136 * @tc.type: FUNC
137 * @tc.require: I6RW8M
138 */
139 HWTEST_F(AVSessionDumperTest, ShowErrorInfo001, TestSize.Level1)
140 {
141 SLOGI("ShowErrorInfo001 begin");
142 std::string showErrorInfo = "-show_error_info";
143 std::string inputString = "This is error info";
144 std::string outputString;
145 AVSessionDumper dumper;
146 dumper.SetErrorInfo(inputString);
147 std::vector<std::string> args;
148 args.push_back(showErrorInfo);
149 dumper.Dump(args, outputString, *avSessionService_);
150 EXPECT_EQ(outputString.size() > inputString.size(), true);
151 SLOGI("ShowErrorInfo001 end");
152 }
153
154 /**
155 * @tc.name: ShowIllegalInfo001
156 * @tc.desc: Show illegal info
157 * @tc.type: FUNC
158 * @tc.require: I6RW8M
159 */
160 HWTEST_F(AVSessionDumperTest, ShowIllegalInfo001, TestSize.Level1)
161 {
162 SLOGI("ShowIllegalInfo001 begin");
163 std::string illegalInformation = "AVSession service, enter '-h' for usage.\n";
164 std::string illegalArg = "illegalArg";
165 std::string actualString;
166 std::vector<std::string> args;
167 args.push_back(illegalArg);
168 AVSessionDumper dumper;
169 dumper.Dump(args, actualString, *avSessionService_);
170 EXPECT_EQ(actualString, illegalInformation);
171 SLOGI("ShowIllegalInfo001 end");
172 }
173
174 /**
175 * @tc.name: Dump001
176 * @tc.desc: Test dump function in unexpected situations
177 * @tc.type: FUNC
178 * @tc.require: I6RW8M
179 */
180 HWTEST_F(AVSessionDumperTest, Dump001, TestSize.Level1)
181 {
182 SLOGI("Dump001 begin");
183 std::string illegalInformation = "AVSession service, enter '-h' for usage.\n";
184 std::string actualString;
185 std::vector<std::string> args;
186 AVSessionDumper dumper;
187 dumper.Dump(args, actualString, *avSessionService_);
188 EXPECT_EQ(actualString, illegalInformation);
189 SLOGI("Dump001 end");
190 }
191
192 /**
193 * @tc.name: OnDump001
194 * @tc.desc: avsession service ondump
195 * @tc.type: FUNC
196 * @tc.require:
197 */
198 HWTEST_F(AVSessionDumperTest, OnDump001, TestSize.Level1)
199 {
200 avSessionService_->OnDump();
201 }
202
203 /**
204 * @tc.name: OnStop001
205 * @tc.desc: avsession service onstop
206 * @tc.type: FUNC
207 * @tc.require:
208 */
209 HWTEST_F(AVSessionDumperTest, OnStop001, TestSize.Level1)
210 {
211 avSessionService_->OnStop();
212 }
213
214 /**
215 * @tc.name: UpdataTopSession001
216 * @tc.desc:
217 * @tc.type: FUNC
218 * @tc.require:
219 */
220 HWTEST_F(AVSessionDumperTest, UpdataTopSession001, TestSize.Level1)
221 {
222 AVSessionDescriptor descriptor;
223 avSessionService_->topSession_ = new AVSessionItem(descriptor);
224 auto item = new AVSessionItem(descriptor);
225 avSessionService_->UpdateTopSession(item);
226 }
227
228 /**
229 * @tc.name: HandleFocusSession001
230 * @tc.desc:
231 * @tc.type: FUNC
232 * @tc.require:
233 */
234 HWTEST_F(AVSessionDumperTest, HandleFocusSession001, TestSize.Level1)
235 {
236 AVSessionDescriptor descriptor;
237 descriptor.uid_ = 1;
238 avSessionService_->topSession_ = new AVSessionItem(descriptor);
239 FocusSessionStrategy::FocusSessionChangeInfo info;
240 info.uid = 1;
241 avSessionService_->HandleFocusSession(info);
242 }
243
244 /**
245 * @tc.name: HandleFocusSession002
246 * @tc.desc:
247 * @tc.type: FUNC
248 * @tc.require:
249 */
250 HWTEST_F(AVSessionDumperTest, HandleFocusSession002, TestSize.Level1)
251 {
252 AVSessionDescriptor descriptor;
253 descriptor.uid_ = 1;
254 avSessionService_->topSession_ = new AVSessionItem(descriptor);
255 descriptor.uid_ = 2;
256 sptr<AVSessionItem> item = new AVSessionItem(descriptor);
257 FocusSessionStrategy::FocusSessionChangeInfo info;
258 info.uid = 2;
259 avSessionService_->GetContainer().AddSession(1, "abilityName", item);
260 avSessionService_->HandleFocusSession(info);
261 avSessionService_->GetContainer().RemoveSession(1);
262 }
263
264 class TestSessionListener : public SessionListener {
265 public:
OnSessionCreate(const AVSessionDescriptor & descriptor)266 void OnSessionCreate(const AVSessionDescriptor& descriptor) override
267 {
268 SLOGI("sessionId=%{public}s created", descriptor.sessionId_.c_str());
269 }
270
OnSessionRelease(const AVSessionDescriptor & descriptor)271 void OnSessionRelease(const AVSessionDescriptor& descriptor) override
272 {
273 SLOGI("sessionId=%{public}s released", descriptor.sessionId_.c_str());
274 }
275
OnTopSessionChange(const AVSessionDescriptor & descriptor)276 void OnTopSessionChange(const AVSessionDescriptor& descriptor) override
277 {
278 SLOGI("sessionId=%{public}s be top session", descriptor.sessionId_.c_str());
279 }
280
OnAudioSessionChecked(const int32_t uid)281 void OnAudioSessionChecked(const int32_t uid) override
282 {
283 SLOGI("uid=%{public}d checked", uid);
284 }
285 };
286
287 class TestISessionListener : public ISessionListener {
288 public:
OnSessionCreate(const AVSessionDescriptor & descriptor)289 void OnSessionCreate(const AVSessionDescriptor& descriptor) override
290 {
291 };
292
OnSessionRelease(const AVSessionDescriptor & descriptor)293 void OnSessionRelease(const AVSessionDescriptor& descriptor) override
294 {
295 };
296
OnTopSessionChange(const AVSessionDescriptor & descriptor)297 void OnTopSessionChange(const AVSessionDescriptor& descriptor) override
298 {
299 };
300
OnAudioSessionChecked(const int32_t uid)301 void OnAudioSessionChecked(const int32_t uid) override
302 {
303 };
304
OnDeviceAvailable(const OutputDeviceInfo & castOutputDeviceInfo)305 void OnDeviceAvailable(const OutputDeviceInfo& castOutputDeviceInfo) override
306 {
307 };
308
OnDeviceOffline(const std::string & deviceId)309 void OnDeviceOffline(const std::string& deviceId) override
310 {
311 };
312
AsObject()313 sptr<IRemoteObject> AsObject() override
314 {
315 return nullptr;
316 };
317 };
318
319 /**
320 * @tc.name: NotifyAudioSessionCheck001
321 * @tc.desc:
322 * @tc.type: FUNC
323 * @tc.require:
324 */
325 HWTEST_F(AVSessionDumperTest, NotifyAudioSessionCheck001, TestSize.Level1)
326 {
327 TestSessionListener listener;
328 avSessionService_->AddInnerSessionListener(&listener);
329 sptr<TestISessionListener> iListener = new TestISessionListener();
330 avSessionService_->AddSessionListener(1, iListener);
331 avSessionService_->NotifyAudioSessionCheck(1);
332 }
333
334 /**
335 * @tc.name: GetSessionDescriptorsBySessionId001
336 * @tc.desc:
337 * @tc.type: FUNC
338 * @tc.require:
339 */
340 HWTEST_F(AVSessionDumperTest, GetSessionDescriptorsBySessionId001, TestSize.Level1)
341 {
342 AVSessionDescriptor descriptor;
343 descriptor.sessionId_ = "sessionId";
344 sptr<AVSessionItem> item1 = new AVSessionItem(descriptor);
345 avSessionService_->GetContainer().AddSession(1, "abilityName1", item1);
346 EXPECT_EQ(avSessionService_->GetSessionDescriptorsBySessionId("sessionId", descriptor), AVSESSION_SUCCESS);
347 avSessionService_->GetContainer().RemoveSession(1);
348 }
349
350 /**
351 * @tc.name: StartDefaultAbilityByCall001
352 * @tc.desc:
353 * @tc.type: FUNC
354 * @tc.require:
355 */
356 HWTEST_F(AVSessionDumperTest, StartDefaultAbilityByCall001, TestSize.Level1)
357 {
358 std::string sessionId = "sessionId";
359 EXPECT_EQ(avSessionService_->StartDefaultAbilityByCall(sessionId), -1016);
360 }
361
362 /**
363 * @tc.name: StartAbilityByCall001
364 * @tc.desc:
365 * @tc.type: FUNC
366 * @tc.require:
367 */
368 HWTEST_F(AVSessionDumperTest, StartAbilityByCall001, TestSize.Level1)
369 {
370 std::string sessionIdNeeded = "sessionIdNeeded";
371 std::string sessionId = "sessionId";
372 EXPECT_EQ(avSessionService_->StartAbilityByCall(sessionIdNeeded, sessionId), AVSESSION_ERROR);
373 }
374
375 /**
376 * @tc.name: DeleteHistoricalRecord001
377 * @tc.desc:
378 * @tc.type: FUNC
379 * @tc.require:
380 */
381 HWTEST_F(AVSessionDumperTest, DeleteHistoricalRecord001, TestSize.Level1)
382 {
383 std::string bundleName = "bundleName";
384 avSessionService_->DeleteHistoricalRecord(bundleName);
385 }
386
387 /**
388 * @tc.name: GetService001
389 * @tc.desc:
390 * @tc.type: FUNC
391 * @tc.require:
392 */
393 HWTEST_F(AVSessionDumperTest, GetService001, TestSize.Level1)
394 {
395 std::string deviceId = "deviceId";
396 EXPECT_EQ(avSessionService_->GetService(deviceId), nullptr);
397 }
398
399 /**
400 * @tc.name: IsLocalDevice001
401 * @tc.desc:
402 * @tc.type: FUNC
403 * @tc.require:
404 */
405 HWTEST_F(AVSessionDumperTest, IsLocalDevice001, TestSize.Level1)
406 {
407 std::string networkId = "networkId";
408 EXPECT_EQ(avSessionService_->IsLocalDevice(networkId), true);
409 }
410
411 /**
412 * @tc.name: GetTrustedDeviceName001
413 * @tc.desc:
414 * @tc.type: FUNC
415 * @tc.require:
416 */
417 HWTEST_F(AVSessionDumperTest, GetTrustedDeviceName001, TestSize.Level1)
418 {
419 std::string networkId = "networkId";
420 std::string deviceName = "deviceName";
421 EXPECT_EQ(avSessionService_->GetTrustedDeviceName(networkId, deviceName), AVSESSION_SUCCESS);
422 }
423
424 /**
425 * @tc.name: SetBasicInfo001
426 * @tc.desc:
427 * @tc.type: FUNC
428 * @tc.require:
429 */
430 HWTEST_F(AVSessionDumperTest, SetBasicInfo001, TestSize.Level1)
431 {
432 std::string basicInfo = "basicInfo";
433 EXPECT_EQ(avSessionService_->SetBasicInfo(basicInfo), AVSESSION_ERROR);
434 }
435
436 /**
437 * @tc.name: SetDeviceInfo001
438 * @tc.desc:
439 * @tc.type: FUNC
440 * @tc.require:
441 */
442 HWTEST_F(AVSessionDumperTest, SetDeviceInfo001, TestSize.Level1)
443 {
444 std::vector<AudioStandard::AudioDeviceDescriptor> castAudioDescriptors;
445 AudioStandard::AudioDeviceDescriptor des;
446 castAudioDescriptors.push_back(des);
447 AVSessionDescriptor descriptor;
448 descriptor.sessionId_ = "sessionId";
449 sptr<AVSessionItem> item1 = new AVSessionItem(descriptor);
450 avSessionService_->SetDeviceInfo(castAudioDescriptors, item1);
451 }
452
453 /**
454 * @tc.name: GetAudioDescriptorByDeviceId001
455 * @tc.desc:
456 * @tc.type: FUNC
457 * @tc.require:
458 */
459 HWTEST_F(AVSessionDumperTest, GetAudioDescriptorByDeviceId001, TestSize.Level1)
460 {
461 std::vector<sptr<AudioStandard::AudioDeviceDescriptor>> castAudioDescriptors;
462 sptr<AudioStandard::AudioDeviceDescriptor> des = new AudioStandard::AudioDeviceDescriptor();
463 AudioStandard::AudioDeviceDescriptor res;
464 des->deviceId_ = 12;
465 castAudioDescriptors.push_back(des);
466 std::string deviceId = "12";
467 EXPECT_EQ(avSessionService_->GetAudioDescriptorByDeviceId(castAudioDescriptors, deviceId, res), true);
468 }
469
470 /**
471 * @tc.name: GetAudioDescriptorByDeviceId002
472 * @tc.desc:
473 * @tc.type: FUNC
474 * @tc.require:
475 */
476 HWTEST_F(AVSessionDumperTest, GetAudioDescriptorByDeviceId002, TestSize.Level1)
477 {
478 std::vector<sptr<AudioStandard::AudioDeviceDescriptor>> castAudioDescriptors;
479 sptr<AudioStandard::AudioDeviceDescriptor> des = new AudioStandard::AudioDeviceDescriptor();
480 AudioStandard::AudioDeviceDescriptor res;
481 des->deviceId_ = 11;
482 castAudioDescriptors.push_back(des);
483 std::string deviceId = "12";
484 EXPECT_EQ(avSessionService_->GetAudioDescriptorByDeviceId(castAudioDescriptors, deviceId, res), false);
485 }
486
487 /**
488 * @tc.name: SelectOutputDevice001
489 * @tc.desc:
490 * @tc.type: FUNC
491 * @tc.require:
492 */
493 HWTEST_F(AVSessionDumperTest, SelectOutputDevice001, TestSize.Level1)
494 {
495 AudioStandard::AudioDeviceDescriptor des;
496 EXPECT_EQ(avSessionService_->SelectOutputDevice(1, des), AVSESSION_ERROR);
497 }
498
499 /**
500 * @tc.name: GetAudioDescriptor001
501 * @tc.desc:
502 * @tc.type: FUNC
503 * @tc.require:
504 */
505 HWTEST_F(AVSessionDumperTest, GetAudioDescriptor001, TestSize.Level1)
506 {
507 std::vector<AudioStandard::AudioDeviceDescriptor> castAudioDescriptors;
508 AudioStandard::AudioDeviceDescriptor des;
509 des.deviceId_ = 11;
510 castAudioDescriptors.push_back(des);
511 std::string deviceId = "12";
512 EXPECT_EQ(avSessionService_->GetAudioDescriptor(deviceId, castAudioDescriptors), AVSESSION_ERROR);
513 }
514 } // namespace AVSession
515 } // namespace OHOS
516