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