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
mime_data_destroy_func(void * data)30 mime_data_destroy_func (void *data)
31 {
32     cairo_bool_t *called = data;
33     *called = TRUE;
34 }
35 
36 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)37 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     cairo_surface_get_mime_data (surface, mimetype, &data_ret, &length_ret);
45     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     return CAIRO_TEST_SUCCESS;
54 }
55 
56 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)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)
60 {
61     cairo_status_t status;
62 
63     status = cairo_surface_set_mime_data (surface, mimetype,
64 					  data, length,
65 					  mime_data_destroy_func,
66 					  destroy_called);
67     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     return check_mime_data (ctx, surface, mimetype, data, length);
74 }
75 
76 static cairo_test_status_t
preamble(cairo_test_context_t * ctx)77 preamble (cairo_test_context_t *ctx)
78 {
79     const char *mimetype = "text/x-uri";
80     const char *data1 = "https://www.cairographics.org";
81     const char *data2 = "https://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;
86 
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;
91 	goto out;
92     }
93 
94     test_status = check_mime_data (ctx, surface, mimetype, NULL, 0);
95     if (test_status)
96 	goto out;
97 
98     test_status = set_and_check_mime_data (ctx, surface, mimetype,
99 					   (const unsigned char *) data1,
100 					   strlen (data1),
101 					   &destroy1_called);
102     if (test_status)
103 	goto out;
104 
105     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     test_status = set_and_check_mime_data (ctx, surface, mimetype,
112 					   (const unsigned char *) data2,
113 					   strlen (data2),
114 					   &destroy2_called);
115     if (test_status)
116 	goto out;
117 
118     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     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     test_status = set_and_check_mime_data (ctx, surface, mimetype,
130 					   NULL, 0, NULL);
131     if (test_status)
132 	goto out;
133 
134     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 out:
141     cairo_surface_destroy (surface);
142 
143     return test_status;
144 }
145 
146 CAIRO_TEST (mime_surface_api,
147 	    "Check the mime data API",
148 	    "api", /* keywords */
149 	    NULL, /* requirements */
150 	    0, 0,
151 	    preamble, NULL)
152