root/gtkimageview/tests/test-gdk-pixbuf-draw-cache.c

Revision 390, 5.7 kB (checked in by bjourne, 6 years ago)

Renaming

Line 
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 GdkPixbufDrawCache
4  * class works correctly.
5  **/
6 #include <assert.h>
7 #include <src/gtkimageview.h>
8
9 /**
10  * test_only_scale_op_on_new_identical_pixbuf:
11  *
12  * The objective of this test is to verify that the cached pixbuf that
13  * the GdkPixbufDrawCache holds is not discarded when it receives a new
14  * pixbuf with the exact same bit depth and colorspace.
15  **/
16 static void
17 test_only_scale_op_on_new_identical_pixbuf ()
18 {
19     printf ("test_only_scale_op_on_new_identical_pixbuf\n");
20
21     GdkPixbuf *pixbuf1 = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 9, 9);
22     GdkPixbuf *pixbuf2 = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 9, 9);
23    
24     int interp = GDK_INTERP_BILINEAR;
25     GdkRectangle area = {0, 0, 10, 10};
26    
27     GdkPixbufDrawOpts o1 = {1, area, 0, 0, interp, pixbuf1, 0, 0};
28     GdkPixbufDrawOpts o2 = {1, area, 0, 0, interp, pixbuf2, 0, 0};
29
30     GdkPixbufDrawMethod meth = gdk_pixbuf_draw_cache_get_method (&o1, &o2);
31     assert (meth == GDK_PIXBUF_DRAW_METHOD_SCALE);
32
33     g_object_unref (pixbuf1);
34     g_object_unref (pixbuf2);
35 }
36
37 /**
38  * test_cache_is_used_on_equal_opts:
39  *
40  * The objective of this test is to verify that the cache is used when
41  * the new draw options are identical to the old ones if the area is
42  * smaller than the cached pixbuf. If so, the #GdkPixbufDrawMethod
43  * should be %GDK_PIXBUF_DRAW_METHOD_CONTAINS.
44  **/
45 static void
46 test_cache_is_used_on_equal_opts ()
47 {
48     printf ("test_cache_is_used_on_equal_opts\n");
49     int interp = GDK_INTERP_BILINEAR;
50     GdkRectangle area = {0, 0, 10, 10};
51     GdkPixbuf *pb = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 30, 30);
52     GdkPixbufDrawOpts o1 = {1, area, 0, 0, interp, pb, 0, 0};
53     GdkPixbufDrawOpts o2 = {1, area, 0, 0, interp, pb, 0, 0};
54
55     GdkPixbufDrawMethod meth = gdk_pixbuf_draw_cache_get_method (&o1, &o2);
56     assert (meth == GDK_PIXBUF_DRAW_METHOD_CONTAINS);
57
58     g_object_unref (pb);
59 }
60
61 /**
62  * test_contains_even_if_cache_to_small:
63  *
64  * The objective of this test is to verfy that the method is
65  * %GDK_PIXBUF_DRAW_METHOD_CONTAINS even if the cache is smaller than the
66  * requsted draw area.
67  **/
68 static void
69 test_contains_even_if_cache_to_small ()
70 {
71     printf ("test_contains_even_if_cache_to_small\n");
72     int interp = GDK_INTERP_BILINEAR;
73     GdkRectangle area = {0, 0, 20, 20};
74    
75     GdkPixbuf *draw_pb = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE,
76                                          8, 30, 30);
77     GdkPixbufDrawOpts o1 = {1, area, 0, 0, interp, draw_pb, 0, 0};
78     GdkPixbufDrawOpts o2 = {1, area, 0, 0, interp, draw_pb, 0, 0};
79
80     GdkPixbufDrawMethod meth = gdk_pixbuf_draw_cache_get_method (&o1, &o2);
81     assert (meth == GDK_PIXBUF_DRAW_METHOD_CONTAINS);
82     g_object_unref (draw_pb);
83 }
84
85
86 /**
87  * test_scroll_needed_if_rect_size_not_equal:
88  *
89  * The objective of this test is to verify that a scroll operation is
90  * needed if the size of the rectangle changes.
91  **/
92 static void
93 test_scroll_needed_if_rect_size_not_equal ()
94 {
95     printf ("test_scroll_needed_if_rect_size_not_equal\n");
96     int interp = GDK_INTERP_BILINEAR;
97     GdkPixbuf *pb = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 9, 9);
98     GdkPixbufDrawOpts o1 = {1, (GdkRectangle){0, 0, 20, 20},
99                            0, 0, interp, pb, 0, 0};
100     GdkPixbufDrawOpts o2 = {1, (GdkRectangle){20, 0, 10, 10},
101                            0, 0, interp, pb, 0, 0};
102
103     GdkPixbufDrawMethod meth = gdk_pixbuf_draw_cache_get_method (&o1, &o2);
104     assert (meth == GDK_PIXBUF_DRAW_METHOD_SCROLL);
105
106     g_object_unref (pb);
107 }
108
109 /**
110  * test_default_draw_options:
111  *
112  * The objective of this test is to verify that the default draw
113  * options in the pixbuf draw cache results in the correct draw
114  * method.
115  **/
116 static void
117 test_default_draw_options ()
118 {
119     printf ("test_default_draw_options\n");
120     GdkPixbufDrawCache *cache = gdk_pixbuf_draw_cache_new ();
121     int interp = GDK_INTERP_BILINEAR;
122     GdkPixbuf *pb = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 9, 9);
123     GdkPixbufDrawOpts o = {1, (GdkRectangle){0, 0, 20, 20},
124                            0, 0, interp, pb, 0, 0};
125     GdkPixbufDrawMethod meth =
126         gdk_pixbuf_draw_cache_get_method (&cache->old, &o);
127     assert (meth == GDK_PIXBUF_DRAW_METHOD_SCALE);
128    
129     gdk_pixbuf_draw_cache_free (cache);
130     g_object_unref (pb);
131 }
132
133 /**
134  * test_invalidate:
135  *
136  * The objective of this test is to verify that forcing the
137  * GdkPixbufDrawCache to apply a scale operation by invalidating works
138  * as expected.
139  **/
140 static void
141 test_invalidate ()
142 {
143     printf ("test_invalidate\n");
144     GdkPixbufDrawCache *cache = gdk_pixbuf_draw_cache_new ();
145    
146     GdkInterpType interp = GDK_INTERP_BILINEAR;
147     GdkPixbuf *pb = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 9, 9);
148     GdkPixbufDrawOpts opts = {1, (GdkRectangle){0, 0, 5, 5},
149                               0, 0, interp, pb, 0, 0};
150     cache->old = opts;
151
152
153     /* GdkPixbufDrawOpts are identical, so no allocation or scale
154        necessary. */
155     assert (gdk_pixbuf_draw_cache_get_method (&cache->old, &opts)
156             == GDK_PIXBUF_DRAW_METHOD_CONTAINS);
157
158     /* Force a scale operation in the next draw. */
159     gdk_pixbuf_draw_cache_invalidate (cache);
160
161     assert (gdk_pixbuf_draw_cache_get_method (&cache->old, &opts) ==
162             GDK_PIXBUF_DRAW_METHOD_SCALE);
163    
164     gdk_pixbuf_draw_cache_free (cache);
165     g_object_unref (pb);
166 }
167
168 int
169 main(int argc, char *argv[])
170 {
171     gtk_init (&argc, &argv);
172     test_only_scale_op_on_new_identical_pixbuf ();
173     test_cache_is_used_on_equal_opts ();
174     test_contains_even_if_cache_to_small ();
175     test_scroll_needed_if_rect_size_not_equal ();
176     test_default_draw_options ();
177     test_invalidate ();
178     printf ("6 tests passed.\n");
179 }
Note: See TracBrowser for help on using the browser.