1 /* GtkAction tests.
2 *
3 * Authors: Jan Arne Petersen <jpetersen@openismus.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #define GDK_DISABLE_DEPRECATION_WARNINGS
20 #include <gtk/gtk.h>
21
22 /* Fixture */
23
24 typedef struct
25 {
26 GtkAction *action;
27 } ActionTest;
28
29 static void
action_test_setup(ActionTest * fixture,gconstpointer test_data)30 action_test_setup (ActionTest *fixture,
31 gconstpointer test_data)
32 {
33 fixture->action = gtk_action_new ("name", "label", NULL, NULL);
34 }
35
36 static void
action_test_teardown(ActionTest * fixture,gconstpointer test_data)37 action_test_teardown (ActionTest *fixture,
38 gconstpointer test_data)
39 {
40 g_object_unref (fixture->action);
41 }
42
43 static void
notify_count_emmisions(GObject * object,GParamSpec * pspec,gpointer data)44 notify_count_emmisions (GObject *object,
45 GParamSpec *pspec,
46 gpointer data)
47 {
48 unsigned int *i = data;
49 (*i)++;
50 }
51
52 static void
menu_item_label_notify_count(ActionTest * fixture,gconstpointer test_data)53 menu_item_label_notify_count (ActionTest *fixture,
54 gconstpointer test_data)
55 {
56 GtkWidget *item = gtk_menu_item_new ();
57 unsigned int emmisions = 0;
58
59 g_object_ref_sink (item);
60 g_signal_connect (item, "notify::label",
61 G_CALLBACK (notify_count_emmisions), &emmisions);
62
63 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (item),
64 fixture->action);
65
66 g_assert_cmpuint (emmisions, ==, 1);
67
68 gtk_action_set_label (fixture->action, "new label");
69
70 g_assert_cmpuint (emmisions, ==, 2);
71
72 g_object_unref (item);
73 }
74
75 #if !GLIB_CHECK_VERSION(2,60,0)
76 gboolean
g_strv_equal(const gchar * const * strv1,const gchar * const * strv2)77 g_strv_equal (const gchar * const *strv1,
78 const gchar * const *strv2)
79 {
80 g_return_val_if_fail (strv1 != NULL, FALSE);
81 g_return_val_if_fail (strv2 != NULL, FALSE);
82
83 if (strv1 == strv2)
84 return TRUE;
85
86 for (; *strv1 != NULL && *strv2 != NULL; strv1++, strv2++)
87 {
88 if (!g_str_equal (*strv1, *strv2))
89 return FALSE;
90 }
91
92 return (*strv1 == NULL && *strv2 == NULL);
93 }
94 #endif
95
96 static void
g_test_action_muxer(void)97 g_test_action_muxer (void)
98 {
99 GtkWidget *window;
100 GtkWidget *box;
101 GtkWidget *button;
102 const char **prefixes;
103 const char * const expected[] = { "win", NULL };
104 const char * const expected1[] = { "group1", "win", NULL };
105 const char * const expected2[] = { "group2", "win", NULL };
106 const char * const expected3[] = { "group1", "group2", "win", NULL };
107 GActionGroup *win;
108 GActionGroup *group1;
109 GActionGroup *group2;
110 GActionGroup *grp;
111
112 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
113 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
114 button = gtk_button_new_with_label ("test");
115
116 gtk_container_add (GTK_CONTAINER (window), box);
117 gtk_container_add (GTK_CONTAINER (box), button);
118
119 win = G_ACTION_GROUP (g_simple_action_group_new ());
120 gtk_widget_insert_action_group (window, "win", win);
121
122 prefixes = gtk_widget_list_action_prefixes (window);
123 g_assert (g_strv_equal ((const char * const *)prefixes, expected));
124 g_free (prefixes);
125
126 prefixes = gtk_widget_list_action_prefixes (box);
127 g_assert (g_strv_equal ((const char * const *)prefixes, expected));
128 g_free (prefixes);
129
130 prefixes = gtk_widget_list_action_prefixes (button);
131 g_assert (g_strv_equal ((const char * const *)prefixes, expected));
132 g_free (prefixes);
133
134 grp = gtk_widget_get_action_group (window, "win");
135 g_assert (grp == win);
136 grp = gtk_widget_get_action_group (window, "bla");
137 g_assert (grp == NULL);
138
139 grp = gtk_widget_get_action_group (box, "win");
140 g_assert (grp == win);
141 grp = gtk_widget_get_action_group (box, "bla");
142 g_assert (grp == NULL);
143
144 grp = gtk_widget_get_action_group (button, "win");
145 g_assert (grp == win);
146 grp = gtk_widget_get_action_group (button, "bla");
147 g_assert (grp == NULL);
148
149 group1 = G_ACTION_GROUP (g_simple_action_group_new ());
150 gtk_widget_insert_action_group (button, "group1", group1);
151
152 prefixes = gtk_widget_list_action_prefixes (window);
153 g_assert (g_strv_equal ((const char * const *)prefixes, expected));
154 g_free (prefixes);
155
156 prefixes = gtk_widget_list_action_prefixes (box);
157 g_assert (g_strv_equal ((const char * const *)prefixes, expected));
158 g_free (prefixes);
159
160 prefixes = gtk_widget_list_action_prefixes (button);
161 g_assert (g_strv_equal ((const char * const *)prefixes, expected1));
162 g_free (prefixes);
163
164 grp = gtk_widget_get_action_group (window, "win");
165 g_assert (grp == win);
166 grp = gtk_widget_get_action_group (window, "group1");
167 g_assert (grp == NULL);
168
169 grp = gtk_widget_get_action_group (box, "win");
170 g_assert (grp == win);
171 grp = gtk_widget_get_action_group (box, "group1");
172 g_assert (grp == NULL);
173
174 grp = gtk_widget_get_action_group (button, "win");
175 g_assert (grp == win);
176 grp = gtk_widget_get_action_group (button, "group1");
177 g_assert (grp == group1);
178
179 group2 = G_ACTION_GROUP (g_simple_action_group_new ());
180 gtk_widget_insert_action_group (box, "group2", group2);
181
182 prefixes = gtk_widget_list_action_prefixes (window);
183 g_assert (g_strv_equal ((const char * const *)prefixes, expected));
184 g_free (prefixes);
185
186 prefixes = gtk_widget_list_action_prefixes (box);
187 g_assert (g_strv_equal ((const char * const *)prefixes, expected2));
188 g_free (prefixes);
189
190 prefixes = gtk_widget_list_action_prefixes (button);
191 g_assert (g_strv_equal ((const char * const *)prefixes, expected3));
192 g_free (prefixes);
193
194 grp = gtk_widget_get_action_group (window, "win");
195 g_assert (grp == win);
196 grp = gtk_widget_get_action_group (window, "group2");
197 g_assert (grp == NULL);
198
199 grp = gtk_widget_get_action_group (box, "win");
200 g_assert (grp == win);
201 grp = gtk_widget_get_action_group (box, "group2");
202 g_assert (grp == group2);
203
204 grp = gtk_widget_get_action_group (button, "win");
205 g_assert (grp == win);
206 grp = gtk_widget_get_action_group (button, "group2");
207 g_assert (grp == group2);
208
209 gtk_widget_destroy (window);
210 g_object_unref (win);
211 g_object_unref (group1);
212 g_object_unref (group2);
213 }
214
215 /* main */
216
217 static void
test_reinsert(void)218 test_reinsert (void)
219 {
220 GtkWidget *widget;
221 GActionGroup *group;
222
223 widget = gtk_label_new ("");
224 group = G_ACTION_GROUP (g_simple_action_group_new ());
225
226 gtk_widget_insert_action_group (widget, "test", group);
227 g_assert (gtk_widget_get_action_group (widget, "test") == group);
228
229 g_clear_object (&group);
230
231 group = gtk_widget_get_action_group (widget, "test");
232 gtk_widget_insert_action_group (widget, "test", group);
233
234 gtk_widget_destroy (widget);
235 }
236
237 int
main(int argc,char ** argv)238 main (int argc,
239 char **argv)
240 {
241 gtk_test_init (&argc, &argv, NULL);
242
243 g_test_add ("/Action/MenuItem/label-notify-count",
244 ActionTest, NULL,
245 action_test_setup,
246 menu_item_label_notify_count,
247 action_test_teardown);
248
249 g_test_add_func ("/action/muxer/update-parent", g_test_action_muxer);
250 g_test_add_func ("/action/reinsert", test_reinsert);
251
252 return g_test_run ();
253 }
254