2 * Copyright (C) 2011 Red Hat Inc.
5 * Matthias Clasen <mclasen@redhat.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
32 text_deleted (AtkText *atk_text, gint position, gint length, SignalData *data)
35 data->position = position;
36 data->length = length;
40 text_inserted (AtkText *atk_text, gint position, gint length, SignalData *data)
43 data->position = position;
44 data->length = length;
48 set_text (GtkWidget *widget,
51 if (GTK_IS_LABEL (widget))
52 gtk_label_set_text (GTK_LABEL (widget), text);
53 else if (GTK_IS_ENTRY (widget))
54 gtk_entry_set_text (GTK_ENTRY (widget), text);
55 else if (GTK_IS_TEXT_VIEW (widget))
56 gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget)), text, -1);
58 g_assert_not_reached ();
62 test_text_changed (GtkWidget *widget)
65 const gchar *text = "Text goes here";
66 SignalData delete_data;
67 SignalData insert_data;
69 atk_text = ATK_TEXT (gtk_widget_get_accessible (widget));
71 delete_data.count = 0;
72 insert_data.count = 0;
74 g_signal_connect (atk_text, "text_changed::delete",
75 G_CALLBACK (text_deleted), &delete_data);
76 g_signal_connect (atk_text, "text_changed::insert",
77 G_CALLBACK (text_inserted), &insert_data);
79 set_text (widget, text);
81 g_assert_cmpint (delete_data.count, >, 0);
82 g_assert_cmpint (delete_data.position, ==, 0);
83 g_assert_cmpint (delete_data.length, ==, -1);
85 g_assert_cmpint (insert_data.count, >, 0);
86 g_assert_cmpint (insert_data.position, ==, 0);
87 g_assert_cmpint (insert_data.length, ==, -1);
89 g_signal_handlers_disconnect_by_func (atk_text, G_CALLBACK (text_deleted), &delete_data);
90 g_signal_handlers_disconnect_by_func (atk_text, G_CALLBACK (text_inserted), &insert_data);
95 AtkTextBoundary boundary;
102 test_words (GtkWidget *widget)
105 const gchar *text = "This is a medium-size test string, including some \303\204\303\226\303\234 and 123 for good measure.";
106 const gchar *expected_words[] = {
116 "\303\204\303\226\303\234 ",
130 atk_text = ATK_TEXT (gtk_widget_get_accessible (widget));
132 set_text (widget, text);
136 for (offset = 0; offset < g_utf8_strlen (text, -1); offset++)
138 word = atk_text_get_text_at_offset (atk_text,
140 ATK_TEXT_BOUNDARY_WORD_START,
142 if (g_strcmp0 (last_word, word) != 0)
144 g_assert_cmpstr (word, ==, expected_words[i]);
151 g_assert (expected_words[i] == 0);
155 setup_test (GtkWidget *widget)
157 set_text (widget, "");
161 add_text_test (const gchar *prefix,
162 GTestFixtureFunc test_func,
167 path = g_strdup_printf ("%s/%s", prefix, G_OBJECT_TYPE_NAME (widget));
168 g_test_add_vtable (path,
170 g_object_ref (widget),
171 (GTestFixtureFunc) setup_test,
172 (GTestFixtureFunc) test_func,
173 (GTestFixtureFunc) g_object_unref);
178 add_text_tests (GtkWidget *widget)
180 g_object_ref_sink (widget);
181 add_text_test ("/text/words", (GTestFixtureFunc) test_words, widget);
182 add_text_test ("/text/changed", (GTestFixtureFunc) test_text_changed, widget);
183 g_object_unref (widget);
187 main (int argc, char *argv[])
189 gtk_test_init (&argc, &argv, NULL);
191 add_text_tests (gtk_text_view_new ());
192 add_text_tests (gtk_entry_new ());
193 add_text_tests (gtk_label_new (""));
195 return g_test_run ();