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 
18 #include "avsession_log.h"
19 #include "avsession_dumper.h"
20 #include "avsession_proxy.h"
21 #include "av_session.h"
22 
23 using namespace testing::ext;
24 // using namespace OHOS;
25 // using namespace OHOS::AVSession;
26 namespace OHOS::AVSession {
27 
28 static const std::string ARGS_HELP = "-h";
29 static const std::string ILLEGAL_INFORMATION = "AVSession service, enter '-h' for usage.\n";
30 static const std::string ARGS_SHOW_METADATA = "-show_metadata";
31 static const std::string ARGS_SHOW_SESSION_INFO = "-show_session_info";
32 static const std::string ARGS_SHOW_CONTROLLER_INFO = "-show_controller_info";
33 static const std::string ARGS_SHOW_ERROR_INFO = "-show_error_info";
34 static const std::string ARGS_TRUSTED_DEVICES_INFO = "-show_trusted_devices_Info";
35 
36 std::shared_ptr<AVSessionDumper> g_AVSessionDumper = nullptr;
37 sptr<AVSessionService> g_AVSessionService = nullptr;
38 class AVSessionDumperAnotherTest : public testing::Test {
39 public:
40     static void SetUpTestCase(void);
41     static void TearDownTestCase(void);
42     void SetUp();
43     void TearDown();
44 };
45 
SetUpTestCase()46 void AVSessionDumperAnotherTest::SetUpTestCase()
47 {
48     int32_t systemAbilityId = 1;
49     bool runOnCreate = true;
50     g_AVSessionService = new AVSessionService(systemAbilityId, runOnCreate);
51     g_AVSessionService->OnStart();
52     ASSERT_NE(g_AVSessionService, nullptr);
53 
54     g_AVSessionDumper = std::make_shared<AVSessionDumper>();
55     ASSERT_NE(g_AVSessionDumper, nullptr);
56 }
57 
TearDownTestCase()58 void AVSessionDumperAnotherTest::TearDownTestCase()
59 {
60     g_AVSessionService->OnStop();
61 }
62 
SetUp()63 void AVSessionDumperAnotherTest::SetUp()
64 
65 {
66 }
67 
TearDown()68 void AVSessionDumperAnotherTest::TearDown()
69 {
70 }
71 
72 /**
73  * @tc.name: Dump__ShowIllegalInfo001
74  * @tc.desc: Test ShowIllegalInfo
75  * @tc.type: FUNC
76  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowIllegalInfo001, TestSize.Level1)77 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowIllegalInfo001, TestSize.Level1)
78 {
79     SLOGI("Dump__ShowHelp001 begin!");
80     std::vector<std::string> args = { "aa", "bb" };
81     std::string result = "";
82     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
83     EXPECT_EQ(result, ILLEGAL_INFORMATION);
84     SLOGI("Dump__ShowHelp001 end!");
85 }
86 
87 /**
88  * @tc.name: Dump__ShowIllegalInfo002
89  * @tc.desc: Test ShowIllegalInfo
90  * @tc.type: FUNC
91  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowIllegalInfo002, TestSize.Level1)92 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowIllegalInfo002, TestSize.Level1)
93 {
94     SLOGI("Dump__ShowHelp002 begin!");
95     std::vector<std::string> args = { "****" };
96     std::string result = "";
97     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
98     EXPECT_EQ(result, ILLEGAL_INFORMATION);
99     SLOGI("Dump__ShowHelp002 end!");
100 }
101 
102 /**
103  * @tc.name: Dump__ShowHelp
104  * @tc.desc: Test ShowHelp
105  * @tc.type: FUNC
106  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowHelp, TestSize.Level1)107 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowHelp, TestSize.Level1)
108 {
109     SLOGI("Dump__ShowHelp begin!");
110 
111     std::vector<std::string> args = { ARGS_HELP };
112     std::string result = "";
113     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
114     EXPECT_NE(result.size(), 0);
115 
116     SLOGI("Dump__ShowHelp end!");
117 }
118 
119 /**
120  * @tc.name: Dump__ShowMetaData
121  * @tc.desc: Test ShowMetaData
122  * @tc.type: FUNC
123  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowMetaData, TestSize.Level1)124 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowMetaData, TestSize.Level1)
125 {
126     SLOGI("Dump__ShowMetaData begin!");
127 
128     std::vector<std::string> args = { ARGS_SHOW_METADATA };
129     std::string result = "";
130     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
131     EXPECT_EQ(result.size(), 0);
132 
133     SLOGI("Dump__ShowMetaData end!");
134 }
135 
136 /**
137  * @tc.name: Dump__ShowSessionInfo__SESSION_TYPE_AUDIO
138  * @tc.desc: Test ShowSessionInfo by SESSION_TYPE_AUDIO
139  * @tc.type: FUNC
140  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_AUDIO, TestSize.Level1)141 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_AUDIO, TestSize.Level1)
142 {
143     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_AUDIO begin!");
144     std::vector<std::string> args = { ARGS_SHOW_SESSION_INFO };
145     std::string result = "";
146     OHOS::AppExecFwk::ElementName elementName;
147     elementName.SetBundleName("test.ohos.avsession");
148     elementName.SetAbilityName("test.ability");
149     auto obj = g_AVSessionService->CreateSessionInner("test", AVSession::SESSION_TYPE_AUDIO, elementName);
150     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
151     EXPECT_NE(result.size(), 0);
152     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_AUDIO end!");
153 }
154 
155 /**
156  * @tc.name: Dump__ShowSessionInfo__SESSION_TYPE_VIDEO
157  * @tc.desc: Test ShowSessionInfo by SESSION_TYPE_VIDEO
158  * @tc.type: FUNC
159  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VIDEO, TestSize.Level1)160 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VIDEO, TestSize.Level1)
161 {
162     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VIDEO begin!");
163 
164     std::vector<std::string> args = { ARGS_SHOW_SESSION_INFO };
165     std::string result = "";
166     OHOS::AppExecFwk::ElementName elementName;
167     elementName.SetBundleName("test.ohos.avsession");
168     elementName.SetAbilityName("test.ability");
169     auto obj = g_AVSessionService->CreateSessionInner("test", AVSession::SESSION_TYPE_VIDEO, elementName);
170     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
171     EXPECT_NE(result.size(), 0);
172     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VIDEO end!");
173 }
174 
175 /**
176  * @tc.name: Dump__ShowSessionInfo__SESSION_TYPE_VOICE_CALL
177  * @tc.desc: Test ShowSessionInfo by SESSION_TYPE_VOICE_CALL
178  * @tc.type: FUNC
179  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VOICE_CALL, TestSize.Level1)180 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VOICE_CALL, TestSize.Level1)
181 {
182     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VOICE_CALL begin!");
183     std::vector<std::string> args = { ARGS_SHOW_SESSION_INFO };
184     std::string result = "";
185     OHOS::AppExecFwk::ElementName elementName;
186     elementName.SetBundleName("test.ohos.avsession");
187     elementName.SetAbilityName("test.ability");
188     auto obj = g_AVSessionService->CreateSessionInner("test", AVSession::SESSION_TYPE_VOICE_CALL, elementName);
189     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
190     EXPECT_NE(result.size(), 0);
191     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VOICE_CALL end!");
192 }
193 
194 /**
195  * @tc.name: Dump__ShowSessionInfo__SESSION_TYPE_VIDEO_CALL
196  * @tc.desc: Test ShowSessionInfo by SESSION_TYPE_VIDEO_CALL
197  * @tc.type: FUNC
198  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VIDEO_CALL, TestSize.Level1)199 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VIDEO_CALL, TestSize.Level1)
200 {
201     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VIDEO_CALL begin!");
202     std::vector<std::string> args = { ARGS_SHOW_SESSION_INFO };
203     std::string result = "";
204     OHOS::AppExecFwk::ElementName elementName;
205     elementName.SetBundleName("test.ohos.avsession");
206     elementName.SetAbilityName("test.ability");
207     auto obj = g_AVSessionService->CreateSessionInner("test", AVSession::SESSION_TYPE_VIDEO_CALL, elementName);
208     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
209     EXPECT_NE(result.size(), 0);
210     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VIDEO_CALL end!");
211 }
212 
213 /**
214  * @tc.name: Dump__ShowControllerInfo
215  * @tc.desc: Test ShowControllerInfo
216  * @tc.type: FUNC
217  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowControllerInfo, TestSize.Level1)218 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowControllerInfo, TestSize.Level1)
219 {
220     SLOGI("Dump__ShowControllerInfo begin!");
221     std::vector<std::string> args = { ARGS_SHOW_CONTROLLER_INFO };
222     std::string result = "";
223     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
224     EXPECT_NE(result.size(), 0);
225     SLOGI("Dump__ShowControllerInfo end!");
226 }
227 
228 /**
229  * @tc.name: Dump__ShowErrorInfo001
230  * @tc.desc: Test ShowErrorInfo
231  * @tc.type: FUNC
232  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowErrorInfo001, TestSize.Level1)233 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowErrorInfo001, TestSize.Level1)
234 {
235     SLOGI("Dump__ShowErrorInfo001 begin!");
236     std::vector<std::string> args = { ARGS_SHOW_ERROR_INFO };
237     std::string result = "";
238     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
239     EXPECT_NE(result.size(), 0);
240     SLOGI("Dump__ShowErrorInfo001 end!");
241 }
242 
243 /**
244  * @tc.name: Dump__ShowErrorInfo002
245  * @tc.desc: Test ShowErrorInfo
246  * @tc.type: FUNC
247  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowErrorInfo002, TestSize.Level1)248 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowErrorInfo002, TestSize.Level1)
249 {
250     SLOGI("Dump__ShowErrorInfo002 begin!");
251     std::vector<std::string> args = { ARGS_SHOW_ERROR_INFO };
252     std::string result = "";
253     g_AVSessionDumper->SetErrorInfo("***");
254     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
255     EXPECT_NE(result.size(), 0);
256     SLOGI("Dump__ShowErrorInfo002 end!");
257 }
258 
259 /**
260  * @tc.name: Dump__ShowTrustedDevicesInfo
261  * @tc.desc: Test ShowTrustedDevicesInfo
262  * @tc.type: FUNC
263  */
HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowTrustedDevicesInfo, TestSize.Level1)264 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowTrustedDevicesInfo, TestSize.Level1)
265 {
266     SLOGI("Dump__ShowTrustedDevicesInfo begin!");
267     std::vector<std::string> args = { ARGS_TRUSTED_DEVICES_INFO };
268     std::string result = "";
269     g_AVSessionDumper->Dump(args, result, *g_AVSessionService);
270     EXPECT_NE(result.size(), 0);
271     SLOGI("Dump__ShowTrustedDevicesInfo end!");
272 }
273 
274 }