2 * Copyright © 2011 Uli Schlachter
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Author: Uli Schlachter <psychon@znc.in>
27 #include "cairo-test.h"
30 mime_data_destroy_func (void *data)
32 cairo_bool_t *called = data;
36 static cairo_test_status_t
37 check_mime_data (cairo_test_context_t *ctx, cairo_surface_t *surface,
38 const char *mimetype, const unsigned char *data,
41 const unsigned char *data_ret;
42 unsigned long length_ret;
44 cairo_surface_get_mime_data (surface, mimetype, &data_ret, &length_ret);
45 if (data_ret != data || length_ret != length) {
47 "Surface has mime data %p with length %lu, "
48 "but expected %p with length %lu\n",
49 data_ret, length_ret, data, length);
50 return CAIRO_TEST_ERROR;
53 return CAIRO_TEST_SUCCESS;
56 static cairo_test_status_t
57 set_and_check_mime_data (cairo_test_context_t *ctx, cairo_surface_t *surface,
58 const char *mimetype, const unsigned char *data,
59 unsigned long length, cairo_bool_t *destroy_called)
61 cairo_status_t status;
63 status = cairo_surface_set_mime_data (surface, mimetype,
65 mime_data_destroy_func,
68 cairo_test_log (ctx, "Could not set mime data to %s: %s\n",
69 data, cairo_status_to_string(status));
70 return CAIRO_TEST_ERROR;
73 return check_mime_data (ctx, surface, mimetype, data, length);
76 static cairo_test_status_t
77 preamble (cairo_test_context_t *ctx)
79 const char *mimetype = "text/x-uri";
80 const char *data1 = "http://www.cairographics.org";
81 const char *data2 = "http://cairographics.org/examples/";
82 cairo_bool_t destroy1_called = FALSE;
83 cairo_bool_t destroy2_called = FALSE;
84 cairo_surface_t *surface;
85 cairo_test_status_t test_status = CAIRO_TEST_SUCCESS;
87 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
88 if (cairo_surface_status (surface)) {
89 cairo_test_log (ctx, "Could not create image surface\n");
90 test_status = CAIRO_TEST_ERROR;
94 test_status = check_mime_data (ctx, surface, mimetype, NULL, 0);
98 test_status = set_and_check_mime_data (ctx, surface, mimetype,
99 (const unsigned char *) data1,
105 if (destroy1_called) {
106 cairo_test_log (ctx, "MIME data 1 destroyed too early\n");
107 test_status = CAIRO_TEST_ERROR;
111 test_status = set_and_check_mime_data (ctx, surface, mimetype,
112 (const unsigned char *) data2,
118 if (!destroy1_called) {
119 cairo_test_log (ctx, "MIME data 1 destroy callback not called\n");
120 test_status = CAIRO_TEST_ERROR;
123 if (destroy2_called) {
124 cairo_test_log (ctx, "MIME data 2 destroyed too early\n");
125 test_status = CAIRO_TEST_ERROR;
129 test_status = set_and_check_mime_data (ctx, surface, mimetype,
134 if (!destroy2_called) {
135 cairo_test_log (ctx, "MIME data destroy callback not called\n");
136 test_status = CAIRO_TEST_ERROR;
141 cairo_surface_destroy (surface);
146 CAIRO_TEST (mime_surface_api,
147 "Check the mime data API",
148 "api", /* keywords */
149 NULL, /* requirements */