utests: fix errors in the vblank's test cases
[platform/core/uifw/libtdm.git] / utests / src / ut_tdm_layer.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
34 extern "C" {
35 #include "tdm.h"
36 #include "tbm_bufmgr.h"
37 #include "tbm_drm_helper.h"
38 }
39
40 class TDMLayer : public ::testing::Test {
41 protected:
42         tdm_display *dpy = NULL;
43         tbm_bufmgr tbm_bufmgr = NULL;
44         int master_fd = -42, tbm_fd = -42, layer_count = 0, output_count = 0;
45         tdm_layer ** tdm_layer_array = NULL;
46         bool has_layers = false;
47         void SetUp(void)
48         {
49                 setenv("TDM_DLOG", "1", 1);
50                 setenv("XDG_RUNTIME_DIR", ".", 1);
51                 setenv("TBM_DLOG", "1", 1);
52                 setenv("TBM_DISPLAY_SERVER", "1", 1);
53                 tbm_bufmgr = tbm_bufmgr_init(-1);
54                 ASSERT_FALSE(tbm_bufmgr == NULL);
55                 tdm_error error = TDM_ERROR_NONE;
56                 dpy = tdm_display_init(&error);
57                 ASSERT_TRUE(error == TDM_ERROR_NONE);
58                 ASSERT_FALSE(dpy == NULL);
59                 master_fd = tbm_drm_helper_get_master_fd();
60                 tbm_fd = tbm_drm_helper_get_fd();
61                 ASSERT_TRUE(tdm_display_get_output_count(dpy, &output_count) == TDM_ERROR_NONE);
62                 for (int i = 0; i < output_count; i++) {
63                         tdm_output * output = tdm_display_get_output(dpy, i, &error);
64                         if (TDM_ERROR_NONE != error || NULL == output)
65                                 continue;
66                         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
67                         if (TDM_ERROR_NONE != tdm_output_get_conn_status(output, &status))
68                                 continue;
69                         if (status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
70                                 continue;
71                         int temp_layer_count = 0;
72                         if (TDM_ERROR_NONE != tdm_output_get_layer_count(output, &temp_layer_count))
73                                 continue;
74                         if (0 == temp_layer_count)
75                                 continue;
76                         tdm_layer_array = (tdm_layer **) realloc(tdm_layer_array,
77                                                                                                          (layer_count + temp_layer_count)*sizeof(tdm_layer *));
78                         ASSERT_FALSE(NULL == tdm_layer_array);
79                         for (int k = layer_count; k < (layer_count + temp_layer_count); k++) {
80                                 tdm_layer_array[k] = tdm_output_get_layer(output, k, &error);
81                                 ASSERT_TRUE(TDM_ERROR_NONE == error);
82                                 ASSERT_FALSE(NULL == tdm_layer_array[k]);
83                         }
84                         layer_count+=temp_layer_count;
85                 }
86
87 #ifdef FAIL_ON_UNSUPPORTED
88                 ASSERT_GT(layer_count, 0);
89 #endif
90                 if (layer_count > 0)
91                         has_layers = true;
92         }
93         void TearDown(void)
94         {
95                 tdm_display_deinit(dpy);
96                 dpy = NULL;
97                 tbm_bufmgr_deinit(tbm_bufmgr);
98                 tbm_bufmgr = NULL;
99                 if (tdm_layer_array)
100                         free(tdm_layer_array);
101                 if (master_fd > -1) {
102                         int temp_master_fd = tbm_drm_helper_get_master_fd();
103                         EXPECT_EQ(temp_master_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
104                         if (temp_master_fd > -1)
105                                 exit(1);
106                         close(master_fd);
107                 }
108                 if (tbm_fd > -1) {
109                         int temp_tbm_fd = tbm_drm_helper_get_fd();
110                         EXPECT_EQ(temp_tbm_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
111                         if (temp_tbm_fd > -1)
112                                 exit(1);
113                         close(tbm_fd);
114                 }
115                 unsetenv("TDM_DLOG");
116                 unsetenv("XDG_RUNTIME_DIR");
117                 unsetenv("TBM_DLOG");
118                 unsetenv("TBM_DISPLAY_SERVER");
119         }
120 };
121
122 TEST_F(TDMLayer, LayerGetCapabilitiesSuccessful)
123 {
124         SKIP_FLAG(has_layers);
125         for (int i = 0; i < layer_count; i++) {
126                 tdm_layer_capability capabilities = (tdm_layer_capability) -42;
127                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_layer_get_capabilities(tdm_layer_array[i], &capabilities));
128                 ASSERT_NE(capabilities, -42);
129         }
130 }
131
132 TEST_F(TDMLayer, LayerGetCapabilitiesFailNullAll)
133 {
134         SKIP_FLAG(has_layers);
135         ASSERT_EXIT({if (tdm_layer_get_capabilities(NULL, NULL) == TDM_ERROR_NONE) exit(1);
136                                  exit(0);}, ::testing::ExitedWithCode(0), "");
137 }
138
139 TEST_F(TDMLayer, LayerGetCapabilitiesFailOnlyLayer)
140 {
141         SKIP_FLAG(has_layers);
142         ASSERT_EXIT({for (int i = 0; i < layer_count; i++) {
143                                          if (tdm_layer_get_capabilities(tdm_layer_array[i], NULL) == TDM_ERROR_NONE) exit(1);
144                                 }
145                                 exit(0);}, ::testing::ExitedWithCode(0), "");
146 }
147
148 TEST_F(TDMLayer, OutputGetPrimaryIndexSuccessful)
149 {
150         SKIP_FLAG(has_layers);
151         for (int i = 0; i < output_count; i++) {
152                 tdm_error error = TDM_ERROR_NONE;
153                 int index = -42;
154                 tdm_output * output = tdm_display_get_output(dpy, i, &error);
155                 ASSERT_FALSE(NULL == output);
156                 ASSERT_TRUE(TDM_ERROR_NONE == error);
157                 ASSERT_TRUE(TDM_ERROR_NONE == tdm_output_get_primary_index(output, &index));
158                 ASSERT_NE(index, -42);
159         }
160 }
161
162 TEST_F(TDMLayer, OutputGetPrimaryIndexFailNullAll)
163 {
164         SKIP_FLAG(has_layers);
165         ASSERT_EXIT({if (tdm_output_get_primary_index(NULL, NULL) == TDM_ERROR_NONE) exit(1);
166                                  exit(0);}, ::testing::ExitedWithCode(0), "");
167 }
168
169 TEST_F(TDMLayer, OutputGetPrimaryIndexFailOnlyOutput)
170 {
171         SKIP_FLAG(has_layers);
172         ASSERT_EXIT({for (int i = 0; i < output_count; i++) {
173                                          tdm_error error = TDM_ERROR_NONE;
174                                          tdm_output * output = tdm_display_get_output(dpy, i, &error);
175                                          if (NULL == output) exit(1);
176                                          if (TDM_ERROR_NONE != error) exit(1);
177                                          if (tdm_output_get_primary_index(output, NULL) == TDM_ERROR_NONE) exit(1);
178                                 }
179                                 exit(0);}, ::testing::ExitedWithCode(0), "");
180 }