From 7637627e018391904b501fa5fcad25756ce3e71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Thu, 22 Aug 2024 15:06:20 +0200 Subject: [PATCH] tests/test-runner: Add 'wait_focused' command This aims to replace a reoccuring arbitrary sleep followed by a focus assertion. The problem with these is that the sleep does not reliably wait for long enough, due to arbitrary system resource usage in CI and elsewhere. By instead waiting, and instead asserting we don't have any intermediate wrong focus, we remove the race, and test for more invalid situations. Part-of: --- src/tests/test-runner.c | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/tests/test-runner.c b/src/tests/test-runner.c index 3513f76c5..716cb3e2c 100644 --- a/src/tests/test-runner.c +++ b/src/tests/test-runner.c @@ -1400,6 +1400,86 @@ test_case_do (TestCase *test, if (!test_case_assert_focused (test, argv[1], error)) return FALSE; } + else if (strcmp (argv[0], "wait_focused") == 0) + { + MetaDisplay *display = meta_context_get_display (test->context); + MetaWindow *old_focus; + const char *expected_window; + + if (argc != 2) + BAD_COMMAND ("usage: %s /|none", argv[0]); + + expected_window = argv[1]; + old_focus = display->focus_window; + + if (g_strcmp0 (expected_window, "none") == 0) + { + while (TRUE) + { + if (display->focus_window && + display->focus_window != old_focus) + { + const char *focused = display->focus_window->title; + + if (g_str_has_prefix (focused, "test/")) + focused += 5; + + g_set_error (error, + META_TEST_CLIENT_ERROR, + META_TEST_CLIENT_ERROR_ASSERTION_FAILED, + "focus: expected='none', actual='%s'", + focused); + return FALSE; + } + else if (!display->focus_window) + { + break; + } + + g_main_context_iteration (NULL, TRUE); + } + } + else + { + while (TRUE) + { + if (display->focus_window != old_focus && + !display->focus_window) + { + g_set_error (error, + META_TEST_CLIENT_ERROR, + META_TEST_CLIENT_ERROR_ASSERTION_FAILED, + "focus: expected='%s', actual='none'", + expected_window); + return FALSE; + } + else if (display->focus_window) + { + const char *focused; + + focused = display->focus_window->title; + if (g_str_has_prefix (focused, "test/")) + focused += 5; + + if (g_strcmp0 (focused, expected_window) == 0) + { + break; + } + else if (old_focus != display->focus_window) + { + g_set_error (error, + META_TEST_CLIENT_ERROR, + META_TEST_CLIENT_ERROR_ASSERTION_FAILED, + "focus: expected='%s', actual='%s'", + expected_window, focused); + return FALSE; + } + } + + g_main_context_iteration (NULL, TRUE); + } + } + } else if (strcmp (argv[0], "assert_size") == 0) { MetaWindow *window;