change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / tests / tc_indicator.cpp
1 #include "tc_main.h"
2 #include "mockclient.h"
3 #include "mockcompositor.h"
4 #include <libds-tizen/indicator.h>
5 #include <tizen-extension-client-protocol.h>
6
7 #define TIZEN_INDICATOR_VERSION 1
8 class MockIndicatorCompositor : public MockCompositor
9 {
10 public:
11     MockIndicatorCompositor()
12         : MockCompositor(&MockIndicatorCompositor::TestSetup, this)
13     {
14         ds_inf("%s : this(%p)", __func__, this);
15
16         // initialize the flags to check
17         bSurfaceDestroyed = false;
18
19         bDestroyed = false;
20
21         bStateOn = false;
22         bStateOff = false;
23
24         bOpacityModeOpque = false;
25         bOpacityModeTranslucent = false;
26         bOpacityModeTransparent = false;
27         bOpacityModeBgTransparent = false;
28
29         bVisibleTypeHidden = false;
30         bVisibleTypeShown = false;
31     }
32
33     ~MockIndicatorCompositor()
34     {
35         ds_inf("%s : this(%p)", __func__, this);
36     }
37
38     static void TestSetup(void *data)
39     {
40         MockIndicatorCompositor *mockComp =
41             static_cast<MockIndicatorCompositor *>(data);
42         Compositor *comp = mockComp->compositor;
43
44         ds_inf("%s: mockComp(%p)", __func__, mockComp);
45
46         // new surface listener
47         mockComp->mNewSurfaceListener.notify =
48             MockIndicatorCompositor::NewSurfaceCallback;
49         mockComp->mNewSurfaceListener.parent = mockComp;
50         ds_compositor_add_new_surface_listener(comp->compositor,
51                 &mockComp->mNewSurfaceListener);
52
53         mockComp->mIndicator = ds_tizen_indicator_create(comp->display);
54
55         // destroy listener
56         mockComp->mDestroyListener.notify =
57             MockIndicatorCompositor::DestroyCallback;
58         mockComp->mDestroyListener.parent = mockComp;
59         ds_tizen_indicator_add_destroy_listener(mockComp->mIndicator,
60             &mockComp->mDestroyListener);
61
62         // change_state listener
63         mockComp->mChangeStateListener.notify =
64             MockIndicatorCompositor::ChangeStateCallback;
65         mockComp->mChangeStateListener.parent = mockComp;
66         ds_tizen_indicator_add_change_state_listener(mockComp->mIndicator,
67             &mockComp->mChangeStateListener);
68
69         // change_opacity_mode listener
70         mockComp->mChangeOpacityModeListener.notify =
71             MockIndicatorCompositor::ChangeOpacityModeCallback;
72         mockComp->mChangeOpacityModeListener.parent = mockComp;
73         ds_tizen_indicator_add_change_opacity_mode_listener(
74             mockComp->mIndicator,
75             &mockComp->mChangeOpacityModeListener);
76
77         // change_visible_type listener
78         mockComp->mChangeVisibleTypeListener.notify =
79             MockIndicatorCompositor::ChangeVisibleTypeCallback;
80         mockComp->mChangeVisibleTypeListener.parent = mockComp;
81         ds_tizen_indicator_add_change_visible_type_listener(
82             mockComp->mIndicator,
83             &mockComp->mChangeVisibleTypeListener);
84     }
85
86     static void NewSurfaceCallback(struct wl_listener *listener, void *data)
87     {
88         MockIndicatorCompositor *mockComp =
89             reinterpret_cast<NewSurfaceListener *>(listener)->parent;
90         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
91
92         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
93
94         mockComp->mSurface = surface;
95
96         // del surface listener
97         mockComp->mDelSurfaceListener.notify =
98             MockIndicatorCompositor::DelSurfaceCallback;
99         mockComp->mDelSurfaceListener.parent = mockComp;
100         ds_surface_add_destroy_listener(surface,
101             &mockComp->mDelSurfaceListener);
102     }
103
104     static void DelSurfaceCallback(struct wl_listener *listener, void *data)
105     {
106         MockIndicatorCompositor *mockComp =
107             reinterpret_cast<DelSurfaceListener *>(listener)->parent;
108         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
109
110         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
111
112         if (ds_surface_get_wl_resource(mockComp->mSurface) ==
113             ds_surface_get_wl_resource(surface)) {
114             ds_inf("%s: surface is deleted.", __func__);
115             mockComp->bSurfaceDestroyed = true;
116         }
117     }
118
119     static void DestroyCallback(struct wl_listener *listener, void *data)
120     {
121         ds_inf("%s", __func__);
122
123         MockIndicatorCompositor *mockComp =
124             reinterpret_cast<DestroyListener *>(listener)->parent;
125
126         mockComp->bDestroyed = true;
127     }
128
129     static void ChangeStateCallback(struct wl_listener *listener, void *data)
130     {
131         ds_inf("%s", __func__);
132
133         MockIndicatorCompositor *mockComp =
134             reinterpret_cast<ChangeStateListener *>(listener)->parent;
135         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
136         enum ds_tizen_indicator_state state;
137
138         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
139
140         state = ds_tizen_indicator_get_state(mockComp->mIndicator,
141             mockComp->mSurface);
142
143         if (state == DS_TIZEN_INDICATOR_STATE_ON)
144             mockComp->bStateOn = true;
145         if (state == DS_TIZEN_INDICATOR_STATE_OFF)
146             mockComp->bStateOff = true;
147     }
148
149     static void ChangeOpacityModeCallback(struct wl_listener *listener,
150         void *data)
151     {
152         ds_inf("%s", __func__);
153
154         MockIndicatorCompositor *mockComp =
155             reinterpret_cast<ChangeVisibleTypeListener *>(listener)->parent;
156         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
157         enum ds_tizen_indicator_opacity_mode opacity_mode;
158
159         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
160
161         opacity_mode = ds_tizen_indicator_get_opacity_mode(mockComp->mIndicator,
162             mockComp->mSurface);
163
164         if (opacity_mode == DS_TIZEN_INDICATOR_OPACITY_MODE_OPAQUE)
165             mockComp->bOpacityModeOpque = true;
166         if (opacity_mode == DS_TIZEN_INDICATOR_OPACITY_MODE_TRANSLUCENT)
167             mockComp->bOpacityModeTranslucent = true;
168         if (opacity_mode == DS_TIZEN_INDICATOR_OPACITY_MODE_TRANSPARENT)
169             mockComp->bOpacityModeTransparent = true;
170         if (opacity_mode == DS_TIZEN_INDICATOR_OPACITY_MODE_BG_TRANSPARENT)
171             mockComp->bOpacityModeBgTransparent = true;
172     }
173
174     static void ChangeVisibleTypeCallback(struct wl_listener *listener,
175         void *data)
176     {
177         ds_inf("%s", __func__);
178
179         MockIndicatorCompositor *mockComp =
180             reinterpret_cast<ChangeOpacityModeListener *>(listener)->parent;
181         struct ds_surface *surface = static_cast<struct ds_surface *>(data);
182         enum ds_tizen_indicator_visible_type visible_type;
183
184         ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
185
186         visible_type = ds_tizen_indicator_get_visible_type(mockComp->mIndicator,
187             mockComp->mSurface);
188
189         if (visible_type == DS_TIZEN_INDICATOR_VISIBLE_TYPE_HIDDEN)
190             mockComp->bVisibleTypeHidden = true;
191         if (visible_type == DS_TIZEN_INDICATOR_VISIBLE_TYPE_SHOWN)
192             mockComp->bVisibleTypeShown = true;
193     }
194
195     void SendFlick()
196     {
197         ds_inf("%s", __func__);
198
199         ds_tizen_indicator_send_flick(mIndicator, mSurface);
200     }
201
202 public:
203     bool bSurfaceDestroyed;
204     bool bDestroyed;
205     bool bStateOn;
206     bool bStateOff;
207     bool bOpacityModeOpque;
208     bool bOpacityModeTranslucent;
209     bool bOpacityModeTransparent;
210     bool bOpacityModeBgTransparent;
211     bool bVisibleTypeHidden;
212     bool bVisibleTypeShown;
213
214 private:
215     struct ds_surface *mSurface;
216     struct NewSurfaceListener : ::wl_listener {
217         MockIndicatorCompositor *parent;
218     };
219     NewSurfaceListener mNewSurfaceListener;
220     struct DelSurfaceListener : ::wl_listener {
221         MockIndicatorCompositor *parent;
222     };
223     DelSurfaceListener mDelSurfaceListener;
224
225     struct ds_tizen_indicator *mIndicator;
226     struct DestroyListener : ::wl_listener {
227         MockIndicatorCompositor *parent;
228     };
229     DestroyListener mDestroyListener;
230     struct ChangeStateListener : ::wl_listener {
231         MockIndicatorCompositor *parent;
232     };
233     ChangeStateListener mChangeStateListener;
234     struct ChangeOpacityModeListener : ::wl_listener {
235         MockIndicatorCompositor *parent;
236     };
237     ChangeOpacityModeListener mChangeOpacityModeListener;
238     struct ChangeVisibleTypeListener : ::wl_listener {
239         MockIndicatorCompositor *parent;
240     };
241     ChangeVisibleTypeListener mChangeVisibleTypeListener;
242 };
243
244 class MockIndicatorClient : public MockClient
245 {
246 public:
247     MockIndicatorClient()
248         : bFlickEvent(false),
249           compositor_res(nullptr),
250           tizen_indicator(nullptr)
251     {}
252     MockIndicatorClient(const struct wl_registry_listener *listener)
253         : MockClient(listener, this)
254     {
255         ds_inf("%s", __func__);
256
257         bFlickEvent = false;
258     }
259     ~MockIndicatorClient()
260     {
261         ds_inf("%s", __func__);
262     }
263
264     void SetWlCompositor(struct wl_compositor *global_res)
265     {
266         ds_inf("%s", __func__);
267
268         compositor_res = global_res;
269     }
270
271     struct wl_compositor *GetWlCompositor()
272     {
273         ds_inf("%s", __func__);
274
275         return compositor_res;
276     }
277
278     void SetTizenIndicator(struct tizen_indicator *global_res)
279     {
280         ds_inf("%s", __func__);
281
282         tizen_indicator = global_res;
283     }
284
285     struct tizen_indicator *GetTizenIndicator()
286     {
287         ds_inf("%s", __func__);
288
289         return tizen_indicator;
290     }
291
292 public:
293     bool bFlickEvent;
294
295 private:
296     struct wl_compositor *compositor_res;
297     struct tizen_indicator *tizen_indicator;
298 };
299
300 static void
301 client_tizen_indicator_cb_flick(void *data, struct tizen_indicator *indicator,
302     struct wl_surface *surface, int32_t type)
303 {
304     ds_inf("%s", __func__);
305
306     MockIndicatorClient *client = static_cast<MockIndicatorClient *>(data);
307
308     client->bFlickEvent = true;
309 }
310
311 static const struct tizen_indicator_listener indicator_cb_listener = {
312     .flick = client_tizen_indicator_cb_flick
313 };
314
315 static void
316 client_registry_cb_global(void *data, struct wl_registry *registry,
317     uint32_t name, const char *interface, uint32_t version)
318 {
319     ds_inf("%s", __func__);
320
321     MockIndicatorClient *client = static_cast<MockIndicatorClient *>(data);
322     struct wl_compositor *compositor_res;
323     struct tizen_indicator *tizen_indicator;
324
325     if (!strcmp(interface, "wl_compositor")) {
326         compositor_res = (struct wl_compositor *)wl_registry_bind(registry,
327             name, &wl_compositor_interface, 1);
328         if (compositor_res == nullptr) {
329             ds_err("wl_registry_bind() failed. wl_compositor resource.");
330             return;
331         }
332         client->SetWlCompositor(compositor_res);
333     } else if (!strcmp(interface, "tizen_indicator")) {
334         tizen_indicator = (struct tizen_indicator *)wl_registry_bind(registry,
335             name, &tizen_indicator_interface, TIZEN_INDICATOR_VERSION);
336         if (tizen_indicator == nullptr) {
337             ds_err("wl_registry_bind() failed. tizen_indicator resource.");
338             return;
339         }
340         client->SetTizenIndicator(tizen_indicator);
341
342         tizen_indicator_add_listener(tizen_indicator, &indicator_cb_listener,
343             client);
344     }
345 }
346
347 static void
348 client_registry_cb_global_remove(void *data, struct wl_registry *registry,
349     uint32_t name)
350 {
351     ds_inf("%s", __func__);
352
353     MockIndicatorClient *client = static_cast<MockIndicatorClient *>(data);
354     struct wl_compositor *compositor_res = client->GetWlCompositor();
355     struct tizen_indicator *indicator_res = client->GetTizenIndicator();
356
357     tizen_indicator_destroy(indicator_res);
358     wl_compositor_destroy(compositor_res);
359 }
360
361 static const struct wl_registry_listener registry_listener = {
362     .global = client_registry_cb_global,
363     .global_remove = client_registry_cb_global_remove
364 };
365
366 class IndicatorTest : public ::testing::Test
367 {
368 public:
369     void SetUp(void) override;
370     void TearDown(void) override;
371
372     MockIndicatorCompositor *comp;
373     MockIndicatorClient *client;
374     struct wl_compositor *compositor_res;
375     struct tizen_indicator *indicator_res;
376         struct wl_surface *surface_res;
377 };
378
379 void
380 IndicatorTest::SetUp(void)
381 {
382     //ds_log_init(DS_DBG, NULL);
383
384     ds_inf("%s", __func__);
385
386     comp = new MockIndicatorCompositor();
387     client = new MockIndicatorClient(&registry_listener);
388     compositor_res = client->GetWlCompositor();
389     indicator_res = client->GetTizenIndicator();
390         surface_res = wl_compositor_create_surface(compositor_res);
391
392     client->RoundTrip();
393 }
394
395 void
396 IndicatorTest::TearDown(void)
397 {
398     ds_inf("%s", __func__);
399
400     wl_surface_destroy(surface_res);
401     client->RoundTrip();
402     EXPECT_TRUE(comp->bSurfaceDestroyed);
403
404     delete client;
405     delete comp;
406 }
407
408
409 TEST_F(IndicatorTest, Create_P)
410 {
411     EXPECT_TRUE(true);
412 }
413
414 TEST_F(IndicatorTest, Req_TizenIndicatorStateOn)
415 {
416     tizen_indicator_set_state(indicator_res, surface_res,
417         TIZEN_INDICATOR_STATE_ON);
418     client->RoundTrip();
419     EXPECT_TRUE(comp->bStateOn);
420     EXPECT_FALSE(comp->bStateOff);
421 }
422
423 TEST_F(IndicatorTest, Req_TizenIndicatorStateOff)
424 {
425     tizen_indicator_set_state(indicator_res, surface_res,
426         TIZEN_INDICATOR_STATE_OFF);
427     client->RoundTrip();
428     EXPECT_FALSE(comp->bStateOn);
429     EXPECT_TRUE(comp->bStateOff);
430 }
431
432 TEST_F(IndicatorTest, Req_TizenIndicatorOpacityModeOpaque)
433 {
434     tizen_indicator_set_opacity_mode(indicator_res, surface_res,
435         TIZEN_INDICATOR_OPACITY_MODE_OPAQUE);
436     client->RoundTrip();
437     EXPECT_TRUE(comp->bOpacityModeOpque);
438     EXPECT_FALSE(comp->bOpacityModeTranslucent);
439     EXPECT_FALSE(comp->bOpacityModeTransparent);
440     EXPECT_FALSE(comp->bOpacityModeBgTransparent);
441 }
442
443 TEST_F(IndicatorTest, Req_TizenIndicatorOpacityModeTranslucent)
444 {
445     tizen_indicator_set_opacity_mode(indicator_res, surface_res,
446         TIZEN_INDICATOR_OPACITY_MODE_TRANSLUCENT);
447     client->RoundTrip();
448     EXPECT_FALSE(comp->bOpacityModeOpque);
449     EXPECT_TRUE(comp->bOpacityModeTranslucent);
450     EXPECT_FALSE(comp->bOpacityModeTransparent);
451     EXPECT_FALSE(comp->bOpacityModeBgTransparent);
452 }
453
454 TEST_F(IndicatorTest, Req_TizenIndicatorOpacityModeTransparent)
455 {
456     tizen_indicator_set_opacity_mode(indicator_res, surface_res,
457         TIZEN_INDICATOR_OPACITY_MODE_TRANSPARENT);
458     client->RoundTrip();
459     EXPECT_FALSE(comp->bOpacityModeOpque);
460     EXPECT_FALSE(comp->bOpacityModeTranslucent);
461     EXPECT_TRUE(comp->bOpacityModeTransparent);
462     EXPECT_FALSE(comp->bOpacityModeBgTransparent);
463 }
464
465 TEST_F(IndicatorTest, Req_TizenIndicatorOpacityModeBgTransparent)
466 {
467     tizen_indicator_set_opacity_mode(indicator_res, surface_res,
468         TIZEN_INDICATOR_OPACITY_MODE_BG_TRANSPARENT);
469     client->RoundTrip();
470     EXPECT_FALSE(comp->bOpacityModeOpque);
471     EXPECT_FALSE(comp->bOpacityModeTranslucent);
472     EXPECT_FALSE(comp->bOpacityModeTransparent);
473     EXPECT_TRUE(comp->bOpacityModeBgTransparent);
474 }
475
476 TEST_F(IndicatorTest, Req_TizenIndicatorVisibleTypeHidden)
477 {
478     tizen_indicator_set_visible_type(indicator_res, surface_res,
479         TIZEN_INDICATOR_VISIBLE_TYPE_HIDDEN);
480     client->RoundTrip();
481     EXPECT_TRUE(comp->bVisibleTypeHidden);
482     EXPECT_FALSE(comp->bVisibleTypeShown);
483 }
484
485 TEST_F(IndicatorTest, Req_TizenIndicatorVisibleTypeShown)
486 {
487     tizen_indicator_set_visible_type(indicator_res, surface_res,
488         TIZEN_INDICATOR_VISIBLE_TYPE_SHOWN);
489     client->RoundTrip();
490     EXPECT_FALSE(comp->bVisibleTypeHidden);
491     EXPECT_TRUE(comp->bVisibleTypeShown);
492 }
493
494 TEST_F(IndicatorTest, Ev_TizenIndicatorFlick)
495 {
496     tizen_indicator_set_state(indicator_res, surface_res,
497         TIZEN_INDICATOR_STATE_ON);
498     client->RoundTrip();
499     EXPECT_TRUE(comp->bStateOn);
500
501     comp->SendFlick();
502     comp->Process();
503
504     client->RoundTrip();
505     EXPECT_TRUE(client->bFlickEvent);
506 }