From 476ef76de669c49f93a9d9d9f5da3585f0d05333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Thu, 2 Apr 2020 16:11:09 +0200 Subject: [PATCH] tests/test-runner: Add 'assert_size' command The 'assert_size' command checks that the size of the window, both client side and compositor side, corresponds to an expected size set by the test case. The size comparison can only be done when the window is using 'csd', in order for both the client and server to have the same amount of understanding of the title bar. For ssd, the client cannot know how large the title bar, thus cannot verify the full window size. Sizes can be specified to mean the size of the monitor divided by a number. This is that one can make sure a window is maximized or fullscreened correctly. https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1171 --- src/backends/meta-logical-monitor.h | 1 + src/core/window-private.h | 1 + src/tests/test-client.c | 30 +++++++ src/tests/test-runner.c | 117 ++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) diff --git a/src/backends/meta-logical-monitor.h b/src/backends/meta-logical-monitor.h index 8cddc6b05..424390d17 100644 --- a/src/backends/meta-logical-monitor.h +++ b/src/backends/meta-logical-monitor.h @@ -90,6 +90,7 @@ float meta_logical_monitor_get_scale (MetaLogicalMonitor *logical_monitor); MetaMonitorTransform meta_logical_monitor_get_transform (MetaLogicalMonitor *logical_monitor); +META_EXPORT_TEST MetaRectangle meta_logical_monitor_get_layout (MetaLogicalMonitor *logical_monitor); META_EXPORT_TEST diff --git a/src/core/window-private.h b/src/core/window-private.h index 9afe2085c..8af1bb3fa 100644 --- a/src/core/window-private.h +++ b/src/core/window-private.h @@ -830,6 +830,7 @@ void meta_window_activate_full (MetaWindow *window, MetaClientType source_indication, MetaWorkspace *workspace); +META_EXPORT_TEST MetaLogicalMonitor * meta_window_calculate_main_logical_monitor (MetaWindow *window); MetaLogicalMonitor * meta_window_get_main_logical_monitor (MetaWindow *window); diff --git a/src/tests/test-client.c b/src/tests/test-client.c index 8b37a4225..618161ac5 100644 --- a/src/tests/test-client.c +++ b/src/tests/test-client.c @@ -733,6 +733,36 @@ process_line (const char *line) gtk_window_unmaximize (GTK_WINDOW (window)); } + else if (strcmp (argv[0], "assert_size") == 0) + { + int expected_width; + int expected_height; + int width; + int height; + + if (argc != 4) + { + g_print ("usage: assert_size \n"); + goto out; + } + + GtkWidget *window = lookup_window (argv[1]); + if (!window) + goto out; + + gtk_window_get_size (GTK_WINDOW (window), &width, &height); + height += calculate_titlebar_height (GTK_WINDOW (window)); + + expected_width = atoi (argv[2]); + expected_height = atoi (argv[3]); + if (expected_width != width || expected_height != height) + { + g_print ("Expected size %dx%d didn't match actual size %dx%d\n", + expected_width, expected_height, + width, height); + goto out; + } + } else { g_print ("Unknown command %s\n", argv[0]); diff --git a/src/tests/test-runner.c b/src/tests/test-runner.c index 98292d5d8..b6ff931b2 100644 --- a/src/tests/test-runner.c +++ b/src/tests/test-runner.c @@ -291,6 +291,30 @@ test_case_assert_focused (TestCase *test, return *error == NULL; } +static gboolean +test_case_assert_size (TestCase *test, + MetaWindow *window, + int expected_width, + int expected_height, + GError **error) +{ + MetaRectangle frame_rect; + + meta_window_get_frame_rect (window, &frame_rect); + + if (frame_rect.width != expected_width || + frame_rect.height != expected_height) + { + g_set_error (error, TEST_RUNNER_ERROR, TEST_RUNNER_ERROR_ASSERTION_FAILED, + "Expected size %dx%d didn't match actual size %dx%d", + expected_width, expected_height, + frame_rect.width, frame_rect.height); + return FALSE; + } + + return TRUE; +} + static gboolean test_case_check_xserver_stacking (TestCase *test, GError **error) @@ -344,6 +368,56 @@ test_case_check_xserver_stacking (TestCase *test, return *error == NULL; } +static int +maybe_divide (const char *str, + int value) +{ + if (strstr (str, "/") == str) + { + int divisor; + + str += 1; + divisor = atoi (str); + + value /= divisor; + } + + return value; +} + +static int +parse_window_size (MetaWindow *window, + const char *size_str) +{ + MetaLogicalMonitor *logical_monitor; + MetaRectangle logical_monitor_layout; + int value; + + logical_monitor = meta_window_calculate_main_logical_monitor (window); + g_assert_nonnull (logical_monitor); + + logical_monitor_layout = meta_logical_monitor_get_layout (logical_monitor); + + if (strstr (size_str, "MONITOR_WIDTH") == size_str) + { + value = logical_monitor_layout.width; + size_str += strlen ("MONITOR_WIDTH"); + value = maybe_divide (size_str, value); + } + else if (strstr (size_str, "MONITOR_HEIGHT") == size_str) + { + value = logical_monitor_layout.height; + size_str += strlen ("MONITOR_HEIGHT"); + value = maybe_divide (size_str, value); + } + else + { + value = atoi (size_str); + } + + return value; +} + static gboolean test_case_do (TestCase *test, int argc, @@ -598,6 +672,49 @@ test_case_do (TestCase *test, if (!test_case_assert_focused (test, argv[1], error)) return FALSE; } + else if (strcmp (argv[0], "assert_size") == 0) + { + if (argc != 4) + { + BAD_COMMAND("usage: %s / ", + argv[0]); + } + + TestClient *client; + const char *window_id; + if (!test_case_parse_window_id (test, argv[1], &client, &window_id, error)) + return FALSE; + + MetaWindow *window = test_client_find_window (client, window_id, error); + if (!window) + return FALSE; + + if (meta_window_get_frame (window)) + { + g_set_error (error, + TEST_RUNNER_ERROR, + TEST_RUNNER_ERROR_ASSERTION_FAILED, + "Can only assert size of CSD window"); + return FALSE; + } + + int width = parse_window_size (window, argv[2]); + int height = parse_window_size (window, argv[3]); + g_autofree char *width_str = g_strdup_printf ("%d", width); + g_autofree char *height_str = g_strdup_printf ("%d", height); + + if (!test_client_do (client, error, argv[0], + window_id, + width_str, + height_str, + NULL)) + return FALSE; + + if (!test_case_assert_size (test, window, + width, height, + error)) + return FALSE; + } else { BAD_COMMAND("Unknown command %s", argv[0]);