change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / tests / tc_global_resource.cpp
1 #include "tc_main.h"
2 #include "mockclient.h"
3 #include "mockcompositor.h"
4 #include <libds-tizen/global_resource.h>
5 #include <tizen-extension-client-protocol.h>
6
7 #define TIZEN_SURFACE_VERSION 1
8
9 class MockGlobalResourceCompositor : public MockCompositor
10 {
11 public:
12     MockGlobalResourceCompositor()
13         : MockCompositor(&MockGlobalResourceCompositor::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         bGetResourceInfo = false;
22         bDestroyResourceInfo = false;
23         mResourceId = 0;
24     }
25
26     ~MockGlobalResourceCompositor()
27     {
28         ds_inf("%s : this(%p)", __func__, this);
29     }
30
31     static void TestSetup(void *data)
32     {
33         MockGlobalResourceCompositor *mockComp =
34             static_cast<MockGlobalResourceCompositor *>(data);
35         Compositor *comp = mockComp->compositor;
36
37         ds_inf("%s: mockComp(%p)", __func__, mockComp);
38
39         // new surface listener
40         mockComp->mNewSurfaceListener.notify =
41             MockGlobalResourceCompositor::NewSurfaceCallback;
42         mockComp->mNewSurfaceListener.parent = mockComp;
43         ds_compositor_add_new_surface_listener(comp->compositor,
44                 &mockComp->mNewSurfaceListener);
45
46         mockComp->mGlobalResource =
47             ds_tizen_global_resource_create(comp->display);
48
49         // destroy listener
50         mockComp->mDestroyListener.notify =
51             MockGlobalResourceCompositor::DestroyCallback;
52         mockComp->mDestroyListener.parent = mockComp;
53         ds_tizen_global_resource_add_destroy_listener(mockComp->mGlobalResource,
54             &mockComp->mDestroyListener);
55
56         // get_resource_info listener
57         mockComp->mGetResourceInfoListener.notify =
58             MockGlobalResourceCompositor::GetResourceInfoCallback;
59         mockComp->mGetResourceInfoListener.parent = mockComp;
60         ds_tizen_global_resource_add_get_resource_info_listener(
61             mockComp->mGlobalResource,
62             &mockComp->mGetResourceInfoListener);
63     }
64
65     static void NewSurfaceCallback(struct wl_listener *listener, void *data)
66     {
67         MockGlobalResourceCompositor *mockComp =
68             reinterpret_cast<NewSurfaceListener *>(listener)->parent;
69         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
70
71         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
72
73         mockComp->mSurface = surface;
74
75         // del surface listener
76         mockComp->mDelSurfaceListener.notify =
77             MockGlobalResourceCompositor::DelSurfaceCallback;
78         mockComp->mDelSurfaceListener.parent = mockComp;
79         ds_surface_add_destroy_listener(surface,
80             &mockComp->mDelSurfaceListener);
81     }
82
83     static void DelSurfaceCallback(struct wl_listener *listener, void *data)
84     {
85         MockGlobalResourceCompositor *mockComp =
86             reinterpret_cast<DelSurfaceListener *>(listener)->parent;
87         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
88
89         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
90
91         if (ds_surface_get_wl_resource(mockComp->mSurface) ==
92             ds_surface_get_wl_resource(surface)) {
93             ds_inf("%s: surface is deleted.", __func__);
94             mockComp->bSurfaceDestroyed = true;
95         }
96     }
97
98     static void DestroyCallback(struct wl_listener *listener, void *data)
99     {
100         ds_inf("%s", __func__);
101
102         MockGlobalResourceCompositor *mockComp =
103             reinterpret_cast<DestroyListener *>(listener)->parent;
104
105         mockComp->bDestroyed = true;
106     }
107
108     static void GetResourceInfoCallback(struct wl_listener *listener,
109         void *data)
110     {
111         ds_inf("%s", __func__);
112
113         MockGlobalResourceCompositor *mockComp =
114             reinterpret_cast<GetResourceInfoListener *>(listener)->parent;
115         struct ds_tizen_global_resource_info *info =
116             static_cast<struct ds_tizen_global_resource_info *>(data);
117
118         ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
119
120         mockComp->bGetResourceInfo = true;
121
122         mockComp->mInfo = info;
123         mockComp->mSurface =
124             ds_tizen_global_resource_info_get_surface(mockComp->mInfo);
125
126         mockComp->mResourceId = ds_tizen_global_resource_get_universal_id(mockComp->mInfo);
127
128         // info destroy listener
129         mockComp->mGobalResourceInfoDestroyListener.notify =
130             MockGlobalResourceCompositor::GobalResourceInfoDestroyCallback;
131         mockComp->mGobalResourceInfoDestroyListener.parent = mockComp;
132         ds_tizen_global_resource_info_add_destroy_listener(mockComp->mInfo,
133             &mockComp->mGobalResourceInfoDestroyListener);
134     }
135
136     static void GobalResourceInfoDestroyCallback(struct wl_listener *listener,
137         void *data)
138     {
139         ds_inf("%s", __func__);
140
141         MockGlobalResourceCompositor *mockComp =
142             reinterpret_cast<GobalResourceInfoDestroyListener *>(listener)->parent;
143         struct ds_tizen_global_resource_info *info =
144             static_cast<struct ds_tizen_global_resource_info *>(data);
145
146         ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
147
148         if (mockComp->mInfo == info) {
149             ds_inf("%s: info is deleted.", __func__);
150             mockComp->bDestroyResourceInfo = true;
151         }
152     }
153
154 public:
155     bool bSurfaceDestroyed;
156     bool bDestroyed;
157     bool bGetResourceInfo;
158     bool bDestroyResourceInfo;
159     uint32_t mResourceId;
160
161 private:
162     struct ds_tizen_global_resource_info *mInfo;
163     struct ds_surface *mSurface;
164
165     struct NewSurfaceListener : ::wl_listener {
166         MockGlobalResourceCompositor *parent;
167     };
168     NewSurfaceListener mNewSurfaceListener;
169
170     struct DelSurfaceListener : ::wl_listener {
171         MockGlobalResourceCompositor *parent;
172     };
173     DelSurfaceListener mDelSurfaceListener;
174
175     struct ds_tizen_global_resource *mGlobalResource;
176
177     struct DestroyListener : ::wl_listener {
178         MockGlobalResourceCompositor *parent;
179     };
180     DestroyListener mDestroyListener;
181
182     struct GetResourceInfoListener : ::wl_listener {
183         MockGlobalResourceCompositor *parent;
184     };
185     GetResourceInfoListener mGetResourceInfoListener;
186
187     struct GobalResourceInfoDestroyListener : ::wl_listener {
188         MockGlobalResourceCompositor *parent;
189     };
190     GobalResourceInfoDestroyListener mGobalResourceInfoDestroyListener;
191 };
192
193 class MockGlobalResourceClient : public MockClient
194 {
195 public:
196     MockGlobalResourceClient()
197         : mResourceId(0),
198           compositor_res(nullptr),
199           tizen_surface_res(nullptr)
200     {}
201     MockGlobalResourceClient(const struct wl_registry_listener *listener)
202         : MockClient(listener, this)
203     {
204         ds_inf("%s", __func__);
205
206         mResourceId = 0;
207     }
208     ~MockGlobalResourceClient()
209     {
210         ds_inf("%s", __func__);
211     }
212
213     void SetWlCompositor(struct wl_compositor *global_res)
214     {
215         ds_inf("%s", __func__);
216
217         compositor_res = global_res;
218     }
219
220     struct wl_compositor *GetWlCompositor()
221     {
222         ds_inf("%s", __func__);
223
224         return compositor_res;
225     }
226
227     void SetTizenSurfaceShm(struct tizen_surface *global_res)
228     {
229         ds_inf("%s", __func__);
230
231         tizen_surface_res = global_res;
232     }
233
234     struct tizen_surface *GetTizenSurfaceShm()
235     {
236         ds_inf("%s", __func__);
237
238         return tizen_surface_res;
239     }
240
241 public:
242     uint32_t mResourceId;
243
244 private:
245     struct wl_compositor *compositor_res;
246     struct tizen_surface *tizen_surface_res;
247 };
248
249 static void
250 client_registry_cb_global(void *data, struct wl_registry *registry,
251     uint32_t name, const char *interface, uint32_t version)
252 {
253     ds_inf("%s", __func__);
254
255     MockGlobalResourceClient *client = static_cast<MockGlobalResourceClient *>(data);
256     struct wl_compositor *compositor_res;
257     struct tizen_surface *tizen_surface_res;
258
259     if (!strcmp(interface, "wl_compositor")) {
260         compositor_res = (struct wl_compositor *)wl_registry_bind(registry,
261             name, &wl_compositor_interface, 1);
262         if (compositor_res == nullptr) {
263             ds_err("wl_registry_bind() failed. wl_compositor resource.");
264             return;
265         }
266         client->SetWlCompositor(compositor_res);
267     } else if (!strcmp(interface, "tizen_surface")) {
268         tizen_surface_res = (struct tizen_surface *)wl_registry_bind(registry,
269             name, &tizen_surface_interface, TIZEN_SURFACE_VERSION);
270         if (tizen_surface_res == nullptr) {
271             ds_err("wl_registry_bind() failed. tizen_surface resource.");
272             return;
273         }
274         client->SetTizenSurfaceShm(tizen_surface_res);
275     }
276 }
277
278 static void
279 client_registry_cb_global_remove(void *data, struct wl_registry *registry,
280     uint32_t name)
281 {
282     ds_inf("%s", __func__);
283
284     MockGlobalResourceClient *client = static_cast<MockGlobalResourceClient *>(data);
285     struct wl_compositor *compositor_res = client->GetWlCompositor();
286     struct tizen_surface *tizen_surface_res = client->GetTizenSurfaceShm();
287
288     tizen_surface_destroy(tizen_surface_res);
289     wl_compositor_destroy(compositor_res);
290 }
291
292 static const struct wl_registry_listener registry_listener = {
293     .global = client_registry_cb_global,
294     .global_remove = client_registry_cb_global_remove
295 };
296
297 static void
298 client_tizen_resource_cb_resoruce_id(void *data,
299     struct tizen_resource *resource_res, uint32_t id)
300 {
301     ds_inf("%s", __func__);
302
303     MockGlobalResourceClient *client =
304         static_cast<MockGlobalResourceClient *>(data);
305
306     client->mResourceId = id;
307 }
308
309 static const struct
310 tizen_resource_listener resource_cb_listener = {
311     .resource_id = client_tizen_resource_cb_resoruce_id,
312 };
313
314 class GlobalResourceTest : public ::testing::Test
315 {
316 public:
317     void SetUp(void) override;
318     void TearDown(void) override;
319
320     MockGlobalResourceCompositor *comp;
321     MockGlobalResourceClient *client;
322     struct wl_compositor *compositor_res;
323     struct tizen_surface *tizen_surface_res;
324     struct wl_surface *surface_res;
325     struct tizen_resource *resource_res;
326 };
327
328 void
329 GlobalResourceTest::SetUp(void)
330 {
331     //ds_log_init(DS_DBG, NULL);
332
333     ds_inf("%s", __func__);
334
335     comp = new MockGlobalResourceCompositor();
336     client = new MockGlobalResourceClient(&registry_listener);
337     compositor_res = client->GetWlCompositor();
338     tizen_surface_res = client->GetTizenSurfaceShm();
339     surface_res = wl_compositor_create_surface(compositor_res);
340
341     resource_res =
342         tizen_surface_get_tizen_resource(tizen_surface_res, surface_res);
343
344     tizen_resource_add_listener(resource_res, &resource_cb_listener, client);
345
346     client->RoundTrip();
347 }
348
349 void
350 GlobalResourceTest::TearDown(void)
351 {
352     ds_inf("%s", __func__);
353
354     tizen_resource_destroy(resource_res);
355     client->RoundTrip();
356     EXPECT_TRUE(comp->bDestroyResourceInfo);
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(GlobalResourceTest, Create_P)
367 {
368     EXPECT_TRUE(true);
369     EXPECT_TRUE(comp->bGetResourceInfo);
370     EXPECT_TRUE(client->mResourceId == 0);
371 }