• 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 "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 "avcontroller_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.Level1)
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.Level1)
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.Level1)
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.Level1)
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.Level1)
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.Level1)
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: GetPixelMapBuffer001
261  * @tc.desc: Test GetPixelMapBuffer
262  * @tc.type: FUNC
263  */
264 static HWTEST_F(AVControllerCallbackProxyTest, GetPixelMapBuffer001, testing::ext::TestSize.Level1)
265 {
266     SLOGI("GetPixelMapBuffer001, start");
267     OHOS::AVSession::AVMetaData metaData;
268     OHOS::MessageParcel parcel;
269     int32_t ret = aVControllerCallbackProxy->GetPixelMapBuffer(metaData, parcel);
270     EXPECT_EQ(ret, 0);
271     SLOGI("GetPixelMapBuffer001, end");
272 }
273 
274 /**
275  * @tc.name: GetPixelMapBuffer002
276  * @tc.desc: Test GetPixelMapBuffer
277  * @tc.type: FUNC
278  */
279 static HWTEST_F(AVControllerCallbackProxyTest, GetPixelMapBuffer002, testing::ext::TestSize.Level1)
280 {
281     SLOGI("GetPixelMapBuffer002, start");
282     OHOS::AVSession::AVMetaData metaData = GetAVMetaData();
283     OHOS::MessageParcel parcel;
284     int32_t ret = aVControllerCallbackProxy->GetPixelMapBuffer(metaData, parcel);
285 
286     int32_t mediaImageLength = 0;
287     std::vector<uint8_t> mediaImageBuffer;
288     std::shared_ptr<AVSessionPixelMap> mediaPixelMap = metaData.GetMediaImage();
289     if (mediaPixelMap != nullptr) {
290         mediaImageBuffer = mediaPixelMap->GetInnerImgBuffer();
291         mediaImageLength = static_cast<int32_t>(mediaImageBuffer.size());
292         metaData.SetMediaLength(mediaImageLength);
293     }
294 
295     int32_t avQueueImageLength = 0;
296     std::vector<uint8_t> avQueueImageBuffer;
297     std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaData.GetAVQueueImage();
298     if (avQueuePixelMap != nullptr) {
299         avQueueImageBuffer = avQueuePixelMap->GetInnerImgBuffer();
300         avQueueImageLength = static_cast<int32_t>(avQueueImageBuffer.size());
301     }
302 
303     int32_t retExpect = mediaImageLength + avQueueImageLength;
304     EXPECT_EQ(ret, retExpect);
305 
306     SLOGI("GetPixelMapBuffer002, end");
307 }
308 
309 /**
310  * @tc.name: GetPixelMapBuffer003
311  * @tc.desc: Test GetPixelMapBuffer
312  * @tc.type: FUNC
313  */
314 static HWTEST_F(AVControllerCallbackProxyTest, GetPixelMapBuffer003, testing::ext::TestSize.Level1)
315 {
316     SLOGI("GetPixelMapBuffer003, start");
317     OHOS::AVSession::AVMetaData metaData = GetAVMetaData();
318     std::shared_ptr<AVSessionPixelMap> avQueuePixelMapSet = std::make_shared<AVSessionPixelMap>();
319     std::vector<uint8_t> imgBuffer = {0, 1, 0, 1};
320     avQueuePixelMapSet->SetInnerImgBuffer(imgBuffer);
321     metaData.SetAVQueueImage(avQueuePixelMapSet);
322 
323     OHOS::MessageParcel parcel;
324     int32_t ret = aVControllerCallbackProxy->GetPixelMapBuffer(metaData, parcel);
325 
326     int32_t mediaImageLength = 0;
327     std::vector<uint8_t> mediaImageBuffer;
328     std::shared_ptr<AVSessionPixelMap> mediaPixelMap = metaData.GetMediaImage();
329     if (mediaPixelMap != nullptr) {
330         mediaImageBuffer = mediaPixelMap->GetInnerImgBuffer();
331         mediaImageLength = static_cast<int>(mediaImageBuffer.size());
332         metaData.SetMediaLength(mediaImageLength);
333     }
334 
335     int32_t avQueueImageLength = 0;
336     std::vector<uint8_t> avQueueImageBuffer;
337     std::shared_ptr<AVSessionPixelMap> avQueuePixelMapGet = metaData.GetAVQueueImage();
338     if (avQueuePixelMapGet != nullptr) {
339         avQueueImageBuffer = avQueuePixelMapGet->GetInnerImgBuffer();
340         avQueueImageLength = static_cast<int>(avQueueImageBuffer.size());
341     }
342 
343     int32_t retExpect = mediaImageLength + avQueueImageLength;
344 
345     EXPECT_EQ(ret, retExpect);
346 
347     SLOGI("GetPixelMapBuffer003, end");
348 }
349 
350 /**
351  * @tc.name: OnActiveStateChange001
352  * @tc.desc: Test OnActiveStateChange
353  * @tc.type: FUNC
354  */
355 static HWTEST_F(AVControllerCallbackProxyTest, OnActiveStateChange001, testing::ext::TestSize.Level1)
356 {
357     SLOGI("OnActiveStateChange001, start");
358     LOG_SetCallback(MyLogCallback);
359     bool isActive = true;
360     aVControllerCallbackProxy->OnActiveStateChange(isActive);
361     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
362     SLOGI("OnActiveStateChange001, end");
363 }
364 
365 /**
366  * @tc.name: OnActiveStateChange002
367  * @tc.desc: Test OnActiveStateChange
368  * @tc.type: FUNC
369  */
370  static HWTEST_F(AVControllerCallbackProxyTest, OnActiveStateChange002, testing::ext::TestSize.Level1)
371 {
372     SLOGI("OnActiveStateChange002, start");
373     LOG_SetCallback(MyLogCallback);
374     bool isActive = false;
375     aVControllerCallbackProxy->OnActiveStateChange(isActive);
376     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
377     SLOGI("OnActiveStateChange002, end");
378 }
379 
380 /**
381  * @tc.name: OnValidCommandChange001
382  * @tc.desc: Test OnValidCommandChange
383  * @tc.type: FUNC
384  */
385 static HWTEST_F(AVControllerCallbackProxyTest, OnValidCommandChange001, testing::ext::TestSize.Level1)
386 {
387     SLOGI("OnValidCommandChange001, start");
388     LOG_SetCallback(MyLogCallback);
389     std::vector<int32_t> cmds = {0};
390     aVControllerCallbackProxy->OnValidCommandChange(cmds);
391     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
392     SLOGI("OnValidCommandChange001, end");
393 }
394 
395 /**
396  * @tc.name: OnOutputDeviceChange001
397  * @tc.desc: Test OnOutputDeviceChange
398  * @tc.type: FUNC
399  */
400 static HWTEST_F(AVControllerCallbackProxyTest, OnOutputDeviceChange001, testing::ext::TestSize.Level1)
401 {
402     SLOGI("OnOutputDeviceChange001, start");
403     LOG_SetCallback(MyLogCallback);
404     int32_t connectionState = 0;
405     OHOS::AVSession::OutputDeviceInfo outputDeviceInfo;
406     aVControllerCallbackProxy->OnOutputDeviceChange(connectionState, outputDeviceInfo);
407     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
408     SLOGI("OnOutputDeviceChange001, end");
409 }
410 
411 /**
412  * @tc.name: OnSessionEventChange001
413  * @tc.desc: Test OnSessionEventChange
414  * @tc.type: FUNC
415  */
416 static HWTEST_F(AVControllerCallbackProxyTest, OnSessionEventChange001, testing::ext::TestSize.Level1)
417 {
418     SLOGI("OnSessionEventChange001, start");
419     LOG_SetCallback(MyLogCallback);
420     std::string event = "";
421     OHOS::AAFwk::WantParams args;
422     aVControllerCallbackProxy->OnSessionEventChange(event, args);
423     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
424     SLOGI("OnSessionEventChange001, end");
425 }
426 
427 /**
428  * @tc.name: OnQueueItemsChange001
429  * @tc.desc: Test OnQueueItemsChange
430  * @tc.type: FUNC
431  */
432 static HWTEST_F(AVControllerCallbackProxyTest, OnQueueItemsChange001, testing::ext::TestSize.Level1)
433 {
434     SLOGI("OnQueueItemsChange001, start");
435     LOG_SetCallback(MyLogCallback);
436     std::vector<AVQueueItem> items = {};
437     aVControllerCallbackProxy->OnQueueItemsChange(items);
438     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
439     SLOGI("OnQueueItemsChange001, end");
440 }
441 
442 /**
443  * @tc.name: OnQueueTitleChange001
444  * @tc.desc: Test OnQueueTitleChange
445  * @tc.type: FUNC
446  */
447 static HWTEST_F(AVControllerCallbackProxyTest, OnQueueTitleChange001, testing::ext::TestSize.Level1)
448 {
449     SLOGI("OnQueueTitleChange001, start");
450     LOG_SetCallback(MyLogCallback);
451     std::string title = "title";
452     aVControllerCallbackProxy->OnQueueTitleChange(title);
453     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
454     SLOGI("OnQueueTitleChange001, end");
455 }
456 
457 /**
458  * @tc.name: OnExtrasChange001
459  * @tc.desc: Test OnExtrasChange
460  * @tc.type: FUNC
461  */
462 static HWTEST_F(AVControllerCallbackProxyTest, OnExtrasChange001, testing::ext::TestSize.Level1)
463 {
464     SLOGI("OnExtrasChange001, start");
465     LOG_SetCallback(MyLogCallback);
466     OHOS::AAFwk::WantParams extras;
467     aVControllerCallbackProxy->OnExtrasChange(extras);
468     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
469     SLOGI("OnExtrasChange001, end");
470 }