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