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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2010 litl, LLC
imports.gi.versions.Gdk = '3.0';
imports.gi.versions.Gtk = '3.0';
const Cairo = imports.cairo;
const {Gdk, GIMarshallingTests, GLib, Gtk, Regress} = imports.gi;
function _ts(obj) {
return obj.toString().slice(8, -1);
}
describe('Cairo', function () {
let cr, surface;
beforeEach(function () {
surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 10, 10);
cr = new Cairo.Context(surface);
});
describe('context', function () {
it('has the right type', function () {
expect(cr instanceof Cairo.Context).toBeTruthy();
});
it('reports its target surface', function () {
expect(_ts(cr.getTarget())).toEqual('ImageSurface');
});
it('can set its source to a pattern', function () {
let pattern = Cairo.SolidPattern.createRGB(1, 2, 3);
cr.setSource(pattern);
expect(_ts(cr.getSource())).toEqual('SolidPattern');
});
it('can set its antialias', function () {
cr.setAntialias(Cairo.Antialias.NONE);
expect(cr.getAntialias()).toEqual(Cairo.Antialias.NONE);
});
it('can set its fill rule', function () {
cr.setFillRule(Cairo.FillRule.EVEN_ODD);
expect(cr.getFillRule()).toEqual(Cairo.FillRule.EVEN_ODD);
});
it('can set its line cap', function () {
cr.setLineCap(Cairo.LineCap.ROUND);
expect(cr.getLineCap()).toEqual(Cairo.LineCap.ROUND);
});
it('can set its line join', function () {
cr.setLineJoin(Cairo.LineJoin.ROUND);
expect(cr.getLineJoin()).toEqual(Cairo.LineJoin.ROUND);
});
it('can set its line width', function () {
cr.setLineWidth(1138);
expect(cr.getLineWidth()).toEqual(1138);
});
it('can set its miter limit', function () {
cr.setMiterLimit(42);
expect(cr.getMiterLimit()).toEqual(42);
});
it('can set its operator', function () {
cr.setOperator(Cairo.Operator.IN);
expect(cr.getOperator()).toEqual(Cairo.Operator.IN);
});
it('can set its tolerance', function () {
cr.setTolerance(144);
expect(cr.getTolerance()).toEqual(144);
});
it('has a rectangle as clip extents', function () {
expect(cr.clipExtents().length).toEqual(4);
});
it('has a rectangle as fill extents', function () {
expect(cr.fillExtents().length).toEqual(4);
});
it('has a rectangle as stroke extents', function () {
expect(cr.strokeExtents().length).toEqual(4);
});
it('has zero dashes initially', function () {
expect(cr.getDashCount()).toEqual(0);
});
it('transforms user to device coordinates', function () {
expect(cr.userToDevice(0, 0).length).toEqual(2);
});
it('transforms user to device distance', function () {
expect(cr.userToDeviceDistance(0, 0).length).toEqual(2);
});
it('transforms device to user coordinates', function () {
expect(cr.deviceToUser(0, 0).length).toEqual(2);
});
it('transforms device to user distance', function () {
expect(cr.deviceToUserDistance(0, 0).length).toEqual(2);
});
it('computes text extents', function () {
expect(cr.textExtents('')).toEqual({
xBearing: 0,
yBearing: 0,
width: 0,
height: 0,
xAdvance: 0,
yAdvance: 0,
});
expect(cr.textExtents('trailing spaces ')).toEqual({
xBearing: jasmine.any(Number),
yBearing: jasmine.any(Number),
width: jasmine.any(Number),
height: jasmine.any(Number),
xAdvance: jasmine.any(Number),
yAdvance: 0,
});
});
it('can call various, otherwise untested, methods without crashing', function () {
expect(() => {
cr.save();
cr.restore();
cr.setSourceSurface(surface, 0, 0);
cr.pushGroup();
cr.popGroup();
cr.pushGroupWithContent(Cairo.Content.COLOR);
cr.popGroupToSource();
cr.setSourceRGB(1, 2, 3);
cr.setSourceRGBA(1, 2, 3, 4);
cr.clip();
cr.clipPreserve();
cr.fill();
cr.fillPreserve();
let pattern = Cairo.SolidPattern.createRGB(1, 2, 3);
cr.mask(pattern);
cr.maskSurface(surface, 0, 0);
cr.paint();
cr.paintWithAlpha(1);
cr.setDash([1, 0.5], 1);
cr.stroke();
cr.strokePreserve();
cr.inFill(0, 0);
cr.inStroke(0, 0);
cr.copyPage();
cr.showPage();
cr.translate(10, 10);
cr.scale(10, 10);
cr.rotate(180);
cr.identityMatrix();
cr.showText('foobar');
cr.moveTo(0, 0);
cr.setDash([], 1);
cr.lineTo(1, 0);
cr.lineTo(1, 1);
cr.lineTo(0, 1);
cr.closePath();
let path = cr.copyPath();
cr.fill();
cr.appendPath(path);
cr.stroke();
}).not.toThrow();
});
it('has methods when created from a C function', function () {
if (GLib.getenv('ENABLE_GTK') !== 'yes') {
pending('GTK disabled');
return;
}
Gtk.init(null);
let win = new Gtk.OffscreenWindow();
let da = new Gtk.DrawingArea();
win.add(da);
da.realize();
cr = Gdk.cairo_create(da.window);
expect(cr.save).toBeDefined();
expect(cr.getTarget()).toBeDefined();
});
});
describe('pattern', function () {
it('has typechecks', function () {
expect(() => cr.setSource({})).toThrow();
expect(() => cr.setSource(surface)).toThrow();
});
});
describe('solid pattern', function () {
it('can be created from RGB static method', function () {
let p1 = Cairo.SolidPattern.createRGB(1, 2, 3);
expect(_ts(p1)).toEqual('SolidPattern');
cr.setSource(p1);
expect(_ts(cr.getSource())).toEqual('SolidPattern');
});
it('can be created from RGBA static method', function () {
let p2 = Cairo.SolidPattern.createRGBA(1, 2, 3, 4);
expect(_ts(p2)).toEqual('SolidPattern');
cr.setSource(p2);
expect(_ts(cr.getSource())).toEqual('SolidPattern');
});
});
describe('surface pattern', function () {
it('can be created and added as a source', function () {
let p1 = new Cairo.SurfacePattern(surface);
expect(_ts(p1)).toEqual('SurfacePattern');
cr.setSource(p1);
expect(_ts(cr.getSource())).toEqual('SurfacePattern');
});
});
describe('linear gradient', function () {
it('can be created and added as a source', function () {
let p1 = new Cairo.LinearGradient(1, 2, 3, 4);
expect(_ts(p1)).toEqual('LinearGradient');
cr.setSource(p1);
expect(_ts(cr.getSource())).toEqual('LinearGradient');
});
});
describe('radial gradient', function () {
it('can be created and added as a source', function () {
let p1 = new Cairo.RadialGradient(1, 2, 3, 4, 5, 6);
expect(_ts(p1)).toEqual('RadialGradient');
cr.setSource(p1);
expect(_ts(cr.getSource())).toEqual('RadialGradient');
});
});
describe('path', function () {
it('has typechecks', function () {
expect(() => cr.appendPath({})).toThrow();
expect(() => cr.appendPath(surface)).toThrow();
});
});
describe('surface', function () {
it('has typechecks', function () {
expect(() => new Cairo.Context({})).toThrow();
const pattern = new Cairo.SurfacePattern(surface);
expect(() => new Cairo.Context(pattern)).toThrow();
});
it('can access the device scale', function () {
let [x, y] = surface.getDeviceScale();
expect(x).toEqual(1);
expect(y).toEqual(1);
surface.setDeviceScale(1.2, 1.2);
[x, y] = surface.getDeviceScale();
expect(x).toEqual(1.2);
expect(y).toEqual(1.2);
});
it('can access the device offset', function () {
let [x, y] = surface.getDeviceOffset();
expect(x).toEqual(0);
expect(y).toEqual(0);
surface.setDeviceOffset(50, 50);
[x, y] = surface.getDeviceOffset();
expect(x).toEqual(50);
expect(y).toEqual(50);
});
it('can be finalized', function () {
expect(() => {
let _surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 10, 10);
let _cr = new Cairo.Context(_surface);
_surface.finish();
_cr.stroke();
}).toThrow();
expect(() => {
let _surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 10, 10);
_surface.finish();
_surface.flush();
}).not.toThrow();
});
});
describe('GI test suite', function () {
describe('for context', function () {
it('can be marshalled as a return value', function () {
const outCr = Regress.test_cairo_context_full_return();
const outSurface = outCr.getTarget();
expect(outSurface.getFormat()).toEqual(Cairo.Format.ARGB32);
expect(outSurface.getWidth()).toEqual(10);
expect(outSurface.getHeight()).toEqual(10);
});
it('can be marshalled as an in parameter', function () {
expect(() => Regress.test_cairo_context_none_in(cr)).not.toThrow();
});
});
describe('for surface', function () {
['none', 'full'].forEach(transfer => {
it(`can be marshalled as a transfer-${transfer} return value`, function () {
const outSurface = Regress[`test_cairo_surface_${transfer}_return`]();
expect(outSurface.getFormat()).toEqual(Cairo.Format.ARGB32);
expect(outSurface.getWidth()).toEqual(10);
expect(outSurface.getHeight()).toEqual(10);
});
});
it('can be marshalled as an in parameter', function () {
expect(() => Regress.test_cairo_surface_none_in(surface)).not.toThrow();
});
it('can be marshalled as an out parameter', function () {
const outSurface = Regress.test_cairo_surface_full_out();
expect(outSurface.getFormat()).toEqual(Cairo.Format.ARGB32);
expect(outSurface.getWidth()).toEqual(10);
expect(outSurface.getHeight()).toEqual(10);
});
});
it('can be marshalled through a signal handler', function () {
let o = new Regress.TestObj();
let foreignSpy = jasmine.createSpy('sig-with-foreign-struct');
o.connect('sig-with-foreign-struct', foreignSpy);
o.emit_sig_with_foreign_struct();
expect(foreignSpy).toHaveBeenCalledWith(o, cr);
});
it('can have its type inferred as a foreign struct', function () {
expect(() => GIMarshallingTests.gvalue_in_with_type(cr, Cairo.Context))
.not.toThrow();
expect(() => GIMarshallingTests.gvalue_in_with_type(surface, Cairo.Surface))
.not.toThrow();
});
});
});
describe('Cairo imported via GI', function () {
const giCairo = imports.gi.cairo;
it('has the same functionality as imports.cairo', function () {
const surface = new giCairo.ImageSurface(Cairo.Format.ARGB32, 1, 1);
void new giCairo.Context(surface);
});
it('has boxed types from the GIR file', function () {
void new giCairo.RectangleInt();
});
});
|