change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / tests / tc_scaler.cpp
1 #include "tc_main.h"
2 #include "mockclient.h"
3 #include "mockcompositor.h"
4
5 #include <cstring>
6 #include <libds/log.h>
7 #include <libds-tizen/scaler.h>
8 #include <scaler-client-protocol.h>
9
10 TEST(ScalerSimpleTest, CreateScaler)
11 {
12     struct wl_display *display = wl_display_create();
13
14     struct ds_tizen_scaler *scaler = ds_tizen_scaler_create(display);
15     ASSERT_NE(scaler, nullptr);
16
17     wl_display_destroy(display);
18 }
19
20 class ScalerCompositor : public MockCompositor, public ::testing::Test
21 {
22 public:
23     ScalerCompositor() :
24         MockCompositor(&ScalerCompositor::SetUpComp, this)
25     {
26         /* We are only interested in test results. Let's silence libds log. */
27         ds_log_init(DS_SILENT, nullptr);
28     }
29
30     static void SetUpComp(void *data)
31     {
32         ScalerCompositor *comp = static_cast<ScalerCompositor*>(data);
33
34         struct ds_tizen_scaler *scaler =
35             ds_tizen_scaler_create(comp->GetWlDisplay());
36         ASSERT_NE(scaler, nullptr);
37     }
38 };
39
40 static void handle_global(void *data, struct wl_registry *registry,
41     uint32_t name, const char *interface, uint32_t version);
42 static void handle_global_remove(void *data, struct wl_registry *registry,
43         uint32_t name);
44
45 static const struct wl_registry_listener registry_listener = {
46     .global = handle_global,
47     .global_remove = handle_global_remove,
48 };
49
50 class MockScalerClient : public MockClient
51 {
52 public:
53     MockScalerClient() : MockClient(&registry_listener, this)
54     {
55         EXPECT_NE(this->compositor, nullptr);
56         EXPECT_NE(this->scaler, nullptr);
57
58         surface = wl_compositor_create_surface(this->compositor);
59         EXPECT_NE(this->surface, nullptr);
60     }
61
62     ~MockScalerClient() 
63     {
64         wl_surface_destroy(this->surface);
65         wl_scaler_destroy(this->scaler);
66         wl_compositor_destroy(this->compositor);
67     }
68
69     struct wl_compositor *compositor;
70     struct wl_scaler *scaler;
71     struct wl_surface *surface;
72 };
73
74 static void
75 handle_global(void *data, struct wl_registry *registry,
76     uint32_t name, const char *interface, uint32_t version)
77 {
78     MockScalerClient *client = static_cast<MockScalerClient*>(data);
79
80     if (strcmp(interface, "wl_compositor") == 0) {
81         client->compositor = static_cast<struct wl_compositor*>(
82                 wl_registry_bind(registry, name, &wl_compositor_interface, 4));
83     } else if (strcmp(interface, "wl_scaler") == 0) {
84         client->scaler = static_cast<struct wl_scaler*>(
85                 wl_registry_bind(registry, name, &wl_scaler_interface, 2));
86     }
87 }
88
89 static void
90 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
91 {
92 }
93
94 TEST_F(ScalerCompositor, CreateViewport)
95 {
96     MockScalerClient *client = new MockScalerClient();
97
98     struct wl_viewport *vp = wl_scaler_get_viewport(client->scaler,
99             client->surface);
100
101     client->ExpectNoError();
102
103     wl_viewport_destroy(vp);
104     delete client;
105 }
106
107 TEST_F(ScalerCompositor, CreateViewportTwice)
108 {
109     MockScalerClient *client = new MockScalerClient();
110
111     struct wl_viewport *vp[2];
112     vp[0] = wl_scaler_get_viewport(client->scaler, client->surface);
113     vp[1] = wl_scaler_get_viewport(client->scaler, client->surface);
114
115     client->ExpectProtocolError(&wl_scaler_interface,
116             WL_SCALER_ERROR_VIEWPORT_EXISTS);
117
118     wl_viewport_destroy(vp[0]);
119     wl_viewport_destroy(vp[1]);
120     delete client;
121 }
122
123 TEST_F(ScalerCompositor, SetSource)
124 {
125     MockScalerClient *client = new MockScalerClient();
126
127     struct wl_viewport *vp = wl_scaler_get_viewport(client->scaler,
128             client->surface);
129
130     wl_viewport_set_source(vp,
131             wl_fixed_from_int(100), wl_fixed_from_int(100),
132             wl_fixed_from_int(100), wl_fixed_from_int(100));
133     wl_surface_commit(client->surface);
134
135     client->ExpectNoError();
136
137     wl_viewport_destroy(vp);
138     delete client;
139 }
140
141 TEST_F(ScalerCompositor, UnsetSource)
142 {
143     MockScalerClient *client = new MockScalerClient();
144
145     struct wl_viewport *vp = wl_scaler_get_viewport(client->scaler,
146             client->surface);
147
148     wl_viewport_set_source(vp,
149             wl_fixed_from_int(-1.0), wl_fixed_from_int(-1.0),
150             wl_fixed_from_int(-1.0), wl_fixed_from_int(-1.0));
151     wl_surface_commit(client->surface);
152
153     client->ExpectNoError();
154
155     wl_viewport_destroy(vp);
156     delete client;
157 }
158
159 TEST_F(ScalerCompositor, SetDestination)
160 {
161     MockScalerClient *client = new MockScalerClient();
162
163     struct wl_viewport *vp = wl_scaler_get_viewport(client->scaler,
164             client->surface);
165
166     wl_viewport_set_destination(vp, 100, 100);
167     wl_surface_commit(client->surface);
168
169     client->ExpectNoError();
170
171     wl_viewport_destroy(vp);
172     delete client;
173 }
174
175 TEST_F(ScalerCompositor, UnsetDestination)
176 {
177     MockScalerClient *client = new MockScalerClient();
178
179     struct wl_viewport *vp = wl_scaler_get_viewport(client->scaler,
180             client->surface);
181
182     wl_viewport_set_destination(vp, -1, -1);
183     wl_surface_commit(client->surface);
184
185     client->ExpectNoError();
186
187     wl_viewport_destroy(vp);
188     delete client;
189 }
190
191 struct Box {
192     int x, y, width, height;
193 };
194
195 class ScalerCompositorBadSourceTest :
196     public ScalerCompositor,
197     public testing::WithParamInterface<Box>
198 {
199 };
200
201 INSTANTIATE_TEST_CASE_P(ScalerBadSourceParamTest,
202         ScalerCompositorBadSourceTest,
203         testing::Values(
204             Box{0, 0, 0, 0},
205             Box{0, 0, 0, 10},
206             Box{0, 0, 20, 0},
207             Box{0, 0, -20, 10},
208             Box{0, 0, 20, -10},
209             Box{0, 0, -20, -10}));
210
211 TEST_P(ScalerCompositorBadSourceTest, SetBadSource)
212 {
213     MockScalerClient *client = new MockScalerClient();
214
215     struct wl_viewport *vp = wl_scaler_get_viewport(client->scaler,
216             client->surface);
217
218     Box src = GetParam();
219     wl_viewport_set_source(vp,
220             wl_fixed_from_int(src.x), wl_fixed_from_int(src.y),
221             wl_fixed_from_int(src.width), wl_fixed_from_int(src.height));
222     wl_surface_commit(client->surface);
223
224     client->ExpectProtocolError(&wl_viewport_interface,
225             WL_VIEWPORT_ERROR_BAD_VALUE);
226
227     wl_viewport_destroy(vp);
228     delete client;
229 }
230
231 struct Size {
232     int width, height;
233 };
234
235 class ScalerCompositorBadDestinationTest :
236     public ScalerCompositor,
237     public testing::WithParamInterface<Size>
238 {
239 };
240
241 INSTANTIATE_TEST_CASE_P(ScalerBadDestinationParamTest,
242         ScalerCompositorBadDestinationTest,
243         testing::Values(
244             Size{0, 0},
245             Size{0, 10},
246             Size{20, 0},
247             Size{-20, 10},
248             Size{20, -10},
249             Size{-20, -10}));
250
251 TEST_P(ScalerCompositorBadDestinationTest, SetBadDestination)
252 {
253     MockScalerClient *client = new MockScalerClient();
254
255     struct wl_viewport *vp = wl_scaler_get_viewport(client->scaler,
256             client->surface);
257
258     Size dst = GetParam();
259     wl_viewport_set_destination(vp, dst.width, dst.height);
260     wl_surface_commit(client->surface);
261
262     client->ExpectProtocolError(&wl_viewport_interface,
263             WL_VIEWPORT_ERROR_BAD_VALUE);
264
265     wl_viewport_destroy(vp);
266     delete client;
267 }