gtk2/testsuite/gdk/texture.c
Benjamin Otte 2e555db9a4 testsuite: Fail more accurately
Don't run the full test and then return "something failed" - instead
just explode when you find the failure.
2021-10-06 22:50:07 +02:00

123 lines
3.1 KiB
C

#include <gtk.h>
static void
compare_pixels (int width,
int height,
guchar *data1,
gsize stride1,
guchar *data2,
gsize stride2)
{
int x, y;
for (y = 0; y < height; y++)
{
const guint32 *p1 = (const guint32*) (data1 + y * stride1);
const guint32 *p2 = (const guint32*) (data2 + y * stride2);
for (x = 0; x < width; x++)
{
g_assert_cmphex (p1[x], ==, p2[x]);
}
}
}
static void
test_texture_from_pixbuf (void)
{
GdkPixbuf *pixbuf;
GdkTexture *texture;
GError *error = NULL;
guchar *data;
int width, height;
gsize stride;
cairo_surface_t *surface;
cairo_t *cr;
pixbuf = gdk_pixbuf_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png", &error);
g_assert_no_error (error);
g_assert_nonnull (pixbuf);
g_assert_true (gdk_pixbuf_get_has_alpha (pixbuf));
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
texture = gdk_texture_new_for_pixbuf (pixbuf);
g_assert_nonnull (texture);
g_assert_cmpint (gdk_texture_get_width (texture), ==, width);
g_assert_cmpint (gdk_texture_get_height (texture), ==, height);
stride = 4 * width;
data = g_new0 (guchar, stride * height);
gdk_texture_download (texture, data, stride);
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
cr = cairo_create (surface);
gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
compare_pixels (width, height,
data, stride,
cairo_image_surface_get_data (surface),
cairo_image_surface_get_stride (surface));
g_free (data);
g_object_unref (pixbuf);
g_object_unref (texture);
cairo_surface_destroy (surface);
}
static void
test_texture_from_resource (void)
{
GdkTexture *texture;
int width, height;
texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png");
g_assert_nonnull (texture);
g_object_get (texture,
"width", &width,
"height", &height,
NULL);
g_assert_cmpint (width, ==, 16);
g_assert_cmpint (height, ==, 16);
g_object_unref (texture);
}
static void
test_texture_save_to_png (void)
{
GdkTexture *texture;
GError *error = NULL;
GFile *file;
GdkTexture *texture2;
texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png");
gdk_texture_save_to_png (texture, "test.png");
file = g_file_new_for_path ("test.png");
texture2 = gdk_texture_new_from_file (file, &error);
g_object_unref (file);
g_assert_no_error (error);
g_object_unref (texture);
g_object_unref (texture2);
}
int
main (int argc, char *argv[])
{
/* We want to use resources from libgtk, so we need gtk initialized */
gtk_test_init (&argc, &argv, NULL);
g_test_add_func ("/texture/from-pixbuf", test_texture_from_pixbuf);
g_test_add_func ("/texture/from-resource", test_texture_from_resource);
g_test_add_func ("/texture/save-to-png", test_texture_save_to_png);
return g_test_run ();
}