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_errors.h"
18 #include "avsession_log.h"
19 #include "av_session.h"
20 #include "avsession_item.h"
21 #include "avsession_service.h"
22 #include "avsession_info.h"
23 #include "remote_session_sink_impl.h"
24 #include "remote_session_sink_proxy.h"
25 
26 namespace OHOS::AVSession {
27 
28 sptr <AVSessionService> g_AVSessionService = nullptr;
29 sptr <AVSessionItem> g_AVSessionItem = nullptr;
30 std::shared_ptr<RemoteSessionSink> g_RemoteSessionSink;
31 
32 class RemoteSessionSinkTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38 };
39 
SetUpTestCase()40 void RemoteSessionSinkTest::SetUpTestCase()
41 {
42     g_AVSessionService = new AVSessionService(OHOS::AVSESSION_SERVICE_ID);
43     OHOS::AppExecFwk::ElementName elementName;
44     elementName.SetBundleName("testRemoteSink.ohos.g_AVSessionItem");
45     elementName.SetAbilityName("testRemoteSink.ability");
46     g_AVSessionItem = g_AVSessionService->CreateSessionInner("testRemoteSink",
47         AVSession::SESSION_TYPE_VOICE_CALL, false, elementName);
48     std::string sessionId = g_AVSessionItem->GetSessionId();
49     std::string sourceDevice = "sourceDevice";
50     std::string sinkDevice = "sinkDevice";
51     std::string sourceCapability = "sourceCapability";
52     g_RemoteSessionSink = std::make_shared<RemoteSessionSinkProxy>();
53     g_RemoteSessionSink->CastSessionFromRemote(g_AVSessionItem, sessionId, sourceDevice, sinkDevice, sourceCapability);
54     ASSERT_NE(g_RemoteSessionSink, nullptr);
55 }
56 
TearDownTestCase()57 void RemoteSessionSinkTest::TearDownTestCase()
58 {
59     g_RemoteSessionSink->CancelCastSession();
60     g_AVSessionService->Close();
61     [[maybe_unused]] int32_t ret = AVSESSION_ERROR;
62     if (g_AVSessionItem != nullptr) {
63         ret = g_AVSessionItem->Destroy();
64         g_AVSessionItem = nullptr;
65     }
66 }
67 
SetUp()68 void RemoteSessionSinkTest::SetUp()
69 {
70 }
71 
TearDown()72 void RemoteSessionSinkTest::TearDown()
73 {
74 }
75 
76 /**
77  * @tc.name: SetControlCommand001
78  * @tc.desc: Test SetControlCommand
79  * @tc.type: FUNC
80  */
HWTEST_F(RemoteSessionSinkTest, SetControlCommand001, testing::ext::TestSize.Level1)81 static HWTEST_F(RemoteSessionSinkTest, SetControlCommand001, testing::ext::TestSize.Level1)
82 {
83     SLOGI("SetControlCommand001 begin!");
84     AVControlCommand command;
85     command.SetCommand(1000);
86     int32_t ret = g_RemoteSessionSink->SetControlCommand(command);
87     g_RemoteSessionSink->CancelCastSession();
88     EXPECT_EQ(ret, AVSESSION_SUCCESS);
89     SLOGI("SetControlCommand001 end!");
90 }
91 
92 /**
93  * @tc.name: SetCommonCommand002
94  * @tc.desc: Test SetCommonCommand
95  * @tc.type: FUNC
96  */
HWTEST_F(RemoteSessionSinkTest, SetCommonCommand002, testing::ext::TestSize.Level1)97 static HWTEST_F(RemoteSessionSinkTest, SetCommonCommand002, testing::ext::TestSize.Level1)
98 {
99     SLOGI("SetCommonCommand002 begin!");
100     std::string commonCommand = "";
101     AAFwk::WantParams commandArgs;
102     int32_t ret = g_RemoteSessionSink->SetCommonCommand(commonCommand, commandArgs);
103     g_RemoteSessionSink->CancelCastSession();
104     EXPECT_EQ(ret, AVSESSION_SUCCESS);
105     SLOGI("SetCommonCommand002 end!");
106 }
107 
108 }