b4a949629d1d70f8f0735a80183c4ec5cca76a88
[platform/core/uifw/libtdm.git] / ut / 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  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27 **************************************************************************/
28
29 #include "../stubs/stub_dlfcn.h"
30 #include "gtest/gtest.h"
31
32 /*------ stubs -----------------*/
33 #include "tdm_event_loop_stubs.h"
34 #include "tbm_stubs.h"
35 #include "stub_pthread.h"
36 #include "stub_tdm_vblank.h"
37 #include "stub_stdlib.h"
38 #include "stub_stdio.h"
39 #include "stub_unistd.h"
40 #include "tdm_event_loop_stubs.h"
41 #include "tdm.c"
42 /*------ tested file -----------*/
43
44 static void _init_test()
45 {
46         stub_pthread_init();
47         stub_tbm_init();
48         stub_stdlib_init();
49
50         stub_dlfcn_init();
51         stub_stdio_init();
52         stub_tdm_vblank_init();
53         g_private_display = NULL;
54 }
55
56 extern tdm_backend_module tdm_backend_module_stub;
57
58
59 /* tdm_display_update */
60
61 TEST(tdm_display_update, work_flow_success_1)
62 {
63         tdm_error error = TDM_ERROR_NONE;
64         tdm_error expected_error = TDM_ERROR_INVALID_PARAMETER;
65
66         _init_test();
67
68         error = tdm_display_update(NULL);
69
70         ASSERT_EQ(error, expected_error);
71 }
72
73 /* tdm_display_init() */
74 TEST(tdm_display_init, work_flow_success_1___first_call)
75 {
76         tdm_error error = TDM_ERROR_BAD_REQUEST;
77         tdm_display *disp;
78
79         _init_test();
80
81         //to download tdm_backend_module;
82         stub_dlfcn_ctrl.dlopen_returned_val = (void*)1;
83         stub_dlfcn_ctrl.dlsym_returned_val = &tdm_backend_module_stub;
84
85         setenv("TDM_DEBUG_MODULE", "all", 1);
86         setenv("TDM_DEBUG_DUMP", "capture,pp,layer,all", 1);
87         setenv("TDM_DEBUG_PATH", "log.txt", 1);
88         stub_getenv_return_real_value = 1;
89
90         disp = tdm_display_init(&error);
91
92         ASSERT_EQ(TDM_ERROR_NONE, error);
93         ASSERT_TRUE(g_private_display != NULL);
94         ASSERT_TRUE(g_private_display == disp);
95         ASSERT_EQ(1, g_private_display->init_count);
96 }
97
98 TEST(tdm_display_init, work_flow_success_2___second_call)
99 {
100         tdm_error error = TDM_ERROR_BAD_REQUEST;
101         tdm_display *disp;
102         tdm_private_display display;
103
104         _init_test();
105         memset(&display, 0, sizeof(display));
106         g_private_display = &display;
107         g_private_display->init_count = 1;
108
109         disp = tdm_display_init(&error);
110
111         ASSERT_EQ(TDM_ERROR_NONE, error);
112         ASSERT_TRUE(g_private_display != NULL);
113         ASSERT_TRUE(g_private_display == disp);
114         ASSERT_EQ(2, g_private_display->init_count);
115 }
116
117
118 TEST(tdm_display_init, work_flow_error_1___calloc_error)
119 {
120         tdm_error error = TDM_ERROR_BAD_REQUEST;
121         tdm_display *disp;
122
123         _init_test();
124         CALLOC_ERROR = 1;
125
126         disp = tdm_display_init(&error);
127
128         ASSERT_EQ(TDM_ERROR_OUT_OF_MEMORY, error);
129         ASSERT_TRUE(disp == NULL);
130         ASSERT_TRUE(g_private_display == NULL);
131 }
132
133 TEST(tdm_display_init, work_flow_error_2___mutex_init_error)
134 {
135         tdm_error error = TDM_ERROR_BAD_REQUEST;
136         tdm_display *disp;
137
138         _init_test();
139         PTHREAD_MUTEX_INIT_ERROR = 1;
140
141         disp = tdm_display_init(&error);
142
143         ASSERT_NE(TDM_ERROR_NONE, error);
144         ASSERT_TRUE(disp == NULL);
145         ASSERT_TRUE(g_private_display == NULL);
146 }
147
148 /* tdm_display_deinit */
149
150 TEST(tdm_display_deinit, work_flow_success_1___init_count_more_then_one)
151 {
152         tdm_private_display *dpy;
153         unsigned int expected = 2;
154         unsigned int actual;
155
156         _init_test();
157
158         dpy = (tdm_private_display *)calloc(1, sizeof(tdm_private_display));
159         g_private_display = dpy;
160         dpy->init_count = 3;
161
162         tdm_display_deinit(dpy);
163
164         actual = dpy->init_count;
165
166         free(dpy);
167
168         ASSERT_NE(g_private_display, NULL);
169         ASSERT_EQ(actual, expected);
170 }
171
172 TEST(tdm_display_deinit, work_flow_success_2___init_count_is_one)
173 {
174         unsigned int expected = 2;
175         unsigned int actual;
176
177         tdm_error error = TDM_ERROR_BAD_REQUEST;
178         tdm_display *disp;
179
180         _init_test();
181
182         //to download tdm_backend_module;
183         stub_dlfcn_ctrl.dlopen_returned_val = (void*)1;
184         stub_dlfcn_ctrl.dlsym_returned_val = &tdm_backend_module_stub;
185
186         disp = tdm_display_init(&error);
187
188         ASSERT_EQ(TDM_ERROR_NONE, error);
189
190         //call tested function
191         tdm_display_deinit(disp);
192
193         ASSERT_EQ(NULL, g_private_display);
194         ASSERT_TRUE(FREE_CALLED);
195 }
196
197
198 /* tdm_display_check_module_abi */
199 TEST(tdm_display_check_module_abi, work_flow_success_1)
200 {
201         int res;
202         tdm_private_display private_display;
203         tdm_backend_module module;
204         private_display.module_data = &module;
205         module.abi_version = 0x00010001;
206
207         res = tdm_display_check_module_abi(&private_display, 1, 1);
208
209         ASSERT_EQ(1, res);
210 }
211
212 TEST(tdm_display_check_module_abi, work_flow_error_1___wrong_version)
213 {
214         int res;
215         tdm_private_display private_display;
216         tdm_backend_module module;
217         private_display.module_data = &module;
218         module.abi_version = 0x00010001;
219
220         /*minor*/
221         res = tdm_display_check_module_abi(&private_display, 1, 2);
222         ASSERT_EQ(0, res);
223
224         /*major*/
225         res = tdm_display_check_module_abi(&private_display, 2, 1);
226         ASSERT_EQ(0, res);
227 }
228
229 /* tdm_display_check_module_abi */
230 TEST(tdm_display_enable_debug_module, work_flow_success_1)
231 {
232         tdm_error error;
233
234         //check all
235         error = tdm_display_enable_debug_module("all");
236         ASSERT_EQ(0xFFFFFFFF, tdm_debug_module);
237         ASSERT_EQ(TDM_ERROR_NONE, error);
238
239         //check none
240         error = tdm_display_enable_debug_module("none");
241         ASSERT_EQ(0, tdm_debug_module);
242         ASSERT_EQ(TDM_ERROR_NONE, error);
243
244         //check buffer,thread,mutex,vblank
245         error = tdm_display_enable_debug_module("buffer,thread,mutex,vblank");
246         ASSERT_EQ((TDM_DEBUG_BUFFER | TDM_DEBUG_THREAD | TDM_DEBUG_MUTEX | TDM_DEBUG_VBLANK) ,
247                   tdm_debug_module);
248         ASSERT_EQ(TDM_ERROR_NONE, error);
249
250 }
251
252 TEST(tdm_display_enable_debug_module, work_flow_error_1___bad_module)
253 {
254         tdm_error error;
255
256         //check bufer
257         error = tdm_display_enable_debug_module("bufer");
258         ASSERT_EQ(0 , tdm_debug_module);
259         ASSERT_NE(TDM_ERROR_NONE, error);
260 }
261
262 /* tdm_display_enable_ttrace_vblank */
263 TEST(tdm_display_enable_ttrace_vblank, work_flow_success_1)
264 {
265         tdm_error error;
266         tdm_private_display private_display;
267         tdm_private_output output;
268         tdm_output_mode current_mode;
269         int vblank = 1;
270
271         _init_test();
272         stub_tdm_vblank_create_returned = &vblank;
273
274         output.current_mode = &current_mode;
275         output.private_display = &private_display;
276
277         error = tdm_display_enable_ttrace_vblank(&private_display, &output, 1);
278         ASSERT_EQ(TDM_ERROR_NONE, error);
279
280         error = tdm_display_enable_ttrace_vblank(&private_display, &output, 0);
281 }
282
283 TEST(tdm_display_enable_ttrace_vblank, work_flow_error_1___vblank_wait_fail)
284 {
285         tdm_error error;
286         tdm_private_display private_display;
287         tdm_private_output output;
288         tdm_output_mode current_mode;
289         int vblank = 1;
290
291         _init_test();
292
293         output.current_mode = &current_mode;
294         output.private_display = &private_display;
295         stub_tdm_vblank_create_returned = &vblank;
296
297         stub_tdm_vblank_wait_error = 1;
298
299         error = tdm_display_enable_ttrace_vblank(&private_display, &output, 1);
300         ASSERT_NE(TDM_ERROR_NONE, error);
301 }
302
303 TEST(tdm_display_enable_ttrace_vblank, work_flow_error_2___vblank_create_fail)
304 {
305         tdm_error error;
306         tdm_private_display private_display;
307         tdm_private_output output;
308         tdm_output_mode current_mode;
309
310         _init_test();
311         stub_tdm_vblank_create_returned = NULL;
312
313         output.current_mode = &current_mode;
314         output.private_display = &private_display;
315
316         error = tdm_display_enable_ttrace_vblank(&private_display, &output, 1);
317         ASSERT_EQ(TDM_ERROR_NONE, error);
318 }
319