aboutsummaryrefslogtreecommitdiff
path: root/tests/test-indicator-ng.c
blob: 084433404e4026f4638462307375fa2ce0035f3c (plain)
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

#include <libayatana-indicator/indicator-ng.h>

static void
indicator_ng_test_func (gconstpointer user_data)
{
  GTestFunc test_func = user_data;
  GTestDBus *bus;

  bus = g_test_dbus_new (G_TEST_DBUS_NONE);
  g_test_dbus_add_service_dir (bus, BUILD_DIR);
  g_test_dbus_up (bus);

  test_func ();

  g_test_dbus_down (bus);
  g_object_unref (bus);
}

#define indicator_ng_test_add(name, test_func) \
  g_test_add_data_func ("/indicator-ng/" name, test_func, indicator_ng_test_func)

static gboolean
stop_main_loop (gpointer user_data)
{
  GMainLoop *loop = user_data;

  g_main_loop_quit (loop);

  return FALSE;
}

static void
test_non_existing (void)
{
  IndicatorNg *indicator;
  GError *error = NULL;

  indicator = indicator_ng_new (SRCDIR "/org.ayatana.does.not.exist.indicator", &error);
  g_assert (indicator == NULL);
  g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);

  g_clear_error (&error);
}

static void
test_instantiation (void)
{
  IndicatorNg *indicator;
  GError *error = NULL;
  GMainLoop *loop;

  indicator = indicator_ng_new (SRCDIR "/org.ayatana.indicator.no-such-service", &error);
  g_assert (indicator);
  g_assert (error == NULL);

  g_assert_cmpstr (indicator_ng_get_service_file (indicator), ==, SRCDIR "/org.ayatana.indicator.no-such-service");
  g_assert_cmpstr (indicator_ng_get_profile (indicator), ==, "desktop");

  {
    gchar *service_file;
    gchar *profile;

    g_object_get (indicator, "service-file", &service_file,
                             "profile", &profile,
                             NULL);

    g_assert_cmpstr (service_file, ==, SRCDIR "/org.ayatana.indicator.no-such-service");
    g_assert_cmpstr (profile, ==, "desktop");

    g_free (service_file);
    g_free (profile);
  }

  loop = g_main_loop_new (NULL, FALSE);
  g_timeout_add (200, stop_main_loop, loop);
  g_main_loop_run (loop);

  /* no such service, so there shouldn't be any indicators */
  g_assert (indicator_object_get_entries (INDICATOR_OBJECT (indicator)) == NULL);

  g_main_loop_unref (loop);
  g_object_unref (indicator);
}

static void
test_instantiation_with_profile (void)
{
  IndicatorNg *indicator;
  GError *error = NULL;

  indicator = indicator_ng_new_for_profile (SRCDIR "/org.ayatana.indicator.test", "greeter", &error);
  g_assert (indicator);
  g_assert (error == NULL);

  g_assert_cmpstr (indicator_ng_get_profile (indicator), ==, "greeter");

  g_object_unref (indicator);
}

/* From gtk+/testsuite/gtk/gtkmenu.c
 *
 * Returns the label of a GtkModelMenuItem, for which
 * gtk_menu_item_get_label() returns NULL, because it uses its own
 * widgets to accommodate an icon.
 *
 */
static const gchar *
get_label (GtkMenuItem *item)
{
  GList *children = gtk_container_get_children (GTK_CONTAINER (item));
  const gchar *label = NULL;

  while (children)
    {
      if (GTK_IS_CONTAINER (children->data))
        children = g_list_concat (children, gtk_container_get_children (children->data));
      else if (GTK_IS_LABEL (children->data))
        label = gtk_label_get_text (children->data);

      children = g_list_delete_link (children, children);
    }

  return label;
}

static void
test_menu (void)
{
  IndicatorNg *indicator;
  GError *error = NULL;
  GMainLoop *loop;
  GList *entries;
  IndicatorObjectEntry *entry;

  indicator = indicator_ng_new (SRCDIR "/org.ayatana.indicator.test", &error);
  g_assert (indicator);
  g_assert (error == NULL);

  loop = g_main_loop_new (NULL, FALSE);
  g_timeout_add (500, stop_main_loop, loop);
  g_main_loop_run (loop);

  entries = indicator_object_get_entries (INDICATOR_OBJECT (indicator));
  g_assert_cmpint (g_list_length (entries), ==, 1);

  entry = entries->data;
  g_assert_cmpstr (entry->name_hint, ==, "indicator-test");
  g_assert_cmpstr (entry->accessible_desc, ==, "Test indicator");
  g_assert_cmpstr (gtk_label_get_label (entry->label), ==, "Test");
  g_assert (gtk_image_get_storage_type (entry->image) == GTK_IMAGE_ICON_NAME);
  {
    GList *children;
    GtkMenuItem *item;

    g_assert (entry->menu != NULL);

    children = gtk_container_get_children (GTK_CONTAINER (entry->menu));
    g_assert_cmpint (g_list_length (children), ==, 1);

    item = children->data;
    g_assert (GTK_IS_MENU_ITEM (item));
    g_assert (gtk_widget_is_sensitive (GTK_WIDGET (item)));
    g_assert_cmpstr (get_label (item), ==, "Show");

    g_list_free (children);
  }

  g_list_free (entries);
  g_main_loop_unref (loop);
  g_object_unref (indicator);
}

int
main (int argc, char **argv)
{
  /* gvfs, dconf, and appmenu-gtk leak GDbusConnections, which confuses
   * g_test_dbus_down.  Make sure we're not using any of those.
   */
  g_setenv ("GIO_USE_VFS", "local", TRUE);
  g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
  g_unsetenv ("UBUNTU_MENUPROXY");

  g_test_init (&argc, &argv, NULL);
  gtk_init (&argc, &argv);

  indicator_ng_test_add ("non-existing", test_non_existing);
  indicator_ng_test_add ("instantiation", test_instantiation);
  indicator_ng_test_add ("instantiation-with-profile", test_instantiation_with_profile);
  indicator_ng_test_add ("menu", test_menu);

  return g_test_run ();
}