utest: correct wrong behaviors
[platform/core/uifw/libtdm.git] / utests / src / ut_tdm_env.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 "ut_tdm.h"
32
33 void TDMEnv::SetUp(void)
34 {
35         const char *test_backend;
36         int thread = 1;
37
38         TDM_UT_ENTRY();
39
40         setenv("XDG_RUNTIME_DIR", "/run", 1);
41         setenv("TBM_DISPLAY_SERVER", "1", 1);
42
43 #ifdef TDM_UT_TEST_WITH_PARAMS
44         /* commit_per_vblank depends on backend. can't choose it in frontend side.
45          * this is only for testing.
46          */
47         int commit_per_vblank = ::testing::get<0>(GetParam());
48         tdm_config_set_int(TDM_CONFIG_KEY_GENERAL_COMMIT_PER_VBLANK, commit_per_vblank);
49
50         thread = ::testing::get<1>(GetParam());
51         test_backend = ::testing::get<2>(GetParam());
52 #else
53         test_backend = ::testing::get<0>(GetParam());
54 #endif
55
56         tdm_config_set_int(TDM_CONFIG_KEY_GENERAL_THREAD, thread);
57
58         if (!test_backend)
59                 test_backend = TDM_DEFAULT_MODULE;
60         tdm_config_set_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, test_backend);
61
62         const char *debug = getenv("TDM_UT_DEBUG_MODULE");
63         if (debug && strstr(debug, "1"))
64                 tdm_config_set_string(TDM_CONFIG_KEY_DEBUG_MODULE, "buffer,thread,event,vblank,commit,pp,capture");
65 }
66
67 void TDMEnv::TearDown(void)
68 {
69         unsetenv("XDG_RUNTIME_DIR");
70         unsetenv("TBM_DISPLAY_SERVER");
71 }
72
73 TEST_P(TDMEnv, DisplayInitDeinit)
74 {
75         tdm_display *dpy;
76         tdm_error ret;
77
78         dpy = tdm_display_init(&ret);
79         ASSERT_TRUE(ret == TDM_ERROR_NONE);
80         ASSERT_TRUE(dpy != NULL);
81
82         tdm_display_deinit(dpy);
83 }
84
85 TEST_P(TDMEnv, DisplayInitDeinitWithoutEnv)
86 {
87         tdm_display *dpy;
88         tdm_error ret;
89
90         TDMEnv::TearDown();
91
92         dpy = tdm_display_init(&ret);
93         ASSERT_TRUE(ret == TDM_ERROR_OPERATION_FAILED);
94         ASSERT_TRUE(dpy == NULL);
95 }
96
97 TEST_P(TDMEnv, DisplayInitFewTimes)
98 {
99         tdm_display *dpy[10];
100         int d;
101         tdm_error ret;
102
103         for (d = 0; d < 10; d++) {
104                 dpy[d] = tdm_display_init(&ret);
105                 ASSERT_TRUE(ret == TDM_ERROR_NONE);
106                 ASSERT_TRUE(dpy[d] != NULL);
107         }
108
109         for (d = 0; d < 9; d++)
110                 ASSERT_TRUE(dpy[d] == dpy[d + 1]);
111
112         for (d = 0; d < 10; d++)
113                 tdm_display_deinit(dpy[d]);
114 }
115
116 TEST_P(TDMEnv, DisplayInitDeinitWithTBM)
117 {
118         tdm_display *dpy;
119         tbm_bufmgr bufmgr;
120         tdm_error ret;
121
122         bufmgr = tbm_bufmgr_init(-1);
123         ASSERT_TRUE(bufmgr != NULL);
124
125         dpy = tdm_display_init(&ret);
126         ASSERT_TRUE(ret == TDM_ERROR_NONE);
127         ASSERT_TRUE(dpy != NULL);
128
129         tdm_display_deinit(dpy);
130         tbm_bufmgr_deinit(bufmgr);
131 }
132
133 TEST_P(TDMEnv, DisplayInitDeinitWithoutBackends)
134 {
135         ASSERT_TRUE(1);
136 }
137
138 TEST_P(TDMEnv, DisplayInitDeinitWrongDpyBadAddress)
139 {
140         tdm_display *dpy;
141         tdm_error ret;
142
143         dpy = tdm_display_init(&ret);
144         ASSERT_TRUE(ret == TDM_ERROR_NONE);
145         ASSERT_TRUE(dpy != NULL);
146
147         EXPECT_EXIT({
148                 tdm_display *wrong_dpy = (tdm_display *)TDM_UT_INVALID_VALUE;
149                 tdm_display_deinit(wrong_dpy);
150                 exit(0);
151         }, ::testing::ExitedWithCode(0), "");
152
153         tdm_display_deinit(dpy);
154 }
155
156 TEST_P(TDMEnv, DisplayDeinitWithNULL)
157 {
158         tdm_display_deinit(NULL);
159 }
160
161 TEST_P(TDMEnv, DisplayDeinitRepeatWithSameDpy)
162 {
163         tdm_display *dpy;
164         tdm_error ret;
165
166         dpy = tdm_display_init(&ret);
167         ASSERT_TRUE(ret == TDM_ERROR_NONE);
168         ASSERT_TRUE(dpy != NULL);
169
170         EXPECT_EXIT({
171                 tdm_display_deinit(dpy);
172                 tdm_display_deinit(dpy);
173                 exit(0);
174         }, ::testing::ExitedWithCode(0), "");
175
176         tdm_display_deinit(dpy);
177 }
178
179 #ifdef TDM_UT_TEST_WITH_PARAMS
180 INSTANTIATE_TEST_CASE_P(TDMEnvParams,
181                                                 TDMEnv,
182                                                 Combine(Bool(), Bool(), Values(TDM_DEFAULT_MODULE)));
183 #else
184 INSTANTIATE_TEST_CASE_P(TDMEnvParams,
185                                                 TDMEnv,
186                                                 Values(TDM_DEFAULT_MODULE));
187 #endif