tests: Move orientation helper to monitor test utils

It was used by both the orientation tests, and the monitor configuration
tests. Lets move the helper to common ground.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2030>
This commit is contained in:
Jonas Ådahl 2021-09-30 14:47:11 +02:00 committed by Marge Bot
parent 20686f3354
commit 67425bf653
5 changed files with 195 additions and 186 deletions

View File

@ -874,3 +874,149 @@ void check_monitor_scales (MonitorTestCaseExpect *expect,
}
}
}
const char *
meta_orientation_to_string (MetaOrientation orientation)
{
switch (orientation)
{
case META_ORIENTATION_UNDEFINED:
return "(undefined)";
case META_ORIENTATION_NORMAL:
return "normal";
case META_ORIENTATION_BOTTOM_UP:
return "bottom-up";
case META_ORIENTATION_LEFT_UP:
return "left-up";
case META_ORIENTATION_RIGHT_UP:
return "right-up";
default:
return "(invalid)";
}
}
typedef struct
{
MetaOrientation expected;
MetaOrientation orientation;
gulong connection_id;
guint timeout_id;
unsigned int times_signalled;
} WaitForOrientation;
static void
on_orientation_changed (WaitForOrientation *wfo,
MetaOrientationManager *orientation_manager)
{
wfo->orientation = meta_orientation_manager_get_orientation (orientation_manager);
wfo->times_signalled++;
g_test_message ("wait_for_orientation_changes: Orientation changed to %d: %s",
wfo->orientation, meta_orientation_to_string (wfo->orientation));
}
static gboolean
on_max_wait_timeout (gpointer data)
{
WaitForOrientation *wfo = data;
wfo->timeout_id = 0;
return G_SOURCE_REMOVE;
}
/*
* Assert that the orientation eventually changes to @orientation.
*/
void
meta_wait_for_orientation (MetaOrientationManager *orientation_manager,
MetaOrientation orientation,
unsigned int *times_signalled_out)
{
WaitForOrientation wfo = {
.expected = orientation,
};
wfo.orientation = meta_orientation_manager_get_orientation (orientation_manager);
g_test_message ("%s: Waiting for orientation to change from "
"%d: %s to %d: %s...",
G_STRFUNC, wfo.orientation,
meta_orientation_to_string (wfo.orientation),
orientation, meta_orientation_to_string (orientation));
/* This timeout can be relatively generous because we don't expect to
* reach it: if we do, that's a test failure. */
wfo.timeout_id = g_timeout_add_seconds (10, on_max_wait_timeout, &wfo);
wfo.connection_id = g_signal_connect_swapped (orientation_manager,
"orientation-changed",
G_CALLBACK (on_orientation_changed),
&wfo);
while (wfo.orientation != orientation && wfo.timeout_id != 0)
g_main_context_iteration (NULL, TRUE);
if (wfo.orientation != orientation)
{
g_error ("Timed out waiting for orientation to change from %s to %s "
"(received %u orientation-changed signal(s) while waiting)",
meta_orientation_to_string (wfo.orientation),
meta_orientation_to_string (orientation),
wfo.times_signalled);
}
g_test_message ("%s: Orientation is now %d: %s",
G_STRFUNC, orientation,
meta_orientation_to_string (orientation));
g_clear_handle_id (&wfo.timeout_id, g_source_remove);
g_signal_handler_disconnect (orientation_manager, wfo.connection_id);
if (times_signalled_out != NULL)
*times_signalled_out = wfo.times_signalled;
}
/*
* Wait for a possible orientation change, but don't assert that one occurs.
*/
void
meta_wait_for_possible_orientation_change (MetaOrientationManager *orientation_manager,
unsigned int *times_signalled_out)
{
WaitForOrientation wfo = {
.expected = META_ORIENTATION_UNDEFINED,
};
wfo.orientation = meta_orientation_manager_get_orientation (orientation_manager);
g_test_message ("%s: Waiting for orientation to maybe change from %d: %s...",
G_STRFUNC, wfo.orientation,
meta_orientation_to_string (wfo.orientation));
/* This can't be as long as the timeout for meta_wait_for_orientation(),
* because in the usual case we expect to reach this timeout: we're
* only waiting so that if the orientation (incorrectly?) changed here,
* we'd have a chance to detect that. */
wfo.timeout_id = g_timeout_add (1000, on_max_wait_timeout, &wfo);
wfo.connection_id = g_signal_connect_swapped (orientation_manager,
"orientation-changed",
G_CALLBACK (on_orientation_changed),
&wfo);
while (wfo.times_signalled == 0 && wfo.timeout_id != 0)
g_main_context_iteration (NULL, TRUE);
if (wfo.timeout_id == 0)
{
g_test_message ("%s: Orientation didn't change", G_STRFUNC);
}
else
{
g_test_message ("%s: Orientation is now %d: %s",
G_STRFUNC, wfo.orientation,
meta_orientation_to_string (wfo.orientation));
}
g_clear_handle_id (&wfo.timeout_id, g_source_remove);
g_signal_handler_disconnect (orientation_manager, wfo.connection_id);
if (times_signalled_out != NULL)
*times_signalled_out = wfo.times_signalled;
}

View File

@ -212,4 +212,13 @@ void check_monitor_scales (MonitorTestCaseExpect *expect,
MetaMonitorTestSetup * create_monitor_test_setup (MonitorTestCaseSetup *setup,
MonitorTestFlag flags);
const char * meta_orientation_to_string (MetaOrientation orientation);
void meta_wait_for_orientation (MetaOrientationManager *orientation_manager,
MetaOrientation orientation,
unsigned int *times_signalled_out);
void meta_wait_for_possible_orientation_change (MetaOrientationManager *orientation_manager,
unsigned int *times_signalled_out);
#endif /* MONITOR_TEST_UTILS_H */

View File

@ -36,7 +36,6 @@
#include "tests/monitor-test-utils.h"
#include "tests/meta-test-utils.h"
#include "tests/unit-tests.h"
#include "tests/orientation-manager-unit-tests.h"
#include "x11/meta-x11-display-private.h"
static MonitorTestCase initial_test_case = {
@ -3564,7 +3563,7 @@ meta_sensors_proxy_reset (MetaSensorsProxyMock *proxy)
g_test_message ("Resetting proxy");
meta_sensors_proxy_mock_set_orientation (proxy,
META_ORIENTATION_NORMAL);
wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL, NULL);
meta_wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL, NULL);
g_object_unref (proxy);
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MetaSensorsProxyAutoResetMock,
@ -3889,7 +3888,8 @@ meta_test_monitor_orientation_initial_rotated (void)
touch_device = meta_test_add_touch_device (backend);
orientation = META_ORIENTATION_LEFT_UP;
meta_sensors_proxy_mock_set_orientation (orientation_mock, orientation);
wait_for_orientation (orientation_manager, orientation, &times_signalled);
meta_wait_for_orientation (orientation_manager, orientation,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
test_setup = create_monitor_test_setup (&test_case.setup,
@ -3995,7 +3995,8 @@ meta_test_monitor_orientation_initial_rotated_no_touch_mode (void)
orientation_mock = meta_sensors_proxy_mock_get ();
orientation = META_ORIENTATION_LEFT_UP;
meta_sensors_proxy_mock_set_orientation (orientation_mock, orientation);
wait_for_orientation (orientation_manager, orientation, &times_signalled);
meta_wait_for_orientation (orientation_manager, orientation,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
test_setup = create_monitor_test_setup (&test_case.setup,
@ -4111,7 +4112,8 @@ meta_test_monitor_orientation_initial_stored_rotated (void)
touch_device = meta_test_add_touch_device (backend);
orientation = META_ORIENTATION_RIGHT_UP;
meta_sensors_proxy_mock_set_orientation (orientation_mock, orientation);
wait_for_orientation (orientation_manager, orientation, &times_signalled);
meta_wait_for_orientation (orientation_manager, orientation,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
test_setup = create_monitor_test_setup (&test_case.setup,
@ -4136,7 +4138,8 @@ meta_test_monitor_orientation_initial_stored_rotated (void)
g_test_message ("Rotating to left-up");
orientation = META_ORIENTATION_LEFT_UP;
meta_sensors_proxy_mock_set_orientation (orientation_mock, orientation);
wait_for_orientation (orientation_manager, orientation, &times_signalled);
meta_wait_for_orientation (orientation_manager, orientation,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
meta_backend_test_set_is_lid_closed (META_BACKEND_TEST (backend), FALSE);
@ -4154,7 +4157,8 @@ meta_test_monitor_orientation_initial_stored_rotated (void)
g_test_message ("Rotating to right-up");
orientation = META_ORIENTATION_RIGHT_UP;
meta_sensors_proxy_mock_set_orientation (orientation_mock, orientation);
wait_for_orientation (orientation_manager, orientation, &times_signalled);
meta_wait_for_orientation (orientation_manager, orientation,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
META_TEST_LOG_CALL ("Checking configuration per orientation",
@ -4265,7 +4269,8 @@ meta_test_monitor_orientation_initial_stored_rotated_no_touch (void)
orientation_mock = meta_sensors_proxy_mock_get ();
orientation = META_ORIENTATION_RIGHT_UP;
meta_sensors_proxy_mock_set_orientation (orientation_mock, orientation);
wait_for_orientation (orientation_manager, orientation, &times_signalled);
meta_wait_for_orientation (orientation_manager, orientation,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
test_setup = create_monitor_test_setup (&test_case.setup,
@ -4414,7 +4419,7 @@ meta_test_monitor_orientation_changes (void)
got_monitors_changed = FALSE;
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (orientation_manager, i, &times_signalled);
meta_wait_for_orientation (orientation_manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
META_TEST_LOG_CALL ("Checking configuration per orientation",
@ -4443,8 +4448,8 @@ meta_test_monitor_orientation_changes (void)
got_monitors_changed = FALSE;
meta_sensors_proxy_mock_set_orientation (orientation_mock,
META_ORIENTATION_NORMAL);
wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
meta_wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
g_assert_cmpuint (times_signalled, ==, 0);
META_TEST_LOG_CALL ("Checking configuration per orientation",
check_monitor_configuration_per_orientation (
@ -4466,7 +4471,7 @@ meta_test_monitor_orientation_changes (void)
got_monitors_changed = FALSE;
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (orientation_manager, i, &times_signalled);
meta_wait_for_orientation (orientation_manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
META_TEST_LOG_CALL ("Checking configuration per orientation",
@ -4610,7 +4615,7 @@ meta_test_monitor_orientation_changes_for_transformed_panel (void)
got_monitors_changed = FALSE;
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (orientation_manager, i, &times_signalled);
meta_wait_for_orientation (orientation_manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
META_TEST_LOG_CALL ("Checking configuration per orientation",
@ -4639,8 +4644,8 @@ meta_test_monitor_orientation_changes_for_transformed_panel (void)
got_monitors_changed = FALSE;
meta_sensors_proxy_mock_set_orientation (orientation_mock,
META_ORIENTATION_NORMAL);
wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
meta_wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
g_assert_cmpuint (times_signalled, ==, 0);
META_TEST_LOG_CALL ("Checking configuration per orientation",
check_monitor_configuration_per_orientation (
@ -4662,7 +4667,7 @@ meta_test_monitor_orientation_changes_for_transformed_panel (void)
got_monitors_changed = FALSE;
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (orientation_manager, i, &times_signalled);
meta_wait_for_orientation (orientation_manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
META_TEST_LOG_CALL ("Checking configuration per orientation",
@ -4687,9 +4692,9 @@ meta_test_monitor_orientation_changes_for_transformed_panel (void)
got_monitors_changed = FALSE;
meta_sensors_proxy_mock_set_orientation (orientation_mock,
META_ORIENTATION_RIGHT_UP);
wait_for_orientation (orientation_manager,
META_ORIENTATION_RIGHT_UP,
&times_signalled);
meta_wait_for_orientation (orientation_manager,
META_ORIENTATION_RIGHT_UP,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
META_TEST_LOG_CALL ("Checking configuration per orientation",
check_monitor_configuration_per_orientation (
@ -4859,7 +4864,7 @@ meta_test_monitor_orientation_changes_with_hotplugging (void)
for (i = META_N_ORIENTATIONS - 1; i > META_ORIENTATION_UNDEFINED; i--)
{
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (orientation_manager, i, &times_signalled);
meta_wait_for_orientation (orientation_manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
META_TEST_LOG_CALL ("Checking configuration per orientation",
@ -4869,8 +4874,8 @@ meta_test_monitor_orientation_changes_with_hotplugging (void)
meta_sensors_proxy_mock_set_orientation (orientation_mock,
META_ORIENTATION_NORMAL);
wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
meta_wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
check_monitor_configuration (&test_case.expect);
@ -4892,7 +4897,7 @@ meta_test_monitor_orientation_changes_with_hotplugging (void)
for (i = META_N_ORIENTATIONS - 1; i > META_ORIENTATION_UNDEFINED; i--)
{
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (orientation_manager, i, &times_signalled);
meta_wait_for_orientation (orientation_manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
META_TEST_LOG_CALL ("Checking configuration per orientation",
@ -4902,8 +4907,8 @@ meta_test_monitor_orientation_changes_with_hotplugging (void)
meta_sensors_proxy_mock_set_orientation (orientation_mock,
META_ORIENTATION_NORMAL);
wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
meta_wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
check_monitor_configuration (&test_case.expect);
@ -4924,15 +4929,15 @@ meta_test_monitor_orientation_changes_with_hotplugging (void)
for (i = META_N_ORIENTATIONS - 1; i > META_ORIENTATION_UNDEFINED; i--)
{
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (orientation_manager, i, &times_signalled);
meta_wait_for_orientation (orientation_manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
check_monitor_configuration (&test_case.expect);
}
meta_sensors_proxy_mock_set_orientation (orientation_mock,
META_ORIENTATION_NORMAL);
wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
meta_wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
/*
@ -4976,7 +4981,7 @@ meta_test_monitor_orientation_changes_with_hotplugging (void)
/* Change orientation */
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (orientation_manager, i, &times_signalled);
meta_wait_for_orientation (orientation_manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
check_monitor_configuration (&test_case.expect);
@ -4996,7 +5001,8 @@ meta_test_monitor_orientation_changes_with_hotplugging (void)
/* We don't actually expect the orientation to change here, so we
* just wait for a moment (so that if the orientation *did* change,
* mutter has had a chance to process it), and then continue. */
wait_for_possible_orientation_change (orientation_manager, &times_signalled);
meta_wait_for_possible_orientation_change (orientation_manager,
&times_signalled);
g_assert_cmpuint (times_signalled, ==, 0);
META_TEST_LOG_CALL ("Checking configuration per orientation",
@ -5035,8 +5041,8 @@ meta_test_monitor_orientation_changes_with_hotplugging (void)
meta_sensors_proxy_mock_set_orientation (orientation_mock,
META_ORIENTATION_NORMAL);
wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
meta_wait_for_orientation (orientation_manager, META_ORIENTATION_NORMAL,
&times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
}

View File

@ -23,151 +23,7 @@
#include "orientation-manager-unit-tests.h"
#include "tests/meta-sensors-proxy-mock.h"
const char *
orientation_to_string (MetaOrientation orientation)
{
switch (orientation)
{
case META_ORIENTATION_UNDEFINED:
return "(undefined)";
case META_ORIENTATION_NORMAL:
return "normal";
case META_ORIENTATION_BOTTOM_UP:
return "bottom-up";
case META_ORIENTATION_LEFT_UP:
return "left-up";
case META_ORIENTATION_RIGHT_UP:
return "right-up";
default:
return "(invalid)";
}
}
typedef struct
{
MetaOrientation expected;
MetaOrientation orientation;
gulong connection_id;
guint timeout_id;
unsigned int times_signalled;
} WaitForOrientation;
static void
on_orientation_changed (WaitForOrientation *wfo,
MetaOrientationManager *orientation_manager)
{
wfo->orientation = meta_orientation_manager_get_orientation (orientation_manager);
wfo->times_signalled++;
g_test_message ("wait_for_orientation_changes: Orientation changed to %d: %s",
wfo->orientation, orientation_to_string (wfo->orientation));
}
static gboolean
on_max_wait_timeout (gpointer data)
{
WaitForOrientation *wfo = data;
wfo->timeout_id = 0;
return G_SOURCE_REMOVE;
}
/*
* Assert that the orientation eventually changes to @orientation.
*/
void
wait_for_orientation (MetaOrientationManager *orientation_manager,
MetaOrientation orientation,
unsigned int *times_signalled_out)
{
WaitForOrientation wfo = {
.expected = orientation,
};
wfo.orientation = meta_orientation_manager_get_orientation (orientation_manager);
g_test_message ("%s: Waiting for orientation to change from "
"%d: %s to %d: %s...",
G_STRFUNC, wfo.orientation,
orientation_to_string (wfo.orientation),
orientation, orientation_to_string (orientation));
/* This timeout can be relatively generous because we don't expect to
* reach it: if we do, that's a test failure. */
wfo.timeout_id = g_timeout_add_seconds (10, on_max_wait_timeout, &wfo);
wfo.connection_id = g_signal_connect_swapped (orientation_manager,
"orientation-changed",
G_CALLBACK (on_orientation_changed),
&wfo);
while (wfo.orientation != orientation && wfo.timeout_id != 0)
g_main_context_iteration (NULL, TRUE);
if (wfo.orientation != orientation)
{
g_error ("Timed out waiting for orientation to change from %s to %s "
"(received %u orientation-changed signal(s) while waiting)",
orientation_to_string (wfo.orientation),
orientation_to_string (orientation),
wfo.times_signalled);
}
g_test_message ("%s: Orientation is now %d: %s",
G_STRFUNC, orientation, orientation_to_string (orientation));
g_clear_handle_id (&wfo.timeout_id, g_source_remove);
g_signal_handler_disconnect (orientation_manager, wfo.connection_id);
if (times_signalled_out != NULL)
*times_signalled_out = wfo.times_signalled;
}
/*
* Wait for a possible orientation change, but don't assert that one occurs.
*/
void
wait_for_possible_orientation_change (MetaOrientationManager *orientation_manager,
unsigned int *times_signalled_out)
{
WaitForOrientation wfo = {
.expected = META_ORIENTATION_UNDEFINED,
};
wfo.orientation = meta_orientation_manager_get_orientation (orientation_manager);
g_test_message ("%s: Waiting for orientation to maybe change from %d: %s...",
G_STRFUNC, wfo.orientation,
orientation_to_string (wfo.orientation));
/* This can't be as long as the timeout for wait_for_orientation(),
* because in the usual case we expect to reach this timeout: we're
* only waiting so that if the orientation (incorrectly?) changed here,
* we'd have a chance to detect that. */
wfo.timeout_id = g_timeout_add (1000, on_max_wait_timeout, &wfo);
wfo.connection_id = g_signal_connect_swapped (orientation_manager,
"orientation-changed",
G_CALLBACK (on_orientation_changed),
&wfo);
while (wfo.times_signalled == 0 && wfo.timeout_id != 0)
g_main_context_iteration (NULL, TRUE);
if (wfo.timeout_id == 0)
{
g_test_message ("%s: Orientation didn't change", G_STRFUNC);
}
else
{
g_test_message ("%s: Orientation is now %d: %s",
G_STRFUNC, wfo.orientation,
orientation_to_string (wfo.orientation));
}
g_clear_handle_id (&wfo.timeout_id, g_source_remove);
g_signal_handler_disconnect (orientation_manager, wfo.connection_id);
if (times_signalled_out != NULL)
*times_signalled_out = wfo.times_signalled;
}
#include "tests/monitor-test-utils.h"
static void
meta_test_orientation_manager_no_daemon (void)
@ -268,7 +124,7 @@ meta_test_orientation_manager_accelerometer_orientations (void)
changed_called = FALSE;
g_debug ("Checking orientation %d", i);
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
wait_for_orientation (manager, i, &times_signalled);
meta_wait_for_orientation (manager, i, &times_signalled);
g_assert_cmpuint (times_signalled, <=, 1);
if (i != META_ORIENTATION_UNDEFINED)

View File

@ -25,12 +25,4 @@
void init_orientation_manager_tests (void);
void wait_for_orientation (MetaOrientationManager *orientation_manager,
MetaOrientation orientation,
unsigned int *times_signalled_out);
void wait_for_possible_orientation_change (MetaOrientationManager *orientation_manager,
unsigned int *times_signalled_out);
const char * orientation_to_string (MetaOrientation orientation);
#endif /* ORIENTATION_MANAGER_UNIT_TESTS_H */