~ubuntu-branches/ubuntu/hardy/cairo/hardy-updates

« back to all changes in this revision

Viewing changes to test/create-from-png.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-01-17 13:00:59 UTC
  • Revision ID: james.westby@ubuntu.com-20080117130059-3gbudaudr2w8bl4w
Tags: upstream-1.5.6
Import upstream version 1.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2005 Red Hat, Inc.
 
3
 *
 
4
 * Permission to use, copy, modify, distribute, and sell this software
 
5
 * and its documentation for any purpose is hereby granted without
 
6
 * fee, provided that the above copyright notice appear in all copies
 
7
 * and that both that copyright notice and this permission notice
 
8
 * appear in supporting documentation, and that the name of
 
9
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
 
10
 * distribution of the software without specific, written prior
 
11
 * permission. Red Hat, Inc. makes no representations about the
 
12
 * suitability of this software for any purpose.  It is provided "as
 
13
 * is" without express or implied warranty.
 
14
 *
 
15
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
17
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
 
18
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 
19
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 
20
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 
21
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
22
 *
 
23
 * Author: Carl Worth <cworth@cworth.org>
 
24
 */
 
25
 
 
26
#include "cairo-test.h"
 
27
 
 
28
#include <stdlib.h>
 
29
 
 
30
#define WIDTH 2
 
31
#define HEIGHT 2
 
32
 
 
33
static cairo_test_draw_function_t draw;
 
34
 
 
35
cairo_test_t test = {
 
36
    "create-from-png",
 
37
    "Tests the creation of an image surface from a PNG file",
 
38
    WIDTH, HEIGHT,
 
39
    draw
 
40
};
 
41
 
 
42
static cairo_status_t
 
43
no_memory_error (void *closure, unsigned char *data, unsigned int size)
 
44
{
 
45
    return CAIRO_STATUS_NO_MEMORY;
 
46
}
 
47
 
 
48
static cairo_status_t
 
49
read_error (void *closure, unsigned char *data, unsigned int size)
 
50
{
 
51
    return CAIRO_STATUS_READ_ERROR;
 
52
}
 
53
 
 
54
static cairo_test_status_t
 
55
draw (cairo_t *cr, int width, int height)
 
56
{
 
57
    char *srcdir = getenv ("srcdir");
 
58
    char *filename;
 
59
    cairo_surface_t *surface;
 
60
 
 
61
    xasprintf (&filename, "%s/%s", srcdir ? srcdir : ".",
 
62
               "create-from-png-ref.png");
 
63
 
 
64
    surface = cairo_image_surface_create_from_png (filename);
 
65
    if (cairo_surface_status (surface)) {
 
66
        cairo_test_log ("Error reading PNG image %s: %s\n",
 
67
                        filename,
 
68
                        cairo_status_to_string (cairo_surface_status (surface)));
 
69
        free (filename);
 
70
        return CAIRO_TEST_FAILURE;
 
71
    }
 
72
    free (filename);
 
73
 
 
74
    cairo_set_source_surface (cr, surface, 0, 0);
 
75
    cairo_paint (cr);
 
76
 
 
77
    cairo_surface_destroy (surface);
 
78
 
 
79
    return CAIRO_TEST_SUCCESS;
 
80
}
 
81
 
 
82
int
 
83
main (void)
 
84
{
 
85
    char *srcdir = getenv ("srcdir");
 
86
    char *filename;
 
87
    cairo_surface_t *surface;
 
88
    cairo_status_t status;
 
89
 
 
90
    surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
 
91
    if (cairo_surface_status (surface) != CAIRO_STATUS_FILE_NOT_FOUND) {
 
92
        cairo_test_log ("Error: expected \"file not found\", but got: %s\n",
 
93
                        cairo_status_to_string (cairo_surface_status (surface)));
 
94
        cairo_surface_destroy (surface);
 
95
        return CAIRO_TEST_FAILURE;
 
96
    }
 
97
 
 
98
    surface = cairo_image_surface_create_from_png_stream (no_memory_error, NULL);
 
99
    if (cairo_surface_status (surface) != CAIRO_STATUS_NO_MEMORY) {
 
100
        cairo_test_log ("Error: expected \"out of memory\", but got: %s\n",
 
101
                        cairo_status_to_string (cairo_surface_status (surface)));
 
102
        cairo_surface_destroy (surface);
 
103
        return CAIRO_TEST_FAILURE;
 
104
    }
 
105
 
 
106
    surface = cairo_image_surface_create_from_png_stream (read_error, NULL);
 
107
    if (cairo_surface_status (surface) != CAIRO_STATUS_READ_ERROR) {
 
108
        cairo_test_log ("Error: expected \"read error\", but got: %s\n",
 
109
                        cairo_status_to_string (cairo_surface_status (surface)));
 
110
        cairo_surface_destroy (surface);
 
111
        return CAIRO_TEST_FAILURE;
 
112
    }
 
113
 
 
114
    /* cheekily test error propagation from the user write funcs as well ... */
 
115
    xasprintf (&filename, "%s/%s", srcdir ? srcdir : ".",
 
116
               "create-from-png-ref.png");
 
117
 
 
118
    surface = cairo_image_surface_create_from_png (filename);
 
119
    if (cairo_surface_status (surface)) {
 
120
        cairo_test_log ("Error reading PNG image %s: %s\n",
 
121
                        filename,
 
122
                        cairo_status_to_string (cairo_surface_status (surface)));
 
123
        free (filename);
 
124
        return CAIRO_TEST_FAILURE;
 
125
    }
 
126
    free (filename);
 
127
 
 
128
    status = cairo_surface_write_to_png_stream (surface,
 
129
                                           (cairo_write_func_t) no_memory_error,
 
130
                                           NULL);
 
131
    if (status != CAIRO_STATUS_NO_MEMORY) {
 
132
        cairo_test_log ("Error: expected \"out of memory\", but got: %s\n",
 
133
                        cairo_status_to_string (status));
 
134
        cairo_surface_destroy (surface);
 
135
        return CAIRO_TEST_FAILURE;
 
136
    }
 
137
    status = cairo_surface_write_to_png_stream (surface,
 
138
                                                (cairo_write_func_t) read_error,
 
139
                                                NULL);
 
140
    if (status != CAIRO_STATUS_READ_ERROR) {
 
141
        cairo_test_log ("Error: expected \"read error\", but got: %s\n",
 
142
                        cairo_status_to_string (status));
 
143
        cairo_surface_destroy (surface);
 
144
        return CAIRO_TEST_FAILURE;
 
145
    }
 
146
    cairo_surface_destroy (surface);
 
147
 
 
148
    return cairo_test (&test);
 
149
}