• 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 <fstream>
18 #include "avsession_descriptor.h"
19 #include "avsession_log.h"
20 #include "avsession_errors.h"
21 #include "avsession_service.h"
22 #include "system_ability_definition.h"
23 
24 using namespace testing::ext;
25 
26 namespace OHOS::AVSession {
27 static char g_testSessionTag[] = "test";
28 static char g_testAnotherBundleName[] = "testAnother.ohos.avsession";
29 static char g_testAnotherAbilityName[] = "testAnother.ability";
30 static sptr<AVSessionService> g_AVSessionService;
31 
32 class AVSessionServiceSupplementTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp() override;
37     void TearDown() override;
38 };
39 
SetUpTestCase()40 void AVSessionServiceSupplementTest::SetUpTestCase()
41 {
42     SLOGI("set up AVSessionServiceTest with OnStart");
43     system("killall -9 com.example.hiMusicDemo");
44     g_AVSessionService = new AVSessionService(OHOS::AVSESSION_SERVICE_ID, true);
45     g_AVSessionService->OnStart();
46     SLOGI("set up AVSessionServiceTest done");
47 }
48 
TearDownTestCase()49 void AVSessionServiceSupplementTest::TearDownTestCase()
50 {
51     SLOGI("tear down AVSessionServiceTest");
52 }
53 
SetUp()54 void AVSessionServiceSupplementTest::SetUp()
55 {
56 }
57 
TearDown()58 void AVSessionServiceSupplementTest::TearDown()
59 {
60 }
61 
62 /**
63  * @tc.name: GetService001
64  * @tc.desc: Test GetService
65  * @tc.type: FUNC
66  */
67 static HWTEST_F(AVSessionServiceSupplementTest, GetService001, TestSize.Level1)
68 {
69     SLOGI("GetService001 begin!");
70     std::string id = "";
71     auto ret = g_AVSessionService->GetService(id);
72     EXPECT_NE(ret, nullptr);
73     SLOGI("GetService001 end!");
74 }
75 
76 /**
77  * @tc.name: IsLocalDevice001
78  * @tc.desc: Test IsLocalDevice
79  * @tc.type: FUNC
80  */
81 static HWTEST_F(AVSessionServiceSupplementTest, IsLocalDevice001, TestSize.Level1)
82 {
83     SLOGI("IsLocalDevice001 begin!");
84     std::string id;
85     int32_t getSuccess = g_AVSessionService->GetLocalNetworkId(id);
86     EXPECT_NE(getSuccess, AVSESSION_SUCCESS);
87     bool ret = g_AVSessionService->IsLocalDevice(id);
88     EXPECT_EQ(ret, true);
89     SLOGI("IsLocalDevice001 end!");
90 }
91 
92 /**
93  * @tc.name: GetTrustedDeviceName001
94  * @tc.desc: Test GetTrustedDeviceName
95  * @tc.type: FUNC
96  */
97 static HWTEST_F(AVSessionServiceSupplementTest, GetTrustedDeviceName001, TestSize.Level1)
98 {
99     SLOGI("GetTrustedDeviceName001 begin!");
100     std::string networkId = "";
101     std::string deviceName = "";
102     int32_t ret = g_AVSessionService->GetTrustedDeviceName(networkId, deviceName);
103     EXPECT_EQ(ret, AVSESSION_SUCCESS);
104     SLOGI("GetTrustedDeviceName001 end!");
105 }
106 
107 /**
108  * @tc.name: SetDeviceInfo001
109  * @tc.desc: Test SetDeviceInfo
110  * @tc.type: FUNC
111  */
112 static HWTEST_F(AVSessionServiceSupplementTest, SetDeviceInfo001, TestSize.Level1)
113 {
114     SLOGI("SetDeviceInfo001 begin!");
115     AudioStandard::AudioDeviceDescriptor descriptor;
116     std::vector<AudioStandard::AudioDeviceDescriptor> castAudioDescriptors;
117     castAudioDescriptors.push_back(descriptor);
118 
119     OHOS::AppExecFwk::ElementName elementName;
120     elementName.SetBundleName(g_testAnotherBundleName);
121     elementName.SetAbilityName(g_testAnotherAbilityName);
122     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
123         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
124 
125     EXPECT_NE(avsessionItem, nullptr);
126 
127     g_AVSessionService->SetDeviceInfo(castAudioDescriptors, avsessionItem);
128     avsessionItem->Destroy();
129     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
130     SLOGI("SetDeviceInfo001 end!");
131 }
132 
133 /**
134  * @tc.name: GetAudioDescriptorByDeviceId001
135  * @tc.desc: Test GetAudioDescriptorByDeviceId
136  * @tc.type: FUNC
137  */
138 static HWTEST_F(AVSessionServiceSupplementTest, GetAudioDescriptorByDeviceId001, TestSize.Level1)
139 {
140     SLOGI("GetAudioDescriptorByDeviceId001 begin!");
141     std::vector<std::shared_ptr<AudioStandard::AudioDeviceDescriptor>> descriptors;
142     std::shared_ptr<AudioStandard::AudioDeviceDescriptor> descriptor =
143         std::make_shared<AudioStandard::AudioDeviceDescriptor>();
144     descriptors.push_back(descriptor);
145     std::string deviceId = "0";
146     AudioStandard::AudioDeviceDescriptor audioDescriptor;
147     bool ret = g_AVSessionService->GetAudioDescriptorByDeviceId(descriptors, deviceId, audioDescriptor);
148     EXPECT_EQ(ret, true);
149     SLOGI("GetAudioDescriptorByDeviceId001 end!");
150 }
151 
152 /**
153  * @tc.name: GetAudioDescriptorByDeviceId002
154  * @tc.desc: Test GetAudioDescriptorByDeviceId
155  * @tc.type: FUNC
156  */
157 static HWTEST_F(AVSessionServiceSupplementTest, GetAudioDescriptorByDeviceId002, TestSize.Level1)
158 {
159     SLOGI("GetAudioDescriptorByDeviceId002 begin!");
160     std::vector<std::shared_ptr<AudioStandard::AudioDeviceDescriptor>> descriptors;
161     std::shared_ptr<AudioStandard::AudioDeviceDescriptor> descriptor =
162         std::make_shared<AudioStandard::AudioDeviceDescriptor>();
163     descriptors.push_back(descriptor);
164     std::string deviceId = "1";
165     AudioStandard::AudioDeviceDescriptor audioDescriptor;
166     bool ret = g_AVSessionService->GetAudioDescriptorByDeviceId(descriptors, deviceId, audioDescriptor);
167     EXPECT_EQ(ret, false);
168     SLOGI("GetAudioDescriptorByDeviceId002 end!");
169 }
170 
171 /**
172  * @tc.name: GetDeviceInfo001
173  * @tc.desc: Test GetDeviceInfo
174  * @tc.type: FUNC
175  */
176 static HWTEST_F(AVSessionServiceSupplementTest, GetDeviceInfo001, TestSize.Level1)
177 {
178     SLOGI("GetDeviceInfo001 begin!");
179     OHOS::AppExecFwk::ElementName elementName;
180     elementName.SetBundleName(g_testAnotherBundleName);
181     elementName.SetAbilityName(g_testAnotherAbilityName);
182     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
183         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
184 
185     std::vector<AudioStandard::AudioDeviceDescriptor> castSinkDescriptors;
186     std::vector<AudioStandard::AudioDeviceDescriptor> cancelSinkDescriptors;
187     std::vector<AudioStandard::AudioDeviceDescriptor> descriptors;
188     AudioStandard::AudioDeviceDescriptor descriptor;
189     descriptors.push_back(descriptor);
190 
191     g_AVSessionService->GetDeviceInfo(avsessionItem,
192         descriptors, castSinkDescriptors, cancelSinkDescriptors);
193     EXPECT_EQ(castSinkDescriptors.size(), descriptors.size());
194     avsessionItem->Destroy();
195     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
196     SLOGI("GetDeviceInfo001 end!");
197 }
198 
199 /**
200  * @tc.name: GetDeviceInfo002
201  * @tc.desc: Test GetDeviceInfo
202  * @tc.type: FUNC
203  */
204 static HWTEST_F(AVSessionServiceSupplementTest, GetDeviceInfo002, TestSize.Level1)
205 {
206     SLOGI("GetDeviceInfo002 begin!");
207     OHOS::AppExecFwk::ElementName elementName;
208     elementName.SetBundleName(g_testAnotherBundleName);
209     elementName.SetAbilityName(g_testAnotherAbilityName);
210     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
211         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
212 
213     std::vector<AudioStandard::AudioDeviceDescriptor> castSinkDescriptors;
214     std::vector<AudioStandard::AudioDeviceDescriptor> cancelSinkDescriptors;
215     std::vector<AudioStandard::AudioDeviceDescriptor> descriptors;
216 
217     g_AVSessionService->GetDeviceInfo(avsessionItem,
218         descriptors, castSinkDescriptors, cancelSinkDescriptors);
219     EXPECT_EQ(descriptors.size(), 0);
220     avsessionItem->Destroy();
221     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
222     SLOGI("GetDeviceInfo002 end!");
223 }
224 
225 /**
226  * @tc.name: CastAudioProcess001
227  * @tc.desc: Test CastAudioProcess
228  * @tc.type: FUNC
229  */
230 static HWTEST_F(AVSessionServiceSupplementTest, CastAudioProcess001, TestSize.Level1)
231 {
232     SLOGI("CastAudioProcess001 begin!");
233     OHOS::AppExecFwk::ElementName elementName;
234     elementName.SetBundleName(g_testAnotherBundleName);
235     elementName.SetAbilityName(g_testAnotherAbilityName);
236     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
237         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
238 
239     std::vector<AudioStandard::AudioDeviceDescriptor> descriptors;
240     AudioStandard::AudioDeviceDescriptor descriptor;
241     descriptors.push_back(descriptor);
242 
243     std::string sourceSessionInfo = "";
244     int32_t ret = g_AVSessionService->CastAudioProcess(descriptors,
245         sourceSessionInfo, avsessionItem);
246     EXPECT_EQ(ret, AVSESSION_ERROR);
247     avsessionItem->Destroy();
248     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
249     SLOGI("CastAudioProcess001 end!");
250 }
251 
252 /**
253  * @tc.name: CastAudioProcess002
254  * @tc.desc: Test CastAudioProcess
255  * @tc.type: FUNC
256  */
257 static HWTEST_F(AVSessionServiceSupplementTest, CastAudioProcess002, TestSize.Level1)
258 {
259     SLOGI("CastAudioProcess002 begin!");
260     OHOS::AppExecFwk::ElementName elementName;
261     elementName.SetBundleName(g_testAnotherBundleName);
262     elementName.SetAbilityName(g_testAnotherAbilityName);
263     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
264         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
265 
266     AudioStandard::AudioDeviceDescriptor descriptor;
267     std::vector<AudioStandard::AudioDeviceDescriptor> setDescriptors;
268     setDescriptors.push_back(descriptor);
269     g_AVSessionService->SetDeviceInfo(setDescriptors, avsessionItem);
270 
271     std::string sourceSessionInfo = "";
272     int32_t ret = g_AVSessionService->CastAudioProcess(setDescriptors,
273         sourceSessionInfo, avsessionItem);
274     EXPECT_EQ(ret, AVSESSION_ERROR);
275     avsessionItem->Destroy();
276     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
277     SLOGI("CastAudioProcess002 end!");
278 }
279 
280 /**
281  * @tc.name: IsHistoricalSession001
282  * @tc.desc: Test IsHistoricalSession
283  * @tc.type: FUNC
284  */
285 static HWTEST_F(AVSessionServiceSupplementTest, IsHistoricalSession001, TestSize.Level1)
286 {
287     SLOGI("IsHistoricalSession001 begin!");
288     OHOS::AppExecFwk::ElementName elementName;
289     elementName.SetBundleName(g_testAnotherBundleName);
290     elementName.SetAbilityName(g_testAnotherAbilityName);
291     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
292         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
293 
294     std::string id = "*****";
295     std::string filePath = g_AVSessionService->GetAVSortDir();
296     std::ofstream ofs;
297     ofs.open(filePath, std::ios::out);
298     ofs << id;
299     ofs.close();
300 
301     bool ret = g_AVSessionService->IsHistoricalSession(id);
302     EXPECT_EQ(ret, true);
303     avsessionItem->Destroy();
304     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
305     SLOGI("IsHistoricalSession001 end!");
306 }
307 
308 /**
309  * @tc.name: IsHistoricalSession002
310  * @tc.desc: Test IsHistoricalSession
311  * @tc.type: FUNC
312  */
313 static HWTEST_F(AVSessionServiceSupplementTest, IsHistoricalSession002, TestSize.Level1)
314 {
315     SLOGI("IsHistoricalSession002 begin!");
316     OHOS::AppExecFwk::ElementName elementName;
317     elementName.SetBundleName(g_testAnotherBundleName);
318     elementName.SetAbilityName(g_testAnotherAbilityName);
319     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
320         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
321 
322     std::string sessionId = avsessionItem->GetSessionId();
323     std::string filePath = g_AVSessionService->GetAVSortDir();
324     std::ofstream ofs;
325     ofs.open(filePath, std::ios::out);
326     ofs << sessionId;
327     ofs.close();
328 
329     bool ret = g_AVSessionService->IsHistoricalSession(sessionId);
330     EXPECT_EQ(ret, false);
331     avsessionItem->Destroy();
332     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
333     SLOGI("IsHistoricalSession002 end!");
334 }
335 
336 /**
337  * @tc.name: Dump001
338  * @tc.desc: Test Dump
339  * @tc.type: FUNC
340  */
341 static HWTEST_F(AVSessionServiceSupplementTest, Dump001, TestSize.Level1)
342 {
343     SLOGI("Dump001 with OnStartProcess begin!");
344     std::vector<std::u16string> argsList;
345     g_AVSessionService->dumpHelper_ = std::make_unique<AVSessionDumper>();
346     int32_t ret = g_AVSessionService->Dump(1, argsList);
347     EXPECT_EQ(ret, AVSESSION_SUCCESS);
348     SLOGI("Dump001 end!");
349 }
350 
351 /**
352  * @tc.name: Dump002
353  * @tc.desc: Test Dump
354  * @tc.type: FUNC
355  */
356 static HWTEST_F(AVSessionServiceSupplementTest, Dump002, TestSize.Level1)
357 {
358     SLOGI("Dump002 with OnStartProcess begin!");
359     std::vector<std::u16string> argsList;
360     std::u16string str(5, 'a');
361     argsList.emplace_back(str);
362     g_AVSessionService->dumpHelper_ = std::make_unique<AVSessionDumper>();
363     int32_t ret = g_AVSessionService->Dump(1, argsList);
364     EXPECT_EQ(ret, AVSESSION_SUCCESS);
365     SLOGI("Dump002 end!");
366 }
367 
368 /**
369  * @tc.name: SaveStringToFileEx001
370  * @tc.desc: Test SaveStringToFile
371  * @tc.type: FUNC
372  */
373 static HWTEST_F(AVSessionServiceSupplementTest, SaveStringToFileEx001, TestSize.Level1)
374 {
375     SLOGI("SaveStringToFileEx001 begin!");
376     OHOS::AppExecFwk::ElementName elementName;
377     elementName.SetBundleName(g_testAnotherBundleName);
378     elementName.SetAbilityName(g_testAnotherAbilityName);
379     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
380         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
381 
382     std::string filePath = "/adcdXYZ123/test5.txt";
383     std::string content;
384     std::ifstream file(filePath, std::ios_base::in);
385     bool ret = g_AVSessionService->SaveStringToFileEx(filePath, content);
386     file.close();
387     EXPECT_EQ(ret, false);
388     avsessionItem->Destroy();
389     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
390     SLOGI("SaveStringToFileEx001 end!");
391 }
392 
393 /**
394  * @tc.name: SaveStringToFileEx002
395  * @tc.desc: Test SaveStringToFile
396  * @tc.type: FUNC
397  */
398 static HWTEST_F(AVSessionServiceSupplementTest, SaveStringToFileEx002, TestSize.Level1)
399 {
400     SLOGI("SaveStringToFileEx002 begin!");
401     OHOS::AppExecFwk::ElementName elementName;
402     elementName.SetBundleName(g_testAnotherBundleName);
403     elementName.SetAbilityName(g_testAnotherAbilityName);
404     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
405         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
406 
407     std::string filePath = g_AVSessionService->GetAVSortDir();
408     std::string content;
409     bool ret = g_AVSessionService->SaveStringToFileEx(filePath, content);
410     EXPECT_EQ(ret, false);
411     avsessionItem->Destroy();
412     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
413     SLOGI("SaveStringToFileEx002 end!");
414 }
415 
416 /**
417  * @tc.name: SaveStringToFileEx003
418  * @tc.desc: Test SaveStringToFile
419  * @tc.type: FUNC
420  */
421 static HWTEST_F(AVSessionServiceSupplementTest, SaveStringToFileEx003, TestSize.Level1)
422 {
423     SLOGI("SaveStringToFileEx003 begin!");
424     OHOS::AppExecFwk::ElementName elementName;
425     elementName.SetBundleName(g_testAnotherBundleName);
426     elementName.SetAbilityName(g_testAnotherAbilityName);
427     sptr<AVSessionItem> avsessionItem = g_AVSessionService->CreateSessionInner(
428         g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName);
429 
430     std::string filePath = g_AVSessionService->GetAVSortDir();
431     std::string content = "123456";
432     bool ret = g_AVSessionService->SaveStringToFileEx(filePath, content);
433     EXPECT_EQ(ret, true);
434     avsessionItem->Destroy();
435     g_AVSessionService->HandleSessionRelease(avsessionItem->GetSessionId());
436     SLOGI("SaveStringToFileEx003 end!");
437 }
438 
439 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
440 /**
441  * @tc.name: SuperLauncher001
442  * @tc.desc: Test SuperLauncher
443  * @tc.type: FUNC
444  */
445 static HWTEST_F(AVSessionServiceSupplementTest, SuperLauncher001, TestSize.Level1)
446 {
447     SLOGI("SuperLauncher001 begin!");
448     std::string devideId = "***";
449     std::string extraInfo = "*,**";
450     std::vector< std::string > serviceNames = {"HuaweiCast", "HuaweiCast-Dual"};
451     std::vector< std::string > states = {"IDLE", "CONNECT_SUCC"};
452     for (std::string serviceName : serviceNames) {
453         for (std::string state : states) {
454             g_AVSessionService->SuperLauncher(devideId, serviceName, extraInfo, state);
455         }
456     }
457 
458     EXPECT_EQ(serviceNames.size(), 2);
459     SLOGI("SuperLauncher001 end!");
460 }
461 
462 /**
463  * @tc.name: SuperLauncher002
464  * @tc.desc: Test SuperLauncher
465  * @tc.type: FUNC
466  */
467 static HWTEST_F(AVSessionServiceSupplementTest, SuperLauncher002, TestSize.Level1)
468 {
469     SLOGI("SuperLauncher002 begin!");
470     std::string devideId = "***";
471     std::string extraInfo = "***";
472     std::string serviceName = "***";
473     std::string state = "***";
474     g_AVSessionService->SuperLauncher(devideId, serviceName, extraInfo, state);
475     EXPECT_EQ(serviceName, "***");
476     SLOGI("SuperLauncher002 end!");
477 }
478 
479 /**
480  * @tc.name: SplitExtraInfo001
481  * @tc.desc: Test SplitExtraInfo
482  * @tc.type: FUNC
483  */
484 static HWTEST_F(AVSessionServiceSupplementTest, SplitExtraInfo001, TestSize.Level1)
485 {
486     SLOGI("SplitExtraInfo001 begin!");
487     std::string info = R"(
488         {SUPPORT_MIRROR_TO_STREAM: true},
489         {deviceId: ***},
490         {deviceName: ***},
491         {deviceType: ***}
492     )";
493     g_AVSessionService->SplitExtraInfo(info);
494     EXPECT_NE(info.size(), 0);
495     SLOGI("SplitExtraInfo001 end!");
496 }
497 
498 /**
499  * @tc.name: SplitExtraInfo002
500  * @tc.desc: Test SplitExtraInfo
501  * @tc.type: FUNC
502  */
503 static HWTEST_F(AVSessionServiceSupplementTest, SplitExtraInfo002, TestSize.Level1)
504 {
505     SLOGI("SplitExtraInfo002 begin!");
506     std::string info = R"(
507         {SUPPORT_MIRROR_TO_STREAM, false},
508         {deviceId, ***},
509         {deviceName, ***},
510         {deviceType, ***}
511         )";
512     g_AVSessionService->SplitExtraInfo(info);
513     EXPECT_NE(info.size(), 0);
514     SLOGI("SplitExtraInfo002 end!");
515 }
516 
517 /**
518  * @tc.name: SplitExtraInfo003
519  * @tc.desc: Test SplitExtraInfo
520  * @tc.type: FUNC
521  */
522 static HWTEST_F(AVSessionServiceSupplementTest, SplitExtraInfo003, TestSize.Level1)
523 {
524     SLOGI("SplitExtraInfo003 begin!");
525     std::string info = "";
526     g_AVSessionService->SplitExtraInfo(info);
527     EXPECT_EQ(info.size(), 0);
528     SLOGI("SplitExtraInfo003 end!");
529 }
530 
531 #endif
532 } // OHOS::AVSession