1
/*
2
 * Copyright © 2011 Uli Schlachter
3
 *
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:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
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
22
 * SOFTWARE.
23
 *
24
 * Author: Uli Schlachter <psychon@znc.in>
25
 */
26

            
27
#include "cairo-test.h"
28

            
29
static void
30
2
mime_data_destroy_func (void *data)
31
{
32
2
    cairo_bool_t *called = data;
33
2
    *called = TRUE;
34
2
}
35

            
36
static cairo_test_status_t
37
4
check_mime_data (cairo_test_context_t *ctx, cairo_surface_t *surface,
38
		 const char *mimetype, const unsigned char *data,
39
		 unsigned long length)
40
{
41
    const unsigned char *data_ret;
42
    unsigned long length_ret;
43

            
44
4
    cairo_surface_get_mime_data (surface, mimetype, &data_ret, &length_ret);
45
4
    if (data_ret != data || length_ret != length) {
46
	cairo_test_log (ctx,
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;
51
    }
52

            
53
4
    return CAIRO_TEST_SUCCESS;
54
}
55

            
56
static cairo_test_status_t
57
3
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)
60
{
61
    cairo_status_t status;
62

            
63
3
    status = cairo_surface_set_mime_data (surface, mimetype,
64
					  data, length,
65
					  mime_data_destroy_func,
66
					  destroy_called);
67
3
    if (status) {
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;
71
    }
72

            
73
3
    return check_mime_data (ctx, surface, mimetype, data, length);
74
}
75

            
76
static cairo_test_status_t
77
1
preamble (cairo_test_context_t *ctx)
78
{
79
1
    const char *mimetype = "text/x-uri";
80
1
    const char *data1 = "https://www.cairographics.org";
81
1
    const char *data2 = "https://cairographics.org/examples/";
82
1
    cairo_bool_t destroy1_called = FALSE;
83
1
    cairo_bool_t destroy2_called = FALSE;
84
    cairo_surface_t *surface;
85
1
    cairo_test_status_t test_status = CAIRO_TEST_SUCCESS;
86

            
87
1
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
88
1
    if (cairo_surface_status (surface)) {
89
	cairo_test_log (ctx, "Could not create image surface\n");
90
	test_status = CAIRO_TEST_ERROR;
91
	goto out;
92
    }
93

            
94
1
    test_status = check_mime_data (ctx, surface, mimetype, NULL, 0);
95
1
    if (test_status)
96
	goto out;
97

            
98
1
    test_status = set_and_check_mime_data (ctx, surface, mimetype,
99
					   (const unsigned char *) data1,
100
					   strlen (data1),
101
					   &destroy1_called);
102
1
    if (test_status)
103
	goto out;
104

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

            
111
1
    test_status = set_and_check_mime_data (ctx, surface, mimetype,
112
					   (const unsigned char *) data2,
113
					   strlen (data2),
114
					   &destroy2_called);
115
1
    if (test_status)
116
	goto out;
117

            
118
1
    if (!destroy1_called) {
119
	cairo_test_log (ctx, "MIME data 1 destroy callback not called\n");
120
	test_status = CAIRO_TEST_ERROR;
121
	goto out;
122
    }
123
1
    if (destroy2_called) {
124
	cairo_test_log (ctx, "MIME data 2 destroyed too early\n");
125
	test_status = CAIRO_TEST_ERROR;
126
	goto out;
127
    }
128

            
129
1
    test_status = set_and_check_mime_data (ctx, surface, mimetype,
130
					   NULL, 0, NULL);
131
1
    if (test_status)
132
	goto out;
133

            
134
1
    if (!destroy2_called) {
135
	cairo_test_log (ctx, "MIME data destroy callback not called\n");
136
	test_status = CAIRO_TEST_ERROR;
137
	goto out;
138
    }
139

            
140
1
out:
141
1
    cairo_surface_destroy (surface);
142

            
143
1
    return test_status;
144
}
145

            
146
1
CAIRO_TEST (mime_surface_api,
147
	    "Check the mime data API",
148
	    "api", /* keywords */
149
	    NULL, /* requirements */
150
	    0, 0,
151
	    preamble, NULL)