tests: add test_seat_release() for symmetry
[platform/upstream/weston.git] / tests / event-test.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2013 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include "config.h"
28
29 #include "weston-test-client-helper.h"
30
31 static int
32 output_contains_client(struct client *client)
33 {
34         struct output *output = client->output;
35         struct surface *surface = client->surface;
36
37         return !(output->x >= surface->x + surface->width
38                 || output->x + output->width <= surface->x
39                 || output->y >= surface->y + surface->height
40                 || output->y + output->height <= surface->y);
41 }
42
43 static void
44 check_client_move(struct client *client, int x, int y)
45 {
46         move_client(client, x, y);
47
48         if (output_contains_client(client)) {
49                 assert(client->surface->output == client->output);
50         } else {
51                 assert(client->surface->output == NULL);
52         }
53 }
54
55 TEST(test_surface_output)
56 {
57         struct client *client;
58         int x, y;
59
60         client = create_client_and_test_surface(100, 100, 100, 100);
61         assert(client);
62
63         assert(output_contains_client(client));
64
65         /* not visible */
66         x = 0;
67         y = -client->surface->height;
68         check_client_move(client, x, y);
69
70         /* visible */
71         check_client_move(client, x, ++y);
72
73         /* not visible */
74         x = -client->surface->width;
75         y = 0;
76         check_client_move(client, x, y);
77
78         /* visible */
79         check_client_move(client, ++x, y);
80
81         /* not visible */
82         x = client->output->width;
83         y = 0;
84         check_client_move(client, x, y);
85
86         /* visible */
87         check_client_move(client, --x, y);
88         assert(output_contains_client(client));
89
90         /* not visible */
91         x = 0;
92         y = client->output->height;
93         check_client_move(client, x, y);
94         assert(!output_contains_client(client));
95
96         /* visible */
97         check_client_move(client, x, --y);
98         assert(output_contains_client(client));
99 }
100
101 static void
102 buffer_release_handler(void *data, struct wl_buffer *buffer)
103 {
104         int *released = data;
105
106         *released = 1;
107 }
108
109 static struct wl_buffer_listener buffer_listener = {
110         buffer_release_handler
111 };
112
113 TEST(buffer_release)
114 {
115         struct client *client;
116         struct wl_surface *surface;
117         struct buffer *buf1;
118         struct buffer *buf2;
119         struct buffer *buf3;
120         int buf1_released = 0;
121         int buf2_released = 0;
122         int buf3_released = 0;
123         int frame;
124
125         client = create_client_and_test_surface(100, 100, 100, 100);
126         assert(client);
127         surface = client->surface->wl_surface;
128
129         buf1 = create_shm_buffer_a8r8g8b8(client, 100, 100);
130         wl_buffer_add_listener(buf1->proxy, &buffer_listener, &buf1_released);
131
132         buf2 = create_shm_buffer_a8r8g8b8(client, 100, 100);
133         wl_buffer_add_listener(buf2->proxy, &buffer_listener, &buf2_released);
134
135         buf3 = create_shm_buffer_a8r8g8b8(client, 100, 100);
136         wl_buffer_add_listener(buf3->proxy, &buffer_listener, &buf3_released);
137
138         /*
139          * buf1 must never be released, since it is replaced before
140          * it is committed, therefore it never becomes busy.
141          */
142
143         wl_surface_attach(surface, buf1->proxy, 0, 0);
144         wl_surface_attach(surface, buf2->proxy, 0, 0);
145         frame_callback_set(surface, &frame);
146         wl_surface_commit(surface);
147         frame_callback_wait(client, &frame);
148         assert(buf1_released == 0);
149         /* buf2 may or may not be released */
150         assert(buf3_released == 0);
151
152         wl_surface_attach(surface, buf3->proxy, 0, 0);
153         frame_callback_set(surface, &frame);
154         wl_surface_commit(surface);
155         frame_callback_wait(client, &frame);
156         assert(buf1_released == 0);
157         assert(buf2_released == 1);
158         /* buf3 may or may not be released */
159
160         wl_surface_attach(surface, client->surface->buffer->proxy, 0, 0);
161         frame_callback_set(surface, &frame);
162         wl_surface_commit(surface);
163         frame_callback_wait(client, &frame);
164         assert(buf1_released == 0);
165         assert(buf2_released == 1);
166         assert(buf3_released == 1);
167 }