1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 static gint serial = 0;
36 notify (GtkEditable *editable, GParamSpec *pspec, EntryData *data)
38 data->serial = serial++;
40 data->text = gtk_editable_get_chars (editable, 0, -1);
41 gtk_editable_get_selection_bounds (editable, &data->start, &data->end);
44 g_print ("notify::%s\n", pspec->name);
45 g_print ("\ttext: %s\n", data->text);
46 g_print ("\tstart: %d\n", data->start);
47 g_print ("\tend: %d\n", data->end);
52 insert_text (GtkEditable *editable,
53 const gchar *new_text,
58 data->serial = serial++;
60 data->text = gtk_editable_get_chars (editable, 0, -1);
61 gtk_editable_get_selection_bounds (editable, &data->start, &data->end);
62 data->new_text = g_strdup (new_text);
63 data->position = *position;
64 data->length = new_text_length;
67 g_print ("insert-text \"%s\", %d\n", new_text, *position);
68 g_print ("\ttext: %s\n", data->text);
69 g_print ("\tstart: %d\n", data->start);
70 g_print ("\tend: %d\n", data->end);
75 delete_text (GtkEditable *editable,
80 data->serial = serial++;
82 data->text = gtk_editable_get_chars (editable, 0, -1);
83 gtk_editable_get_selection_bounds (editable, &data->start, &data->end);
84 data->position = start_pos;
85 data->length = end_pos - start_pos;
88 g_print ("delete-text %d %d\n", start_pos, end_pos);
89 g_print ("\ttext: %s\n", data->text);
90 g_print ("\tstart: %d\n", data->start);
91 g_print ("\tend: %d\n", data->end);
96 changed (GtkEditable *editable,
99 data->serial = serial++;
101 data->text = gtk_editable_get_chars (editable, 0, -1);
102 gtk_editable_get_selection_bounds (editable, &data->start, &data->end);
105 g_print ("changed\n");
106 g_print ("\ttext: %s\n", data->text);
107 g_print ("\tstart: %d\n", data->start);
108 g_print ("\tend: %d\n", data->end);
124 entry = gtk_entry_new ();
125 g_object_ref_sink (entry);
127 gtk_entry_set_text (GTK_ENTRY (entry), "bar");
128 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
129 pos = gtk_editable_get_position (GTK_EDITABLE (entry));
130 g_assert_cmpint (pos, ==, 3);
138 g_signal_connect (entry, "notify::cursor-position",
139 G_CALLBACK (notify), &data1);
140 g_signal_connect (entry, "notify::selection-bound",
141 G_CALLBACK (notify), &data2);
142 g_signal_connect (entry, "notify::text",
143 G_CALLBACK (notify), &data3);
144 g_signal_connect (entry, "insert-text",
145 G_CALLBACK (insert_text), &data4);
146 g_signal_connect (entry, "delete-text",
147 G_CALLBACK (delete_text), &data5);
148 g_signal_connect (entry, "changed",
149 G_CALLBACK (changed), &data6);
152 gtk_editable_insert_text (GTK_EDITABLE (entry), "foo", -1, &pos);
153 g_assert_cmpint (pos, ==, 3);
155 pos = gtk_editable_get_position (GTK_EDITABLE (entry));
156 g_assert_cmpint (pos, ==, 6);
158 /* Check that notification for ::text, ::cursor-position and
159 * ::selection-bound happens in a consistent state after the
162 g_assert_cmpint (data1.count, ==, 1);
163 g_assert_cmpint (data1.start, ==, 6);
164 g_assert_cmpint (data1.end, ==, 6);
165 g_assert_cmpstr (data1.text, ==, "foobar");
168 g_assert_cmpint (data2.count, ==, 1);
169 g_assert_cmpint (data2.start, ==, 6);
170 g_assert_cmpint (data2.end, ==, 6);
171 g_assert_cmpstr (data2.text, ==, "foobar");
174 g_assert_cmpint (data3.count, ==, 1);
175 g_assert_cmpint (data3.start, ==, 6);
176 g_assert_cmpint (data3.end, ==, 6);
177 g_assert_cmpstr (data3.text, ==, "foobar");
180 /* Check that ::insert-text sees the state _before_ the insertion */
181 g_assert_cmpint (data4.count, ==, 1);
182 g_assert_cmpint (data4.start, ==, 3);
183 g_assert_cmpint (data4.end, ==, 3);
184 g_assert_cmpstr (data4.text, ==, "bar");
185 g_assert_cmpint (data4.position, ==, 0);
186 g_assert_cmpint (data4.length, ==, 3);
187 g_assert_cmpstr (data4.new_text, ==, "foo");
189 g_free (data4.new_text);
191 /* no deletion here */
192 g_assert_cmpint (data5.count, ==, 0);
194 /* Check that ::changed sees the post-change state */
195 g_assert_cmpint (data6.count, ==, 1);
196 g_assert_cmpint (data6.start, ==, 6);
197 g_assert_cmpint (data6.end, ==, 6);
198 g_assert_cmpstr (data6.text, ==, "foobar");
201 /* Now check ordering: ::insert-text comes before ::notify */
202 g_assert_cmpint (data4.serial, <, data1.serial);
203 g_assert_cmpint (data4.serial, <, data2.serial);
204 g_assert_cmpint (data4.serial, <, data3.serial);
206 /* ... and ::changed comes after ::notify */
207 g_assert_cmpint (data6.serial, >, data1.serial);
208 g_assert_cmpint (data6.serial, >, data2.serial);
209 g_assert_cmpint (data6.serial, >, data3.serial);
211 g_object_unref (entry);
226 entry = gtk_entry_new ();
227 g_object_ref_sink (entry);
229 gtk_entry_set_text (GTK_ENTRY (entry), "foobar");
230 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
231 pos = gtk_editable_get_position (GTK_EDITABLE (entry));
232 g_assert_cmpint (pos, ==, 6);
240 g_signal_connect (entry, "notify::cursor-position",
241 G_CALLBACK (notify), &data1);
242 g_signal_connect (entry, "notify::selection-bound",
243 G_CALLBACK (notify), &data2);
244 g_signal_connect (entry, "notify::text",
245 G_CALLBACK (notify), &data3);
246 g_signal_connect (entry, "insert-text",
247 G_CALLBACK (insert_text), &data4);
248 g_signal_connect (entry, "delete-text",
249 G_CALLBACK (delete_text), &data5);
250 g_signal_connect (entry, "changed",
251 G_CALLBACK (changed), &data6);
253 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 3);
255 pos = gtk_editable_get_position (GTK_EDITABLE (entry));
256 g_assert_cmpint (pos, ==, 3);
258 /* Check that notification for ::text, ::cursor-position and
259 * ::selection-bound happens in a consistent state after the
262 g_assert_cmpint (data1.count, ==, 1);
263 g_assert_cmpint (data1.start, ==, 3);
264 g_assert_cmpint (data1.end, ==, 3);
265 g_assert_cmpstr (data1.text, ==, "bar");
268 g_assert_cmpint (data2.count, ==, 1);
269 g_assert_cmpint (data2.start, ==, 3);
270 g_assert_cmpint (data2.end, ==, 3);
271 g_assert_cmpstr (data2.text, ==, "bar");
274 g_assert_cmpint (data3.count, ==, 1);
275 g_assert_cmpint (data3.start, ==, 3);
276 g_assert_cmpint (data3.end, ==, 3);
277 g_assert_cmpstr (data3.text, ==, "bar");
280 /* no insertion here */
281 g_assert_cmpint (data4.count, ==, 0);
283 /* Check that ::delete-text sees the state _before_ the insertion */
284 g_assert_cmpint (data5.count, ==, 1);
285 g_assert_cmpint (data5.start, ==, 6);
286 g_assert_cmpint (data5.end, ==, 6);
287 g_assert_cmpstr (data5.text, ==, "foobar");
288 g_assert_cmpint (data5.position, ==, 0);
289 g_assert_cmpint (data5.length, ==, 3);
292 /* Check that ::changed sees the post-change state */
293 g_assert_cmpint (data6.count, ==, 1);
294 g_assert_cmpint (data6.start, ==, 3);
295 g_assert_cmpint (data6.end, ==, 3);
296 g_assert_cmpstr (data6.text, ==, "bar");
299 /* Now check ordering: ::delete-text comes before ::notify */
300 g_assert_cmpint (data5.serial, <, data1.serial);
301 g_assert_cmpint (data5.serial, <, data2.serial);
302 g_assert_cmpint (data5.serial, <, data3.serial);
304 /* ... and ::changed comes after ::notify */
305 g_assert_cmpint (data6.serial, >, data1.serial);
306 g_assert_cmpint (data6.serial, >, data2.serial);
307 g_assert_cmpint (data6.serial, >, data3.serial);
308 g_object_unref (entry);
315 gtk_test_init (&argc, &argv);
317 g_test_add_func ("/entry/delete", test_delete);
318 g_test_add_func ("/entry/insert", test_insert);