2 * Copyright © 2006 Red Hat, Inc.
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.
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.
23 * Author: Kristian Høgsberg <krh@redhat.com>
26 #include "cairo-test.h"
32 #if CAIRO_HAS_PS_SURFACE
36 #if CAIRO_HAS_PDF_SURFACE
37 #include <cairo-pdf.h>
40 #if CAIRO_HAS_SVG_SURFACE
41 #include <cairo-svg.h>
44 #include "cairo-test.h"
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
53 #define MAX_OUTPUT_SIZE 4096
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)
60 #define BASENAME "create-for-stream.out"
62 static cairo_test_status_t
63 draw (cairo_t *cr, int width, int height)
65 /* Just draw a rectangle. */
67 cairo_rectangle (cr, width / 10., height /10.,
68 width - 2 * width / 10.,
69 height - 2 * height /10.);
74 return CAIRO_TEST_SUCCESS;
78 draw_to (cairo_surface_t *surface)
82 cr = cairo_create (surface);
84 draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
89 typedef struct _write_closure {
90 const cairo_test_context_t *ctx;
91 char buffer[MAX_OUTPUT_SIZE];
93 cairo_test_status_t status;
97 bad_write (void *closure,
98 const unsigned char *data,
101 return CAIRO_STATUS_WRITE_ERROR;
104 static cairo_status_t
105 test_write (void *closure,
106 const unsigned char *data,
109 write_closure_t *wc = closure;
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;
117 memcpy (&wc->buffer[wc->index], data, length);
120 return CAIRO_STATUS_SUCCESS;
124 typedef cairo_surface_t *
125 (*file_constructor_t) (const char *filename,
126 double width_in_points,
127 double height_in_points);
129 typedef cairo_surface_t *
130 (*stream_constructor_t) (cairo_write_func_t write_func,
132 double width_in_points,
133 double height_in_points);
135 static cairo_test_status_t
136 test_surface (const cairo_test_context_t *ctx,
138 const char *filename,
139 file_constructor_t file_constructor,
140 stream_constructor_t stream_constructor)
142 cairo_surface_t *surface;
144 char file_contents[MAX_OUTPUT_SIZE];
145 cairo_status_t status;
148 /* test propagation of user errors */
149 surface = stream_constructor (bad_write, &wc,
150 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
152 status = cairo_surface_status (surface);
155 "%s: Failed to create surface for stream.\n",
157 return CAIRO_TEST_FAILURE;
162 cairo_surface_finish (surface);
163 status = cairo_surface_status (surface);
164 cairo_surface_destroy (surface);
166 if (status != CAIRO_STATUS_WRITE_ERROR) {
168 "%s: Error: expected \"write error\", but received \"%s\".\n",
169 backend, cairo_status_to_string (status));
170 return CAIRO_TEST_FAILURE;
173 /* construct the real surface */
175 wc.status = CAIRO_TEST_SUCCESS;
178 surface = stream_constructor (test_write, &wc,
179 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
181 status = cairo_surface_status (surface);
184 "%s: Failed to create surface for stream.\n", backend);
185 return CAIRO_TEST_FAILURE;
190 cairo_surface_destroy (surface);
192 if (wc.status != CAIRO_TEST_SUCCESS) {
193 /* Error already reported. */
197 surface = file_constructor (filename,
198 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
200 status = cairo_surface_status (surface);
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;
209 cairo_surface_destroy (surface);
211 fp = fopen (filename, "r");
213 cairo_test_log (ctx, "%s: Failed to open %s for reading: %s.\n",
214 backend, filename, strerror (errno));
215 return CAIRO_TEST_FAILURE;
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));
222 return CAIRO_TEST_FAILURE;
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",
229 return CAIRO_TEST_FAILURE;
234 return CAIRO_TEST_SUCCESS;
237 static cairo_test_status_t
238 preamble (cairo_test_context_t *ctx)
240 cairo_test_status_t status = CAIRO_TEST_UNTESTED;
241 cairo_test_status_t test_status;
243 const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
245 #if CAIRO_HAS_PS_SURFACE
246 if (cairo_test_is_target_enabled (ctx, "ps2") ||
247 cairo_test_is_target_enabled (ctx, "ps3"))
249 if (status == CAIRO_TEST_UNTESTED)
250 status = CAIRO_TEST_SUCCESS;
252 xasprintf (&filename, "%s/%s", path, BASENAME ".ps");
253 test_status = test_surface (ctx, "ps", filename,
254 cairo_ps_surface_create,
255 cairo_ps_surface_create_for_stream);
256 cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
257 ctx->test->name, "ps",
258 test_status ? "FAIL" : "PASS");
259 if (status == CAIRO_TEST_SUCCESS)
260 status = test_status;
265 #if CAIRO_HAS_PDF_SURFACE
266 if (cairo_test_is_target_enabled (ctx, "pdf")) {
267 if (status == CAIRO_TEST_UNTESTED)
268 status = CAIRO_TEST_SUCCESS;
270 xasprintf (&filename, "%s/%s", path, BASENAME ".pdf");
271 test_status = test_surface (ctx, "pdf", filename,
272 cairo_pdf_surface_create,
273 cairo_pdf_surface_create_for_stream);
274 cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
275 ctx->test->name, "pdf",
276 test_status ? "FAIL" : "PASS");
277 if (status == CAIRO_TEST_SUCCESS)
278 status = test_status;
283 #if CAIRO_HAS_SVG_SURFACE
284 if (cairo_test_is_target_enabled (ctx, "svg11") ||
285 cairo_test_is_target_enabled (ctx, "svg12"))
287 if (status == CAIRO_TEST_UNTESTED)
288 status = CAIRO_TEST_SUCCESS;
290 xasprintf (&filename, "%s/%s", path, BASENAME ".svg");
291 test_status = test_surface (ctx, "svg", filename,
292 cairo_svg_surface_create,
293 cairo_svg_surface_create_for_stream);
294 cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
295 ctx->test->name, "svg",
296 test_status ? "FAIL" : "PASS");
297 if (status == CAIRO_TEST_SUCCESS)
298 status = test_status;
306 CAIRO_TEST (create_for_stream,
307 "Checks creating vector surfaces with user defined I/O\n",
308 "stream", /* keywords */
309 "target=vector", /* requirements */