rust_widgets 2.5.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

use super::*;
use crate::core::Rect;
use crate::widget::view_widgets::virtual_list::VirtualList;

#[test]
fn default_factory_registers_core_capabilities() {
    let factory = WidgetFactory::new_with_defaults();

    assert!(factory.capability("label").is_some());
    assert!(factory.capability("checkbox").is_some());
    assert!(factory.capability("radiobutton").is_some());
    assert!(factory.capability("slider").is_some());
    assert!(factory.capability("lineedit").is_some());
    assert!(factory.capability("list_view").is_some());
    assert!(factory.capability("treeview").is_some());
    assert!(factory.capability("table").is_some());
    assert!(factory.capability("dataview").is_some());
    assert!(factory.capability("menu").is_some());
    assert!(factory.capability("menubar").is_some());
    assert!(factory.capability("toolbar").is_some());
    assert!(factory.capability("ribbon").is_some());
    assert!(factory.capability("colorpicker").is_some());
    assert!(factory.capability("code_editor").is_some());
    assert!(factory.capability("gantt").is_some());
    assert!(factory.capability("terminalview").is_some());
    assert!(factory.capability("snackbar").is_some());
    assert!(factory.capability("mapview").is_some());
    assert!(factory.capability("mediaplayer").is_some());
    assert!(factory.capability("breadcrumb").is_some());
    assert!(factory.capability("splitbutton").is_some());
    assert!(factory.capability("segmentedcontrol").is_some());
    assert!(factory.capability("chips").is_some());
    assert!(factory.capability("gridwidget").is_some());
    assert!(factory.capability("freeformshape").is_some());
    assert!(factory.capability("progressbar").is_some());
    assert!(factory.capability("scrollbar").is_some());
    assert!(factory.capability("listbox").is_some());
    assert!(factory.capability("spinbox").is_some());
    assert!(factory.capability("combobox").is_some());
    assert!(factory.capability("dial").is_some());
    assert!(factory.capability("window").is_some());
    assert!(factory.capability("groupbox").is_some());
    assert!(factory.capability("splitter").is_some());
    assert!(factory.capability("lcdnumber").is_some());
    assert!(factory.capability("commandlink").is_some());
    assert!(factory.capability("fontcombobox").is_some());
    assert!(factory.capability("action").is_some());
    assert!(factory.capability("toolbox").is_some());
    assert!(factory.capability("tabbar").is_some());
    assert!(factory.capability("calendar").is_some());
    assert!(factory.capability("dateedit").is_some());
    assert!(factory.capability("timeedit").is_some());
    assert!(factory.capability("datagrid").is_some());
    assert!(factory.capability("treetable").is_some());
    assert!(factory.capability("virtualtable").is_some());

    // ── Newly registered capabilities (R3/R4/R5) ───────
    assert!(factory.capability("messagebox").is_some());
    assert!(factory.capability("filedialog").is_some());
    assert!(factory.capability("fontdialog").is_some());
    assert!(factory.capability("inputdialog").is_some());
    assert!(factory.capability("progressdialog").is_some());
    assert!(factory.capability("popupwindow").is_some());
    assert!(factory.capability("scrollarea").is_some());
    assert!(factory.capability("tabwidget").is_some());
    assert!(factory.capability("stackedwidget").is_some());
    assert!(factory.capability("collapsiblepane").is_some());
    assert!(factory.capability("dockwidget").is_some());
    assert!(factory.capability("mdiarea").is_some());
    assert!(factory.capability("textedit").is_some());
    assert!(factory.capability("webview").is_some());
    assert!(factory.capability("piemenu").is_some());
    assert!(factory.capability("datetimepicker").is_some());
}

#[test]
fn factory_creates_registered_widgets_by_alias() {
    let factory = WidgetFactory::new_with_defaults();
    let rect = Rect::new(1, 2, 120, 40);

    let button = factory.create("btn", rect, "Run").expect("button must be created via alias");
    assert_eq!(button.kind(), WidgetKind::Button);
    assert_eq!(button.geometry(), rect);

    let label =
        factory.create("label", rect, "Name").expect("label must be created by canonical name");
    assert_eq!(label.kind(), WidgetKind::Label);

    let check_box =
        factory.create("checkbox", rect, "Accept").expect("checkbox must be created via alias");
    assert_eq!(check_box.kind(), WidgetKind::CheckBox);

    let radio_button = factory
        .create("radiobutton", rect, "Option")
        .expect("radio button must be created via alias");
    assert_eq!(radio_button.kind(), WidgetKind::RadioButton);

    let slider =
        factory.create("slider", rect, "").expect("slider must be created by canonical name");
    assert_eq!(slider.kind(), WidgetKind::Slider);

    let line_edit =
        factory.create("input", rect, "hello").expect("line edit must be created via alias");
    assert_eq!(line_edit.kind(), WidgetKind::LineEdit);

    let table = factory.create("table", rect, "").expect("table widget must be created via alias");
    assert_eq!(table.kind(), WidgetKind::Table);

    let data_view =
        factory.create("dataview", rect, "").expect("data view must be created via alias");
    assert_eq!(data_view.kind(), WidgetKind::DataView);

    let tree = factory.create("treeview", rect, "").expect("tree view must be created via alias");
    assert_eq!(tree.kind(), WidgetKind::TreeView);

    let ribbon = factory.create("ribbon", rect, "").expect("ribbon bar must be created via alias");
    assert_eq!(ribbon.kind(), WidgetKind::RibbonBar);

    // `colorpicker` is the same control as `color_picker`, so it reports the picker's
    // own kind since BLUE16 phase E-6. It used to report `ColorDialog` because the
    // picker had no kind of its own; the test is updated rather than the behaviour,
    // because reporting "dialog" for a control with no window was the bug.
    let color_picker =
        factory.create("colorpicker", rect, "").expect("color picker must be created via alias");
    assert_eq!(color_picker.kind(), WidgetKind::ColorPicker);

    // The dialog is a separate control reached by its own name, and it still reports
    // `ColorDialog`.
    let color_dialog =
        factory.create("color_dialog", rect, "").expect("colour dialog must be registered");
    assert_eq!(color_dialog.kind(), WidgetKind::ColorDialog);

    let code_editor = factory
        .create("codeeditor", rect, "// code")
        .expect("code editor must be created via alias");
    assert_eq!(code_editor.kind(), WidgetKind::RichEdit);

    let gantt = factory.create("gantt", rect, "").expect("gantt must be created via alias");
    assert_eq!(gantt.kind(), WidgetKind::Chart);

    let terminal = factory
        .create("terminalview", rect, "echo hi")
        .expect("terminal view must be created via alias");
    assert_eq!(terminal.kind(), WidgetKind::TextEdit);

    let snackbar =
        factory.create("snackbar", rect, "Saved").expect("snackbar must be created via alias");
    assert_eq!(snackbar.kind(), WidgetKind::StatusBar);

    let map = factory.create("mapview", rect, "").expect("map view must be created via alias");
    assert_eq!(map.kind(), WidgetKind::Canvas);

    let media =
        factory.create("mediaplayer", rect, "").expect("media player must be created via alias");
    assert_eq!(media.kind(), WidgetKind::WebEngineView);

    let breadcrumb =
        factory.create("breadcrumb", rect, "").expect("breadcrumb must be created via alias");
    assert_eq!(breadcrumb.kind(), WidgetKind::Breadcrumb);

    let split_button = factory
        .create("splitbutton", rect, "Menu")
        .expect("split button must be created via alias");
    assert_eq!(split_button.kind(), WidgetKind::ToolButton);

    let segmented = factory
        .create("segmentedcontrol", rect, "")
        .expect("segmented control must be created via alias");
    assert_eq!(segmented.kind(), WidgetKind::ToggleButton);

    let chips = factory.create("chips", rect, "").expect("chip must be created via alias");
    // A `Chip` is its own kind. It previously reported `CheckListBox` — a type alias
    // for `ListBox` — which made the kind→control lookup ambiguous and left
    // `WidgetKind::Chip` with no capability at all.
    assert_eq!(chips.kind(), WidgetKind::Chip);

    let grid =
        factory.create("gridwidget", rect, "").expect("grid widget must be created via alias");
    assert_eq!(grid.kind(), WidgetKind::Grid);

    let shape = factory
        .create("freeformshape", rect, "")
        .expect("freeform shape must be created via alias");
    assert_eq!(shape.kind(), WidgetKind::FreeformShape);

    let msg =
        factory.create("messagebox", rect, "").expect("message box must be created via alias");
    assert_eq!(msg.kind(), WidgetKind::MessageBox);

    let fd = factory.create("filedialog", rect, "").expect("file dialog must be created via alias");
    assert_eq!(fd.kind(), WidgetKind::FileDialog);

    let font_dialog =
        factory.create("fontdialog", rect, "").expect("font dialog must be created via alias");
    assert_eq!(font_dialog.kind(), WidgetKind::FontDialog);

    let input_dialog =
        factory.create("inputdialog", rect, "").expect("input dialog must be created via alias");
    assert_eq!(input_dialog.kind(), WidgetKind::InputDialog);

    let progress_dialog = factory
        .create("progressdialog", rect, "")
        .expect("progress dialog must be created via alias");
    assert_eq!(progress_dialog.kind(), WidgetKind::ProgressDialog);

    let popup =
        factory.create("popupwindow", rect, "").expect("popup window must be created via alias");
    assert_eq!(popup.kind(), WidgetKind::PopupWindow);

    let scroll =
        factory.create("scrollarea", rect, "").expect("scroll area must be created via alias");
    assert_eq!(scroll.kind(), WidgetKind::ScrollArea);

    let tab_widget =
        factory.create("tabwidget", rect, "").expect("tab widget must be created via alias");
    assert_eq!(tab_widget.kind(), WidgetKind::TabWidget);

    let stacked = factory
        .create("stackedwidget", rect, "")
        .expect("stacked widget must be created via alias");
    assert_eq!(stacked.kind(), WidgetKind::StackedWidget);

    let collapsible = factory
        .create("collapsiblepane", rect, "")
        .expect("collapsible pane must be created via alias");
    assert_eq!(collapsible.kind(), WidgetKind::CollapsiblePane);

    let dock =
        factory.create("dockwidget", rect, "").expect("dock widget must be created via alias");
    assert_eq!(dock.kind(), WidgetKind::DockWidget);

    let mdi = factory.create("mdiarea", rect, "").expect("mdi area must be created via alias");
    assert_eq!(mdi.kind(), WidgetKind::MdiArea);

    let text_edit =
        factory.create("textedit", rect, "").expect("text edit must be created via alias");
    assert_eq!(text_edit.kind(), WidgetKind::TextEdit);

    let web = factory.create("webview", rect, "").expect("web view must be created via alias");
    assert_eq!(web.kind(), WidgetKind::WebEngineView);

    let pie_menu = factory.create("piemenu", rect, "").expect("pie menu must be created via alias");
    assert_eq!(pie_menu.kind(), WidgetKind::PieMenu);

    let datetime = factory
        .create("datetimepicker", rect, "")
        .expect("date time picker must be created via alias");
    assert_eq!(datetime.kind(), WidgetKind::DateTimePicker);
}

#[test]
fn new_widget_kinds_resolve_their_own_kind() {
    let factory = WidgetFactory::new_with_defaults();
    let rect = Rect::new(0, 0, 200, 120);

    let tree_table = factory.create("tree_table", rect, "").expect("tree table must be created");
    assert_eq!(tree_table.kind(), WidgetKind::TreeTable);

    let breadcrumb = factory.create("breadcrumb", rect, "").expect("breadcrumb must be created");
    assert_eq!(breadcrumb.kind(), WidgetKind::Breadcrumb);

    let pad = factory.create("signature_pad", rect, "").expect("signature pad must be created");
    assert_eq!(pad.kind(), WidgetKind::SignaturePad);

    let zone = factory.create("drop_zone", rect, "").expect("drop zone must be created");
    assert_eq!(zone.kind(), WidgetKind::DropZone);

    // `dialog` used to be an alias of `popup_window`, so `create("dialog")` built a
    // `PopupWindow`. It is now its own control and must report `WidgetKind::Dialog`.
    let dialog = factory.create("dialog", rect, "").expect("dialog must be created");
    assert_eq!(dialog.kind(), WidgetKind::Dialog);
}

#[test]
fn capability_by_kind_returns_expected_schema() {
    let factory = WidgetFactory::new_with_defaults();

    let capability = factory.capability_by_kind(WidgetKind::Button).unwrap();
    assert_eq!(capability.canonical_name, "button");
    assert!(capability.properties.iter().any(|p| p.name == "text"));

    let capability = factory.capability_by_kind(WidgetKind::Slider).unwrap();
    assert_eq!(capability.canonical_name, "slider");
    assert!(capability.properties.iter().any(|p| p.name == "value"));
}

#[test]
fn create_unknown_widget_returns_none() {
    let factory = WidgetFactory::new_with_defaults();
    assert!(factory.create("not_registered", Rect::new(0, 0, 1, 1), "").is_none());
}

#[test]
fn create_by_kind_uses_registered_constructor() {
    let factory = WidgetFactory::new_with_defaults();
    let rect = Rect::new(10, 20, 180, 30);

    let widget = factory.create_by_kind(WidgetKind::Button, rect, "Click").unwrap();
    assert_eq!(widget.kind(), WidgetKind::Button);
    assert_eq!(widget.geometry(), rect);
}

#[test]
fn read_property_returns_value_for_registered_widget() {
    let factory = WidgetFactory::new_with_defaults();
    let widget = factory.create("btn", Rect::new(0, 0, 100, 30), "Save").unwrap();

    let text = factory.read_property(widget.as_ref(), "text").unwrap();
    assert_eq!(text, CapabilityValue::String("Save".to_string()));
}

#[test]
fn read_property_returns_unknown_property_for_missing_schema_item() {
    let factory = WidgetFactory::new_with_defaults();
    let widget = factory.create("btn", Rect::new(0, 0, 100, 30), "Save").unwrap();
    let result = factory.read_property(widget.as_ref(), "nonexistent");
    assert_eq!(result, Err(CapabilityAccessError::UnknownProperty));
}

#[test]
fn read_property_is_case_and_separator_insensitive() {
    let factory = WidgetFactory::new_with_defaults();
    let widget = factory.create("btn", Rect::new(0, 0, 100, 30), "Important").unwrap();

    let text = factory.read_property(widget.as_ref(), "TEXT").unwrap();
    assert_eq!(text, CapabilityValue::String("Important".to_string()));

    let text2 = factory.read_property(widget.as_ref(), "tool-tip").unwrap();
    assert_eq!(text2, CapabilityValue::String("".to_string()));

    let text3 = factory.read_property(widget.as_ref(), "t o o l t i p").unwrap();
    assert_eq!(text3, CapabilityValue::String("".to_string()));
}

#[test]
fn write_property_updates_mutable_scalar_fields() {
    let factory = WidgetFactory::new_with_defaults();
    let mut widget = factory.create("btn", Rect::new(0, 0, 100, 30), "").unwrap();

    factory
        .write_property(widget.as_mut(), "text", CapabilityValue::String("Updated".to_string()))
        .unwrap();
    let text = factory.read_property(widget.as_ref(), "text").unwrap();
    assert_eq!(text, CapabilityValue::String("Updated".to_string()));

    factory.write_property(widget.as_mut(), "enabled", CapabilityValue::Bool(false)).unwrap();
    let enabled = factory.read_property(widget.as_ref(), "enabled").unwrap();
    assert_eq!(enabled, CapabilityValue::Bool(false));
}

#[test]
fn write_property_reports_readonly_property() {
    let factory = WidgetFactory::new_with_defaults();
    let mut list_view = factory.create("listview", Rect::new(0, 0, 200, 60), "").unwrap();

    let result =
        factory.write_property(list_view.as_mut(), "has_model", CapabilityValue::Bool(false));
    assert_eq!(result, Err(CapabilityAccessError::ReadOnlyProperty));
}

#[test]
fn write_property_reports_type_mismatch() {
    let factory = WidgetFactory::new_with_defaults();
    let mut widget = factory.create("btn", Rect::new(0, 0, 100, 30), "").unwrap();

    let result = factory.write_property(widget.as_mut(), "text", CapabilityValue::Bool(true));
    assert_eq!(result, Err(CapabilityAccessError::TypeMismatch));
}

#[test]
fn read_property_covers_declared_scalar_fields() {
    let factory = WidgetFactory::new_with_defaults();
    let mut slider = factory.create("slider", Rect::new(0, 0, 200, 30), "").unwrap();

    assert_eq!(factory.read_property(slider.as_ref(), "minimum"), Ok(CapabilityValue::Int(0)));
    assert_eq!(factory.read_property(slider.as_ref(), "maximum"), Ok(CapabilityValue::Int(100)));

    factory.write_property(slider.as_mut(), "value", CapabilityValue::Int(42)).unwrap();
    assert_eq!(factory.read_property(slider.as_ref(), "value"), Ok(CapabilityValue::Int(42)));
}

#[test]
fn read_property_returns_null_for_optional_projection_fields() {
    let factory = WidgetFactory::new_with_defaults();
    let list_view = factory.create("listview", Rect::new(0, 0, 200, 60), "").unwrap();

    assert_eq!(factory.read_property(list_view.as_ref(), "focused_row"), Ok(CapabilityValue::Null));
}

#[test]
fn write_property_supports_enum_backed_fields() {
    let factory = WidgetFactory::new_with_defaults();
    let mut check_box = factory.create("checkbox", Rect::new(0, 0, 100, 30), "Option").unwrap();

    factory
        .write_property(check_box.as_mut(), "state", CapabilityValue::String("checked".to_string()))
        .unwrap();
    assert_eq!(
        factory.read_property(check_box.as_ref(), "state"),
        Ok(CapabilityValue::String("checked".to_string()))
    );

    factory
        .write_property(
            check_box.as_mut(),
            "state",
            CapabilityValue::String("unchecked".to_string()),
        )
        .unwrap();
    assert_eq!(
        factory.read_property(check_box.as_ref(), "state"),
        Ok(CapabilityValue::String("unchecked".to_string()))
    );

    factory
        .write_property(check_box.as_mut(), "state", CapabilityValue::String("partial".to_string()))
        .unwrap();
    assert_eq!(
        factory.read_property(check_box.as_ref(), "state"),
        Ok(CapabilityValue::String("partially_checked".to_string()))
    );
}

#[test]
fn write_property_supports_r3_data_controls() {
    let factory = WidgetFactory::new_with_defaults();
    let mut list_view = factory.create("listview", Rect::new(0, 0, 200, 60), "").unwrap();

    factory
        .write_property(
            list_view.as_mut(),
            "view_mode",
            CapabilityValue::String("icon".to_string()),
        )
        .unwrap();
    assert_eq!(
        factory.read_property(list_view.as_ref(), "view_mode"),
        Ok(CapabilityValue::String("icon".to_string()))
    );

    factory
        .write_property(
            list_view.as_mut(),
            "view_mode",
            CapabilityValue::String("details".to_string()),
        )
        .unwrap();
    assert_eq!(
        factory.read_property(list_view.as_ref(), "view_mode"),
        Ok(CapabilityValue::String("details".to_string()))
    );

    factory
        .write_property(
            list_view.as_mut(),
            "selection_mode",
            CapabilityValue::String("multi".to_string()),
        )
        .unwrap();
    assert_eq!(
        factory.read_property(list_view.as_ref(), "selection_mode"),
        Ok(CapabilityValue::String("multi".to_string()))
    );

    let mut tree_view = factory.create("treeview", Rect::new(0, 0, 200, 60), "").unwrap();
    factory.write_property(tree_view.as_mut(), "focused_node", CapabilityValue::Null).unwrap();
    assert_eq!(
        factory.read_property(tree_view.as_ref(), "focused_node"),
        Ok(CapabilityValue::Null)
    );

    let mut data_grid = factory.create("datagrid", Rect::new(0, 0, 300, 100), "").unwrap();
    factory.write_property(data_grid.as_mut(), "row_height", CapabilityValue::UInt(28)).unwrap();
    assert_eq!(
        factory.read_property(data_grid.as_ref(), "row_height"),
        Ok(CapabilityValue::UInt(28))
    );

    let mut dg2 = factory.create("datagrid", Rect::new(0, 0, 300, 100), "").unwrap();
    factory.write_property(dg2.as_mut(), "column_width", CapabilityValue::UInt(150)).unwrap();
    assert_eq!(factory.read_property(dg2.as_ref(), "column_width"), Ok(CapabilityValue::UInt(150)));
}

#[test]
fn write_property_accepts_null_for_optional_focus_fields() {
    let factory = WidgetFactory::new_with_defaults();
    let mut tree_view = factory.create("treeview", Rect::new(0, 0, 200, 60), "").unwrap();

    factory.write_property(tree_view.as_mut(), "focused_node", CapabilityValue::Null).unwrap();
    assert_eq!(
        factory.read_property(tree_view.as_ref(), "focused_node"),
        Ok(CapabilityValue::Null)
    );
}

#[cfg(not(alloc_frugal))]
#[test]
fn schema_defaults_are_readable_and_writable_when_declared() {
    let factory = WidgetFactory::new_with_defaults();
    for capability in factory.capabilities() {
        for prop in capability.properties {
            if prop.readable {
                assert!(
                    factory.default_property_value(capability.canonical_name, prop.name).is_ok(),
                    "default_property_value should be readable for {}::{}",
                    capability.canonical_name,
                    prop.name
                );
            }
        }
    }
}

#[cfg(not(alloc_frugal))]
#[test]
fn default_property_value_returns_schema_defaults() {
    let factory = WidgetFactory::new_with_defaults();

    assert_eq!(
        factory.default_property_value("button", "text"),
        Ok(CapabilityValue::String(String::new()))
    );
    assert_eq!(
        factory.default_property_value("button", "pressed"),
        Ok(CapabilityValue::Bool(false))
    );
    assert_eq!(
        factory.default_property_value("button", "default"),
        Ok(CapabilityValue::Bool(false))
    );
    assert_eq!(
        factory.default_property_value("button", "enabled"),
        Ok(CapabilityValue::Bool(true))
    );
    assert_eq!(factory.default_property_value("slider", "minimum"), Ok(CapabilityValue::Int(0)));
    assert_eq!(factory.default_property_value("slider", "maximum"), Ok(CapabilityValue::Int(100)));
    assert_eq!(
        factory.default_property_value("slider", "orientation"),
        Ok(CapabilityValue::String("horizontal".to_string()))
    );
    assert_eq!(
        factory.default_property_value("progress_bar", "progress"),
        Ok(CapabilityValue::Float(0.0))
    );
    assert_eq!(
        factory.default_property_value("progress_bar", "inverted_appearance"),
        Ok(CapabilityValue::Bool(false))
    );
    assert_eq!(
        factory.default_property_value("check_box", "state"),
        Ok(CapabilityValue::String("unchecked".to_string()))
    );
    assert_eq!(
        factory.default_property_value("check_box", "checked"),
        Ok(CapabilityValue::Bool(false))
    );
    assert_eq!(
        factory.default_property_value("listbox", "item_height"),
        Ok(CapabilityValue::Float(20.0))
    );
    assert_eq!(
        factory.default_property_value("spinbox", "wrapping"),
        Ok(CapabilityValue::Bool(false))
    );
    assert_eq!(
        factory.default_property_value("spinbox", "special_value_text"),
        Ok(CapabilityValue::Null)
    );
    assert_eq!(
        factory.default_property_value("combobox", "current_index"),
        Ok(CapabilityValue::Null)
    );
    assert_eq!(
        factory.default_property_value("combobox", "max_visible_items"),
        Ok(CapabilityValue::UInt(10))
    );
    assert_eq!(
        factory.default_property_value("dial", "notch_target"),
        Ok(CapabilityValue::Float(3.7))
    );
    assert_eq!(
        factory.default_property_value("lcdnumber", "num_digits"),
        Ok(CapabilityValue::Int(6))
    );
    assert_eq!(
        factory.default_property_value("lcdnumber", "segment_style"),
        Ok(CapabilityValue::String("filled".to_string()))
    );
    assert_eq!(
        factory.default_property_value("lcdnumber", "mode"),
        Ok(CapabilityValue::String("dec".to_string()))
    );
    assert_eq!(
        factory.default_property_value("fontcombobox", "current_font_family"),
        Ok(CapabilityValue::String("Arial".to_string()))
    );
    assert_eq!(factory.default_property_value("action", "command_id"), Ok(CapabilityValue::Null));
    assert_eq!(
        factory.default_property_value("action", "separator"),
        Ok(CapabilityValue::Bool(false))
    );
    assert_eq!(
        factory.default_property_value("line_edit", "max_length"),
        Ok(CapabilityValue::Null)
    );
    assert_eq!(
        factory.default_property_value("listview", "selection_mode"),
        Ok(CapabilityValue::String("single".to_string()))
    );
    assert_eq!(
        factory.default_property_value("toolbar", "orientation"),
        Ok(CapabilityValue::String("horizontal".to_string()))
    );
    assert_eq!(
        factory.default_property_value("color_picker", "show_alpha"),
        Ok(CapabilityValue::Bool(true))
    );
    assert_eq!(
        factory.default_property_value("tabbar", "tab_min_width"),
        Ok(CapabilityValue::UInt(40))
    );
    assert_eq!(
        factory.default_property_value("calendar", "first_day_of_week"),
        Ok(CapabilityValue::String("mon".to_string()))
    );
    assert_eq!(
        factory.default_property_value("dateedit", "display_format"),
        Ok(CapabilityValue::String("yyyy-MM-dd".to_string()))
    );
    assert_eq!(
        factory.default_property_value("timeedit", "display_format"),
        Ok(CapabilityValue::String("HH:mm:ss".to_string()))
    );
}

#[test]
fn property_schema_lookup_is_normalized() {
    let factory = WidgetFactory::new_with_defaults();

    let schema = factory
        .property_schema("line-edit", "max length")
        .expect("normalized schema lookup should succeed");
    assert_eq!(schema.name, "max_length");
    assert_eq!(schema.value_kind, PropertyValueKind::UInt);
    assert!(schema.readable);
    assert!(schema.writable);
}

#[test]
fn every_kind_with_a_constructor_has_a_resolvable_factory_name() {
    // `Panel` is a real `WidgetKind` with a real constructor, but it has no capability
    // row of its own: `panel_capability()` is keyed on `GroupBox` (the same control
    // under another name, exactly as `DockPanel` is to `DockWidget`). It therefore fell
    // through to the `other` arm of `alias_factory_name` and returned `""`.
    //
    // An empty name is not inert: `theme::apply_active_theme` bails out on it, so a
    // `Panel`-kinded widget silently received no theme role. This test pins the
    // resolution so a future kind added without a capability row fails here rather than
    // losing its theme silently.
    let factory = WidgetFactory::new_with_defaults();
    for kind in [WidgetKind::Panel, WidgetKind::DockPanel] {
        let name = crate::widget::capability::factory_name_for_kind(kind);
        assert!(
            !name.is_empty(),
            "{kind:?} resolves to an empty factory name, so `theme::apply_active_theme` \
             will silently skip it; add an alias row in `alias_factory_name`"
        );
        // A non-empty name the factory rejects would be equally broken, so the
        // resolved spelling has to be one the factory actually answers to.
        assert!(
            factory.capability(name).is_some(),
            "factory_name_for_kind({kind:?}) = {name:?}, but the factory has no such name"
        );
    }

    // `MenuItem` is the documented exception: `kind.rs` declares it `kind-role: child`
    // ("created by their owning `Menu`, never constructed directly from a factory
    // name"), so it has no capability row *and* no factory name by design. It must
    // still resolve to a non-empty name, because the theme layer classifies on that
    // name rather than on the factory lookup — an empty string silently skips theming.
    let menu_item = crate::widget::capability::factory_name_for_kind(WidgetKind::MenuItem);
    assert!(!menu_item.is_empty(), "MenuItem must still be nameable for theme classification");
    assert_eq!(menu_item, "menu_item");
}

#[test]
fn a_kind_alias_reaches_the_same_capability_as_its_canonical_spelling() {
    // `context_menu` is an alias of `menu` (the kinds are the same control, as
    // `src/widget/mod.rs` records with `pub type ContextMenu = Menu;`), so both
    // spellings must land on one capability row rather than two.
    let factory = WidgetFactory::new_with_defaults();
    let by_alias = factory.capability("context_menu").expect("context_menu must resolve");
    let by_name = factory.capability("menu").expect("menu must resolve");
    assert_eq!(by_alias.canonical_name, by_name.canonical_name);
    assert_eq!(by_alias.kind, by_name.kind);
}

#[test]
fn capability_manifest_exports_defaults_and_metadata() {
    let factory = WidgetFactory::new_with_defaults();

    let manifest =
        factory.capability_manifest("table").expect("table manifest should be exportable");

    assert_eq!(manifest.kind, WidgetKind::Table);
    // `table` is the canonical name; `table_widget` and `tablewidget` reach the same
    // control through `normalize_key` (which strips `_`), not through an alias row —
    // see `tests/capability_alias_hygiene_test.rs`. The list is therefore empty, and
    // that is the correct state: every alias it used to carry was inert.
    assert_eq!(manifest.canonical_name, "table");
    assert!(
        manifest.aliases.is_empty(),
        "`table` needs no alias: `normalize_key` already accepts `table_widget` / \
         `tablewidget`, so an alias row would be a no-op (found: {:?})",
        manifest.aliases
    );
    // and those spellings must still resolve.
    assert!(factory.capability("table_widget").is_some());
    assert!(factory.capability("tablewidget").is_some());
    assert!(manifest.events.iter().any(|event| event.name == "selection_changed"));
    assert!(manifest.commands.contains(&"clear_selection"));

    let has_model = manifest
        .properties
        .iter()
        .find(|entry| entry.schema.name == "has_model")
        .expect("has_model schema should exist");
    assert_eq!(has_model.default_value, CapabilityValue::Bool(false));

    let selection_mode = manifest
        .properties
        .iter()
        .find(|entry| entry.schema.name == "selection_mode")
        .expect("selection_mode schema should exist");
    assert_eq!(selection_mode.default_value, CapabilityValue::String("single".to_string()));
}

#[test]
fn virtual_list_capability_read_write_roundtrip() {
    let factory = WidgetFactory::new_with_defaults();
    let mut list = VirtualList::new(Rect::new(0, 0, 120, 60));

    assert_eq!(factory.read_property(&list, "has_data_source"), Ok(CapabilityValue::Bool(false)));
    assert_eq!(factory.read_property(&list, "selected_row"), Ok(CapabilityValue::Null));

    factory
        .write_property(&mut list, "row_height", CapabilityValue::UInt(32))
        .expect("row_height should be writable");
    factory
        .write_property(&mut list, "overscan", CapabilityValue::UInt(4))
        .expect("overscan should be writable");
    factory
        .write_property(&mut list, "scroll_row", CapabilityValue::UInt(7))
        .expect("scroll_row should be writable");

    assert_eq!(factory.read_property(&list, "row_height"), Ok(CapabilityValue::UInt(32)));
    assert_eq!(factory.read_property(&list, "overscan"), Ok(CapabilityValue::UInt(4)));
    // Without a data source, scroll_row is normalized back to 0.
    assert_eq!(factory.read_property(&list, "scroll_row"), Ok(CapabilityValue::UInt(0)));
}

#[test]
fn test_create_nonexistent_widget_returns_error() {
    let factory = WidgetFactory::new_with_defaults();
    // `create` returns None (no Ok/Err) for unregistered names
    assert!(factory.create("nonexistent", Rect::new(0, 0, 1, 1), "").is_none());
    // `create_by_kind` returns None for kinds not in the factory
    let empty = WidgetFactory::new();
    assert!(empty.create("btn", Rect::new(0, 0, 1, 1), "").is_none());
}

#[test]
fn test_read_property_unregistered_widget_returns_error() {
    let factory = WidgetFactory::new_with_defaults();
    let widget = factory.create("btn", Rect::new(0, 0, 100, 30), "Save").unwrap();

    // Verify the public API returns UnknownProperty for a schema item that
    // does not exist on any widget, simulating an unregistered property
    let result = factory.read_property(widget.as_ref(), "__bogus_prop__");
    assert_eq!(result, Err(CapabilityAccessError::UnknownProperty));
}

// ---------------------------------------------------------------------------
// Controls that had a complete implementation but were never registered
// ---------------------------------------------------------------------------

/// The six controls that used to be constructible only by naming their Rust
/// type must now resolve through the factory by every name they advertise.
///
/// # Why the assertion is on `create`, not on `capability`
///
/// Declaring a capability and registering a constructor are separate statements,
/// and the defect being fixed was exactly a control that had the former and not
/// the latter. Only `create` proves both are present.
#[test]
fn unregistered_controls_are_constructible_by_canonical_name_and_alias() {
    let factory = WidgetFactory::new_with_defaults();
    let rect = Rect::new(0, 0, 240, 160);

    let cases: &[(&str, &str, WidgetKind)] = &[
        ("timeline_widget", "timeline", WidgetKind::Chart),
        ("timeline_view", "timeline_view", WidgetKind::Chart),
        ("command_palette", "command_box", WidgetKind::ListView),
        ("notification_center", "notifications", WidgetKind::ListView),
        ("diff_viewer", "diff", WidgetKind::Table),
        ("markdown_editor", "md_editor", WidgetKind::RichEdit),
        ("toast_stack", "toasts", WidgetKind::PopupWindow),
        ("grid_table", "gridtable", WidgetKind::GridTable),
        // The four controls added in BLUE16 Phase E-2. Their Rust-side construction
        // paths are order-independent, but only these rows prove the *name* a
        // counterparty would use actually resolves.
        ("number_picker", "picker", WidgetKind::NumberPicker),
        ("otp_input", "otp", WidgetKind::OtpInput),
        ("banner", "notice", WidgetKind::Banner),
        ("pagination", "page_numbers", WidgetKind::Pagination),
        // `RadarChart` (BLUE17 Phase D-3) is a kind of its own rather than a
        // `chart_type` token, because its data model (series over *dimensions*)
        // is not the same shape as `ChartWidget`'s (a value over its index).
        ("radar_chart", "radar", WidgetKind::RadarChart),
        // `KanbanBoard` (BLUE17 Phase D-1) is two-level (columns of cards) with
        // cross-container moves, which no existing control's model carries.
        ("kanban_board", "kanban", WidgetKind::KanbanBoard),
        // `Cascader` (BLUE17 Phase D-5) selects a *path* of varying depth, where
        // `dropdown` selects one index into a flat list.
        ("cascader", "cascade", WidgetKind::Cascader),
        // `QueryBuilder` (BLUE17 Phase D-6) renders the same `FilterExpr` the grid
        // evaluates; the flat `Vec<ColumnFilter>` model could not express it.
        ("query_builder", "filter_builder", WidgetKind::QueryBuilder),
        // `EmojiPicker` (BLUE17 Phase D-4) is the picker shell; its glyph table is
        // supplied by the caller, so it ships no data of its own.
        ("emoji_picker", "emoji", WidgetKind::EmojiPicker),
        // `Mention` (BLUE17 Phase D-2) completes the token before the caret and
        // holds several mentions, where `auto_complete_edit` replaces the whole field.
        ("mention", "at_mention", WidgetKind::Mention),
    ];

    for (name, alias, kind) in cases {
        let widget = factory
            .create(name, rect, "")
            .unwrap_or_else(|| panic!("{name} must be constructible through the factory"));
        assert_eq!(widget.kind(), *kind, "{name} reported the wrong kind");

        let via_alias = factory
            .create(alias, rect, "")
            .unwrap_or_else(|| panic!("{alias} must resolve to the same control as {name}"));
        assert_eq!(via_alias.kind(), *kind, "{alias} reported the wrong kind");
    }
}

/// A registered control must also be reachable through its own property
/// contract, and the factory must resolve it back to *its own* capability.
///
/// # Why the read is asserted, not just the registration
///
/// Three of these controls share a `WidgetKind` with an existing one
/// (`command_palette`/`notification_center` with `list_view`, `diff_viewer` with
/// `table_widget`, `markdown_editor` with `rich_edit`, `timeline_widget` with
/// `chart`, `toast_stack` with `popup_window`). Registering without a type-based
/// tie-break makes the lookup fall through to an empty schema, so the control
/// answers `UnknownWidget` for a property it really has — the exact failure this
/// test exists to catch.
#[test]
fn unregistered_controls_publish_their_own_properties() {
    let factory = WidgetFactory::new_with_defaults();
    let rect = Rect::new(0, 0, 240, 160);

    let cases: &[(&str, &str)] = &[
        ("timeline_widget", "item_count"),
        ("command_palette", "filtered_count"),
        ("notification_center", "unread_count"),
        ("diff_viewer", "change_count"),
        ("markdown_editor", "word_count"),
        ("toast_stack", "toast_count"),
        ("grid_table", "row_count"),
        ("number_picker", "row_count"),
        ("otp_input", "length"),
        ("banner", "action_count"),
        ("pagination", "page_count"),
    ];

    for (name, property) in cases {
        let widget = factory
            .create(name, rect, "")
            .unwrap_or_else(|| panic!("{name} must be constructible through the factory"));
        let value = factory.read_property(widget.as_ref(), property).unwrap_or_else(|error| {
            panic!("{name} must answer its own property {property}, got {error:?}")
        });
        // The point is that the name is *answered by this control*, not that every
        // control starts at zero: `number_picker` legitimately reports the size of
        // its default `0..=100` range. A read that resolved to a sibling's schema
        // would fail above with `UnknownWidget`.
        assert!(
            matches!(value, CapabilityValue::UInt(_)),
            "{name}::{property} must report a count, got {value:?}"
        );
    }
}

/// Writing through the contract must reach the control's real state, so the
/// registered name is not merely a constructor alias.
///
/// # Why a round trip is the evidence
///
/// A write that returns `Ok` but stores nothing would satisfy a weaker test. The
/// assertion is that a subsequent read reports the written value, which can only
/// happen if the setter ran the control's own method.
#[test]
fn unregistered_controls_round_trip_writable_properties() {
    let factory = WidgetFactory::new_with_defaults();
    let rect = Rect::new(0, 0, 240, 160);

    let mut timeline = factory.create("timeline_widget", rect, "").expect("timeline");
    factory
        .write_property(timeline.as_mut(), "row_height", CapabilityValue::UInt(32))
        .expect("timeline row_height should be writable");
    assert_eq!(
        factory.read_property(timeline.as_ref(), "row_height"),
        Ok(CapabilityValue::UInt(32))
    );

    let mut palette = factory.create("command_palette", rect, "").expect("palette");
    factory
        .write_property(palette.as_mut(), "query", CapabilityValue::String("open".to_string()))
        .expect("palette query should be writable");
    assert_eq!(
        factory.read_property(palette.as_ref(), "query"),
        Ok(CapabilityValue::String("open".to_string()))
    );

    let mut diff = factory.create("diff_viewer", rect, "").expect("diff");
    factory
        .write_property(diff.as_mut(), "left_text", CapabilityValue::String("a".to_string()))
        .expect("diff left_text should be writable");
    factory
        .write_property(diff.as_mut(), "right_text", CapabilityValue::String("b".to_string()))
        .expect("diff right_text should be writable");
    assert_eq!(
        factory.read_property(diff.as_ref(), "left_text"),
        Ok(CapabilityValue::String("a".to_string()))
    );
    assert_eq!(
        factory.read_property(diff.as_ref(), "right_text"),
        Ok(CapabilityValue::String("b".to_string()))
    );

    let mut markdown = factory.create("markdown_editor", rect, "").expect("markdown editor");
    factory
        .write_property(markdown.as_mut(), "preview_mode", CapabilityValue::Bool(true))
        .expect("markdown preview_mode should be writable");
    assert_eq!(
        factory.read_property(markdown.as_ref(), "preview_mode"),
        Ok(CapabilityValue::Bool(true))
    );

    let mut toasts = factory.create("toast_stack", rect, "").expect("toast stack");
    factory
        .write_property(toasts.as_mut(), "row_height", CapabilityValue::UInt(48))
        .expect("toast row_height should be writable");
    assert_eq!(factory.read_property(toasts.as_ref(), "row_height"), Ok(CapabilityValue::UInt(48)));

    let mut grid_table = factory.create("grid_table", rect, "").expect("grid table");
    factory
        .write_property(grid_table.as_mut(), "row_height", CapabilityValue::UInt(36))
        .expect("grid table row_height should be writable");
    assert_eq!(
        factory.read_property(grid_table.as_ref(), "row_height"),
        Ok(CapabilityValue::UInt(36))
    );
    factory
        .write_property(
            grid_table.as_mut(),
            "selection_mode",
            CapabilityValue::String("row".to_string()),
        )
        .expect("grid table selection_mode should be writable");
    assert_eq!(
        factory.read_property(grid_table.as_ref(), "selection_mode"),
        Ok(CapabilityValue::String("row".to_string()))
    );
}

/// Reversed bounds must not produce an inverted viewport.
///
/// `TimelineWidget::set_viewport` raises `end` above `start`, so writing an
/// `end` below the live `start` must clamp rather than store a negative span — a
/// span that would otherwise make the projection divide by a non-positive range.
#[test]
fn timeline_viewport_write_never_inverts_the_range() {
    let factory = WidgetFactory::new_with_defaults();
    let mut timeline = factory
        .create("timeline_widget", Rect::new(0, 0, 240, 160), "")
        .expect("timeline must be constructible");

    factory
        .write_property(timeline.as_mut(), "viewport_start", CapabilityValue::Int(50))
        .expect("viewport_start should be writable");
    factory
        .write_property(timeline.as_mut(), "viewport_end", CapabilityValue::Int(10))
        .expect("viewport_end should be writable");

    let start = match factory.read_property(timeline.as_ref(), "viewport_start") {
        Ok(CapabilityValue::Int(value)) => value,
        other => panic!("viewport_start must read back as Int, got {other:?}"),
    };
    let end = match factory.read_property(timeline.as_ref(), "viewport_end") {
        Ok(CapabilityValue::Int(value)) => value,
        other => panic!("viewport_end must read back as Int, got {other:?}"),
    };
    assert!(end > start, "an inverted write must be clamped: start={start}, end={end}");
}

/// Read-only names must answer `ReadOnlyProperty` rather than accepting a write.
///
/// # Why this is asserted per control
///
/// `selected_index` / derived counts have no setter, so accepting a write would
/// silently discard the caller's value while reporting success.
#[test]
fn derived_names_reject_writes_on_newly_registered_controls() {
    let factory = WidgetFactory::new_with_defaults();
    let rect = Rect::new(0, 0, 240, 160);

    let cases: &[(&str, &str, CapabilityValue)] = &[
        ("timeline_widget", "item_count", CapabilityValue::UInt(1)),
        ("command_palette", "filtered_count", CapabilityValue::UInt(1)),
        ("notification_center", "unread_count", CapabilityValue::UInt(1)),
        ("diff_viewer", "change_count", CapabilityValue::UInt(1)),
        ("markdown_editor", "word_count", CapabilityValue::UInt(1)),
        ("toast_stack", "toast_count", CapabilityValue::UInt(1)),
        ("grid_table", "row_count", CapabilityValue::UInt(1)),
        ("otp_input", "is_complete", CapabilityValue::Bool(true)),
        ("banner", "dismissed", CapabilityValue::Bool(true)),
        ("pagination", "page_count", CapabilityValue::UInt(1)),
    ];

    for (name, property, value) in cases {
        let mut widget = factory
            .create(name, rect, "")
            .unwrap_or_else(|| panic!("{name} must be constructible through the factory"));
        assert_eq!(
            factory.write_property(widget.as_mut(), property, value.clone()),
            Err(CapabilityAccessError::ReadOnlyProperty),
            "{name}::{property} must be declared read-only"
        );
    }
}

/// Every schema default must agree with the control the schema describes.
///
/// # Why this is a test and not a comment
///
/// A schema default is load-bearing, not documentation: `view::apply`'s
/// `resolve_null_reset` substitutes it for every `CapabilityValue::Null` patch, so a
/// wrong default is a wrong *value* reaching a live control — dropping a `visible`
/// binding in a view manifest would hide a control the manifest never mentioned.
///
/// The `visible` entry in `access.rs` had drifted from four controls at once. It
/// excluded `Tooltip`/`Popover`/`StatusBar` on the theory that those kinds read bare
/// `visible` as their own popup state, but all three route the name to the base flag
/// and publish popup state under a *different* name (`shown`, `message`). The result
/// was a schema saying `false` for controls whose live answer is `true`.
///
/// This asserts the invariant directly for the property that broke, and asserts the
/// weaker "no default is reported for a property the control does not publish"
/// invariant across the whole registry.
#[test]
fn declared_visible_default_matches_the_constructed_control() {
    let factory = WidgetFactory::new_with_defaults();
    let rect = Rect::new(0, 0, 200, 60);

    let mut checked = 0usize;
    for capability in factory.capabilities() {
        let name = capability.canonical_name;
        let Ok(declared) = factory.default_property_value(name, "visible") else {
            continue;
        };
        let Some(widget) = factory.create(name, rect, "") else {
            continue;
        };
        let Ok(live) = factory.read_property(widget.as_ref(), "visible") else {
            continue;
        };
        assert_eq!(
            declared, live,
            "`{name}` declares `visible` default {declared:?} but a freshly \
             constructed control reports {live:?}"
        );
        checked += 1;
    }
    assert!(checked >= 20, "expected the `visible` default to cover many kinds, got {checked}");
}

/// A schema default must never name a property the control does not publish.
///
/// `default_property_value` resolves through `capability().kind` to the shared
/// per-kind tables, so a kind whose row is shared by two controls (`table` and
/// `virtual_list` both declare `WidgetKind::Table`) can hand one control the other's
/// defaults. This checks the property *name* is one the capability advertises, which
/// is the part that is checkable without constructing every kind.
#[test]
fn declared_defaults_are_published_properties() {
    let factory = WidgetFactory::new_with_defaults();

    let mut violations = Vec::new();
    for capability in factory.capabilities() {
        let name = capability.canonical_name;
        for property in capability.properties {
            let Ok(value) = factory.default_property_value(name, property.name) else {
                continue;
            };
            let published = capability
                .properties
                .iter()
                .any(|schema| normalize_key(schema.name) == normalize_key(property.name));
            if !published {
                violations.push(format!("{name}::{} -> {value:?}", property.name));
            }
        }
    }
    assert!(violations.is_empty(), "defaults declared for unpublished properties: {violations:?}");
}