processing 0.5.33 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,6 +13,30 @@ def browser(width, height, headless: true)
13
13
  hash[key] ||= Ferrum::Browser.new headless: headless, window_size: [width, height]
14
14
  end
15
15
 
16
+ def get_svg_html(width, height, svg_xml)
17
+ <<~END
18
+ <html>
19
+ <head>
20
+ <style type="text/css">
21
+ body {margin: 0;}
22
+ </style>
23
+ <script type="text/javascript">
24
+ window.onload = function() {
25
+ let e = document.createElement("span");
26
+ e.id = 'completed';
27
+ document.body.appendChild(e);
28
+ }
29
+ </script>
30
+ </head>
31
+ <body>
32
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 #{width} #{height}">
33
+ #{svg_xml}
34
+ </svg>
35
+ </body>
36
+ </html>
37
+ END
38
+ end
39
+
16
40
  def get_p5rb_html(width, height, draw_src, webgl: false)
17
41
  <<~END
18
42
  <html>
@@ -57,17 +81,17 @@ def sleep_until(try: 3, timeout: 10, &block)
57
81
  limit = now[timeout]
58
82
  try -= 1
59
83
  next if try > 0
60
- raise 'Drawing timed out in p5.rb'
84
+ raise 'Drawing timed out in browser'
61
85
  end
62
86
  sleep 0.1
63
87
  end
64
88
  end
65
89
 
66
- def draw_p5rb(width, height, draw_src, path, headless: true, webgl: false)
90
+ def draw_on_browser(width, height, path, html, headless: true)
67
91
  b = browser width, height, headless: headless
68
92
  unless File.exist? path
69
93
  b.reset
70
- b.main_frame.content = get_p5rb_html width, height, draw_src, webgl: webgl
94
+ b.main_frame.content = html
71
95
  sleep_until do
72
96
  b.evaluate 'document.querySelector("#completed") != null'
73
97
  end
@@ -75,3 +99,13 @@ def draw_p5rb(width, height, draw_src, path, headless: true, webgl: false)
75
99
  end
76
100
  b.device_pixel_ratio
77
101
  end
102
+
103
+ def draw_svg(width, height, svg_xml, path, headless: true)
104
+ html = get_svg_html width, height, svg_xml
105
+ draw_on_browser width, height, path, html, headless: headless
106
+ end
107
+
108
+ def draw_p5rb(width, height, draw_src, path, headless: true, webgl: false)
109
+ html = get_p5rb_html width, height, draw_src, webgl: webgl
110
+ draw_on_browser width, height, path, html, headless: headless
111
+ end
data/test/helper.rb CHANGED
@@ -20,9 +20,11 @@ DEFAULT_DRAW_HEADER = <<~END
20
20
  strokeWeight 50
21
21
  END
22
22
 
23
+ THRESHOLD_TO_BE_FIXED = 0.0
23
24
 
24
- def test_with_p5?()
25
- ENV['TEST_WITH_P5'] == '1'
25
+
26
+ def test_with_browser?()
27
+ (ENV['TEST_WITH_BROWSER'] || '0') != '0'
26
28
  end
27
29
 
28
30
  def md5(s)
@@ -107,16 +109,47 @@ def assert_equal_draw(
107
109
  assert_equal_pixels e, a, threshold: threshold
108
110
  end
109
111
 
112
+ def assert_svg_draw(
113
+ svg_xml,
114
+ width: 1000, height: 1000, threshold: 0.99, label: test_label, **kwargs)
115
+
116
+ source = <<~END
117
+ background 255
118
+ shape Processing::SVGLoader.new(self).parse <<~SVG
119
+ <?xml version="1.0" encoding="UTF-8"?>
120
+ <svg xmlns="http://www.w3.org/2000/svg"
121
+ xmlns:xlink="http://www.w3.org/1999/xlink"
122
+ viewBox="0 0 100 100">
123
+ #{svg_xml}
124
+ </svg>
125
+ SVG
126
+ END
127
+ assert_draw_on_browser(
128
+ source, width, height, threshold, label, **kwargs
129
+ ) do |path|
130
+ draw_svg width, height, svg_xml, path, **kwargs
131
+ end
132
+ end
133
+
110
134
  def assert_p5_draw(
111
135
  *sources, default_header: DEFAULT_DRAW_HEADER,
112
136
  width: 1000, height: 1000, threshold: 0.99, label: test_label, **kwargs)
113
137
 
114
- return unless test_with_p5?
115
-
116
138
  source = [default_header, *sources].compact.join("\n")
117
- path = draw_output_path "#{label}_expected", source
139
+ assert_draw_on_browser(
140
+ source, width, height, threshold, label, **kwargs
141
+ ) do |path|
142
+ draw_p5rb width, height, source, path, **kwargs
143
+ end
144
+ end
118
145
 
119
- pd = draw_p5rb width, height, source, path, **kwargs
146
+ def assert_draw_on_browser(
147
+ source, width, height, threshold, label, **kwargs, &draw_on_browser)
148
+
149
+ return unless test_with_browser?
150
+
151
+ path = draw_output_path "#{label}_expected", source
152
+ pd = draw_on_browser.call path
120
153
  actual = test_draw source, width: width, height: height, pixelDensity: pd
121
154
  actual.save path.sub('_expected', '_actual')
122
155
 
@@ -136,4 +169,4 @@ def assert_p5_fill_stroke(*sources, **kwargs)
136
169
  end
137
170
 
138
171
 
139
- require_relative 'p5' if test_with_p5?
172
+ require_relative 'browser' if test_with_browser?
@@ -0,0 +1,94 @@
1
+ require_relative 'helper'
2
+
3
+
4
+ class TestColor < Test::Unit::TestCase
5
+
6
+ P = Processing
7
+ G = P::Graphics
8
+
9
+ def test_rgb_color()
10
+ g = graphics
11
+
12
+ g.colorMode G::RGB, 255
13
+ c = g.color 10, 20, 30, 40
14
+ assert_equal 0x280a141e, c
15
+ assert_equal [10, 20, 30, 40], [g.red(c), g.green(c), g.blue(c), g.alpha(c)]
16
+
17
+ g.colorMode G::RGB, 1.0
18
+ c = g.color 0.1, 0.2, 0.3, 0.4
19
+ assert_equal 0x6619334c, c
20
+ assert_in_delta 0.1, g.red(c), 1 / 256.0
21
+ assert_in_delta 0.2, g.green(c), 1 / 256.0
22
+ assert_in_delta 0.3, g.blue(c), 1 / 256.0
23
+ assert_in_delta 0.4, g.alpha(c), 1 / 256.0
24
+
25
+ g.colorMode G::RGB, 0.5
26
+ c = g.color 0.1, 0.2, 0.3, 0.4
27
+ assert_equal 0xcc336699, c
28
+ assert_in_delta 0.1, g.red(c), 1 / 256.0
29
+ assert_in_delta 0.2, g.green(c), 1 / 256.0
30
+ assert_in_delta 0.3, g.blue(c), 1 / 256.0
31
+ assert_in_delta 0.4, g.alpha(c), 1 / 256.0
32
+ end
33
+
34
+ def test_hsb_color()
35
+ g = graphics
36
+
37
+ g.colorMode G::HSB, 1.0
38
+ c = g.color 0.1, 0.2, 0.3, 0.4
39
+ assert_in_delta 0.1, g.hue(c), 1 / 256.0
40
+ assert_in_delta 0.2, g.saturation(c), 1 / 256.0
41
+ assert_in_delta 0.3, g.brightness(c), 1 / 256.0
42
+ assert_in_delta 0.4, g.alpha(c), 1 / 256.0
43
+
44
+ g.colorMode G::HSB, 0.5
45
+ c = g.color 0.1, 0.2, 0.3, 0.4
46
+ assert_in_delta 0.1, g.hue(c), 1 / 256.0
47
+ assert_in_delta 0.2, g.saturation(c), 1 / 256.0
48
+ assert_in_delta 0.3, g.brightness(c), 1 / 256.0
49
+ assert_in_delta 0.4, g.alpha(c), 1 / 256.0
50
+ end
51
+
52
+ def test_parse_color()
53
+ g = graphics
54
+
55
+ assert_equal 0xff010203, g.color('#010203')
56
+ assert_equal 0x04010203, g.color('#01020304')
57
+ assert_equal 0xff112233, g.color('#123')
58
+ assert_equal 0x44112233, g.color('#1234')
59
+ assert_equal 0xff0000ff, g.color('blue')
60
+ assert_equal 0x040000ff, g.color('blue', 4)
61
+ end
62
+
63
+ def test_color_codes()
64
+ g = graphics
65
+
66
+ assert_equal 0xffff0000, g.color(:red)
67
+ assert_equal 0xff008000, g.color(:GREEN)
68
+ assert_equal 0xff0000ff, g.color('blue')
69
+
70
+ assert_raise(ArgumentError) {g.color :unknown}
71
+ end
72
+
73
+ def test_default_background_color()
74
+ assert_p5_draw '', default_header: '', threshold: THRESHOLD_TO_BE_FIXED
75
+ end
76
+
77
+ def test_default_fill_color()
78
+ assert_p5_draw <<~END, default_header: nil
79
+ background 100
80
+ noStroke
81
+ rect 100, 100, 500, 500
82
+ END
83
+ end
84
+
85
+ def test_default_stroke_color()
86
+ assert_p5_draw <<~END, default_header: nil
87
+ background 100
88
+ noFill
89
+ strokeWeight 50
90
+ line 100, 100, 500, 500
91
+ END
92
+ end
93
+
94
+ end# TestColor
@@ -6,51 +6,6 @@ class TestGraphicsContext < Test::Unit::TestCase
6
6
  P = Processing
7
7
  G = P::Graphics
8
8
 
9
- THRESHOLD_TO_BE_FIXED = 0.0
10
-
11
- def test_rgb_color()
12
- g = graphics
13
-
14
- g.colorMode G::RGB, 255
15
- c = g.color 10, 20, 30, 40
16
- assert_equal 0x280a141e, c
17
- assert_equal [10, 20, 30, 40], [g.red(c), g.green(c), g.blue(c), g.alpha(c)]
18
-
19
- g.colorMode G::RGB, 1.0
20
- c = g.color 0.1, 0.2, 0.3, 0.4
21
- assert_equal 0x6619334c, c
22
- assert_in_delta 0.1, g.red(c), 1 / 256.0
23
- assert_in_delta 0.2, g.green(c), 1 / 256.0
24
- assert_in_delta 0.3, g.blue(c), 1 / 256.0
25
- assert_in_delta 0.4, g.alpha(c), 1 / 256.0
26
-
27
- g.colorMode G::RGB, 0.5
28
- c = g.color 0.1, 0.2, 0.3, 0.4
29
- assert_equal 0xcc336699, c
30
- assert_in_delta 0.1, g.red(c), 1 / 256.0
31
- assert_in_delta 0.2, g.green(c), 1 / 256.0
32
- assert_in_delta 0.3, g.blue(c), 1 / 256.0
33
- assert_in_delta 0.4, g.alpha(c), 1 / 256.0
34
- end
35
-
36
- def test_hsb_color()
37
- g = graphics
38
-
39
- g.colorMode G::HSB, 1.0
40
- c = g.color 0.1, 0.2, 0.3, 0.4
41
- assert_in_delta 0.1, g.hue(c), 1 / 256.0
42
- assert_in_delta 0.2, g.saturation(c), 1 / 256.0
43
- assert_in_delta 0.3, g.brightness(c), 1 / 256.0
44
- assert_in_delta 0.4, g.alpha(c), 1 / 256.0
45
-
46
- g.colorMode G::HSB, 0.5
47
- c = g.color 0.1, 0.2, 0.3, 0.4
48
- assert_in_delta 0.1, g.hue(c), 1 / 256.0
49
- assert_in_delta 0.2, g.saturation(c), 1 / 256.0
50
- assert_in_delta 0.3, g.brightness(c), 1 / 256.0
51
- assert_in_delta 0.4, g.alpha(c), 1 / 256.0
52
- end
53
-
54
9
  def test_colorMode()
55
10
  g = graphics
56
11
  assert_equal G::RGB, g.colorMode
@@ -81,25 +36,37 @@ class TestGraphicsContext < Test::Unit::TestCase
81
36
  assert_raise {g.blendMode LEFT}
82
37
  end
83
38
 
84
- def test_default_background_color()
85
- assert_p5_draw '', default_header: '', threshold: THRESHOLD_TO_BE_FIXED
86
- end
87
-
88
- def test_default_fill_color()
89
- assert_p5_draw <<~END, default_header: nil
90
- background 100
91
- noStroke
92
- rect 100, 100, 500, 500
39
+ def test_strokeCap()
40
+ header = <<~END
41
+ noFill
42
+ strokeWeight 200
93
43
  END
44
+ footer = <<~END
45
+ line 200, 200, 800, 800
46
+ END
47
+ assert_p5_draw header, '', footer
48
+ assert_p5_draw header, 'strokeCap ROUND', footer
49
+ assert_p5_draw header, 'strokeCap SQUARE', footer
50
+ assert_p5_draw header, 'strokeCap PROJECT', footer
94
51
  end
95
52
 
96
- def test_default_stroke_color()
97
- assert_p5_draw <<~END, default_header: nil
98
- background 100
53
+ def test_strokeJoin()
54
+ header = <<~END
99
55
  noFill
100
- strokeWeight 50
101
- line 100, 100, 500, 500
56
+ strokeWeight 200
57
+ strokeCap SQUARE
58
+ END
59
+ footer = <<~END
60
+ beginShape
61
+ vertex 200, 200
62
+ vertex 800, 500
63
+ vertex 200, 800
64
+ endShape
102
65
  END
66
+ assert_p5_draw header, '', footer
67
+ assert_p5_draw header, 'strokeJoin MITER', footer
68
+ assert_p5_draw header, 'strokeJoin BEVEL', footer, threshold: THRESHOLD_TO_BE_FIXED
69
+ assert_p5_draw header, 'strokeJoin ROUND', footer
103
70
  end
104
71
 
105
72
  def test_textFont()
data/test/test_shape.rb CHANGED
@@ -366,14 +366,33 @@ class TestShape < Test::Unit::TestCase
366
366
 
367
367
  def test_fill()
368
368
  assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
369
+ noFill
369
370
  noStroke
370
371
  HEADER
371
- fill 0, 255, 0
372
+ fill 0, 0, 255
372
373
  rect 100, 100, 500, 400
373
374
  EXPECTED
374
375
  s = createShape
375
376
  s.beginShape
376
- s.fill 0, 255, 0
377
+ s.fill 0, 0, 255
378
+ s.vertex 100, 100
379
+ s.vertex 600, 100
380
+ s.vertex 600, 500
381
+ s.vertex 100, 500
382
+ s.endShape
383
+ shape s
384
+ ACTUAL
385
+
386
+ assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
387
+ noFill
388
+ noStroke
389
+ HEADER
390
+ fill 0, 0, 255
391
+ rect 100, 100, 500, 400
392
+ EXPECTED
393
+ fill 0, 0, 255
394
+ s = createShape
395
+ s.beginShape
377
396
  s.vertex 100, 100
378
397
  s.vertex 600, 100
379
398
  s.vertex 600, 500
@@ -383,11 +402,50 @@ class TestShape < Test::Unit::TestCase
383
402
  ACTUAL
384
403
  end
385
404
 
405
+ def test_stroke()
406
+ assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
407
+ noFill
408
+ noStroke
409
+ HEADER
410
+ stroke 0, 0, 255
411
+ rect 100, 100, 500, 400
412
+ EXPECTED
413
+ s = createShape
414
+ s.beginShape
415
+ s.stroke 0, 0, 255
416
+ s.vertex 100, 100
417
+ s.vertex 600, 100
418
+ s.vertex 600, 500
419
+ s.vertex 100, 500
420
+ s.endShape CLOSE
421
+ shape s
422
+ ACTUAL
423
+
424
+ assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
425
+ noFill
426
+ noStroke
427
+ HEADER
428
+ stroke 0, 0, 255
429
+ rect 100, 100, 500, 400
430
+ EXPECTED
431
+ stroke 0, 0, 255
432
+ s = createShape
433
+ s.beginShape
434
+ s.vertex 100, 100
435
+ s.vertex 600, 100
436
+ s.vertex 600, 500
437
+ s.vertex 100, 500
438
+ s.endShape CLOSE
439
+ shape s
440
+ ACTUAL
441
+ end
442
+
386
443
  def test_setFill()
387
444
  assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
445
+ noFill
388
446
  noStroke
389
447
  HEADER
390
- fill 0, 255, 0
448
+ fill 0, 0, 255
391
449
  rect 100, 100, 500, 400
392
450
  EXPECTED
393
451
  s = createShape
@@ -397,18 +455,83 @@ class TestShape < Test::Unit::TestCase
397
455
  s.vertex 600, 500
398
456
  s.vertex 100, 500
399
457
  s.endShape
400
- s.setFill 0, 255, 0
458
+ s.setFill 0, 0, 255
459
+ shape s
460
+ ACTUAL
461
+
462
+ assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
463
+ noFill
464
+ noStroke
465
+ HEADER
466
+ fill 0, 0, 255
467
+ ellipse 300, 400, 500, 400
468
+ EXPECTED
469
+ s = createShape ELLIPSE, 300, 400, 500, 400
470
+ s.setFill 0, 0, 255
471
+ shape s
472
+ ACTUAL
473
+ end
474
+
475
+ def test_setStroke()
476
+ assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
477
+ noFill
478
+ noStroke
479
+ HEADER
480
+ stroke 0, 0, 255
481
+ rect 100, 100, 500, 400
482
+ EXPECTED
483
+ s = createShape
484
+ s.beginShape
485
+ s.vertex 100, 100
486
+ s.vertex 600, 100
487
+ s.vertex 600, 500
488
+ s.vertex 100, 500
489
+ s.endShape CLOSE
490
+ s.setStroke 0, 0, 255
401
491
  shape s
402
492
  ACTUAL
403
493
 
404
494
  assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
495
+ noFill
405
496
  noStroke
406
497
  HEADER
407
- fill 0, 255, 0
498
+ stroke 0, 0, 255
499
+ ellipse 300, 400, 500, 400
500
+ EXPECTED
501
+ s = createShape ELLIPSE, 300, 400, 500, 400
502
+ s.setStroke 0, 0, 255
503
+ shape s
504
+ ACTUAL
505
+ end
506
+
507
+ def test_setStrokeWeight()
508
+ assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
509
+ noFill
510
+ strokeWeight 1
511
+ HEADER
512
+ strokeWeight 100
513
+ rect 100, 100, 500, 400
514
+ EXPECTED
515
+ s = createShape
516
+ s.beginShape
517
+ s.vertex 100, 100
518
+ s.vertex 600, 100
519
+ s.vertex 600, 500
520
+ s.vertex 100, 500
521
+ s.endShape CLOSE
522
+ s.setStrokeWeight 100
523
+ shape s
524
+ ACTUAL
525
+
526
+ assert_equal_draw <<~HEADER, <<~EXPECTED, <<~ACTUAL
527
+ noFill
528
+ strokeWeight 1
529
+ HEADER
530
+ strokeWeight 100
408
531
  ellipse 300, 400, 500, 400
409
532
  EXPECTED
410
533
  s = createShape ELLIPSE, 300, 400, 500, 400
411
- s.setFill 0, 255, 0
534
+ s.setStrokeWeight 100
412
535
  shape s
413
536
  ACTUAL
414
537
  end