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