[utest] Add 108 test cases
[platform/core/uifw/libtdm.git] / utests / src / ut_tdm.cpp
1 /**************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics co., Ltd. All Rights Reserved.
4  *
5  * Contact: Konstantin Drabeniuk <k.drabeniuk@samsung.com>
6  * Contact: Andrii Sokolenko <a.sokolenko@samsung.com>
7  * Contact: Roman Marchenko <r.marchenko@samsung.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sub license, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the
18  * next paragraph) shall be included in all copies or substantial portions
19  * of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  *
29 **************************************************************************/
30
31 #include "gtest/gtest.h"
32 extern "C" {
33 #include "tdm.h"
34 #include "tbm_bufmgr.h"
35 #include "tbm_drm_helper.h"
36 }
37
38 class TDMInit : public ::testing::Test {
39 protected:
40         int master_fd = -42, tbm_fd = -42;
41         void SetUp(void)
42         {
43                 setenv("TDM_DLOG", "1", 1);
44                 setenv("XDG_RUNTIME_DIR", "/tmp", 1);
45                 setenv("TBM_DLOG", "1", 1);
46                 setenv("TBM_DISPLAY_SERVER", "1", 1);
47         }
48         void TearDown(void)
49         {
50                 if (master_fd > -1) {
51                         int temp_master_fd = tbm_drm_helper_get_master_fd();
52                         EXPECT_EQ(temp_master_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
53                         if (temp_master_fd > -1)
54                                 exit(1);
55                         close(master_fd);
56                 }
57                 if (tbm_fd > -1) {
58                         int temp_tbm_fd = tbm_drm_helper_get_fd();
59                         EXPECT_EQ(temp_tbm_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
60                         if (temp_tbm_fd > -1)
61                                 exit(1);
62                         close(tbm_fd);
63                 }
64                 unsetenv("TDM_DLOG");
65                 unsetenv("XDG_RUNTIME_DIR");
66                 unsetenv("TBM_DLOG");
67                 unsetenv("TBM_DISPLAY_SERVER");
68         }
69 };
70
71 class TDMDefault : public ::testing::Test {
72 protected:
73         tdm_display *dpy = NULL;
74         tbm_bufmgr tbm_bufmgr = NULL;
75         int master_fd = -42, tbm_fd = -42;
76         void SetUp(void)
77         {
78                 setenv("TDM_DLOG", "1", 1);
79                 setenv("XDG_RUNTIME_DIR", "/tmp", 1);
80                 setenv("TBM_DLOG", "1", 1);
81                 setenv("TBM_DISPLAY_SERVER", "1", 1);
82                 tbm_bufmgr = tbm_bufmgr_init(-1);
83                 ASSERT_FALSE(tbm_bufmgr == NULL);
84                 tdm_error error = TDM_ERROR_NONE;
85                 dpy = tdm_display_init(&error);
86                 ASSERT_TRUE(error == TDM_ERROR_NONE);
87                 ASSERT_FALSE(dpy == NULL);
88                 master_fd = tbm_drm_helper_get_master_fd();
89                 tbm_fd = tbm_drm_helper_get_fd();
90         }
91         void TearDown(void)
92         {
93                 tdm_display_deinit(dpy);
94                 tbm_bufmgr_deinit(tbm_bufmgr);
95                 tbm_bufmgr = NULL;
96                 dpy = NULL;
97                 if (master_fd > -1) {
98                         int temp_master_fd = tbm_drm_helper_get_master_fd();
99                         EXPECT_EQ(temp_master_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
100                         if (temp_master_fd > -1)
101                                 exit(1);
102                         close(master_fd);
103                 }
104                 if (tbm_fd > -1) {
105                         int temp_tbm_fd = tbm_drm_helper_get_fd();
106                         EXPECT_EQ(temp_tbm_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
107                         if (temp_tbm_fd > -1)
108                                 exit(1);
109                         close(tbm_fd);
110                 }
111                 unsetenv("TDM_DLOG");
112                 unsetenv("XDG_RUNTIME_DIR");
113                 unsetenv("TBM_DLOG");
114                 unsetenv("TBM_DISPLAY_SERVER");
115         }
116 };
117
118
119 TEST_F(TDMInit, DisplayInitDeinitSuccessfulWithoutTBM)
120 {
121         tdm_error error = TDM_ERROR_NONE;
122         tdm_display *dpy = tdm_display_init(&error);
123         ASSERT_TRUE(error == TDM_ERROR_NONE);
124         ASSERT_FALSE(dpy == NULL);
125         master_fd = tbm_drm_helper_get_master_fd();
126         tbm_fd = tbm_drm_helper_get_fd();
127         tdm_display_deinit(dpy);
128         dpy = NULL;
129 }
130
131 TEST_F(TDMInit, DisplayInitDeinitSuccessfulWithTBM)
132 {
133         tbm_bufmgr tbm_bufmgr = NULL;
134         tbm_bufmgr = tbm_bufmgr_init(-1);
135         ASSERT_FALSE(tbm_bufmgr == NULL);
136         tdm_error error = TDM_ERROR_NONE;
137         tdm_display *dpy = tdm_display_init(&error);
138         EXPECT_TRUE(error == TDM_ERROR_NONE);
139         EXPECT_FALSE(dpy == NULL);
140         master_fd = tbm_drm_helper_get_master_fd();
141         tbm_fd = tbm_drm_helper_get_fd();
142         if (dpy != NULL && error == TDM_ERROR_NONE)
143                 tdm_display_deinit(dpy);
144         dpy = NULL;
145         tbm_bufmgr_deinit(tbm_bufmgr);
146         tbm_bufmgr = NULL;
147 }
148
149
150 TEST_F(TDMInit, DisplayInitFewTimesSuccessfulWithTBM)
151 {
152         tdm_error error = TDM_ERROR_NONE;
153         tdm_display *dpy[20] = {NULL};
154         tbm_bufmgr tbm_bufmgr = NULL;
155         tbm_bufmgr = tbm_bufmgr_init(-1);
156         ASSERT_FALSE(tbm_bufmgr == NULL);
157         dpy[0] = tdm_display_init(&error);
158         EXPECT_TRUE(error == TDM_ERROR_NONE);
159         EXPECT_FALSE(dpy[0] == NULL);
160         master_fd = tbm_drm_helper_get_master_fd();
161         tbm_fd = tbm_drm_helper_get_fd();
162         for (int i = 1; i < 20; i++) {
163                 dpy[i] = tdm_display_init(&error);
164                 EXPECT_TRUE(error == TDM_ERROR_NONE);
165                 EXPECT_FALSE(dpy[i] == NULL);
166                 EXPECT_EQ(dpy[0], dpy[i]);
167         }
168         for (int i = 19; i > 0; i--) {
169                 tdm_display_deinit(dpy[i]);
170                 if (master_fd > -1) {
171                         int temp_master_fd = tbm_drm_helper_get_master_fd();
172                         EXPECT_NE(temp_master_fd, -1);
173                         if (temp_master_fd > -1)
174                                 close(temp_master_fd);
175                 }
176                 if (tbm_fd > -1) {
177                         int temp_tbm_fd = tbm_drm_helper_get_fd();
178                         EXPECT_NE(temp_tbm_fd, -1);
179                         if (temp_tbm_fd > -1)
180                                 close(temp_tbm_fd);
181                 }
182         }
183         tdm_display_deinit(dpy[0]);
184         tbm_bufmgr_deinit(tbm_bufmgr);
185         tbm_bufmgr = NULL;
186 }
187
188 TEST_F(TDMInit, DisplayInitDeinitSuccessfulNullErrorWithTBM)
189 {
190         tbm_bufmgr tbm_bufmgr = NULL;
191         tbm_bufmgr = tbm_bufmgr_init(-1);
192         ASSERT_FALSE(tbm_bufmgr == NULL);
193         tdm_display *dpy = tdm_display_init(NULL);
194         EXPECT_FALSE(dpy == NULL);
195         master_fd = tbm_drm_helper_get_master_fd();
196         tbm_fd = tbm_drm_helper_get_fd();
197         if (dpy != NULL)
198                 tdm_display_deinit(dpy);
199         dpy = NULL;
200         tbm_bufmgr_deinit(tbm_bufmgr);
201         tbm_bufmgr = NULL;
202 }
203
204 TEST_F(TDMInit, DisplayInitDeinitSuccessfulFewTimesWithTBM)
205 {
206         for (int i = 0; i < 20; ++i) {
207                 tbm_bufmgr tbm_bufmgr = NULL;
208                 tbm_bufmgr = tbm_bufmgr_init(-1);
209                 ASSERT_FALSE(tbm_bufmgr == NULL);
210                 tdm_error error = TDM_ERROR_NONE;
211                 tdm_display *dpy = tdm_display_init(&error);
212                 EXPECT_TRUE(error == TDM_ERROR_NONE);
213                 EXPECT_FALSE(dpy == NULL);
214                 master_fd = tbm_drm_helper_get_master_fd();
215                 tbm_fd = tbm_drm_helper_get_fd();
216                 if (dpy != NULL)
217                         tdm_display_deinit(dpy);
218                 dpy = NULL;
219                 tbm_bufmgr_deinit(tbm_bufmgr);
220                 tbm_bufmgr = NULL;
221                 if (master_fd > -1) {
222                         int temp_master_fd = tbm_drm_helper_get_master_fd();
223                         EXPECT_EQ(temp_master_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
224                         if (temp_master_fd > -1)
225                                 exit(1);
226                         close(master_fd);
227                         master_fd = -1;
228                 }
229                 if (tbm_fd > -1) {
230                         int temp_tbm_fd = tbm_drm_helper_get_fd();
231                         EXPECT_EQ(temp_tbm_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
232                         if (temp_tbm_fd > -1)
233                                 exit(1);
234                         close(tbm_fd);
235                         tbm_fd = -1;
236                 }
237         }
238 }
239
240 TEST_F(TDMInit, DisplayDeinitSuccessfulNullDpyWithoutTBM)
241 {
242         tdm_error error = TDM_ERROR_NONE;
243         tdm_display *dpy = tdm_display_init(&error);
244         ASSERT_TRUE(error == TDM_ERROR_NONE);
245         ASSERT_FALSE(dpy == NULL);
246         master_fd = tbm_drm_helper_get_master_fd();
247         tbm_fd = tbm_drm_helper_get_fd();
248         tdm_display_deinit(NULL);
249         tdm_display_deinit(dpy);
250         dpy = NULL;
251 }
252
253 TEST_F(TDMInit, DisplayDeinitFailWrongDpyBadAddress)
254 {
255         tdm_error error = TDM_ERROR_NONE;
256         tdm_display *dpy = tdm_display_init(&error);
257         ASSERT_TRUE(error == TDM_ERROR_NONE);
258         ASSERT_FALSE(dpy == NULL);
259         master_fd = tbm_drm_helper_get_master_fd();
260         tbm_fd = tbm_drm_helper_get_fd();
261         EXPECT_EXIT({tdm_display *wrong_dpy = (tdm_display *) 0xBEAF;
262                                  tdm_display_deinit(wrong_dpy);
263                                  exit(0); }, ::testing::ExitedWithCode(0), "");
264         tdm_display_deinit(dpy);
265         dpy = NULL;
266 }
267
268 TEST_F(TDMInit, DisplayDeinitFailWrongDpyStackAddress)
269 {
270         tdm_error error = TDM_ERROR_NONE;
271         tdm_display *dpy = tdm_display_init(&error);
272         ASSERT_TRUE(error == TDM_ERROR_NONE);
273         ASSERT_FALSE(dpy == NULL);
274         master_fd = tbm_drm_helper_get_master_fd();
275         tbm_fd = tbm_drm_helper_get_fd();
276         EXPECT_EXIT({tdm_display *wrong_dpy = &error;
277                                  tdm_display_deinit(wrong_dpy);
278                                  exit(0); }, ::testing::ExitedWithCode(0), "");
279         tdm_display_deinit(dpy);
280         dpy = NULL;
281 }
282
283 TEST_F(TDMInit, DisplayDeinitFailWrongDpyHeapAddress)
284 {
285         tdm_error error = TDM_ERROR_NONE;
286         tdm_display *dpy = tdm_display_init(&error);
287         ASSERT_TRUE(error == TDM_ERROR_NONE);
288         ASSERT_FALSE(dpy == NULL);
289         master_fd = tbm_drm_helper_get_master_fd();
290         tbm_fd = tbm_drm_helper_get_fd();
291         EXPECT_EXIT({tdm_display *wrong_dpy = (tdm_display *) ((unsigned long) sbrk(0) - sizeof(unsigned long));
292                                  tdm_display_deinit(wrong_dpy);
293                                  exit(0); }, ::testing::ExitedWithCode(0), "");
294         tdm_display_deinit(dpy);
295         dpy = NULL;
296 }
297
298 TEST_F(TDMInit, DISABLED_DisplayDeinitFailRepeatWithSameDpy)
299 {
300         EXPECT_EXIT({tdm_error error = TDM_ERROR_NONE;
301                                  tdm_display *dpy = tdm_display_init(&error);
302                                  if (error != TDM_ERROR_NONE) exit(1);
303                                  tdm_display_deinit(dpy);
304                                  if (tbm_drm_helper_get_master_fd() != -1) exit(1);
305                                  if (tbm_drm_helper_get_fd() != -1) exit(1);
306                                  tdm_display_deinit(dpy);
307                                  tdm_display *dpy2 = malloc(2048);
308                                  *((unsigned int *)dpy2) = 673282346;
309                                  tdm_display_deinit(dpy2);
310                                  tdm_display_deinit(dpy);
311                                  tdm_display_deinit(dpy2);
312                                  exit(0); }, ::testing::ExitedWithCode(0), "");
313 }
314
315 TEST_F(TDMDefault, DisplayUpdateSuccessful)
316 {
317         ASSERT_TRUE(tdm_display_update(dpy) == TDM_ERROR_NONE);
318 }
319
320 TEST_F(TDMDefault, DisplayUpdateFailWrongDpy)
321 {
322         ASSERT_EXIT({tdm_display *wrong_dpy = (tdm_display *)0xBEAF;
323                                  tdm_display_update(wrong_dpy);
324                                  exit(0);}, ::testing::ExitedWithCode(0), "");
325 }
326
327 TEST_F(TDMDefault, DisplayUpdateFailNullDpy)
328 {
329         ASSERT_FALSE(tdm_display_update(NULL) == TDM_ERROR_NONE);
330 }
331
332 TEST_F(TDMDefault, DisplayGetFDSuccesful)
333 {
334         int fd = -42;
335         ASSERT_TRUE(tdm_display_get_fd(dpy, &fd) == TDM_ERROR_NONE);
336         ASSERT_FALSE(fd == -42);
337 }
338
339 TEST_F(TDMDefault, DisplayGetFDFailNullAll)
340 {
341         ASSERT_FALSE(tdm_display_get_fd(NULL, NULL) == TDM_ERROR_NONE);
342 }
343
344 TEST_F(TDMDefault, DisplayGetFDFailNullFD)
345 {
346         ASSERT_FALSE(tdm_display_get_fd(dpy, NULL) == TDM_ERROR_NONE);
347 }
348
349 TEST_F(TDMDefault, DisplayGetFDFailNullDpy)
350 {
351         int fd = -42;
352         ASSERT_FALSE(tdm_display_get_fd(NULL, &fd) == TDM_ERROR_NONE);
353 }
354
355 TEST_F(TDMDefault, DisplayGetFDFailWrongDpy)
356 {
357         ASSERT_EXIT({tdm_display *wrong_dpy = (tdm_display *) 0xBEAF; int fd = -42;
358                                  tdm_display_get_fd(wrong_dpy, &fd);
359                                  exit(0);}, ::testing::ExitedWithCode(0), "");
360 }
361
362 TEST_F(TDMDefault, DISABLED_DisplayHandleEventsSuccessful)
363 {
364         /* TODO Generate events*/
365         ASSERT_TRUE(tdm_display_handle_events(dpy) == TDM_ERROR_NONE);
366 }
367
368 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessful)
369 {
370         const char *name = NULL;
371         const char *vendor = NULL;
372         int major = -42, minor = -42;
373         ASSERT_TRUE(tdm_display_get_backend_info(dpy, &name, &vendor, &major, &minor) == TDM_ERROR_NONE);
374         ASSERT_FALSE(name == NULL);
375         ASSERT_FALSE(vendor == NULL);
376         ASSERT_FALSE(major == -42);
377         ASSERT_FALSE(minor == -42);
378 }
379
380 TEST_F(TDMDefault, DisplayGetBackendInfoFailNullAll)
381 {
382         ASSERT_FALSE(tdm_display_get_backend_info(NULL, NULL, NULL, NULL, NULL) == TDM_ERROR_NONE);
383 }
384
385 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyDpy)
386 {
387         ASSERT_TRUE(tdm_display_get_backend_info(dpy, NULL, NULL, NULL, NULL) == TDM_ERROR_NONE);
388 }
389
390 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyName)
391 {
392         const char *name = NULL;
393         ASSERT_TRUE(tdm_display_get_backend_info(dpy, &name, NULL, NULL, NULL) == TDM_ERROR_NONE);
394         ASSERT_FALSE(name == NULL);
395 }
396
397 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyVendor)
398 {
399         const char *vendor = NULL;
400         ASSERT_TRUE(tdm_display_get_backend_info(dpy, NULL, &vendor, NULL, NULL) == TDM_ERROR_NONE);
401         ASSERT_FALSE(vendor == NULL);
402 }
403
404 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyMajor)
405 {
406         int major = -42;
407         ASSERT_TRUE(tdm_display_get_backend_info(dpy, NULL, NULL, &major, NULL) == TDM_ERROR_NONE);
408         ASSERT_FALSE(major == -42);
409 }
410
411 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyMinor)
412 {
413         int minor = -42;
414         ASSERT_TRUE(tdm_display_get_backend_info(dpy, NULL, NULL, NULL, &minor) == TDM_ERROR_NONE);
415         ASSERT_FALSE(minor == -1);
416 }
417
418 TEST_F(TDMDefault, DisplayGetCapabilitiesSuccessful)
419 {
420         tdm_display_capability capabilities = (tdm_display_capability) -42;
421         ASSERT_TRUE(tdm_display_get_capabilities(dpy, &capabilities) == TDM_ERROR_NONE);
422         ASSERT_FALSE(capabilities == -1);
423 }
424
425 TEST_F(TDMDefault, DisplayGetCapabilitiesFailNullAll)
426 {
427         ASSERT_FALSE(tdm_display_get_capabilities(NULL, NULL) == TDM_ERROR_NONE);
428 }
429
430 TEST_F(TDMDefault, DisplayGetCapabilitiesFailSetOnlyDpy)
431 {
432         ASSERT_FALSE(tdm_display_get_capabilities(dpy, NULL) == TDM_ERROR_NONE);
433 }
434
435 TEST_F(TDMDefault, DisplayGetPPCapabilitiesSuccessful)
436 {
437         tdm_pp_capability capabilities = (tdm_pp_capability) -42;
438         tdm_error error = TDM_ERROR_NONE;
439         error = tdm_display_get_pp_capabilities(dpy, &capabilities);
440         ASSERT_TRUE(error == TDM_ERROR_NONE || error == TDM_ERROR_NO_CAPABILITY);
441         if (error == TDM_ERROR_NONE) {
442                 ASSERT_FALSE(capabilities == -42);
443         }
444 }
445
446 TEST_F(TDMDefault, DisplayGetPPCapabilitiesFailNullAll)
447 {
448         ASSERT_FALSE(tdm_display_get_pp_capabilities(NULL, NULL) == TDM_ERROR_NONE);
449 }
450
451 TEST_F(TDMDefault, DisplayGetCaptureCapabilitiesSuccessful)
452 {
453         tdm_capture_capability capabilities = (tdm_capture_capability) -42;
454         tdm_error error = TDM_ERROR_NONE;
455         error = tdm_display_get_capture_capabilities(dpy, &capabilities);
456         ASSERT_TRUE(error == TDM_ERROR_NONE || error == TDM_ERROR_NO_CAPABILITY);
457         if (error == TDM_ERROR_NONE) {
458                 ASSERT_FALSE(capabilities == -42);
459         }
460 }
461
462 TEST_F(TDMDefault, DisplayGetCaptureCapabilitiesFailNullAll)
463 {
464         ASSERT_FALSE(tdm_display_get_capture_capabilities(NULL, NULL) == TDM_ERROR_NONE);
465 }
466
467 TEST_F(TDMDefault, DisplayGetMaxLayerCountSuccessful)
468 {
469         int max_count = -42;
470         ASSERT_TRUE(TDM_ERROR_NONE == tdm_display_get_max_layer_count(dpy, &max_count));
471         ASSERT_NE(max_count, -42);
472 }
473
474 TEST_F(TDMDefault, DisplayGetMaxLayerCountFailNullAll)
475 {
476         ASSERT_FALSE(TDM_ERROR_NONE == tdm_display_get_max_layer_count(NULL, NULL));
477 }
478
479 TEST_F(TDMDefault, DisplayGetMaxLayerCountFailSetOnlyDpy)
480 {
481         ASSERT_FALSE(TDM_ERROR_NONE == tdm_display_get_max_layer_count(dpy, NULL));
482 }
483
484 TEST_F(TDMDefault, DisplayGetOutputCountSuccessful)
485 {
486         int count = -42;
487         ASSERT_TRUE(TDM_ERROR_NONE == tdm_display_get_output_count(dpy, &count));
488         ASSERT_FALSE(-42 == count);
489 }
490
491 TEST_F(TDMDefault, DisplayGetOutputCountFailNullALl)
492 {
493         ASSERT_FALSE(TDM_ERROR_NONE == tdm_display_get_output_count(NULL, NULL));
494 }
495
496 TEST_F(TDMDefault, DisplayGetOutputCountFailSetOnlyDpy)
497 {
498         ASSERT_FALSE(TDM_ERROR_NONE == tdm_display_get_output_count(dpy, NULL));
499 }