1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdint.h>
30 #include <stdio.h>
31
32 #include "weston-test-client-helper.h"
33 #include "text-input-unstable-v1-client-protocol.h"
34 #include "weston-test-fixture-compositor.h"
35
36 static enum test_result_code
fixture_setup(struct weston_test_harness * harness)37 fixture_setup(struct weston_test_harness *harness)
38 {
39 struct compositor_setup setup;
40
41 compositor_setup_defaults(&setup);
42
43 return weston_test_harness_execute_as_client(harness, &setup);
44 }
45 DECLARE_FIXTURE_SETUP(fixture_setup);
46
47 struct text_input_state {
48 int activated;
49 int deactivated;
50 };
51
52 static void
text_input_commit_string(void * data,struct zwp_text_input_v1 * text_input,uint32_t serial,const char * text)53 text_input_commit_string(void *data,
54 struct zwp_text_input_v1 *text_input,
55 uint32_t serial,
56 const char *text)
57 {
58 }
59
60 static void
text_input_preedit_string(void * data,struct zwp_text_input_v1 * text_input,uint32_t serial,const char * text,const char * commit)61 text_input_preedit_string(void *data,
62 struct zwp_text_input_v1 *text_input,
63 uint32_t serial,
64 const char *text,
65 const char *commit)
66 {
67 }
68
69 static void
text_input_delete_surrounding_text(void * data,struct zwp_text_input_v1 * text_input,int32_t index,uint32_t length)70 text_input_delete_surrounding_text(void *data,
71 struct zwp_text_input_v1 *text_input,
72 int32_t index,
73 uint32_t length)
74 {
75 }
76
77 static void
text_input_cursor_position(void * data,struct zwp_text_input_v1 * text_input,int32_t index,int32_t anchor)78 text_input_cursor_position(void *data,
79 struct zwp_text_input_v1 *text_input,
80 int32_t index,
81 int32_t anchor)
82 {
83 }
84
85 static void
text_input_preedit_styling(void * data,struct zwp_text_input_v1 * text_input,uint32_t index,uint32_t length,uint32_t style)86 text_input_preedit_styling(void *data,
87 struct zwp_text_input_v1 *text_input,
88 uint32_t index,
89 uint32_t length,
90 uint32_t style)
91 {
92 }
93
94 static void
text_input_preedit_cursor(void * data,struct zwp_text_input_v1 * text_input,int32_t index)95 text_input_preedit_cursor(void *data,
96 struct zwp_text_input_v1 *text_input,
97 int32_t index)
98 {
99 }
100
101 static void
text_input_modifiers_map(void * data,struct zwp_text_input_v1 * text_input,struct wl_array * map)102 text_input_modifiers_map(void *data,
103 struct zwp_text_input_v1 *text_input,
104 struct wl_array *map)
105 {
106 }
107
108 static void
text_input_keysym(void * data,struct zwp_text_input_v1 * text_input,uint32_t serial,uint32_t time,uint32_t sym,uint32_t state,uint32_t modifiers)109 text_input_keysym(void *data,
110 struct zwp_text_input_v1 *text_input,
111 uint32_t serial,
112 uint32_t time,
113 uint32_t sym,
114 uint32_t state,
115 uint32_t modifiers)
116 {
117 }
118
119 static void
text_input_enter(void * data,struct zwp_text_input_v1 * text_input,struct wl_surface * surface)120 text_input_enter(void *data,
121 struct zwp_text_input_v1 *text_input,
122 struct wl_surface *surface)
123
124 {
125 struct text_input_state *state = data;
126
127 testlog("%s\n", __FUNCTION__);
128
129 state->activated += 1;
130 }
131
132 static void
text_input_leave(void * data,struct zwp_text_input_v1 * text_input)133 text_input_leave(void *data,
134 struct zwp_text_input_v1 *text_input)
135 {
136 struct text_input_state *state = data;
137
138 state->deactivated += 1;
139 }
140
141 static void
text_input_input_panel_state(void * data,struct zwp_text_input_v1 * text_input,uint32_t state)142 text_input_input_panel_state(void *data,
143 struct zwp_text_input_v1 *text_input,
144 uint32_t state)
145 {
146 }
147
148 static void
text_input_language(void * data,struct zwp_text_input_v1 * text_input,uint32_t serial,const char * language)149 text_input_language(void *data,
150 struct zwp_text_input_v1 *text_input,
151 uint32_t serial,
152 const char *language)
153 {
154 }
155
156 static void
text_input_text_direction(void * data,struct zwp_text_input_v1 * text_input,uint32_t serial,uint32_t direction)157 text_input_text_direction(void *data,
158 struct zwp_text_input_v1 *text_input,
159 uint32_t serial,
160 uint32_t direction)
161 {
162 }
163
164 static const struct zwp_text_input_v1_listener text_input_listener = {
165 text_input_enter,
166 text_input_leave,
167 text_input_modifiers_map,
168 text_input_input_panel_state,
169 text_input_preedit_string,
170 text_input_preedit_styling,
171 text_input_preedit_cursor,
172 text_input_commit_string,
173 text_input_cursor_position,
174 text_input_delete_surrounding_text,
175 text_input_keysym,
176 text_input_language,
177 text_input_text_direction
178 };
179
TEST(text_test)180 TEST(text_test)
181 {
182 struct client *client;
183 struct global *global;
184 struct zwp_text_input_manager_v1 *factory;
185 struct zwp_text_input_v1 *text_input;
186 struct text_input_state state;
187
188 client = create_client_and_test_surface(100, 100, 100, 100);
189 assert(client);
190
191 factory = NULL;
192 wl_list_for_each(global, &client->global_list, link) {
193 if (strcmp(global->interface, "zwp_text_input_manager_v1") == 0)
194 factory = wl_registry_bind(client->wl_registry,
195 global->name,
196 &zwp_text_input_manager_v1_interface, 1);
197 }
198
199 assert(factory);
200
201 memset(&state, 0, sizeof state);
202 text_input = zwp_text_input_manager_v1_create_text_input(factory);
203 zwp_text_input_v1_add_listener(text_input,
204 &text_input_listener,
205 &state);
206
207 /* Make sure our test surface has keyboard focus. */
208 weston_test_activate_surface(client->test->weston_test,
209 client->surface->wl_surface);
210 client_roundtrip(client);
211 assert(client->input->keyboard->focus == client->surface);
212
213 /* Activate test model and make sure we get enter event. */
214 zwp_text_input_v1_activate(text_input, client->input->wl_seat,
215 client->surface->wl_surface);
216 client_roundtrip(client);
217 assert(state.activated == 1 && state.deactivated == 0);
218
219 /* Deactivate test model and make sure we get leave event. */
220 zwp_text_input_v1_deactivate(text_input, client->input->wl_seat);
221 client_roundtrip(client);
222 assert(state.activated == 1 && state.deactivated == 1);
223
224 /* Activate test model again. */
225 zwp_text_input_v1_activate(text_input, client->input->wl_seat,
226 client->surface->wl_surface);
227 client_roundtrip(client);
228 assert(state.activated == 2 && state.deactivated == 1);
229
230 /* Take keyboard focus away and verify we get leave event. */
231 weston_test_activate_surface(client->test->weston_test, NULL);
232 client_roundtrip(client);
233 assert(state.activated == 2 && state.deactivated == 2);
234 }
235