1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* CdkPixbuf library - tests for CdkPixbuf constructors
 *
 * Copyright (C) 2018 Federico Mena Quintero
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
#include "config.h"
#include "cdk-pixbuf/cdk-pixbuf.h"
#include "test-common.h"

static void
test_no_construct_properties (void)
{
  CdkPixbuf *pixbuf = g_object_new (CDK_TYPE_PIXBUF, NULL);
  GBytes *bytes;
  guchar *pixels;

  g_assert_cmpint (cdk_pixbuf_get_width (pixbuf), ==, 1);<--- syntax error
  g_assert_cmpint (cdk_pixbuf_get_height (pixbuf), ==, 1);

  g_object_get (pixbuf, "pixel-bytes", &bytes, NULL);
  g_assert_nonnull (bytes);
  g_bytes_unref (bytes);

  g_object_get (pixbuf, "pixels", &pixels, NULL);
  g_assert (pixels != NULL);

  g_object_unref (pixbuf);
}

#define WIDTH 10
#define HEIGHT 20
#define BUFFER_SIZE (WIDTH * HEIGHT * 4)
#define ROWSTRIDE (WIDTH * 4)

static void
test_pixels (void)
{
  guchar *pixels = g_new0 (guchar, BUFFER_SIZE);

  CdkPixbuf *pixbuf = g_object_new (CDK_TYPE_PIXBUF,
				    "width", WIDTH,
				    "height", HEIGHT,
				    "rowstride", ROWSTRIDE,
				    "bits-per-sample", 8,
				    "n-channels", 3,
				    "has-alpha", TRUE,
				    "pixels", pixels,
				    NULL);

  g_assert (cdk_pixbuf_get_pixels (pixbuf) == pixels);
  g_assert_cmpint (cdk_pixbuf_get_width (pixbuf), ==, WIDTH);
  g_assert_cmpint (cdk_pixbuf_get_height (pixbuf), ==, HEIGHT);
  g_assert_cmpint (cdk_pixbuf_get_rowstride (pixbuf), ==, ROWSTRIDE);

  g_object_unref (pixbuf);
  g_free (pixels);
}

static void
test_bytes (void)
{
  guchar *pixels = g_new0 (guchar, BUFFER_SIZE);
  GBytes *bytes = g_bytes_new_take (pixels, BUFFER_SIZE);
  CdkPixbuf *pixbuf = g_object_new (CDK_TYPE_PIXBUF,
				    "width", WIDTH,
				    "height", HEIGHT,
				    "rowstride", ROWSTRIDE,
				    "bits-per-sample", 8,
				    "n-channels", 3,
				    "has-alpha", TRUE,
				    "pixel-bytes", bytes,
				    NULL);

  GBytes *read_bytes = cdk_pixbuf_read_pixel_bytes (pixbuf);

  g_assert (read_bytes == bytes);
  g_assert_cmpint (cdk_pixbuf_get_width (pixbuf), ==, WIDTH);
  g_assert_cmpint (cdk_pixbuf_get_height (pixbuf), ==, HEIGHT);
  g_assert_cmpint (cdk_pixbuf_get_rowstride (pixbuf), ==, ROWSTRIDE);

  g_bytes_unref (read_bytes);
  g_object_unref (pixbuf);
  g_bytes_unref (bytes);
}

int
main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/pixbuf/construction/no_construct_properties", test_no_construct_properties);
  g_test_add_func ("/pixbuf/construction/pixels", test_pixels);
  g_test_add_func ("/pixbuf/construction/bytes", test_bytes);

  return g_test_run ();
}