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