1
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2
/*
3
 * Copyright © 2005 Mozilla Corporation, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software
6
 * and its documentation for any purpose is hereby granted without
7
 * fee, provided that the above copyright notice appear in all copies
8
 * and that both that copyright notice and this permission notice
9
 * appear in supporting documentation, and that the name of
10
 * Mozilla Corporation not be used in advertising or publicity pertaining to
11
 * distribution of the software without specific, written prior
12
 * permission. Mozilla Corporation makes no representations about the
13
 * suitability of this software for any purpose.  It is provided "as
14
 * is" without express or implied warranty.
15
 *
16
 * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18
 * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION BE LIABLE FOR ANY SPECIAL,
19
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
 *
24
 * Author: Vladimir Vukicevic <vladimir@pobox.com>
25
 */
26

            
27
#include "cairo-test.h"
28
#include <stdlib.h>
29

            
30
#define CHECK_SUCCESS do { \
31
    if (status) { \
32
	cairo_pattern_destroy (pat); \
33
	return cairo_test_status_from_status (ctx, status); \
34
    } \
35
} while (0)
36

            
37
static int
38
3
double_buf_equal (const cairo_test_context_t *ctx, double *a, double *b, int nc)
39
{
40
    int i;
41
48
    for (i = 0; i < nc; i++) {
42
45
	if (!CAIRO_TEST_DOUBLE_EQUALS(a[i],b[i])) {
43
	    cairo_test_log (ctx, "Error: doubles not equal: %g, %g\n",
44
			    a[i], b[i]);
45
	    return 0;
46
	}
47
    }
48
3
    return 1;
49
}
50

            
51
static cairo_test_status_t
52
3
draw (cairo_t *cr, int width, int height)
53
{
54
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
55
    cairo_status_t status;
56
    cairo_pattern_t *pat;
57

            
58
    /* Test pattern_get_rgba */
59
    {
60
	double r, g, b, a;
61
3
	pat = cairo_pattern_create_rgba (0.2, 0.3, 0.4, 0.5);
62

            
63
3
	status = cairo_pattern_get_rgba (pat, &r, &g, &b, &a);
64
3
	CHECK_SUCCESS;
65

            
66
3
	if (!CAIRO_TEST_DOUBLE_EQUALS(r,0.2) ||
67
3
	    !CAIRO_TEST_DOUBLE_EQUALS(g,0.3) ||
68
3
	    !CAIRO_TEST_DOUBLE_EQUALS(b,0.4) ||
69
3
	    !CAIRO_TEST_DOUBLE_EQUALS(a,0.5)) {
70
	    cairo_test_log (ctx, "Error: cairo_pattern_get_rgba returned unexpected results: %g, %g, %g, %g\n",
71
			    r, g, b, a);
72
	    cairo_pattern_destroy (pat);
73
	    return CAIRO_TEST_FAILURE;
74
	}
75

            
76
3
	cairo_pattern_destroy (pat);
77
    }
78

            
79
    /* Test pattern_get_surface */
80
    {
81
	cairo_surface_t *surf;
82

            
83
3
	pat = cairo_pattern_create_for_surface (cairo_get_target (cr));
84

            
85
3
	status = cairo_pattern_get_surface (pat, &surf);
86
3
	CHECK_SUCCESS;
87

            
88
3
	if (surf != cairo_get_target (cr)) {
89
	    cairo_test_log (ctx, "Error: cairo_pattern_get_resurface returned wrong surface\n");
90
	    cairo_pattern_destroy (pat);
91
	    return CAIRO_TEST_FAILURE;
92
	}
93

            
94
3
	cairo_pattern_destroy (pat);
95
    }
96

            
97
    /* Test get_color_stops & linear_get_points */
98
    {
99
	int i;
100
	double x0, y0, x1, y1;
101
3
	double expected_values[15] = { 0.0, 0.2, 0.4, 0.2, 1.0,
102
				       0.5, 0.4, 0.5, 0.2, 0.5,
103
				       1.0, 0.2, 0.4, 0.5, 0.2 };
104
	double new_buf[15];
105

            
106
3
	pat = cairo_pattern_create_linear (1.0, 2.0, 3.0, 4.0);
107

            
108
12
	for (i = 0; i < 3; i++) {
109
9
	    cairo_pattern_add_color_stop_rgba (pat,
110
9
					       expected_values[i*5+0],
111
9
					       expected_values[i*5+1],
112
9
					       expected_values[i*5+2],
113
9
					       expected_values[i*5+3],
114
9
					       expected_values[i*5+4]);
115
	}
116

            
117
3
	status = cairo_pattern_get_linear_points (pat, &x0, &y0, &x1, &y1);
118
3
	CHECK_SUCCESS;
119

            
120
3
	if (!CAIRO_TEST_DOUBLE_EQUALS(x0,1.0) ||
121
3
	    !CAIRO_TEST_DOUBLE_EQUALS(y0,2.0) ||
122
3
	    !CAIRO_TEST_DOUBLE_EQUALS(x1,3.0) ||
123
3
	    !CAIRO_TEST_DOUBLE_EQUALS(y1,4.0))
124
	{
125
	    cairo_pattern_destroy (pat);
126
	    return CAIRO_TEST_FAILURE;
127
	}
128

            
129
3
	status = cairo_pattern_get_color_stop_count (pat, &i);
130
3
	CHECK_SUCCESS;
131

            
132
3
	if (i != 3) {
133
	    cairo_pattern_destroy (pat);
134
	    return CAIRO_TEST_FAILURE;
135
	}
136

            
137
12
	for (i = 0; i < 3; i++) {
138
9
	    status = cairo_pattern_get_color_stop_rgba (pat, i,
139
9
							&new_buf[i*5+0],
140
9
							&new_buf[i*5+1],
141
9
							&new_buf[i*5+2],
142
9
							&new_buf[i*5+3],
143
9
							&new_buf[i*5+4]);
144
9
	    CHECK_SUCCESS;
145
	}
146

            
147
3
	status = cairo_pattern_get_color_stop_rgba (pat, 5, NULL, NULL, NULL, NULL, NULL);
148
3
	if (status != CAIRO_STATUS_INVALID_INDEX) {
149
	    cairo_pattern_destroy (pat);
150
	    return CAIRO_TEST_FAILURE;
151
	}
152

            
153
3
	if (!double_buf_equal (ctx, new_buf, expected_values,
154
			       ARRAY_LENGTH (expected_values)))
155
	{
156
	    cairo_pattern_destroy (pat);
157
	    return CAIRO_TEST_FAILURE;
158
	}
159

            
160
3
	cairo_pattern_destroy (pat);
161
    }
162

            
163
    /* Test radial_get_circles */
164
    {
165
	double a, b, c, d, e, f;
166
3
	pat = cairo_pattern_create_radial (1, 2, 3,
167
					   4, 5, 6);
168

            
169
3
	status = cairo_pattern_get_radial_circles (pat, &a, &b, &c, &d, &e, &f);
170
3
	CHECK_SUCCESS;
171

            
172
3
	if (!CAIRO_TEST_DOUBLE_EQUALS(a,1.0) ||
173
3
	    !CAIRO_TEST_DOUBLE_EQUALS(b,2.0) ||
174
3
	    !CAIRO_TEST_DOUBLE_EQUALS(c,3.0) ||
175
3
	    !CAIRO_TEST_DOUBLE_EQUALS(d,4.0) ||
176
3
	    !CAIRO_TEST_DOUBLE_EQUALS(e,5.0) ||
177
3
	    !CAIRO_TEST_DOUBLE_EQUALS(f,6.0))
178
	{
179
	    cairo_pattern_destroy (pat);
180
	    return CAIRO_TEST_FAILURE;
181
	}
182

            
183
3
	cairo_pattern_destroy (pat);
184
    }
185

            
186
    /* Test mesh getters */
187
    {
188
	unsigned int count;
189
	int i;
190
3
	pat = cairo_pattern_create_mesh ();
191

            
192
3
	status = cairo_mesh_pattern_get_patch_count (pat, &count);
193
3
	CHECK_SUCCESS;
194

            
195
3
	if (count != 0) {
196
	    cairo_pattern_destroy (pat);
197
	    return CAIRO_TEST_FAILURE;
198
	}
199

            
200
3
	cairo_mesh_pattern_begin_patch (pat);
201
3
	cairo_mesh_pattern_move_to (pat, 0, 0);
202
3
	cairo_mesh_pattern_line_to (pat, 0, 3);
203
3
	cairo_mesh_pattern_line_to (pat, 3, 3);
204
3
	cairo_mesh_pattern_line_to (pat, 3, 0);
205

            
206
3
	status = cairo_mesh_pattern_get_patch_count (pat, &count);
207
3
	CHECK_SUCCESS;
208

            
209
3
	if (count != 0) {
210
	    cairo_pattern_destroy (pat);
211
	    return CAIRO_TEST_FAILURE;
212
	}
213

            
214
3
	cairo_mesh_pattern_end_patch (pat);
215

            
216
3
	status = cairo_mesh_pattern_get_patch_count (pat, &count);
217
3
	CHECK_SUCCESS;
218

            
219
3
	if (count != 1) {
220
	    cairo_pattern_destroy (pat);
221
	    return CAIRO_TEST_FAILURE;
222
	}
223

            
224
15
	for (i = 0; i < 4; i++) {
225
12
	    double cp_x[4] = { 1, 1, 2, 2 };
226
12
	    double cp_y[4] = { 1, 2, 2, 1 };
227
	    double x, y;
228

            
229
12
	    status = cairo_mesh_pattern_get_control_point (pat, 0, i, &x, &y);
230
12
	    CHECK_SUCCESS;
231

            
232
12
	    if (!CAIRO_TEST_DOUBLE_EQUALS(x,cp_x[i]) ||
233
12
		!CAIRO_TEST_DOUBLE_EQUALS(y,cp_y[i]))
234
	    {
235
		cairo_pattern_destroy (pat);
236
		return CAIRO_TEST_FAILURE;
237
	    }
238
	}
239

            
240
3
	cairo_mesh_pattern_begin_patch (pat);
241
3
	cairo_mesh_pattern_move_to (pat, 0, 0);
242
3
	cairo_mesh_pattern_line_to (pat, 1, 0);
243
3
	cairo_mesh_pattern_line_to (pat, 1, 1);
244
3
	cairo_mesh_pattern_set_corner_color_rgb (pat, 0, 1, 1, 1);
245
3
	cairo_mesh_pattern_end_patch (pat);
246

            
247
15
	for (i = 0; i < 4; i++) {
248
12
	    double corner_color[4] = { 1, 0, 0, 1 };
249
	    double a, r, g, b;
250

            
251
12
	    status = cairo_mesh_pattern_get_corner_color_rgba (pat, 1, i,
252
							       &r, &g, &b, &a);
253
12
	    CHECK_SUCCESS;
254

            
255
12
	    if (!CAIRO_TEST_DOUBLE_EQUALS(a,corner_color[i]) ||
256
12
		!CAIRO_TEST_DOUBLE_EQUALS(r,corner_color[i]) ||
257
12
		!CAIRO_TEST_DOUBLE_EQUALS(g,corner_color[i]) ||
258
12
		!CAIRO_TEST_DOUBLE_EQUALS(b,corner_color[i]))
259
	    {
260
		cairo_pattern_destroy (pat);
261
		return CAIRO_TEST_FAILURE;
262
	    }
263
	}
264

            
265
3
	cairo_pattern_destroy (pat);
266
    }
267

            
268
3
    cairo_set_source_rgb (cr, 0, 1, 0);
269
3
    cairo_paint (cr);
270

            
271
3
    return CAIRO_TEST_SUCCESS;
272
}
273

            
274
1
CAIRO_TEST (pattern_getters,
275
	    "Tests calls to pattern getter functions",
276
	    "pattern, api", /* keywords */
277
	    NULL, /* requirements */
278
	    1, 1,
279
	    NULL, draw)