2020-05-04 17:22:35 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Canonical, Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Marco Trevisan <marco.trevisan@canonical.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "orientation-manager-unit-tests.h"
|
|
|
|
|
|
|
|
#include "tests/meta-sensors-proxy-mock.h"
|
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
const char *
|
|
|
|
orientation_to_string (MetaOrientation orientation)
|
2020-05-01 14:32:46 +00:00
|
|
|
{
|
2021-10-10 11:31:47 +00:00
|
|
|
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)";
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 14:32:46 +00:00
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
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++;
|
2020-05-01 14:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
on_max_wait_timeout (gpointer data)
|
|
|
|
{
|
2021-10-10 11:31:47 +00:00
|
|
|
WaitForOrientation *wfo = data;
|
2020-05-01 14:32:46 +00:00
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
wfo->timeout_id = 0;
|
2020-05-01 14:32:46 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
/*
|
|
|
|
* Assert that the orientation eventually changes to @orientation.
|
|
|
|
*/
|
2020-05-01 14:32:46 +00:00
|
|
|
void
|
2021-10-10 11:31:47 +00:00
|
|
|
wait_for_orientation (MetaOrientationManager *orientation_manager,
|
|
|
|
MetaOrientation orientation,
|
|
|
|
unsigned int *times_signalled_out)
|
2020-05-01 14:32:46 +00:00
|
|
|
{
|
2021-10-10 11:31:47 +00:00
|
|
|
WaitForOrientation wfo = {
|
|
|
|
.expected = orientation,
|
|
|
|
};
|
|
|
|
|
|
|
|
wfo.orientation = meta_orientation_manager_get_orientation (orientation_manager);
|
2020-05-01 14:32:46 +00:00
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
/* 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);
|
2020-05-01 14:32:46 +00:00
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
while (wfo.orientation != orientation && wfo.timeout_id != 0)
|
2020-05-01 14:32:46 +00:00
|
|
|
g_main_context_iteration (NULL, TRUE);
|
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
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_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);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
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;
|
2020-05-01 14:32:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 17:22:35 +00:00
|
|
|
static void
|
|
|
|
meta_test_orientation_manager_no_daemon (void)
|
|
|
|
{
|
|
|
|
g_autoptr (MetaOrientationManager) manager = NULL;
|
|
|
|
|
|
|
|
manager = g_object_new (META_TYPE_ORIENTATION_MANAGER, NULL);
|
|
|
|
g_assert_false (meta_orientation_manager_has_accelerometer (manager));
|
|
|
|
g_assert_cmpuint (meta_orientation_manager_get_orientation (manager),
|
|
|
|
==,
|
|
|
|
META_ORIENTATION_UNDEFINED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_test_orientation_manager_no_device (void)
|
|
|
|
{
|
|
|
|
g_autoptr (MetaOrientationManager) manager = NULL;
|
|
|
|
MetaSensorsProxyMock* orientation_mock = NULL;
|
|
|
|
|
|
|
|
orientation_mock = meta_sensors_proxy_mock_get ();
|
|
|
|
manager = g_object_new (META_TYPE_ORIENTATION_MANAGER, NULL);
|
|
|
|
g_assert_false (meta_orientation_manager_has_accelerometer (manager));
|
|
|
|
g_assert_cmpuint (meta_orientation_manager_get_orientation (manager),
|
|
|
|
==,
|
|
|
|
META_ORIENTATION_UNDEFINED);
|
|
|
|
|
|
|
|
g_object_unref (orientation_mock);
|
|
|
|
}
|
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
static gboolean
|
|
|
|
on_wait_for_accel_timeout (gpointer data)
|
|
|
|
{
|
|
|
|
guint *timeout_p = data;
|
|
|
|
|
|
|
|
*timeout_p = 0;
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:22:35 +00:00
|
|
|
static void
|
|
|
|
meta_test_orientation_manager_has_accelerometer (void)
|
|
|
|
{
|
|
|
|
g_autoptr (MetaOrientationManager) manager = NULL;
|
|
|
|
g_autoptr (MetaSensorsProxyMock) orientation_mock = NULL;
|
2021-10-10 11:31:47 +00:00
|
|
|
guint timeout_id;
|
2020-05-04 17:22:35 +00:00
|
|
|
|
|
|
|
manager = g_object_new (META_TYPE_ORIENTATION_MANAGER, NULL);
|
|
|
|
orientation_mock = meta_sensors_proxy_mock_get ();
|
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
timeout_id = g_timeout_add_seconds (10, on_wait_for_accel_timeout, &timeout_id);
|
2020-05-04 17:22:35 +00:00
|
|
|
meta_sensors_proxy_mock_set_property (orientation_mock,
|
|
|
|
"HasAccelerometer",
|
|
|
|
g_variant_new_boolean (TRUE));
|
2021-10-10 11:31:47 +00:00
|
|
|
|
|
|
|
while (!meta_orientation_manager_has_accelerometer (manager) &&
|
|
|
|
timeout_id != 0)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2020-05-04 17:22:35 +00:00
|
|
|
|
|
|
|
g_debug ("Checking whether accelerometer is present");
|
|
|
|
g_assert_true (meta_orientation_manager_has_accelerometer (manager));
|
|
|
|
g_assert_cmpuint (meta_orientation_manager_get_orientation (manager),
|
|
|
|
==,
|
|
|
|
META_ORIENTATION_UNDEFINED);
|
2021-10-10 11:31:47 +00:00
|
|
|
g_clear_handle_id (&timeout_id, g_source_remove);
|
2020-05-04 17:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
orientation_changed_cb (MetaOrientationManager *manager,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
gboolean *changed_called = user_data;
|
|
|
|
|
|
|
|
*changed_called = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_test_orientation_manager_accelerometer_orientations (void)
|
|
|
|
{
|
|
|
|
g_autoptr (MetaOrientationManager) manager = NULL;
|
|
|
|
g_autoptr (MetaSensorsProxyMock) orientation_mock = NULL;
|
|
|
|
|
|
|
|
manager = g_object_new (META_TYPE_ORIENTATION_MANAGER, NULL);
|
|
|
|
orientation_mock = meta_sensors_proxy_mock_get ();
|
|
|
|
|
|
|
|
MetaOrientation initial;
|
|
|
|
gboolean changed_called;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
g_signal_connect (manager, "orientation-changed",
|
|
|
|
G_CALLBACK (orientation_changed_cb),
|
|
|
|
&changed_called);
|
|
|
|
|
|
|
|
initial = meta_orientation_manager_get_orientation (manager);
|
|
|
|
|
|
|
|
for (i = initial + 1; i != initial; i = (i + 1) % META_N_ORIENTATIONS)
|
|
|
|
{
|
2021-10-10 11:31:47 +00:00
|
|
|
unsigned int times_signalled = 0;
|
2020-05-04 17:22:35 +00:00
|
|
|
|
2021-10-10 11:31:47 +00:00
|
|
|
changed_called = FALSE;
|
2020-05-04 17:22:35 +00:00
|
|
|
g_debug ("Checking orientation %d", i);
|
2021-10-10 11:31:47 +00:00
|
|
|
meta_sensors_proxy_mock_set_orientation (orientation_mock, i);
|
|
|
|
wait_for_orientation (manager, i, ×_signalled);
|
|
|
|
g_assert_cmpuint (times_signalled, <=, 1);
|
2020-05-04 17:22:35 +00:00
|
|
|
|
|
|
|
if (i != META_ORIENTATION_UNDEFINED)
|
|
|
|
g_assert_true (changed_called);
|
|
|
|
else
|
|
|
|
g_assert_false (changed_called);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init_orientation_manager_tests (void)
|
|
|
|
{
|
|
|
|
g_test_add_func ("/backends/orientation-manager/no-daemon",
|
|
|
|
meta_test_orientation_manager_no_daemon);
|
|
|
|
g_test_add_func ("/backends/orientation-manager/no-device",
|
|
|
|
meta_test_orientation_manager_no_device);
|
|
|
|
g_test_add_func ("/backends/orientation-manager/has-accelerometer",
|
|
|
|
meta_test_orientation_manager_has_accelerometer);
|
|
|
|
g_test_add_func ("/backends/orientation-manager/accelerometer-orientations",
|
|
|
|
meta_test_orientation_manager_accelerometer_orientations);
|
|
|
|
}
|