change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / tests / tc_memory_flusher.cpp
1 #include "tc_main.h"
2 #include "mockclient.h"
3 #include "mockcompositor.h"
4 #include <libds-tizen/memory_flusher.h>
5 #include <tizen-surface-client-protocol.h>
6
7 #define TIZEN_MEMORY_FLUSHER_VERSION 2
8
9 class MockMemoryFlusherCompositor : public MockCompositor
10 {
11 public:
12     MockMemoryFlusherCompositor()
13         : MockCompositor(&MockMemoryFlusherCompositor::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         bGetFlusherInfo = false;
22         bDestroyFlusherInfo = false;
23     }
24
25     ~MockMemoryFlusherCompositor()
26     {
27         ds_inf("%s : this(%p)", __func__, this);
28     }
29
30     static void TestSetup(void *data)
31     {
32         MockMemoryFlusherCompositor *mockComp =
33             static_cast<MockMemoryFlusherCompositor *>(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             MockMemoryFlusherCompositor::NewSurfaceCallback;
41         mockComp->mNewSurfaceListener.parent = mockComp;
42         ds_compositor_add_new_surface_listener(comp->compositor,
43                 &mockComp->mNewSurfaceListener);
44
45         mockComp->mMemoryFlusher =
46             ds_tizen_memory_flusher_create(comp->display);
47
48         // destroy listener
49         mockComp->mDestroyListener.notify =
50             MockMemoryFlusherCompositor::DestroyCallback;
51         mockComp->mDestroyListener.parent = mockComp;
52         ds_tizen_memory_flusher_add_destroy_listener(mockComp->mMemoryFlusher,
53             &mockComp->mDestroyListener);
54
55         // get_flusher_info listener
56         mockComp->mGetFlusherInfoListener.notify =
57             MockMemoryFlusherCompositor::GetFlusherInfoCallback;
58         mockComp->mGetFlusherInfoListener.parent = mockComp;
59         ds_tizen_memory_flusher_add_get_flusher_info_listener(
60             mockComp->mMemoryFlusher,
61             &mockComp->mGetFlusherInfoListener);
62     }
63
64     static void NewSurfaceCallback(struct wl_listener *listener, void *data)
65     {
66         MockMemoryFlusherCompositor *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             MockMemoryFlusherCompositor::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         MockMemoryFlusherCompositor *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         MockMemoryFlusherCompositor *mockComp =
102             reinterpret_cast<DestroyListener *>(listener)->parent;
103
104         mockComp->bDestroyed = true;
105     }
106
107     static void GetFlusherInfoCallback(struct wl_listener *listener,
108         void *data)
109     {
110         ds_inf("%s", __func__);
111
112         MockMemoryFlusherCompositor *mockComp =
113             reinterpret_cast<GetFlusherInfoListener *>(listener)->parent;
114         struct ds_tizen_memory_flusher_info *info =
115             static_cast<struct ds_tizen_memory_flusher_info *>(data);
116
117         ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
118
119         mockComp->bGetFlusherInfo = true;
120
121         mockComp->mInfo = info;
122         mockComp->mSurface =
123             ds_tizen_memory_flusher_info_get_surface(mockComp->mInfo);
124
125         // info destroy listener
126         mockComp->mFlusherInfoDestroyListener.notify =
127             MockMemoryFlusherCompositor::FlusherInfoDestroyCallback;
128         mockComp->mFlusherInfoDestroyListener.parent = mockComp;
129         ds_tizen_memory_flusher_info_add_destroy_listener(mockComp->mInfo,
130             &mockComp->mFlusherInfoDestroyListener);
131     }
132
133     static void FlusherInfoDestroyCallback(struct wl_listener *listener,
134         void *data)
135     {
136         ds_inf("%s", __func__);
137
138         MockMemoryFlusherCompositor *mockComp =
139             reinterpret_cast<FlusherInfoDestroyListener *>(listener)->parent;
140         struct ds_tizen_memory_flusher_info *info =
141             static_cast<struct ds_tizen_memory_flusher_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->bDestroyFlusherInfo = true;
148         }
149     }
150
151     void SendMemoryFlusherInfoFlush()
152     {
153         ds_inf("%s", __func__);
154
155         ds_tizen_memory_flusher_info_send_flush(mInfo);
156     }
157
158     void SendMemoryFlusherInfoFreeFlush()
159     {
160         ds_inf("%s", __func__);
161
162         ds_tizen_memory_flusher_info_send_free_flush(mInfo);
163     }
164
165 public:
166     bool bSurfaceDestroyed;
167     bool bDestroyed;
168     bool bGetFlusherInfo;
169     bool bDestroyFlusherInfo;
170
171 private:
172     struct ds_tizen_memory_flusher_info *mInfo;
173     struct ds_surface *mSurface;
174
175     struct NewSurfaceListener : ::wl_listener {
176         MockMemoryFlusherCompositor *parent;
177     };
178     NewSurfaceListener mNewSurfaceListener;
179
180     struct DelSurfaceListener : ::wl_listener {
181         MockMemoryFlusherCompositor *parent;
182     };
183     DelSurfaceListener mDelSurfaceListener;
184
185     struct ds_tizen_memory_flusher *mMemoryFlusher;
186
187     struct DestroyListener : ::wl_listener {
188         MockMemoryFlusherCompositor *parent;
189     };
190     DestroyListener mDestroyListener;
191
192     struct GetFlusherInfoListener : ::wl_listener {
193         MockMemoryFlusherCompositor *parent;
194     };
195     GetFlusherInfoListener mGetFlusherInfoListener;
196
197     struct FlusherInfoDestroyListener : ::wl_listener {
198         MockMemoryFlusherCompositor *parent;
199     };
200     FlusherInfoDestroyListener mFlusherInfoDestroyListener;
201 };
202
203 class MockMemoryFlusherClient : public MockClient
204 {
205 public:
206     MockMemoryFlusherClient()
207         : bFlushEvent(false),
208           bFreeFlushEvent(false),
209           compositor_res(nullptr),
210           tizen_surface_shm(nullptr)
211     {}
212     MockMemoryFlusherClient(const struct wl_registry_listener *listener)
213         : MockClient(listener, this)
214     {
215         ds_inf("%s", __func__);
216
217         bFlushEvent = false;
218         bFreeFlushEvent = false;
219     }
220     ~MockMemoryFlusherClient()
221     {
222         ds_inf("%s", __func__);
223     }
224
225     void SetWlCompositor(struct wl_compositor *global_res)
226     {
227         ds_inf("%s", __func__);
228
229         compositor_res = global_res;
230     }
231
232     struct wl_compositor *GetWlCompositor()
233     {
234         ds_inf("%s", __func__);
235
236         return compositor_res;
237     }
238
239     void SetTizenSurfaceShm(struct tizen_surface_shm *global_res)
240     {
241         ds_inf("%s", __func__);
242
243         tizen_surface_shm = global_res;
244     }
245
246     struct tizen_surface_shm *GetTizenSurfaceShm()
247     {
248         ds_inf("%s", __func__);
249
250         return tizen_surface_shm;
251     }
252
253 public:
254     bool bFlushEvent;
255     bool bFreeFlushEvent;
256
257 private:
258     struct wl_compositor *compositor_res;
259     struct tizen_surface_shm *tizen_surface_shm;
260 };
261
262 static void
263 client_registry_cb_global(void *data, struct wl_registry *registry,
264     uint32_t name, const char *interface, uint32_t version)
265 {
266     ds_inf("%s", __func__);
267
268     MockMemoryFlusherClient *client = static_cast<MockMemoryFlusherClient *>(data);
269     struct wl_compositor *compositor_res;
270     struct tizen_surface_shm *surface_shm_res;
271
272     if (!strcmp(interface, "wl_compositor")) {
273         compositor_res = (struct wl_compositor *)wl_registry_bind(registry,
274             name, &wl_compositor_interface, 1);
275         if (compositor_res == nullptr) {
276             ds_err("wl_registry_bind() failed. wl_compositor resource.");
277             return;
278         }
279         client->SetWlCompositor(compositor_res);
280     } else if (!strcmp(interface, "tizen_surface_shm")) {
281         surface_shm_res = (struct tizen_surface_shm *)wl_registry_bind(registry,
282             name, &tizen_surface_shm_interface, TIZEN_MEMORY_FLUSHER_VERSION);
283         if (surface_shm_res == nullptr) {
284             ds_err("wl_registry_bind() failed. tizen_surface_shm resource.");
285             return;
286         }
287         client->SetTizenSurfaceShm(surface_shm_res);
288     }
289 }
290
291 static void
292 client_registry_cb_global_remove(void *data, struct wl_registry *registry,
293     uint32_t name)
294 {
295     ds_inf("%s", __func__);
296
297     MockMemoryFlusherClient *client = static_cast<MockMemoryFlusherClient *>(data);
298     struct wl_compositor *compositor_res = client->GetWlCompositor();
299     struct tizen_surface_shm *surface_shm_res = client->GetTizenSurfaceShm();
300
301     tizen_surface_shm_destroy(surface_shm_res);
302     wl_compositor_destroy(compositor_res);
303 }
304
305 static const struct wl_registry_listener registry_listener = {
306     .global = client_registry_cb_global,
307     .global_remove = client_registry_cb_global_remove
308 };
309
310 static void
311 client_tizen_surface_shm_flusher_cb_flush(void *data,
312     struct tizen_surface_shm_flusher *surface_shm_flusher_res)
313 {
314     ds_inf("%s", __func__);
315
316     MockMemoryFlusherClient *client = static_cast<MockMemoryFlusherClient *>(data);
317
318     client->bFlushEvent = true;
319 }
320
321 static void
322 client_tizen_surface_shm_flusher_cb_free_flush(void *data,
323     struct tizen_surface_shm_flusher *surface_shm_flusher_res)
324 {
325     ds_inf("%s", __func__);
326
327     MockMemoryFlusherClient *client = static_cast<MockMemoryFlusherClient *>(data);
328
329     client->bFreeFlushEvent = true;
330 }
331
332 static const struct
333 tizen_surface_shm_flusher_listener surface_shm_flusher_cb_listener = {
334     .flush = client_tizen_surface_shm_flusher_cb_flush,
335     .free_flush = client_tizen_surface_shm_flusher_cb_free_flush,
336 };
337
338 class MemoryFlusherTest : public ::testing::Test
339 {
340 public:
341     void SetUp(void) override;
342     void TearDown(void) override;
343
344     MockMemoryFlusherCompositor *comp;
345     MockMemoryFlusherClient *client;
346     struct wl_compositor *compositor_res;
347     struct tizen_surface_shm *surface_shm_res;
348     struct wl_surface *surface_res;
349     struct tizen_surface_shm_flusher *surface_shm_flusher_res;
350 };
351
352 void
353 MemoryFlusherTest::SetUp(void)
354 {
355     //ds_log_init(DS_DBG, NULL);
356
357     ds_inf("%s", __func__);
358
359     comp = new MockMemoryFlusherCompositor();
360     client = new MockMemoryFlusherClient(&registry_listener);
361     compositor_res = client->GetWlCompositor();
362     surface_shm_res = client->GetTizenSurfaceShm();
363     surface_res = wl_compositor_create_surface(compositor_res);
364
365     surface_shm_flusher_res =
366         tizen_surface_shm_get_flusher(surface_shm_res, surface_res);
367
368     tizen_surface_shm_flusher_add_listener(surface_shm_flusher_res,
369         &surface_shm_flusher_cb_listener, client);
370
371     client->RoundTrip();
372 }
373
374 void
375 MemoryFlusherTest::TearDown(void)
376 {
377     ds_inf("%s", __func__);
378
379     tizen_surface_shm_flusher_destroy(surface_shm_flusher_res);
380     client->RoundTrip();
381     EXPECT_TRUE(comp->bDestroyFlusherInfo);
382
383     wl_surface_destroy(surface_res);
384     client->RoundTrip();
385     EXPECT_TRUE(comp->bSurfaceDestroyed);
386
387     delete client;
388     delete comp;
389 }
390
391 TEST_F(MemoryFlusherTest, Create_P)
392 {
393     EXPECT_TRUE(true);
394     EXPECT_TRUE(comp->bGetFlusherInfo);
395 }
396
397 TEST_F(MemoryFlusherTest, Ev_TizenSurfaceShmFlusherFlush)
398 {
399     comp->SendMemoryFlusherInfoFlush();
400     comp->Process();
401
402     client->RoundTrip();
403     EXPECT_TRUE(client->bFlushEvent);
404 }
405
406 TEST_F(MemoryFlusherTest, Ev_TizenSurfaceShmFlusherFreeFlush)
407 {
408     comp->SendMemoryFlusherInfoFreeFlush();
409     comp->Process();
410
411     client->RoundTrip();
412     EXPECT_TRUE(client->bFreeFlushEvent);
413 }