aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2012-05-28 11:33:07 -0500
committerCharles Kerr <charles.kerr@canonical.com>2012-05-28 11:33:07 -0500
commitb9a6194c12bf39fb6317edc1126ea32e890da583 (patch)
tree77ee861b8290309219a0911270f1f87d282e1129
parent23a2672a1a763cdef67ccc5cb9f8fec7daf021be (diff)
downloadayatana-indicator-power-b9a6194c12bf39fb6317edc1126ea32e890da583.tar.gz
ayatana-indicator-power-b9a6194c12bf39fb6317edc1126ea32e890da583.tar.bz2
ayatana-indicator-power-b9a6194c12bf39fb6317edc1126ea32e890da583.zip
add a mock GSD.Power to handle the GetPower requests
-rw-r--r--src/dbus-listener.c11
-rw-r--r--src/dbus-listener.h5
-rw-r--r--tests/test-dbus-listener.cc170
3 files changed, 177 insertions, 9 deletions
diff --git a/src/dbus-listener.c b/src/dbus-listener.c
index 6a85fd4..a7a29ee 100644
--- a/src/dbus-listener.c
+++ b/src/dbus-listener.c
@@ -24,11 +24,6 @@ License along with this library. If not, see
#include "dbus-listener.h"
#include "device.h"
-#define DBUS_SERVICE "org.gnome.SettingsDaemon"
-#define DBUS_PATH "/org/gnome/SettingsDaemon"
-#define POWER_DBUS_PATH DBUS_PATH "/Power"
-#define POWER_DBUS_INTERFACE "org.gnome.SettingsDaemon.Power"
-
struct _IndicatorPowerDbusListenerPrivate
{
GCancellable * cancellable;
@@ -89,7 +84,7 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self)
priv->cancellable = g_cancellable_new ();
priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
- DBUS_SERVICE,
+ GSD_SERVICE,
G_BUS_NAME_WATCHER_FLAGS_NONE,
gsd_appeared_callback,
NULL,
@@ -226,8 +221,8 @@ gsd_appeared_callback (GDBusConnection *connection,
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
NULL,
name,
- POWER_DBUS_PATH,
- POWER_DBUS_INTERFACE,
+ GSD_POWER_DBUS_PATH,
+ GSD_POWER_DBUS_INTERFACE,
priv->cancellable,
service_proxy_cb,
self);
diff --git a/src/dbus-listener.h b/src/dbus-listener.h
index 2dc0865..d1ab993 100644
--- a/src/dbus-listener.h
+++ b/src/dbus-listener.h
@@ -41,6 +41,11 @@ typedef struct _IndicatorPowerDbusListener IndicatorPowerDbusListener;
typedef struct _IndicatorPowerDbusListenerClass IndicatorPowerDbusListenerClass;
typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPrivate;
+#define GSD_SERVICE "org.gnome.SettingsDaemon"
+#define GSD_PATH "/org/gnome/SettingsDaemon"
+#define GSD_POWER_DBUS_INTERFACE GSD_SERVICE ".Power"
+#define GSD_POWER_DBUS_PATH GSD_PATH "/Power"
+
/* signals */
#define INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED "indicator-power-dbus-listener-devices-enumerated"
diff --git a/tests/test-dbus-listener.cc b/tests/test-dbus-listener.cc
index 869d08a..2cfe4a7 100644
--- a/tests/test-dbus-listener.cc
+++ b/tests/test-dbus-listener.cc
@@ -46,15 +46,33 @@ namespace
class DbusListenerTest : public ::testing::Test
{
- protected:
+ public:
+
+ GSList * devices;
+ GDBusConnection * connection;
+ GMainLoop * mainloop;
+ GTestDBus * bus;
+ bool bus_acquired;
+ bool name_acquired;
virtual void SetUp()
{
+ bus_acquired = false;
+ name_acquired = false;
+ connection = NULL;
+ devices = NULL;
+
ensure_glib_initialized ();
+ mainloop = g_main_loop_new (NULL, FALSE);
+ bus = g_test_dbus_new (G_TEST_DBUS_NONE);
+ g_test_dbus_up (bus);
}
virtual void TearDown()
{
+ g_slist_free_full (devices, g_object_unref);
+ g_test_dbus_down (bus);
+ g_main_loop_unref (mainloop);
}
};
@@ -62,6 +80,7 @@ class DbusListenerTest : public ::testing::Test
****
***/
+/*
TEST_F(DbusListenerTest, GObjectNew)
{
GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL));
@@ -70,4 +89,153 @@ TEST_F(DbusListenerTest, GObjectNew)
g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls
g_object_unref (o);
}
+*/
+static void
+on_bus_acquired (GDBusConnection *conn, const gchar * name, gpointer gself)
+{
+ g_debug ("bus acquired: %s, connection is %p", name, conn);
+
+ DbusListenerTest * test = static_cast<DbusListenerTest*> (gself);
+ test->connection = conn;
+ test->bus_acquired = true;
+}
+
+static void
+on_name_acquired (GDBusConnection * conn, const gchar * name, gpointer gself)
+{
+ g_debug ("name acquired: %s, connection is %p", name, conn);
+
+ DbusListenerTest * test = static_cast<DbusListenerTest*> (gself);
+ test->name_acquired = true;
+ g_main_loop_quit (test->mainloop);
+}
+
+static void
+on_name_lost (GDBusConnection * conn, const gchar * name, gpointer gself)
+{
+ g_debug ("name lost: %s, connection is %p", name, conn);
+
+ DbusListenerTest * test = static_cast<DbusListenerTest*> (gself);
+ test->name_acquired = false;
+ g_main_loop_quit (test->mainloop);
+}
+
+static void
+on_devices_enumerated (IndicatorPowerDbusListener * l, GSList * devices, gpointer gself)
+{
+ g_debug ("on_devices_enumerated");
+
+ DbusListenerTest * test = static_cast<DbusListenerTest*> (gself);
+ test->name_acquired = false;
+ g_slist_foreach (devices, (GFunc)g_object_ref, NULL);
+ test->devices = g_slist_copy (devices);
+ g_main_loop_quit (test->mainloop);
+}
+
+static void
+gsd_power_handle_method_call (GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ if (!g_strcmp0 (method_name, "GetDevices"))
+ {
+ /* create builder */
+ GVariantBuilder * builder = g_variant_builder_new (G_VARIANT_TYPE("a(susdut)"));
+
+ g_variant_builder_add (builder, "(susdut)",
+ "/org/freedesktop/UPower/devices/line_power_AC",
+ UP_DEVICE_KIND_LINE_POWER,
+ ". GThemedIcon ac-adapter-symbolic ac-adapter ",
+ 0.0,
+ UP_DEVICE_STATE_UNKNOWN,
+ 0);
+ g_variant_builder_add (builder, "(susdut)",
+ "/org/freedesktop/UPower/devices/battery_BAT0",
+ UP_DEVICE_KIND_BATTERY,
+ ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ",
+ 52.871712,
+ UP_DEVICE_STATE_DISCHARGING,
+ 8834);
+
+ GVariant * value = g_variant_builder_end (builder);
+ GVariant * tuple = g_variant_new_tuple (&value, 1);
+ g_dbus_method_invocation_return_value (invocation, tuple);
+ g_variant_builder_unref (builder);
+ }
+}
+
+static const GDBusInterfaceVTable gsd_power_interface_vtable =
+{
+ gsd_power_handle_method_call,
+ NULL, /* GetProperty */
+ NULL, /* SetProperty */
+};
+
+TEST_F(DbusListenerTest, GnomeSettingsDaemon)
+{
+ // own org.gnome.SettingsDameon on the bus
+ const int name_ownership_id = g_bus_own_name (
+ G_BUS_TYPE_SESSION,
+ GSD_SERVICE,
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ on_bus_acquired,
+ on_name_acquired,
+ on_name_lost,
+ this, NULL);
+ ASSERT_FALSE (bus_acquired);
+ ASSERT_FALSE (name_acquired);
+ ASSERT_TRUE (connection == NULL);
+ g_main_loop_run (mainloop);
+ ASSERT_TRUE (bus_acquired);
+ ASSERT_TRUE (name_acquired);
+ ASSERT_TRUE (G_IS_DBUS_CONNECTION(connection));
+
+ // register a mock org.gnome.SettingsDaemon.Power interface
+ const gchar introspection_xml[] = "<node>"
+ " <interface name='org.gnome.SettingsDaemon.Power'>"
+ " <property name='Icon' type='s' access='read'>"
+ " </property>"
+ " <property name='Tooltip' type='s' access='read'>"
+ " </property>"
+ " <method name='GetDevices'>"
+ " <arg name='devices' type='a(susdut)' direction='out' />"
+ " </method>"
+ " </interface>"
+ "</node>";
+ GDBusNodeInfo * introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
+ ASSERT_TRUE (introspection_data != NULL);
+ const int registration_id = g_dbus_connection_register_object (connection,
+ GSD_POWER_DBUS_PATH,
+ introspection_data->interfaces[0],
+ &gsd_power_interface_vtable,
+ NULL, NULL, NULL);
+
+ // create an i-power dbus listener to watch for GSD
+ ASSERT_EQ (g_slist_length(devices), 0);
+ GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL));
+ ASSERT_TRUE (o != NULL);
+ ASSERT_TRUE (INDICATOR_IS_POWER_DBUS_LISTENER(o));
+ g_signal_connect(o, INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED,
+ G_CALLBACK(on_devices_enumerated), this);
+ g_main_loop_run (mainloop);
+
+ // test the devices should have gotten
+ ASSERT_EQ (g_slist_length(devices), 2);
+ IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_slist_nth_data (devices, 0));
+ ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_LINE_POWER);
+ ASSERT_STREQ ("/org/freedesktop/UPower/devices/line_power_AC", indicator_power_device_get_object_path(device));
+ device = INDICATOR_POWER_DEVICE (g_slist_nth_data (devices, 1));
+ ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device));
+ ASSERT_STREQ ("/org/freedesktop/UPower/devices/battery_BAT0", indicator_power_device_get_object_path(device));
+
+ // cleanup
+ g_object_unref (o);
+ g_dbus_connection_unregister_object (connection, registration_id);
+ g_bus_unown_name (name_ownership_id);
+}