1 /*
2 * Copyright (c) 2022 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 #define private public
19 #define protected public
20 #include "rtg_interface.h"
21 #undef private
22 #undef protected
23
24 namespace OHOS {
25 namespace RME {
26 using namespace testing;
27 using namespace testing::ext;
28
29 #define RTG_INTERFACE_SO_PATH "/system/lib/librtg_interface.z.so"
30
31 enum rtg_type : int {
32 VIP = 0,
33 TOP_TASK_KEY,
34 NORMAL_TASK,
35 RTG_TYPE_MAX,
36 };
37
38 class RtgInterfaceTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase()46 void RtgInterfaceTest::SetUpTestCase()
47 {
48 }
49
TearDownTestCase()50 void RtgInterfaceTest::TearDownTestCase()
51 {
52 }
53
SetUp()54 void RtgInterfaceTest::SetUp()
55 {
56 // must enable rtg before use the interface
57 bool ret = EnableRtg(true);
58 EXPECT_EQ(ret, false);
59 }
60
TearDown()61 void RtgInterfaceTest::TearDown()
62 {
63 // disable rtg after use the interface
64 bool ret = EnableRtg(false);
65 EXPECT_EQ(ret, false);
66 }
67
68 /**
69 * @tc.name: RtgInterfaceCreateAndDestroy
70 * @tc.desc: Verify the CreateAndDestroy function.
71 * @tc.type: FUNC
72 */
73 HWTEST_F(RtgInterfaceTest, RtgInterfaceCreateAndDestroy, TestSize.Level1)
74 {
75 int ret;
76 int grpId;
77 grpId = CreateNewRtgGrp(NORMAL_TASK, 0);
78 EXPECT_GT(grpId, 0);
79 ret = DestroyRtgGrp(grpId);
80 EXPECT_EQ(ret, 0);
81 }
82
83 /**
84 * @tc.name: RtgInterfaceDestroyErrorGroup
85 * @tc.desc: Verify Destroy function with error param.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(RtgInterfaceTest, RtgInterfaceDestroyErrorGroup, TestSize.Level1)
89 {
90 int ret;
91 ret = DestroyRtgGrp(-1);
92 EXPECT_NE(ret, 0);
93 }
94
95 /**
96 * @tc.name: RtgInterfaceAddRtg
97 * @tc.desc: Verify Rtg add function.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(RtgInterfaceTest, RtgInterfaceAddRtg, TestSize.Level1)
101 {
102 int ret;
103 int grpId;
104 int pid = getpid();
105 grpId = CreateNewRtgGrp(VIP, 0);
106 EXPECT_GT(grpId, 0);
107 ret = AddThreadToRtg(pid, grpId);
108 EXPECT_EQ(ret, 0);
109 ret = DestroyRtgGrp(grpId);
110 EXPECT_EQ(ret, 0);
111 }
112
113 /**
114 * @tc.name: RtgInterfaceAddErrorThread
115 * @tc.desc: Verify Rtg add function with error pid.
116 * @tc.type: FUNC
117 */
118 HWTEST_F(RtgInterfaceTest, RtgInterfaceAddErrorThread, TestSize.Level1)
119 {
120 int ret;
121 int grpId;
122 grpId = CreateNewRtgGrp(VIP, 0);
123 EXPECT_GT(grpId, 0);
124 ret = AddThreadToRtg(-1, grpId);
125 EXPECT_NE(ret, 0);
126 ret = DestroyRtgGrp(grpId);
127 EXPECT_EQ(ret, 0);
128 }
129
130 /**
131 * @tc.name: RtgInterfaceAddErrorGroup
132 * @tc.desc: Verify Rtg add function with error groupid.
133 * @tc.type: FUNC
134 */
135 HWTEST_F(RtgInterfaceTest, RtgInterfaceAddErrorGroup, TestSize.Level1)
136 {
137 int ret;
138 int pid = getpid();
139 ret = AddThreadToRtg(pid, -1);
140 EXPECT_NE(ret, 0);
141 }
142
143 /**
144 * @tc.name: RtgInterfaceAddRtgs
145 * @tc.desc: Verify Rtg add multiple thread function.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(RtgInterfaceTest, RtgInterfaceAddRtgs, TestSize.Level1)
149 {
150 int ret;
151 int grpId;
152 int pid = getpid();
153 vector<int> pids = {};
154 pids.push_back(pid);
155 grpId = CreateNewRtgGrp(VIP, 0);
156 EXPECT_GT(grpId, 0);
157 ret = AddThreadsToRtg(pids, grpId);
158 EXPECT_EQ(ret, 0);
159 ret = DestroyRtgGrp(grpId);
160 EXPECT_EQ(ret, 0);
161 }
162
163 /**
164 * @tc.name: RtgInterfaceClearRtg
165 * @tc.desc: Verify Rtg clear function.
166 * @tc.type: FUNC
167 */
168 HWTEST_F(RtgInterfaceTest, RtgInterfaceClearRtg, TestSize.Level1)
169 {
170 int ret;
171 int grpId;
172 int pid = getpid();
173 grpId = CreateNewRtgGrp(VIP, 0);
174 EXPECT_GT(grpId, 0);
175 ret = AddThreadToRtg(pid, grpId);
176 EXPECT_EQ(ret, 0);
177 ret = ClearRtgGrp(grpId);
178 EXPECT_EQ(ret, 0);
179 ret = DestroyRtgGrp(grpId);
180 EXPECT_EQ(ret, 0);
181 }
182
183 /**
184 * @tc.name: RtgInterfaceClearErrorGroup
185 * @tc.desc: Verify Rtg clear function with error groupid.
186 * @tc.type: FUNC
187 */
188 HWTEST_F(RtgInterfaceTest, RtgInterfaceClearErrorGroup, TestSize.Level1)
189 {
190 int ret;
191 ret = ClearRtgGrp(-1);
192 EXPECT_NE(ret, 0);
193 }
194
195 /**
196 * @tc.name: RtgInterfaceBeginFrameFreq
197 * @tc.desc: Verify rtg frame start function.
198 * @tc.type: FUNC
199 */
200 HWTEST_F(RtgInterfaceTest, RtgInterfaceBeginFrameFreq, TestSize.Level1)
201 {
202 int ret;
203 int grpId;
204 grpId = CreateNewRtgGrp(VIP, 0);
205 EXPECT_GT(grpId, 0);
206 ret = BeginFrameFreq(grpId, 0);
207 EXPECT_EQ(ret, 0);
208 ret = DestroyRtgGrp(grpId);
209 EXPECT_EQ(ret, 0);
210 }
211
212 /**
213 * @tc.name: RtgInterfaceBeginFrameFreqWithErrorGrp
214 * @tc.desc: Verify rtg frame start function with error groupid.
215 * @tc.type: FUNC
216 */
217 HWTEST_F(RtgInterfaceTest, RtgInterfaceBeginFrameFreqWithErrorGrp, TestSize.Level1)
218 {
219 int ret;
220 ret = BeginFrameFreq(-1, 0);
221 EXPECT_NE(ret, 0);
222 }
223
224 /**
225 * @tc.name: RtgInterfaceEndFrameFreq
226 * @tc.desc: Verify rtg frame end function.
227 * @tc.type: FUNC
228 */
229 HWTEST_F(RtgInterfaceTest, RtgInterfaceEndFrameFreq, TestSize.Level1)
230 {
231 int ret;
232 int grpId;
233 grpId = CreateNewRtgGrp(VIP, 0);
234 EXPECT_GT(grpId, 0);
235 ret = EndFrameFreq(grpId);
236 EXPECT_EQ(ret, 0);
237 ret = DestroyRtgGrp(grpId);
238 EXPECT_EQ(ret, 0);
239 }
240
241 /**
242 * @tc.name: RtgInterfaceEndFrameFreqWithErrorGrp
243 * @tc.desc: Verify rtg frame end function with error groupid.
244 * @tc.type: FUNC
245 */
246 HWTEST_F(RtgInterfaceTest, RtgInterfaceEndFrameFreqWithErrorGrp, TestSize.Level1)
247 {
248 int ret;
249 ret = EndFrameFreq(-1);
250 EXPECT_NE(ret, 0);
251 }
252
253 /**
254 * @tc.name: RtgInterfaceEndScene
255 * @tc.desc: Verify rtg frame scene end function.
256 * @tc.type: FUNC
257 */
258 HWTEST_F(RtgInterfaceTest, RtgInterfaceEndScene, TestSize.Level1)
259 {
260 int ret;
261 int grpId;
262 grpId = CreateNewRtgGrp(VIP, 0);
263 EXPECT_GT(grpId, 0);
264 ret = EndScene(grpId);
265 EXPECT_EQ(ret, 0);
266 ret = DestroyRtgGrp(grpId);
267 EXPECT_EQ(ret, 0);
268 }
269
270 /**
271 * @tc.name: RtgInterfaceEndSceneWithErrorGrp
272 * @tc.desc: Verify rtg frame scene end function with error groupid.
273 * @tc.type: FUNC
274 */
275 HWTEST_F(RtgInterfaceTest, RtgInterfaceEndSceneWithErrorGrp, TestSize.Level1)
276 {
277 int ret;
278 ret = EndScene(-1);
279 EXPECT_NE(ret, 0);
280 }
281
282 /**
283 * @tc.name: RtgInterfaceSetMinUtil
284 * @tc.desc: Verify rtg minUtil set function.
285 * @tc.type: FUNC
286 */
287 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetMinUtil, TestSize.Level1)
288 {
289 int ret;
290 int grpId;
291 grpId = CreateNewRtgGrp(VIP, 0);
292 EXPECT_GT(grpId, 0);
293 ret = SetMinUtil(grpId, 0);
294 EXPECT_EQ(ret, 0);
295 ret = DestroyRtgGrp(grpId);
296 EXPECT_EQ(ret, 0);
297 }
298
299 /**
300 * @tc.name: RtgInterfaceSetMinUtilWithErrorGrp
301 * @tc.desc: Verify rtg minUtil set function with error groupid.
302 * @tc.type: FUNC
303 */
304 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetMinUtilWithErrorGrp, TestSize.Level1)
305 {
306 int ret;
307 ret = SetMinUtil(-1, 0);
308 EXPECT_NE(ret, 0);
309 }
310
311 /**
312 * @tc.name: RtgInterfaceSetMargin
313 * @tc.desc: Verify rtg margin set function.
314 * @tc.type: FUNC
315 */
316 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetMargin, TestSize.Level1)
317 {
318 int ret;
319 int grpId;
320 grpId = CreateNewRtgGrp(VIP, 0);
321 EXPECT_GT(grpId, 0);
322 ret = SetMargin(grpId, 0);
323 EXPECT_EQ(ret, 0);
324 ret = DestroyRtgGrp(grpId);
325 EXPECT_EQ(ret, 0);
326 }
327
328 /**
329 * @tc.name: RtgInterfaceSetMarginWithErrorGrp
330 * @tc.desc: Verify rtg margin set function with error groupid.
331 * @tc.type: FUNC
332 */
333 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetMarginWithErrorGrp, TestSize.Level1)
334 {
335 int ret;
336 ret = SetMargin(-1, 0);
337 EXPECT_NE(ret, 0);
338 }
339
340 /**
341 * @tc.name: RtgInterfaceListRtgThread
342 * @tc.desc: Verify rtg list function.
343 * @tc.type: FUNC
344 */
345 HWTEST_F(RtgInterfaceTest, RtgInterfaceListRtgThread, TestSize.Level1)
346 {
347 int ret;
348 int grpId;
349 vector<int> rs;
350 grpId = CreateNewRtgGrp(VIP, 0);
351 EXPECT_GT(grpId, 0);
352 ret = ListRtgThread(grpId, &rs);
353 EXPECT_EQ(ret, 0);
354 ret = DestroyRtgGrp(grpId);
355 EXPECT_EQ(ret, 0);
356 }
357
358 /**
359 * @tc.name: RtgInterfaceListRtgThreadWithNullRes
360 * @tc.desc: Verify rtg thread list function with null vector input.
361 * @tc.type: FUNC
362 */
363 HWTEST_F(RtgInterfaceTest, RtgInterfaceListRtgThreadWithNullRes, TestSize.Level1)
364 {
365 int ret;
366 int grpId;
367 grpId = CreateNewRtgGrp(VIP, 0);
368 EXPECT_GT(grpId, 0);
369 ret = ListRtgThread(grpId, nullptr);
370 EXPECT_NE(ret, 0);
371 ret = DestroyRtgGrp(grpId);
372 EXPECT_EQ(ret, 0);
373 }
374
375 /**
376 * @tc.name: RtgInterfaceListRtgThreadWithErrorGrp
377 * @tc.desc: Verify rtg thread list function with error groupid.
378 * @tc.type: FUNC
379 */
380 HWTEST_F(RtgInterfaceTest, RtgInterfaceListRtgThreadWithErrorGrp, TestSize.Level1)
381 {
382 int ret;
383 vector<int> rs;
384 ret = ListRtgThread(-1, &rs);
385 EXPECT_NE(ret, 0);
386 }
387
388 /**
389 * @tc.name: RtgInterfaceListRtgGroup
390 * @tc.desc: Verify rtg list function.
391 * @tc.type: FUNC
392 */
393 HWTEST_F(RtgInterfaceTest, RtgInterfaceListRtgGroup, TestSize.Level1)
394 {
395 int ret;
396 vector<int> rs;
397 ret = ListRtgGroup(&rs);
398 EXPECT_EQ(ret, 0);
399 }
400
401 /**
402 * @tc.name: RtgInterfaceListRtgGroupWithNullRes
403 * @tc.desc: Verify rtg list function with null vector input.
404 * @tc.type: FUNC
405 */
406 HWTEST_F(RtgInterfaceTest, RtgInterfaceListRtgGroupWithNullRes, TestSize.Level1)
407 {
408 int ret;
409 ret = ListRtgGroup(nullptr);
410 EXPECT_NE(ret, 0);
411 }
412
413 /**
414 * @tc.name: RtgInterfaceSetAttr
415 * @tc.desc: Verify rtg attr set function.
416 * @tc.type: FUNC
417 */
418 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetAttr, TestSize.Level1)
419 {
420 int ret;
421 int grpId;
422 grpId = CreateNewRtgGrp(VIP, 0);
423 EXPECT_GT(grpId, 0);
424 ret = SetFrameRateAndPrioType(grpId, 60, VIP);
425 EXPECT_EQ(ret, 0);
426 ret = DestroyRtgGrp(grpId);
427 EXPECT_EQ(ret, 0);
428 }
429
430 /**
431 * @tc.name: RtgInterfaceSetErrorAttr
432 * @tc.desc: Verify rtg attr set function with error attr param.
433 * @tc.type: FUNC
434 */
435 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetErrorAttr, TestSize.Level1)
436 {
437 int ret;
438 int grpId;
439 grpId = CreateNewRtgGrp(VIP, 0);
440 EXPECT_GT(grpId, 0);
441 ret = SetFrameRateAndPrioType(grpId, 90, -1);
442 EXPECT_NE(ret, 0);
443 ret = DestroyRtgGrp(grpId);
444 EXPECT_EQ(ret, 0);
445 }
446
447 /**
448 * @tc.name: RtgInterfaceSetMaxVips
449 * @tc.desc: Verify rtg max vip num set function.
450 * @tc.type: FUNC
451 */
452 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetMaxVips, TestSize.Level1)
453 {
454 int ret;
455 ret = SetMaxVipRtgs(2);
456 EXPECT_EQ(ret, 0);
457 }
458
459 /**
460 * @tc.name: RtgInterfaceSetErrorMaxVips
461 * @tc.desc: Verify rtg max vip num set function with 0 vip nums.
462 * @tc.type: FUNC
463 */
464 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetErrorMaxVips, TestSize.Level1)
465 {
466 int ret;
467 ret = SetMaxVipRtgs(0);
468 EXPECT_NE(ret, 0);
469 }
470
471 /**
472 * @tc.name: RtgInterfaceSetLargeMaxVips
473 * @tc.desc: Verify rtg max vip num set function with too large vip nums.
474 * @tc.type: FUNC
475 */
476 HWTEST_F(RtgInterfaceTest, RtgInterfaceSetLargeMaxVips, TestSize.Level1)
477 {
478 int ret;
479 ret = SetMaxVipRtgs(50000);
480 EXPECT_NE(ret, 0);
481 }
482
483 /**
484 * @tc.name: RtgInterfaceAddMultipleThreads
485 * @tc.desc: Verify rtg multiple add function.
486 * @tc.type: FUNC
487 */
488 HWTEST_F(RtgInterfaceTest, RtgInterfaceAddMultipleThreads, TestSize.Level1)
489 {
490 int ret;
491 int pid[3];
492 vector<int> threads;
493 int grpId;
494 for (int i = 0; i < 3; i++) {
495 pid[i] = fork();
496 ASSERT_TRUE(pid[i] >= 0) << "> parent: fork errno = " << errno;
497 if (pid[i] == 0) {
498 usleep(50000);
499 _Exit(0);
500 }
501 threads.push_back(pid[i]);
502 }
503 grpId = CreateNewRtgGrp(NORMAL_TASK, 0);
504 EXPECT_GT(grpId, 0);
505 ret = AddThreadsToRtg(threads, grpId);
506 EXPECT_EQ(ret, 0);
507 ret = DestroyRtgGrp(grpId);
508 EXPECT_EQ(ret, 0);
509 }
510
511 /**
512 * @tc.name: RtgInterfaceAddMultipleThreadsOutOfLimit
513 * @tc.desc: Verify rtg multiple add function with out of limit threads.
514 * @tc.type: FUNC
515 */
516 HWTEST_F(RtgInterfaceTest, RtgInterfaceAddMultipleThreadsOutOfLimit, TestSize.Level1)
517 {
518 int ret;
519 int pid[8];
520 vector<int> threads;
521 int grpId;
522 for (int i = 0; i < 8; i++) {
523 pid[i] = fork();
524 ASSERT_TRUE(pid[i] >= 0) << "> parent: fork errno = " << errno;
525 if (pid[i] == 0) {
526 usleep(50000);
527 _Exit(0);
528 }
529 threads.push_back(pid[i]);
530 }
531 grpId = CreateNewRtgGrp(NORMAL_TASK, 0);
532 EXPECT_GT(grpId, 0);
533 ret = AddThreadsToRtg(threads, grpId);
534 EXPECT_NE(ret, 0);
535 ret = DestroyRtgGrp(grpId);
536 EXPECT_EQ(ret, 0);
537 }
538 } // namespace RME
539 } // namespace OHOS
540