ferroui-base 0.3.0

The base library of FerroUI: the property system, layout, input, styling, media, text, animation, data binding and the compositor
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
//! The compositor test fixture (the reference `CompositorTestsBase` /
//! `CompositorTestServices`) and the compositor tests that need no
//! controls: invalidation, clipping, lifetime, hit testing, child
//! synchronization, target rendering and readback.
//!
//! The reference tests build their trees from `Canvas`, `Border` and
//! `Decorator`. Those live in the controls crate, so here a visual that
//! fills its bounds stands in for `Border` and bounds are assigned directly
//! instead of being produced by layout.

use super::hit_testing::{CompositionHitTestAabbTree, PointCompositionHitTester};
use super::server::{ServerCompositionTarget, ServerCompositionVisual};
use super::{CompositingRenderer, Compositor, ElementComposition, ICompositionTargetDebugEvents};
use crate::layout::ILayoutRoot;
use crate::media::{
    Brushes, DrawingBrush, DrawingContext, Geometry, GeometryDrawing, IBrush, MediaContext, RectangleGeometry, Stretch,
    VisualBrush,
};
use crate::platform::LtrbRect;
use crate::rendering::testing::{ManualRenderLoop, MockPlatformRenderInterface};
use crate::rendering::{IHitTester, IPresentationSource, IRenderer};
use crate::threading::Dispatcher;
use crate::*;
use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};
use std::sync::Arc;

/// A visual that fills its bounds with its background, like a border.
#[repr(C)]
pub(crate) struct TestBorder {
    base: Visual,
    background: RefCell<Option<Rc<dyn IBrush>>>,
    custom_hit_test: Cell<Option<bool>>,
}

ferro_class!(TestBorder: Visual);
ferro_impl_classes!(TestBorder: FerroObjectImpl, StyledElementImpl);

impl VisualImpl for TestBorder {
    fn render(this: &Self, context: &mut DrawingContext) {
        let background = this.background.borrow().clone();
        if let Some(background) = background {
            context.fill_rectangle(&background, Rect::from_size(this.bounds().size()), 0.0);
        }
    }

    fn custom_hit_test(this: &Self, _point: Point) -> Option<bool> {
        this.custom_hit_test.get()
    }
}

impl TestBorder {
    pub(crate) fn new() -> Ref<Self> {
        instantiate(Self {
            base: Visual::construct(),
            background: RefCell::new(None),
            custom_hit_test: Cell::new(None),
        })
    }

    /// A border with a background at the given bounds.
    pub(crate) fn filled(x: f64, y: f64, width: f64, height: f64) -> Ref<Self> {
        let border = Self::new();
        border.set_background(Some(Brushes::red()));
        border.set_bounds(Rect::new(x, y, width, height));
        border
    }

    pub(crate) fn set_background(&self, value: Option<Rc<dyn IBrush>>) {
        *self.background.borrow_mut() = value;
        self.invalidate_visual();
    }

    fn add<T: ObjectType + Upcast<Visual>>(&self, child: &Ref<T>) {
        self.visual_children().add(child.clone().upcast());
        // Panels report child changes through their logical children.
        self.invalidate_visual();
        if let Some(source) = self.presentation_source() {
            source.renderer().recalculate_children(self);
        }
    }

    fn remove<T: ObjectType + Upcast<Visual>>(&self, child: &Ref<T>) {
        self.visual_children().remove(&child.clone().upcast());
        if let Some(source) = self.presentation_source() {
            source.renderer().recalculate_children(self);
        }
    }
}

struct TestSource {
    root: Ref<Visual>,
    renderer: RefCell<Option<Rc<CompositingRenderer>>>,
    client_size: Cell<Size>,
}

impl IPresentationSource for TestSource {
    fn root_visual(&self) -> Option<Ref<Visual>> {
        Some(self.root.clone())
    }
    fn render_scaling(&self) -> f64 {
        1.0
    }
    fn renderer(&self) -> Rc<dyn IRenderer> {
        self.renderer.borrow().clone().expect("the renderer is created with the source")
    }
    fn layout_root(&self) -> Rc<dyn ILayoutRoot> {
        unimplemented!("not used by these tests")
    }
    fn hit_tester(&self) -> Rc<dyn IHitTester> {
        self.renderer.borrow().clone().expect("the renderer is created with the source")
    }
    fn input_root(&self) -> Rc<dyn crate::input::IInputRoot> {
        unimplemented!("not used by these tests")
    }
    fn client_size(&self) -> Size {
        self.client_size.get()
    }
}

#[derive(Default)]
struct DebugEvents {
    rects: RefCell<Vec<Rect>>,
    rendered_visuals: Cell<i32>,
    visited_visuals: Cell<i32>,
}

// SAFETY: the tests create and use the receiver on one thread; the impls
// only satisfy the thread-safety bound of the contract.
unsafe impl Send for DebugEvents {}
unsafe impl Sync for DebugEvents {}

impl DebugEvents {
    fn reset(&self) {
        self.rects.borrow_mut().clear();
        self.rendered_visuals.set(0);
        self.visited_visuals.set(0);
    }
}

impl ICompositionTargetDebugEvents for DebugEvents {
    fn rendered_visuals(&self) -> i32 {
        self.rendered_visuals.get()
    }
    fn set_rendered_visuals(&self, value: i32) {
        self.rendered_visuals.set(value);
    }
    fn visited_visuals(&self) -> i32 {
        self.visited_visuals.get()
    }
    fn set_visited_visuals(&self, value: i32) {
        self.visited_visuals.set(value);
    }
    fn rect_invalidated(&self, rc: LtrbRect) {
        self.rects.borrow_mut().push(rc.to_rect());
    }
}

/// The reference `CompositorCanvas`: a compositor, a renderer over a root
/// visual, and a canvas-like visual as the only child of the root.
struct CompositorCanvas {
    // Declared first: dropped before the scopes below.
    canvas: Ref<TestBorder>,
    root: Ref<TestBorder>,
    renderer: Rc<CompositingRenderer>,
    source: Rc<TestSource>,
    compositor: Rc<Compositor>,
    events: std::sync::Arc<DebugEvents>,
    render_loop: Arc<ManualRenderLoop>,
    render_interface: Rc<MockPlatformRenderInterface>,
    locator_scope: Rc<dyn crate::reactive::IDisposable>,
    _dispatcher_scope: crate::threading::UnitTestDispatcherScope,
}

impl CompositorCanvas {
    fn new() -> CompositorCanvas {
        Self::with_size(Size::new(1000.0, 1000.0))
    }

    fn with_size(size: Size) -> CompositorCanvas {
        let dispatcher_scope = Dispatcher::unit_test_scope();
        let (locator_scope, render_interface) = MockPlatformRenderInterface::install();
        let render_loop = ManualRenderLoop::new();
        let compositor = Compositor::with_scheduler(
            render_loop.clone(),
            None,
            true,
            &MediaContext::instance().scheduler(),
            Dispatcher::ui_thread(),
            None,
            None,
        );

        let root = TestBorder::new();
        root.set_bounds(Rect::from_size(size));
        let source = Rc::new(TestSource {
            root: root.clone().upcast(),
            renderer: RefCell::new(None),
            client_size: Cell::new(size),
        });
        let as_source: Rc<dyn IPresentationSource> = source.clone();
        let weak: Weak<dyn IPresentationSource> = Rc::downgrade(&as_source);
        let renderer = CompositingRenderer::with_weak_root(weak, &compositor, std::sync::Arc::new(Vec::new));
        *source.renderer.borrow_mut() = Some(renderer.clone());

        root.set_presentation_source_for_root_visual(Some(as_source));
        renderer.set_root(Some(root.clone().upcast()));
        renderer.start();

        let events = std::sync::Arc::new(DebugEvents::default());
        renderer.composition_target().set_debug_events(Some(events.clone()));

        let canvas = TestBorder::new();
        canvas.set_bounds(Rect::from_size(size));
        let services = CompositorCanvas {
            canvas: canvas.clone(),
            root,
            renderer,
            source,
            compositor,
            events,
            render_loop,
            render_interface,
            locator_scope,
            _dispatcher_scope: dispatcher_scope,
        };
        services.root.add(&canvas);
        services.run_jobs();
        services.events.reset();
        services
    }

    fn run_jobs(&self) {
        Dispatcher::ui_thread().run_jobs(None);
        self.render_loop.tick();
        Dispatcher::ui_thread().run_jobs(None);
    }

    fn assert_rects(&self, rects: &[Rect]) {
        self.run_jobs();
        let distinct = |rects: &[Rect]| {
            let mut texts: Vec<String> = rects.iter().map(|r| r.to_string()).collect();
            texts.sort();
            texts.dedup();
            texts
        };
        assert_eq!(distinct(rects), distinct(&self.events.rects.borrow()));
        self.events.rects.borrow_mut().clear();
    }

    fn assert_rendered_visuals(&self, visited_visuals: i32, render_visuals: i32) {
        self.run_jobs();
        assert_eq!(visited_visuals, self.events.visited_visuals.get());
        assert_eq!(render_visuals, self.events.rendered_visuals.get());
        self.events.rects.borrow_mut().clear();
    }

    fn assert_hit_test(&self, x: f64, y: f64, filter: Option<&dyn Fn(&Visual) -> bool>, expected: &[&Ref<TestBorder>]) {
        self.run_jobs();
        let tested = self.renderer.hit_test(Point::new(x, y), &self.root, filter);
        let expected: Vec<Ref<Visual>> = expected.iter().map(|v| (*v).clone().upcast()).collect();
        assert_eq!(expected, tested);
    }

    fn assert_hit_test_first(&self, x: f64, y: f64, filter: Option<&dyn Fn(&Visual) -> bool>, expected: Option<&Ref<TestBorder>>) {
        self.run_jobs();
        let tested = self.renderer.hit_test_first(Point::new(x, y), &self.root, filter);
        assert_eq!(expected.map(|v| v.clone().upcast::<Visual>()), tested);
    }

    fn server_visual(&self, visual: &Visual) -> Rc<ServerCompositionVisual> {
        let id = visual.composition_visual().expect("the visual is attached").server();
        self.compositor.server().get::<ServerCompositionVisual>(id).expect("the server visual exists")
    }
}

impl Drop for CompositorCanvas {
    fn drop(&mut self) {
        self.renderer.stop();
        self.root.set_presentation_source_for_root_visual(None);
        self.renderer.dispose();
        *self.source.renderer.borrow_mut() = None;
        self.locator_scope.dispose();
    }
}

// --- CompositorInvalidationTests ---------------------------------------------------

#[test]
fn control_should_invalidate_own_rect_when_added() {
    let s = CompositorCanvas::new();
    let control = TestBorder::filled(30.0, 50.0, 20.0, 10.0);
    s.canvas.add(&control);
    s.assert_rects(&[Rect::new(30.0, 50.0, 20.0, 10.0)]);
}

#[test]
fn control_should_invalidate_own_rect_when_removed() {
    let s = CompositorCanvas::new();
    let control = TestBorder::filled(30.0, 50.0, 20.0, 10.0);
    s.canvas.add(&control);
    s.run_jobs();
    s.events.rects.borrow_mut().clear();
    s.canvas.remove(&control);
    s.assert_rects(&[Rect::new(30.0, 50.0, 20.0, 10.0)]);
}

#[test]
fn sibling_controls_should_invalidate_union_rect_when_removed() {
    let s = CompositorCanvas::new();
    let control = TestBorder::filled(30.0, 10.0, 20.0, 10.0);
    let control2 = TestBorder::filled(30.0, 50.0, 20.0, 10.0);
    control2.set_background(Some(Brushes::blue()));
    s.canvas.add(&control);
    s.canvas.add(&control2);
    s.run_jobs();
    s.events.rects.borrow_mut().clear();
    s.canvas.remove(&control);
    s.canvas.remove(&control2);
    s.assert_rects(&[Rect::new(30.0, 10.0, 20.0, 50.0)]);
}

#[test]
fn control_should_invalidate_both_own_rects_when_moved() {
    let s = CompositorCanvas::new();
    let control = TestBorder::filled(30.0, 50.0, 20.0, 10.0);
    s.canvas.add(&control);
    s.run_jobs();
    s.events.rects.borrow_mut().clear();
    control.set_bounds(Rect::new(55.0, 50.0, 20.0, 10.0));
    s.assert_rects(&[Rect::new(30.0, 50.0, 20.0, 10.0), Rect::new(55.0, 50.0, 20.0, 10.0)]);
}

/// A decorator with a padding of 10 around a 20x10 border.
fn decorator_with_child() -> (Ref<TestBorder>, Ref<TestBorder>) {
    let control = TestBorder::new();
    control.set_bounds(Rect::new(30.0, 50.0, 40.0, 30.0));
    let child = TestBorder::filled(10.0, 10.0, 20.0, 10.0);
    control.add(&child);
    (control, child)
}

#[test]
fn control_should_invalidate_child_rects_when_moved() {
    let s = CompositorCanvas::new();
    let (control, _child) = decorator_with_child();
    s.canvas.add(&control);
    s.run_jobs();
    s.events.rects.borrow_mut().clear();
    control.set_bounds(Rect::new(55.0, 50.0, 40.0, 30.0));
    s.assert_rects(&[Rect::new(40.0, 60.0, 20.0, 10.0), Rect::new(65.0, 60.0, 20.0, 10.0)]);
}

#[test]
fn control_should_invalidate_child_rects_when_becomes_invisible() {
    let s = CompositorCanvas::new();
    let (control, _child) = decorator_with_child();
    s.canvas.add(&control);
    s.run_jobs();
    s.events.rects.borrow_mut().clear();
    control.set_is_visible(false);
    s.assert_rects(&[Rect::new(40.0, 60.0, 20.0, 10.0)]);
}

// --- CompositorInvalidationClippingTests -------------------------------------------

fn count_visuals(visual: &Visual) -> i32 {
    let mut count = 1;
    if let Some(children) = visual.visual_children_snapshot() {
        for child in children.iter() {
            count += count_visuals(child);
        }
    }
    count
}

// The root of the fixture (the reference tree has two visuals above the canvas).
const TOP_LEVEL_OVERHEAD: i32 = 1;

fn do_not_re_render_unaffected_visual_trees(
    clip_to_bounds: bool,
    clip_geometry: bool,
    canvas_has_content: bool,
    expected_visited_visuals_count: i32,
    expected_rendered_visuals_count: i32,
) {
    let s = CompositorCanvas::new();
    for (x, y) in [(0.0, 0.0), (30.0, 50.0)] {
        let border = TestBorder::filled(x, y, 20.0, 10.0);
        border.set_clip_to_bounds(clip_to_bounds);
        if clip_geometry {
            border.set_clip(RectangleGeometry::with_rect(Rect::from_size(Size::new(20.0, 10.0))).upcast::<crate::media::Geometry>());
        }
        s.canvas.add(&border);
    }

    if canvas_has_content {
        s.canvas.set_background(Some(Brushes::green()));
    }
    s.run_jobs();
    s.events.reset();

    assert_eq!(3 + TOP_LEVEL_OVERHEAD, count_visuals(&s.root), "the tree of the test is broken");

    // invalidate border1
    s.canvas.visual_children().snapshot()[0].set_is_visible(false);
    s.run_jobs();

    s.assert_rendered_visuals(expected_visited_visuals_count, expected_rendered_visuals_count);
}

#[test]
fn do_not_re_render_unaffected_visual_trees_cases() {
    // If canvas itself has no background, the second render won't draw any visuals at all, since
    // root visual's subtree bounds will exactly match the second visual
    do_not_re_render_unaffected_visual_trees(false, false, false, 1, 0);
    do_not_re_render_unaffected_visual_trees(true, false, false, 1, 0);
    do_not_re_render_unaffected_visual_trees(false, true, false, 1, 0);
    // If canvas has background, the second render will draw only the canvas visual itself
    // (and the root above it); every visual is visited.
    do_not_re_render_unaffected_visual_trees(false, false, true, 3 + TOP_LEVEL_OVERHEAD, 1 + TOP_LEVEL_OVERHEAD);
    do_not_re_render_unaffected_visual_trees(true, false, true, 3 + TOP_LEVEL_OVERHEAD, 1 + TOP_LEVEL_OVERHEAD);
    do_not_re_render_unaffected_visual_trees(false, true, true, 3 + TOP_LEVEL_OVERHEAD, 1 + TOP_LEVEL_OVERHEAD);
}

// --- CompositorLifetimeTests -------------------------------------------------------

#[test]
fn invalidate_visual_does_not_update_rendering_target_when_rendering_stopped() {
    let services = CompositorCanvas::with_size(Size::new(200.0, 200.0));
    let composition_target = services.renderer.composition_target().clone();
    assert!(composition_target.is_enabled());
    assert_eq!(Size::new(200.0, 200.0), composition_target.size());

    // Stop rendering and invalidate a visual: this should not result in an update
    services.renderer.stop();
    services.source.client_size.set(Size::new(300.0, 300.0));
    services.root.invalidate_visual();
    services.run_jobs();
    assert_eq!(Size::new(200.0, 200.0), composition_target.size());

    // Check that restarting rendering re-queues the pending invalidation
    services.renderer.start();
    services.run_jobs();
    assert_eq!(Size::new(300.0, 300.0), composition_target.size());
}

#[test]
fn visuals_removed_while_rendering_stopped_are_not_kept_by_renderer() {
    let services = CompositorCanvas::new();

    let kept = TestBorder::filled(30.0, 50.0, 20.0, 10.0);
    services.canvas.add(&kept);
    services.run_jobs();

    services.renderer.stop();

    let removed_child = TestBorder::new();
    removed_child.set_background(Some(Brushes::blue()));
    removed_child.set_bounds(Rect::new(0.0, 0.0, 20.0, 10.0));
    let removed = TestBorder::new();
    removed.set_bounds(Rect::new(100.0, 50.0, 20.0, 10.0));
    removed.add(&removed_child);
    services.canvas.add(&removed);
    kept.set_background(Some(Brushes::green()));
    services.run_jobs();

    assert!(services.renderer.has_pending_update_for_unit_tests(&removed));
    assert!(services.renderer.has_pending_update_for_unit_tests(&removed_child));

    services.canvas.remove(&removed);
    services.run_jobs();

    assert!(!services.renderer.has_pending_update_for_unit_tests(&removed));
    assert!(!services.renderer.has_pending_update_for_unit_tests(&removed_child));

    assert!(services.renderer.has_pending_update_for_unit_tests(&kept));
    assert!(services.renderer.has_pending_update_for_unit_tests(&services.canvas));

    services.renderer.start();
    services.run_jobs();

    assert!(!services.renderer.has_pending_update_for_unit_tests(&kept));
    assert!(!services.renderer.has_pending_update_for_unit_tests(&services.canvas));
    services.assert_hit_test(35.0, 55.0, None, &[&kept]);
    services.assert_hit_test(105.0, 55.0, None, &[]);
}

// --- CompositionChildVisualTests --------------------------------------------------

/// Clears its child visual when it leaves the tree, the usual counterpart to
/// setting one on attach.
#[repr(C)]
struct ClearingHost {
    base: Visual,
}

ferro_class!(ClearingHost: Visual);
ferro_impl_classes!(ClearingHost: FerroObjectImpl, StyledElementImpl);

impl VisualImpl for ClearingHost {
    fn on_detached_from_visual_tree(this: &Self, _e: &VisualTreeAttachmentEventArgs) {
        ElementComposition::set_element_child_visual(this, None);
    }
}

impl ClearingHost {
    fn new() -> Ref<Self> {
        instantiate(Self { base: Visual::construct() })
    }
}

#[test]
fn child_visual_cleared_during_detach_can_reattach() {
    let s = CompositorCanvas::new();
    let child = s.compositor.create_container_visual();
    let host = ClearingHost::new();
    host.set_bounds(Rect::new(0.0, 0.0, 100.0, 100.0));
    s.canvas.add(&host);
    s.run_jobs();
    ElementComposition::set_element_child_visual(&host, Some(child.clone()));
    s.run_jobs();

    s.canvas.remove(&host);
    s.run_jobs();
    assert!(child.parent().is_none());

    s.canvas.add(&host);
    s.run_jobs();
    ElementComposition::set_element_child_visual(&host, Some(child.clone()));
    s.run_jobs();

    let element_visual = ElementComposition::get_element_visual(&host).expect("the host is attached");
    assert!(element_visual.children().contains(&child));
}

// --- hit testing (CompositorHitTestingTests, without controls) ----------------------

#[test]
fn hit_test_should_find_controls_at_point() {
    let s = CompositorCanvas::new();
    let border = TestBorder::filled(450.0, 450.0, 100.0, 100.0);
    s.canvas.add(&border);

    s.assert_hit_test(500.0, 500.0, None, &[&border]);
    s.assert_hit_test(10.0, 10.0, None, &[]);
    s.assert_hit_test_first(500.0, 500.0, None, Some(&border));
    s.assert_hit_test_first(10.0, 10.0, None, None);
}

#[test]
fn hit_test_should_not_find_invisible_controls_at_point() {
    let s = CompositorCanvas::new();
    let border = TestBorder::filled(450.0, 450.0, 100.0, 100.0);
    let child = TestBorder::filled(0.0, 0.0, 100.0, 100.0);
    border.add(&child);
    border.set_is_visible(false);
    s.canvas.add(&border);

    s.assert_hit_test(500.0, 500.0, None, &[]);
}

#[test]
fn hit_test_should_not_find_control_outside_point() {
    let s = CompositorCanvas::new();
    let border = TestBorder::filled(450.0, 450.0, 100.0, 100.0);
    s.canvas.add(&border);

    s.assert_hit_test(10.0, 10.0, None, &[]);
    s.assert_hit_test(551.0, 500.0, None, &[]);
}

#[test]
fn hit_test_should_return_top_controls_first() {
    let s = CompositorCanvas::new();
    let bottom = TestBorder::filled(400.0, 400.0, 200.0, 200.0);
    let top = TestBorder::filled(450.0, 450.0, 100.0, 100.0);
    s.canvas.add(&bottom);
    s.canvas.add(&top);

    s.assert_hit_test(500.0, 500.0, None, &[&top, &bottom]);
    s.assert_hit_test_first(500.0, 500.0, None, Some(&top));
}

#[test]
fn hit_test_should_return_top_controls_first_with_z_index() {
    let s = CompositorCanvas::new();
    let a = TestBorder::filled(400.0, 400.0, 200.0, 200.0);
    a.set_z_index(1);
    let b = TestBorder::filled(450.0, 450.0, 100.0, 100.0);
    let c = TestBorder::filled(425.0, 425.0, 150.0, 150.0);
    c.set_z_index(2);
    s.canvas.add(&a);
    s.canvas.add(&b);
    s.canvas.add(&c);

    s.assert_hit_test(500.0, 500.0, None, &[&c, &a, &b]);

    // Changing the Z index reorders the composition children.
    b.set_z_index(3);
    s.assert_hit_test(500.0, 500.0, None, &[&b, &c, &a]);
}

#[test]
fn hit_test_should_find_control_translated_outside_parent_bounds() {
    let s = CompositorCanvas::new();
    let parent = TestBorder::filled(100.0, 100.0, 100.0, 100.0);
    let child = TestBorder::filled(150.0, 0.0, 50.0, 50.0);
    parent.add(&child);
    s.canvas.add(&parent);

    s.assert_hit_test(260.0, 110.0, None, &[&child]);

    // Clipping the parent to its bounds hides the child from hit testing.
    parent.set_clip_to_bounds(true);
    s.assert_hit_test(260.0, 110.0, None, &[]);
}

#[test]
fn hit_test_should_respect_geometry_clip() {
    let s = CompositorCanvas::new();
    let border = TestBorder::filled(100.0, 100.0, 200.0, 200.0);
    border.set_clip(RectangleGeometry::with_rect(Rect::new(0.0, 0.0, 50.0, 50.0)).upcast::<crate::media::Geometry>());
    s.canvas.add(&border);

    s.assert_hit_test(120.0, 120.0, None, &[&border]);
    s.assert_hit_test(200.0, 200.0, None, &[]);
}

#[test]
fn hit_test_should_respect_filter() {
    let s = CompositorCanvas::new();
    let parent = TestBorder::filled(100.0, 100.0, 100.0, 100.0);
    let child = TestBorder::filled(0.0, 0.0, 50.0, 50.0);
    parent.add(&child);
    let other = TestBorder::filled(100.0, 100.0, 100.0, 100.0);
    s.canvas.add(&other);
    s.canvas.add(&parent);

    s.assert_hit_test(110.0, 110.0, None, &[&child, &parent, &other]);

    // A filtered-out visual excludes its subtree.
    let parent_visual: Ref<Visual> = parent.clone().upcast();
    let filter = move |v: &Visual| v.to_ref() != parent_visual;
    s.assert_hit_test(110.0, 110.0, Some(&filter), &[&other]);
    s.assert_hit_test_first(110.0, 110.0, Some(&filter), Some(&other));
}

#[test]
fn hit_test_should_respect_render_transform() {
    let s = CompositorCanvas::new();
    let border = TestBorder::filled(100.0, 100.0, 100.0, 100.0);
    border.set_render_transform(Some(Rc::new(crate::media::immutable::ImmutableTransform::new(
        Matrix::create_translation(300.0, 0.0),
    ))));
    s.canvas.add(&border);

    s.assert_hit_test(150.0, 150.0, None, &[]);
    s.assert_hit_test(450.0, 150.0, None, &[&border]);
}

#[test]
fn hit_test_should_honour_custom_hit_test() {
    let s = CompositorCanvas::new();
    let border = TestBorder::filled(100.0, 100.0, 100.0, 100.0);
    border.custom_hit_test.set(Some(false));
    s.canvas.add(&border);
    s.assert_hit_test(150.0, 150.0, None, &[]);

    // A custom hit test may hit outside of what the visual draws.
    let outside = TestBorder::new();
    outside.set_bounds(Rect::new(300.0, 300.0, 10.0, 10.0));
    outside.custom_hit_test.set(Some(true));
    s.canvas.add(&outside);
    s.assert_hit_test(600.0, 600.0, None, &[&outside]);
}

#[test]
fn hit_test_uses_the_child_index_for_many_children() {
    let s = CompositorCanvas::new();
    let mut borders = Vec::new();
    for i in 0..100 {
        let border = TestBorder::filled((i % 10) as f64 * 50.0, (i / 10) as f64 * 50.0, 40.0, 40.0);
        s.canvas.add(&border);
        borders.push(border);
    }
    // Two overlapping visuals in different buckets.
    let overlapping = TestBorder::filled(0.0, 0.0, 40.0, 40.0);
    s.canvas.add(&overlapping);

    s.assert_hit_test(10.0, 10.0, None, &[&overlapping, &borders[0]]);
    s.assert_hit_test_first(10.0, 10.0, None, Some(&overlapping));
    s.assert_hit_test(460.0, 460.0, None, &[&borders[99]]);
    s.assert_hit_test(45.0, 45.0, None, &[]);

    // Moving and removing children keeps the index up to date.
    borders[99].set_bounds(Rect::new(700.0, 700.0, 40.0, 40.0));
    s.assert_hit_test(460.0, 460.0, None, &[]);
    s.assert_hit_test(710.0, 710.0, None, &[&borders[99]]);
    s.canvas.remove(&overlapping);
    s.assert_hit_test(10.0, 10.0, None, &[&borders[0]]);

    let canvas_visual = s.canvas.composition_visual().unwrap();
    let tree = canvas_visual.hit_test_children.borrow();
    let tree = tree.as_ref().expect("the children are indexed");
    tree.validate();
    assert_eq!(4, tree.bucket_count());
}

// --- the hit test index ------------------------------------------------------------

#[test]
fn aabb_tree_orders_candidates_and_follows_changes() {
    let s = CompositorCanvas::new();
    let mut borders = Vec::new();
    for i in 0..70 {
        let border = TestBorder::filled(i as f64 * 10.0, 0.0, 30.0, 30.0);
        s.canvas.add(&border);
        borders.push(border);
    }
    s.run_jobs();
    let canvas_visual = s.canvas.composition_visual().unwrap();
    let revision = {
        let readback = s.compositor.server().readback();
        readback.next_read();
        readback.read_revision()
    };
    let mut tree = CompositionHitTestAabbTree::new(canvas_visual.children().clone());
    tree.validate();
    assert_eq!(3, tree.bucket_count());

    let query = |tree: &mut CompositionHitTestAabbTree, x: f64| {
        let mut results = Vec::new();
        tree.query::<PointCompositionHitTester>(&Point::new(x, 5.0), &mut results, revision);
        let children = canvas_visual.children();
        results.iter().map(|v| children.index_of(v).unwrap()).collect::<Vec<_>>()
    };
    // The bounds are fattened by one unit; candidates are topmost first.
    assert_eq!(vec![33, 32, 31, 30], query(&mut tree, 330.5));
    assert_eq!(vec![0], query(&mut tree, 5.0));
    assert!(query(&mut tree, 900.0).is_empty());

    let removed = canvas_visual.children().get(32);
    tree.remove(&removed);
    tree.validate();
    assert_eq!(vec![33, 31, 30], query(&mut tree, 330.5));

    tree.clear();
    assert_eq!(0, tree.bucket_count());
    assert!(query(&mut tree, 5.0).is_empty());
}

// --- child synchronization -----------------------------------------------------------

#[test]
fn composition_children_follow_visual_children_and_z_index() {
    let s = CompositorCanvas::new();
    let a = TestBorder::filled(0.0, 0.0, 10.0, 10.0);
    let b = TestBorder::filled(0.0, 0.0, 10.0, 10.0);
    let c = TestBorder::filled(0.0, 0.0, 10.0, 10.0);
    s.canvas.add(&a);
    s.canvas.add(&b);
    s.canvas.add(&c);
    s.run_jobs();

    let canvas_visual = s.canvas.composition_visual().unwrap();
    let order = |expected: &[&Ref<TestBorder>]| {
        let children = canvas_visual.children().items();
        assert_eq!(expected.len(), children.len());
        for (child, expected) in children.iter().zip(expected) {
            assert!(Rc::ptr_eq(child, &expected.composition_visual().unwrap()));
            assert!(Rc::ptr_eq(&child.parent().unwrap(), &canvas_visual));
        }
    };
    order(&[&a, &b, &c]);

    a.set_z_index(5);
    s.run_jobs();
    order(&[&b, &c, &a]);

    // The server sees the same order.
    let server_children = s.server_visual(&s.canvas).children().unwrap().list();
    let ids: Vec<_> = [&b, &c, &a].iter().map(|v| v.composition_visual().unwrap().server()).collect();
    for (child, id) in server_children.iter().zip(&ids) {
        assert!(Rc::ptr_eq(child, &s.compositor.server().get::<ServerCompositionVisual>(*id).unwrap()));
    }

    // A child composition visual comes last.
    let extra = s.compositor.create_solid_color_visual();
    ElementComposition::set_element_child_visual(&s.canvas, Some((*extra).clone()));
    s.run_jobs();
    let children = canvas_visual.children().items();
    assert_eq!(4, children.len());
    assert!(Rc::ptr_eq(&children[3], &extra));

    let removed_visual = b.composition_visual().unwrap();
    s.canvas.remove(&b);
    s.run_jobs();
    assert!(b.composition_visual().is_none());
    assert!(removed_visual.parent().is_none());
    assert_eq!(3, canvas_visual.children().count());
}

#[test]
fn detached_visuals_release_their_server_objects() {
    let s = CompositorCanvas::new();
    s.run_jobs();
    let baseline = s.compositor.server().object_count();

    let (control, _child) = decorator_with_child();
    s.canvas.add(&control);
    s.run_jobs();
    assert!(s.compositor.server().object_count() > baseline);

    s.canvas.remove(&control);
    s.run_jobs();
    // The disposal of dropped objects rides on a later batch.
    s.canvas.invalidate_visual();
    s.run_jobs();
    assert_eq!(baseline, s.compositor.server().object_count());
}

// --- target rendering ----------------------------------------------------------------

#[test]
fn target_renders_only_the_dirty_area() {
    let s = CompositorCanvas::new();
    let log = s.render_interface.log().clone();
    let first = TestBorder::filled(10.0, 10.0, 20.0, 20.0);
    let second = TestBorder::filled(500.0, 500.0, 20.0, 20.0);
    second.set_background(Some(Brushes::blue()));
    s.canvas.add(&first);
    s.canvas.add(&second);
    s.run_jobs();
    assert_eq!(1, log.count("DrawRectangle Red"), "{:?}", log.entries());
    assert_eq!(1, log.count("DrawRectangle Blue"));
    log.clear();

    // Nothing changed: nothing is drawn.
    s.run_jobs();
    assert!(log.entries().is_empty(), "{:?}", log.entries());

    // Only the changed visual is redrawn, clipped to its area.
    s.events.rects.borrow_mut().clear();
    second.set_background(Some(Brushes::green()));
    s.run_jobs();
    assert_eq!(0, log.count("DrawRectangle Red"), "{:?}", log.entries());
    assert_eq!(1, log.count("DrawRectangle Green"));
    assert!(log.entries().iter().any(|e| e.starts_with("PushClip 499, 499, 22, 22")), "{:?}", log.entries());
    s.assert_rects(&[Rect::new(500.0, 500.0, 20.0, 20.0)]);
}

#[test]
fn opacity_and_clip_are_pushed_around_the_content() {
    let s = CompositorCanvas::new();
    let log = s.render_interface.log().clone();
    let border = TestBorder::filled(10.0, 10.0, 20.0, 20.0);
    border.set_opacity(0.5);
    border.set_clip_to_bounds(true);
    s.canvas.add(&border);
    s.run_jobs();

    let entries = log.entries();
    let position = |prefix: &str| entries.iter().position(|e| e.starts_with(prefix)).unwrap_or_else(|| panic!("{prefix}: {entries:?}"));
    let push_opacity = position("PushOpacity 0.5");
    let push_clip = position("PushClip 0, 0, 20, 20");
    let draw = position("DrawRectangle Red");
    let pop_opacity = position("PopOpacity");
    assert!(push_opacity < push_clip && push_clip < draw && draw < pop_opacity, "{entries:?}");
}

// --- readback ------------------------------------------------------------------------

#[test]
fn readback_reports_the_transform_and_bounds_of_the_last_frame() {
    let s = CompositorCanvas::new();
    let border = TestBorder::filled(30.0, 50.0, 20.0, 10.0);
    s.canvas.add(&border);
    let visual = border.composition_visual().unwrap();

    // Nothing was rendered yet.
    s.compositor.server().readback().next_read();
    assert!(visual.try_get_valid_readback().is_none());

    s.run_jobs();
    s.compositor.server().readback().next_read();
    let readback = visual.try_get_valid_readback().expect("the visual was rendered");
    assert_eq!(Matrix::create_translation(30.0, 50.0), readback.matrix);
    assert_eq!(Some(LtrbRect::new(30.0, 50.0, 50.0, 60.0)), readback.transformed_subtree_bounds);
    assert!(readback.visible);
    assert_eq!(s.renderer.composition_target().id(), readback.target_id);

    let server_target =
        s.compositor.server().get::<ServerCompositionTarget>(s.renderer.composition_target().server()).unwrap();
    assert_eq!(server_target.id(), readback.target_id);

    // The reader keeps its revision until it asks for the next one.
    border.set_bounds(Rect::new(70.0, 50.0, 20.0, 10.0));
    s.run_jobs();
    assert_eq!(Matrix::create_translation(30.0, 50.0), visual.try_get_valid_readback().unwrap().matrix);
    s.compositor.server().readback().next_read();
    assert_eq!(Matrix::create_translation(70.0, 50.0), visual.try_get_valid_readback().unwrap().matrix);

    // An invisible visual has no valid readback.
    border.set_is_visible(false);
    s.run_jobs();
    s.compositor.server().readback().next_read();
    assert!(visual.try_get_valid_readback().is_none());
}

#[test]
fn scene_invalidated_is_raised_after_a_frame() {
    let s = CompositorCanvas::new();
    let raised = Rc::new(Cell::new(0));
    let r = raised.clone();
    let subscription = s.renderer.scene_invalidated(Rc::new(move |e| {
        assert_eq!(Rect::new(0.0, 0.0, 1000.0, 1000.0), e.dirty_rect());
        r.set(r.get() + 1);
    }));
    s.canvas.add(&TestBorder::filled(0.0, 0.0, 10.0, 10.0));
    s.run_jobs();
    assert_eq!(1, raised.get());
    subscription.dispose();
}

#[test]
fn paint_renders_immediately() {
    let s = CompositorCanvas::new();
    let log = s.render_interface.log().clone();
    s.canvas.add(&TestBorder::filled(0.0, 0.0, 10.0, 10.0));
    log.clear();
    // No dispatcher job and no render loop tick in between.
    s.renderer.paint(Rect::new(0.0, 0.0, 1000.0, 1000.0));
    assert_eq!(1, log.count("DrawRectangle Red"), "{:?}", log.entries());
}

// --- bitmap cache --------------------------------------------------------------------

#[test]
fn bitmap_cache_renders_the_subtree_once_and_redraws_from_the_layer() {
    use crate::media::{BitmapCache, CacheMode};

    let s = CompositorCanvas::new();
    let log = s.render_interface.log().clone();
    let (control, child) = decorator_with_child();
    let cache = BitmapCache::new();
    control.set_cache_mode(cache.clone().upcast::<CacheMode>());
    s.canvas.add(&control);
    s.run_jobs();

    let composition_visual = control.composition_visual().unwrap();
    let composition_cache = composition_visual.cache_mode().expect("the cache mode is synchronized");
    assert_eq!(1.0, composition_cache.render_at_scale());
    assert!(s.server_visual(&control).cache().is_some());
    // The subtree is drawn into the layer and the layer onto the target.
    assert_eq!(1, log.count("DrawRectangle Red"), "{:?}", log.entries());
    assert!(log.count("DrawBitmap 1 0, 0, 20, 10 10, 10, 20, 10") >= 1, "{:?}", log.entries());
    log.clear();

    // Moving the cached visual redraws the layer bitmap, not the subtree.
    control.set_bounds(Rect::new(100.0, 50.0, 40.0, 30.0));
    s.run_jobs();
    assert_eq!(0, log.count("DrawRectangle Red"), "{:?}", log.entries());
    assert!(log.count("DrawBitmap 1 0, 0, 20, 10 10, 10, 20, 10") >= 1, "{:?}", log.entries());
    log.clear();

    // A change inside the subtree re-renders the cache.
    child.set_background(Some(Brushes::blue()));
    s.run_jobs();
    assert_eq!(1, log.count("DrawRectangle Blue"), "{:?}", log.entries());

    // Changing the cache properties reaches the server and invalidates the cache.
    cache.set_render_at_scale(2.0);
    s.run_jobs();
    assert_eq!(2.0, composition_cache.render_at_scale());
    assert!(log.count("DrawBitmap 1 0, 0, 40, 20 10, 10, 20, 10") >= 1, "{:?}", log.entries());

    // Removing the cache mode removes the server cache.
    control.set_cache_mode(None::<Ref<CacheMode>>);
    control.invalidate_visual();
    s.run_jobs();
    assert!(composition_visual.cache_mode().is_none());
    assert!(s.server_visual(&control).cache().is_none());
}

// --- other visual classes ------------------------------------------------------------

#[test]
fn solid_color_and_acrylic_visuals_render() {
    use crate::media::{Colors, ImmutableExperimentalAcrylicMaterial};
    use crate::rendering::composition::CompositionExperimentalAcrylicVisual;

    let s = CompositorCanvas::new();
    let log = s.render_interface.log().clone();

    let solid = s.compositor.create_solid_color_visual();
    solid.set_size(Vector::new(30.0, 20.0));
    solid.set_offset(Vector3D::new(5.0, 5.0, 0.0));
    solid.set_color(Colors::BLUE);
    ElementComposition::set_element_child_visual(&s.canvas, Some((*solid).clone()));
    s.run_jobs();
    assert_eq!(Colors::BLUE, solid.color());
    assert_eq!(1, log.count("DrawRectangle Blue"), "{:?}", log.entries());
    s.assert_rects(&[Rect::new(5.0, 5.0, 30.0, 20.0)]);

    // An acrylic visual is a draw list visual with two more properties;
    // without acrylic support of the context only its draw list is drawn.
    let host = TestBorder::filled(100.0, 100.0, 10.0, 10.0);
    let acrylic = CompositionExperimentalAcrylicVisual::new(&s.compositor, &host);
    acrylic.set_material(ImmutableExperimentalAcrylicMaterial::default());
    acrylic.set_corner_radius(CornerRadius::uniform(4.0));
    acrylic.set_size(Vector::new(10.0, 10.0));
    assert!(CompositionExperimentalAcrylicVisual::from_visual(&acrylic).is_some());
    assert!(CompositionExperimentalAcrylicVisual::from_visual(&solid).is_none());
    ElementComposition::set_element_child_visual(&s.canvas, Some((**acrylic).clone()));
    s.run_jobs();
    assert_eq!(CornerRadius::uniform(4.0), acrylic.corner_radius());
    let server = s.compositor.server().get::<ServerCompositionVisual>(acrylic.server()).unwrap();
    let content = server
        .content_as::<crate::rendering::composition::server::ServerCompositionExperimentalAcrylicVisual>()
        .expect("the server visual is an acrylic visual");
    assert_eq!(CornerRadius::uniform(4.0), content.props().corner_radius());
    s.assert_rects(&[Rect::new(5.0, 5.0, 30.0, 20.0), Rect::new(0.0, 0.0, 10.0, 10.0)]);
}

// --- geometry hit testing --------------------------------------------------------------

#[test]
fn geometry_hit_test_finds_intersecting_controls() {
    let s = CompositorCanvas::new();
    let inside = TestBorder::filled(100.0, 100.0, 50.0, 50.0);
    let outside = TestBorder::filled(600.0, 600.0, 50.0, 50.0);
    s.canvas.add(&inside);
    s.canvas.add(&outside);
    s.run_jobs();

    let geometry = RectangleGeometry::with_rect(Rect::new(90.0, 90.0, 100.0, 100.0)).upcast::<crate::media::Geometry>();
    let tested = s.renderer.hit_test_geometry(&geometry, &s.root, None);
    assert_eq!(1, tested.len());
    assert_eq!(inside.clone().upcast::<Visual>(), tested[0].visual_hit);

    let first = s.renderer.hit_test_first_geometry(&geometry, &s.root, None).expect("a visual is hit");
    assert_eq!(inside.upcast::<Visual>(), first.visual_hit);

    let nothing = RectangleGeometry::with_rect(Rect::new(300.0, 300.0, 10.0, 10.0)).upcast::<crate::media::Geometry>();
    assert!(s.renderer.hit_test_geometry(&nothing, &s.root, None).is_empty());
    assert!(s.renderer.hit_test_first_geometry(&nothing, &s.root, None).is_none());
}

// --- DrawingBrushPropagationTests -----------------------------------------------------

fn drawing_brush_border(brush: &Ref<DrawingBrush>) -> Ref<TestBorder> {
    let border = TestBorder::new();
    border.set_background(Some(brush.into()));
    border.set_bounds(Rect::new(30.0, 50.0, 20.0, 10.0));
    border
}

fn rectangle_drawing(brush: Rc<dyn IBrush>, geometry: &Ref<RectangleGeometry>) -> Ref<GeometryDrawing> {
    let drawing = GeometryDrawing::new();
    drawing.set_brush(Some(brush));
    drawing.set_geometry(geometry.clone().upcast::<Geometry>());
    drawing
}

#[test]
fn mutating_geometry_inside_drawing_brush_invalidates_consumer() {
    let s = CompositorCanvas::new();

    let geometry = RectangleGeometry::with_rect(Rect::new(0.0, 0.0, 20.0, 10.0));
    let brush = DrawingBrush::with_drawing(&rectangle_drawing(Brushes::red(), &geometry));
    s.canvas.add(&drawing_brush_border(&brush));
    s.run_jobs();
    s.events.rects.borrow_mut().clear();

    geometry.set_rect(Rect::new(0.0, 0.0, 30.0, 15.0));

    s.assert_rects(&[Rect::new(30.0, 50.0, 20.0, 10.0)]);
}

#[test]
fn replacing_drawing_invalidates_consumer() {
    let s = CompositorCanvas::new();

    let brush = DrawingBrush::with_drawing(&rectangle_drawing(
        Brushes::red(),
        &RectangleGeometry::with_rect(Rect::new(0.0, 0.0, 20.0, 10.0)),
    ));
    s.canvas.add(&drawing_brush_border(&brush));
    s.run_jobs();
    s.events.rects.borrow_mut().clear();

    brush.set_drawing(&rectangle_drawing(
        Brushes::blue(),
        &RectangleGeometry::with_rect(Rect::new(0.0, 0.0, 20.0, 10.0)),
    ));

    s.assert_rects(&[Rect::new(30.0, 50.0, 20.0, 10.0)]);
}

// --- scene brushes on a visual (not from upstream) --------------------------------------

#[test]
fn visual_brush_as_opacity_mask_is_a_server_side_scene_brush() {
    let s = CompositorCanvas::new();
    let log = s.render_interface.log().clone();

    // The visual of the brush is not part of the tree.
    let source = TestBorder::filled(0.0, 0.0, 8.0, 4.0);
    let mask = VisualBrush::with_visual(&source.clone().upcast::<Visual>());
    let border = TestBorder::filled(10.0, 10.0, 20.0, 20.0);
    border.set_opacity_mask(Some((&mask).into()));
    s.canvas.add(&border);
    s.run_jobs();

    let entries = log.entries();
    let position = |prefix: &str| entries.iter().position(|e| e.starts_with(prefix)).unwrap_or_else(|| panic!("{prefix}: {entries:?}"));
    let push_mask = position("PushOpacityMask brush");
    let draw = position("DrawRectangle Red none 0, 0, 20, 20");
    let pop_mask = position("PopOpacityMask");
    assert!(push_mask < draw && draw < pop_mask, "{entries:?}");

    // The mask of the server visual is the server-side counterpart of the
    // brush, with the rendering of the visual as its content.
    let content_of = |visual: &Ref<TestBorder>| {
        let mask = s.server_visual(visual).opacity_mask_brush().expect("the server visual has a mask");
        let content = mask.as_scene_brush().expect("a scene brush").create_content();
        (mask, content)
    };
    let (server_mask, content) = content_of(&border);
    let content = content.expect("the visual draws something");
    assert_eq!(content.rect(), Rect::new(0.0, 0.0, 8.0, 4.0));
    assert_eq!(server_mask.as_tile_brush().unwrap().stretch(), Stretch::Uniform);
    let content_log = crate::rendering::testing::DrawingLog::new();
    let mut context = crate::rendering::testing::MockDrawingContextImpl::new(content_log.clone());
    context.log_transforms = false;
    content.render(&mut context, None);
    assert!(
        content_log.entries().iter().any(|e| e == "DrawRectangle Red none 0, 0, 8, 4 shadows=0"),
        "{:?}",
        content_log.entries()
    );

    // A change of the brush records its content again and redraws what it
    // masks.
    s.events.rects.borrow_mut().clear();
    source.set_bounds(Rect::new(0.0, 0.0, 6.0, 6.0));
    mask.set_stretch(Stretch::Fill);
    s.assert_rects(&[Rect::new(10.0, 10.0, 20.0, 20.0)]);
    let (server_mask, content) = content_of(&border);
    assert_eq!(server_mask.as_tile_brush().unwrap().stretch(), Stretch::Fill);
    assert_eq!(content.expect("the visual draws something").rect(), Rect::new(0.0, 0.0, 6.0, 6.0));

    // Without a visual the brush has no content.
    mask.set_visual(None);
    s.run_jobs();
    assert!(content_of(&border).1.is_none());

    // Removing the mask releases the brush on the compositor.
    assert!(mask.is_on_compositor(&s.compositor));
    border.set_opacity_mask(None);
    s.run_jobs();
    assert!(!mask.is_on_compositor(&s.compositor));
    assert!(s.server_visual(&border).opacity_mask_brush().is_none());
}

#[test]
fn visual_brush_as_background_is_drawn_through_its_server_side_counterpart() {
    let s = CompositorCanvas::new();
    let log = s.render_interface.log().clone();

    let source = TestBorder::filled(0.0, 0.0, 8.0, 4.0);
    let brush = VisualBrush::with_visual(&source.clone().upcast::<Visual>());
    let border = TestBorder::new();
    border.set_background(Some((&brush).into()));
    border.set_bounds(Rect::new(30.0, 50.0, 20.0, 10.0));
    s.canvas.add(&border);
    s.run_jobs();
    assert!(log.entries().iter().any(|e| e == "DrawRectangle brush none 0, 0, 20, 10 shadows=0"), "{:?}", log.entries());
    assert!(brush.is_on_compositor(&s.compositor));

    s.events.rects.borrow_mut().clear();
    brush.set_opacity(0.5);
    s.assert_rects(&[Rect::new(30.0, 50.0, 20.0, 10.0)]);

    s.canvas.remove(&border);
    s.run_jobs();
    assert!(!brush.is_on_compositor(&s.compositor));
}

// --- serialization queue (not from upstream) ---------------------------------------------

struct InertServerObject;

impl super::server::IServerObject for InertServerObject {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn into_any_rc(self: Rc<Self>) -> Rc<dyn std::any::Any> {
        self
    }
}

type PrepareAction = Box<dyn FnOnce(&Compositor)>;

/// Something a compositor serializes: counts how often, and may run an
/// action when it is about to be serialized.
struct SerializationProbe {
    server: super::server::ServerObjectId,
    serialized: Rc<Cell<u32>>,
    on_prepare: RefCell<Option<PrepareAction>>,
}

impl SerializationProbe {
    fn new(compositor: &Compositor, serialized: &Rc<Cell<u32>>, on_prepare: Option<PrepareAction>) -> Rc<Self> {
        Rc::new(SerializationProbe {
            server: compositor.create_server_object(|_, _| Rc::new(InertServerObject)),
            serialized: serialized.clone(),
            on_prepare: RefCell::new(on_prepare),
        })
    }
}

impl super::ICompositorSerializable for SerializationProbe {
    fn try_get_server(&self, _c: &Compositor) -> Option<super::server::ServerObjectId> {
        Some(self.server)
    }

    fn prepare_serialization(&self, c: &Compositor) {
        let action = self.on_prepare.borrow_mut().take();
        if let Some(action) = action {
            action(c);
        }
    }

    fn serialize_changes(&self, _c: &Compositor, _writer: &mut super::transport::BatchStreamWriter<'_>) {
        self.serialized.set(self.serialized.get() + 1);
    }
}

#[test]
fn an_object_created_while_another_is_serialized_is_serialized_with_the_same_batch() {
    let s = CompositorCanvas::new();
    let (early, late) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0)));

    // The queue holds the only reference to each of the first objects: they
    // are released once they have been serialized. An object of the same
    // type created after that, while a later object is serialized, is likely
    // to be given the memory of one of them; it must not be taken for an
    // object that is queued already.
    for _ in 0..8 {
        s.compositor.register_for_serialization(SerializationProbe::new(&s.compositor, &early, None));
    }
    let late_counter = late.clone();
    let creating = SerializationProbe::new(
        &s.compositor,
        &early,
        Some(Box::new(move |c: &Compositor| {
            for _ in 0..8 {
                c.register_for_serialization(SerializationProbe::new(c, &late_counter, None));
            }
        })),
    );
    s.compositor.register_for_serialization(creating);

    s.compositor.commit();
    assert_eq!(9, early.get());
    assert_eq!(8, late.get());

    // The batch is applied: the server objects were created before use.
    s.run_jobs();
}

/// Port of `CompositionAnimationTests.ExpressionAnimation_Requeues_Target_When_Another_Animation_Is_Invalidated_During_Evaluation`.
#[test]
fn expression_animation_requeues_target_when_another_animation_is_invalidated_during_evaluation() {
    use super::ICompositionObjectAnimations;

    let s = CompositorCanvas::new();
    let border = TestBorder::filled(0.0, 0.0, 10.0, 10.0);
    s.canvas.add(&border);
    s.run_jobs();

    let visual = ElementComposition::get_element_visual(&border).unwrap();
    let opacity_animation = visual.compositor().create_expression_animation_with("this.Target.RotationAngle * 0.1");
    let rotation_animation = visual.compositor().create_expression_animation_with("this.Target.Offset.X * 0.5");

    visual.start_animation("Opacity", &*opacity_animation);
    visual.start_animation("RotationAngle", &*rotation_animation);

    s.run_jobs();
    visual.set_offset(Vector3D::new(100.0, 0.0, 0.0));
    s.run_jobs();

    let server = s.compositor.server().get::<ServerCompositionVisual>(visual.server()).unwrap();
    assert_eq!(50.0, server.rotation_angle());
    assert_eq!(5.0, server.opacity());
}

/// Not from upstream: a custom visual receives the messages sent to it on
/// the render thread, draws through its handler, and its handler gets
/// animation frame updates while it asks for them.
#[test]
fn custom_visual_draws_through_its_handler() {
    use crate::media::immutable::ImmutableSolidColorBrush;
    use crate::media::{Colors, ImmediateDrawingContext};
    use crate::rendering::composition::{CompositionCustomVisualHandler, ICompositionCustomVisualHandler};
    use std::any::Any;

    #[derive(Default)]
    struct Handler {
        base: CompositionCustomVisualHandler,
        messages: RefCell<Vec<i32>>,
        frames: Cell<i32>,
        clip_contains: Cell<Option<bool>>,
        size: Cell<Option<Vector>>,
    }

    impl ICompositionCustomVisualHandler for Handler {
        fn handler_base(&self) -> &CompositionCustomVisualHandler {
            &self.base
        }

        fn on_message(&self, message: std::sync::Arc<dyn Any + Send + Sync>) {
            let value = *message.downcast_ref::<i32>().unwrap();
            self.messages.borrow_mut().push(value);
            if value == 1 {
                self.base.register_for_next_animation_frame_update();
            }
        }

        fn on_animation_frame_update(&self) {
            self.frames.set(self.frames.get() + 1);
            if self.frames.get() < 3 {
                self.base.register_for_next_animation_frame_update();
            }
            self.base.invalidate();
        }

        fn on_render(&self, drawing_context: &mut ImmediateDrawingContext<'_>) {
            self.clip_contains.set(Some(self.base.render_clip_contains(Point::new(1.0, 1.0))));
            let size = self.base.effective_size();
            self.size.set(Some(size));
            drawing_context.fill_rectangle(
                &ImmutableSolidColorBrush::new(Colors::GREEN),
                Rect::new(0.0, 0.0, size.x, size.y),
                0.0,
            );
        }
    }

    let s = CompositorCanvas::new();
    let log = s.render_interface.log().clone();
    let handler = Rc::new(Handler::default());
    // The tests run the server on their own thread and look at the handler:
    // the factory hands out the one made here.
    let server_handler = crate::utilities::ThreadBound::new(handler.clone());
    let visual = s
        .compositor
        .create_custom_visual(move || server_handler.into_inner() as Rc<dyn ICompositionCustomVisualHandler>);
    visual.set_size(Vector::new(20.0, 10.0));
    ElementComposition::set_element_child_visual(&s.canvas, Some((*visual).clone()));
    visual.send_handler_message(std::sync::Arc::new(1i32));
    visual.send_handler_message(std::sync::Arc::new(2i32));
    assert!(handler.messages.borrow().is_empty());
    s.run_jobs();

    assert_eq!(*handler.messages.borrow(), [1, 2]);
    assert_eq!(handler.size.get(), Some(Vector::new(20.0, 10.0)));
    assert_eq!(1, log.count("DrawRectangle Green"), "{:?}", log.entries());
    assert_eq!(handler.clip_contains.get(), Some(true));

    // The handler asked for frames: it gets them until it stops asking.
    for _ in 0..5 {
        s.run_jobs();
    }
    assert_eq!(handler.frames.get(), 3);
    assert!(!s.compositor.server().animations().need_next_tick());

    // The render APIs are only available while the handler draws.
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        handler.base.render_clip_contains(Point::new(1.0, 1.0))
    }));
    assert!(result.is_err());
    // The other APIs are only available while the compositor renders.
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| handler.base.effective_size()));
    assert!(result.is_err());
}

// --- Compositor.CreateCompositionVisualSnapshot --------------------------------------
//
// Not upstream tests: upstream covers the member in the headless rendering tests
// (`Should_Render_To_A_Compositor_Snapshot_Capture`), which need a rendering backend.

#[test]
fn create_composition_visual_snapshot_faults_for_a_visual_without_a_root() {
    let s = CompositorCanvas::new();
    let visual = s.compositor.create_container_visual();

    let task = s.compositor.create_composition_visual_snapshot(&visual, 1.0);

    assert!(task.is_faulted());
    assert_eq!(
        "the visual is not attached to a composition target",
        task.exception().expect("the task is faulted").to_string()
    );
}

#[test]
fn create_composition_visual_snapshot_completes_after_the_next_batch() {
    let s = CompositorCanvas::new();
    let control = TestBorder::filled(30.0, 50.0, 20.0, 10.0);
    s.canvas.add(&control);
    s.run_jobs();
    let visual = control.composition_visual().expect("the control is attached");

    let task = s.compositor.create_composition_visual_snapshot(&visual, 2.0);

    assert!(!task.is_completed());
    s.run_jobs();
    assert!(task.is_completed_successfully());
    assert!(matches!(task.take_result(), Some(Ok(_))));
}

// --- the life cycle of server object ids ---------------------------------------------
//
// Not from upstream, where a UI-thread object holds its server object: here
// it names it by an id, which is handed out again once the server has
// dropped the object.

impl CompositorCanvas {
    fn server_object(&self, id: super::server::ServerObjectId) -> Rc<ServerCompositionVisual> {
        self.compositor.server().get::<ServerCompositionVisual>(id).expect("the server visual exists")
    }
}

/// A composition target as a renderer makes one, over no surface.
fn bare_target(s: &CompositorCanvas) -> Rc<super::CompositionTarget> {
    s.compositor.create_composition_target(std::sync::Arc::new(Vec::new))
}

/// Disposes a target as a renderer does when its top level closes.
fn dispose_out_of_band(s: &CompositorCanvas, target: &super::CompositionTarget) {
    target.set_root(None);
    target.mark_disposed_out_of_band();
    MediaContext::instance().sync_dispose_composition_target(&s.compositor, target.server());
}

/// A top level that is closed before the compositor has committed: the
/// batch of the disposal overtakes the batch that would create the server
/// target. The target is created by the batch that disposes it, and keeps
/// its id until the UI-thread target is dropped.
#[test]
fn a_target_disposed_before_its_first_commit_is_created_disposed_and_keeps_its_id() {
    let s = CompositorCanvas::new();
    s.run_jobs();
    let baseline = s.compositor.server().object_count();

    let target = bare_target(&s);
    let id = target.server();
    assert!(s.compositor.server().get_object(id).is_none(), "the creation waits for a commit");

    dispose_out_of_band(&s, &target);

    let server_target = s.compositor.server().get::<ServerCompositionTarget>(id).expect("the disposal created the server target");
    assert!(server_target.is_disposed());
    assert_eq!(baseline + 1, s.compositor.server().object_count());

    // What is created while the target is alive gets other ids, and the
    // commit that follows does not create the target again.
    let visuals: Vec<_> = (0..4).map(|_| s.compositor.create_container_visual()).collect();
    assert!(visuals.iter().all(|visual| visual.server() != id));
    s.run_jobs();
    let with_visuals = s.compositor.server().object_count();
    let objects_of_a_visual = (with_visuals - baseline - 1) / 4;
    assert!(objects_of_a_visual >= 1);
    let same = s.compositor.server().get::<ServerCompositionTarget>(id).expect("the server target is kept");
    assert!(Rc::ptr_eq(&server_target, &same));
    drop((server_target, same));

    // The release rides on the batch after the drop; the id is handed out
    // for the batch after that one.
    drop(target);
    let before_release = s.compositor.create_container_visual();
    assert_ne!(id, before_release.server());
    s.run_jobs();
    assert!(s.compositor.server().get_object(id).is_none());
    assert_eq!(with_visuals - 1 + objects_of_a_visual, s.compositor.server().object_count());

    let _after_release = s.compositor.create_container_visual();
    s.run_jobs();
    assert_eq!(with_visuals - 1 + 2 * objects_of_a_visual, s.compositor.server().object_count());
    let reused = s.compositor.server().get_object(id).expect("the id is handed out again");
    assert!(reused.into_any_rc().downcast::<ServerCompositionTarget>().is_err());
}

/// A top level that was rendered and is closed: the server target is
/// disposed out of band and keeps its id until the UI-thread target is
/// dropped, so that nothing created meanwhile takes its place.
#[test]
fn a_target_disposed_out_of_band_keeps_its_id_until_it_is_dropped() {
    let s = CompositorCanvas::new();
    let target = bare_target(&s);
    let id = target.server();
    s.run_jobs();
    let baseline = s.compositor.server().object_count();
    let server_target = s.compositor.server().get::<ServerCompositionTarget>(id).expect("the commit created the server target");
    assert!(!server_target.is_disposed());

    dispose_out_of_band(&s, &target);
    assert!(server_target.is_disposed());
    drop(server_target);

    let visual = s.compositor.create_container_visual();
    assert_ne!(id, visual.server());
    s.run_jobs();
    let with_visual = s.compositor.server().object_count();
    assert!(with_visual > baseline);
    assert!(s.compositor.server().get::<ServerCompositionTarget>(id).is_some());

    drop(target);
    s.canvas.invalidate_visual();
    s.run_jobs();
    assert!(s.compositor.server().get_object(id).is_none());
    assert_eq!(with_visual - 1, s.compositor.server().object_count());
}

/// A second window of a compositor, as the headless tests found it: a
/// target closed before its first commit, then a target that is shown.
#[test]
fn a_target_shown_after_another_one_was_closed_before_its_first_commit_is_created() {
    let s = CompositorCanvas::new();
    s.run_jobs();

    let closed = bare_target(&s);
    let closed_root = s.compositor.create_container_visual();
    closed.set_root(Some(closed_root.clone()));
    dispose_out_of_band(&s, &closed);
    drop((closed, closed_root));

    let shown = bare_target(&s);
    let shown_root = s.compositor.create_container_visual();
    shown.set_root(Some(shown_root.clone()));
    MediaContext::instance().immediate_render_requested(&s.compositor);
    s.run_jobs();

    let server_target = s.compositor.server().get::<ServerCompositionTarget>(shown.server()).expect("the second target exists");
    assert!(!server_target.is_disposed());
    assert!(s.compositor.server().get::<ServerCompositionVisual>(shown_root.server()).is_some());
    drop(server_target);
    dispose_out_of_band(&s, &shown);
}

/// An id is returned to the free list once, however often its release is
/// asked for: two objects never share one.
#[test]
fn an_id_released_twice_is_handed_out_once() {
    let s = CompositorCanvas::new();
    let visual = s.compositor.create_container_visual();
    let id = visual.server();
    s.run_jobs();

    s.compositor.dispose_on_next_batch(id);
    s.run_jobs();
    s.compositor.dispose_on_next_batch(id);
    s.run_jobs();
    // The drop of the UI-thread object asks for the release a third time.
    drop(visual);
    s.canvas.invalidate_visual();
    s.run_jobs();

    // An id that is in the free list twice goes to two of the objects
    // created next, and the server finds the second under an id in use.
    let visuals: Vec<_> = (0..4).map(|_| s.compositor.create_container_visual()).collect();
    s.run_jobs();
    let servers: Vec<_> = visuals.iter().map(|visual| s.server_object(visual.server())).collect();
    for (index, server) in servers.iter().enumerate() {
        assert!(servers[index + 1..].iter().all(|other| !Rc::ptr_eq(server, other)));
    }
}