change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / tests / tc_input_method_manager.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 MockInputMethodMgrCompositor : public MockCompositor
10 {
11 public:
12     MockInputMethodMgrCompositor()
13         : MockCompositor(&MockInputMethodMgrCompositor::TestSetup, this)
14     {
15         ds_inf("%s : this(%p)", __func__, this);
16
17         // initialize the flags to check
18         bDestroyed = false;
19
20         bSetTransientFor = false;
21         mParentPid = 0;
22         mChildPid = 0;
23     }
24
25     ~MockInputMethodMgrCompositor()
26     {
27         ds_inf("%s : this(%p)", __func__, this);
28     }
29
30     static void TestSetup(void *data)
31     {
32         MockInputMethodMgrCompositor *mockComp =
33             static_cast<MockInputMethodMgrCompositor *>(data);
34         Compositor *comp = mockComp->compositor;
35
36         ds_inf("%s: mockComp(%p)", __func__, mockComp);
37
38         mockComp->mInputMethodMgr = ds_tizen_input_method_manager_create(comp->display);
39
40         // destroy listener
41         mockComp->mDestroyListener.notify =
42             MockInputMethodMgrCompositor::DestroyCallback;
43         mockComp->mDestroyListener.parent = mockComp;
44         ds_tizen_input_method_manager_add_destroy_listener(mockComp->mInputMethodMgr,
45             &mockComp->mDestroyListener);
46
47         // set_transient_for listener
48         mockComp->mSetTransientForListener.notify =
49             MockInputMethodMgrCompositor::SetTransientForCallback;
50         mockComp->mSetTransientForListener.parent = mockComp;
51         ds_tizen_input_method_manager_add_set_transient_for_listener(mockComp->mInputMethodMgr,
52             &mockComp->mSetTransientForListener);
53     }
54
55     static void DestroyCallback(struct wl_listener *listener, void *data)
56     {
57         ds_inf("%s", __func__);
58
59         MockInputMethodMgrCompositor *mockComp =
60             reinterpret_cast<DestroyListener *>(listener)->parent;
61
62         mockComp->bDestroyed = true;
63     }
64
65     static void SetTransientForCallback(struct wl_listener *listener, void *data)
66     {
67         struct ds_tizen_input_method_manager_event_set_transient_for *event =
68             (struct ds_tizen_input_method_manager_event_set_transient_for *)data;
69
70         ds_inf("%s", __func__);
71
72         MockInputMethodMgrCompositor *mockComp =
73             reinterpret_cast<SetTransientForListener *>(listener)->parent;
74
75         mockComp->bSetTransientFor = true;
76         mockComp->mParentPid = event->pid_parent;
77         mockComp->mChildPid = event->pid_child;
78     }
79
80 public:
81     bool bDestroyed;
82     bool bSetTransientFor;
83     uint32_t mParentPid;
84     uint32_t mChildPid;
85
86 private:
87     struct ds_tizen_input_method_manager *mInputMethodMgr;
88     struct DestroyListener: ::wl_listener {
89         MockInputMethodMgrCompositor *parent;
90     };
91     DestroyListener mDestroyListener;
92
93     struct SetTransientForListener: ::wl_listener {
94         MockInputMethodMgrCompositor *parent;
95     };
96     SetTransientForListener mSetTransientForListener;
97 };
98
99 class MockInputMethodMgrClient : public MockClient
100 {
101 public:
102     MockInputMethodMgrClient()
103         : bActivated(false),
104           compositor_res(nullptr),
105           zwp_input_method_manager(nullptr)
106     {}
107
108     MockInputMethodMgrClient(const struct wl_registry_listener *listener)
109         : MockClient(listener, this)
110     {
111         ds_inf("%s", __func__);
112     }
113
114     ~MockInputMethodMgrClient()
115     {
116         ds_inf("%s", __func__);
117     }
118
119     void SetWlCompositor(struct wl_compositor *global_res)
120     {
121         ds_inf("%s", __func__);
122
123         compositor_res = global_res;
124     }
125
126     struct wl_compositor *GetWlCompositor()
127     {
128         ds_inf("%s", __func__);
129
130         return compositor_res;
131     }
132
133     void SetInputMethodMgr(struct zwp_input_method_manager_v1 *global_res)
134     {
135         ds_inf("%s", __func__);
136         zwp_input_method_manager = global_res;
137     }
138
139     struct zwp_input_method_manager_v1 *GetInputMethodMgr()
140     {
141         ds_inf("%s", __func__);
142
143         return zwp_input_method_manager;
144     }
145
146 public:
147     bool bActivated;
148
149 private:
150     struct wl_compositor *compositor_res;
151     struct zwp_input_method_manager_v1 *zwp_input_method_manager;
152 };
153
154 static void
155 client_registry_cb_global(void *data, struct wl_registry *registry,
156     uint32_t name, const char *interface, uint32_t version)
157 {
158     ds_inf("%s", __func__);
159
160     MockInputMethodMgrClient *client = static_cast<MockInputMethodMgrClient *>(data);
161     struct wl_compositor *compositor_res;
162     struct zwp_input_method_manager_v1 *zwp_input_method_manager;
163
164     if (!strcmp(interface, "wl_compositor")) {
165         compositor_res = (struct wl_compositor *)wl_registry_bind(registry,
166             name, &wl_compositor_interface, 1);
167         if (compositor_res == nullptr) {
168             ds_err("wl_registry_bind() failed. wl_compositor resource.");
169             return;
170         }
171         client->SetWlCompositor(compositor_res);
172     } else if (!strcmp(interface, "zwp_input_method_manager_v1")) {
173         zwp_input_method_manager = (struct zwp_input_method_manager_v1 *)wl_registry_bind(registry,
174             name, &zwp_input_method_manager_v1_interface, INPUT_METHOD_VERSION);
175         if (zwp_input_method_manager == nullptr) {
176             ds_err("wl_registry_bind() failed. zwp_input_method_manager_v1 resource.");
177             return;
178         }
179         client->SetInputMethodMgr(zwp_input_method_manager);
180     }
181 }
182
183 static void
184 client_registry_cb_global_remove(void *data, struct wl_registry *registry,
185     uint32_t name)
186 {
187     ds_inf("%s", __func__);
188
189     MockInputMethodMgrClient *client = static_cast<MockInputMethodMgrClient *>(data);
190     struct wl_compositor *compositor_res = client->GetWlCompositor();
191     struct zwp_input_method_manager_v1 *input_method_mgr_res = client->GetInputMethodMgr();
192
193     zwp_input_method_manager_v1_destroy(input_method_mgr_res);
194     wl_compositor_destroy(compositor_res);
195 }
196
197 static const struct wl_registry_listener registry_listener = {
198     .global = client_registry_cb_global,
199     .global_remove = client_registry_cb_global_remove
200 };
201
202 class InputMethodMgrTest : public ::testing::Test
203 {
204 public:
205     void SetUp(void) override;
206     void TearDown(void) override;
207
208     MockInputMethodMgrCompositor *comp;
209     MockInputMethodMgrClient *client;
210     struct wl_compositor *compositor_res;
211     struct zwp_input_method_manager_v1 *input_method_mgr_res;
212 };
213
214 void
215 InputMethodMgrTest::SetUp(void)
216 {
217     ds_log_init(DS_INF, NULL);
218
219     ds_inf("%s", __func__);
220
221     comp = new MockInputMethodMgrCompositor();
222     client = new MockInputMethodMgrClient(&registry_listener);
223     compositor_res = client->GetWlCompositor();
224     input_method_mgr_res = client->GetInputMethodMgr();
225
226     client->RoundTrip();
227 }
228
229 void
230 InputMethodMgrTest::TearDown(void)
231 {
232     ds_inf("%s", __func__);
233
234     client->RoundTrip();
235
236     delete client;
237     delete comp;
238 }
239
240 TEST_F(InputMethodMgrTest, Create_P)
241 {
242     EXPECT_TRUE(true);
243 }
244
245 TEST_F(InputMethodMgrTest, Req_SetTransientFor)
246 {
247     uint32_t parent_pid = 123;
248     uint32_t child_pid = 321;
249
250     zwp_input_method_manager_v1_set_transient_for(input_method_mgr_res, parent_pid, child_pid);
251     client->RoundTrip();
252     EXPECT_TRUE(comp->mParentPid == parent_pid);
253     EXPECT_TRUE(comp->mChildPid == child_pid);
254 }