1 /* Scrolling test suite for GtkTreeView
2 * Copyright (C) 2006 Kristian Rietveld <kris@gtk.org>
3 * Copyright (C) 2007 Imendio AB, Kristian Rietveld
4 * Copyright (C) 2009 Kristian Rietveld <kris@gtk.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 /* Original v1.0 -- December 26, 2006
21 * Conversion to GLib/GTK+ test framework during December, 2007
29 #define VIEW_WIDTH 320
30 #define VIEW_HEIGHT 240
33 #define BIG_N_ROWS N_ROWS * 100
37 * - Test that nothing happens if the row is fully visible.
38 * - The tests are dependent on the theme/font (size measurements,
40 * - Convert to proper GTK+ coding style.
41 * - Briefly test scrolling in tree stores as well.
44 * - For tests with "mixed height" models, you must ensure that
45 * there are only two heights used in total and that the rows with
46 * height A and B are strictly alternating. The model creation
47 * functions already do this for you, but take this into account
48 * when you write a unit test that adds rows to such a created
49 * model, you must follow this rule otherwise things will break.
53 /* Constructing models for testing */
55 create_model (gboolean constant)
62 store = gtk_list_store_new (1, G_TYPE_STRING);
64 for (i = 0; i < N_ROWS; i++) {
65 gtk_list_store_append (store, &iter);
66 if (constant || i % 2 == 0)
67 gtk_list_store_set (store, &iter, 0, "Foo", -1);
69 gtk_list_store_set (store, &iter, 0, "Sliff\nSloff\nBleh", -1);
72 return GTK_TREE_MODEL (store);
76 create_big_model (gboolean constant)
83 store = gtk_list_store_new (1, G_TYPE_STRING);
85 for (i = 0; i < BIG_N_ROWS; i++) {
86 gtk_list_store_append (store, &iter);
87 if (constant || i % 2 == 0)
88 gtk_list_store_set (store, &iter, 0, "Foo", -1);
90 gtk_list_store_set (store, &iter, 0, "Sliff\nSloff\nBleh", -1);
93 return GTK_TREE_MODEL (store);
103 GtkWidget *tree_view;
108 scroll_fixture_setup (ScrollFixture *fixture,
110 gconstpointer test_data)
113 GtkCellRenderer *renderer;
114 GtkTreeViewColumn *column;
116 fixture->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
118 sw = gtk_scrolled_window_new (NULL, NULL);
119 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
122 gtk_container_add (GTK_CONTAINER (fixture->window), sw);
124 fixture->tree_view = gtk_tree_view_new_with_model (model);
125 g_object_unref (model);
126 gtk_scrolled_window_set_min_content_width (GTK_SCROLLED_WINDOW (sw), VIEW_WIDTH);
127 gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (sw), VIEW_HEIGHT);
128 gtk_widget_set_size_request (fixture->tree_view, VIEW_WIDTH, VIEW_HEIGHT);
130 renderer = gtk_cell_renderer_text_new ();
131 g_object_set (renderer, "editable", TRUE, NULL);
132 column = gtk_tree_view_column_new_with_attributes ("Title",
137 gtk_tree_view_append_column (GTK_TREE_VIEW (fixture->tree_view), column);
138 gtk_container_add (GTK_CONTAINER (sw), fixture->tree_view);
141 /* sets up a fixture with a model with constant row heights */
143 scroll_fixture_constant_setup (ScrollFixture *fixture,
144 gconstpointer test_data)
146 scroll_fixture_setup (fixture, create_model (TRUE), test_data);
149 /* sets up a fixture with a model with varying row heights */
151 scroll_fixture_mixed_setup (ScrollFixture *fixture,
152 gconstpointer test_data)
154 scroll_fixture_setup (fixture, create_model (FALSE), test_data);
157 /* sets up a fixture with a large model with constant row heights */
159 scroll_fixture_constant_big_setup (ScrollFixture *fixture,
160 gconstpointer test_data)
162 scroll_fixture_setup (fixture, create_big_model (TRUE), test_data);
165 /* sets up a fixture with a large model with varying row heights */
167 scroll_fixture_mixed_big_setup (ScrollFixture *fixture,
168 gconstpointer test_data)
170 scroll_fixture_setup (fixture, create_big_model (FALSE), test_data);
173 /* sets up a fixture with only a single row for the "single row scroll" test */
175 scroll_fixture_single_setup (ScrollFixture *fixture,
176 gconstpointer test_data)
179 GtkTreeIter iter, child;
181 store = gtk_tree_store_new (1, G_TYPE_STRING);
183 gtk_tree_store_append (store, &iter, NULL);
184 gtk_tree_store_set (store, &iter, 0, "Foo", -1);
186 gtk_tree_store_append (store, &child, &iter);
187 gtk_tree_store_set (store, &child, 0, "Two\nLines", -1);
189 /* The teardown will also destroy the model */
190 scroll_fixture_setup (fixture, GTK_TREE_MODEL (store), test_data);
193 /* sets up a fixture with a tree store */
195 scroll_fixture_tree_setup (ScrollFixture *fixture,
196 gconstpointer test_data)
199 GtkTreeIter iter, child;
202 store = gtk_tree_store_new (1, G_TYPE_STRING);
204 gtk_tree_store_append (store, &iter, NULL);
205 gtk_tree_store_set (store, &iter, 0, "Root node", -1);
207 for (i = 0; i < 5; i++) {
208 gtk_tree_store_append (store, &child, &iter);
209 gtk_tree_store_set (store, &child, 0, "Child node", -1);
212 for (i = 0; i < 5; i++) {
213 gtk_tree_store_append (store, &iter, NULL);
214 gtk_tree_store_set (store, &iter, 0, "Other node", -1);
217 /* The teardown will also destroy the model */
218 scroll_fixture_setup (fixture, GTK_TREE_MODEL (store), test_data);
222 scroll_fixture_mixed_tree_setup (ScrollFixture *fixture,
223 gconstpointer test_data)
226 GtkTreeIter iter, child;
229 store = gtk_tree_store_new (1, G_TYPE_STRING);
231 gtk_tree_store_append (store, &iter, NULL);
232 gtk_tree_store_set (store, &iter, 0, "Root\nnode", -1);
234 for (i = 0; i < 5; i++) {
235 gtk_tree_store_append (store, &child, &iter);
237 gtk_tree_store_set (store, &child, 0, "Child node", -1);
239 gtk_tree_store_set (store, &child,
240 0, "Child\nnode", -1);
243 for (i = 0; i < 5; i++) {
244 gtk_tree_store_append (store, &iter, NULL);
246 gtk_tree_store_set (store, &iter, 0, "Other node", -1);
248 gtk_tree_store_set (store, &iter, 0, "Other\nnode", -1);
251 /* The teardown will also destroy the model */
252 scroll_fixture_setup (fixture, GTK_TREE_MODEL (store), test_data);
256 scroll_fixture_teardown (ScrollFixture *fixture,
257 gconstpointer test_data)
259 gtk_widget_destroy (fixture->window);
263 * Position check and helpers.
273 get_row_start_for_index (GtkTreeView *tree_view, int index)
275 gint height1, height2;
280 path = gtk_tree_path_new_from_indices (0, -1);
281 gtk_tree_view_get_background_area (tree_view, path, NULL, &rect);
282 height1 = rect.height;
284 gtk_tree_path_next (path);
285 gtk_tree_view_get_background_area (tree_view, path, NULL, &rect);
286 height2 = rect.height;
287 gtk_tree_path_free (path);
289 row_start = (index / 2) * height1 + (index / 2) * height2;
291 row_start += height1;
297 get_pos_from_path (GtkTreeView *tree_view,
300 GtkAdjustment *vadjustment)
304 row_start = get_row_start_for_index (tree_view,
305 gtk_tree_path_get_indices (path)[0]);
307 if (row_start + row_height < gtk_adjustment_get_page_size (vadjustment))
310 if (row_start >= gtk_adjustment_get_upper (vadjustment) - gtk_adjustment_get_page_size (vadjustment))
317 assert_position_with_align (GtkTreeView *tree_view,
324 GtkAdjustment *vadjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (tree_view));
326 /* Switch on row-align: 0.0, 0.5, 1.0 */
327 switch ((int)(row_align * 2.)) {
329 if (pos == POS_TOP || pos == POS_CENTER) {
330 /* The row in question is the first row
332 * - rect.y should be zero
333 * - dy should be equal to the top
334 * y coordinate of the row.
336 g_assert (row_y == 0);
337 g_assert (gtk_adjustment_get_value (vadjustment) == row_start);
339 /* The row can be anywhere at the last
340 * page of the tree view.
341 * - dy is set to the start of the
344 g_assert (gtk_adjustment_get_value (vadjustment) == gtk_adjustment_get_upper (vadjustment) - gtk_adjustment_get_page_size (vadjustment));
351 && row_start < (gtk_adjustment_get_page_size (vadjustment) - row_height) / 2) {
352 /* For the first half of the top view we can't
353 * center the row in the view, instead we
354 * show the first page.
355 * - dy should be zero
357 g_assert (gtk_adjustment_get_value (vadjustment) == 0);
358 } else if (pos == POS_BOTTOM
359 && row_start + row_height >= gtk_adjustment_get_upper (vadjustment) - (gtk_adjustment_get_page_size (vadjustment) - row_height) / 2) {
360 /* For the last half of the bottom view we
361 * can't center the row in the view, instead
362 * we show the last page.
363 * - dy should be the start of the
366 g_assert (gtk_adjustment_get_value (vadjustment) == gtk_adjustment_get_upper (vadjustment) - gtk_adjustment_get_page_size (vadjustment));
368 /* The row is located in the middle of
370 * - top y coordinate is equal to
371 * middle of the view minus
372 * half the height of the row.
373 * (ie. the row's center is at the
374 * center of the view).
376 gdouble middle = (gtk_adjustment_get_page_size (vadjustment) - row_height) / 2.0;
377 g_assert (row_y == ceil (middle) || row_y == floor (middle));
383 if (pos == POS_TOP) {
384 /* The row can be anywhere on the
385 * first page of the tree view.
388 g_assert (gtk_adjustment_get_value (vadjustment) == 0);
389 } else if (pos == POS_CENTER || pos == POS_BOTTOM) {
390 /* The row is the last row visible in the
392 * - rect.y is set to the top of the
394 * - row_start is greater than page_size
395 * (ie we are not on the first page).
396 * - dy is greater than zero
398 g_assert (row_start >= gtk_adjustment_get_page_size (vadjustment)
399 || row_start + row_height >= gtk_adjustment_get_page_size (vadjustment));
400 g_assert (row_y == gtk_adjustment_get_page_size (vadjustment) - row_height);
407 assert_position_without_align (GtkTreeView *tree_view,
411 GtkAdjustment *vadjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (tree_view));
413 /* Without align the tree view does as less work as possible,
414 * so basically we only have to check whether the row
415 * is visible on the screen.
417 g_assert (gtk_adjustment_get_value (vadjustment) <= row_start);
418 g_assert (gtk_adjustment_get_value (vadjustment) + gtk_adjustment_get_page_size (vadjustment) >= row_start + row_height);
422 test_position (GtkTreeView *tree_view,
433 /* Get the location of the path we scrolled to */
434 gtk_tree_view_get_background_area (GTK_TREE_VIEW (tree_view),
437 row_start = get_row_start_for_index (GTK_TREE_VIEW (tree_view),
438 gtk_tree_path_get_indices (path)[0]);
441 pos = get_pos_from_path (GTK_TREE_VIEW (tree_view),
443 gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (tree_view)));
445 /* This is only tested for during test_single() */
446 model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));
447 if (gtk_tree_model_iter_n_children (model, NULL) == 1) {
448 GtkAllocation allocation;
449 GtkTreePath *tmppath;
451 /* Test nothing is dangling at the bottom; read
452 * description for test_single() for more information.
455 /* FIXME: hardcoded width */
456 gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
457 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (tree_view), 0, allocation.height - 30, &tmppath, NULL, NULL, NULL)) {
458 g_assert_not_reached ();
459 gtk_tree_path_free (tmppath);
463 path_str = gtk_tree_path_to_string (path);
465 assert_position_with_align (tree_view, pos, rect.y,
466 row_start, rect.height, row_align);
468 assert_position_without_align (tree_view, row_start, rect.height);
479 /* Testing scrolling to various positions with various alignments */
484 /* HACK: sleep for more than one frame, to give the paint clock
485 * time to prepare the new layout */
486 g_usleep (100 * 1000);
488 while (gtk_events_pending ())
489 gtk_main_iteration ();
493 scroll (ScrollFixture *fixture,
498 gtk_tree_view_set_cursor (GTK_TREE_VIEW (fixture->tree_view), path,
500 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fixture->tree_view),
502 use_align, row_align, 0.0);
504 gtk_widget_show_all (fixture->window);
507 test_position (GTK_TREE_VIEW (fixture->tree_view), path,
508 use_align, row_align);
512 scroll_no_align (ScrollFixture *fixture,
513 gconstpointer test_data)
517 path = gtk_tree_path_new_from_string (test_data);
518 scroll (fixture, path, FALSE, 0.0);
519 gtk_tree_path_free (path);
523 scroll_align_0_0 (ScrollFixture *fixture,
524 gconstpointer test_data)
528 path = gtk_tree_path_new_from_string (test_data);
529 scroll (fixture, path, TRUE, 0.0);
530 gtk_tree_path_free (path);
534 scroll_align_0_5 (ScrollFixture *fixture,
535 gconstpointer test_data)
539 path = gtk_tree_path_new_from_string (test_data);
540 scroll (fixture, path, TRUE, 0.5);
541 gtk_tree_path_free (path);
545 scroll_align_1_0 (ScrollFixture *fixture,
546 gconstpointer test_data)
550 path = gtk_tree_path_new_from_string (test_data);
551 scroll (fixture, path, TRUE, 1.0);
552 gtk_tree_path_free (path);
557 scroll_after_realize (ScrollFixture *fixture,
562 gtk_widget_show_all (fixture->window);
564 while (gtk_events_pending ())
565 gtk_main_iteration ();
567 gtk_tree_view_set_cursor (GTK_TREE_VIEW (fixture->tree_view), path,
569 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fixture->tree_view),
571 use_align, row_align, 0.0);
574 test_position (GTK_TREE_VIEW (fixture->tree_view), path,
575 use_align, row_align);
579 scroll_after_no_align (ScrollFixture *fixture,
580 gconstpointer test_data)
584 path = gtk_tree_path_new_from_string (test_data);
585 scroll_after_realize (fixture, path, FALSE, 0.0);
586 gtk_tree_path_free (path);
590 scroll_after_align_0_0 (ScrollFixture *fixture,
591 gconstpointer test_data)
595 path = gtk_tree_path_new_from_string (test_data);
596 scroll_after_realize (fixture, path, TRUE, 0.0);
597 gtk_tree_path_free (path);
601 scroll_after_align_0_5 (ScrollFixture *fixture,
602 gconstpointer test_data)
606 path = gtk_tree_path_new_from_string (test_data);
607 scroll_after_realize (fixture, path, TRUE, 0.5);
608 gtk_tree_path_free (path);
612 scroll_after_align_1_0 (ScrollFixture *fixture,
613 gconstpointer test_data)
617 path = gtk_tree_path_new_from_string (test_data);
618 scroll_after_realize (fixture, path, TRUE, 1.0);
619 gtk_tree_path_free (path);
624 scroll_both_realize (ScrollFixture *fixture,
631 gtk_widget_show_all (fixture->window);
634 end = gtk_tree_path_new_from_indices (999, -1);
636 gtk_tree_view_set_cursor (GTK_TREE_VIEW (fixture->tree_view), end,
638 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fixture->tree_view),
640 use_align, row_align, 0.0);
641 gtk_tree_path_free (end);
643 while (gtk_events_pending ())
644 gtk_main_iteration ();
646 /* Scroll to final position */
647 gtk_tree_view_set_cursor (GTK_TREE_VIEW (fixture->tree_view), path,
649 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fixture->tree_view),
651 use_align, row_align, 0.0);
654 test_position (GTK_TREE_VIEW (fixture->tree_view), path,
655 use_align, row_align);
659 scroll_both_no_align (ScrollFixture *fixture,
660 gconstpointer test_data)
664 path = gtk_tree_path_new_from_string (test_data);
665 scroll_both_realize (fixture, path, FALSE, 0.0);
666 gtk_tree_path_free (path);
670 scroll_both_align_0_0 (ScrollFixture *fixture,
671 gconstpointer test_data)
675 path = gtk_tree_path_new_from_string (test_data);
676 scroll_both_realize (fixture, path, TRUE, 0.0);
677 gtk_tree_path_free (path);
681 scroll_both_align_0_5 (ScrollFixture *fixture,
682 gconstpointer test_data)
686 path = gtk_tree_path_new_from_string (test_data);
687 scroll_both_realize (fixture, path, TRUE, 0.5);
688 gtk_tree_path_free (path);
692 scroll_both_align_1_0 (ScrollFixture *fixture,
693 gconstpointer test_data)
697 path = gtk_tree_path_new_from_string (test_data);
698 scroll_both_realize (fixture, path, TRUE, 1.0);
699 gtk_tree_path_free (path);
702 /* Testing scrolling to a newly created row */
704 create_new_row (GtkListStore *store,
711 gtk_list_store_prepend (store, iter);
715 /* Add a row in the middle of the visible area */
716 gtk_list_store_insert (store, iter, 3);
720 /* Add a row in the middle of the visible area */
721 gtk_list_store_insert (store, iter, 4);
725 /* Add a row which is not completely visible */
726 gtk_list_store_insert (store, iter, 5);
730 /* Add a row which is not completely visible */
731 gtk_list_store_insert (store, iter, 8);
735 /* Add a row in the middle */
736 gtk_list_store_insert (store, iter, 500);
741 gtk_list_store_append (store, iter);
745 gtk_list_store_set (store, iter, 0, "New...", -1);
749 scroll_new_row_editing_started (GtkCellRenderer *cell,
750 GtkCellEditable *editable,
754 GtkWidget **widget = user_data;
756 *widget = GTK_WIDGET (editable);
760 test_editable_position (GtkWidget *tree_view,
762 GtkTreePath *cursor_path)
764 GtkAllocation allocation;
767 gtk_tree_view_get_background_area (GTK_TREE_VIEW (tree_view),
768 cursor_path, NULL, &rect);
770 /* There are all in bin_window coordinates */
771 gtk_widget_get_allocation (editable, &allocation);
772 g_assert (allocation.y == rect.y + ((rect.height - allocation.height) / 2));
776 scroll_new_row (ScrollFixture *fixture,
777 gconstpointer test_data)
779 GtkTreeIter scroll_iter;
780 GtkTreePath *scroll_path;
783 GtkTreeViewColumn *column;
786 /* The aim of this test is creating a new row at several places,
787 * and immediately put the cursor on it. TreeView should correctly
788 * scroll to the row and show the editable widget.
793 g_test_bug ("81627");
795 gtk_widget_show_all (fixture->window);
797 while (gtk_events_pending ())
798 gtk_main_iteration ();
800 /* Create the new row and scroll to it */
801 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fixture->tree_view));
802 create_new_row (GTK_LIST_STORE (model), GPOINTER_TO_INT (test_data),
805 /* Set up a signal handler to acquire the editable widget */
806 column = gtk_tree_view_get_column (GTK_TREE_VIEW (fixture->tree_view), 0);
807 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
809 g_signal_connect (G_OBJECT (renderers->data), "editing-started",
810 G_CALLBACK (scroll_new_row_editing_started),
813 /* Now set the cursor on the path and start editing */
814 scroll_path = gtk_tree_model_get_path (model, &scroll_iter);
815 gtk_tree_view_set_cursor (GTK_TREE_VIEW (fixture->tree_view),
823 test_position (GTK_TREE_VIEW (fixture->tree_view), scroll_path,
825 test_editable_position (fixture->tree_view, editable, scroll_path);
827 gtk_tree_path_free (scroll_path);
831 scroll_new_row_tree (ScrollFixture *fixture,
832 gconstpointer test_data)
835 GtkAdjustment *vadjustment;
838 /* The goal of this test is to append new rows at the end of a tree
839 * store and immediately scroll to them. If there is a parent
840 * node with a couple of childs in the "area above" to explore,
841 * this used to lead to unexpected results due to a bug.
843 * This issue has been reported by Miroslav Rajcic on
844 * gtk-app-devel-list:
845 * http://mail.gnome.org/archives/gtk-app-devel-list/2008-December/msg00068.html
848 gtk_widget_show_all (fixture->window);
850 gtk_tree_view_expand_all (GTK_TREE_VIEW (fixture->tree_view));
852 while (gtk_events_pending ())
853 gtk_main_iteration ();
855 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fixture->tree_view));
856 vadjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (fixture->tree_view));
858 for (i = 0; i < 5; i++) {
859 GtkTreeIter scroll_iter;
860 GtkTreePath *scroll_path;
862 gtk_tree_store_append (GTK_TREE_STORE (model), &scroll_iter,
864 gtk_tree_store_set (GTK_TREE_STORE (model), &scroll_iter,
867 scroll_path = gtk_tree_model_get_path (model, &scroll_iter);
868 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fixture->tree_view),
869 scroll_path, NULL, FALSE, 0.0, 0.0);
870 gtk_tree_path_free (scroll_path);
874 /* Test position, the scroll bar must be at the end */
875 g_assert (gtk_adjustment_get_value (vadjustment) == gtk_adjustment_get_upper (vadjustment) - gtk_adjustment_get_page_size (vadjustment));
879 /* Test for GNOME bugzilla bug 359231; tests "recovery when removing a bunch of
880 * rows at the bottom.
883 test_bug316689 (ScrollFixture *fixture,
884 gconstpointer test_data)
888 GtkAdjustment *vadjustment;
891 /* The aim of this test is to scroll to the bottom of a TreeView,
892 * remove at least one page_size of items and check if TreeView
893 * correctly corrects the scroll bar (else they will look "broken").
898 g_test_bug ("316689");
900 /* Scroll to some place close to the end */
901 path = gtk_tree_path_new_from_indices (N_ROWS - 4, -1);
902 scroll (fixture, path, FALSE, 0.0);
903 gtk_tree_path_free (path);
905 /* No need for a while events pending loop here, scroll() does this for us.
907 * We now remove a bunch of rows, wait for events to process and then
908 * check the adjustments to see if the TreeView gracefully recovered.
910 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fixture->tree_view));
912 while (gtk_tree_model_iter_nth_child (model, &iter, NULL, N_ROWS - 15))
913 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
915 while (gtk_events_pending ())
916 gtk_main_iteration ();
918 vadjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (fixture->tree_view));
920 g_assert (gtk_adjustment_get_value (vadjustment) + gtk_adjustment_get_page_size (vadjustment) <= gtk_adjustment_get_upper (vadjustment));
921 g_assert (gtk_adjustment_get_value (vadjustment) == gtk_adjustment_get_upper (vadjustment) - gtk_adjustment_get_page_size (vadjustment));
925 /* Test for GNOME bugzilla bug 359231 */
927 test_bug359231 (void)
930 int height1, height2;
932 GtkTreeIter iter, child;
935 ScrollFixture *fixture;
938 g_test_bug ("359231");
940 /* Create model (GtkTreeStore in this case) */
941 store = gtk_tree_store_new (1, G_TYPE_STRING);
943 gtk_tree_store_append (store, &iter, NULL);
944 gtk_tree_store_set (store, &iter, 0, "Foo", -1);
946 for (i = 0; i < 4; i++) {
947 gtk_tree_store_append (store, &child, &iter);
948 gtk_tree_store_set (store, &child, 0, "Two\nLines", -1);
951 fixture = g_new0 (ScrollFixture, 1);
952 scroll_fixture_setup (fixture, GTK_TREE_MODEL (store), NULL);
953 gtk_widget_show_all (fixture->window);
955 while (gtk_events_pending ())
956 gtk_main_iteration ();
958 /* Prepend some rows at the top, expand */
959 gtk_tree_store_prepend (store, &iter, NULL);
960 gtk_tree_store_set (store, &iter, 0, "Foo", -1);
962 gtk_tree_store_prepend (store, &child, &iter);
963 gtk_tree_store_set (store, &child, 0, "Two\nLines", -1);
965 gtk_tree_view_expand_all (GTK_TREE_VIEW (fixture->tree_view));
967 while (gtk_events_pending ())
968 gtk_main_iteration ();
970 /* Test if height of row 0:0 is correct */
971 path = gtk_tree_path_new_from_indices (0, -1);
972 gtk_tree_view_get_background_area (GTK_TREE_VIEW (fixture->tree_view),
974 height1 = rect.height;
976 gtk_tree_path_down (path);
977 gtk_tree_view_get_background_area (GTK_TREE_VIEW (fixture->tree_view),
979 height2 = rect.height;
980 gtk_tree_path_free (path);
982 g_assert (height2 > height1);
984 /* Clean up; the tear down also cleans up the model */
985 scroll_fixture_teardown (fixture, NULL);
988 /* Test for GNOME bugzilla bug 93584. We add 150 rows to an existing
989 * small model, and scroll to one of these with alignment.
992 test_bug93584 (ScrollFixture *fixture,
993 gconstpointer test_data)
999 g_test_bug ("93584");
1001 /* Mimic state as in original test case */
1002 g_signal_connect (G_OBJECT (fixture->tree_view), "realize",
1003 G_CALLBACK (gtk_tree_view_expand_all), NULL);
1004 gtk_widget_show_all (fixture->window);
1006 store = GTK_TREE_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fixture->tree_view)));
1009 for (i = 0; i < 150; i++) {
1012 gtk_tree_store_append (store, &iter, NULL);
1013 gtk_tree_store_set (store, &iter, 0, "Row", -1);
1016 row = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
1019 while (gtk_events_pending ())
1020 gtk_main_iteration ();
1022 path = gtk_tree_path_new_from_indices (row, -1);
1023 scroll (fixture, path, TRUE, 0.5);
1024 gtk_tree_path_free (path);
1027 /* GNOME bugzilla bug 111500. Expand a row and immediately scroll
1028 * to its first child. Make sure that expansion happens in currently
1032 test_bug111500 (ScrollFixture *fixture,
1033 gconstpointer test_data)
1036 GtkTreeStore *store;
1040 g_test_bug ("111500");
1042 gtk_widget_show_all (fixture->window);
1044 /* Make sure all events have been processed and the window
1047 while (gtk_events_pending ())
1048 gtk_main_iteration ();
1050 /* Further prepare model */
1051 store = GTK_TREE_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fixture->tree_view)));
1053 for (i = 0; i < 15; i++) {
1056 gtk_tree_store_append (store, &iter, NULL);
1057 gtk_tree_store_set (store, &iter, 0, "Other node", -1);
1060 len = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
1061 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (store), &parent,
1064 for (i = 0; i < 5; i++) {
1067 gtk_tree_store_append (store, &iter, &parent);
1068 gtk_tree_store_set (store, &iter, 0, "Row", -1);
1071 path = gtk_tree_path_new_from_indices (len - 1, -1);
1072 gtk_tree_view_expand_row (GTK_TREE_VIEW (fixture->tree_view),
1075 gtk_tree_path_down (path);
1077 scroll (fixture, path, FALSE, 0.5);
1078 gtk_tree_path_free (path);
1082 test_bug111500_mixed (ScrollFixture *fixture,
1083 gconstpointer test_data)
1086 GtkTreeStore *store;
1090 g_test_bug ("111500");
1092 gtk_widget_show_all (fixture->window);
1094 /* Make sure all events have been processed and the window
1097 while (gtk_events_pending ())
1098 gtk_main_iteration ();
1100 /* Further prepare model */
1101 store = GTK_TREE_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fixture->tree_view)));
1103 for (i = 0; i < 15; i++) {
1106 gtk_tree_store_append (store, &iter, NULL);
1108 gtk_tree_store_set (store, &iter, 0, "Other node", -1);
1110 gtk_tree_store_set (store, &iter, 0, "Other\nnode", -1);
1113 len = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
1114 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (store), &parent,
1117 for (i = 0; i < 5; i++) {
1120 gtk_tree_store_append (store, &iter, &parent);
1122 gtk_tree_store_set (store, &iter, 0, "Row", -1);
1124 gtk_tree_store_set (store, &iter, 0, "Row\nRow", -1);
1127 path = gtk_tree_path_new_from_indices (len - 1, -1);
1128 gtk_tree_view_expand_row (GTK_TREE_VIEW (fixture->tree_view),
1131 gtk_tree_path_down (path);
1133 scroll (fixture, path, FALSE, 0.5);
1134 gtk_tree_path_free (path);
1137 /* Test for GNOME bugzilla bug 163214. Invalidate a couple of rows,
1138 * then scroll to one of these.
1141 test_bug163214 (ScrollFixture *fixture,
1142 gconstpointer test_data)
1145 GtkListStore *store;
1148 g_test_bug ("163214");
1150 gtk_widget_show_all (fixture->window);
1152 /* Make sure all events have been processed and the window
1155 while (gtk_events_pending ())
1156 gtk_main_iteration ();
1158 store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fixture->tree_view)));
1160 /* Invalidate a page of rows */
1161 for (i = 100; i < 110; i++) {
1164 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (store), &iter,
1166 gtk_list_store_set (store, &iter, 0, "Row", -1);
1169 /* Then scroll to that page. */
1170 path = gtk_tree_path_new_from_indices (105, -1);
1171 scroll (fixture, path, TRUE, 0.5);
1172 gtk_tree_path_free (path);
1174 /* Make sure all events have been processed and the window
1177 while (gtk_events_pending ())
1178 gtk_main_iteration ();
1180 store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fixture->tree_view)));
1182 /* Invalidate a page of rows */
1183 for (i = 300; i < 310; i++) {
1186 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (store), &iter,
1188 gtk_list_store_set (store, &iter, 0, "Row", -1);
1191 /* Then scroll to the first row */
1192 path = gtk_tree_path_new_from_indices (0, -1);
1193 scroll (fixture, path, TRUE, 0.5);
1194 gtk_tree_path_free (path);
1197 /* Infrastructure for automatically adding tests */
1206 test_type_string (int test_type)
1208 switch (test_type) {
1210 return "before-realize";
1213 return "after-realize";
1223 align_string (gboolean use_align,
1229 return g_strdup ("no-align");
1231 ret = g_strdup_printf ("align-%1.1f", row_align);
1236 add_test (const char *path,
1241 void (* setup) (ScrollFixture *, gconstpointer),
1242 void (* scroll_func) (ScrollFixture *, gconstpointer))
1247 align = align_string (use_align, row_align);
1249 test_path = g_strdup_printf ("/TreeView/scrolling/%s/%s-height/path-%s-%s",
1250 test_type_string (test_type),
1251 mixed ? "mixed" : "constant",
1255 g_test_add (test_path, ScrollFixture, path,
1256 setup, scroll_func, scroll_fixture_teardown);
1262 add_tests (gboolean mixed,
1266 void (*scroll_func) (ScrollFixture *, gconstpointer))
1268 void (* setup) (ScrollFixture *, gconstpointer);
1271 setup = scroll_fixture_mixed_setup;
1273 setup = scroll_fixture_constant_setup;
1275 add_test ("0", mixed, test_type, use_align, row_align, setup, scroll_func);
1276 add_test ("2", mixed, test_type, use_align, row_align, setup, scroll_func);
1277 add_test ("5", mixed, test_type, use_align, row_align, setup, scroll_func);
1278 /* We scroll to 8 to test a partial visible row. The 8 is
1279 * based on my font setting of "Vera Sans 11" and
1280 * the separators set to 0. (This should be made dynamic; FIXME).
1282 add_test ("8", mixed, test_type, use_align, row_align, setup, scroll_func);
1283 add_test ("10", mixed, test_type, use_align, row_align, setup, scroll_func);
1284 add_test ("250", mixed, test_type, use_align, row_align, setup, scroll_func);
1285 add_test ("500", mixed, test_type, use_align, row_align, setup, scroll_func);
1286 add_test ("750", mixed, test_type, use_align, row_align, setup, scroll_func);
1287 add_test ("990", mixed, test_type, use_align, row_align, setup, scroll_func);
1288 add_test ("991", mixed, test_type, use_align, row_align, setup, scroll_func);
1289 add_test ("995", mixed, test_type, use_align, row_align, setup, scroll_func);
1290 add_test ("997", mixed, test_type, use_align, row_align, setup, scroll_func);
1291 add_test ("999", mixed, test_type, use_align, row_align, setup, scroll_func);
1295 main (int argc, char **argv)
1297 gtk_test_init (&argc, &argv);
1299 /* Scrolls before realization */
1300 add_tests (FALSE, BEFORE, FALSE, 0.0, scroll_no_align);
1301 if (g_test_thorough ())
1302 add_tests (TRUE, BEFORE, FALSE, 0.0, scroll_no_align);
1304 add_tests (FALSE, BEFORE, TRUE, 0.0, scroll_align_0_0);
1305 if (g_test_thorough ())
1306 add_tests (TRUE, BEFORE, TRUE, 0.0, scroll_align_0_0);
1308 add_tests (FALSE, BEFORE, TRUE, 0.5, scroll_align_0_5);
1309 if (g_test_thorough ())
1310 add_tests (TRUE, BEFORE, TRUE, 0.5, scroll_align_0_5);
1312 add_tests (FALSE, BEFORE, TRUE, 1.0, scroll_align_1_0);
1313 if (g_test_thorough ())
1314 add_tests (TRUE, BEFORE, TRUE, 1.0, scroll_align_1_0);
1316 /* Scrolls after realization */
1317 add_tests (FALSE, AFTER, FALSE, 0.0, scroll_after_no_align);
1318 if (g_test_thorough ())
1319 add_tests (TRUE, AFTER, FALSE, 0.0, scroll_after_no_align);
1321 add_tests (FALSE, AFTER, TRUE, 0.0, scroll_after_align_0_0);
1322 if (g_test_thorough ())
1323 add_tests (TRUE, AFTER, TRUE, 0.0, scroll_after_align_0_0);
1325 add_tests (FALSE, AFTER, TRUE, 0.5, scroll_after_align_0_5);
1326 if (g_test_thorough ())
1327 add_tests (TRUE, AFTER, TRUE, 0.5, scroll_after_align_0_5);
1329 add_tests (FALSE, AFTER, TRUE, 1.0, scroll_after_align_1_0);
1330 if (g_test_thorough ())
1331 add_tests (TRUE, AFTER, TRUE, 1.0, scroll_after_align_1_0);
1333 /* Scroll to end before realization, to a real position after */
1334 if (g_test_thorough ()) {
1335 add_tests (FALSE, BOTH, FALSE, 0.0, scroll_both_no_align);
1336 add_tests (TRUE, BOTH, FALSE, 0.0, scroll_both_no_align);
1338 add_tests (FALSE, BOTH, TRUE, 0.0, scroll_both_align_0_0);
1339 add_tests (TRUE, BOTH, TRUE, 0.0, scroll_both_align_0_0);
1341 add_tests (FALSE, BOTH, TRUE, 0.5, scroll_both_align_0_5);
1342 add_tests (TRUE, BOTH, TRUE, 0.5, scroll_both_align_0_5);
1344 add_tests (FALSE, BOTH, TRUE, 1.0, scroll_both_align_1_0);
1345 add_tests (TRUE, BOTH, TRUE, 1.0, scroll_both_align_1_0);
1348 /* Test different alignments in view with single row */
1349 g_test_add ("/TreeView/scrolling/single-row/no-align",
1351 scroll_fixture_single_setup,
1353 scroll_fixture_teardown);
1354 g_test_add ("/TreeView/scrolling/single-row/align-0.0",
1356 scroll_fixture_single_setup,
1358 scroll_fixture_teardown);
1359 g_test_add ("/TreeView/scrolling/single-row/align-0.5",
1361 scroll_fixture_single_setup,
1363 scroll_fixture_teardown);
1364 g_test_add ("/TreeView/scrolling/single-row/align-1.0",
1366 scroll_fixture_single_setup,
1368 scroll_fixture_teardown);
1370 /* Test scrolling in a very large model; also very slow */
1371 if (g_test_slow ()) {
1372 g_test_add ("/TreeView/scrolling/large-model/constant-height/middle-no-align",
1373 ScrollFixture, "50000",
1374 scroll_fixture_constant_big_setup,
1376 scroll_fixture_teardown);
1377 g_test_add ("/TreeView/scrolling/large-model/constant-height/end-no-align",
1378 ScrollFixture, "99999",
1379 scroll_fixture_constant_big_setup,
1381 scroll_fixture_teardown);
1383 g_test_add ("/TreeView/scrolling/large-model/mixed-height/middle-no-align",
1384 ScrollFixture, "50000",
1385 scroll_fixture_mixed_big_setup,
1387 scroll_fixture_teardown);
1388 g_test_add ("/TreeView/scrolling/large-model/mixed-height/end-no-align",
1389 ScrollFixture, "99999",
1390 scroll_fixture_mixed_big_setup,
1392 scroll_fixture_teardown);
1395 /* Test scrolling to a newly created row */
1396 g_test_add ("/TreeView/scrolling/new-row/path-0", ScrollFixture,
1397 GINT_TO_POINTER (0),
1398 scroll_fixture_constant_setup,
1400 scroll_fixture_teardown);
1401 g_test_add ("/TreeView/scrolling/new-row/path-4", ScrollFixture,
1402 GINT_TO_POINTER (4),
1403 scroll_fixture_constant_setup,
1405 scroll_fixture_teardown);
1406 /* We scroll to 8 to test a partial visible row. The 8 is
1407 * based on my font setting of "Vera Sans 11" and
1408 * the separators set to 0. (This should be made dynamic; FIXME).
1410 g_test_add ("/TreeView/scrolling/new-row/path-8", ScrollFixture,
1411 GINT_TO_POINTER (8),
1412 scroll_fixture_constant_setup,
1414 scroll_fixture_teardown);
1415 g_test_add ("/TreeView/scrolling/new-row/path-500", ScrollFixture,
1416 GINT_TO_POINTER (500),
1417 scroll_fixture_constant_setup,
1419 scroll_fixture_teardown);
1420 g_test_add ("/TreeView/scrolling/new-row/path-999", ScrollFixture,
1421 GINT_TO_POINTER (999),
1422 scroll_fixture_constant_setup,
1424 scroll_fixture_teardown);
1426 g_test_add ("/TreeView/scrolling/new-row/tree", ScrollFixture,
1428 scroll_fixture_tree_setup,
1429 scroll_new_row_tree,
1430 scroll_fixture_teardown);
1432 /* Test scrolling to a newly created row, in a mixed height model */
1433 g_test_add ("/TreeView/scrolling/new-row-mixed/path-0", ScrollFixture,
1434 GINT_TO_POINTER (0),
1435 scroll_fixture_mixed_setup,
1437 scroll_fixture_teardown);
1438 g_test_add ("/TreeView/scrolling/new-row-mixed/path-3", ScrollFixture,
1439 GINT_TO_POINTER (3),
1440 scroll_fixture_mixed_setup,
1442 scroll_fixture_teardown);
1443 /* We scroll to 8 to test a partial visible row. The 8 is
1444 * based on my font setting of "Vera Sans 11" and
1445 * the separators set to 0. (This should be made dynamic; FIXME).
1447 g_test_add ("/TreeView/scrolling/new-row-mixed/path-5", ScrollFixture,
1448 GINT_TO_POINTER (5),
1449 scroll_fixture_mixed_setup,
1451 scroll_fixture_teardown);
1452 g_test_add ("/TreeView/scrolling/new-row-mixed/path-500", ScrollFixture,
1453 GINT_TO_POINTER (500),
1454 scroll_fixture_mixed_setup,
1456 scroll_fixture_teardown);
1457 g_test_add ("/TreeView/scrolling/new-row-mixed/path-999", ScrollFixture,
1458 GINT_TO_POINTER (999),
1459 scroll_fixture_mixed_setup,
1461 scroll_fixture_teardown);
1463 g_test_add ("/TreeView/scrolling/new-row-mixed/tree", ScrollFixture,
1465 scroll_fixture_mixed_tree_setup,
1466 scroll_new_row_tree,
1467 scroll_fixture_teardown);
1470 g_test_add ("/TreeView/scrolling/specific/bug-316689",
1471 ScrollFixture, NULL,
1472 scroll_fixture_constant_setup, test_bug316689,
1473 scroll_fixture_teardown);
1474 g_test_add_func ("/TreeView/scrolling/specific/bug-359231",
1476 g_test_add ("/TreeView/scrolling/specific/bug-93584",
1477 ScrollFixture, NULL,
1478 scroll_fixture_tree_setup, test_bug93584,
1479 scroll_fixture_teardown);
1480 g_test_add ("/TreeView/scrolling/specific/bug-111500",
1481 ScrollFixture, NULL,
1482 scroll_fixture_tree_setup, test_bug111500,
1483 scroll_fixture_teardown);
1484 g_test_add ("/TreeView/scrolling/specific/bug-111500-mixed",
1485 ScrollFixture, NULL,
1486 scroll_fixture_mixed_tree_setup, test_bug111500_mixed,
1487 scroll_fixture_teardown);
1488 g_test_add ("/TreeView/scrolling/specific/bug-163214",
1489 ScrollFixture, NULL,
1490 scroll_fixture_constant_setup, test_bug163214,
1491 scroll_fixture_teardown);
1493 return g_test_run ();