summaryrefslogtreecommitdiff
path: root/tests/pointer-shot-test.c
blob: a78d7ed9f605054ee7237e6a29dc8a47598b718b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright 2021 Collabora Ltd
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

#include "shared/timespec-util.h"
#include "weston-test-client-helper.h"
#include "weston-test-fixture-compositor.h"

struct setup_args {
	struct fixture_metadata meta;
	enum weston_renderer_type renderer;
};

static const struct setup_args my_setup_args[] = {
	{
		.renderer = WESTON_RENDERER_PIXMAN,
		.meta.name = "pixman"
	},
	{
		.renderer = WESTON_RENDERER_GL,
		.meta.name = "GL"
	},
};

static enum test_result_code
fixture_setup(struct weston_test_harness *harness, const struct setup_args *arg)
{
	struct compositor_setup setup;

	compositor_setup_defaults(&setup);
	setup.renderer = arg->renderer;
	setup.width = 320;
	setup.height = 240;
	setup.shell = SHELL_TEST_DESKTOP;

	return weston_test_harness_execute_as_client(harness, &setup);
}
DECLARE_FIXTURE_SETUP_WITH_ARG(fixture_setup, my_setup_args, meta);

static const struct timespec t0 = { .tv_sec = 0, .tv_nsec = 0 };
static const struct timespec t1 = { .tv_sec = 1, .tv_nsec = 0 };
static const struct timespec t2 = { .tv_sec = 2, .tv_nsec = 0 };
static const struct timespec t3 = { .tv_sec = 3, .tv_nsec = 0 };

static void
send_motion(struct client *client, const struct timespec *time, int x, int y)
{
	uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;

	timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
	weston_test_move_pointer(client->test->weston_test, tv_sec_hi, tv_sec_lo,
				 tv_nsec, x, y);
	client_roundtrip(client);
}

static struct buffer *
surface_commit_color(struct client *client, struct surface *surface,
		     pixman_color_t *color, int width, int height)
{
	struct buffer *buf;

	buf = create_shm_buffer_a8r8g8b8(client, width, height);
	fill_image_with_color(buf->image, color);
	wl_surface_attach(surface->wl_surface, buf->proxy, 0, 0);
	wl_surface_damage(surface->wl_surface, 0, 0, width, height);
	wl_surface_commit(surface->wl_surface);

	assert(!surface->buffer);
	surface->buffer = buf;

	return buf;
}

TEST(pointer_cursor_retains_committed_buffer_after_reenter)
{
	struct client *client;
	pixman_color_t red;
	pixman_color_t green;
	pixman_color_t gray;
	pixman_color_t magenta;
	bool match;
	struct surface *main_surface;
	struct surface *back_surface;
	struct surface *main_cursor_surface;
	struct surface *back_cursor_surface;

	color_rgb888(&red, 255, 0, 0);
	color_rgb888(&green, 0, 255, 0);
	color_rgb888(&gray, 127, 127, 127);
	color_rgb888(&magenta, 255, 0, 255);

	client = create_client();

	/* Move the cursor out of the way of the main surface */
	send_motion(client, &t0, 0, 0);

	/* Create all surfaces. */
	main_surface = create_test_surface(client);
	back_surface = create_test_surface(client);
	main_cursor_surface = create_test_surface(client);
	back_cursor_surface = create_test_surface(client);

	/* Commit buffers for cursors. */
	surface_commit_color(client, main_cursor_surface, &green, 25, 25);
	surface_commit_color(client, back_cursor_surface, &magenta, 25, 25);

	/* We need our own background surface so that we are able to change the cursor
	 * when the pointer leaves the main surface.
	 */
	weston_test_move_surface(client->test->weston_test, back_surface->wl_surface, 0, 0);
	surface_commit_color(client, back_surface, &gray, 320, 240);

	/* Set up the main surface. */
	client->surface = main_surface;
	client->surface->buffer = create_shm_buffer_a8r8g8b8(client, 100, 100);
	fill_image_with_color(client->surface->buffer->image, &red);
	move_client(client, 50, 50);

	/* Move the pointer into the main surface. */
	send_motion(client, &t1, 100, 100);
	wl_pointer_set_cursor(client->input->pointer->wl_pointer,
			      client->input->pointer->serial,
			      main_cursor_surface->wl_surface, 0, 0);
	match = verify_screen_content(client, "pointer_cursor_reenter", 0,
				      NULL, 0, NULL);
	assert(match);

	/* Move the cursor just outside the main surface. */
	send_motion(client, &t2, 150, 150);
	wl_pointer_set_cursor(client->input->pointer->wl_pointer,
			      client->input->pointer->serial,
			      back_cursor_surface->wl_surface, 0, 0);
	match = verify_screen_content(client, "pointer_cursor_reenter", 1,
				      NULL, 1, NULL);
	assert(match);

	/* And back in the main surface again. */
	send_motion(client, &t3, 149, 149);
	wl_pointer_set_cursor(client->input->pointer->wl_pointer,
			      client->input->pointer->serial,
			      main_cursor_surface->wl_surface, 0, 0);
	match = verify_screen_content(client, "pointer_cursor_reenter", 2,
				      NULL, 2, NULL);
	assert(match);

	surface_destroy(back_cursor_surface);
	surface_destroy(main_cursor_surface);
	surface_destroy(back_surface);
	/* main_surface is destroyed when destroying the client. */
	client_destroy(client);
}