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 "avsession_log.h"
18 #include "avsession_errors.h"
19 #include "accesstoken_kit.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22 #include "iservice_registry.h"
23 #include "avmedia_description.h"
24 #include "av_file_descriptor.h"
25 #include "system_ability_definition.h"
26 #include "avsession_pixel_map.h"
27 #include "avsession_pixel_map_adapter.h"
28 #define private public
29 #define protected public
30 #include "av_controller_callback_proxy.h"
31 #undef protected
32 #undef private
33
34 using namespace OHOS::AVSession;
35 using namespace OHOS::Media;
36 using namespace OHOS::Security::AccessToken;
37
38 static uint64_t g_selfTokenId = 0;
39
40 static std::string g_errLog;
41
MyLogCallback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)42 static void MyLogCallback(const LogType type, const LogLevel level,
43 const unsigned int domain, const char *tag, const char *msg)
44 {
45 g_errLog = msg;
46 }
47
48 static HapInfoParams g_info = {
49 .userID = 100,
50 .bundleName = "ohos.permission_test.demo",
51 .instIndex = 0,
52 .appIDDesc = "ohos.permission_test.demo",
53 .isSystemApp = true
54 };
55
56 static HapPolicyParams g_policy = {
57 .apl = APL_NORMAL,
58 .domain = "test.domain",
59 .permList = {
60 {
61 .permissionName = "ohos.permission.MANAGE_MEDIA_RESOURCES",
62 .bundleName = "ohos.permission_test.demo",
63 .grantMode = 1,
64 .availableLevel = APL_NORMAL,
65 .label = "label",
66 .labelId = 1,
67 .description = "test",
68 .descriptionId = 1
69 }
70 },
71 .permStateList = {
72 {
73 .permissionName = "ohos.permission.MANAGE_MEDIA_RESOURCES",
74 .isGeneral = true,
75 .resDeviceID = {"local"},
76 .grantStatus = {PermissionState::PERMISSION_GRANTED},
77 .grantFlags = {1}
78 }
79 }
80 };
81
82 class AVControllerCallbackProxyTest : public testing::Test {
83 public:
84 static void SetUpTestCase(void);
85 static void TearDownTestCase(void);
86 void SetUp();
87 void TearDown();
88
89 OHOS::sptr<AVControllerCallbackProxy> aVControllerCallbackProxy;
90 };
91
SetUpTestCase()92 void AVControllerCallbackProxyTest::SetUpTestCase()
93 {
94 g_selfTokenId = GetSelfTokenID();
95 AccessTokenKit::AllocHapToken(g_info, g_policy);
96 AccessTokenIDEx tokenID = AccessTokenKit::GetHapTokenIDEx(g_info.userID, g_info.bundleName, g_info.instIndex);
97 SetSelfTokenID(tokenID.tokenIDEx);
98 }
99
TearDownTestCase()100 void AVControllerCallbackProxyTest::TearDownTestCase()
101 {
102 SetSelfTokenID(g_selfTokenId);
103 auto tokenId = AccessTokenKit::GetHapTokenID(g_info.userID, g_info.bundleName, g_info.instIndex);
104 AccessTokenKit::DeleteToken(tokenId);
105 }
106
SetUp()107 void AVControllerCallbackProxyTest::SetUp()
108 {
109 auto mgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
110 if (mgr == nullptr) {
111 SLOGI("failed to get sa mgr");
112 return;
113 }
114 auto object = mgr->GetSystemAbility(OHOS::AVSESSION_SERVICE_ID);
115 if (object == nullptr) {
116 SLOGI("failed to get service");
117 return;
118 }
119 aVControllerCallbackProxy = OHOS::iface_cast<AVControllerCallbackProxy>(object);
120 }
121
TearDown()122 void AVControllerCallbackProxyTest::TearDown()
123 {
124 }
125
CreatePixelMap()126 static std::shared_ptr<PixelMap> CreatePixelMap()
127 {
128 int32_t pixelMapWidth = 4;
129 int32_t pixelMapHeight = 3;
130 const std::shared_ptr<PixelMap> &pixelMap = std::make_shared<PixelMap>();
131 ImageInfo info;
132 info.size.width = pixelMapWidth;
133 info.size.height = pixelMapHeight;
134 info.pixelFormat = PixelFormat::RGB_888;
135 info.colorSpace = ColorSpace::SRGB;
136 pixelMap->SetImageInfo(info);
137 int32_t rowDataSize = pixelMapWidth;
138 uint32_t bufferSize = rowDataSize * pixelMapHeight;
139 if (bufferSize <= 0) {
140 return pixelMap;
141 }
142
143 std::vector<std::uint8_t> buffer(bufferSize, 0x03);
144 pixelMap->SetPixelsAddr(buffer.data(), nullptr, bufferSize, AllocatorType::CUSTOM_ALLOC, nullptr);
145
146 return pixelMap;
147 }
148
149 static const int32_t DURATION_TIME = 40000;
GetAVMetaData()150 static OHOS::AVSession::AVMetaData GetAVMetaData()
151 {
152 OHOS::AVSession::AVMetaData g_metaData;
153 g_metaData.Reset();
154 g_metaData.SetAssetId("123");
155 g_metaData.SetTitle("Black Humor");
156 g_metaData.SetArtist("zhoujielun");
157 g_metaData.SetAuthor("zhoujielun");
158 g_metaData.SetAlbum("Jay");
159 g_metaData.SetWriter("zhoujielun");
160 g_metaData.SetComposer("zhoujielun");
161 g_metaData.SetDuration(DURATION_TIME);
162 g_metaData.SetMediaImageUri("xxxxx");
163 g_metaData.SetSubTitle("fac");
164 g_metaData.SetDescription("for friends");
165 g_metaData.SetLyric("xxxxx");
166 g_metaData.SetMediaImage(AVSessionPixelMapAdapter::ConvertToInner(CreatePixelMap()));
167 return g_metaData;
168 }
169
170 /**
171 * @tc.name: OnSessionDestroy001
172 * @tc.desc: Test OnSessionDestroy
173 * @tc.type: FUNC
174 */
175 static HWTEST_F(AVControllerCallbackProxyTest, OnSessionDestroy001, testing::ext::TestSize.Level0)
176 {
177 SLOGI("OnSessionDestroy001, start");
178 LOG_SetCallback(MyLogCallback);
179 aVControllerCallbackProxy->OnSessionDestroy();
180 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
181 SLOGI("OnSessionDestroy001, end");
182 }
183
184 /**
185 * @tc.name: OnAVCallMetaDataChange001
186 * @tc.desc: Test OnAVCallMetaDataChange
187 * @tc.type: FUNC
188 */
189 static HWTEST_F(AVControllerCallbackProxyTest, OnAVCallMetaDataChange001, testing::ext::TestSize.Level0)
190 {
191 SLOGI("OnAVCallMetaDataChange001, start");
192 LOG_SetCallback(MyLogCallback);
193 OHOS::AVSession::AVCallMetaData data;
194 aVControllerCallbackProxy->OnAVCallMetaDataChange(data);
195 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
196 SLOGI("OnAVCallMetaDataChange001, end");
197 }
198
199 /**
200 * @tc.name: OnAVCallStateChange001
201 * @tc.desc: Test OnAVCallStateChange
202 * @tc.type: FUNC
203 */
204 static HWTEST_F(AVControllerCallbackProxyTest, OnAVCallStateChange001, testing::ext::TestSize.Level0)
205 {
206 SLOGI("OnAVCallStateChange001, start");
207 LOG_SetCallback(MyLogCallback);
208 OHOS::AVSession::AVCallState data;
209 aVControllerCallbackProxy->OnAVCallStateChange(data);
210 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
211 SLOGI("OnAVCallStateChange001, end");
212 }
213
214 /**
215 * @tc.name: OnPlaybackStateChange001
216 * @tc.desc: Test OnPlaybackStateChange
217 * @tc.type: FUNC
218 */
219 static HWTEST_F(AVControllerCallbackProxyTest, OnPlaybackStateChange001, testing::ext::TestSize.Level0)
220 {
221 SLOGI("OnAVCallStateChange001, start");
222 LOG_SetCallback(MyLogCallback);
223 OHOS::AVSession::AVPlaybackState data;
224 aVControllerCallbackProxy->OnPlaybackStateChange(data);
225 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
226 SLOGI("OnAVCallStateChange001, end");
227 }
228
229 /**
230 * @tc.name: OnMetaDataChange001
231 * @tc.desc: Test OnMetaDataChange
232 * @tc.type: FUNC
233 */
234 static HWTEST_F(AVControllerCallbackProxyTest, OnMetaDataChange001, testing::ext::TestSize.Level0)
235 {
236 SLOGI("OnMetaDataChange001, start");
237 LOG_SetCallback(MyLogCallback);
238 OHOS::AVSession::AVMetaData data;
239 aVControllerCallbackProxy->OnMetaDataChange(data);
240 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
241 SLOGI("OnMetaDataChange001, end");
242 }
243
244 /**
245 * @tc.name: OnMetaDataChange001
246 * @tc.desc: Test OnMetaDataChange
247 * @tc.type: FUNC
248 */
249 static HWTEST_F(AVControllerCallbackProxyTest, OnMetaDataChange002, testing::ext::TestSize.Level0)
250 {
251 SLOGI("OnMetaDataChange002, start");
252 LOG_SetCallback(MyLogCallback);
253 OHOS::AVSession::AVMetaData data = GetAVMetaData();
254 aVControllerCallbackProxy->OnMetaDataChange(data);
255 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
256 SLOGI("OnMetaDataChange002, end");
257 }
258
259 /**
260 * @tc.name: OnActiveStateChange001
261 * @tc.desc: Test OnActiveStateChange
262 * @tc.type: FUNC
263 */
264 static HWTEST_F(AVControllerCallbackProxyTest, OnActiveStateChange001, testing::ext::TestSize.Level0)
265 {
266 SLOGI("OnActiveStateChange001, start");
267 LOG_SetCallback(MyLogCallback);
268 bool isActive = true;
269 aVControllerCallbackProxy->OnActiveStateChange(isActive);
270 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
271 SLOGI("OnActiveStateChange001, end");
272 }
273
274 /**
275 * @tc.name: OnActiveStateChange002
276 * @tc.desc: Test OnActiveStateChange
277 * @tc.type: FUNC
278 */
279 static HWTEST_F(AVControllerCallbackProxyTest, OnActiveStateChange002, testing::ext::TestSize.Level0)
280 {
281 SLOGI("OnActiveStateChange002, start");
282 LOG_SetCallback(MyLogCallback);
283 bool isActive = false;
284 aVControllerCallbackProxy->OnActiveStateChange(isActive);
285 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
286 SLOGI("OnActiveStateChange002, end");
287 }
288
289 /**
290 * @tc.name: OnValidCommandChange001
291 * @tc.desc: Test OnValidCommandChange
292 * @tc.type: FUNC
293 */
294 static HWTEST_F(AVControllerCallbackProxyTest, OnValidCommandChange001, testing::ext::TestSize.Level0)
295 {
296 SLOGI("OnValidCommandChange001, start");
297 LOG_SetCallback(MyLogCallback);
298 std::vector<int32_t> cmds = {0};
299 aVControllerCallbackProxy->OnValidCommandChange(cmds);
300 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
301 SLOGI("OnValidCommandChange001, end");
302 }
303
304 /**
305 * @tc.name: OnOutputDeviceChange001
306 * @tc.desc: Test OnOutputDeviceChange
307 * @tc.type: FUNC
308 */
309 static HWTEST_F(AVControllerCallbackProxyTest, OnOutputDeviceChange001, testing::ext::TestSize.Level0)
310 {
311 SLOGI("OnOutputDeviceChange001, start");
312 LOG_SetCallback(MyLogCallback);
313 int32_t connectionState = 0;
314 OHOS::AVSession::OutputDeviceInfo outputDeviceInfo;
315 aVControllerCallbackProxy->OnOutputDeviceChange(connectionState, outputDeviceInfo);
316 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
317 SLOGI("OnOutputDeviceChange001, end");
318 }
319
320 /**
321 * @tc.name: OnOutputDeviceChange002
322 * @tc.desc: Test OnOutputDeviceChange
323 * @tc.type: FUNC
324 */
325 static HWTEST_F(AVControllerCallbackProxyTest, OnOutputDeviceChange002, testing::ext::TestSize.Level0)
326 {
327 SLOGI("OnOutputDeviceChange002, start");
328 LOG_SetCallback(MyLogCallback);
329 int32_t connectionState = 0;
330 OHOS::AVSession::OutputDeviceInfo outputDeviceInfo;
331 DeviceInfo deviceInfo;
332 deviceInfo.deviceId_ = "1";
333 outputDeviceInfo.deviceInfos_.push_back(deviceInfo);
334 aVControllerCallbackProxy->OnOutputDeviceChange(connectionState, outputDeviceInfo);
335 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
336 SLOGI("OnOutputDeviceChange002, end");
337 }
338
339 /**
340 * @tc.name: OnSessionEventChange001
341 * @tc.desc: Test OnSessionEventChange
342 * @tc.type: FUNC
343 */
344 static HWTEST_F(AVControllerCallbackProxyTest, OnSessionEventChange001, testing::ext::TestSize.Level0)
345 {
346 SLOGI("OnSessionEventChange001, start");
347 LOG_SetCallback(MyLogCallback);
348 std::string event = "";
349 OHOS::AAFwk::WantParams args;
350 aVControllerCallbackProxy->OnSessionEventChange(event, args);
351 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
352 SLOGI("OnSessionEventChange001, end");
353 }
354
355 /**
356 * @tc.name: OnQueueItemsChange001
357 * @tc.desc: Test OnQueueItemsChange
358 * @tc.type: FUNC
359 */
360 static HWTEST_F(AVControllerCallbackProxyTest, OnQueueItemsChange001, testing::ext::TestSize.Level0)
361 {
362 SLOGI("OnQueueItemsChange001, start");
363 LOG_SetCallback(MyLogCallback);
364 std::vector<AVQueueItem> items = {};
365 aVControllerCallbackProxy->OnQueueItemsChange(items);
366 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
367 SLOGI("OnQueueItemsChange001, end");
368 }
369
370 /**
371 * @tc.name: OnQueueTitleChange001
372 * @tc.desc: Test OnQueueTitleChange
373 * @tc.type: FUNC
374 */
375 static HWTEST_F(AVControllerCallbackProxyTest, OnQueueTitleChange001, testing::ext::TestSize.Level0)
376 {
377 SLOGI("OnQueueTitleChange001, start");
378 LOG_SetCallback(MyLogCallback);
379 std::string title = "title";
380 aVControllerCallbackProxy->OnQueueTitleChange(title);
381 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
382 SLOGI("OnQueueTitleChange001, end");
383 }
384
385 /**
386 * @tc.name: OnExtrasChange001
387 * @tc.desc: Test OnExtrasChange
388 * @tc.type: FUNC
389 */
390 static HWTEST_F(AVControllerCallbackProxyTest, OnExtrasChange001, testing::ext::TestSize.Level0)
391 {
392 SLOGI("OnExtrasChange001, start");
393 LOG_SetCallback(MyLogCallback);
394 OHOS::AAFwk::WantParams extras;
395 aVControllerCallbackProxy->OnExtrasChange(extras);
396 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
397 SLOGI("OnExtrasChange001, end");
398 }
399
400 /**
401 * @tc.name: OnCustomData001
402 * @tc.desc: Test OnCustomData
403 * @tc.type: FUNC
404 */
405 static HWTEST_F(AVControllerCallbackProxyTest, OnCustomData001, testing::ext::TestSize.Level0)
406 {
407 SLOGI("OnCustomData001, start");
408 LOG_SetCallback(MyLogCallback);
409 OHOS::AAFwk::WantParams extras;
410 aVControllerCallbackProxy->OnCustomData(extras);
411 EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
412 SLOGI("OnCustomData001, end");
413 }