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