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 #include "av_router.h"
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "avsession_service.h"
21
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace AVSession {
25
26 static std::shared_ptr<AVSessionService> g_AVSessionService;
27
28 class AVRouterTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 };
35
SetUpTestCase()36 void AVRouterTest::SetUpTestCase()
37 {
38 SLOGI("set up AVSessionServiceTest");
39 system("killall -9 com.example.hiMusicDemo");
40 g_AVSessionService = std::make_shared<AVSessionService>(OHOS::AVSESSION_SERVICE_ID);
41 }
42
TearDownTestCase()43 void AVRouterTest::TearDownTestCase()
44 {}
45
SetUp()46 void AVRouterTest::SetUp()
47 {}
48
TearDown()49 void AVRouterTest::TearDown()
50 {}
51
52 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
53
54 /**
55 * @tc.name: StartCastDiscovery001
56 * @tc.desc: start cast discovery for default cast type "local"
57 * @tc.type: FUNC
58 * @tc.require: NA
59 */
60 static HWTEST_F(AVRouterTest, StartCastDiscovery001, TestSize.Level1)
61 {
62 SLOGI("StartCastDiscovery001 begin");
63 auto ret = AVRouter::GetInstance().StartCastDiscovery(ProtocolType::TYPE_LOCAL, {});
64 EXPECT_EQ(ret, AVSESSION_SUCCESS);
65 SLOGI("StartCastDiscovery001 end");
66 }
67
68 /**
69 * @tc.name: StartCastDiscovery002
70 * @tc.desc: start cast discovery for invalid cast type
71 * @tc.type: FUNC
72 * @tc.require: NA
73 */
74 static HWTEST_F(AVRouterTest, StartCastDiscovery002, TestSize.Level1)
75 {
76 SLOGI("StartCastDiscovery001 begin");
77 auto ret = AVRouter::GetInstance().StartCastDiscovery(-1, {});
78 EXPECT_EQ(ret, AVSESSION_SUCCESS);
79 SLOGI("StartCastDiscovery001 end");
80 }
81
82 /**
83 * @tc.name: StopCastDiscovery001
84 * @tc.desc: stop cast discovery success
85 * @tc.type: FUNC
86 * @tc.require: NA
87 */
88 static HWTEST_F(AVRouterTest, StopCastDiscovery001, TestSize.Level1)
89 {
90 SLOGI("StopCastDiscovery001 begin");
91 auto ret = AVRouter::GetInstance().StartCastDiscovery(ProtocolType::TYPE_LOCAL, {});
92 EXPECT_EQ(ret, AVSESSION_SUCCESS);
93 ret = AVRouter::GetInstance().StopCastDiscovery();
94 EXPECT_EQ(ret, AVSESSION_SUCCESS);
95 SLOGI("StopCastDiscovery001 end");
96 }
97
98 /**
99 * @tc.name: SetDiscoverable001
100 * @tc.desc: setDiscoverable true
101 * @tc.type: FUNC
102 * @tc.require: NA
103 */
104 static HWTEST_F(AVRouterTest, SetDiscoverable001, TestSize.Level1)
105 {
106 SLOGI("SetDiscoverable001 begin");
107 auto ret = AVRouter::GetInstance().SetDiscoverable(true);
108 EXPECT_EQ(ret, AVSESSION_SUCCESS);
109 SLOGI("SetDiscoverable001 end");
110 }
111
112 /**
113 * @tc.name: OnDeviceAvailable001
114 * @tc.desc: StartCast success
115 * @tc.type: FUNC
116 * @tc.require: NA
117 */
118 HWTEST_F(AVRouterTest, OnDeviceAvailable001, TestSize.Level1)
119 {
120 SLOGI("OnDeviceAvailable001 begin");
121 OutputDeviceInfo outputDeviceInfo;
122 auto ret = AVRouter::GetInstance().OnDeviceAvailable(outputDeviceInfo);
123 EXPECT_NE(ret, AVSESSION_SUCCESS);
124 SLOGI("OnDeviceAvailable001 end");
125 }
126
127 /**
128 * @tc.name: OnCastServerDied001
129 * @tc.desc: setDiscoverable true
130 * @tc.type: FUNC
131 * @tc.require: NA
132 */
133 static HWTEST_F(AVRouterTest, OnCastServerDied001, TestSize.Level1)
134 {
135 SLOGI("OnCastServerDied001 begin");
136 auto ret = AVRouter::GetInstance().OnCastServerDied(-1);
137 EXPECT_EQ(ret, ERR_SERVICE_NOT_EXIST);
138 SLOGI("OnCastServerDied001 end");
139 }
140
141 /**
142 * @tc.name: StartDeviceLogging001
143 * @tc.desc: test StartDeviceLogging
144 * @tc.type: FUNC
145 * @tc.require: NA
146 */
147 static HWTEST_F(AVRouterTest, StartDeviceLogging001, TestSize.Level1)
148 {
149 SLOGI("StartDeviceLogging001 begin");
150 int32_t fd = 1;
151 uint32_t maxSize = 10;
152 auto ret = AVRouter::GetInstance().StartDeviceLogging(fd, maxSize);
153 AVRouter::GetInstance().StopDeviceLogging();
154 EXPECT_EQ(ret, AVSESSION_SUCCESS);
155 SLOGI("StartDeviceLogging001 end");
156 }
157
158 /**
159 * @tc.name: StartDeviceLogging002
160 * @tc.desc: test StartDeviceLogging
161 * @tc.type: FUNC
162 * @tc.require: NA
163 */
164 static HWTEST_F(AVRouterTest, StartDeviceLogging002, TestSize.Level1)
165 {
166 SLOGI("StartDeviceLogging002 begin");
167 int32_t fd = 1;
168 uint32_t maxSize = 10;
169 AVRouter::GetInstance().Init(g_AVSessionService.get());
170 auto ret = AVRouter::GetInstance().StartDeviceLogging(fd, maxSize);
171 AVRouter::GetInstance().StopDeviceLogging();
172 EXPECT_EQ(ret, AVSESSION_SUCCESS);
173 SLOGI("StartDeviceLogging002 end");
174 }
175
176 /**
177 * @tc.name: OnDeviceLogEvent001
178 * @tc.desc: test OnDeviceLogEvent
179 * @tc.type: FUNC
180 * @tc.require: NA
181 */
182 static HWTEST_F(AVRouterTest, OnDeviceLogEvent001, TestSize.Level1)
183 {
184 SLOGI("OnDeviceLogEvent001 begin");
185 DeviceLogEventCode eventId = DEVICE_LOG_FULL;
186 int64_t param = 0;
187 AVRouter::GetInstance().Init(nullptr);
188 auto ret = AVRouter::GetInstance().OnDeviceLogEvent(eventId, param);
189 EXPECT_EQ(ret, ERR_SERVICE_NOT_EXIST);
190 SLOGI("OnDeviceLogEvent001 end");
191 }
192
193 /**
194 * @tc.name: OnDeviceLogEvent002
195 * @tc.desc: test OnDeviceLogEvent
196 * @tc.type: FUNC
197 * @tc.require: NA
198 */
199 static HWTEST_F(AVRouterTest, OnDeviceLogEvent002, TestSize.Level1)
200 {
201 SLOGI("OnDeviceLogEvent002 begin");
202 DeviceLogEventCode eventId = DEVICE_LOG_FULL;
203 int64_t param = 0;
204 AVRouter::GetInstance().Init(g_AVSessionService.get());
205 auto ret = AVRouter::GetInstance().OnDeviceLogEvent(eventId, param);
206 EXPECT_EQ(ret, AVSESSION_SUCCESS);
207 SLOGI("OnDeviceLogEvent002 end");
208 }
209
210 /**
211 * @tc.name: OnDeviceOffline001
212 * @tc.desc: test OnDeviceOffline
213 * @tc.type: FUNC
214 * @tc.require: NA
215 */
216 static HWTEST_F(AVRouterTest, OnDeviceOffline001, TestSize.Level1)
217 {
218 SLOGI("OnDeviceOffline001 begin");
219 std::string deviceId = "***";
220 AVRouter::GetInstance().Init(nullptr);
221 auto ret = AVRouter::GetInstance().OnDeviceOffline(deviceId);
222 EXPECT_EQ(ret, ERR_SERVICE_NOT_EXIST);
223 SLOGI("OnDeviceOffline001 end");
224 }
225
226 /**
227 * @tc.name: OnDeviceOffline002
228 * @tc.desc: test OnDeviceOffline
229 * @tc.type: FUNC
230 * @tc.require: NA
231 */
232 static HWTEST_F(AVRouterTest, OnDeviceOffline002, TestSize.Level1)
233 {
234 SLOGI("OnDeviceOffline002 begin");
235 AVRouter::GetInstance().Init(g_AVSessionService.get());
236 std::string deviceId = "***";
237 auto ret = AVRouter::GetInstance().OnDeviceOffline(deviceId);
238 EXPECT_EQ(ret, AVSESSION_SUCCESS);
239 SLOGI("OnDeviceOffline002 end");
240 }
241 #endif
242 }
243 }