text: Remove surface arg in create_text_model
[profile/ivi/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 <stdlib.h>
24 #include <stdio.h>
25 #include <sys/socket.h>
26 #include <assert.h>
27 #include <unistd.h>
28
29 #include <string.h>
30
31 #include "test-runner.h"
32
33 struct text_test_data {
34         struct weston_layer *layer;
35
36         unsigned int expected_activated_count;
37         unsigned int expected_deactivated_count;
38
39         const char *next_command;
40         void (*next_handle)(struct test_client *client);
41 };
42
43 static void
44 pre_assert_state(struct test_client *client,
45                  unsigned int expected_activated_count,
46                  unsigned int expected_deactivated_count)
47 {
48         unsigned int activated_count, deactivated_count;
49
50         assert(sscanf(client->buf, "activated %u deactivated %u", &activated_count, &deactivated_count) == 2);
51         fprintf(stderr, "Text model activations: %u deactivations: %u\n", activated_count, deactivated_count);
52         assert(activated_count == expected_activated_count);
53         assert(deactivated_count == expected_deactivated_count);
54 }
55
56 static void
57 handle_assert_state(struct test_client *client)
58 {
59         struct text_test_data *data = client->data;
60
61         pre_assert_state(client, data->expected_activated_count, data->expected_deactivated_count);
62
63         test_client_send(client, data->next_command);
64         client->handle = data->next_handle;
65 }
66
67 static void
68 post_assert_state(struct test_client *client,
69                   unsigned int expected_activated_count,
70                   unsigned int expected_deactivated_count,
71                   const char *next_command,
72                   void (*next_handle)(struct test_client *client))
73 {
74         struct text_test_data *data = client->data;
75
76         data->expected_activated_count = expected_activated_count;
77         data->expected_deactivated_count = expected_deactivated_count;
78
79         data->next_command = next_command;
80         data->next_handle = next_handle;
81
82         test_client_send(client, "assert-state\n");
83         client->handle = handle_assert_state;
84 }
85
86 static struct weston_seat*
87 get_seat(struct test_client *client)
88 {
89         struct wl_list *seat_list;
90         struct weston_seat *seat;
91
92         seat_list = &client->compositor->seat_list;
93         assert(wl_list_length(seat_list) == 1);
94         seat = container_of(seat_list->next, struct weston_seat, link);
95
96         return seat;
97 }
98
99 static void
100 handle_surface_unfocus(struct test_client *client)
101 {
102         struct weston_seat *seat;
103
104         seat = get_seat(client);
105
106         pre_assert_state(client, 2, 1);
107
108         /* Unfocus the surface */
109         wl_keyboard_set_focus(&seat->keyboard, NULL);
110
111         post_assert_state(client, 2, 2, "bye\n", NULL);
112 }
113
114 static void
115 handle_reactivate_text_model(struct test_client *client)
116 {
117         pre_assert_state(client, 1, 1);
118
119         /* text_model is activated */
120
121         post_assert_state(client, 2, 1,
122                           "assert-state\n", handle_surface_unfocus);
123 }
124
125 static void
126 handle_deactivate_text_model(struct test_client *client)
127 {
128         pre_assert_state(client, 1, 0);
129
130         /* text_model is deactivated */
131
132         post_assert_state(client, 1, 1,
133                           "activate-text-model\n", handle_reactivate_text_model);
134 }
135
136 static void
137 handle_activate_text_model(struct test_client *client)
138 {
139         pre_assert_state(client, 0, 0);
140
141         /* text_model is activated */
142
143         post_assert_state(client, 1, 0,
144                           "deactivate-text-model\n", handle_deactivate_text_model);
145 }
146
147 static void
148 handle_text_model(struct test_client *client)
149 {
150         uint32_t id;
151         struct wl_resource *resource;
152
153         assert(sscanf(client->buf, "text_model %u", &id) == 1);
154         fprintf(stderr, "got text_model id %u\n", id);
155         resource = wl_client_get_object(client->client, id);
156         assert(resource);
157         assert(strcmp(resource->object.interface->name, "text_model") == 0);
158
159         test_client_send(client, "activate-text-model\n");
160         client->handle = handle_activate_text_model;
161 }
162
163 static void
164 handle_surface(struct test_client *client)
165 {
166         uint32_t id;
167         struct wl_resource *resource;
168         struct weston_surface *surface;
169         struct text_test_data *data = client->data;
170         struct weston_seat *seat;
171
172         assert(sscanf(client->buf, "surface %u", &id) == 1);
173         fprintf(stderr, "got surface id %u\n", id);
174         resource = wl_client_get_object(client->client, id);
175         assert(resource);
176         assert(strcmp(resource->object.interface->name, "wl_surface") == 0);
177
178         surface = (struct weston_surface *) resource;
179
180         weston_surface_configure(surface, 100, 100, 200, 200);
181         weston_surface_assign_output(surface);
182         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
183
184         data->layer = malloc(sizeof *data->layer);
185         weston_layer_init(data->layer, &client->compositor->cursor_layer.link);
186         wl_list_insert(&data->layer->surface_list, &surface->layer_link);
187         weston_surface_damage(surface);
188
189         seat = get_seat(client);
190         client->compositor->focus = 1; /* Make it work even if pointer is
191                                         * outside X window. */
192         wl_keyboard_set_focus(&seat->keyboard, &surface->surface);
193
194         test_client_send(client, "create-text-model\n");
195         client->handle = handle_text_model;
196 }
197
198 TEST(text_test)
199 {
200         struct test_client *client;
201         struct text_test_data *data;
202
203         client = test_client_launch(compositor, "test-text-client");
204         client->terminate = 1;
205
206         test_client_send(client, "create-surface\n");
207         client->handle = handle_surface;
208
209         data = malloc(sizeof *data);
210         assert(data);
211         client->data = data;
212
213 }