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 <stdio.h>
30
31 #include "weston-test-client-helper.h"
32 #include "text-client-protocol.h"
33
34 struct text_input_state {
35 int activated;
36 int deactivated;
37 };
38
39 static void
text_input_commit_string(void * data,struct wl_text_input * text_input,uint32_t serial,const char * text)40 text_input_commit_string(void *data,
41 struct wl_text_input *text_input,
42 uint32_t serial,
43 const char *text)
44 {
45 }
46
47 static void
text_input_preedit_string(void * data,struct wl_text_input * text_input,uint32_t serial,const char * text,const char * commit)48 text_input_preedit_string(void *data,
49 struct wl_text_input *text_input,
50 uint32_t serial,
51 const char *text,
52 const char *commit)
53 {
54 }
55
56 static void
text_input_delete_surrounding_text(void * data,struct wl_text_input * text_input,int32_t index,uint32_t length)57 text_input_delete_surrounding_text(void *data,
58 struct wl_text_input *text_input,
59 int32_t index,
60 uint32_t length)
61 {
62 }
63
64 static void
text_input_cursor_position(void * data,struct wl_text_input * text_input,int32_t index,int32_t anchor)65 text_input_cursor_position(void *data,
66 struct wl_text_input *text_input,
67 int32_t index,
68 int32_t anchor)
69 {
70 }
71
72 static void
text_input_preedit_styling(void * data,struct wl_text_input * text_input,uint32_t index,uint32_t length,uint32_t style)73 text_input_preedit_styling(void *data,
74 struct wl_text_input *text_input,
75 uint32_t index,
76 uint32_t length,
77 uint32_t style)
78 {
79 }
80
81 static void
text_input_preedit_cursor(void * data,struct wl_text_input * text_input,int32_t index)82 text_input_preedit_cursor(void *data,
83 struct wl_text_input *text_input,
84 int32_t index)
85 {
86 }
87
88 static void
text_input_modifiers_map(void * data,struct wl_text_input * text_input,struct wl_array * map)89 text_input_modifiers_map(void *data,
90 struct wl_text_input *text_input,
91 struct wl_array *map)
92 {
93 }
94
95 static void
text_input_keysym(void * data,struct wl_text_input * text_input,uint32_t serial,uint32_t time,uint32_t sym,uint32_t state,uint32_t modifiers)96 text_input_keysym(void *data,
97 struct wl_text_input *text_input,
98 uint32_t serial,
99 uint32_t time,
100 uint32_t sym,
101 uint32_t state,
102 uint32_t modifiers)
103 {
104 }
105
106 static void
text_input_enter(void * data,struct wl_text_input * text_input,struct wl_surface * surface)107 text_input_enter(void *data,
108 struct wl_text_input *text_input,
109 struct wl_surface *surface)
110
111 {
112 struct text_input_state *state = data;
113
114 fprintf(stderr, "%s\n", __FUNCTION__);
115
116 state->activated += 1;
117 }
118
119 static void
text_input_leave(void * data,struct wl_text_input * text_input)120 text_input_leave(void *data,
121 struct wl_text_input *text_input)
122 {
123 struct text_input_state *state = data;
124
125 state->deactivated += 1;
126 }
127
128 static void
text_input_input_panel_state(void * data,struct wl_text_input * text_input,uint32_t state)129 text_input_input_panel_state(void *data,
130 struct wl_text_input *text_input,
131 uint32_t state)
132 {
133 }
134
135 static void
text_input_language(void * data,struct wl_text_input * text_input,uint32_t serial,const char * language)136 text_input_language(void *data,
137 struct wl_text_input *text_input,
138 uint32_t serial,
139 const char *language)
140 {
141 }
142
143 static void
text_input_text_direction(void * data,struct wl_text_input * text_input,uint32_t serial,uint32_t direction)144 text_input_text_direction(void *data,
145 struct wl_text_input *text_input,
146 uint32_t serial,
147 uint32_t direction)
148 {
149 }
150
151 static const struct wl_text_input_listener text_input_listener = {
152 text_input_enter,
153 text_input_leave,
154 text_input_modifiers_map,
155 text_input_input_panel_state,
156 text_input_preedit_string,
157 text_input_preedit_styling,
158 text_input_preedit_cursor,
159 text_input_commit_string,
160 text_input_cursor_position,
161 text_input_delete_surrounding_text,
162 text_input_keysym,
163 text_input_language,
164 text_input_text_direction
165 };
166
TEST(text_test)167 TEST(text_test)
168 {
169 struct client *client;
170 struct global *global;
171 struct wl_text_input_manager *factory;
172 struct wl_text_input *text_input;
173 struct text_input_state state;
174
175 client = create_client_and_test_surface(100, 100, 100, 100);
176 assert(client);
177
178 factory = NULL;
179 wl_list_for_each(global, &client->global_list, link) {
180 if (strcmp(global->interface, "wl_text_input_manager") == 0)
181 factory = wl_registry_bind(client->wl_registry,
182 global->name,
183 &wl_text_input_manager_interface, 1);
184 }
185
186 assert(factory);
187
188 memset(&state, 0, sizeof state);
189 text_input = wl_text_input_manager_create_text_input(factory);
190 wl_text_input_add_listener(text_input, &text_input_listener, &state);
191
192 /* Make sure our test surface has keyboard focus. */
193 weston_test_activate_surface(client->test->weston_test,
194 client->surface->wl_surface);
195 client_roundtrip(client);
196 assert(client->input->keyboard->focus == client->surface);
197
198 /* Activate test model and make sure we get enter event. */
199 wl_text_input_activate(text_input, client->input->wl_seat,
200 client->surface->wl_surface);
201 client_roundtrip(client);
202 assert(state.activated == 1 && state.deactivated == 0);
203
204 /* Deactivate test model and make sure we get leave event. */
205 wl_text_input_deactivate(text_input, client->input->wl_seat);
206 client_roundtrip(client);
207 assert(state.activated == 1 && state.deactivated == 1);
208
209 /* Activate test model again. */
210 wl_text_input_activate(text_input, client->input->wl_seat,
211 client->surface->wl_surface);
212 client_roundtrip(client);
213 assert(state.activated == 2 && state.deactivated == 1);
214
215 /* Take keyboard focus away and verify we get leave event. */
216 weston_test_activate_surface(client->test->weston_test, NULL);
217 client_roundtrip(client);
218 assert(state.activated == 2 && state.deactivated == 2);
219 }
220