tinyds: fix the SVACE issue
[platform/core/uifw/libds-tizen.git] / tests / tc_screen_rotation.cpp
1 #include "tc_main.h"
2 #include "mockclient.h"
3 #include "mockcompositor.h"
4 #include <libds-tizen/screen_rotation.h>
5 #include <tizen-extension-client-protocol.h>
6
7 #define TIZEN_SCREEN_ROTATION_VERSION 1
8
9 class MockScreenRotationCompositor : public MockCompositor
10 {
11 public:
12     MockScreenRotationCompositor()
13         : MockCompositor(&MockScreenRotationCompositor::TestSetup, this)
14     {
15         ds_inf("%s : this(%p)", __func__, this);
16
17         // initialize the flags to check
18         bSurfaceDestroyed = false;
19
20         bDestroyed = false;
21         bGetIgnoreOutputTransform = false;
22         bDestroyScreenRotationInfo = false;
23     }
24
25     ~MockScreenRotationCompositor()
26     {
27         ds_inf("%s : this(%p)", __func__, this);
28     }
29
30     static void TestSetup(void *data)
31     {
32         MockScreenRotationCompositor *mockComp =
33             static_cast<MockScreenRotationCompositor *>(data);
34         Compositor *comp = mockComp->compositor;
35
36         ds_inf("%s: mockComp(%p)", __func__, mockComp);
37
38         // new surface listener
39         mockComp->mNewSurfaceListener.notify =
40             MockScreenRotationCompositor::NewSurfaceCallback;
41         mockComp->mNewSurfaceListener.parent = mockComp;
42         ds_compositor_add_new_surface_listener(comp->compositor,
43                 &mockComp->mNewSurfaceListener);
44
45         mockComp->mScreenRotation =
46             ds_tizen_screen_rotation_create(comp->display);
47
48         // destroy listener
49         mockComp->mDestroyListener.notify =
50             MockScreenRotationCompositor::DestroyCallback;
51         mockComp->mDestroyListener.parent = mockComp;
52         ds_tizen_screen_rotation_add_destroy_listener(mockComp->mScreenRotation,
53             &mockComp->mDestroyListener);
54
55         // add_ignore_output_transform listener
56         mockComp->mGetIgnoreOutputTransformListener.notify =
57             MockScreenRotationCompositor::GetIgnoreOutputTransformCallback;
58         mockComp->mGetIgnoreOutputTransformListener.parent = mockComp;
59         ds_tizen_screen_rotation_add_get_ignore_output_transform_info_listener(
60             mockComp->mScreenRotation,
61             &mockComp->mGetIgnoreOutputTransformListener);
62     }
63
64     static void NewSurfaceCallback(struct wl_listener *listener, void *data)
65     {
66         MockScreenRotationCompositor *mockComp =
67             reinterpret_cast<NewSurfaceListener *>(listener)->parent;
68         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
69
70         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
71
72         mockComp->mSurface = surface;
73
74         // del surface listener
75         mockComp->mDelSurfaceListener.notify =
76             MockScreenRotationCompositor::DelSurfaceCallback;
77         mockComp->mDelSurfaceListener.parent = mockComp;
78         ds_surface_add_destroy_listener(surface,
79             &mockComp->mDelSurfaceListener);
80     }
81
82     static void DelSurfaceCallback(struct wl_listener *listener, void *data)
83     {
84         MockScreenRotationCompositor *mockComp =
85             reinterpret_cast<DelSurfaceListener *>(listener)->parent;
86         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
87
88         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
89
90         if (ds_surface_get_wl_resource(mockComp->mSurface) ==
91             ds_surface_get_wl_resource(surface)) {
92             ds_inf("%s: surface is deleted.", __func__);
93             mockComp->bSurfaceDestroyed = true;
94         }
95     }
96
97     static void DestroyCallback(struct wl_listener *listener, void *data)
98     {
99         ds_inf("%s", __func__);
100
101         MockScreenRotationCompositor *mockComp =
102             reinterpret_cast<DestroyListener *>(listener)->parent;
103
104         mockComp->bDestroyed = true;
105     }
106
107     static void GetIgnoreOutputTransformCallback(struct wl_listener *listener,
108         void *data)
109     {
110         ds_inf("%s", __func__);
111
112         MockScreenRotationCompositor *mockComp =
113             reinterpret_cast<GetIgnoreOutputTransformListener *>(listener)->parent;
114         struct ds_tizen_screen_rotation_info *info =
115             static_cast<struct ds_tizen_screen_rotation_info *>(data);
116
117         ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
118
119         mockComp->bGetIgnoreOutputTransform = true;
120
121         mockComp->mInfo = info;
122         mockComp->mSurface =
123             ds_tizen_screen_rotation_info_get_surface(mockComp->mInfo);
124
125         // info destroy listener
126         mockComp->mScreenRotationInfoDestroyListener.notify =
127             MockScreenRotationCompositor::ScreenRotationInfoDestroyCallback;
128         mockComp->mScreenRotationInfoDestroyListener.parent = mockComp;
129         ds_tizen_screen_rotation_info_add_destroy_listener(mockComp->mInfo,
130             &mockComp->mScreenRotationInfoDestroyListener);
131     }
132
133     static void ScreenRotationInfoDestroyCallback(struct wl_listener *listener,
134         void *data)
135     {
136         ds_inf("%s", __func__);
137
138         MockScreenRotationCompositor *mockComp =
139             reinterpret_cast<ScreenRotationInfoDestroyListener *>(listener)->parent;
140         struct ds_tizen_screen_rotation_info *info =
141             static_cast<struct ds_tizen_screen_rotation_info *>(data);
142
143         ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
144
145         if (mockComp->mInfo == info) {
146             ds_inf("%s: info is deleted.", __func__);
147             mockComp->bDestroyScreenRotationInfo = true;
148         }
149     }
150
151     void SendIgnoreOutputTransform(uint32_t ignore)
152     {
153         ds_inf("%s", __func__);
154
155         ds_tizen_screen_rotation_send_ignore_output_transform(mInfo, ignore);
156     }
157
158 public:
159     bool bSurfaceDestroyed;
160     bool bDestroyed;
161     bool bGetIgnoreOutputTransform;
162     bool bDestroyScreenRotationInfo;
163
164 private:
165     struct ds_tizen_screen_rotation_info *mInfo;
166     struct ds_surface *mSurface;
167
168     struct NewSurfaceListener : ::wl_listener {
169         MockScreenRotationCompositor *parent;
170     };
171     NewSurfaceListener mNewSurfaceListener;
172
173     struct DelSurfaceListener : ::wl_listener {
174         MockScreenRotationCompositor *parent;
175     };
176     DelSurfaceListener mDelSurfaceListener;
177
178     struct ds_tizen_screen_rotation *mScreenRotation;
179
180     struct DestroyListener : ::wl_listener {
181         MockScreenRotationCompositor *parent;
182     };
183     DestroyListener mDestroyListener;
184
185     struct GetIgnoreOutputTransformListener : ::wl_listener {
186         MockScreenRotationCompositor *parent;
187     };
188     GetIgnoreOutputTransformListener mGetIgnoreOutputTransformListener;
189
190     struct ScreenRotationInfoDestroyListener : ::wl_listener {
191         MockScreenRotationCompositor *parent;
192     };
193     ScreenRotationInfoDestroyListener mScreenRotationInfoDestroyListener;
194 };
195
196 class MockScreenRotationClient : public MockClient
197 {
198 public:
199     MockScreenRotationClient()
200         : bIgnoreOutputTransformEvent(false),
201           mIgnore(0),
202           compositor_res(nullptr),
203           screen_rotation_res(nullptr)
204     {}
205     MockScreenRotationClient(const struct wl_registry_listener *listener)
206         : MockClient(listener, this)
207     {
208         ds_inf("%s", __func__);
209
210         bIgnoreOutputTransformEvent = false;
211         mIgnore = 0;
212     }
213     ~MockScreenRotationClient()
214     {
215         ds_inf("%s", __func__);
216     }
217
218     void SetWlCompositor(struct wl_compositor *global_res)
219     {
220         ds_inf("%s", __func__);
221
222         compositor_res = global_res;
223     }
224
225     struct wl_compositor *GetWlCompositor()
226     {
227         ds_inf("%s", __func__);
228
229         return compositor_res;
230     }
231
232     void SetTizenScreenRotation(struct tizen_screen_rotation *resource)
233     {
234         ds_inf("%s", __func__);
235
236         screen_rotation_res = resource;
237     }
238
239     struct tizen_screen_rotation *GetTizenScreenRotation()
240     {
241         ds_inf("%s", __func__);
242
243         return screen_rotation_res;
244     }
245
246 public:
247     bool bIgnoreOutputTransformEvent;
248     uint32_t mIgnore;
249
250 private:
251     struct wl_compositor *compositor_res;
252     struct tizen_screen_rotation *screen_rotation_res;
253 };
254
255 static void
256 client_tizen_screen_rotation_cb_ignore_output_transform(void *data,
257     struct tizen_screen_rotation *screen_rotation_res,
258     struct wl_surface *surface_resource, uint32_t ignore)
259 {
260     ds_inf("%s", __func__);
261
262     MockScreenRotationClient *client = static_cast<MockScreenRotationClient *>(data);
263
264     client->bIgnoreOutputTransformEvent = true;
265     client->mIgnore = ignore;
266 }
267
268 static const struct
269 tizen_screen_rotation_listener screen_rotation_cb_listener = {
270     .ignore_output_transform = client_tizen_screen_rotation_cb_ignore_output_transform,
271 };
272
273 static void
274 client_registry_cb_global(void *data, struct wl_registry *registry,
275     uint32_t name, const char *interface, uint32_t version)
276 {
277     ds_inf("%s", __func__);
278
279     MockScreenRotationClient *client = static_cast<MockScreenRotationClient *>(data);
280     struct wl_compositor *compositor_res;
281     struct tizen_screen_rotation *screen_rotation_res;
282
283     if (!strcmp(interface, "wl_compositor")) {
284         compositor_res = (struct wl_compositor *)wl_registry_bind(registry,
285             name, &wl_compositor_interface, 1);
286         if (compositor_res == nullptr) {
287             ds_err("wl_registry_bind() failed. wl_compositor resource.");
288             return;
289         }
290         client->SetWlCompositor(compositor_res);
291     } else if (!strcmp(interface, "tizen_screen_rotation")) {
292         screen_rotation_res = (struct tizen_screen_rotation *)wl_registry_bind(registry,
293             name, &tizen_screen_rotation_interface, TIZEN_SCREEN_ROTATION_VERSION);
294         if (screen_rotation_res == nullptr) {
295             ds_err("wl_registry_bind() failed. tizen_screen_rotation resource.");
296             return;
297         }
298         client->SetTizenScreenRotation(screen_rotation_res);
299
300         tizen_screen_rotation_add_listener(screen_rotation_res,
301             &screen_rotation_cb_listener, client);
302     }
303 }
304
305 static void
306 client_registry_cb_global_remove(void *data, struct wl_registry *registry,
307     uint32_t name)
308 {
309     ds_inf("%s", __func__);
310
311     MockScreenRotationClient *client = static_cast<MockScreenRotationClient *>(data);
312     struct wl_compositor *compositor_res = client->GetWlCompositor();
313     struct tizen_screen_rotation *screen_rotation_res = client->GetTizenScreenRotation();
314
315     tizen_screen_rotation_destroy(screen_rotation_res);
316     wl_compositor_destroy(compositor_res);
317 }
318
319 static const struct wl_registry_listener registry_listener = {
320     .global = client_registry_cb_global,
321     .global_remove = client_registry_cb_global_remove
322 };
323
324 class ScreenRotationTest : public ::testing::Test
325 {
326 public:
327     void SetUp(void) override;
328     void TearDown(void) override;
329
330     MockScreenRotationCompositor *comp;
331     MockScreenRotationClient *client;
332     struct wl_compositor *compositor_res;
333     struct tizen_screen_rotation *screen_rotation_res;
334     struct wl_surface *surface_res;
335 };
336
337 void
338 ScreenRotationTest::SetUp(void)
339 {
340     //ds_log_init(DS_DBG, NULL);
341
342     ds_inf("%s", __func__);
343
344     comp = new MockScreenRotationCompositor();
345     client = new MockScreenRotationClient(&registry_listener);
346     compositor_res = client->GetWlCompositor();
347     screen_rotation_res = client->GetTizenScreenRotation();
348     surface_res = wl_compositor_create_surface(compositor_res);
349
350     client->RoundTrip();
351 }
352
353 void
354 ScreenRotationTest::TearDown(void)
355 {
356     ds_inf("%s", __func__);
357
358     wl_surface_destroy(surface_res);
359     client->RoundTrip();
360     EXPECT_TRUE(comp->bSurfaceDestroyed);
361
362     delete client;
363     delete comp;
364 }
365
366 TEST_F(ScreenRotationTest, Create_P)
367 {
368     EXPECT_TRUE(true);
369 }
370
371 TEST_F(ScreenRotationTest, Req_TizenScreenRotationGetIgnoreOutputTransform)
372 {
373     tizen_screen_rotation_get_ignore_output_transform(screen_rotation_res, surface_res);
374     client->RoundTrip();
375     EXPECT_TRUE(comp->bGetIgnoreOutputTransform);
376 }
377
378 TEST_F(ScreenRotationTest, Ev_TizenScreenRotationSurfaceRedraw)
379 {
380     tizen_screen_rotation_get_ignore_output_transform(screen_rotation_res, surface_res);
381     client->RoundTrip();
382     EXPECT_TRUE(comp->bGetIgnoreOutputTransform);
383
384     comp->SendIgnoreOutputTransform(1);
385     comp->Process();
386
387     client->RoundTrip();
388     EXPECT_TRUE(client->bIgnoreOutputTransformEvent);
389     EXPECT_TRUE(client->mIgnore == 1);
390 }