1 /**************************************************************************
3 * Copyright 2016 Samsung Electronics co., Ltd. All Rights Reserved.
5 * Contact: Konstantin Drabeniuk <k.drabeniuk@samsung.com>
6 * Contact: Andrii Sokolenko <a.sokolenko@samsung.com>
7 * Contact: Roman Marchenko <r.marchenko@samsung.com>
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:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
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.
29 **************************************************************************/
31 #include "gtest/gtest.h"
34 #include "tbm_bufmgr.h"
35 #include "tbm_drm_helper.h"
38 class TDMInit : public ::testing::Test {
40 int master_fd = -42, tbm_fd = -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);
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)
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;
65 unsetenv("XDG_RUNTIME_DIR");
67 unsetenv("TBM_DISPLAY_SERVER");
71 class TDMDefault : public ::testing::Test {
73 tdm_display *dpy = NULL;
74 tbm_bufmgr tbm_bufmgr = NULL;
75 int master_fd = -42, tbm_fd = -42;
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();
93 tdm_display_deinit(dpy);
94 tbm_bufmgr_deinit(tbm_bufmgr);
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)
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)
111 unsetenv("TDM_DLOG");
112 unsetenv("XDG_RUNTIME_DIR");
113 unsetenv("TBM_DLOG");
114 unsetenv("TBM_DISPLAY_SERVER");
119 TEST_F(TDMInit, DisplayInitDeinitSuccessfulWithoutTBM)
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);
131 TEST_F(TDMInit, DisplayInitDeinitSuccessfulWithTBM)
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);
145 tbm_bufmgr_deinit(tbm_bufmgr);
150 TEST_F(TDMInit, DisplayInitFewTimesSuccessfulWithTBM)
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]);
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);
177 int temp_tbm_fd = tbm_drm_helper_get_fd();
178 EXPECT_NE(temp_tbm_fd, -1);
179 if (temp_tbm_fd > -1)
183 tdm_display_deinit(dpy[0]);
184 tbm_bufmgr_deinit(tbm_bufmgr);
188 TEST_F(TDMInit, DisplayInitDeinitSuccessfulNullErrorWithTBM)
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();
198 tdm_display_deinit(dpy);
200 tbm_bufmgr_deinit(tbm_bufmgr);
204 TEST_F(TDMInit, DisplayInitDeinitSuccessfulFewTimesWithTBM)
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();
217 tdm_display_deinit(dpy);
219 tbm_bufmgr_deinit(tbm_bufmgr);
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)
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)
240 TEST_F(TDMInit, DisplayDeinitSuccessfulNullDpyWithoutTBM)
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);
253 TEST_F(TDMInit, DisplayDeinitFailWrongDpyBadAddress)
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);
268 TEST_F(TDMInit, DisplayDeinitFailWrongDpyStackAddress)
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);
283 TEST_F(TDMInit, DisplayDeinitFailWrongDpyHeapAddress)
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);
298 TEST_F(TDMInit, DISABLED_DisplayDeinitFailRepeatWithSameDpy)
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), "");
315 TEST_F(TDMDefault, DisplayUpdateSuccessful)
317 ASSERT_TRUE(tdm_display_update(dpy) == TDM_ERROR_NONE);
320 TEST_F(TDMDefault, DisplayUpdateFailWrongDpy)
322 ASSERT_EXIT({tdm_display *wrong_dpy = (tdm_display *)0xBEAF;
323 tdm_display_update(wrong_dpy);
324 exit(0);}, ::testing::ExitedWithCode(0), "");
327 TEST_F(TDMDefault, DisplayUpdateFailNullDpy)
329 ASSERT_FALSE(tdm_display_update(NULL) == TDM_ERROR_NONE);
332 TEST_F(TDMDefault, DisplayGetFDSuccesful)
335 ASSERT_TRUE(tdm_display_get_fd(dpy, &fd) == TDM_ERROR_NONE);
336 ASSERT_FALSE(fd == -42);
339 TEST_F(TDMDefault, DisplayGetFDFailNullAll)
341 ASSERT_FALSE(tdm_display_get_fd(NULL, NULL) == TDM_ERROR_NONE);
344 TEST_F(TDMDefault, DisplayGetFDFailNullFD)
346 ASSERT_FALSE(tdm_display_get_fd(dpy, NULL) == TDM_ERROR_NONE);
349 TEST_F(TDMDefault, DisplayGetFDFailNullDpy)
352 ASSERT_FALSE(tdm_display_get_fd(NULL, &fd) == TDM_ERROR_NONE);
355 TEST_F(TDMDefault, DisplayGetFDFailWrongDpy)
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), "");
362 TEST_F(TDMDefault, DISABLED_DisplayHandleEventsSuccessful)
364 /* TODO Generate events*/
365 ASSERT_TRUE(tdm_display_handle_events(dpy) == TDM_ERROR_NONE);
368 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessful)
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);
380 TEST_F(TDMDefault, DisplayGetBackendInfoFailNullAll)
382 ASSERT_FALSE(tdm_display_get_backend_info(NULL, NULL, NULL, NULL, NULL) == TDM_ERROR_NONE);
385 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyDpy)
387 ASSERT_TRUE(tdm_display_get_backend_info(dpy, NULL, NULL, NULL, NULL) == TDM_ERROR_NONE);
390 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyName)
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);
397 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyVendor)
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);
404 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyMajor)
407 ASSERT_TRUE(tdm_display_get_backend_info(dpy, NULL, NULL, &major, NULL) == TDM_ERROR_NONE);
408 ASSERT_FALSE(major == -42);
411 TEST_F(TDMDefault, DisplayGetBackendInfoSuccessfulSetOnlyMinor)
414 ASSERT_TRUE(tdm_display_get_backend_info(dpy, NULL, NULL, NULL, &minor) == TDM_ERROR_NONE);
415 ASSERT_FALSE(minor == -1);
418 TEST_F(TDMDefault, DisplayGetCapabilitiesSuccessful)
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);
425 TEST_F(TDMDefault, DisplayGetCapabilitiesFailNullAll)
427 ASSERT_FALSE(tdm_display_get_capabilities(NULL, NULL) == TDM_ERROR_NONE);
430 TEST_F(TDMDefault, DisplayGetCapabilitiesFailSetOnlyDpy)
432 ASSERT_FALSE(tdm_display_get_capabilities(dpy, NULL) == TDM_ERROR_NONE);
435 TEST_F(TDMDefault, DisplayGetPPCapabilitiesSuccessful)
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);
446 TEST_F(TDMDefault, DisplayGetPPCapabilitiesFailNullAll)
448 ASSERT_FALSE(tdm_display_get_pp_capabilities(NULL, NULL) == TDM_ERROR_NONE);
451 TEST_F(TDMDefault, DisplayGetCaptureCapabilitiesSuccessful)
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);
462 TEST_F(TDMDefault, DisplayGetCaptureCapabilitiesFailNullAll)
464 ASSERT_FALSE(tdm_display_get_capture_capabilities(NULL, NULL) == TDM_ERROR_NONE);
467 TEST_F(TDMDefault, DisplayGetMaxLayerCountSuccessful)
470 ASSERT_TRUE(TDM_ERROR_NONE == tdm_display_get_max_layer_count(dpy, &max_count));
471 ASSERT_NE(max_count, -42);
474 TEST_F(TDMDefault, DisplayGetMaxLayerCountFailNullAll)
476 ASSERT_FALSE(TDM_ERROR_NONE == tdm_display_get_max_layer_count(NULL, NULL));
479 TEST_F(TDMDefault, DisplayGetMaxLayerCountFailSetOnlyDpy)
481 ASSERT_FALSE(TDM_ERROR_NONE == tdm_display_get_max_layer_count(dpy, NULL));
484 TEST_F(TDMDefault, DisplayGetOutputCountSuccessful)
487 ASSERT_TRUE(TDM_ERROR_NONE == tdm_display_get_output_count(dpy, &count));
488 ASSERT_FALSE(-42 == count);
491 TEST_F(TDMDefault, DisplayGetOutputCountFailNullALl)
493 ASSERT_FALSE(TDM_ERROR_NONE == tdm_display_get_output_count(NULL, NULL));
496 TEST_F(TDMDefault, DisplayGetOutputCountFailSetOnlyDpy)
498 ASSERT_FALSE(TDM_ERROR_NONE == tdm_display_get_output_count(dpy, NULL));