change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / tests / tc_input_method.cpp
1 #include "tc_main.h"
2 #include "mockclient.h"
3 #include "mockcompositor.h"
4 #include <libds-tizen/input_method.h>
5 #include <input-method-client-protocol.h>
6
7 #define INPUT_METHOD_VERSION 1
8
9 class MockInputMethodCompositor : public MockCompositor
10 {
11 public:
12     MockInputMethodCompositor()
13         : MockCompositor(&MockInputMethodCompositor::TestSetup, this)
14     {
15         ds_inf("%s : this(%p)", __func__, this);
16
17         // initialize the flags to check
18         bDestroyed = false;
19         bNewInputMethodContext = false;
20     }
21
22     ~MockInputMethodCompositor()
23     {
24         ds_inf("%s : this(%p)", __func__, this);
25     }
26
27     static void TestSetup(void *data)
28     {
29         MockInputMethodCompositor *mockComp =
30             static_cast<MockInputMethodCompositor *>(data);
31         Compositor *comp = mockComp->compositor;
32
33         ds_inf("%s: mockComp(%p)", __func__, mockComp);
34
35         mockComp->mInputMethod = ds_tizen_input_method_create(comp->display);
36
37         // destroy listener
38         mockComp->mDestroyListener.notify =
39             MockInputMethodCompositor::DestroyCallback;
40         mockComp->mDestroyListener.parent = mockComp;
41         ds_tizen_input_method_add_destroy_listener(mockComp->mInputMethod,
42             &mockComp->mDestroyListener);
43     }
44
45     static void DestroyCallback(struct wl_listener *listener, void *data)
46     {
47         ds_inf("%s", __func__);
48
49         MockInputMethodCompositor *mockComp =
50             reinterpret_cast<DestroyListener *>(listener)->parent;
51
52         mockComp->bDestroyed = true;
53     }
54
55     void CreateInputMethodContext()
56     {
57         ds_inf("%s", __func__);
58
59         mInputMethodContext = ds_tizen_input_method_create_context(mInputMethod);
60         if (mInputMethodContext)
61             bNewInputMethodContext = true;
62     }
63
64     void SendActivate()
65     {
66         ds_inf("%s", __func__);
67
68         ds_tizen_input_method_send_activate(mInputMethod, mInputMethodContext);
69     }
70
71     void SendDeactivate()
72     {
73         ds_inf("%s", __func__);
74
75         ds_tizen_input_method_send_deactivate(mInputMethod, mInputMethodContext);
76     }
77
78 public:
79     bool bDestroyed;
80     bool bNewInputMethodContext;
81
82 private:
83     struct ds_tizen_input_method_manager *mInputMethodMgr;
84     struct ds_tizen_input_method *mInputMethod;
85     struct ds_tizen_input_method_context *mInputMethodContext;
86
87     struct DestroyListener: ::wl_listener {
88         MockInputMethodCompositor *parent;
89     };
90     DestroyListener mDestroyListener;
91 };
92
93 class MockInputMethodClient : public MockClient
94 {
95 public:
96     MockInputMethodClient()
97         : bActivate(false),
98           bDeactivate(false),
99           compositor_res(nullptr),
100           zwp_input_method(nullptr)
101     {}
102
103     MockInputMethodClient(const struct wl_registry_listener *listener)
104         : MockClient(listener, this)
105     {
106         ds_inf("%s", __func__);
107     }
108
109     ~MockInputMethodClient()
110     {
111         ds_inf("%s", __func__);
112     }
113
114     void SetWlCompositor(struct wl_compositor *global_res)
115     {
116         ds_inf("%s", __func__);
117
118         compositor_res = global_res;
119     }
120
121     struct wl_compositor *GetWlCompositor()
122     {
123         ds_inf("%s", __func__);
124
125         return compositor_res;
126     }
127
128     void SetInputMethod(struct zwp_input_method_v1 *global_res)
129     {
130         ds_inf("%s", __func__);
131         zwp_input_method = global_res;
132     }
133
134     struct zwp_input_method_v1 *GetInputMethod()
135     {
136         ds_inf("%s", __func__);
137
138         return zwp_input_method;
139     }
140
141 public:
142     bool bActivate;
143     bool bDeactivate;
144
145 private:
146     struct wl_compositor *compositor_res;
147     struct zwp_input_method_v1 *zwp_input_method;
148 };
149
150 static void
151 client_registry_cb_global(void *data, struct wl_registry *registry,
152     uint32_t name, const char *interface, uint32_t version)
153 {
154     ds_inf("%s", __func__);
155
156     MockInputMethodClient *client = static_cast<MockInputMethodClient *>(data);
157     struct wl_compositor *compositor_res;
158     struct zwp_input_method_v1 *zwp_input_method;
159
160     if (!strcmp(interface, "wl_compositor")) {
161         compositor_res = (struct wl_compositor *)wl_registry_bind(registry,
162             name, &wl_compositor_interface, 1);
163         if (compositor_res == nullptr) {
164             ds_err("wl_registry_bind() failed. wl_compositor resource.");
165             return;
166         }
167         client->SetWlCompositor(compositor_res);
168     } else if (!strcmp(interface, "zwp_input_method_v1")) {
169         zwp_input_method = (struct zwp_input_method_v1 *)wl_registry_bind(registry,
170             name, &zwp_input_method_v1_interface, INPUT_METHOD_VERSION);
171         if (zwp_input_method == nullptr) {
172             ds_err("wl_registry_bind() failed. zwp_input_method_v1 resource.");
173             return;
174         }
175         client->SetInputMethod(zwp_input_method);
176     }
177 }
178
179 static void
180 client_registry_cb_global_remove(void *data, struct wl_registry *registry,
181     uint32_t name)
182 {
183     ds_inf("%s", __func__);
184
185     MockInputMethodClient *client = static_cast<MockInputMethodClient *>(data);
186     struct wl_compositor *compositor_res = client->GetWlCompositor();
187     struct zwp_input_method_v1 *input_method_res = client->GetInputMethod();
188
189     zwp_input_method_v1_destroy(input_method_res);
190     wl_compositor_destroy(compositor_res);
191 }
192
193 static const struct wl_registry_listener registry_listener = {
194     .global = client_registry_cb_global,
195     .global_remove = client_registry_cb_global_remove
196 };
197
198 static void
199 im_cb_activate (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx)
200 {
201     ds_inf("%s", __func__);
202
203     MockInputMethodClient *client = static_cast<MockInputMethodClient *>(data);
204     client->bActivate = true;
205 }
206 static void
207 im_cb_deactivate (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx)
208 {
209     ds_inf("%s", __func__);
210
211     MockInputMethodClient *client = static_cast<MockInputMethodClient *>(data);
212     client->bDeactivate = true;
213 }
214 static void
215 im_cb_destroy (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx)
216 {}
217 static void
218 im_cb_show_input_panel (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx, uint32_t angle)
219 {}
220 static void
221 im_cb_hide_input_panel (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx)
222 {}
223 static void
224 im_cb_open_connection (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx)
225 {}
226 static void
227 im_cb_close_connection (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx)
228 {}
229 static void
230 im_cb_set_text_input_id (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx, uint32_t text_input_id)
231 {}
232 static const struct zwp_input_method_v1_listener input_method_cb_listener = {
233     .activate = im_cb_activate,
234     .deactivate = im_cb_deactivate,
235     //for tizen only
236     .destroy = im_cb_destroy,
237     .show_input_panel = im_cb_show_input_panel,
238     .hide_input_panel = im_cb_hide_input_panel,
239     .open_connection = im_cb_open_connection,
240     .close_connection = im_cb_close_connection,
241     .set_text_input_id = im_cb_set_text_input_id,
242 };
243
244 class InputMethodTest : public ::testing::Test
245 {
246 public:
247     void SetUp(void) override;
248     void TearDown(void) override;
249
250     MockInputMethodCompositor *comp;
251     MockInputMethodClient *client;
252     struct wl_compositor *compositor_res;
253     struct zwp_input_method_v1 *input_method_res;
254 };
255
256 void
257 InputMethodTest::SetUp(void)
258 {
259     ds_log_init(DS_INF, NULL);
260
261     ds_inf("%s", __func__);
262
263     comp = new MockInputMethodCompositor();
264     client = new MockInputMethodClient(&registry_listener);
265     compositor_res = client->GetWlCompositor();
266     input_method_res = client->GetInputMethod();
267
268     zwp_input_method_v1_add_listener(input_method_res,
269         &input_method_cb_listener, client);
270
271     client->RoundTrip();
272 }
273
274 void
275 InputMethodTest::TearDown(void)
276 {
277     ds_inf("%s", __func__);
278
279     client->RoundTrip();
280
281     delete client;
282     delete comp;
283 }
284
285 TEST_F(InputMethodTest, Create_P)
286 {
287     EXPECT_TRUE(true);
288 }
289
290 TEST_F(InputMethodTest, CreateInputMethodContext)
291 {
292     comp->CreateInputMethodContext();
293     comp->Process();
294
295     EXPECT_TRUE(comp->bNewInputMethodContext);
296 }
297
298 TEST_F(InputMethodTest, Ev_Activate)
299 {
300     comp->CreateInputMethodContext();
301     comp->Process();
302
303     comp->SendActivate();
304     comp->Process();
305
306     client->RoundTrip();
307     EXPECT_TRUE(client->bActivate);
308 }
309
310 TEST_F(InputMethodTest, Ev_Deactivate)
311 {
312     comp->CreateInputMethodContext();
313     comp->Process();
314
315     comp->SendDeactivate();
316     comp->Process();
317
318     client->RoundTrip();
319     EXPECT_TRUE(client->bDeactivate);
320 }