1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/* -*- 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 "meta-sensors-proxy-mock.h"
#define SENSORS_MOCK_TEMPLATE "iio-sensors-proxy"
static MetaSensorsProxyMock *sensors_proxy_mock = NULL;
static const char *
orientation_to_string (MetaOrientation orientation)
{
const char *orientation_str = "undefined";
switch (orientation)
{
case META_ORIENTATION_UNDEFINED:
orientation_str = "undefined";
break;
case META_ORIENTATION_NORMAL:
orientation_str = "normal";
break;
case META_ORIENTATION_BOTTOM_UP:
orientation_str = "bottom-up";
break;
case META_ORIENTATION_LEFT_UP:
orientation_str = "left-up";
break;
case META_ORIENTATION_RIGHT_UP:
orientation_str = "right-up";
break;
}
return orientation_str;
}
static void
on_proxy_call_cb (GObject *object,
GAsyncResult *res,
gpointer user_data)
{
g_autoptr(GError) error = NULL;
GVariant **ret = user_data;
*ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (object), res, &error);
g_assert_no_error (error);
g_assert_nonnull (ret);
}
static GVariant *
get_internal_property_value (MetaSensorsProxyMock *proxy,
const char *property_name)
{
g_autoptr (GVariant) ret = NULL;
g_dbus_proxy_call (proxy, "GetInternalProperty",
g_variant_new ("(s)", property_name),
G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, NULL,
on_proxy_call_cb, &ret);
while (!ret)
g_main_context_iteration (NULL, TRUE);
return g_variant_get_child_value (ret, 0);
}
static void
ensure_property (MetaSensorsProxyMock *proxy,
const char *property_name,
GVariant *expected_value)
{
g_autoptr (GVariant) value = NULL;
g_autoptr (GVariant) expected = NULL;
gboolean equal_properties;
value = get_internal_property_value (proxy, property_name);
if (!g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT))
{
g_autoptr (GVariant) tmp = g_variant_ref (value);
value = g_variant_new ("v", tmp);
}
if (g_variant_is_of_type (expected_value, G_VARIANT_TYPE_VARIANT))
expected = g_variant_ref (expected_value);
else
expected = g_variant_new ("v", expected_value);
equal_properties = g_variant_equal (expected, value);
if (!equal_properties)
{
g_autofree char *actual_str = g_variant_print (value, TRUE);
g_autofree char *expected_str = g_variant_print (expected, TRUE);
g_debug ("Property: %s", property_name);
g_debug ("Expected: %s", expected_str);
g_debug ("Actual: %s", actual_str);
}
g_assert_true (equal_properties);
}
static void
stop_sensors_mock (GDBusConnection *connection)
{
g_autoptr (GVariant) ret = NULL;
g_autoptr (GError) error = NULL;
ret = g_dbus_connection_call_sync (connection,
"org.gnome.Mutter.TestDBusMocksManager",
"/org/gnome/Mutter/TestDBusMocksManager",
"org.gnome.Mutter.TestDBusMocksManager",
"StopLocalTemplate",
g_variant_new ("(s)", SENSORS_MOCK_TEMPLATE),
NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (ret);
}
static void
start_sensors_mock (void)
{
g_autoptr (GDBusConnection) connection = NULL;
g_autoptr (GError) error = NULL;
g_autoptr (GVariant) ret = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
g_assert_no_error (error);
ret = g_dbus_connection_call_sync (connection,
"org.gnome.Mutter.TestDBusMocksManager",
"/org/gnome/Mutter/TestDBusMocksManager",
"org.gnome.Mutter.TestDBusMocksManager",
"StartFromLocalTemplate",
g_variant_new ("(s)", SENSORS_MOCK_TEMPLATE),
NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (ret);
}
static void
on_proxy_removed (gpointer data)
{
g_autoptr (GDBusConnection) connection = data;
stop_sensors_mock (connection);
}
MetaSensorsProxyMock *
meta_sensors_proxy_mock_get (void)
{
GDBusProxy *proxy = NULL;
g_autoptr (GError) error = NULL;
if (sensors_proxy_mock)
return g_object_ref (sensors_proxy_mock);
start_sensors_mock ();
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
NULL,
"net.hadess.SensorProxy",
"/net/hadess/SensorProxy",
"org.freedesktop.DBus.Mock",
NULL, &error);
g_assert_true (G_IS_DBUS_PROXY (proxy));
g_assert_no_error (error);
sensors_proxy_mock = proxy;
g_object_add_weak_pointer (G_OBJECT (sensors_proxy_mock),
(gpointer *) &sensors_proxy_mock);
g_object_set_data_full (G_OBJECT (proxy), "proxy-data",
g_object_ref (g_dbus_proxy_get_connection (proxy)),
on_proxy_removed);
return proxy;
}
void
meta_sensors_proxy_mock_set_property (MetaSensorsProxyMock *proxy,
const gchar *property_name,
GVariant *value)
{
g_autoptr (GVariant) ret = NULL;
g_autoptr (GVariant) reffed_value = g_variant_ref (value);
g_dbus_proxy_call (proxy, "SetInternalProperty",
g_variant_new ("(ssv)",
"net.hadess.SensorProxy",
property_name,
reffed_value),
G_DBUS_CALL_FLAGS_NONE,
-1, NULL, on_proxy_call_cb, &ret);
while (!ret)
g_main_context_iteration (NULL, TRUE);
g_assert_nonnull (ret);
ensure_property (proxy, property_name, value);
}
void
meta_sensors_proxy_mock_set_orientation (MetaSensorsProxyMock *proxy,
MetaOrientation orientation)
{
const char *orientation_str;
meta_sensors_proxy_mock_set_property (proxy, "HasAccelerometer",
g_variant_new_boolean (TRUE));
orientation_str = orientation_to_string (orientation);
meta_sensors_proxy_mock_set_property (proxy, "AccelerometerOrientation",
g_variant_new_string (orientation_str));
}
void
meta_sensors_proxy_mock_wait_accelerometer_claimed (MetaSensorsProxyMock *proxy,
gboolean claimed)
{
while (TRUE)
{
g_autoptr (GVariant) ret = NULL;
size_t n_owners = 0;
ret = get_internal_property_value (proxy, "AccelerometerOwners");
if (!g_variant_get_strv (ret, &n_owners))
g_assert_not_reached ();
if (n_owners == (claimed ? 1 : 0))
break;
g_main_context_iteration (NULL, TRUE);
}
}
|