AtlatestRepositorysigil-ui

sigil-ui / tree / testtest-tree.sgl

1(import (sigil test)
2 (sigil ui raster)
3 (sigil ui font)
4 (sigil ui mask)
5 (sigil ui display)
6 (sigil ui theme)
7 (sigil ui tree))
8
9;; An 8x2 font with space, "A" (full top row) and "B" (left pixel of each
10;; row), and its 16x4 double.
11(define font-1x
12 (make-bitmap-font "t-8x2" 8 2 (bytevector #x00 #x00 #xFF #x00 #x80 #x80) (list 32 65 66)))
13(define font-2x
14 (make-bitmap-font "t-16x4" 16 4
15 (bytevector #x00 #x00 #x00 #x00 #x00 #x00 #x00 #x00
16 #xFF #xFF #xFF #xFF #x00 #x00 #x00 #x00
17 #xC0 #x00 #xC0 #x00 #xC0 #x00 #xC0 #x00)
18 (list 32 65 66)))
19(define dot
20 (make-mask-set (list (cons 1 (make-mask 2 2 (bytevector 255 136 136 255)))
21 (cons 2 (make-mask 4 4 (make-bytevector 16 255))))))
23(define theme
24 (make-theme (list (cons 'bg #x0E0E0C) (cons 'fg #xE6E2D5) (cons 'accent #xEDBB45))
25 (list (cons 'text (make-font-set (list (cons 1 font-1x) (cons 2 font-2x)))))
26 (list (cons 'dot dot))))
28(define (new-ui s) (make-ui theme (make-canvas (* s 64) (* s 24)) s 'bg))
30;; The canvas a fresh ui paints for element e: the reference for every
31;; incremental update.
32(define (fresh-pixels e s)
33 (let ((u (new-ui s))) (ui-update! u e) (canvas->rgb (ui-canvas u))))
35(define (same-as-fresh? u e)
36 (equal? (canvas->rgb (ui-canvas u)) (fresh-pixels e (ui-scale u))))
38(define (stats-list u)
39 (let ((st (ui-stats u)))
40 (list (stats-leaves-kept st) (stats-leaves-built st) (stats-leaves-moved st)
41 (stats-components-skipped st) (stats-components-run st))))
43(define (scene n)
44 (group 0 0
45 (list (rect 0 0 64 2 'accent)
46 (label 4 4 "AB A" 'fg)
47 (runs 4 8 (list (cons "A" 'fg) (cons "BB" 'accent)))
48 (image (+ 40 n) 12 'dot 'fg)
49 (blend-rect 2 18 10 3 'accent 136))))
51(test-group "first update"
52 (test "paints the whole screen and reports it"
53 (let ((u (new-ui 1)))
54 (assert-equal (list (list 0 0 64 24)) (ui-update! u (scene 0)))
55 (assert-equal (list 0 5 0 0 0) (stats-list u))
56 (assert-equal #xEDBB45 (canvas-pixel (ui-canvas u) 10 1))
57 (assert-equal #x0E0E0C (canvas-pixel (ui-canvas u) 10 3))))
58 (test "runs advance by the text width of each run"
59 (let ((u (new-ui 1)))
60 (ui-update! u (runs 0 0 (list (cons "A" 'fg) (cons "B" 'accent))))
61 (assert-equal #xE6E2D5 (canvas-pixel (ui-canvas u) 7 0))
62 (assert-equal #xEDBB45 (canvas-pixel (ui-canvas u) 8 0))
63 (assert-equal #x0E0E0C (canvas-pixel (ui-canvas u) 9 0)))))
65(test-group "reconciliation"
66 (test "an equal tree repaints nothing"
67 (let ((u (new-ui 1)))
68 (ui-update! u (scene 0))
69 (assert-equal '() (ui-update! u (scene 0)))
70 (assert-equal (list 5 0 0 0 0) (stats-list u))))
71 (test "a moved leaf damages its old and new bounds only"
72 (let ((u (new-ui 1)))
73 (ui-update! u (scene 0))
74 (assert-equal (list (list 40 12 3 2)) (ui-update! u (scene 1)))
75 (assert-equal (list 4 1 0 0 0) (stats-list u))
76 (assert-true (same-as-fresh? u (scene 1)))))
77 (test "a removed child damages its old bounds"
78 (let ((u (new-ui 1)))
79 (ui-update! u (group 0 0 (list (rect 0 0 4 4 'fg) (rect 10 10 4 4 'fg))))
80 (assert-equal (list (list 10 10 4 4)) (ui-update! u (group 0 0 (list (rect 0 0 4 4 'fg)))))
81 (assert-true (same-as-fresh? u (group 0 0 (list (rect 0 0 4 4 'fg)))))))
82 (test "a group offset moves every child"
83 (let ((u (new-ui 1)))
84 (ui-update! u (group 0 0 (list (rect 0 0 4 4 'fg) (label 0 6 "A" 'fg))))
85 (ui-update! u (group 3 1 (list (rect 0 0 4 4 'fg) (label 0 6 "A" 'fg))))
86 (assert-equal (list 0 0 2 0 0) (stats-list u))
87 (assert-true (same-as-fresh? u (group 3 1 (list (rect 0 0 4 4 'fg) (label 0 6 "A" 'fg)))))))
88 (test "a kind change at the same position replaces the node"
89 (let ((u (new-ui 1)))
90 (ui-update! u (group 0 0 (list (rect 0 0 4 4 'fg))))
91 (ui-update! u (group 0 0 (list (group 0 0 (list (rect 0 0 4 4 'accent))))))
92 (assert-true (same-as-fresh? u (group 0 0 (list (group 0 0 (list (rect 0 0 4 4 'accent)))))))))
93 (test "a group replaced by a smaller leaf damages the group's old bounds"
94 (let ((u (new-ui 1)))
95 (ui-update! u (group 0 0 (list (group 0 0 (list (rect 0 0 4 4 'fg) (rect 30 10 4 4 'fg))))))
96 (ui-update! u (group 0 0 (list (rect 0 0 4 4 'fg))))
97 (assert-equal #x0E0E0C (canvas-pixel (ui-canvas u) 31 11))
98 (assert-true (same-as-fresh? u (group 0 0 (list (rect 0 0 4 4 'fg))))))))
100(define (rows keys keyed?)
101 (group 0 0
102 (map (lambda (k)
103 (let ((e (rect (* 8 k) 20 6 2 'fg)))
104 (if keyed? (keyed k e) e)))
105 keys)))
107(test-group "keyed identity"
108 (test "keyed children keep their nodes when one is inserted in front"
109 (let ((u (new-ui 1)))
110 (ui-update! u (rows (list 1 2 3 4) #t))
111 (ui-update! u (rows (list 0 1 2 3 4) #t))
112 (assert-equal (list 4 1 0 0 0) (stats-list u))
113 (assert-true (same-as-fresh? u (rows (list 0 1 2 3 4) #t)))))
114 (test "unkeyed children are matched by position, so all of them change"
115 (let ((u (new-ui 1)))
116 (ui-update! u (rows (list 1 2 3 4) #f))
117 (ui-update! u (rows (list 0 1 2 3 4) #f))
118 (assert-equal (list 0 5 0 0 0) (stats-list u))
119 (assert-true (same-as-fresh? u (rows (list 0 1 2 3 4) #f)))))
120 (test "a reordered keyed child is repainted in its new paint order"
121 (let* ((u (new-ui 1))
122 (a (keyed 'a (rect 0 0 8 8 'fg)))
123 (b (keyed 'b (rect 4 4 8 8 'accent))))
124 (ui-update! u (group 0 0 (list a b)))
125 (ui-update! u (group 0 0 (list b a)))
126 (assert-equal #xE6E2D5 (canvas-pixel (ui-canvas u) 5 5))
127 (assert-true (same-as-fresh? u (group 0 0 (list b a))))))
128 (test "duplicate keys are refused"
129 (assert-error (ui-update! (new-ui 1) (group 0 0 (list (keyed 1 (rect 0 0 1 1 'fg))
130 (keyed 1 (rect 2 0 1 1 'fg))))))))
132(define runs-count 0)
133(define (counted-view n)
134 (set! runs-count (+ runs-count 1))
135 (label 0 0 (if (= n 0) "A" "B") 'fg))
137(test-group "components"
138 (test "a component is skipped while its argument is equal"
139 (set! runs-count 0)
140 (let ((u (new-ui 1)))
141 (ui-update! u (group 0 0 (list (component counted-view 0) (rect 0 10 2 2 'fg))))
142 (ui-update! u (group 0 0 (list (component counted-view 0) (rect 0 12 2 2 'fg))))
143 (assert-equal 1 runs-count)
144 (assert-equal (list 0 1 0 1 0) (stats-list u))
145 (ui-update! u (group 0 0 (list (component counted-view 1) (rect 0 12 2 2 'fg))))
146 (assert-equal 2 runs-count)
147 (assert-true (same-as-fresh? u (group 0 0 (list (component counted-view 1) (rect 0 12 2 2 'fg)))))))
148 (test "a moved component is placed again without running"
149 (set! runs-count 0)
150 (let ((u (new-ui 1)))
151 (ui-update! u (group 0 0 (list (component counted-view 0))))
152 (ui-update! u (group 5 3 (list (component counted-view 0))))
153 (assert-equal 1 runs-count)
154 (assert-equal (list 0 0 1 1 0) (stats-list u))
155 (assert-true (same-as-fresh? u (group 5 3 (list (component counted-view 0))))))))
157;; A deterministic walk through many trees: after every update the canvas
158;; must equal a fresh render of the same tree, at both scales.
159(define (lcg x) (modulo (+ (* x 1103515245) 12345) 2147483648))
160(define (random-tree seed)
161 (let loop ((i 0) (x seed) (acc '()))
162 (if (= i 6)
163 (group 0 0 (reverse acc))
164 (let* ((x1 (lcg x)) (x2 (lcg x1)) (x3 (lcg x2)) (k (modulo x1 6)))
165 (loop (+ i 1) x3
166 (cons (keyed (modulo x2 9)
167 (case k
168 ((0) (rect (modulo x2 60) (modulo x3 20) 1 4 'accent))
169 ((1) (label (modulo x2 48) (modulo x3 22) "AB" 'fg))
170 ((2) (image (modulo x2 60) (modulo x3 22) 'dot 'accent))
171 ((3) (blend-rect (modulo x2 50) (modulo x3 20) 9 3 'fg 85))
172 ((4) (runs (modulo x2 40) (modulo x3 22) (list (cons "B" 'accent) (cons " A" 'fg))))
173 (else (group (modulo x2 40) (modulo x3 12) (list (rect 0 0 6 6 'fg) (label 2 8 "A" 'accent))))))
174 acc))))))
175(define (dedupe-keys tree)
176 ;; random keys may repeat; keep the first of each
177 (let loop ((cs (cadddr tree)) (seen '()) (acc '()))
178 (cond ((null? cs) (group 0 0 (reverse acc)))
179 ((memv (cadr (car cs)) seen) (loop (cdr cs) seen acc))
180 (else (loop (cdr cs) (cons (cadr (car cs)) seen) (cons (car cs) acc))))))
182(test-group "incremental equals fresh"
183 (test "40 random updates at scale 1 and 2"
184 (for-each
185 (lambda (s)
186 (let ((u (new-ui s)))
187 (let loop ((i 0) (seed 7))
188 (when (< i 40)
189 (let ((t (dedupe-keys (random-tree seed))))
190 (ui-update! u t)
191 (unless (same-as-fresh? u t)
192 (assert-fail (string-append "update " (number->string i) " at scale "
193 (number->string s) " differs from a fresh render")))
194 (loop (+ i 1) (lcg (+ seed i))))))))
195 (list 1 2))
196 (assert-true #t)))