1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; coding: utf-8 -*-
2  *
3  * This file contains tests that validates that the size allocation
4  * signal is handled properly and that the attributes of the
5  * GtkImageView is updated like they should.
6  **/
7 #include <src/gtkimageview.h>
8 #include <src/gtkzooms.h>
9 #include <assert.h>
10 #include "testlib/testlib.h"
11 
12 /**
13  * test_that_image_is_fitted_after_size_changes:
14  *
15  * Test that a pixbuf fitted before a size allocation remains fitted
16  * after the size allocation has taken place.
17  **/
18 static void
test_that_image_is_fitted_after_size_changes()19 test_that_image_is_fitted_after_size_changes ()
20 {
21     printf ("test_that_image_is_fitted_after_size_changes\n");
22     int allocated_sizes[] = {
23         500, 500,
24         50, 50,
25         12, 33,
26         99, 70,
27         249, 120,
28         1517, 124,
29         1, 1
30     };
31     int image_sizes[] = {
32         50, 50,
33         45, 99,
34         33, 22,
35         115, 317,
36         1, 1
37     };
38     GtkImageView *view = (GtkImageView *) gtk_image_view_new ();
39     for (int i = 0; i < G_N_ELEMENTS (image_sizes); i += 2)
40     {
41         int img_width = image_sizes[i];
42         int img_height = image_sizes[i + 1];
43         GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
44                                             img_width, img_height);
45         gtk_image_view_set_pixbuf (view, pixbuf, TRUE);
46         for (int j = 0; j < G_N_ELEMENTS (allocated_sizes); j += 2)
47         {
48             int alloc_width = allocated_sizes[j];
49             int alloc_height = allocated_sizes[j + 1];
50             GtkAllocation alloc = {0, 0, alloc_width, alloc_height};
51             gtk_widget_size_allocate (GTK_WIDGET (view), &alloc);
52 
53             gdouble width_ratio = (gdouble) alloc_width / img_width;
54             gdouble height_ratio = (gdouble) alloc_height / img_height;
55 
56             gdouble expected_zoom = MIN (width_ratio, height_ratio);
57 			expected_zoom = CLAMP (expected_zoom,
58                                    gtk_zooms_get_min_zoom (),
59                                    1.0);
60 
61             assert (gtk_image_view_get_zoom (view) == expected_zoom);
62 
63         }
64         g_object_unref (pixbuf);
65     }
66 }
67 
68 /**
69  * test_size_allocate_at_image_boundary:
70  *
71  * The objective of this test is to verify that a size allocate event
72  * that occurs when the image is scrolled to an edge, causes the image
73  * offset to change.
74  **/
75 static void
test_size_allocate_at_image_boundary()76 test_size_allocate_at_image_boundary ()
77 {
78 	printf ("test_size_allocate_at_image_boundary\n");
79 
80 	GtkWidget *view = GTK_WIDGET (gtk_image_view_new ());
81 	g_object_ref (view);
82     gtk_object_sink (GTK_OBJECT (view));
83 
84 	fake_realize (view);
85 
86 	GtkAllocation alloc = {0, 0, 200, 200};
87 	gtk_widget_size_allocate (view, &alloc);
88 
89 	GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
90 										500, 500);
91 
92 	gtk_image_view_set_pixbuf (GTK_IMAGE_VIEW (view), pixbuf, TRUE);
93 	gtk_image_view_set_zoom (GTK_IMAGE_VIEW (view), 1.0);
94 
95 	/* Create the scroll adjustments, each has the range [0-500]. */
96 	GtkObject *hadj = gtk_adjustment_new (0.0, 0.0, 500.0,
97 										  20.0, 100.0, 200.0);
98 	GtkObject *vadj = gtk_adjustment_new (0.0, 0.0, 500.0,
99 										  20.0, 100.0, 200.0);
100 	gtk_widget_set_scroll_adjustments (view,
101 									   GTK_ADJUSTMENT (hadj),
102 									   GTK_ADJUSTMENT (vadj));
103 
104 	/* Scroll the adjustment to the right edge of the image. */
105 	g_assert (hadj);
106 	gtk_adjustment_set_value (GTK_ADJUSTMENT (hadj), 300.0);
107 
108 
109     GMainContext *main_ctx = g_main_context_default ();
110     while (g_main_context_pending (main_ctx))
111     {
112         g_main_context_iteration (main_ctx, TRUE);
113         g_usleep (1000 * 100);
114     }
115 
116     /* The viewport should now match the rectangle (300, 0)-(200, 200) */
117 	GdkRectangle viewport;
118 	gtk_image_view_get_viewport (GTK_IMAGE_VIEW (view), &viewport);
119 
120     assert (gdk_rectangle_eq (viewport, (GdkRectangle){300, 0, 200, 200}));
121 
122 	/* Now expand the widget by making its width 100 pixels
123 	   wider. This decreased the x-coordinate of the viewport by 100
124 	   and increased the width by the same amount. */
125 	alloc = (GtkAllocation){0, 0, 300, 200};
126 	gtk_widget_size_allocate (view, &alloc);
127 
128 	gtk_image_view_get_viewport (GTK_IMAGE_VIEW (view), &viewport);
129 
130     assert (gdk_rectangle_eq (viewport, (GdkRectangle){200, 0, 300, 200}));
131 
132 	g_object_unref (pixbuf);
133 	gtk_widget_destroy (view);
134     g_object_unref (view);
135 }
136 
137 int
main(int argc,char * argv[])138 main (int argc, char *argv[])
139 {
140     gtk_init (&argc, &argv);
141     test_that_image_is_fitted_after_size_changes ();
142 	test_size_allocate_at_image_boundary ();
143     printf ("2 tests passed.\n");
144 }
145