1 /*
2 * Copyright (c) 2025 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 <chrono>
18 #include <thread>
19
20 #include "accesstoken_kit.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23 #include "avmeta_data.h"
24 #include "avplayback_state.h"
25 #include "avsession_service.h"
26 #include "avsession_errors.h"
27 #include "avsession_item.h"
28 #include "avsession_log.h"
29 #include "migrate_avsession_constant.h"
30 #include "migrate_avsession_manager.h"
31 #include "migrate_avsession_proxy.h"
32 #include "system_ability_definition.h"
33
34 using namespace testing::ext;
35 using namespace OHOS::AVSession;
36
37 static std::shared_ptr<AVSessionService> g_AVSessionService {nullptr};
38 static std::shared_ptr<MigrateAVSessionProxy> g_MigrateAVSessionProxy {nullptr};
39
40 class MigrateAVSessionProxyTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 };
47
SetUpTestCase()48 void MigrateAVSessionProxyTest::SetUpTestCase()
49 {
50 SLOGI("MigrateAVSessionProxyTest SetUpTestCase");
51 g_AVSessionService = std::make_shared<AVSessionService>(OHOS::AVSESSION_SERVICE_ID);
52 g_MigrateAVSessionProxy = std::make_shared<MigrateAVSessionProxy>(g_AVSessionService.get());
53 }
54
TearDownTestCase()55 void MigrateAVSessionProxyTest::TearDownTestCase()
56 {
57 SLOGI("MigrateAVSessionProxyTest TearDownTestCase");
58 g_MigrateAVSessionProxy = nullptr;
59 g_AVSessionService = nullptr;
60 }
61
SetUp()62 void MigrateAVSessionProxyTest::SetUp()
63 {
64 SLOGI("MigrateAVSessionProxyTest SetUp");
65 }
66
TearDown()67 void MigrateAVSessionProxyTest::TearDown()
68 {
69 SLOGI("MigrateAVSessionProxyTest TearDown");
70 }
71
72 /**
73 * @tc.name: MigrateAVSessionProxyControllerCallback001
74 * @tc.desc: not find func
75 * @tc.type: FUNC
76 * @tc.require:
77 */
78 static HWTEST_F(MigrateAVSessionProxyTest, MigrateAVSessionProxyControllerCallback001, TestSize.Level1)
79 {
80 MigrateAVSessionProxyControllerCallbackFunc func =
81 g_MigrateAVSessionProxy->MigrateAVSessionProxyControllerCallback();
82 std::string extraEvent = "";
83 OHOS::AAFwk::WantParams extras;
84 int32_t ret = func(extraEvent, extras);
85 EXPECT_EQ(ret, ERR_COMMAND_NOT_SUPPORT);
86 }
87
88 /**
89 * @tc.name: MigrateAVSessionProxyControllerCallback002
90 * @tc.desc: find the func
91 * @tc.type: FUNC
92 * @tc.require:
93 */
94 static HWTEST_F(MigrateAVSessionProxyTest, MigrateAVSessionProxyControllerCallback002, TestSize.Level1)
95 {
96 auto maps = g_MigrateAVSessionProxy->AUDIO_EVENT_MAPS;
97 MigrateAVSessionProxyControllerCallbackFunc func =
98 g_MigrateAVSessionProxy->MigrateAVSessionProxyControllerCallback();
99 for (auto it = maps.begin(); it != maps.end(); ++it) {
100 std::string extraEvent = it->first;
101 OHOS::AAFwk::WantParams extras;
102 int32_t ret = func(extraEvent, extras);
103 EXPECT_EQ(ret, AVSESSION_SUCCESS);
104 }
105 }
106
107 /**
108 * @tc.name: ProcessSessionInfo001
109 * @tc.desc: test the member of ProcessSessionInfo
110 * @tc.type: FUNC
111 * @tc.require:
112 */
113 static HWTEST_F(MigrateAVSessionProxyTest, ProcessSessionInfo001, TestSize.Level1)
114 {
115 Json::Value jsonValue;
116 jsonValue["test"] = "test";
117 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
118 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
119 g_MigrateAVSessionProxy->ProcessSessionInfo(jsonValue);
120 }
121
122 /**
123 * @tc.name: ProcessSessionInfo002
124 * @tc.desc: test the member of ProcessSessionInfo
125 * @tc.type: FUNC
126 * @tc.require:
127 */
128 static HWTEST_F(MigrateAVSessionProxyTest, ProcessSessionInfo002, TestSize.Level1)
129 {
130 Json::Value jsonValue;
131 jsonValue[MIGRATE_SESSION_ID] = "";
132 jsonValue[MIGRATE_BUNDLE_NAME] = "";
133 jsonValue[MIGRATE_ABILITY_NAME] = "";
134 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
135 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
136 g_MigrateAVSessionProxy->ProcessSessionInfo(jsonValue);
137 }
138
139 /**
140 * @tc.name: ProcessSessionInfo003
141 * @tc.desc: test the member of ProcessSessionInfo
142 * @tc.type: FUNC
143 * @tc.require:
144 */
145 static HWTEST_F(MigrateAVSessionProxyTest, ProcessSessionInfo003, TestSize.Level1)
146 {
147 Json::Value jsonValue;
148 jsonValue[MIGRATE_SESSION_ID] = DEFAULT_STRING;
149 jsonValue[MIGRATE_BUNDLE_NAME] = DEFAULT_STRING;
150 jsonValue[MIGRATE_ABILITY_NAME] = DEFAULT_STRING;
151 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
152 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
153 g_MigrateAVSessionProxy->ProcessSessionInfo(jsonValue);
154 }
155
156 /**
157 * @tc.name: ProcessSessionInfo004
158 * @tc.desc: test the member of ProcessSessionInfo
159 * @tc.type: FUNC
160 * @tc.require:
161 */
162 static HWTEST_F(MigrateAVSessionProxyTest, ProcessSessionInfo004, TestSize.Level1)
163 {
164 Json::Value jsonValue;
165 jsonValue[MIGRATE_SESSION_ID] = EMPTY_SESSION;
166 jsonValue[MIGRATE_BUNDLE_NAME] = EMPTY_SESSION;
167 jsonValue[MIGRATE_ABILITY_NAME] = EMPTY_SESSION;
168 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
169 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
170 g_MigrateAVSessionProxy->ProcessSessionInfo(jsonValue);
171 }
172
173 /**
174 * @tc.name: ProcessMetaData001
175 * @tc.desc: test the member of ProcessMetaData
176 * @tc.type: FUNC
177 * @tc.require:
178 */
179 static HWTEST_F(MigrateAVSessionProxyTest, ProcessMetaData001, TestSize.Level1)
180 {
181 Json::Value jsonValue;
182 jsonValue[METADATA_TITLE] = METADATA_TITLE;
183 jsonValue[METADATA_ARTIST] = METADATA_ARTIST;
184 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
185 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
186 g_MigrateAVSessionProxy->ProcessMetaData(jsonValue);
187 }
188
189 /**
190 * @tc.name: ProcessMetaData002
191 * @tc.desc: test the member of ProcessMetaData
192 * @tc.type: FUNC
193 * @tc.require:
194 */
195 static HWTEST_F(MigrateAVSessionProxyTest, ProcessMetaData002, TestSize.Level1)
196 {
197 Json::Value jsonValue;
198 jsonValue["test"] = "test";
199 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
200 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
201 g_MigrateAVSessionProxy->ProcessMetaData(jsonValue);
202 }
203
204 /**
205 * @tc.name: ProcessPlaybackState001
206 * @tc.desc: test the member of ProcessPlaybackState
207 * @tc.type: FUNC
208 * @tc.require:
209 */
210 static HWTEST_F(MigrateAVSessionProxyTest, ProcessPlaybackState001, TestSize.Level1)
211 {
212 Json::Value jsonValue;
213 jsonValue[PLAYBACK_STATE] = PLAYBACK_STATE;
214 jsonValue[FAVOR_STATE] = FAVOR_STATE;
215 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
216 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
217 g_MigrateAVSessionProxy->ProcessPlaybackState(jsonValue);
218 }
219
220 /**
221 * @tc.name: ProcessPlaybackState002
222 * @tc.desc: test the member of ProcessPlaybackState
223 * @tc.type: FUNC
224 * @tc.require:
225 */
226 static HWTEST_F(MigrateAVSessionProxyTest, ProcessPlaybackState002, TestSize.Level1)
227 {
228 Json::Value jsonValue;
229 jsonValue["test"] = "test";
230 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
231 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
232 g_MigrateAVSessionProxy->ProcessPlaybackState(jsonValue);
233 }
234
235 /**
236 * @tc.name: ProcessValidCommands001
237 * @tc.desc: test the member of ProcessValidCommands
238 * @tc.type: FUNC
239 * @tc.require:
240 */
241 static HWTEST_F(MigrateAVSessionProxyTest, ProcessValidCommands001, TestSize.Level1)
242 {
243 Json::Value jsonValue;
244 jsonValue[VALID_COMMANDS] = VALID_COMMANDS;
245 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
246 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
247 g_MigrateAVSessionProxy->ProcessValidCommands(jsonValue);
248 }
249
250 /**
251 * @tc.name: ProcessValidCommands002
252 * @tc.desc: test the member of ProcessValidCommands
253 * @tc.type: FUNC
254 * @tc.require:
255 */
256 static HWTEST_F(MigrateAVSessionProxyTest, ProcessValidCommands002, TestSize.Level1)
257 {
258 Json::Value jsonValue;
259 jsonValue["test"] = "test";
260 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
261 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
262 g_MigrateAVSessionProxy->ProcessValidCommands(jsonValue);
263 }
264
265 /**
266 * @tc.name: ProcessVolumeControlCommand001
267 * @tc.desc: test the member of ProcessVolumeControlCommand
268 * @tc.type: FUNC
269 * @tc.require:
270 */
271 static HWTEST_F(MigrateAVSessionProxyTest, ProcessVolumeControlCommand001, TestSize.Level1)
272 {
273 Json::Value jsonValue;
274 jsonValue[AUDIO_VOLUME] = AUDIO_VOLUME;
275 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
276 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
277 g_MigrateAVSessionProxy->ProcessVolumeControlCommand(jsonValue);
278 }
279
280 /**
281 * @tc.name: ProcessVolumeControlCommand002
282 * @tc.desc: test the member of ProcessVolumeControlCommand
283 * @tc.type: FUNC
284 * @tc.require:
285 */
286 static HWTEST_F(MigrateAVSessionProxyTest, ProcessVolumeControlCommand002, TestSize.Level1)
287 {
288 Json::Value jsonValue;
289 jsonValue["test"] = "test";
290 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
291 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
292 g_MigrateAVSessionProxy->ProcessVolumeControlCommand(jsonValue);
293 }
294
295 /**
296 * @tc.name: ProcessBundleImg001
297 * @tc.desc: test the member of ProcessBundleImg
298 * @tc.type: FUNC
299 * @tc.require:
300 */
301 static HWTEST_F(MigrateAVSessionProxyTest, ProcessBundleImg001, TestSize.Level1)
302 {
303 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
304 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
305 std::string bundleIconStr = "";
306 g_MigrateAVSessionProxy->ProcessBundleImg(bundleIconStr);
307 }
308
309 /**
310 * @tc.name: ProcessBundleImg002
311 * @tc.desc: test the member of ProcessBundleImg
312 * @tc.type: FUNC
313 * @tc.require:
314 */
315 static HWTEST_F(MigrateAVSessionProxyTest, ProcessBundleImg002, TestSize.Level1)
316 {
317 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
318 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
319 std::string bundleIconStr = "test";
320 g_MigrateAVSessionProxy->ProcessBundleImg(bundleIconStr);
321 }
322
323 /**
324 * @tc.name: ProcessBundleImg003
325 * @tc.desc: test the member of ProcessBundleImg
326 * @tc.type: FUNC
327 * @tc.require:
328 */
329 static HWTEST_F(MigrateAVSessionProxyTest, ProcessBundleImg003, TestSize.Level1)
330 {
331 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
332 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
333 AVMetaData metaData;
334 g_MigrateAVSessionProxy->remoteSession_->SetAVMetaData(metaData);
335 std::string bundleIconStr = "test";
336 g_MigrateAVSessionProxy->ProcessBundleImg(bundleIconStr);
337 }
338
339 /**
340 * @tc.name: ProcessMediaImage001
341 * @tc.desc: test the member of ProcessMediaImage
342 * @tc.type: FUNC
343 * @tc.require:
344 */
345 static HWTEST_F(MigrateAVSessionProxyTest, ProcessMediaImage001, TestSize.Level1)
346 {
347 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
348 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
349 std::string bundleIconStr = "";
350 g_MigrateAVSessionProxy->ProcessMediaImage(bundleIconStr);
351 }
352
353 /**
354 * @tc.name: ProcessMediaImage002
355 * @tc.desc: test the member of ProcessMediaImage
356 * @tc.type: FUNC
357 * @tc.require:
358 */
359 static HWTEST_F(MigrateAVSessionProxyTest, ProcessMediaImage002, TestSize.Level1)
360 {
361 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
362 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
363 std::string bundleIconStr = "test";
364 g_MigrateAVSessionProxy->ProcessMediaImage(bundleIconStr);
365 }
366
367 /**
368 * @tc.name: ProcessMediaImage003
369 * @tc.desc: test the member of ProcessMediaImage
370 * @tc.type: FUNC
371 * @tc.require:
372 */
373 static HWTEST_F(MigrateAVSessionProxyTest, ProcessMediaImage003, TestSize.Level1)
374 {
375 g_MigrateAVSessionProxy->PrepareSessionFromRemote();
376 EXPECT_EQ(g_MigrateAVSessionProxy->remoteSession_ != nullptr, true);
377 AVMetaData metaData;
378 g_MigrateAVSessionProxy->remoteSession_->SetAVMetaData(metaData);
379 std::string bundleIconStr = "test";
380 g_MigrateAVSessionProxy->ProcessMediaImage(bundleIconStr);
381 }