From 0996bce98e7f8c1de5e0d6b3f67589ab083a4713 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Wed, 23 Jan 2013 18:00:49 +0100 Subject: indicator-ng: more elaborate testing Use GTestDBus to spawn a small test service (tests/indicator-test-service.c) and check whether the indicator menu gets turned into a gtkmenu correctly. --- tests/indicator-test-service.c | 107 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/indicator-test-service.c (limited to 'tests/indicator-test-service.c') diff --git a/tests/indicator-test-service.c b/tests/indicator-test-service.c new file mode 100644 index 0000000..3a6a0b2 --- /dev/null +++ b/tests/indicator-test-service.c @@ -0,0 +1,107 @@ + +#include + +typedef struct +{ + GSimpleActionGroup *actions; + GMenu *menu; + + guint actions_export_id; + guint menu_export_id; +} IndicatorTestService; + +static void +bus_acquired (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + IndicatorTestService *indicator = user_data; + GError *error = NULL; + + indicator->actions_export_id = g_dbus_connection_export_action_group (connection, + "/com/canonical/indicator/test", + G_ACTION_GROUP (indicator->actions), + &error); + if (indicator->actions_export_id == 0) + { + g_warning ("cannot export action group: %s", error->message); + g_error_free (error); + return; + } + + indicator->menu_export_id = g_dbus_connection_export_menu_model (connection, + "/com/canonical/indicator/test/desktop", + G_MENU_MODEL (indicator->menu), + &error); + if (indicator->menu_export_id == 0) + { + g_warning ("cannot export menu: %s", error->message); + g_error_free (error); + return; + } +} + +static void +name_lost (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + IndicatorTestService *indicator = user_data; + + if (indicator->actions_export_id) + g_dbus_connection_unexport_action_group (connection, indicator->actions_export_id); + + if (indicator->menu_export_id) + g_dbus_connection_unexport_menu_model (connection, indicator->menu_export_id); +} + +static void +activate_show (GSimpleAction *action, + GVariant *parameter, + gpointer user_data) +{ + g_message ("showing"); +} + +int +main (int argc, char **argv) +{ + IndicatorTestService indicator = { 0 }; + GMenuItem *item; + GMenu *submenu; + GActionEntry entries[] = { + { "_header", NULL, NULL, "('Test', 'indicator-test', 'Test indicator', true)", NULL }, + { "show", activate_show, NULL, NULL, NULL } + }; + GMainLoop *loop; + + indicator.actions = g_simple_action_group_new (); + g_simple_action_group_add_entries (indicator.actions, entries, G_N_ELEMENTS (entries), NULL); + + submenu = g_menu_new (); + g_menu_append (submenu, "Show", "indicator.show"); + item = g_menu_item_new (NULL, "indicator._header"); + g_menu_item_set_submenu (item, G_MENU_MODEL (submenu)); + indicator.menu = g_menu_new (); + g_menu_append_item (indicator.menu, item); + + g_bus_own_name (G_BUS_TYPE_SESSION, + "com.canonical.indicator.test", + G_BUS_NAME_OWNER_FLAGS_NONE, + bus_acquired, + NULL, + name_lost, + &indicator, + NULL); + + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + + g_object_unref (submenu); + g_object_unref (item); + g_object_unref (indicator.actions); + g_object_unref (indicator.menu); + g_object_unref (loop); + + return 0; +} -- cgit v1.2.3 From 82ecfaaa89684a7504913fb039d0dd8302c82ec9 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Fri, 25 Jan 2013 10:29:57 +0100 Subject: indicator-ng: require header item to have x-canonical-type set --- libindicator/indicator-ng.c | 53 ++++++++++++++++++++++++++++++------------ tests/indicator-test-service.c | 1 + 2 files changed, 39 insertions(+), 15 deletions(-) (limited to 'tests/indicator-test-service.c') diff --git a/libindicator/indicator-ng.c b/libindicator/indicator-ng.c index 003ec70..05ece8d 100644 --- a/libindicator/indicator-ng.c +++ b/libindicator/indicator-ng.c @@ -219,6 +219,23 @@ indicator_ng_update_entry (IndicatorNg *self) g_variant_unref (state); } +static gboolean +indicator_ng_menu_item_is_of_type (GMenuModel *menu, + gint index, + const gchar *expected_type) +{ + gchar *type; + gboolean has_type = FALSE; + + if (g_menu_model_get_item_attribute (menu, index, "x-canonical-type", "s", &type)) + { + has_type = g_str_equal (type, expected_type); + g_free (type); + } + + return has_type; +} + static void indicator_ng_menu_changed (GMenuModel *menu, gint position, @@ -240,26 +257,32 @@ indicator_ng_menu_changed (GMenuModel *menu, if (added) { - GMenuModel *popup; - gchar *action; - g_clear_pointer (&self->header_action, g_free); - if (g_menu_model_get_item_attribute (self->menu, 0, G_MENU_ATTRIBUTE_ACTION, "s", &action) && - g_str_has_prefix (action, "indicator.")) - { - self->header_action = g_strdup (action + 10); - } - popup = g_menu_model_get_item_link (self->menu, 0, G_MENU_LINK_SUBMENU); - if (popup) + if (indicator_ng_menu_item_is_of_type (self->menu, 0, "com.canonical.indicator.root")) { - gtk_menu_shell_bind_model (GTK_MENU_SHELL (self->entry.menu), popup, NULL, TRUE); - g_object_unref (popup); - } + GMenuModel *popup; + gchar *action; - indicator_ng_update_entry (self); + if (g_menu_model_get_item_attribute (self->menu, 0, G_MENU_ATTRIBUTE_ACTION, "s", &action) && + g_str_has_prefix (action, "indicator.")) + { + self->header_action = g_strdup (action + 10); + } - g_free (action); + popup = g_menu_model_get_item_link (self->menu, 0, G_MENU_LINK_SUBMENU); + if (popup) + { + gtk_menu_shell_bind_model (GTK_MENU_SHELL (self->entry.menu), popup, NULL, TRUE); + g_object_unref (popup); + } + + indicator_ng_update_entry (self); + + g_free (action); + } + else + g_warning ("indicator menu item must be of type 'com.canonical.indicator.root'"); } } diff --git a/tests/indicator-test-service.c b/tests/indicator-test-service.c index 3a6a0b2..0393677 100644 --- a/tests/indicator-test-service.c +++ b/tests/indicator-test-service.c @@ -81,6 +81,7 @@ main (int argc, char **argv) submenu = g_menu_new (); g_menu_append (submenu, "Show", "indicator.show"); item = g_menu_item_new (NULL, "indicator._header"); + g_menu_item_set_attribute (item, "x-canonical-type", "s", "com.canonical.indicator.root"); g_menu_item_set_submenu (item, G_MENU_MODEL (submenu)); indicator.menu = g_menu_new (); g_menu_append_item (indicator.menu, item); -- cgit v1.2.3