malloc + memset -> zalloc
[platform/upstream/weston.git] / tests / text-test.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <linux/input.h>
26 #include "weston-test-client-helper.h"
27 #include "../clients/text-client-protocol.h"
28
29 struct text_input_state {
30         int activated;
31         int deactivated;
32 };
33
34 static void
35 text_input_commit_string(void *data,
36                          struct wl_text_input *text_input,
37                          uint32_t serial,
38                          const char *text)
39 {
40 }
41
42 static void
43 text_input_preedit_string(void *data,
44                           struct wl_text_input *text_input,
45                           uint32_t serial,
46                           const char *text,
47                           const char *commit)
48 {
49 }
50
51 static void
52 text_input_delete_surrounding_text(void *data,
53                                    struct wl_text_input *text_input,
54                                    int32_t index,
55                                    uint32_t length)
56 {
57 }
58
59 static void
60 text_input_cursor_position(void *data,
61                            struct wl_text_input *text_input,
62                            int32_t index,
63                            int32_t anchor)
64 {
65 }
66
67 static void
68 text_input_preedit_styling(void *data,
69                            struct wl_text_input *text_input,
70                            uint32_t index,
71                            uint32_t length,
72                            uint32_t style)
73 {
74 }
75
76 static void
77 text_input_preedit_cursor(void *data,
78                           struct wl_text_input *text_input,
79                           int32_t index)
80 {
81 }
82
83 static void
84 text_input_modifiers_map(void *data,
85                          struct wl_text_input *text_input,
86                          struct wl_array *map)
87 {
88 }
89
90 static void
91 text_input_keysym(void *data,
92                   struct wl_text_input *text_input,
93                   uint32_t serial,
94                   uint32_t time,
95                   uint32_t sym,
96                   uint32_t state,
97                   uint32_t modifiers)
98 {
99 }
100
101 static void
102 text_input_enter(void *data,
103                  struct wl_text_input *text_input,
104                  struct wl_surface *surface)
105
106 {
107         struct text_input_state *state = data;
108
109         fprintf(stderr, "%s\n", __FUNCTION__);
110
111         state->activated += 1;
112 }
113
114 static void
115 text_input_leave(void *data,
116                  struct wl_text_input *text_input)
117 {
118         struct text_input_state *state = data;
119
120         state->deactivated += 1;
121 }
122
123 static void
124 text_input_input_panel_state(void *data,
125                              struct wl_text_input *text_input,
126                              uint32_t state)
127 {
128 }
129
130 static void
131 text_input_language(void *data,
132                     struct wl_text_input *text_input,
133                     uint32_t serial,
134                     const char *language)
135 {
136 }
137
138 static void
139 text_input_text_direction(void *data,
140                           struct wl_text_input *text_input,
141                           uint32_t serial,
142                           uint32_t direction)
143 {
144 }
145
146 static const struct wl_text_input_listener text_input_listener = {
147         text_input_enter,
148         text_input_leave,
149         text_input_modifiers_map,
150         text_input_input_panel_state,
151         text_input_preedit_string,
152         text_input_preedit_styling,
153         text_input_preedit_cursor,
154         text_input_commit_string,
155         text_input_cursor_position,
156         text_input_delete_surrounding_text,
157         text_input_keysym,
158         text_input_language,
159         text_input_text_direction
160 };
161
162 TEST(text_test)
163 {
164         struct client *client;
165         struct global *global;
166         struct wl_text_input_manager *factory;
167         struct wl_text_input *text_input;
168         struct text_input_state state;
169
170         client = client_create(100, 100, 100, 100);
171         assert(client);
172
173         factory = NULL;
174         wl_list_for_each(global, &client->global_list, link) {
175                 if (strcmp(global->interface, "wl_text_input_manager") == 0)
176                         factory = wl_registry_bind(client->wl_registry,
177                                                    global->name,
178                                                    &wl_text_input_manager_interface, 1);
179         }
180
181         assert(factory);
182
183         memset(&state, 0, sizeof state);
184         text_input = wl_text_input_manager_create_text_input(factory);
185         wl_text_input_add_listener(text_input, &text_input_listener, &state);
186
187         /* Make sure our test surface has keyboard focus. */
188         wl_test_activate_surface(client->test->wl_test,
189                                  client->surface->wl_surface);
190         client_roundtrip(client);
191         assert(client->input->keyboard->focus == client->surface);
192
193         /* Activate test model and make sure we get enter event. */
194         wl_text_input_activate(text_input, client->input->wl_seat,
195                                client->surface->wl_surface);
196         client_roundtrip(client);
197         assert(state.activated == 1 && state.deactivated == 0);
198
199         /* Deactivate test model and make sure we get leave event. */
200         wl_text_input_deactivate(text_input, client->input->wl_seat);
201         client_roundtrip(client);
202         assert(state.activated == 1 && state.deactivated == 1);
203
204         /* Activate test model again. */
205         wl_text_input_activate(text_input, client->input->wl_seat,
206                                client->surface->wl_surface);
207         client_roundtrip(client);
208         assert(state.activated == 2 && state.deactivated == 1);
209
210         /* Take keyboard focus away and verify we get leave event. */
211         wl_test_activate_surface(client->test->wl_test, NULL);
212         client_roundtrip(client);
213         assert(state.activated == 2 && state.deactivated == 2);
214 }