• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "accesstoken_kit.h"
19 #include "bool_wrapper.h"
20 #include "string_wrapper.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23 #include "want_agent.h"
24 #include "avcast_control_command.h"
25 #include "avcast_controller_item.h"
26 #include "avmedia_description.h"
27 #include "avmeta_data.h"
28 #include "avplayback_state.h"
29 #include "avqueue_item.h"
30 #include "avsession_manager.h"
31 #include "avsession_errors.h"
32 #include "avsession_log.h"
33 #include "hw_cast_stream_player.h"
34 #include "iavcast_controller.h"
35 #include "iservice_registry.h"
36 #include "system_ability_definition.h"
37 
38 #define private public
39 #include "hw_cast_provider.h"
40 #undef private
41 
42 using namespace testing::ext;
43 using namespace OHOS::Security::AccessToken;
44 
45 namespace OHOS {
46 namespace AVSession {
47 static int32_t g_onCall = AVSESSION_ERROR;
48 static int32_t g_sessionId = AVSESSION_ERROR;
49 static AVMetaData g_metaData;
50 static AVPlaybackState g_playbackState;
51 static char g_testSessionTag[] = "test";
52 static char g_testBundleName[] = "test.ohos.avsession";
53 static char g_testAbilityName[] = "test.ability";
54 static uint64_t g_selfTokenId = 0;
55 static std::string g_errLog;
56 sptr<AVCastControllerCallbackProxy> g_AVCastControllerCallbackProxy {nullptr};
57 
MyLogCallback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)58 void MyLogCallback(const LogType type, const LogLevel level,
59     const unsigned int domain, const char *tag, const char *msg)
60 {
61     g_errLog = msg;
62 }
63 
64 static HapInfoParams g_info = {
65     .userID = 100,
66     .bundleName = "ohos.permission_test.demo",
67     .instIndex = 0,
68     .appIDDesc = "ohos.permission_test.demo",
69     .isSystemApp = true
70 };
71 
72 static HapPolicyParams g_policy = {
73     .apl = APL_NORMAL,
74     .domain = "test.domain",
75     .permList = {
76         {
77             .permissionName = "ohos.permission.MANAGE_MEDIA_RESOURCES",
78             .bundleName = "ohos.permission_test.demo",
79             .grantMode = 1,
80             .availableLevel = APL_NORMAL,
81             .label = "label",
82             .labelId = 1,
83             .description = "test",
84             .descriptionId = 1
85         }
86     },
87     .permStateList = {
88         {
89             .permissionName = "ohos.permission.MANAGE_MEDIA_RESOURCES",
90             .isGeneral = true,
91             .resDeviceID = { "local" },
92             .grantStatus = { PermissionState::PERMISSION_GRANTED },
93             .grantFlags = { 1 }
94         }
95     }
96 };
97 
98 class AVCastControllerTest : public testing::Test {
99 public:
100     static void SetUpTestCase();
101     static void TearDownTestCase();
102     void SetUp() override;
103     void TearDown() override;
104 
105     std::shared_ptr<AVSession> avsession_ = nullptr;
106     std::shared_ptr<AVSessionController> controller_ = nullptr;
107     std::shared_ptr<AVCastControllerItem> castController_ = std::make_shared<AVCastControllerItem>();
108     std::vector<int32_t> supportedCastCmd_;
109 
110     static constexpr int SESSION_LEN = 64;
111 };
112 
SetUpTestCase()113 void AVCastControllerTest::SetUpTestCase()
114 {
115     g_selfTokenId = GetSelfTokenID();
116     AccessTokenKit::AllocHapToken(g_info, g_policy);
117     AccessTokenIDEx tokenID = AccessTokenKit::GetHapTokenIDEx(g_info.userID, g_info.bundleName, g_info.instIndex);
118     SetSelfTokenID(tokenID.tokenIDEx);
119 
120     auto mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
121     if (mgr == nullptr) {
122         SLOGI("failed to get sa mgr");
123         return;
124     }
125     auto object = mgr->GetSystemAbility(OHOS::AVSESSION_SERVICE_ID);
126     if (object == nullptr) {
127         SLOGI("failed to get service");
128         return;
129     }
130     g_AVCastControllerCallbackProxy = iface_cast<AVCastControllerCallbackProxy>(object);
131     ASSERT_TRUE(g_AVCastControllerCallbackProxy != nullptr);
132 }
133 
TearDownTestCase()134 void AVCastControllerTest::TearDownTestCase()
135 {
136     SetSelfTokenID(g_selfTokenId);
137     auto tokenId = AccessTokenKit::GetHapTokenID(g_info.userID, g_info.bundleName, g_info.instIndex);
138     AccessTokenKit::DeleteToken(tokenId);
139 }
140 
SetUp()141 void AVCastControllerTest::SetUp()
142 {
143     OHOS::AppExecFwk::ElementName elementName;
144     elementName.SetBundleName(g_testBundleName);
145     elementName.SetAbilityName(g_testAbilityName);
146     avsession_ = AVSessionManager::GetInstance().CreateSession(g_testSessionTag, AVSession::SESSION_TYPE_AUDIO,
147                                                                elementName);
148     ASSERT_NE(avsession_, nullptr);
149     g_sessionId++;
150     auto ret = AVSessionManager::GetInstance().CreateController(avsession_->GetSessionId(), controller_);
151     ASSERT_EQ(ret, AVSESSION_SUCCESS);
152     ASSERT_NE(controller_, nullptr);
153 
154     std::shared_ptr<HwCastStreamPlayer> HwCastStreamPlayer_ = std::make_shared<HwCastStreamPlayer>(nullptr);
155     auto validCallback = [this](int32_t cmd, std::vector<int32_t>& supportedCastCmds) {
156         SLOGI("add cast valid command %{public}d", cmd);
157         supportedCastCmds = supportedCastCmd_;
158     };
159     auto preparecallback = []() {
160         SLOGI("prepare callback");
161     };
162     castController_->Init(HwCastStreamPlayer_, validCallback, preparecallback);
163 }
164 
TearDown()165 void AVCastControllerTest::TearDown()
166 {
167     [[maybe_unused]] int32_t ret = AVSESSION_ERROR;
168     if (avsession_ != nullptr) {
169         ret = avsession_->Destroy();
170         avsession_ = nullptr;
171     }
172     if (controller_ != nullptr) {
173         ret = controller_->Destroy();
174         controller_ = nullptr;
175     }
176     g_onCall = AVSESSION_ERROR;
177 }
178 
179 class AVCastControllerCallbackImpl : public AVCastControllerCallback {
180 public:
181     void OnCastPlaybackStateChange(const AVPlaybackState& state) override;
182 
183     void OnMediaItemChange(const AVQueueItem& avQueueItem) override;
184 
185     void OnPlayNext() override;
186 
187     void OnPlayPrevious() override;
188 
189     void OnSeekDone(const int32_t seekNumber) override;
190 
191     void OnVideoSizeChange(const int32_t width, const int32_t height) override;
192 
193     void OnPlayerError(const int32_t errorCode, const std::string& errorMsg) override;
194 
195     void OnCastValidCommandChanged(const std::vector<int32_t> &cmds) override;
196 
197     int32_t onDataSrcRead(const std::shared_ptr<AVSharedMemoryBase>& mem,
198                           uint32_t length, int64_t pos, int32_t& result) override;
199 
200     ~AVCastControllerCallbackImpl() override;
201 
202     AVPlaybackState state_;
203     AVQueueItem avQueueItem_;
204     int32_t seekNumber_;
205     int32_t width_;
206     int32_t height_;
207     int32_t errorCode_;
208     std::string errorMsg_;
209 };
210 
~AVCastControllerCallbackImpl()211 AVCastControllerCallbackImpl::~AVCastControllerCallbackImpl()
212 {
213 }
214 
OnCastPlaybackStateChange(const AVPlaybackState & state)215 void AVCastControllerCallbackImpl::OnCastPlaybackStateChange(const AVPlaybackState& state)
216 {
217     state_ = state;
218 }
219 
OnMediaItemChange(const AVQueueItem & avQueueItem)220 void AVCastControllerCallbackImpl::OnMediaItemChange(const AVQueueItem& avQueueItem)
221 {
222     avQueueItem_ = avQueueItem;
223 }
224 
OnPlayNext()225 void AVCastControllerCallbackImpl::OnPlayNext()
226 {
227 }
228 
OnPlayPrevious()229 void AVCastControllerCallbackImpl::OnPlayPrevious()
230 {
231 }
232 
OnSeekDone(const int32_t seekNumber)233 void AVCastControllerCallbackImpl::OnSeekDone(const int32_t seekNumber)
234 {
235     seekNumber_ = seekNumber;
236 }
237 
OnVideoSizeChange(const int32_t width,const int32_t height)238 void AVCastControllerCallbackImpl::OnVideoSizeChange(const int32_t width, const int32_t height)
239 {
240     width_ = width;
241     height_ = height;
242 }
243 
OnPlayerError(const int32_t errorCode,const std::string & errorMsg)244 void AVCastControllerCallbackImpl::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
245 {
246     errorCode_ = errorCode;
247     errorMsg_ = errorMsg;
248 }
249 
OnCastValidCommandChanged(const std::vector<int32_t> & cmds)250 void AVCastControllerCallbackImpl::OnCastValidCommandChanged(const std::vector<int32_t> &cmds)
251 {
252 }
253 
onDataSrcRead(const std::shared_ptr<AVSharedMemoryBase> & mem,uint32_t length,int64_t pos,int32_t & result)254 int32_t AVCastControllerCallbackImpl::onDataSrcRead(const std::shared_ptr<AVSharedMemoryBase>& mem,
255                                                     uint32_t length, int64_t pos, int32_t& result)
256 {
257     return 0;
258 }
259 
260 class AVCastControllerProxyMock : public IAVCastControllerProxy {
261 public:
Release()262     void Release() {};
RegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener> iAVCastControllerProxyListener)263     int32_t RegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener>
264         iAVCastControllerProxyListener) {return 0;}
UnRegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener> iAVCastControllerProxyListener)265     int32_t UnRegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener>
266         iAVCastControllerProxyListener) {return 0;}
GetCurrentItem()267     AVQueueItem GetCurrentItem() {return AVQueueItem();}
Start(const AVQueueItem & avQueueItem)268     int32_t Start(const AVQueueItem& avQueueItem) {return 0;}
Prepare(const AVQueueItem & avQueueItem)269     int32_t Prepare(const AVQueueItem& avQueueItem) {return 0;}
SendControlCommand(const AVCastControlCommand cmd)270     void SendControlCommand(const AVCastControlCommand cmd) {}
GetDuration(int32_t & duration)271     int32_t GetDuration(int32_t& duration) {return 0;}
GetCastAVPlaybackState(AVPlaybackState & avPlaybackState)272     int32_t GetCastAVPlaybackState(AVPlaybackState& avPlaybackState) {return 0;}
SetValidAbility(const std::vector<int32_t> & validAbilityList)273     int32_t SetValidAbility(const std::vector<int32_t>& validAbilityList) {return 0;}
GetValidAbility(std::vector<int32_t> & validAbilityList)274     int32_t GetValidAbility(std::vector<int32_t> &validAbilityList) {return 0;}
SetDisplaySurface(std::string & surfaceId)275     int32_t SetDisplaySurface(std::string& surfaceId) {return 0;}
ProcessMediaKeyResponse(const std::string & assetId,const std::vector<uint8_t> & response)276     int32_t ProcessMediaKeyResponse(const std::string& assetId, const std::vector<uint8_t>& response) {return 0;}
GetSupportedDecoders(std::vector<std::string> & decoderTypes)277     int32_t GetSupportedDecoders(std::vector<std::string>& decoderTypes) {return 0;}
GetRecommendedResolutionLevel(std::string & decoderType,ResolutionLevel & resolutionLevel)278     int32_t GetRecommendedResolutionLevel(std::string& decoderType, ResolutionLevel& resolutionLevel) {return 0;}
GetSupportedHdrCapabilities(std::vector<HDRFormat> & hdrFormats)279     int32_t GetSupportedHdrCapabilities(std::vector<HDRFormat>& hdrFormats) {return 0;}
GetSupportedPlaySpeeds(std::vector<float> & playSpeeds)280     int32_t GetSupportedPlaySpeeds(std::vector<float>& playSpeeds) {return 0;}
RefreshCurrentAVQueueItem(const AVQueueItem & avQueueItem)281     int32_t RefreshCurrentAVQueueItem(const AVQueueItem& avQueueItem) {return 0;}
SetSessionCallbackForCastCap(const std::function<void (bool,bool)> & callback)282     void SetSessionCallbackForCastCap(const std::function<void(bool, bool)>& callback) {}
SetSpid(uint32_t spid)283     void SetSpid(uint32_t spid) {}
284 };
285 
286 /**
287 * @tc.name: SendControlCommand001
288 * @tc.desc: send command, check if AVCastControlCommand is invalid
289 * @tc.type: FUNC
290 * @tc.require:
291 */
292 HWTEST_F(AVCastControllerTest, SendControlCommand001, TestSize.Level1)
293 {
294     AVCastControlCommand command;
295     EXPECT_EQ(command.SetCommand(AVCastControlCommand::CAST_CONTROL_CMD_INVALID), ERR_INVALID_PARAM);
296     EXPECT_EQ(command.SetCommand(AVCastControlCommand::CAST_CONTROL_CMD_MAX), ERR_INVALID_PARAM);
297     EXPECT_EQ(command.SetForwardTime(0), ERR_INVALID_PARAM);
298     EXPECT_EQ(command.SetRewindTime(-1), ERR_INVALID_PARAM);
299     EXPECT_EQ(command.SetSeekTime(-1), ERR_INVALID_PARAM);
300     EXPECT_EQ(command.SetSpeed(-1), ERR_INVALID_PARAM);
301     EXPECT_EQ(command.SetLoopMode(-1), ERR_INVALID_PARAM);
302 }
303 
304 /**
305 * @tc.name: SendControlCommand002
306 * @tc.desc: send command, check if AVCastControlCommand is invalid
307 * @tc.type: FUNC
308 * @tc.require:
309 */
310 HWTEST_F(AVCastControllerTest, SendControlCommand002, TestSize.Level1)
311 {
312     AVCastControlCommand command;
313     int32_t mode = -1;
314     OHOS::Parcel parcel;
315     EXPECT_EQ(command.SetCommand(AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE), AVSESSION_SUCCESS);
316     EXPECT_EQ(command.SetLoopMode(AVPlaybackState::LOOP_MODE_SEQUENCE), AVSESSION_SUCCESS);
317     EXPECT_EQ(command.Marshalling(parcel), true);
318     AVCastControlCommand *ret = AVCastControlCommand::Unmarshalling(parcel);
319     EXPECT_NE(ret, nullptr);
320     EXPECT_EQ(ret->GetCommand(), AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE);
321     EXPECT_EQ(ret->GetLoopMode(mode), AVSESSION_SUCCESS);
322     EXPECT_EQ(mode, AVPlaybackState::LOOP_MODE_SEQUENCE);
323     delete ret;
324     ret = nullptr;
325 }
326 
327 /**
328 * @tc.name: SendControlCommand003
329 * @tc.desc: send command, check if AVCastControlCommand is invalid
330 * @tc.type: FUNC
331 * @tc.require:
332 */
333 HWTEST_F(AVCastControllerTest, SendControlCommand003, TestSize.Level1)
334 {
335     AVCastControlCommand command;
336     int32_t speed = -1;
337     OHOS::Parcel parcel;
338     EXPECT_EQ(command.SetCommand(AVCastControlCommand::CAST_CONTROL_CMD_SET_SPEED), AVSESSION_SUCCESS);
339     EXPECT_EQ(command.SetSpeed(1), AVSESSION_SUCCESS);
340     EXPECT_EQ(command.Marshalling(parcel), true);
341     AVCastControlCommand *ret = AVCastControlCommand::Unmarshalling(parcel);
342     EXPECT_NE(ret, nullptr);
343     EXPECT_EQ(ret->GetCommand(), AVCastControlCommand::CAST_CONTROL_CMD_SET_SPEED);
344     EXPECT_EQ(ret->GetSpeed(speed), AVSESSION_SUCCESS);
345     EXPECT_EQ(speed, 1);
346     delete ret;
347     ret = nullptr;
348 }
349 
350 /**
351 * @tc.name: SendControlCommand004
352 * @tc.desc: send command, check if AVCastControlCommand is invalid
353 * @tc.type: FUNC
354 * @tc.require:
355 */
356 HWTEST_F(AVCastControllerTest, SendControlCommand004, TestSize.Level1)
357 {
358     AVCastControlCommand command;
359     int32_t volumn = -1;
360     OHOS::Parcel parcel;
361     EXPECT_EQ(command.SetCommand(AVCastControlCommand::CAST_CONTROL_CMD_SET_VOLUME), AVSESSION_SUCCESS);
362     EXPECT_EQ(command.SetVolume(1), AVSESSION_SUCCESS);
363     EXPECT_EQ(command.Marshalling(parcel), true);
364     AVCastControlCommand *ret = AVCastControlCommand::Unmarshalling(parcel);
365     EXPECT_NE(ret, nullptr);
366     EXPECT_EQ(ret->GetCommand(), AVCastControlCommand::CAST_CONTROL_CMD_SET_VOLUME);
367     EXPECT_EQ(ret->GetVolume(volumn), AVSESSION_SUCCESS);
368     EXPECT_EQ(volumn, 1);
369     delete ret;
370     ret = nullptr;
371 }
372 
373 /**
374 * @tc.name: SendControlCommand005
375 * @tc.desc: send command, check if AVCastControlCommand is invalid
376 * @tc.type: FUNC
377 * @tc.require:
378 */
379 HWTEST_F(AVCastControllerTest, SendControlCommand005, TestSize.Level1)
380 {
381     AVCastControlCommand command;
382     int32_t seek = -1;
383     OHOS::Parcel parcel;
384     EXPECT_EQ(command.SetCommand(AVCastControlCommand::CAST_CONTROL_CMD_SEEK), AVSESSION_SUCCESS);
385     EXPECT_EQ(command.SetSeekTime(1), AVSESSION_SUCCESS);
386     EXPECT_EQ(command.Marshalling(parcel), true);
387     AVCastControlCommand *ret = AVCastControlCommand::Unmarshalling(parcel);
388     EXPECT_NE(ret, nullptr);
389     EXPECT_EQ(ret->GetCommand(), AVCastControlCommand::CAST_CONTROL_CMD_SEEK);
390     EXPECT_EQ(ret->GetSeekTime(seek), AVSESSION_SUCCESS);
391     EXPECT_EQ(seek, 1);
392     delete ret;
393     ret = nullptr;
394 }
395 
396 /**
397 * @tc.name: SendControlCommand006
398 * @tc.desc: send command, check if AVCastControlCommand is invalid
399 * @tc.type: FUNC
400 * @tc.require:
401 */
402 HWTEST_F(AVCastControllerTest, SendControlCommand006, TestSize.Level1)
403 {
404     AVCastControlCommand command;
405     int32_t rewind = -1;
406     OHOS::Parcel parcel;
407     EXPECT_EQ(command.SetCommand(AVCastControlCommand::CAST_CONTROL_CMD_REWIND), AVSESSION_SUCCESS);
408     EXPECT_EQ(command.SetRewindTime(1), AVSESSION_SUCCESS);
409     EXPECT_EQ(command.Marshalling(parcel), true);
410     AVCastControlCommand *ret = AVCastControlCommand::Unmarshalling(parcel);
411     EXPECT_NE(ret, nullptr);
412     EXPECT_EQ(ret->GetCommand(), AVCastControlCommand::CAST_CONTROL_CMD_REWIND);
413     EXPECT_EQ(ret->GetRewindTime(rewind), AVSESSION_SUCCESS);
414     EXPECT_EQ(rewind, 1);
415     delete ret;
416     ret = nullptr;
417 }
418 
419 /**
420 * @tc.name: SendControlCommand007
421 * @tc.desc: send command, check if AVCastControlCommand is invalid
422 * @tc.type: FUNC
423 * @tc.require:
424 */
425 HWTEST_F(AVCastControllerTest, SendControlCommand007, TestSize.Level1)
426 {
427     AVCastControlCommand command;
428     int32_t forward = -1;
429     OHOS::Parcel parcel;
430     EXPECT_EQ(command.SetCommand(AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD), AVSESSION_SUCCESS);
431     EXPECT_EQ(command.SetForwardTime(1), AVSESSION_SUCCESS);
432     EXPECT_EQ(command.Marshalling(parcel), true);
433     AVCastControlCommand *ret = AVCastControlCommand::Unmarshalling(parcel);
434     EXPECT_NE(ret, nullptr);
435     EXPECT_EQ(ret->GetCommand(), AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD);
436     EXPECT_EQ(ret->GetForwardTime(forward), AVSESSION_SUCCESS);
437     EXPECT_EQ(forward, 1);
438     delete ret;
439     ret = nullptr;
440 }
441 
442 /**
443 * @tc.name: SendControlCommand008
444 * @tc.desc: send command, check if AVCastControlCommand is invalid
445 * @tc.type: FUNC
446 * @tc.require:
447 */
448 HWTEST_F(AVCastControllerTest, SendControlCommand008, TestSize.Level1)
449 {
450     AVCastControlCommand command;
451     EXPECT_EQ(castController_->SendControlCommand(command), AVSESSION_SUCCESS);
452 }
453 
454 /**
455 * @tc.name: Start001
456 * @tc.desc: Start
457 * @tc.type: FUNC
458 * @tc.require:
459 */
460 HWTEST_F(AVCastControllerTest, Start001, TestSize.Level1)
461 {
462     AVQueueItem avQueueItem;
463     std::shared_ptr<AVMediaDescription> description = std::make_shared<AVMediaDescription>();
464     description->SetMediaId("123");
465     description->SetTitle("Title");
466     description->SetSubtitle("Subtitle");
467     description->SetDescription("This is music description");
468     description->SetIcon(nullptr);
469     description->SetIconUri("xxxxx");
470     description->SetExtras(nullptr);
471     description->SetMediaUri("Media url");
472     avQueueItem.SetDescription(description);
473     EXPECT_EQ(castController_->Start(avQueueItem), AVSESSION_SUCCESS);
474 }
475 
476 /**
477 * @tc.name: Prepare001
478 * @tc.desc: Prepare
479 * @tc.type: FUNC
480 * @tc.require:
481 */
482 HWTEST_F(AVCastControllerTest, Prepare001, TestSize.Level1)
483 {
484     AVQueueItem avQueueItem;
485     std::shared_ptr<AVMediaDescription> description = std::make_shared<AVMediaDescription>();
486     description->SetMediaId("123");
487     description->SetTitle("Title");
488     description->SetSubtitle("Subtitle");
489     description->SetDescription("This is music description");
490     description->SetIcon(nullptr);
491     description->SetIconUri("xxxxx");
492     description->SetExtras(nullptr);
493     description->SetMediaUri("Media url");
494     avQueueItem.SetDescription(description);
495     EXPECT_EQ(castController_->Prepare(avQueueItem), AVSESSION_SUCCESS);
496 }
497 
498 /**
499 * @tc.name: GetDuration001
500 * @tc.desc: GetDuration
501 * @tc.type: FUNC
502 * @tc.require:
503 */
504 HWTEST_F(AVCastControllerTest, GetDuration001, TestSize.Level1)
505 {
506     int32_t duration;
507     EXPECT_EQ(castController_->GetDuration(duration), AVSESSION_ERROR);
508 }
509 
510 /**
511 * @tc.name: GetCastAVPlaybackState001
512 * @tc.desc: GetCastAVPlaybackState
513 * @tc.type: FUNC
514 * @tc.require:
515 */
516 HWTEST_F(AVCastControllerTest, GetCastAVPlaybackState001, TestSize.Level1)
517 {
518     AVPlaybackState avPlaybackState;
519     EXPECT_EQ(castController_->GetCastAVPlaybackState(avPlaybackState), AVSESSION_ERROR);
520 }
521 
522 /**
523 * @tc.name: GetCurrentItem001
524 * @tc.desc: GetCurrentItem
525 * @tc.type: FUNC
526 * @tc.require:
527 */
528 HWTEST_F(AVCastControllerTest, GetCurrentItem001, TestSize.Level1)
529 {
530     AVQueueItem currentItem;
531     EXPECT_EQ(castController_->GetCurrentItem(currentItem), AVSESSION_SUCCESS);
532 }
533 
534 /**
535 * @tc.name: GetValidCommands001
536 * @tc.desc: GetValidCommands
537 * @tc.type: FUNC
538 * @tc.require:
539 */
540 HWTEST_F(AVCastControllerTest, GetValidCommands001, TestSize.Level1)
541 {
542     std::vector<int32_t> cmds;
543     EXPECT_EQ(castController_->GetValidCommands(cmds), AVSESSION_SUCCESS);
544 }
545 
546 /**
547 * @tc.name: SetDisplaySurface001
548 * @tc.desc: SetDisplaySurface
549 * @tc.type: FUNC
550 * @tc.require:
551 */
552 HWTEST_F(AVCastControllerTest, SetDisplaySurface001, TestSize.Level1)
553 {
554     std::string surfaceId = "surfaceId";
555     EXPECT_EQ(castController_->SetDisplaySurface(surfaceId), AVSESSION_ERROR);
556 }
557 
558 /**
559 * @tc.name: ProcessMediaKeyResponse001
560 * @tc.desc: ProcessMediaKeyResponse
561 * @tc.type: FUNC
562 * @tc.require:
563 */
564 HWTEST_F(AVCastControllerTest, ProcessMediaKeyResponse001, TestSize.Level1)
565 {
566     std::string assetId = "assetId";
567     std::vector<uint8_t> response;
568     EXPECT_EQ(castController_->ProcessMediaKeyResponse(assetId, response), AVSESSION_ERROR);
569 }
570 
571 /**
572 * @tc.name: SetCastPlaybackFilter001
573 * @tc.desc: SetCastPlaybackFilter
574 * @tc.type: FUNC
575 * @tc.require:
576 */
577 HWTEST_F(AVCastControllerTest, SetCastPlaybackFilter001, TestSize.Level1)
578 {
579     AVPlaybackState::PlaybackStateMaskType filter;
580     EXPECT_EQ(castController_->SetCastPlaybackFilter(filter), AVSESSION_SUCCESS);
581 }
582 
583 /**
584 * @tc.name: AddAvailableCommand001
585 * @tc.desc: AddAvailableCommand
586 * @tc.type: FUNC
587 * @tc.require:
588 */
589 HWTEST_F(AVCastControllerTest, AddAvailableCommand001, TestSize.Level1)
590 {
591     EXPECT_EQ(castController_->AddAvailableCommand(0), AVSESSION_SUCCESS);
592 }
593 
594 /**
595 * @tc.name: RemoveAvailableCommand001
596 * @tc.desc: RemoveAvailableCommand
597 * @tc.type: FUNC
598 * @tc.require:
599 */
600 HWTEST_F(AVCastControllerTest, RemoveAvailableCommand001, TestSize.Level1)
601 {
602     EXPECT_EQ(castController_->AddAvailableCommand(0), AVSESSION_SUCCESS);
603     EXPECT_EQ(castController_->RemoveAvailableCommand(0), AVSESSION_SUCCESS);
604 }
605 
606 /**
607 * @tc.name: RegisterControllerListener001
608 * @tc.desc: RegisterControllerListener
609 * @tc.type: FUNC
610 * @tc.require:
611 */
612 HWTEST_F(AVCastControllerTest, RegisterControllerListener001, TestSize.Level1)
613 {
614     std::shared_ptr<IAVCastControllerProxy> castControllerProxy = nullptr;
615     EXPECT_EQ(castController_->RegisterControllerListener(castControllerProxy), true);
616 }
617 
618 /**
619 * @tc.name: Destroy001
620 * @tc.desc: Destroy
621 * @tc.type: FUNC
622 * @tc.require:
623 */
624 HWTEST_F(AVCastControllerTest, Destroy001, TestSize.Level1)
625 {
626     EXPECT_EQ(castController_->Destroy(), AVSESSION_SUCCESS);
627 }
628 
629 /**
630 * @tc.name: OnCastPlaybackStateChange001
631 * @tc.desc: OnCastPlaybackStateChange, no callback
632 * @tc.type: FUNC
633 * @tc.require:
634 */
635 HWTEST_F(AVCastControllerTest, OnCastPlaybackStateChange001, TestSize.Level1)
636 {
637     LOG_SetCallback(MyLogCallback);
638     AVPlaybackState state;
639     castController_->OnCastPlaybackStateChange(state);
640     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
641 }
642 
643 /**
644 * @tc.name: OnMediaItemChange001
645 * @tc.desc: OnMediaItemChange, no callback
646 * @tc.type: FUNC
647 * @tc.require:
648 */
649 HWTEST_F(AVCastControllerTest, OnMediaItemChange001, TestSize.Level1)
650 {
651     LOG_SetCallback(MyLogCallback);
652     AVQueueItem avQueueItem;
653     castController_->OnMediaItemChange(avQueueItem);
654     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
655 }
656 
657 /**
658 * @tc.name: OnPlayNext001
659 * @tc.desc: OnPlayNext, no callback
660 * @tc.type: FUNC
661 * @tc.require:
662 */
663 HWTEST_F(AVCastControllerTest, OnPlayNext001, TestSize.Level1)
664 {
665     LOG_SetCallback(MyLogCallback);
666     castController_->OnPlayNext();
667     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
668 }
669 
670 /**
671 * @tc.name: OnPlayPrevious001
672 * @tc.desc: OnPlayPrevious, no callback
673 * @tc.type: FUNC
674 * @tc.require:
675 */
676 HWTEST_F(AVCastControllerTest, OnPlayPrevious001, TestSize.Level1)
677 {
678     LOG_SetCallback(MyLogCallback);
679     castController_->OnPlayPrevious();
680     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
681 }
682 
683 /**
684 * @tc.name: OnSeekDone001
685 * @tc.desc: OnSeekDone, no callback
686 * @tc.type: FUNC
687 * @tc.require:
688 */
689 HWTEST_F(AVCastControllerTest, OnSeekDone001, TestSize.Level1)
690 {
691     LOG_SetCallback(MyLogCallback);
692     int32_t seekNumber = 0;
693     castController_->OnSeekDone(seekNumber);
694     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
695 }
696 
697 /**
698 * @tc.name: OnVideoSizeChange001
699 * @tc.desc: OnVideoSizeChange, no callback
700 * @tc.type: FUNC
701 * @tc.require:
702 */
703 HWTEST_F(AVCastControllerTest, OnVideoSizeChange001, TestSize.Level1)
704 {
705     LOG_SetCallback(MyLogCallback);
706     int32_t width = 0;
707     int32_t height = 0;
708     castController_->OnVideoSizeChange(width, height);
709     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
710 }
711 
712 /**
713 * @tc.name: OnPlayerError001
714 * @tc.desc: OnPlayerError, no callback
715 * @tc.type: FUNC
716 * @tc.require:
717 */
718 HWTEST_F(AVCastControllerTest, OnPlayerError001, TestSize.Level1)
719 {
720     LOG_SetCallback(MyLogCallback);
721     int32_t errorCode = 0;
722     std::string errorMsg = "errorMsg";
723     castController_->OnPlayerError(errorCode, errorMsg);
724     EXPECT_TRUE(g_errLog.find("xxx") == std::string::npos);
725 }
726 
727 /**
728 * @tc.name: StartCastDiscovery001
729 * @tc.desc: StartCastDiscovery
730 * @tc.type: FUNC
731 * @tc.require:
732 */
733 HWTEST_F(AVCastControllerTest, StartCastDiscovery001, TestSize.Level1)
734 {
735     EXPECT_EQ(AVSessionManager::GetInstance().StartCastDiscovery(1, {}), AVSESSION_SUCCESS);
736 }
737 
738 /**
739 * @tc.name: StopCastDiscovery001
740 * @tc.desc: StopCastDiscovery
741 * @tc.type: FUNC
742 * @tc.require:
743 */
744 HWTEST_F(AVCastControllerTest, StopCastDiscovery001, TestSize.Level1)
745 {
746     EXPECT_EQ(AVSessionManager::GetInstance().StopCastDiscovery(), AVSESSION_SUCCESS);
747 }
748 
749 /**
750 * @tc.name: SetDiscoverable001
751 * @tc.desc: SetDiscoverable
752 * @tc.type: FUNC
753 * @tc.require:
754 */
755 HWTEST_F(AVCastControllerTest, SetDiscoverable001, TestSize.Level1)
756 {
757     EXPECT_EQ(AVSessionManager::GetInstance().SetDiscoverable(true), AVSESSION_SUCCESS);
758 }
759 
760 /**
761 * @tc.name: StartCast001
762 * @tc.desc: StartCast
763 * @tc.type: FUNC
764 * @tc.require:
765 */
766 HWTEST_F(AVCastControllerTest, StartCast001, TestSize.Level1)
767 {
768     SessionToken sessionToken;
769     sessionToken.sessionId = avsession_->GetSessionId();
770     OutputDeviceInfo outputDeviceInfo;
771     DeviceInfo deviceInfo;
772     deviceInfo.castCategory_ = 1;
773     deviceInfo.deviceId_ = "deviceId";
774     outputDeviceInfo.deviceInfos_.push_back(deviceInfo);
775     EXPECT_EQ(AVSessionManager::GetInstance().StartCast(sessionToken, outputDeviceInfo), AVSESSION_ERROR);
776 }
777 
778 /**
779 * @tc.name: StopCast001
780 * @tc.desc: StopCast
781 * @tc.type: FUNC
782 * @tc.require:
783 */
784 HWTEST_F(AVCastControllerTest, StopCast001, TestSize.Level1)
785 {
786     SessionToken sessionToken;
787     sessionToken.sessionId = avsession_->GetSessionId();
788     EXPECT_EQ(AVSessionManager::GetInstance().StopCast(sessionToken), AVSESSION_SUCCESS);
789 }
790 
791 HWTEST_F(AVCastControllerTest, StartDiscovery001, TestSize.Level1)
792 {
793     HwCastProvider hwCastProvider;
794     std::vector<std::string> drmSchemes;
795     EXPECT_EQ(hwCastProvider.StartDiscovery(2, drmSchemes), true);
796 }
797 
798 HWTEST_F(AVCastControllerTest, StopDiscovery001, TestSize.Level1)
799 {
800     HwCastProvider hwCastProvider;
801     hwCastProvider.StopDiscovery();
802     EXPECT_TRUE(true);
803 }
804 
805 HWTEST_F(AVCastControllerTest, Release001, TestSize.Level1)
806 {
807     HwCastProvider hwCastProvider;
808     hwCastProvider.Release();
809     EXPECT_TRUE(true);
810 }
811 
812 HWTEST_F(AVCastControllerTest, StartCastSession001, TestSize.Level1)
813 {
814     HwCastProvider hwCastProvider;
815     // StartCastSession may fail with -1003
816     int32_t ret = hwCastProvider.StartCastSession(false);
817     SLOGI("StartCastSession001 with ret %{public}d", ret);
818     EXPECT_TRUE(ret != AVSESSION_SUCCESS);
819 }
820 
821 HWTEST_F(AVCastControllerTest, StopCastSession001, TestSize.Level1)
822 {
823     HwCastProvider hwCastProvider;
824     hwCastProvider.StopCastSession(2);
825     EXPECT_TRUE(true);
826 }
827 
828 HWTEST_F(AVCastControllerTest, AddCastDevice001, TestSize.Level1)
829 {
830     HwCastProvider hwCastProvider;
831 
832     DeviceInfo deviceInfo1;
833     deviceInfo1.castCategory_ = 1;
834     deviceInfo1.deviceId_ = "deviceid1";
835     deviceInfo1.deviceName_ = "devicename1";
836     deviceInfo1.deviceType_ = 1;
837     deviceInfo1.ipAddress_ = "ipAddress1";
838     deviceInfo1.providerId_ = 1;
839     deviceInfo1.supportedProtocols_ = 3;
840     deviceInfo1.authenticationStatus_ = 1;
841     std::vector<std::string> supportedDrmCapabilities;
842     supportedDrmCapabilities.emplace_back("");
843     deviceInfo1.supportedDrmCapabilities_ = supportedDrmCapabilities;
844     deviceInfo1.isLegacy_ = false;
845     deviceInfo1.mediumTypes_ = 2;
846     uint32_t spid = 0;
847     EXPECT_EQ(hwCastProvider.AddCastDevice(1, deviceInfo1, spid), false);
848 }
849 
850 HWTEST_F(AVCastControllerTest, RemoveCastDevice001, TestSize.Level1)
851 {
852     HwCastProvider hwCastProvider;
853 
854     DeviceInfo deviceInfo1;
855     deviceInfo1.castCategory_ = 1;
856     deviceInfo1.deviceId_ = "deviceid1";
857     deviceInfo1.deviceName_ = "devicename1";
858     deviceInfo1.deviceType_ = 1;
859     deviceInfo1.ipAddress_ = "ipAddress1";
860     deviceInfo1.providerId_ = 1;
861     deviceInfo1.supportedProtocols_ = 1;
862     deviceInfo1.authenticationStatus_ = 0;
863     std::vector<std::string> supportedDrmCapabilities;
864     supportedDrmCapabilities.emplace_back("");
865     deviceInfo1.supportedDrmCapabilities_ = supportedDrmCapabilities;
866     deviceInfo1.isLegacy_ = false;
867     deviceInfo1.mediumTypes_ = 2;
868 
869     EXPECT_EQ(hwCastProvider.RemoveCastDevice(1, deviceInfo1), false);
870 }
871 
872 HWTEST_F(AVCastControllerTest, RegisterCastStateListener001, TestSize.Level1)
873 {
874     HwCastProvider hwCastProvider;
875 
876     EXPECT_EQ(hwCastProvider.RegisterCastStateListener(nullptr), false);
877 }
878 
879 HWTEST_F(AVCastControllerTest, UnRegisterCastStateListener001, TestSize.Level1)
880 {
881     HwCastProvider hwCastProvider;
882 
883     EXPECT_EQ(hwCastProvider.UnRegisterCastStateListener(nullptr), false);
884 }
885 
886 HWTEST_F(AVCastControllerTest, RegisterCastSessionStateListener001, TestSize.Level1)
887 {
888     HwCastProvider hwCastProvider;
889 
890     EXPECT_EQ(hwCastProvider.RegisterCastSessionStateListener(2, nullptr), false);
891 }
892 
893 HWTEST_F(AVCastControllerTest, UnRegisterCastSessionStateListener001, TestSize.Level1)
894 {
895     HwCastProvider hwCastProvider;
896 
897     EXPECT_EQ(hwCastProvider.UnRegisterCastSessionStateListener(2, nullptr), false);
898 }
899 
900 /**
901 * @tc.name: OnCastPlaybackStateChange002
902 * @tc.desc: OnCastPlaybackStateChange, no callback
903 * @tc.type: FUNC
904 * @tc.require:
905 */
906 HWTEST_F(AVCastControllerTest, OnCastPlaybackStateChange002, TestSize.Level1)
907 {
908     LOG_SetCallback(MyLogCallback);
909     AVPlaybackState state;
910     state.SetState(AVPlaybackState::PLAYBACK_STATE_PREPARE);
911     castController_->OnCastPlaybackStateChange(state);
912     EXPECT_EQ(castController_->isPlayingState_, false);
913 }
914 
915 /**
916 * @tc.name: OnCastPlaybackStateChange003
917 * @tc.desc: OnCastPlaybackStateChange, have reigstered callback
918 * @tc.type: FUNC
919 * @tc.require:
920 */
921 HWTEST_F(AVCastControllerTest, OnCastPlaybackStateChange003, TestSize.Level1)
922 {
923     LOG_SetCallback(MyLogCallback);
924     castController_->callback_ = g_AVCastControllerCallbackProxy;
925     castController_->castControllerProxy_ = std::make_shared<AVCastControllerProxyMock>();
926 
927     AVPlaybackState state;
928     state.SetState(AVPlaybackState::PLAYBACK_STATE_PLAY);
929     castController_->OnCastPlaybackStateChange(state);
930     EXPECT_EQ(castController_->isPlayingState_, true);
931 }
932 
933 /**
934 * @tc.name: OnCastPlaybackStateChange003
935 * @tc.desc: OnCastPlaybackStateChange, have reigstered callback and session callback
936 * @tc.type: FUNC
937 * @tc.require:
938 */
939 HWTEST_F(AVCastControllerTest, OnCastPlaybackStateChange004, TestSize.Level1)
940 {
941     LOG_SetCallback(MyLogCallback);
942     castController_->callback_ = g_AVCastControllerCallbackProxy;
943     castController_->castControllerProxy_ = std::make_shared<AVCastControllerProxyMock>();
944     AVPlaybackState state;
945     state.SetState(AVPlaybackState::PLAYBACK_STATE_PLAY);
946     castController_->OnCastPlaybackStateChange(state);
947     EXPECT_EQ(castController_->isPlayingState_, true);
948 }
949 
950 /**
951 * @tc.name: OnMediaItemChange002
952 * @tc.desc: OnMediaItemChange, have reigstered callback
953 * @tc.type: FUNC
954 * @tc.require:
955 */
956 HWTEST_F(AVCastControllerTest, OnMediaItemChange002, TestSize.Level1)
957 {
958     LOG_SetCallback(MyLogCallback);
959     castController_->callback_ = g_AVCastControllerCallbackProxy;
960     AVQueueItem avQueueItem;
961     castController_->OnMediaItemChange(avQueueItem);
962     EXPECT_TRUE(castController_->callback_ != nullptr);
963 }
964 
965 /**
966 * @tc.name: OnPlayNext002
967 * @tc.desc: OnPlayNext, have reigstered callback
968 * @tc.type: FUNC
969 * @tc.require:
970 */
971 HWTEST_F(AVCastControllerTest, OnPlayNext002, TestSize.Level1)
972 {
973     LOG_SetCallback(MyLogCallback);
974     castController_->callback_ = g_AVCastControllerCallbackProxy;
975     castController_->OnPlayNext();
976     EXPECT_TRUE(castController_->callback_ != nullptr);
977 }
978 
979 /**
980 * @tc.name: OnPlayPrevious002
981 * @tc.desc: OnPlayPrevious, have reigstered callback
982 * @tc.type: FUNC
983 * @tc.require:
984 */
985 HWTEST_F(AVCastControllerTest, OnPlayPrevious002, TestSize.Level1)
986 {
987     LOG_SetCallback(MyLogCallback);
988     castController_->callback_ = g_AVCastControllerCallbackProxy;
989     castController_->OnPlayPrevious();
990     EXPECT_TRUE(castController_->callback_ != nullptr);
991 }
992 
993 /**
994 * @tc.name: OnSeekDone002
995 * @tc.desc: OnSeekDone, no callback
996 * @tc.type: FUNC
997 * @tc.require:
998 */
999 HWTEST_F(AVCastControllerTest, OnSeekDone002, TestSize.Level1)
1000 {
1001     LOG_SetCallback(MyLogCallback);
1002     castController_->callback_ = g_AVCastControllerCallbackProxy;
1003     int32_t seekNumber = 0;
1004     castController_->OnSeekDone(seekNumber);
1005     EXPECT_TRUE(castController_->callback_ != nullptr);
1006 }
1007 
1008 /**
1009 * @tc.name: OnVideoSizeChange002
1010 * @tc.desc: OnVideoSizeChange, have reigstered callback
1011 * @tc.type: FUNC
1012 * @tc.require:
1013 */
1014 HWTEST_F(AVCastControllerTest, OnVideoSizeChange002, TestSize.Level1)
1015 {
1016     LOG_SetCallback(MyLogCallback);
1017     castController_->callback_ = g_AVCastControllerCallbackProxy;
1018     int32_t width = 0;
1019     int32_t height = 0;
1020     castController_->OnVideoSizeChange(width, height);
1021     EXPECT_TRUE(castController_->callback_ != nullptr);
1022 }
1023 
1024 /**
1025 * @tc.name: OnPlayerError002
1026 * @tc.desc: OnPlayerError, have reigstered callback
1027 * @tc.type: FUNC
1028 * @tc.require:
1029 */
1030 HWTEST_F(AVCastControllerTest, OnPlayerError002, TestSize.Level1)
1031 {
1032     LOG_SetCallback(MyLogCallback);
1033     castController_->callback_ = g_AVCastControllerCallbackProxy;
1034     int32_t errorCode = 0;
1035     std::string errorMsg = "errorMsg";
1036     castController_->OnPlayerError(errorCode, errorMsg);
1037     EXPECT_TRUE(castController_->callback_ != nullptr);
1038 }
1039 
1040 /**
1041 * @tc.name: OnEndOfStream001
1042 * @tc.desc: OnEndOfStream, no callback
1043 * @tc.type: FUNC
1044 * @tc.require:
1045 */
1046 HWTEST_F(AVCastControllerTest, OnEndOfStream001, TestSize.Level1)
1047 {
1048     LOG_SetCallback(MyLogCallback);
1049     castController_->callback_ = nullptr;
1050     int32_t isLooping = true;
1051     castController_->OnEndOfStream(isLooping);
1052     EXPECT_TRUE(castController_->callback_ == nullptr);
1053 }
1054 
1055 /**
1056 * @tc.name: OnEndOfStream002
1057 * @tc.desc: OnEndOfStream, have reigstered callback
1058 * @tc.type: FUNC
1059 * @tc.require:
1060 */
1061 HWTEST_F(AVCastControllerTest, OnEndOfStream002, TestSize.Level1)
1062 {
1063     LOG_SetCallback(MyLogCallback);
1064     castController_->callback_ = g_AVCastControllerCallbackProxy;
1065     int32_t isLooping = true;
1066     castController_->OnEndOfStream(isLooping);
1067     EXPECT_TRUE(castController_->callback_ != nullptr);
1068 }
1069 
1070 /**
1071 * @tc.name: OnPlayRequest001
1072 * @tc.desc: OnPlayRequest, no callback
1073 * @tc.type: FUNC
1074 * @tc.require:
1075 */
1076 HWTEST_F(AVCastControllerTest, OnPlayRequest001, TestSize.Level1)
1077 {
1078     LOG_SetCallback(MyLogCallback);
1079     castController_->callback_ = nullptr;
1080     AVQueueItem avQueueItem;
1081     castController_->OnPlayRequest(avQueueItem);
1082     EXPECT_TRUE(castController_->callback_ == nullptr);
1083 }
1084 
1085 /**
1086 * @tc.name: OnPlayRequest002
1087 * @tc.desc: OnPlayRequest, have reigstered callback
1088 * @tc.type: FUNC
1089 * @tc.require:
1090 */
1091 HWTEST_F(AVCastControllerTest, OnPlayRequest002, TestSize.Level1)
1092 {
1093     LOG_SetCallback(MyLogCallback);
1094     castController_->callback_ = g_AVCastControllerCallbackProxy;
1095     AVQueueItem avQueueItem;
1096     castController_->OnPlayRequest(avQueueItem);
1097     EXPECT_TRUE(castController_->callback_ != nullptr);
1098 }
1099 
1100 /**
1101 * @tc.name: OnKeyRequest001
1102 * @tc.desc: OnKeyRequest, no callback
1103 * @tc.type: FUNC
1104 * @tc.require:
1105 */
1106 HWTEST_F(AVCastControllerTest, OnKeyRequest001, TestSize.Level1)
1107 {
1108     LOG_SetCallback(MyLogCallback);
1109     castController_->callback_ = nullptr;
1110     std::string assetId = "test";
1111     std::vector<uint8_t> keyRequestData {};
1112     castController_->OnKeyRequest(assetId, keyRequestData);
1113     EXPECT_TRUE(castController_->callback_ == nullptr);
1114 }
1115 
1116 /**
1117 * @tc.name: OnKeyRequest002
1118 * @tc.desc: OnKeyRequest, have reigstered callback
1119 * @tc.type: FUNC
1120 * @tc.require:
1121 */
1122 HWTEST_F(AVCastControllerTest, OnKeyRequest002, TestSize.Level1)
1123 {
1124     LOG_SetCallback(MyLogCallback);
1125     castController_->callback_ = g_AVCastControllerCallbackProxy;
1126     std::string assetId = "test";
1127     std::vector<uint8_t> keyRequestData {};
1128     castController_->OnKeyRequest(assetId, keyRequestData);
1129     EXPECT_TRUE(castController_->callback_ != nullptr);
1130 }
1131 
1132 /**
1133 * @tc.name: onDataSrcRead001
1134 * @tc.desc: onDataSrcRead, no callback
1135 * @tc.type: FUNC
1136 * @tc.require:
1137 */
1138 HWTEST_F(AVCastControllerTest, onDataSrcRead001, TestSize.Level1)
1139 {
1140     LOG_SetCallback(MyLogCallback);
1141     castController_->callback_ = nullptr;
1142     std::shared_ptr<AVSharedMemoryBase> mem = nullptr;
1143     uint32_t length = 0;
1144     int64_t pos = 0;
1145     int32_t result = 0;
1146     castController_->onDataSrcRead(mem, length, pos, result);
1147     EXPECT_TRUE(castController_->callback_ == nullptr);
1148 }
1149 
1150 /**
1151 * @tc.name: onDataSrcRead002
1152 * @tc.desc: onDataSrcRead, have reigstered callback
1153 * @tc.type: FUNC
1154 * @tc.require:
1155 */
1156 HWTEST_F(AVCastControllerTest, onDataSrcRead002, TestSize.Level1)
1157 {
1158     LOG_SetCallback(MyLogCallback);
1159     castController_->callback_ = g_AVCastControllerCallbackProxy;
1160     std::shared_ptr<AVSharedMemoryBase> mem = nullptr;
1161     uint32_t length = 0;
1162     int64_t pos = 0;
1163     int32_t result = 0;
1164     castController_->onDataSrcRead(mem, length, pos, result);
1165     EXPECT_TRUE(castController_->callback_ != nullptr);
1166 }
1167 
1168 /**
1169 * @tc.name: onDataSrcRead003
1170 * @tc.desc: onDataSrcRead, with callback
1171 * @tc.type: FUNC
1172 * @tc.require:
1173 */
1174 HWTEST_F(AVCastControllerTest, onDataSrcRead003, TestSize.Level1)
1175 {
1176     LOG_SetCallback(MyLogCallback);
1177     castController_->callback_ = g_AVCastControllerCallbackProxy;
1178     castController_->castControllerProxy_ = std::make_shared<AVCastControllerProxyMock>();
1179     int32_t size = 10;
1180     uint32_t flags = 1;
1181     const std::string name = "test";
1182     auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
1183     uint32_t length = 2;
1184     int64_t pos = 0;
1185     int32_t result = 0;
1186     auto ret = castController_->onDataSrcRead(memory, length, pos, result);
1187     EXPECT_EQ(ret, ERR_INVALID_DATA);
1188 }
1189 
1190 /**
1191 * @tc.name: OnCastPlaybackStateChange005
1192 * @tc.desc: success to copy and no registrered callback
1193 * @tc.type: FUNC
1194 * @tc.require:
1195 */
1196 HWTEST_F(AVCastControllerTest, OnCastPlaybackStateChange005, TestSize.Level1)
1197 {
1198     LOG_SetCallback(MyLogCallback);
1199     std::string bits = std::string(14ULL, '1');
1200     castController_->castPlaybackMask_ = std::bitset<14ULL>(bits);
1201     castController_->callback_ = nullptr;
1202     castController_->currentState_ = AVPlaybackState::PLAYBACK_STATE_INITIAL;
1203     AVPlaybackState state;
1204     state.SetState(AVPlaybackState::PLAYBACK_STATE_PAUSE);
1205     castController_->OnCastPlaybackStateChange(state);
1206     EXPECT_TRUE(state.GetState() != AVPlaybackState::PLAYBACK_STATE_PLAY);
1207 }
1208 
1209 /**
1210 * @tc.name: OnCastPlaybackStateChange006
1211 * @tc.desc: have no reigstered callback
1212 * @tc.type: FUNC
1213 * @tc.require:
1214 */
1215 HWTEST_F(AVCastControllerTest, OnCastPlaybackStateChange006, TestSize.Level1)
1216 {
1217     LOG_SetCallback(MyLogCallback);
1218     std::string bits = std::string(14ULL, '1');
1219     castController_->castPlaybackMask_ = std::bitset<14ULL>(bits);
1220     castController_->callback_ = nullptr;
1221     castController_->currentState_ = AVPlaybackState::PLAYBACK_STATE_INITIAL;
1222     AVPlaybackState state;
1223     state.SetState(AVPlaybackState::PLAYBACK_STATE_PLAY);
1224     castController_->OnCastPlaybackStateChange(state);
1225     EXPECT_TRUE(castController_->isPlayingState_);
1226     EXPECT_TRUE(state.GetState() == AVPlaybackState::PLAYBACK_STATE_PLAY);
1227 }
1228 
1229 /**
1230 * @tc.name: OnCastPlaybackStateChange007
1231 * @tc.desc: success to CopyToByMask and have no reigstered callback
1232 * @tc.type: FUNC
1233 * @tc.require:
1234 */
1235 HWTEST_F(AVCastControllerTest, OnCastPlaybackStateChange007, TestSize.Level1)
1236 {
1237     LOG_SetCallback(MyLogCallback);
1238     std::string bits = std::string(14ULL, '1');
1239     castController_->castPlaybackMask_ = std::bitset<14ULL>(bits);
1240     castController_->callback_ = nullptr;
1241     AVPlaybackState state;
1242     state.SetState(AVPlaybackState::PLAYBACK_STATE_PLAY);
1243     castController_->currentState_ = state.GetState();
1244     castController_->OnCastPlaybackStateChange(state);
1245     AVPlaybackState stateOut;
1246     EXPECT_TRUE(state.CopyToByMask(castController_->castPlaybackMask_, stateOut));
1247 }
1248 
1249 
1250 /**
1251 * @tc.name: OnCastPlaybackStateChange008
1252 * @tc.desc: success to CopyToByMask and have reigstered callback
1253 * @tc.type: FUNC
1254 * @tc.require:
1255 */
1256 HWTEST_F(AVCastControllerTest, OnCastPlaybackStateChange008, TestSize.Level1)
1257 {
1258     LOG_SetCallback(MyLogCallback);
1259     std::string bits = std::string(14ULL, '1');
1260     castController_->castPlaybackMask_ = std::bitset<14ULL>(bits);
1261     castController_->callback_ = g_AVCastControllerCallbackProxy;
1262     AVPlaybackState state;
1263     state.SetState(AVPlaybackState::PLAYBACK_STATE_PLAY);
1264     castController_->currentState_ = state.GetState();
1265     castController_->OnCastPlaybackStateChange(state);
1266     AVPlaybackState stateOut;
1267     EXPECT_TRUE(state.CopyToByMask(castController_->castPlaybackMask_, stateOut));
1268 }
1269 
1270 /**
1271 * @tc.name: OnCustomData001
1272 * @tc.desc: OnCustomData, have reigstered callback
1273 * @tc.type: FUNC
1274 * @tc.require:
1275 */
1276 HWTEST_F(AVCastControllerTest, OnCustomData001, TestSize.Level1)
1277 {
1278     LOG_SetCallback(MyLogCallback);
1279     castController_->callback_ = g_AVCastControllerCallbackProxy;
1280     std::string dataStr = "test";
1281     OHOS::AAFwk::WantParams data;
1282     data.SetParam("customData", OHOS::AAFwk::String::Box(dataStr));
1283     castController_->OnCustomData(data);
1284     EXPECT_TRUE(castController_->callback_ != nullptr);
1285 }
1286 
1287 /**
1288 * @tc.name: Prepare002
1289 * @tc.desc: GetIcon is not nullptr but GetIconUri is failed
1290 * @tc.type: FUNC
1291 * @tc.require:
1292 */
1293 HWTEST_F(AVCastControllerTest, Prepare002, TestSize.Level1)
1294 {
1295     AVQueueItem avQueueItem;
1296     std::shared_ptr<AVSessionPixelMap> icon = std::make_shared<AVSessionPixelMap>();
1297     std::shared_ptr<AVMediaDescription> description = std::make_shared<AVMediaDescription>();
1298     description->SetMediaId("123");
1299     description->SetTitle("Title");
1300     description->SetSubtitle("Subtitle");
1301     description->SetDescription("This is music description");
1302     description->SetIcon(icon);
1303     description->SetIconUri("test");
1304     description->SetExtras(nullptr);
1305     description->SetMediaUri("Media url");
1306     avQueueItem.SetDescription(description);
1307     EXPECT_EQ(castController_->Prepare(avQueueItem), AVSESSION_SUCCESS);
1308 }
1309 
1310 /**
1311 * @tc.name: Prepare003
1312 * @tc.desc: GetIcon is not nullptr and GetIconUri is successed
1313 * @tc.type: FUNC
1314 * @tc.require:
1315 */
1316 HWTEST_F(AVCastControllerTest, Prepare003, TestSize.Level1)
1317 {
1318     AVQueueItem avQueueItem;
1319     std::shared_ptr<AVSessionPixelMap> icon = std::make_shared<AVSessionPixelMap>();
1320     std::shared_ptr<AVMediaDescription> description = std::make_shared<AVMediaDescription>();
1321     description->SetMediaId("123");
1322     description->SetTitle("Title");
1323     description->SetSubtitle("Subtitle");
1324     description->SetDescription("This is music description");
1325     description->SetIcon(icon);
1326     description->SetIconUri("URI_CACHE");
1327     description->SetExtras(nullptr);
1328     description->SetMediaUri("Media url");
1329     avQueueItem.SetDescription(description);
1330     EXPECT_EQ(castController_->Prepare(avQueueItem), AVSESSION_SUCCESS);
1331 }
1332 
1333 /**
1334 * @tc.name: Prepare004
1335 * @tc.desc: callback is registered and isPlayingState is false
1336 * @tc.type: FUNC
1337 * @tc.require:
1338 */
1339 HWTEST_F(AVCastControllerTest, Prepare004, TestSize.Level1)
1340 {
1341     AVQueueItem avQueueItem;
1342     std::shared_ptr<AVSessionPixelMap> icon = std::make_shared<AVSessionPixelMap>();
1343     std::shared_ptr<AVMediaDescription> description = std::make_shared<AVMediaDescription>();
1344     description->SetMediaId("123");
1345     description->SetTitle("Title");
1346     description->SetSubtitle("Subtitle");
1347     description->SetDescription("This is music description");
1348     description->SetIcon(icon);
1349     description->SetIconUri("URI_CACHE");
1350     description->SetExtras(nullptr);
1351     description->SetMediaUri("Media url");
1352     avQueueItem.SetDescription(description);
1353     castController_->isPlayingState_ = false;
1354     EXPECT_EQ(castController_->Prepare(avQueueItem), AVSESSION_SUCCESS);
1355 }
1356 
1357 /**
1358 * @tc.name: Prepare005
1359 * @tc.desc: callback is registered and isPlayingState is true
1360 * @tc.type: FUNC
1361 * @tc.require:
1362 */
1363 HWTEST_F(AVCastControllerTest, Prepare005, TestSize.Level1)
1364 {
1365     AVQueueItem avQueueItem;
1366     std::shared_ptr<AVSessionPixelMap> icon = std::make_shared<AVSessionPixelMap>();
1367     std::shared_ptr<AVMediaDescription> description = std::make_shared<AVMediaDescription>();
1368     description->SetMediaId("123");
1369     description->SetTitle("Title");
1370     description->SetSubtitle("Subtitle");
1371     description->SetDescription("This is music description");
1372     description->SetIcon(icon);
1373     description->SetIconUri("URI_CACHE");
1374     description->SetExtras(nullptr);
1375     description->SetMediaUri("Media url");
1376     avQueueItem.SetDescription(description);
1377     castController_->isPlayingState_ = true;
1378     EXPECT_EQ(castController_->Prepare(avQueueItem), AVSESSION_SUCCESS);
1379 }
1380 
1381 /**
1382 * @tc.name: SetQueueItemDataSrc001
1383 * @tc.desc: callback is registered and isPlayingState is true
1384 * @tc.type: FUNC
1385 * @tc.require:
1386 */
1387 HWTEST_F(AVCastControllerTest, SetQueueItemDataSrc001, TestSize.Level1)
1388 {
1389     AVDataSrcDescriptor dataSrc;
1390     dataSrc.hasCallback = true;
__anon95794c400302(void*, uint32_t, int64_t) 1391     dataSrc.callback_ = [](void*, uint32_t, int64_t) -> int32_t { return 0; };
1392     std::shared_ptr<AVMediaDescription> description = std::make_shared<AVMediaDescription>();
1393     description->SetDataSrc(dataSrc);
1394     AVQueueItem avQueueItem;
1395     avQueueItem.IsValid();
1396     avQueueItem.SetDescription(description);
1397     avQueueItem.IsValid();
1398     castController_->isPlayingState_ = true;
1399     castController_->SetQueueItemDataSrc(avQueueItem);
1400     EXPECT_EQ(avQueueItem.GetDescription()->GetDataSrc().hasCallback, true);
1401 }
1402 
1403 /**
1404 * @tc.name: AddAvailableCommand002
1405 * @tc.desc: AddAvailableCommand with not null supportedCastCmds
1406 * @tc.type: FUNC
1407 * @tc.require:
1408 */
1409 HWTEST_F(AVCastControllerTest, AddAvailableCommand002, TestSize.Level1)
1410 {
1411     std::shared_ptr<AVCastControllerItem> castController = std::make_shared<AVCastControllerItem>();
1412     ASSERT_TRUE(castController != nullptr);
1413     std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(nullptr);
1414     ASSERT_TRUE(hwCastStreamPlayer != nullptr);
__anon95794c400402(int32_t cmd, std::vector<int32_t>& supportedCastCmds) 1415     auto validCallback = [](int32_t cmd, std::vector<int32_t>& supportedCastCmds) {
1416         SLOGI("add cast valid command %{public}d", cmd);
1417         supportedCastCmds = { AVCastControlCommand::CAST_CONTROL_CMD_PLAY };
1418     };
__anon95794c400502() 1419     auto preparecallback = []() {
1420         SLOGI("prepare callback");
1421     };
1422     castController->Init(hwCastStreamPlayer, validCallback, preparecallback);
1423     EXPECT_EQ(castController->AddAvailableCommand(0), AVSESSION_SUCCESS);
1424 }
1425 
1426 /**
1427 * @tc.name: RemoveAvailableCommand002
1428 * @tc.desc: RemoveAvailableCommand with not null supportedCastCmds
1429 * @tc.type: FUNC
1430 * @tc.require:
1431 */
1432 HWTEST_F(AVCastControllerTest, RemoveAvailableCommand002, TestSize.Level1)
1433 {
1434     std::shared_ptr<AVCastControllerItem> castController = std::make_shared<AVCastControllerItem>();
1435     ASSERT_TRUE(castController != nullptr);
1436     std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(nullptr);
1437     ASSERT_TRUE(hwCastStreamPlayer != nullptr);
__anon95794c400602(int32_t cmd, std::vector<int32_t>& supportedCastCmds) 1438     auto validCallback = [](int32_t cmd, std::vector<int32_t>& supportedCastCmds) {
1439         SLOGI("add cast valid command %{public}d", cmd);
1440         supportedCastCmds = { AVCastControlCommand::CAST_CONTROL_CMD_PLAY };
1441     };
__anon95794c400702() 1442     auto preparecallback = []() {
1443         SLOGI("prepare callback");
1444     };
1445     castController->Init(hwCastStreamPlayer, validCallback, preparecallback);
1446     EXPECT_EQ(castController->RemoveAvailableCommand(0), AVSESSION_SUCCESS);
1447 }
1448 
1449 /**
1450 * @tc.name: Destroy002
1451 * @tc.desc: Destroy with not null callback
1452 * @tc.type: FUNC
1453 * @tc.require:
1454 */
1455 HWTEST_F(AVCastControllerTest, Destroy002, TestSize.Level1)
1456 {
1457     ASSERT_TRUE(castController_ != nullptr);
1458     castController_->callback_ = g_AVCastControllerCallbackProxy;
1459     EXPECT_EQ(castController_->Destroy(), AVSESSION_SUCCESS);
1460 }
1461 
1462 /**
1463 * @tc.name: GetSourceType001
1464 * @tc.desc: get source type
1465 * @tc.type: FUNC
1466 * @tc.require:
1467 */
1468 HWTEST_F(AVCastControllerTest, GetSourceType001, TestSize.Level1)
1469 {
1470     AVQueueItem avQueueItem;
1471     std::shared_ptr<AVMediaDescription> description = std::make_shared<AVMediaDescription>();
1472     description->SetPcmSrc(true);
1473     avQueueItem.SetDescription(description);
1474     EXPECT_EQ(castController_->GetSourceType(avQueueItem), "PCM");
1475 
1476     description->SetPcmSrc(false);
1477     AVDataSrcDescriptor dataDes;
1478     dataDes.hasCallback = true;
1479     description->SetDataSrc(dataDes);
1480     EXPECT_EQ(castController_->GetSourceType(avQueueItem), "DATASRC");
1481 
1482     dataDes.hasCallback = false;
1483     description->SetDataSrc(dataDes);
1484     description->SetMediaUri("xxx");
1485     EXPECT_EQ(castController_->GetSourceType(avQueueItem), "URI");
1486 
1487     description->SetMediaUri("");
1488     AVFileDescriptor fileDes;
1489     fileDes.fd_ = 1;
1490     description->SetFdSrc(fileDes);
1491     EXPECT_EQ(castController_->GetSourceType(avQueueItem), "FD");
1492 
1493     fileDes.fd_ = 0;
1494     description->SetFdSrc(fileDes);
1495     EXPECT_EQ(castController_->GetSourceType(avQueueItem), "");
1496 }
1497 
1498 } // namespace AVSession
1499 } // namespace OHOS
1500