client: Fix segmentation fault in the case weston-nested
[platform/upstream/weston.git] / tests / roles-test.c
1 /*
2  * Copyright © 2014 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31
32 #include "weston-test-client-helper.h"
33
34 static struct wl_subcompositor *
35 get_subcompositor(struct client *client)
36 {
37         struct global *g;
38         struct global *global_sub = NULL;
39         struct wl_subcompositor *sub;
40
41         wl_list_for_each(g, &client->global_list, link) {
42                 if (strcmp(g->interface, "wl_subcompositor"))
43                         continue;
44
45                 if (global_sub)
46                         assert(0 && "multiple wl_subcompositor objects");
47
48                 global_sub = g;
49         }
50
51         assert(global_sub && "no wl_subcompositor found");
52
53         assert(global_sub->version == 1);
54
55         sub = wl_registry_bind(client->wl_registry, global_sub->name,
56                                &wl_subcompositor_interface, 1);
57         assert(sub);
58
59         return sub;
60 }
61
62 static struct wl_shell *
63 get_wl_shell(struct client *client)
64 {
65         struct global *g;
66         struct global *global = NULL;
67         struct wl_shell *shell;
68
69         wl_list_for_each(g, &client->global_list, link) {
70                 if (strcmp(g->interface, "wl_shell"))
71                         continue;
72
73                 if (global)
74                         assert(0 && "multiple wl_shell objects");
75
76                 global = g;
77         }
78
79         assert(global && "no wl_shell found");
80
81         shell = wl_registry_bind(client->wl_registry, global->name,
82                                  &wl_shell_interface, 1);
83         assert(shell);
84
85         return shell;
86 }
87
88 TEST(test_role_conflict_sub_wlshell)
89 {
90         struct client *client;
91         struct wl_subcompositor *subco;
92         struct wl_surface *child;
93         struct wl_subsurface *sub;
94         struct wl_shell *shell;
95         struct wl_shell_surface *shsurf;
96
97         client = create_client_and_test_surface(100, 50, 123, 77);
98         assert(client);
99
100         subco = get_subcompositor(client);
101         shell = get_wl_shell(client);
102
103         child = wl_compositor_create_surface(client->wl_compositor);
104         assert(child);
105         sub = wl_subcompositor_get_subsurface(subco, child,
106                                               client->surface->wl_surface);
107         assert(sub);
108
109         shsurf = wl_shell_get_shell_surface(shell, child);
110         assert(shsurf);
111
112         expect_protocol_error(client, &wl_shell_interface,
113                               WL_SHELL_ERROR_ROLE);
114 }
115
116 TEST(test_role_conflict_wlshell_sub)
117 {
118         struct client *client;
119         struct wl_subcompositor *subco;
120         struct wl_surface *child;
121         struct wl_subsurface *sub;
122         struct wl_shell *shell;
123         struct wl_shell_surface *shsurf;
124
125         client = create_client_and_test_surface(100, 50, 123, 77);
126         assert(client);
127
128         subco = get_subcompositor(client);
129         shell = get_wl_shell(client);
130
131         child = wl_compositor_create_surface(client->wl_compositor);
132         assert(child);
133         shsurf = wl_shell_get_shell_surface(shell, child);
134         assert(shsurf);
135
136         sub = wl_subcompositor_get_subsurface(subco, child,
137                                               client->surface->wl_surface);
138         assert(sub);
139
140         expect_protocol_error(client, &wl_subcompositor_interface,
141                               WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE);
142 }