[utest] Add 25 test cases.
[platform/core/uifw/libtdm.git] / utests / src / ut_tdm_output.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 #include "ut_common.h"
33 #include <climits>
34 extern "C" {
35 #include "tdm.h"
36 #include "tbm_bufmgr.h"
37 #include "tbm_drm_helper.h"
38 }
39
40 class TDMOutput : public ::testing::Test {
41 protected:
42         tdm_display *dpy = NULL;
43         int output_count = -42;
44         bool has_output = false;
45         tbm_bufmgr tbm_bufmgr = NULL;
46         static unsigned int handle_call;
47         static void tdm_output_change_handler_test_func(tdm_output *output,
48                                                                                                         tdm_output_change_type type,
49                                                                                                         tdm_value value,
50                                                                                                         void *u_data)
51         {
52                 if ( ((int) u_data) < -100) {
53                         TDMOutput::handle_call++;
54                 }
55         }
56         void SetUp(void)
57         {
58                 setenv("TDM_DLOG", "1", 1);
59                 setenv("XDG_RUNTIME_DIR", ".", 1);
60                 setenv("TBM_DLOG", "1", 1);
61                 setenv("TBM_DISPLAY_SERVER", "1", 1);
62                 tbm_bufmgr = tbm_bufmgr_init(-1);
63                 ASSERT_FALSE(tbm_bufmgr == NULL);
64                 tdm_error error = TDM_ERROR_NONE;
65                 dpy = tdm_display_init(&error);
66                 ASSERT_TRUE(error == TDM_ERROR_NONE);
67                 ASSERT_FALSE(dpy == NULL);
68                 error = tdm_display_get_output_count(dpy, &output_count);
69 #ifdef FAIL_ON_UNSUPPORTED
70                 ASSERT_GT(output_count, 0);
71 #endif
72                 if (output_count > 0)
73                         has_output = true;
74                 handle_call = 0;
75         }
76         void TearDown(void)
77         {
78                 tdm_display_deinit(dpy);
79                 dpy = NULL;
80                 tbm_bufmgr_deinit(tbm_bufmgr);
81                 tbm_bufmgr = NULL;
82                 unsetenv("TDM_DLOG");
83                 unsetenv("XDG_RUNTIME_DIR");
84                 unsetenv("TBM_DLOG");
85                 unsetenv("TBM_DISPLAY_SERVER");
86         }
87 };
88
89 unsigned int TDMOutput::handle_call = 0;
90
91 TEST_F(TDMOutput, DisplayGetOutputSuccessful)
92 {
93         SKIP_FLAG(has_output);
94         for (int i = 0; i < output_count; i++) {
95                 tdm_error error = TDM_ERROR_NONE;
96                 ASSERT_FALSE(NULL == tdm_display_get_output(dpy, i, &error));
97                 ASSERT_TRUE(TDM_ERROR_NONE == error);
98         }
99 }
100
101 TEST_F(TDMOutput, DisplayGetOutputSuccessfulWrongIndex)
102 {
103         SKIP_FLAG(has_output);
104         tdm_error error = TDM_ERROR_NONE;
105         ASSERT_TRUE(NULL == tdm_display_get_output(dpy, -1, &error));
106         ASSERT_TRUE(TDM_ERROR_NONE == error);
107 }
108
109 TEST_F(TDMOutput, DisplayGetOutputSuccessfulBigIndex)
110 {
111         SKIP_FLAG(has_output);
112         tdm_error error = TDM_ERROR_NONE;
113         ASSERT_TRUE(NULL == tdm_display_get_output(dpy, INT_MAX, &error));
114         ASSERT_TRUE(TDM_ERROR_NONE == error);
115 }
116
117 TEST_F(TDMOutput, DisplayGetOutputSuccessfulSmallIndex)
118 {
119         SKIP_FLAG(has_output);
120         tdm_error error = TDM_ERROR_NONE;
121         ASSERT_TRUE(NULL == tdm_display_get_output(dpy, INT_MIN, &error));
122         ASSERT_TRUE(TDM_ERROR_NONE == error);
123 }
124
125 TEST_F(TDMOutput, DisplayGetOutputSuccessfulErrorNull)
126 {
127         SKIP_FLAG(has_output);
128         ASSERT_FALSE(NULL == tdm_display_get_output(dpy, 0, NULL));
129 }
130
131 TEST_F(TDMOutput, DisplayOutputGetCapabilitiesSuccessful)
132 {
133         SKIP_FLAG(has_output);
134         for (int i = 0; i < output_count; i++) {
135                 tdm_error error = TDM_ERROR_NONE;
136                 tdm_output_capability capabilities = -42;
137                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
138                 ASSERT_FALSE(NULL == output);
139                 ASSERT_TRUE(TDM_ERROR_NONE == error);
140                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_get_capabilities(output, &capabilities));
141                 ASSERT_FALSE(-42 == capabilities);
142         }
143 }
144
145 TEST_F(TDMOutput, DisplayOutputGetCapabilitiesFailAllNull)
146 {
147         SKIP_FLAG(has_output);
148         ASSERT_FALSE(TDM_ERROR_NONE == tdm_output_get_capabilities(NULL, NULL));
149 }
150
151 TEST_F(TDMOutput, DisplayOutputGetCapabilitiesFailOnlyOutput)
152 {
153         SKIP_FLAG(has_output);
154         for (int i = 0; i < output_count; i++) {
155                 tdm_error error = TDM_ERROR_NONE;
156                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
157                 ASSERT_FALSE(NULL == output);
158                 ASSERT_TRUE(TDM_ERROR_NONE == error);
159                 ASSERT_FALSE(TDM_ERROR_NONE == tdm_output_get_capabilities(output, NULL));
160         }
161 }
162
163 TEST_F(TDMOutput, OutputGetModelInfoSuccessful)
164 {
165         SKIP_FLAG(has_output);
166         for (int i = 0; i < output_count; i++) {
167                 tdm_error error = TDM_ERROR_NONE;
168                 const char * maker = NULL, * model = NULL, * name = NULL;
169                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
170                 ASSERT_FALSE(NULL == output);
171                 ASSERT_TRUE(TDM_ERROR_NONE == error);
172                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_get_model_info(output, &maker, &model, &name));
173                 ASSERT_FALSE(NULL == maker);
174                 ASSERT_FALSE(NULL == model);
175                 ASSERT_FALSE(NULL == name);
176         }
177 }
178
179 TEST_F(TDMOutput, OutputGetModelInfoFailAllNull)
180 {
181         SKIP_FLAG(has_output);
182         ASSERT_FALSE(TDM_ERROR_NONE == tdm_output_get_model_info(NULL, NULL, NULL, NULL));
183 }
184
185 TEST_F(TDMOutput, OutputGetModelInfoSuccessfulOnlyOutput)
186 {
187         SKIP_FLAG(has_output);
188         for (int i = 0; i < output_count; i++) {
189                 tdm_error error = TDM_ERROR_NONE;
190                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
191                 ASSERT_FALSE(NULL == output);
192                 ASSERT_TRUE(TDM_ERROR_NONE == error);
193                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_get_model_info(output, NULL, NULL, NULL));
194         }
195 }
196
197 TEST_F(TDMOutput, OutputGetConnStatusSuccessful)
198 {
199         SKIP_FLAG(has_output);
200         for (int i = 0; i < output_count; i++) {
201                 tdm_error error = TDM_ERROR_NONE;
202                 tdm_output_conn_status status = -42;
203                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
204                 ASSERT_FALSE(NULL == output);
205                 ASSERT_TRUE(TDM_ERROR_NONE == error);
206                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_get_conn_status(output, &status));
207                 ASSERT_FALSE(-42 == status);
208         }
209 }
210
211 TEST_F(TDMOutput, OutputGetConnStatusFailAllNull)
212 {
213         SKIP_FLAG(has_output);
214         ASSERT_FALSE(TDM_ERROR_NONE == tdm_output_get_conn_status(NULL, NULL));
215 }
216
217 TEST_F(TDMOutput, OutputGetConnStatusFailOnlyOutput)
218 {
219         SKIP_FLAG(has_output);
220         for (int i = 0; i < output_count; i++) {
221                 tdm_error error = TDM_ERROR_NONE;
222                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
223                 ASSERT_FALSE(NULL == output);
224                 ASSERT_TRUE(TDM_ERROR_NONE == error);
225                 ASSERT_FALSE(TDM_ERROR_NONE == tdm_output_get_conn_status(output, NULL));
226         }
227 }
228
229 TEST_F(TDMOutput, OutputSetDPMSSuccessful)
230 {
231         SKIP_FLAG(has_output);
232         for (int i = 0; i < output_count; i++) {
233                 tdm_error error = TDM_ERROR_NONE;
234                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
235                 ASSERT_FALSE(NULL == output);
236                 ASSERT_TRUE(TDM_ERROR_NONE == error);
237                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_ON));
238                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_STANDBY));
239                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_SUSPEND));
240                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_OFF));
241         }
242 }
243
244 TEST_F(TDMOutput, OutputAddChangeHandlerSuccessful)
245 {
246         SKIP_FLAG(has_output);
247         for (int i = 0; i < output_count; i++) {
248                 tdm_error error = TDM_ERROR_NONE;
249                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
250                 ASSERT_FALSE(NULL == output);
251                 ASSERT_TRUE(TDM_ERROR_NONE == error);
252                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_add_change_handler(output,
253                                                                                                                                         tdm_output_change_handler_test_func,
254                                                                                                                                         -101));
255                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_ON));
256                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_OFF));
257                 ASSERT_GT(handle_call, 0);
258         }
259 }
260
261 TEST_F(TDMOutput, OutputAddChangeHandlerSuccessfulFewFuncs)
262 {
263         SKIP_FLAG(has_output);
264         for (int i = 0; i < output_count; i++) {
265                 tdm_error error = TDM_ERROR_NONE;
266                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
267                 ASSERT_FALSE(NULL == output);
268                 ASSERT_TRUE(TDM_ERROR_NONE == error);
269                 for (int k = 0; k < 20; k++) {
270                         ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_add_change_handler(output,
271                                                                                                                                                 tdm_output_change_handler_test_func,
272                                                                                                                                                 (-101-k)));
273                 }
274                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_ON));
275                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_OFF));
276                 ASSERT_GT(handle_call, 20);
277         }
278 }
279
280
281 TEST_F(TDMOutput, OutputAddChangeHandlerFailAllNull)
282 {
283         SKIP_FLAG(has_output);
284         ASSERT_FALSE(TDM_ERROR_NONE ==tdm_output_add_change_handler(NULL, NULL, NULL));
285 }
286
287 TEST_F(TDMOutput, OutputAddChangeHandlerFailOnlyOutput)
288 {
289         SKIP_FLAG(has_output);
290         for (int i = 0; i < output_count; i++) {
291                 tdm_error error = TDM_ERROR_NONE;
292                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
293                 ASSERT_FALSE(NULL == output);
294                 ASSERT_TRUE(TDM_ERROR_NONE == error);
295                 ASSERT_FALSE(TDM_ERROR_NONE == tdm_output_add_change_handler(output, NULL, NULL));
296         }
297 }
298
299 TEST_F(TDMOutput, OutputAddChangeHandlerFailWrongOutput)
300 {
301         SKIP_FLAG(has_output);
302         ASSERT_EXIT({tdm_output *output = 0xBEAF;
303                                  tdm_output_add_change_handler(output,
304                                                                                            tdm_output_change_handler_test_func,
305                                                                                            -101);
306                                  exit(0);}, ::testing::ExitedWithCode(0), "");
307 }
308
309 TEST_F(TDMOutput, OutputRemoveChangeHandlerSuccessful)
310 {
311         for (int i = 0; i < output_count; i++) {
312                 tdm_error error = TDM_ERROR_NONE;
313                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
314                 ASSERT_FALSE(NULL == output);
315                 ASSERT_TRUE(TDM_ERROR_NONE == error);
316                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_add_change_handler(output,
317                                                                                                                                         tdm_output_change_handler_test_func,
318                                                                                                                                         -101));
319                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_ON));
320                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_OFF));
321                 ASSERT_GT(handle_call, 0);
322                 handle_call = 0;
323                 tdm_output_remove_change_handler(output, tdm_output_change_handler_test_func, -101);
324                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_ON));
325                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_OFF));
326                 ASSERT_EQ(handle_call, 0);
327         }
328 }
329
330 TEST_F(TDMOutput, OutputRemoveChangeHandlerSuccessfulFewFuncs)
331 {
332         for (int i = 0; i < output_count; i++) {
333                 tdm_error error = TDM_ERROR_NONE;
334                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
335                 ASSERT_FALSE(NULL == output);
336                 ASSERT_TRUE(TDM_ERROR_NONE == error);
337                 for (int k = 0; k < 20; k++) {
338                         ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_add_change_handler(output,
339                                                                                                                                                 tdm_output_change_handler_test_func,
340                                                                                                                                                 (-101-k)));
341                 }
342                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_ON));
343                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_OFF));
344                 ASSERT_GT(handle_call, 20);
345                 handle_call = 0;
346                 for (int k = 0; k < 20; k++) {
347                         tdm_output_remove_change_handler(output, tdm_output_change_handler_test_func, (-101-k));
348                 }
349                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_ON));
350                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_OFF));
351                 ASSERT_EQ(handle_call, 0);
352         }
353 }