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