Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / create-for-stream.c
1 /*
2  * Copyright © 2006 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: Kristian Høgsberg <krh@redhat.com>
24  */
25
26 #include "cairo-test.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #if CAIRO_HAS_PS_SURFACE
33 #include <cairo-ps.h>
34 #endif
35
36 #if CAIRO_HAS_PDF_SURFACE
37 #include <cairo-pdf.h>
38 #endif
39
40 #if CAIRO_HAS_SVG_SURFACE
41 #include <cairo-svg.h>
42 #endif
43
44 #include "cairo-test.h"
45
46 /* The main test suite doesn't test the *_create_for_stream
47  * constructors for the PDF, PS and SVG surface, so we do that here.
48  * We draw to an in-memory buffer using the stream constructor and
49  * compare the output to the contents of a file written using the
50  * file constructor.
51  */
52
53 #define MAX_OUTPUT_SIZE 4096
54
55 #define WIDTH_IN_INCHES  3
56 #define HEIGHT_IN_INCHES 3
57 #define WIDTH_IN_POINTS  (WIDTH_IN_INCHES  * 72.0)
58 #define HEIGHT_IN_POINTS (HEIGHT_IN_INCHES * 72.0)
59
60 #define BASENAME "create-for-stream.out"
61
62 static cairo_test_status_t
63 draw (cairo_t *cr, int width, int height)
64 {
65     /* Just draw a rectangle. */
66
67     cairo_rectangle (cr, width / 10., height /10.,
68                      width - 2 * width / 10.,
69                      height - 2 * height /10.);
70     cairo_fill (cr);
71
72     cairo_show_page (cr);
73
74     return CAIRO_TEST_SUCCESS;
75 }
76
77 static void
78 draw_to (cairo_surface_t *surface)
79 {
80     cairo_t *cr;
81
82     cr = cairo_create (surface);
83
84     draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
85
86     cairo_destroy (cr);
87 }
88
89 typedef struct _write_closure {
90     const cairo_test_context_t *ctx;
91     char buffer[MAX_OUTPUT_SIZE];
92     size_t index;
93     cairo_test_status_t status;
94 } write_closure_t;
95
96 static cairo_status_t
97 bad_write (void         *closure,
98            const unsigned char  *data,
99            unsigned int  length)
100 {
101     return CAIRO_STATUS_WRITE_ERROR;
102 }
103
104 static cairo_status_t
105 test_write (void                *closure,
106             const unsigned char *data,
107             unsigned int         length)
108 {
109     write_closure_t *wc = closure;
110
111     if (wc->index + length >= sizeof wc->buffer) {
112         cairo_test_log (wc->ctx, "Error: out of bounds in write callback\n");
113         wc->status = CAIRO_TEST_FAILURE;
114         return CAIRO_STATUS_SUCCESS;
115     }
116
117     memcpy (&wc->buffer[wc->index], data, length);
118     wc->index += length;
119
120     return CAIRO_STATUS_SUCCESS;
121 }
122
123
124 typedef cairo_surface_t *
125 (*file_constructor_t) (const char              *filename,
126                        double                   width_in_points,
127                        double                   height_in_points);
128
129 typedef cairo_surface_t *
130 (*stream_constructor_t) (cairo_write_func_t     write_func,
131                          void                  *closure,
132                          double                 width_in_points,
133                          double                 height_in_points);
134
135 static cairo_test_status_t
136 test_surface (const cairo_test_context_t *ctx,
137               const char                 *backend,
138               const char                 *filename,
139               file_constructor_t         file_constructor,
140               stream_constructor_t       stream_constructor)
141 {
142     cairo_surface_t *surface;
143     write_closure_t wc;
144     char file_contents[MAX_OUTPUT_SIZE];
145     cairo_status_t status;
146     FILE *fp;
147
148     /* test propagation of user errors */
149     surface = stream_constructor (bad_write, &wc,
150                                   WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
151
152     status = cairo_surface_status (surface);
153     if (status) {
154         cairo_test_log (ctx,
155                         "%s: Failed to create surface for stream.\n",
156                         backend);
157         return CAIRO_TEST_FAILURE;
158     }
159
160     draw_to (surface);
161
162     cairo_surface_finish (surface);
163     status = cairo_surface_status (surface);
164     cairo_surface_destroy (surface);
165
166     if (status != CAIRO_STATUS_WRITE_ERROR) {
167         cairo_test_log (ctx,
168                         "%s: Error: expected \"write error\", but received \"%s\".\n",
169                         backend, cairo_status_to_string (status));
170         return CAIRO_TEST_FAILURE;
171     }
172
173     /* construct the real surface */
174     wc.ctx = ctx;
175     wc.status = CAIRO_TEST_SUCCESS;
176     wc.index = 0;
177
178     surface = stream_constructor (test_write, &wc,
179                                   WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
180
181     status = cairo_surface_status (surface);
182     if (status) {
183         cairo_test_log (ctx,
184                         "%s: Failed to create surface for stream.\n", backend);
185         return CAIRO_TEST_FAILURE;
186     }
187
188     draw_to (surface);
189
190     cairo_surface_destroy (surface);
191
192     if (wc.status != CAIRO_TEST_SUCCESS) {
193         /* Error already reported. */
194         return wc.status;
195     }
196
197     surface = file_constructor (filename,
198                                 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
199
200     status = cairo_surface_status (surface);
201     if (status) {
202         cairo_test_log (ctx, "%s: Failed to create surface for file %s: %s.\n",
203                         backend, filename, cairo_status_to_string (status));
204         return CAIRO_TEST_FAILURE;
205     }
206
207     draw_to (surface);
208
209     cairo_surface_destroy (surface);
210
211     fp = fopen (filename, "r");
212     if (fp == NULL) {
213         cairo_test_log (ctx, "%s: Failed to open %s for reading: %s.\n",
214                         backend, filename, strerror (errno));
215         return CAIRO_TEST_FAILURE;
216     }
217
218     if (fread (file_contents, 1, wc.index, fp) != wc.index) {
219         cairo_test_log (ctx, "%s: Failed to read %s: %s.\n",
220                         backend, filename, strerror (errno));
221         fclose (fp);
222         return CAIRO_TEST_FAILURE;
223     }
224
225     if (memcmp (file_contents, wc.buffer, wc.index) != 0) {
226         cairo_test_log (ctx, "%s: Stream based output differ from file output for %s.\n",
227                         backend, filename);
228         fclose (fp);
229         return CAIRO_TEST_FAILURE;
230     }
231
232     fclose (fp);
233
234     return CAIRO_TEST_SUCCESS;
235 }
236
237 static cairo_test_status_t
238 preamble (cairo_test_context_t *ctx)
239 {
240     cairo_test_status_t status = CAIRO_TEST_UNTESTED;
241     cairo_test_status_t test_status;
242
243 #if CAIRO_HAS_PS_SURFACE
244     if (cairo_test_is_target_enabled (ctx, "ps2") ||
245         cairo_test_is_target_enabled (ctx, "ps3"))
246     {
247         if (status == CAIRO_TEST_UNTESTED)
248             status = CAIRO_TEST_SUCCESS;
249
250         test_status = test_surface (ctx, "ps", BASENAME ".ps",
251                                     cairo_ps_surface_create,
252                                     cairo_ps_surface_create_for_stream);
253         cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
254                         ctx->test->name, "ps",
255                         test_status ? "FAIL" : "PASS");
256         if (status == CAIRO_TEST_SUCCESS)
257             status = test_status;
258     }
259 #endif
260
261 #if CAIRO_HAS_PDF_SURFACE
262     if (cairo_test_is_target_enabled (ctx, "pdf")) {
263         if (status == CAIRO_TEST_UNTESTED)
264             status = CAIRO_TEST_SUCCESS;
265
266         test_status = test_surface (ctx, "pdf", BASENAME ".pdf",
267                                     cairo_pdf_surface_create,
268                                     cairo_pdf_surface_create_for_stream);
269         cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
270                         ctx->test->name, "pdf",
271                         test_status ? "FAIL" : "PASS");
272         if (status == CAIRO_TEST_SUCCESS)
273             status = test_status;
274     }
275 #endif
276
277 #if CAIRO_HAS_SVG_SURFACE
278     if (cairo_test_is_target_enabled (ctx, "svg11") ||
279         cairo_test_is_target_enabled (ctx, "svg12"))
280     {
281         if (status == CAIRO_TEST_UNTESTED)
282             status = CAIRO_TEST_SUCCESS;
283
284         test_status = test_surface (ctx, "svg", BASENAME ".svg",
285                                     cairo_svg_surface_create,
286                                     cairo_svg_surface_create_for_stream);
287         cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
288                         ctx->test->name, "svg",
289                         test_status ? "FAIL" : "PASS");
290         if (status == CAIRO_TEST_SUCCESS)
291             status = test_status;
292     }
293 #endif
294
295     return status;
296 }
297
298 CAIRO_TEST (create_for_stream,
299             "Checks creating vector surfaces with user defined I/O\n",
300             "stream", /* keywords */
301             "target=vector", /* requirements */
302             0, 0,
303             preamble, NULL)