summaryrefslogtreecommitdiff
path: root/test/mime-surface-api.c
blob: ce12653e13ce864bbc37d5c12d571d6d2d24ffdf (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright © 2011 Uli Schlachter
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author: Uli Schlachter <psychon@znc.in>
 */

#include "cairo-test.h"

static void
mime_data_destroy_func (void *data)
{
    cairo_bool_t *called = data;
    *called = TRUE;
}

static cairo_test_status_t
check_mime_data (cairo_test_context_t *ctx, cairo_surface_t *surface,
		 const char *mimetype, const unsigned char *data,
		 unsigned long length)
{
    const unsigned char *data_ret;
    unsigned long length_ret;

    cairo_surface_get_mime_data (surface, mimetype, &data_ret, &length_ret);
    if (data_ret != data || length_ret != length) {
	cairo_test_log (ctx,
			"Surface has mime data %p with length %lu, "
			"but expected %p with length %lu\n",
			data_ret, length_ret, data, length);
       return CAIRO_TEST_ERROR;
    }

    return CAIRO_TEST_SUCCESS;
}

static cairo_test_status_t
set_and_check_mime_data (cairo_test_context_t *ctx, cairo_surface_t *surface,
			 const char *mimetype, const unsigned char *data,
			 unsigned long length, cairo_bool_t *destroy_called)
{
    cairo_status_t status;

    status = cairo_surface_set_mime_data (surface, mimetype,
					  data, length,
					  mime_data_destroy_func,
					  destroy_called);
    if (status) {
	cairo_test_log (ctx, "Could not set mime data to %s: %s\n",
			data, cairo_status_to_string(status));
	return CAIRO_TEST_ERROR;
    }

    return check_mime_data (ctx, surface, mimetype, data, length);
}

static cairo_test_status_t
preamble (cairo_test_context_t *ctx)
{
    const char *mimetype = "text/x-uri";
    const char *data1 = "http://www.cairographics.org";
    const char *data2 = "http://cairographics.org/examples/";
    cairo_bool_t destroy1_called = FALSE;
    cairo_bool_t destroy2_called = FALSE;
    cairo_surface_t *surface;
    cairo_test_status_t test_status = CAIRO_TEST_SUCCESS;

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
    if (cairo_surface_status (surface)) {
	cairo_test_log (ctx, "Could not create image surface\n");
	test_status = CAIRO_TEST_ERROR;
	goto out;
    }

    test_status = check_mime_data (ctx, surface, mimetype, NULL, 0);
    if (test_status)
	goto out;

    test_status = set_and_check_mime_data (ctx, surface, mimetype,
					   (const unsigned char *) data1,
					   strlen (data1),
					   &destroy1_called);
    if (test_status)
	goto out;

    if (destroy1_called) {
	cairo_test_log (ctx, "MIME data 1 destroyed too early\n");
	test_status = CAIRO_TEST_ERROR;
	goto out;
    }

    test_status = set_and_check_mime_data (ctx, surface, mimetype,
					   (const unsigned char *) data2,
					   strlen (data2),
					   &destroy2_called);
    if (test_status)
	goto out;

    if (!destroy1_called) {
	cairo_test_log (ctx, "MIME data 1 destroy callback not called\n");
	test_status = CAIRO_TEST_ERROR;
	goto out;
    }
    if (destroy2_called) {
	cairo_test_log (ctx, "MIME data 2 destroyed too early\n");
	test_status = CAIRO_TEST_ERROR;
	goto out;
    }

    test_status = set_and_check_mime_data (ctx, surface, mimetype,
					   NULL, 0, NULL);
    if (test_status)
	goto out;

    if (!destroy2_called) {
	cairo_test_log (ctx, "MIME data destroy callback not called\n");
	test_status = CAIRO_TEST_ERROR;
	goto out;
    }

out:
    cairo_surface_destroy (surface);

    return test_status;
}

CAIRO_TEST (mime_surface_api,
	    "Check the mime data API",
	    "api", /* keywords */
	    NULL, /* requirements */
	    0, 0,
	    preamble, NULL)