|
| 1 | +// A subset of node-canvas tests converted to plain javascript that reproes the |
| 2 | +// sync gc problem for easier debugging in lldb |
| 3 | + |
| 4 | +'use strict' |
| 5 | + |
| 6 | +/** |
| 7 | + * Module dependencies. |
| 8 | + */ |
| 9 | +const assert = require('assert') |
| 10 | + |
| 11 | +const { |
| 12 | + createCanvas, |
| 13 | + registerFont, |
| 14 | + Canvas, |
| 15 | + deregisterAllFonts, |
| 16 | +} = require('.'); |
| 17 | + |
| 18 | +(function () { |
| 19 | + const c = new Canvas(10, 10) |
| 20 | + assert.throws(function () { Canvas.prototype.width }, /invalid argument/i) |
| 21 | + assert(!c.hasOwnProperty('width')) |
| 22 | + assert('width' in c) |
| 23 | + assert('width' in Canvas.prototype) |
| 24 | +})(); |
| 25 | + |
| 26 | +(function () { |
| 27 | + assert.throws(() => createCanvas(40_000, 100)) |
| 28 | + assert.throws(() => createCanvas(4000, 40_000)) |
| 29 | + assert.throws(() => createCanvas(40_000, 40_000)) |
| 30 | +})(); |
| 31 | + |
| 32 | +(function () { |
| 33 | + // WebIDL says negative numbers should throw an |
| 34 | + // IndexSizeError, but we skip [EnforceRange] |
| 35 | + const canvas = createCanvas(-0xfffffff1, -0xfffffff1) |
| 36 | + assert.equal(canvas.width, 15) |
| 37 | + assert.equal(canvas.height, 15) |
| 38 | + canvas.width = -0xfffffff2 |
| 39 | + canvas.height = -0xfffffff2 |
| 40 | + assert.equal(canvas.width, 14) |
| 41 | + assert.equal(canvas.height, 14) |
| 42 | +})(); |
| 43 | + |
| 44 | +(function () { |
| 45 | + // Minimal test to make sure nothing is thrown |
| 46 | + registerFont('./examples/pfennigFont/Pfennig.ttf', { family: 'Pfennig' }) |
| 47 | + registerFont('./examples/pfennigFont/PfennigBold.ttf', { family: 'Pfennig', weight: 'bold' }) |
| 48 | + |
| 49 | + // Test to multi byte file path support |
| 50 | + registerFont('./examples/pfennigFont/pfennigMultiByte🚀.ttf', { family: 'Pfennig' }) |
| 51 | + |
| 52 | + deregisterAllFonts() |
| 53 | +})(); |
| 54 | + |
| 55 | +(function () { |
| 56 | + const canvas = createCanvas(200, 200) |
| 57 | + const ctx = canvas.getContext('2d'); |
| 58 | + |
| 59 | + ['fillStyle', 'strokeStyle', 'shadowColor'].forEach(function (prop) { |
| 60 | + ctx[prop] = '#FFFFFF' |
| 61 | + assert.equal('#ffffff', ctx[prop], prop + ' #FFFFFF -> #ffffff, got ' + ctx[prop]) |
| 62 | + |
| 63 | + ctx[prop] = '#FFF' |
| 64 | + assert.equal('#ffffff', ctx[prop], prop + ' #FFF -> #ffffff, got ' + ctx[prop]) |
| 65 | + |
| 66 | + ctx[prop] = 'rgba(128, 200, 128, 1)' |
| 67 | + assert.equal('#80c880', ctx[prop], prop + ' rgba(128, 200, 128, 1) -> #80c880, got ' + ctx[prop]) |
| 68 | + |
| 69 | + ctx[prop] = 'rgba(128,80,0,0.5)' |
| 70 | + assert.equal('rgba(128, 80, 0, 0.50)', ctx[prop], prop + ' rgba(128,80,0,0.5) -> rgba(128, 80, 0, 0.5), got ' + ctx[prop]) |
| 71 | + |
| 72 | + ctx[prop] = 'rgba(128,80,0,0.75)' |
| 73 | + assert.equal('rgba(128, 80, 0, 0.75)', ctx[prop], prop + ' rgba(128,80,0,0.75) -> rgba(128, 80, 0, 0.75), got ' + ctx[prop]) |
| 74 | + |
| 75 | + if (prop === 'shadowColor') return |
| 76 | + |
| 77 | + const grad = ctx.createLinearGradient(0, 0, 0, 150) |
| 78 | + ctx[prop] = grad |
| 79 | + assert.strictEqual(grad, ctx[prop], prop + ' pattern getter failed') |
| 80 | + }) |
| 81 | +})(); |
| 82 | + |
| 83 | +(function () { |
| 84 | + const canvas = createCanvas(200, 200) |
| 85 | + const ctx = canvas.getContext('2d') |
| 86 | + |
| 87 | + ctx.fillStyle = '#ffccaa' |
| 88 | + assert.equal('#ffccaa', ctx.fillStyle) |
| 89 | + |
| 90 | + ctx.fillStyle = '#FFCCAA' |
| 91 | + assert.equal('#ffccaa', ctx.fillStyle) |
| 92 | + |
| 93 | + ctx.fillStyle = '#FCA' |
| 94 | + assert.equal('#ffccaa', ctx.fillStyle) |
| 95 | + |
| 96 | + ctx.fillStyle = '#fff' |
| 97 | + ctx.fillStyle = '#FGG' |
| 98 | + assert.equal('#ff0000', ctx.fillStyle) |
| 99 | + |
| 100 | + ctx.fillStyle = ' #FCA' |
| 101 | + assert.equal('#ffccaa', ctx.fillStyle) |
| 102 | + |
| 103 | + ctx.fillStyle = ' #ffccaa' |
| 104 | + assert.equal('#ffccaa', ctx.fillStyle) |
| 105 | + |
| 106 | + |
| 107 | + ctx.fillStyle = '#fff' |
| 108 | + ctx.fillStyle = 'afasdfasdf' |
| 109 | + assert.equal('#ffffff', ctx.fillStyle) |
| 110 | + |
| 111 | + // #rgba and #rrggbbaa |
| 112 | + ctx.fillStyle = '#ffccaa80' |
| 113 | + assert.equal('rgba(255, 204, 170, 0.50)', ctx.fillStyle) |
| 114 | + |
| 115 | + ctx.fillStyle = '#acf8' |
| 116 | + assert.equal('rgba(170, 204, 255, 0.53)', ctx.fillStyle) |
| 117 | + |
| 118 | + ctx.fillStyle = '#BEAD' |
| 119 | + assert.equal('rgba(187, 238, 170, 0.87)', ctx.fillStyle) |
| 120 | + |
| 121 | + ctx.fillStyle = 'rgb(255,255,255)' |
| 122 | + assert.equal('#ffffff', ctx.fillStyle) |
| 123 | + |
| 124 | + ctx.fillStyle = 'rgb(0,0,0)' |
| 125 | + assert.equal('#000000', ctx.fillStyle) |
| 126 | + |
| 127 | + ctx.fillStyle = 'rgb( 0 , 0 , 0)' |
| 128 | + assert.equal('#000000', ctx.fillStyle) |
| 129 | + |
| 130 | + ctx.fillStyle = 'rgba( 0 , 0 , 0, 1)' |
| 131 | + assert.equal('#000000', ctx.fillStyle) |
| 132 | + |
| 133 | + ctx.fillStyle = 'rgba( 255, 200, 90, 0.5)' |
| 134 | + assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle) |
| 135 | + |
| 136 | + ctx.fillStyle = 'rgba( 255, 200, 90, 0.75)' |
| 137 | + assert.equal('rgba(255, 200, 90, 0.75)', ctx.fillStyle) |
| 138 | + |
| 139 | + ctx.fillStyle = 'rgba( 255, 200, 90, 0.7555)' |
| 140 | + assert.equal('rgba(255, 200, 90, 0.75)', ctx.fillStyle) |
| 141 | + |
| 142 | + ctx.fillStyle = 'rgba( 255, 200, 90, .7555)' |
| 143 | + assert.equal('rgba(255, 200, 90, 0.75)', ctx.fillStyle) |
| 144 | + |
| 145 | + ctx.fillStyle = 'rgb(0, 0, 9000)' |
| 146 | + assert.equal('#0000ff', ctx.fillStyle) |
| 147 | + |
| 148 | + ctx.fillStyle = 'rgba(0, 0, 0, 42.42)' |
| 149 | + assert.equal('#000000', ctx.fillStyle) |
| 150 | + |
| 151 | + ctx.fillStyle = 'rgba(255, 250, 255)'; |
| 152 | + assert.equal('#fffaff', ctx.fillStyle); |
| 153 | + |
| 154 | + ctx.fillStyle = 'rgba(124, 58, 26, 0)'; |
| 155 | + assert.equal('rgba(124, 58, 26, 0.00)', ctx.fillStyle); |
| 156 | + |
| 157 | + ctx.fillStyle = 'rgba( 255, 200, 90, 40%)' |
| 158 | + assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle) |
| 159 | + |
| 160 | + ctx.fillStyle = 'rgba( 255, 200, 90, 50 %)' |
| 161 | + assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle) |
| 162 | + |
| 163 | + ctx.fillStyle = 'rgba( 255, 200, 90, 10%)' |
| 164 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 165 | + |
| 166 | + ctx.fillStyle = 'rgba( 255, 200, 90, 10 %)' |
| 167 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 168 | + |
| 169 | + ctx.fillStyle = 'rgba( 255, 200, 90 / 40%)' |
| 170 | + assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle) |
| 171 | + |
| 172 | + ctx.fillStyle = 'rgba( 255, 200, 90 / 0.5)' |
| 173 | + assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle) |
| 174 | + |
| 175 | + ctx.fillStyle = 'rgba( 255, 200, 90 / 10%)' |
| 176 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 177 | + |
| 178 | + ctx.fillStyle = 'rgba( 255, 200, 90 / 0.1)' |
| 179 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 180 | + |
| 181 | + ctx.fillStyle = 'rgba( 255 200 90 / 10%)' |
| 182 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 183 | + |
| 184 | + ctx.fillStyle = 'rgba( 255 200 90 0.1)' |
| 185 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 186 | + |
| 187 | + ctx.fillStyle = 'rgb(0, 0, 0, 42.42)' |
| 188 | + assert.equal('#000000', ctx.fillStyle) |
| 189 | + |
| 190 | + ctx.fillStyle = 'rgb(255, 250, 255)'; |
| 191 | + assert.equal('#fffaff', ctx.fillStyle); |
| 192 | + |
| 193 | + ctx.fillStyle = 'rgb(124, 58, 26, 0)'; |
| 194 | + assert.equal('rgba(124, 58, 26, 0.00)', ctx.fillStyle); |
| 195 | + |
| 196 | + ctx.fillStyle = 'rgb( 255, 200, 90, 40%)' |
| 197 | + assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle) |
| 198 | + |
| 199 | + ctx.fillStyle = 'rgb( 255, 200, 90, 50 %)' |
| 200 | + assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle) |
| 201 | + |
| 202 | + ctx.fillStyle = 'rgb( 255, 200, 90, 10%)' |
| 203 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 204 | + |
| 205 | + ctx.fillStyle = 'rgb( 255, 200, 90, 10 %)' |
| 206 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 207 | + |
| 208 | + ctx.fillStyle = 'rgb( 255, 200, 90 / 40%)' |
| 209 | + assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle) |
| 210 | + |
| 211 | + ctx.fillStyle = 'rgb( 255, 200, 90 / 0.5)' |
| 212 | + assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle) |
| 213 | + |
| 214 | + ctx.fillStyle = 'rgb( 255, 200, 90 / 10%)' |
| 215 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 216 | + |
| 217 | + ctx.fillStyle = 'rgb( 255, 200, 90 / 0.1)' |
| 218 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 219 | + |
| 220 | + ctx.fillStyle = 'rgb( 255 200 90 / 10%)' |
| 221 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 222 | + |
| 223 | + ctx.fillStyle = 'rgb( 255 200 90 0.1)' |
| 224 | + assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) |
| 225 | + |
| 226 | + ctx.fillStyle = ' rgb( 255 100 90 0.1)' |
| 227 | + assert.equal('rgba(255, 100, 90, 0.10)', ctx.fillStyle) |
| 228 | + |
| 229 | + ctx.fillStyle = 'rgb(124.00, 58, 26, 0)'; |
| 230 | + assert.equal('rgba(124, 58, 26, 0.00)', ctx.fillStyle); |
| 231 | + |
| 232 | + ctx.fillStyle = 'rgb( 255, 200.09, 90, 40%)' |
| 233 | + assert.equal('rgba(255, 201, 90, 0.40)', ctx.fillStyle) |
| 234 | + |
| 235 | + ctx.fillStyle = 'rgb( 255.00, 199.03, 90, 50 %)' |
| 236 | + assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle) |
| 237 | + |
| 238 | + ctx.fillStyle = 'rgb( 255, 300.09, 90, 40%)' |
| 239 | + assert.equal('rgba(255, 255, 90, 0.40)', ctx.fillStyle) |
| 240 | + // hsl / hsla tests |
| 241 | + |
| 242 | + ctx.fillStyle = 'hsl(0, 0%, 0%)' |
| 243 | + assert.equal('#000000', ctx.fillStyle) |
| 244 | + |
| 245 | + ctx.fillStyle = 'hsl(3600, -10%, -10%)' |
| 246 | + assert.equal('#000000', ctx.fillStyle) |
| 247 | + |
| 248 | + ctx.fillStyle = 'hsl(10, 100%, 42%)' |
| 249 | + assert.equal('#d62400', ctx.fillStyle) |
| 250 | + |
| 251 | + ctx.fillStyle = 'hsl(370, 120%, 42%)' |
| 252 | + assert.equal('#d62400', ctx.fillStyle) |
| 253 | + |
| 254 | + ctx.fillStyle = 'hsl(0, 100%, 100%)' |
| 255 | + assert.equal('#ffffff', ctx.fillStyle) |
| 256 | + |
| 257 | + ctx.fillStyle = 'hsl(0, 150%, 150%)' |
| 258 | + assert.equal('#ffffff', ctx.fillStyle) |
| 259 | + |
| 260 | + ctx.fillStyle = 'hsl(237, 76%, 25%)' |
| 261 | + assert.equal('#0f1470', ctx.fillStyle) |
| 262 | + |
| 263 | + ctx.fillStyle = ' hsl(0, 150%, 150%)' |
| 264 | + assert.equal('#ffffff', ctx.fillStyle) |
| 265 | + |
| 266 | + ctx.fillStyle = 'hsl(240, 73%, 25%)' |
| 267 | + assert.equal('#11116e', ctx.fillStyle) |
| 268 | + |
| 269 | + ctx.fillStyle = 'hsl(262, 32%, 42%)' |
| 270 | + assert.equal('#62498d', ctx.fillStyle) |
| 271 | + |
| 272 | + ctx.fillStyle = 'hsla(0, 0%, 0%, 1)' |
| 273 | + assert.equal('#000000', ctx.fillStyle) |
| 274 | + |
| 275 | + ctx.fillStyle = 'hsla(0, 100%, 100%, 1)' |
| 276 | + assert.equal('#ffffff', ctx.fillStyle) |
| 277 | + |
| 278 | + ctx.fillStyle = 'hsla(120, 25%, 75%, 0.5)' |
| 279 | + assert.equal('rgba(175, 207, 175, 0.50)', ctx.fillStyle) |
| 280 | + |
| 281 | + ctx.fillStyle = 'hsla(240, 75%, 25%, 0.75)' |
| 282 | + assert.equal('rgba(16, 16, 112, 0.75)', ctx.fillStyle) |
| 283 | + |
| 284 | + ctx.fillStyle = 'hsla(172.0, 33.00000e0%, 42%, 1)' |
| 285 | + assert.equal('#488e85', ctx.fillStyle) |
| 286 | + |
| 287 | + ctx.fillStyle = 'hsl(124.5, 76.1%, 47.6%)' |
| 288 | + assert.equal('#1dd62b', ctx.fillStyle) |
| 289 | + |
| 290 | + ctx.fillStyle = 'hsl(1.24e2, 760e-1%, 4.7e1%)' |
| 291 | + assert.equal('#1dd329', ctx.fillStyle) |
| 292 | + |
| 293 | + // case-insensitive (#235) |
| 294 | + ctx.fillStyle = 'sILveR' |
| 295 | + assert.equal(ctx.fillStyle, '#c0c0c0') |
| 296 | +})(); |
| 297 | + |
| 298 | +(function () { |
| 299 | + let canvas = createCanvas(10, 10) |
| 300 | + assert.equal(canvas.type, 'image') |
| 301 | + canvas = createCanvas(10, 10, 'pdf') |
| 302 | + assert.equal(canvas.type, 'pdf') |
| 303 | + canvas = createCanvas(10, 10, 'svg') |
| 304 | + assert.equal(canvas.type, 'svg') |
| 305 | + canvas = createCanvas(10, 10, 'hey') |
| 306 | + assert.equal(canvas.type, 'image') |
| 307 | +})(); |
| 308 | + |
| 309 | +(function () { |
| 310 | + const canvas = createCanvas(200, 300) |
| 311 | + const ctx = canvas.getContext('2d') |
| 312 | + assert.ok(typeof ctx === 'object') |
| 313 | + assert.equal(canvas, ctx.canvas, 'context.canvas is not canvas') |
| 314 | + assert.equal(ctx, canvas.context, 'canvas.context is not context') |
| 315 | + |
| 316 | + const MAX_IMAGE_SIZE = 32767; |
| 317 | + |
| 318 | + [ |
| 319 | + [0, 0, 1], |
| 320 | + [1, 0, 1], |
| 321 | + [MAX_IMAGE_SIZE, 0, 1], |
| 322 | + [MAX_IMAGE_SIZE + 1, 0, 3], |
| 323 | + [MAX_IMAGE_SIZE, MAX_IMAGE_SIZE, null], |
| 324 | + [MAX_IMAGE_SIZE + 1, MAX_IMAGE_SIZE, 3], |
| 325 | + [MAX_IMAGE_SIZE + 1, MAX_IMAGE_SIZE + 1, 3], |
| 326 | + [Math.pow(2, 30), 0, 3], |
| 327 | + [Math.pow(2, 30), 1, 3], |
| 328 | + [Math.pow(2, 32), 0, 1], |
| 329 | + [Math.pow(2, 32), 1, 1] |
| 330 | + ].forEach(params => { |
| 331 | + const width = params[0] |
| 332 | + const height = params[1] |
| 333 | + const errorLevel = params[2] |
| 334 | + |
| 335 | + let level = 3 |
| 336 | + |
| 337 | + try { |
| 338 | + const canvas = createCanvas(width, height) |
| 339 | + level-- |
| 340 | + |
| 341 | + const ctx = canvas.getContext('2d') |
| 342 | + level-- |
| 343 | + |
| 344 | + ctx.getImageData(0, 0, 1, 1) |
| 345 | + level-- |
| 346 | + } catch (err) {} |
| 347 | + |
| 348 | + if (errorLevel !== null) { assert.strictEqual(level, errorLevel) } |
| 349 | + }) |
| 350 | +})(); |
0 commit comments