tests: move viewport creation into helpers
authorPekka Paalanen <pekka.paalanen@collabora.com>
Wed, 11 Mar 2020 15:11:03 +0000 (17:11 +0200)
committerPekka Paalanen <pekka.paalanen@collabora.com>
Wed, 11 Mar 2020 15:11:03 +0000 (17:11 +0200)
There will be a new test program using viewports and would like to share this
bit of code.

There are two behavioral changes:
- Compositor wp_viewporter interface version is no longer checked.
- client_create_viewport() does not leak the viewporter object.

test_viewporter_double_create needs to call bind_to_singleton_global() itself
so that the viewporter object still exists when the error event arrives.
Otherwise error verification fails.

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
tests/meson.build
tests/viewporter-test.c
tests/weston-test-client-helper.c
tests/weston-test-client-helper.h

index 463d712..714903f 100644 (file)
@@ -30,6 +30,8 @@ lib_test_client = static_library(
                'weston-test-fixture-compositor.c',
                weston_test_client_protocol_h,
                weston_test_protocol_c,
+               viewporter_client_protocol_h,
+               viewporter_protocol_c,
        ],
        include_directories: common_inc,
        dependencies: [
@@ -43,6 +45,9 @@ lib_test_client = static_library(
 )
 dep_test_client = declare_dependency(
        link_with: lib_test_client,
+       sources: [
+               viewporter_client_protocol_h,
+       ],
        dependencies: [
                dep_wayland_client,
                dep_test_runner,
@@ -179,14 +184,7 @@ tests = [
                'name': 'vertex-clip',
                'dep_objs': dep_vertex_clipping,
        },
-       {
-               'name': 'viewporter',
-               'sources': [
-                       'viewporter-test.c',
-                       viewporter_client_protocol_h,
-                       viewporter_protocol_c,
-               ],
-       },
+       {       'name': 'viewporter', },
 ]
 
 tests_standalone = [
index ad4f377..68e9364 100644 (file)
@@ -34,7 +34,6 @@
 #include "shared/helpers.h"
 #include "shared/xalloc.h"
 #include "weston-test-client-helper.h"
-#include "viewporter-client-protocol.h"
 #include "weston-test-fixture-compositor.h"
 
 static enum test_result_code
@@ -48,48 +47,6 @@ fixture_setup(struct weston_test_harness *harness)
 }
 DECLARE_FIXTURE_SETUP(fixture_setup);
 
-static struct wp_viewporter *
-get_viewporter(struct client *client)
-{
-       struct global *g;
-       struct global *global_wpr = NULL;
-       struct wp_viewporter *wpr;
-
-       wl_list_for_each(g, &client->global_list, link) {
-               if (strcmp(g->interface, wp_viewporter_interface.name))
-                       continue;
-
-               if (global_wpr)
-                       assert(0 && "multiple wp_viewporter objects");
-
-               global_wpr = g;
-       }
-
-       assert(global_wpr && "no wp_viewporter found");
-
-       assert(global_wpr->version == 1);
-
-       wpr = wl_registry_bind(client->wl_registry, global_wpr->name,
-                              &wp_viewporter_interface, 1);
-       assert(wpr);
-
-       return wpr;
-}
-
-static struct wp_viewport *
-create_viewport(struct client *client)
-{
-       struct wp_viewporter *viewporter;
-       struct wp_viewport *viewport;
-
-       viewporter = get_viewporter(client);
-       viewport = wp_viewporter_get_viewport(viewporter,
-                                             client->surface->wl_surface);
-       assert(viewport);
-
-       return viewport;
-}
-
 static void
 set_source(struct wp_viewport *vp, int x, int y, int w, int h)
 {
@@ -104,7 +61,8 @@ TEST(test_viewporter_double_create)
 
        client = create_client_and_test_surface(100, 50, 123, 77);
 
-       viewporter = get_viewporter(client);
+       viewporter = bind_to_singleton_global(client,
+                                             &wp_viewporter_interface, 1);
        wp_viewporter_get_viewport(viewporter, client->surface->wl_surface);
        wp_viewporter_get_viewport(viewporter, client->surface->wl_surface);
 
@@ -135,7 +93,7 @@ TEST_P(test_viewporter_bad_source_rect, bad_source_rect_args)
 
        client = create_client_and_test_surface(100, 50, 123, 77);
 
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
 
        testlog("wp_viewport.set_source x=%d, y=%d, w=%d, h=%d\n",
                args->x, args->y, args->w, args->h);
@@ -152,7 +110,7 @@ TEST(test_viewporter_unset_source_rect)
 
        client = create_client_and_test_surface(100, 50, 123, 77);
 
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
        set_source(vp, -1, -1, -1, -1);
        wl_surface_commit(client->surface->wl_surface);
 
@@ -180,7 +138,7 @@ TEST_P(test_viewporter_bad_destination_size, bad_destination_args)
 
        client = create_client_and_test_surface(100, 50, 123, 77);
 
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
 
        testlog("wp_viewport.set_destination w=%d, h=%d\n", args->w, args->h);
        wp_viewport_set_destination(vp, args->w, args->h);
@@ -196,7 +154,7 @@ TEST(test_viewporter_unset_destination_size)
 
        client = create_client_and_test_surface(100, 50, 123, 77);
 
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
        wp_viewport_set_destination(vp, -1, -1);
        wl_surface_commit(client->surface->wl_surface);
 
@@ -225,7 +183,7 @@ TEST_P(test_viewporter_non_integer_destination_size, nonint_destination_args)
 
        client = create_client_and_test_surface(100, 50, 123, 77);
 
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
 
        testlog("non-integer size w=%f, h=%f\n",
                wl_fixed_to_double(args->w), wl_fixed_to_double(args->h));
@@ -294,7 +252,7 @@ setup_source_vs_buffer(struct client *client,
        struct wp_viewport *vp;
 
        surf = client->surface->wl_surface;
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
 
        testlog("surface %dx%d\n",
                get_surface_width(client->surface,
@@ -487,7 +445,7 @@ TEST(test_viewporter_outside_null_buffer)
        surf = client->surface->wl_surface;
 
        /* If buffer is NULL, does not matter what the source rect is. */
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
        wl_surface_attach(surf, NULL, 0, 0);
        set_source(vp, 1000, 1000, 20, 10);
        wp_viewport_set_destination(vp, 99, 99);
@@ -516,7 +474,7 @@ TEST(test_viewporter_no_surface_set_source)
        struct wp_viewport *vp;
 
        client = create_client_and_test_surface(100, 50, 123, 77);
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
        wl_surface_destroy(client->surface->wl_surface);
        client->surface->wl_surface = NULL;
 
@@ -533,7 +491,7 @@ TEST(test_viewporter_no_surface_set_destination)
        struct wp_viewport *vp;
 
        client = create_client_and_test_surface(100, 50, 123, 77);
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
        wl_surface_destroy(client->surface->wl_surface);
        client->surface->wl_surface = NULL;
 
@@ -550,7 +508,7 @@ TEST(test_viewporter_no_surface_destroy)
        struct wp_viewport *vp;
 
        client = create_client_and_test_surface(100, 50, 123, 77);
-       vp = create_viewport(client);
+       vp = client_create_viewport(client);
        wl_surface_destroy(client->surface->wl_surface);
        client->surface->wl_surface = NULL;
 
index f5f2402..8711cf0 100644 (file)
@@ -1788,3 +1788,63 @@ client_buffer_from_image_file(struct client *client,
 
        return buf;
 }
+
+/**
+ * Bind to a singleton global in wl_registry
+ *
+ * \param client Client whose registry and globals to use.
+ * \param iface The Wayland interface to look for.
+ * \param version The version to bind the interface with.
+ * \return A struct wl_proxy, which you need to cast to the proper type.
+ *
+ * Asserts that the global being searched for is a singleton and is found.
+ *
+ * Binds with the exact version given, does not take compositor interface
+ * version into account.
+ */
+void *
+bind_to_singleton_global(struct client *client,
+                        const struct wl_interface *iface,
+                        int version)
+{
+       struct global *tmp;
+       struct global *g = NULL;
+       struct wl_proxy *proxy;
+
+       wl_list_for_each(tmp, &client->global_list, link) {
+               if (strcmp(tmp->interface, iface->name))
+                       continue;
+
+               assert(!g && "multiple singleton objects");
+               g = tmp;
+       }
+
+       assert(g && "singleton not found");
+
+       proxy = wl_registry_bind(client->wl_registry, g->name, iface, version);
+       assert(proxy);
+
+       return proxy;
+}
+
+/**
+ * Create a wp_viewport for the client surface
+ *
+ * \param client The client->surface to use.
+ * \return A fresh viewport object.
+ */
+struct wp_viewport *
+client_create_viewport(struct client *client)
+{
+       struct wp_viewporter *viewporter;
+       struct wp_viewport *viewport;
+
+       viewporter = bind_to_singleton_global(client,
+                                             &wp_viewporter_interface, 1);
+       viewport = wp_viewporter_get_viewport(viewporter,
+                                             client->surface->wl_surface);
+       assert(viewport);
+       wp_viewporter_destroy(viewporter);
+
+       return viewport;
+}
index a825313..417c62e 100644 (file)
@@ -37,6 +37,7 @@
 #include <wayland-client-protocol.h>
 #include "weston-test-runner.h"
 #include "weston-test-client-protocol.h"
+#include "viewporter-client-protocol.h"
 
 struct client {
        struct wl_display *wl_display;
@@ -269,4 +270,12 @@ client_buffer_from_image_file(struct client *client,
                              const char *basename,
                              int scale);
 
+void *
+bind_to_singleton_global(struct client *client,
+                        const struct wl_interface *iface,
+                        int version);
+
+struct wp_viewport *
+client_create_viewport(struct client *client);
+
 #endif