ferroui-controls 0.3.0

The controls of FerroUI: the application model, top levels, the control set and automation peers
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
//! Positions are computed from the metrics of the test fonts (see
//! `ferroui_base::media::text_formatting::testing`): every glyph advances
//! half an em and a line is 1.1 em high, so at the default size of 12 a
//! character is 6 wide and a line 13.2 high.

use crate::documents::{InlineCollection, InlineUIContainer, Run};
use crate::test_support::{test_scope, TestRoot};
use crate::testing::{
    MockWindowImpl, MockWindowingPlatform, TestClipboardFailure, TestClipboardImpl, TestServices,
    UnitTestApplication, UnitTestApplicationScope,
};
use crate::text_box_tests::{panic_message, record_log_messages, EXPECTED_CLIPBOARD_EXCEPTIONS};
use crate::utils::ClipboardHelper;
use crate::{Border, Control, SelectableTextBlock, Window};
use ferroui_base::input::platform::{
    ClipboardError, ClipboardErrorKind, ClipboardExtensions, ClipboardType, IClipboard, IPlatformClipboardManagerImpl,
    PlatformClipboardManager,
};
use ferroui_base::media::text_formatting::testing::TextTestScope;
use ferroui_base::platform::IPlatformRenderInterface;
use ferroui_base::threading::Dispatcher;
use ferroui_base::{FerroLocator, LocatorExtensions};
use ferroui_base::input::{
    FocusChangedEventArgs, IPointer, InputElement, Key, KeyEventArgs, KeyModifiers, MouseButton, Pointer,
    PointerEventArgs, PointerPointProperties, PointerPressedEventArgs, PointerReleasedEventArgs, PointerType,
    PointerUpdateKind, RawInputModifiers,
};
use ferroui_base::interactivity::Interactive;
use ferroui_base::media::text_formatting::ShapedTextRun;
use ferroui_base::media::{Brushes, FontStyle, FontWeight, IBrush, TextAlignment};
use ferroui_base::{Point, Rect, Ref, Size, Visual};
use std::cell::{Cell, RefCell};
use std::rc::Rc;

fn infinity() -> Size {
    Size::new(f64::INFINITY, f64::INFINITY)
}

fn text_block(text: &str) -> Ref<SelectableTextBlock> {
    let target = SelectableTextBlock::new();
    target.set_text(Some(text));
    target
}

fn utf16_length(text: &str) -> i32 {
    text.encode_utf16().count() as i32
}

/// Raises pointer events on an element the way a mouse would.
struct Mouse {
    pointer: Rc<Pointer>,
    next_stamp: Cell<u64>,
    pressed_buttons: Cell<RawInputModifiers>,
    pressed_button: Cell<MouseButton>,
}

impl Mouse {
    fn new() -> Self {
        Self {
            pointer: Pointer::new(Pointer::get_next_free_id(), PointerType::Mouse, true),
            next_stamp: Cell::new(1),
            pressed_buttons: Cell::new(RawInputModifiers::NONE),
            pressed_button: Cell::new(MouseButton::None),
        }
    }

    fn timestamp(&self) -> u64 {
        let stamp = self.next_stamp.get();
        self.next_stamp.set(stamp + 1);
        stamp
    }

    fn pointer(&self) -> Rc<dyn IPointer> {
        self.pointer.clone()
    }

    fn convert(mouse_button: MouseButton) -> RawInputModifiers {
        match mouse_button {
            MouseButton::Left => RawInputModifiers::LEFT_MOUSE_BUTTON,
            MouseButton::Right => RawInputModifiers::RIGHT_MOUSE_BUTTON,
            MouseButton::Middle => RawInputModifiers::MIDDLE_MOUSE_BUTTON,
            _ => RawInputModifiers::NONE,
        }
    }

    fn root_of(target: &Interactive) -> Ref<Visual> {
        target.visual_root().unwrap_or_else(|| target.to_ref().upcast())
    }

    /// Presses a button at a position in the coordinates of the root.
    fn down(&self, target: &Interactive, mouse_button: MouseButton, position: Point) {
        self.down_with(target, mouse_button, position, KeyModifiers::NONE, 1);
    }

    fn down_with(
        &self,
        target: &Interactive,
        mouse_button: MouseButton,
        position: Point,
        modifiers: KeyModifiers,
        click_count: i32,
    ) {
        self.pressed_buttons.set(self.pressed_buttons.get() | Self::convert(mouse_button));
        self.pressed_button.set(mouse_button);

        let properties = PointerPointProperties::new(
            self.pressed_buttons.get(),
            match mouse_button {
                MouseButton::Left => PointerUpdateKind::LeftButtonPressed,
                MouseButton::Middle => PointerUpdateKind::MiddleButtonPressed,
                MouseButton::Right => PointerUpdateKind::RightButtonPressed,
                _ => PointerUpdateKind::Other,
            },
        );

        let element: Option<Ref<InputElement>> = target.to_ref().cast::<InputElement>();
        self.pointer.capture(element.as_ref());

        let root = Self::root_of(target);
        target.raise_event(&PointerPressedEventArgs::new(
            target.to_ref(),
            self.pointer(),
            &root,
            position,
            self.timestamp(),
            properties,
            modifiers,
            click_count,
        ));
    }

    /// Moves the pointer to a position in the coordinates of the root.
    fn move_(&self, target: &Interactive, position: Point) {
        let root = Self::root_of(target);
        target.raise_event(&PointerEventArgs::new(
            Some(InputElement::pointer_moved_event()),
            target.to_ref(),
            self.pointer(),
            Some(&*root),
            position,
            self.timestamp(),
            PointerPointProperties::new(self.pressed_buttons.get(), PointerUpdateKind::Other),
            KeyModifiers::NONE,
        ));
    }

    /// Releases a button at a position in the coordinates of the root.
    fn up(&self, target: &Interactive, mouse_button: MouseButton, position: Point) {
        let button = Self::convert(mouse_button);
        self.pressed_buttons.set((self.pressed_buttons.get() | button) ^ button);

        let properties = PointerPointProperties::new(
            self.pressed_buttons.get(),
            match mouse_button {
                MouseButton::Left => PointerUpdateKind::LeftButtonReleased,
                MouseButton::Middle => PointerUpdateKind::MiddleButtonReleased,
                MouseButton::Right => PointerUpdateKind::RightButtonReleased,
                _ => PointerUpdateKind::Other,
            },
        );

        let root = Self::root_of(target);
        target.raise_event(&PointerReleasedEventArgs::new(
            target.to_ref(),
            self.pointer(),
            &root,
            position,
            self.timestamp(),
            properties,
            KeyModifiers::NONE,
            self.pressed_button.get(),
        ));

        self.pointer.capture(None);
    }
}

/// The services of the tests that show the text block in a window: the
/// test fonts under the application of the windowing tests.
struct WindowServices {
    _app: UnitTestApplicationScope,
    _text: TextTestScope,
}

fn window_services() -> WindowServices {
    let text = TextTestScope::new();
    let render_interface = FerroLocator::current()
        .get_service::<dyn IPlatformRenderInterface>()
        .expect("the text services have a render interface");
    let app = UnitTestApplication::start(TestServices::styled_window().with_render_interface(render_interface));
    WindowServices { _app: app, _text: text }
}

/// Shows `target` in a window whose platform implementation exposes
/// `clipboard` as its clipboard.
fn window_with_clipboard(target: &Ref<SelectableTextBlock>, clipboard: &Rc<TestClipboardImpl>) -> Ref<Window> {
    let window_impl = MockWindowingPlatform::create_window_mock();
    window_impl.setup_feature::<dyn IClipboard>(clipboard.clipboard());
    show_in_window(target, window_impl)
}

/// Shows `target` in a window whose platform implementation has a
/// clipboard manager with `primary_selection` as the primary selection.
fn window_with_primary_selection(
    target: &Ref<SelectableTextBlock>,
    primary_selection: &Rc<TestClipboardImpl>,
) -> Ref<Window> {
    let window_impl = MockWindowingPlatform::create_window_mock();
    window_impl.setup_feature::<dyn IPlatformClipboardManagerImpl>(Rc::new(PlatformClipboardManager::new(
        None,
        Some(primary_selection.clipboard()),
    )));
    show_in_window(target, window_impl)
}

fn show_in_window(target: &Ref<SelectableTextBlock>, window_impl: Rc<MockWindowImpl>) -> Ref<Window> {
    let window = Window::with_impl(window_impl);
    window.set_content(Some(Control::boxed(target)));
    window.show();
    window
}

/// Runs the clipboard operations the text block posted to the dispatcher.
fn run_clipboard_operations() {
    Dispatcher::ui_thread().run_jobs(None);
}

fn root_with(target: &Ref<SelectableTextBlock>, client_size: Size) -> Ref<TestRoot> {
    let root = TestRoot::with_child(target.clone());
    root.set_client_size(client_size);

    root.measure(client_size);
    root.arrange(Rect::from_size(client_size));
    root.execute_initial_layout_pass();

    root
}

fn raise_key_down(target: &SelectableTextBlock, key: Key, modifiers: KeyModifiers) -> KeyEventArgs {
    let mut args = KeyEventArgs::new();
    args.set_routed_event(Some(InputElement::key_down_event()));
    args.key = key;
    args.key_modifiers = modifiers;
    target.raise_event(&args);
    args
}

// Content: Run("foo") + InlineUIContainer + Run("bar")
// The text of the inlines is "foo\u{FFFC}bar" (indices 0-6):
//   0='f', 1='o', 2='o', 3='\u{FFFC}' (embedded control), 4='b', 5='a', 6='r'
#[test]
fn selection_with_inline_ui_container_returns_correct_text() {
    let cases: [(i32, i32, &str); 7] = [
        // Entirely before the container.
        (0, 3, "foo"),
        // Exactly the character of the container.
        (3, 4, "\u{FFFC}"),
        // Up to and including the container (fencepost: last char before "bar").
        (0, 4, "foo\u{FFFC}"),
        // Starting exactly after the container (fencepost: first char of "bar").
        (4, 7, "bar"),
        // The container through the end.
        (3, 7, "\u{FFFC}bar"),
        // Spanning the container (one char either side).
        (2, 5, "o\u{FFFC}b"),
        // The entire content.
        (0, 7, "foo\u{FFFC}bar"),
    ];

    for (start, end, expected) in cases {
        let _scope = test_scope();
        let target = SelectableTextBlock::new();

        let inlines = target.inlines().unwrap();
        inlines.add(Run::with_text(Some("foo")));
        inlines.add(InlineUIContainer::with_child(Border::new()));
        inlines.add(Run::with_text(Some("bar")));

        target.measure(infinity());

        // The selection indices correspond to character positions of the
        // text layout. The embedded control run occupies 1 position and the
        // text of the inlines has a matching U+FFFC placeholder, so they
        // stay in sync.
        target.set_selection_start(start);
        target.set_selection_end(end);

        assert_eq!(target.selected_text(), expected, "selection {start}..{end}");
    }
}

#[test]
fn dragging_selection_should_reach_end_of_text_when_text_is_aligned() {
    for text_alignment in [TextAlignment::Center, TextAlignment::Right] {
        let _scope = test_scope();
        let target = SelectableTextBlock::new();
        target.set_width(200.0);
        target.set_text(Some("Aligned text"));
        target.set_text_alignment(text_alignment);

        let root = root_with(&target, Size::new(300.0, 100.0));

        let text_length = utf16_length(&target.text().unwrap());

        let first_character_bounds = target.text_layout().hit_test_text_position(0);
        let last_character_bounds = target.text_layout().hit_test_text_position(text_length - 1);
        let mouse = Mouse::new();
        let start_point = Point::new(
            first_character_bounds.x + first_character_bounds.width / 2.0,
            first_character_bounds.y + first_character_bounds.height / 2.0,
        );
        let end_point = Point::new(
            (target.bounds().width - 1.0).min(last_character_bounds.right() + 10.0),
            last_character_bounds.y + last_character_bounds.height / 2.0,
        );

        mouse.down(&target, MouseButton::Left, target.translate_point(start_point, &root).unwrap_or_default());
        mouse.move_(&target, target.translate_point(end_point, &root).unwrap_or_default());

        assert_eq!(text_length, target.selection_start().max(target.selection_end()), "{text_alignment:?}");
    }
}

#[test]
fn selection_foreground_should_not_reset_run_typeface_and_style() {
    let _scope = test_scope();
    let target = SelectableTextBlock::new();
    let selection_foreground_brush: Rc<dyn IBrush> = Brushes::red();
    target.set_selection_foreground_brush(Some(selection_foreground_brush.clone()));

    let run = Run::with_text(Some("Hello"));
    run.set_font_weight(FontWeight::Bold);
    run.set_font_style(FontStyle::Italic);
    run.set_font_size(20.0);

    target.inlines().unwrap().add(run.clone());

    target.measure(infinity());

    target.set_selection_start(0);
    target.set_selection_end(utf16_length(&run.text().unwrap()));

    target.measure(infinity());

    let text_layout = target.text_layout();

    let mut shaped_runs = 0;
    let mut checked = false;

    for text_line in text_layout.text_lines() {
        for text_run in text_line.text_runs().iter() {
            let Some(shaped_run) = text_run.downcast_ref::<ShapedTextRun>() else {
                continue;
            };

            shaped_runs += 1;

            if checked {
                continue;
            }
            checked = true;

            // The first shaped run is the selected one.
            let properties = shaped_run.run_properties();

            assert_eq!(FontWeight::Bold, properties.typeface().weight());
            assert_eq!(FontStyle::Italic, properties.typeface().style());

            let foreground_brush = properties.foreground_brush().expect("the selection foreground");
            assert!(std::ptr::addr_eq(Rc::as_ptr(&selection_foreground_brush), Rc::as_ptr(foreground_brush)));
        }
    }

    assert!(shaped_runs > 0);
}

#[test]
fn pointer_selection_is_published_to_primary_selection() {
    let _services = window_services();
    let primary_selection = TestClipboardImpl::new();

    let target = text_block("0123");
    let window = window_with_primary_selection(&target, &primary_selection);

    let mouse = Mouse::new();
    mouse.down(&target, MouseButton::Left, Point::new(1.0, 300.0));
    mouse.move_(&target, Point::new(700.0, 300.0));
    mouse.up(&target, MouseButton::Left, Point::new(700.0, 300.0));

    assert_eq!("0123", target.selected_text());

    run_clipboard_operations();
    let text = Rc::new(RefCell::new(None));
    let clipboard = window.try_get_clipboard(ClipboardType::PrimarySelection).expect("the window has a primary selection");
    let _ = Dispatcher::ui_thread().invoke_async_task_local({
        let text = text.clone();
        move || async move {
            *text.borrow_mut() = clipboard.try_get_text_async().await.expect("the text of the primary selection");
        }
    });
    run_clipboard_operations();
    assert_eq!(Some("0123".to_owned()), *text.borrow());
    assert_eq!(Some("0123".to_owned()), primary_selection.text());
    // The window has no default clipboard: only the primary selection.
    assert!(window.clipboard().is_none());
}

#[test]
fn inlines_changes_should_update_selection() {
    let _scope = test_scope();
    let target = SelectableTextBlock::new();
    let inlines = target.inlines().unwrap();
    inlines.add(Run::with_text(Some("foo")));
    target.set_selection_end(3);

    let selected_text_changed = Rc::new(Cell::new(false));
    let changed = selected_text_changed.clone();
    let _subscription = target.property_changed(move |e| {
        if e.property() == SelectableTextBlock::selected_text_property().as_property() {
            changed.set(true);
        }
    });

    inlines.add(Run::with_text(Some("bar")));

    assert!(selected_text_changed.get());

    target.set_selection_start(6);
    target.set_selection_end(0);
    inlines.remove_at(1);

    assert_eq!(3, target.selection_start());
    assert_eq!(0, target.selection_end());

    target.set_selection_start(0);
    target.set_selection_end(3);

    inlines.set(0, Run::with_text(Some("a")).upcast());

    assert_eq!(0, target.selection_start());
    assert_eq!(1, target.selection_end());
    assert_eq!("a", target.selected_text());
}

#[test]
fn text_changes_should_update_selection() {
    let _scope = test_scope();
    let target = text_block("foo");
    target.set_selection_end(3);

    let selected_text_changed = Rc::new(Cell::new(false));
    let changed = selected_text_changed.clone();
    let _subscription = target.property_changed(move |e| {
        if e.property() == SelectableTextBlock::selected_text_property().as_property() {
            changed.set(true);
        }
    });

    target.set_text(Some("a"));

    assert_eq!(0, target.selection_start());
    assert_eq!(1, target.selection_end());
    assert!(selected_text_changed.get());
}

#[test]
fn coerce_caret_index_on_text_changed() {
    let _scope = test_scope();
    let target = text_block("foo");
    target.set_selection_start(3);
    target.set_selection_end(3);

    target.set_text(Some("a"));

    assert_eq!(1, target.selection_start());
    assert_eq!(1, target.selection_end());
}

#[test]
fn can_copy_tracks_whether_selection_covers_any_character() {
    for (start, end, expected) in [(2, 2, false), (1, 3, true), (3, 1, true), (0, 4, true)] {
        let _scope = test_scope();
        let target = text_block("abcd");

        target.measure(infinity());

        target.set_selection_start(start);
        target.set_selection_end(end);

        assert_eq!(expected, target.can_copy(), "selection {start}..{end}");
    }
}

#[test]
fn can_copy_tracks_selection_over_inlines() {
    let _scope = test_scope();
    let target = SelectableTextBlock::new();

    let inlines = target.inlines().unwrap();
    inlines.add(Run::with_text(Some("foo")));
    inlines.add(Run::with_text(Some("bar")));

    target.measure(infinity());

    assert!(!target.can_copy());

    target.set_selection_start(2);
    target.set_selection_end(5);

    assert!(target.can_copy());

    target.clear_selection();

    assert!(!target.can_copy());
}

#[test]
fn right_click_below_text_should_keep_selection() {
    let _scope = test_scope();
    let target = text_block("first line\nsecond line\n");
    target.set_width(200.0);

    let root = root_with(&target, Size::new(300.0, 200.0));

    let mouse = Mouse::new();
    let first_character_bounds = target.text_layout().hit_test_text_position(0);
    let start = target
        .translate_point(
            Point::new(
                first_character_bounds.x + 1.0,
                first_character_bounds.y + first_character_bounds.height / 2.0,
            ),
            &root,
        )
        .unwrap_or_default();
    let below_text = target
        .translate_point(
            Point::new(target.text_layout().width() / 2.0, target.text_layout().height() + 10.0),
            &root,
        )
        .unwrap_or_default();

    mouse.down(&target, MouseButton::Left, start);
    mouse.move_(&target, below_text);
    mouse.up(&target, MouseButton::Left, below_text);

    mouse.down(&target, MouseButton::Right, below_text);
    mouse.up(&target, MouseButton::Right, below_text);

    assert_eq!(0, target.selection_start());
    assert_eq!(utf16_length(&target.text().unwrap()), target.selection_end());
    assert!(target.can_copy());
}

#[test]
fn right_click_on_unselected_text_should_move_selection() {
    let _scope = test_scope();
    let target = text_block("first line\nsecond line");
    target.set_width(200.0);

    let root = root_with(&target, Size::new(300.0, 200.0));

    target.set_selection_start(0);
    target.set_selection_end(5);

    let character_bounds = target.text_layout().hit_test_text_position(14);
    let on_unselected_text = target.translate_point(character_bounds.center(), &root).unwrap_or_default();
    let mouse = Mouse::new();

    mouse.down(&target, MouseButton::Right, on_unselected_text);
    mouse.up(&target, MouseButton::Right, on_unselected_text);

    assert_eq!(target.selection_start(), target.selection_end());
    assert!(!target.can_copy());
}

#[test]
fn should_shape_inlines_when_text_layout_is_created_between_content_change_and_measure() {
    let _scope = test_scope();

    let target = SelectableTextBlock::new();
    target.set_inlines(Some(InlineCollection::new()));

    target.measure(Size::new(1000.0, 1000.0));
    target.arrange(Rect::new(0.0, 0.0, 1000.0, 1000.0));

    let inlines = InlineCollection::new();
    inlines.add(Run::with_text(Some("Hello World")));
    target.set_inlines(Some(inlines));

    let _ = target.text_layout();

    target.measure(Size::new(1000.0, 1000.0));

    assert!(target.desired_size().width > 0.0, "DesiredSize was {:?}", target.desired_size());
}

// ---------------------------------------------------------------------------
// Tests that are not in the reference test class: they cover the members it
// leaves untested (keys, clipboard, click counts, focus).
// ---------------------------------------------------------------------------

#[test]
fn is_focusable_by_default() {
    let _scope = test_scope();
    let target = SelectableTextBlock::new();

    assert!(target.focusable());
}

#[test]
fn select_all_and_clear_selection() {
    let _scope = test_scope();
    let target = text_block("ab\u{1F600}");

    target.select_all();

    // The indices are UTF-16 code units: the emoji is a surrogate pair.
    assert_eq!(0, target.selection_start());
    assert_eq!(4, target.selection_end());
    assert_eq!("ab\u{1F600}", target.selected_text());

    target.set_selection_start(2);
    target.clear_selection();

    assert_eq!(2, target.selection_end());
    assert_eq!("", target.selected_text());
}

#[test]
fn selection_index_between_carriage_return_and_line_feed_moves_after_the_pair() {
    let _scope = test_scope();
    let target = text_block("a\r\nb");

    target.set_selection_end(2);
    assert_eq!(3, target.selection_end());

    target.set_selection_start(-1);
    assert_eq!(0, target.selection_start());

    target.set_selection_start(10);
    assert_eq!(4, target.selection_start());
}

#[test]
fn copy_hotkey_copies_the_selection_to_the_clipboard() {
    let _services = window_services();
    let clipboard = TestClipboardImpl::new();

    let target = text_block("abcd");
    let _window = window_with_clipboard(&target, &clipboard);

    target.set_selection_start(1);
    target.set_selection_end(3);

    let args = raise_key_down(&target, Key::C, KeyModifiers::CONTROL);

    assert!(args.handled());
    run_clipboard_operations();
    assert_eq!(1, clipboard.set_data_count());
    assert_eq!(Some("bc".to_owned()), clipboard.text());

    // A key that is no hotkey is left unhandled.
    let args = raise_key_down(&target, Key::C, KeyModifiers::NONE);

    assert!(!args.handled());
    run_clipboard_operations();
    assert_eq!(1, clipboard.set_data_count());
}

#[test]
fn copy_does_nothing_without_selection() {
    let _services = window_services();
    let clipboard = TestClipboardImpl::new();

    let target = text_block("abcd");
    let _window = window_with_clipboard(&target, &clipboard);

    target.copy();

    run_clipboard_operations();
    assert_eq!(0, clipboard.set_data_count());
}

#[test]
fn copy_does_nothing_without_a_top_level_or_a_clipboard() {
    // Not in a top-level.
    {
        let _scope = test_scope();
        let target = text_block("abcd");
        let _root = root_with(&target, Size::new(100.0, 100.0));
        target.set_selection_end(4);

        target.copy();
        Dispatcher::ui_thread().run_jobs(None);
    }

    // In a top-level whose platform has no clipboard.
    let _services = window_services();
    let target = text_block("abcd");
    let window = show_in_window(&target, MockWindowingPlatform::create_window_mock());
    target.set_selection_end(4);
    assert!(window.clipboard().is_none());

    target.copy();
    run_clipboard_operations();
}

#[test]
fn handling_copying_to_clipboard_cancels_the_copy() {
    let _services = window_services();
    let clipboard = TestClipboardImpl::new();

    let target = text_block("abcd");
    let _window = window_with_clipboard(&target, &clipboard);
    target.set_selection_end(4);

    let raised = Rc::new(Cell::new(0));
    let counter = raised.clone();
    let token = target.copying_to_clipboard(move |_, e| {
        counter.set(counter.get() + 1);
        e.set_handled(true);
    });

    target.copy();

    assert_eq!(1, raised.get());
    run_clipboard_operations();
    assert_eq!(0, clipboard.set_data_count());

    // Without the handler the selection is copied.
    target.remove_handler(SelectableTextBlock::copying_to_clipboard_event(), token);
    target.copy();
    run_clipboard_operations();
    assert_eq!(1, clipboard.set_data_count());
    assert_eq!(Some("abcd".to_owned()), clipboard.text());
}

/// A text block with the text "abcd" and "bc" selected, in a window whose
/// platform implementation exposes `clipboard`.
fn create_selectable_text_block_in_top_level(clipboard: &Rc<TestClipboardImpl>) -> (Ref<SelectableTextBlock>, Ref<Window>) {
    let target = text_block("abcd");
    target.set_selection_start(1);
    target.set_selection_end(3);

    let window = window_with_clipboard(&target, clipboard);

    assert!(target.can_copy());

    (target, window)
}

#[test]
fn copy_does_not_throw_when_clipboard_fails() {
    for exception_type in EXPECTED_CLIPBOARD_EXCEPTIONS {
        let _services = window_services();

        let clipboard_impl = TestClipboardImpl::throwing(exception_type);
        let (target, _window) = create_selectable_text_block_in_top_level(&clipboard_impl);
        let (messages, sink) = record_log_messages();

        target.copy();

        let unhandled = std::panic::catch_unwind(std::panic::AssertUnwindSafe(run_clipboard_operations));
        sink.dispose();

        assert!(unhandled.is_ok(), "{exception_type:?}");
        assert_eq!(1, clipboard_impl.set_data_count());
        assert_eq!(vec!["Failed to write text to clipboard: {Error}"], *messages.borrow(), "{exception_type:?}");
    }
}

// The reference fails the clipboard with an `InvalidOperationException` and
// expects it out of the dispatcher: the failure that is not one a platform
// clipboard is known for is the error of the kind `Other`.
#[test]
fn copy_does_not_swallow_unexpected_exceptions() {
    let _services = window_services();

    let clipboard_impl = TestClipboardImpl::throwing(ClipboardErrorKind::Other);
    let (target, _window) = create_selectable_text_block_in_top_level(&clipboard_impl);
    let (messages, sink) = record_log_messages();

    target.copy();

    let unhandled = std::panic::catch_unwind(std::panic::AssertUnwindSafe(run_clipboard_operations))
        .expect_err("the failure of the clipboard");
    sink.dispose();

    let expected = ClipboardHelper::unhandled_message(&ClipboardError::from_kind(ClipboardErrorKind::Other));
    assert_eq!(Some(expected.as_str()), panic_message(&*unhandled));
    assert_eq!(1, clipboard_impl.set_data_count());
    assert!(messages.borrow().is_empty());
}

// Not in the reference: a clipboard that panics is not swallowed either.
#[test]
fn copy_does_not_swallow_unexpected_panics() {
    let _services = window_services();

    let clipboard = TestClipboardImpl::failing(TestClipboardFailure::Panics);
    let (target, _window) = create_selectable_text_block_in_top_level(&clipboard);

    target.copy();

    let unhandled = std::panic::catch_unwind(std::panic::AssertUnwindSafe(run_clipboard_operations))
        .expect_err("the panic of the clipboard");

    assert_eq!(Some(&"The clipboard failed."), unhandled.downcast_ref::<&'static str>());
    assert_eq!(1, clipboard.set_data_count());
}

// Not in the reference: a clipboard whose operation never completes logs
// nothing and raises nothing.
#[test]
fn copy_waits_for_a_clipboard_that_never_completes() {
    let _services = window_services();

    let clipboard = TestClipboardImpl::failing(TestClipboardFailure::NeverCompletes);
    let (target, _window) = create_selectable_text_block_in_top_level(&clipboard);
    let (messages, sink) = record_log_messages();

    target.copy();

    let unhandled = std::panic::catch_unwind(std::panic::AssertUnwindSafe(run_clipboard_operations));
    sink.dispose();

    assert!(unhandled.is_ok());
    assert_eq!(1, clipboard.set_data_count());
    assert!(messages.borrow().is_empty());
}

// Not in the reference, which logs whatever the primary selection throws
// when a selection is published.
#[test]
fn failing_primary_selection_is_logged_when_a_selection_is_published() {
    let failures = EXPECTED_CLIPBOARD_EXCEPTIONS
        .into_iter()
        .chain([ClipboardErrorKind::Other])
        .map(TestClipboardFailure::Fails)
        .chain([TestClipboardFailure::Panics]);

    for failure in failures {
        let _services = window_services();
        let primary_selection = TestClipboardImpl::failing(failure);
        let target = text_block("0123");
        let _window = window_with_primary_selection(&target, &primary_selection);
        let (messages, sink) = record_log_messages();

        let mouse = crate::mouse_test_helper::MouseTestHelper::new();
        let unhandled = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            mouse.down_at(&target, MouseButton::Left, Point::new(1.0, 5.0), 1);
            mouse.move_(&target, Point::new(700.0, 5.0));
            mouse.up_at(&target, MouseButton::Left, Point::new(700.0, 5.0));
            run_clipboard_operations();
        }));
        sink.dispose();

        assert!(unhandled.is_ok(), "{failure:?}");
        assert_eq!(1, primary_selection.set_data_count(), "{failure:?}");
        assert_eq!(vec!["Failed to write text to primary selection: {Error}"], *messages.borrow(), "{failure:?}");
    }
}

#[test]
fn select_all_hotkey_selects_all_text() {
    let _services = window_services();
    let target = text_block("abcd");

    let args = raise_key_down(&target, Key::A, KeyModifiers::CONTROL);

    assert!(args.handled());
    assert_eq!(0, target.selection_start());
    assert_eq!(4, target.selection_end());
    assert!(target.can_copy());
}

#[test]
fn losing_focus_clears_the_selection() {
    let _scope = test_scope();
    let target = text_block("abcd");
    target.set_selection_start(1);
    target.set_selection_end(3);

    target.raise_event(&FocusChangedEventArgs::new(InputElement::lost_focus_event()));

    assert_eq!(1, target.selection_start());
    assert_eq!(1, target.selection_end());
    assert!(!target.can_copy());
}

// "hello world" at size 12: a character is 6 wide, so x = 8 is in 'e'
// (index 1), x = 44 in 'o' of "world" (index 7).
#[test]
fn click_counts_select_caret_word_and_all() {
    let _scope = test_scope();
    let target = text_block("hello world");
    let _root = root_with(&target, Size::new(300.0, 100.0));

    let mouse = Mouse::new();
    let position = Point::new(8.0, 5.0);

    mouse.down_with(&target, MouseButton::Left, position, KeyModifiers::NONE, 1);
    mouse.up(&target, MouseButton::Left, position);

    assert_eq!((1, 1), (target.selection_start(), target.selection_end()));

    mouse.down_with(&target, MouseButton::Left, position, KeyModifiers::NONE, 2);

    assert_eq!((0, 5), (target.selection_start(), target.selection_end()));
    assert_eq!("hello", target.selected_text());

    // Dragging after a double click extends the selection by words.
    mouse.move_(&target, Point::new(44.0, 5.0));

    assert_eq!((0, 11), (target.selection_start(), target.selection_end()));

    mouse.up(&target, MouseButton::Left, Point::new(44.0, 5.0));

    mouse.down_with(&target, MouseButton::Left, position, KeyModifiers::NONE, 3);
    mouse.up(&target, MouseButton::Left, position);

    assert_eq!((0, 11), (target.selection_start(), target.selection_end()));
}

#[test]
fn shift_click_extends_the_selection() {
    let _scope = test_scope();
    let target = text_block("hello world");
    let _root = root_with(&target, Size::new(300.0, 100.0));

    let mouse = Mouse::new();

    mouse.down(&target, MouseButton::Left, Point::new(13.0, 5.0));
    mouse.up(&target, MouseButton::Left, Point::new(13.0, 5.0));

    assert_eq!((2, 2), (target.selection_start(), target.selection_end()));

    mouse.down_with(&target, MouseButton::Left, Point::new(49.0, 5.0), KeyModifiers::SHIFT, 1);
    mouse.up(&target, MouseButton::Left, Point::new(49.0, 5.0));

    assert_eq!((2, 8), (target.selection_start(), target.selection_end()));
    assert_eq!("llo wo", target.selected_text());
}

#[test]
fn pointer_press_captures_the_pointer_and_release_frees_it() {
    let _scope = test_scope();
    let target = text_block("hello world");
    let _root = root_with(&target, Size::new(300.0, 100.0));

    let mouse = Mouse::new();

    mouse.down(&target, MouseButton::Left, Point::new(13.0, 5.0));
    assert!(mouse.pointer.captured().is_some_and(|captured| captured == target));

    // The mouse releases the capture itself after the event; the control
    // has released it by then.
    let released = Rc::new(Cell::new(false));
    let flag = released.clone();
    let pointer = mouse.pointer.clone();
    target.add_handler_with(
        InputElement::pointer_released_event(),
        move |_, _: &PointerReleasedEventArgs| flag.set(pointer.captured().is_none()),
        ferroui_base::interactivity::RoutingStrategies::BUBBLE,
        true,
    );

    mouse.up(&target, MouseButton::Left, Point::new(13.0, 5.0));

    assert!(released.get());
}

/// With `TextBlock.LayoutRoundingPlacement` set to `Above` (a FerroUI addition) the text is drawn
/// lower by what layout rounding adds to the block, and the pointer hits the text where it is
/// drawn: a press just below the end of the first line, inside that offset, is on the first line
/// with `Above` and on the second with `Below`.
#[test]
fn hit_testing_follows_the_text_with_the_layout_rounding_above() {
    use crate::TextLayoutRoundingPlacement;

    let press = |placement| {
        let _scope = test_scope();
        let target = text_block("first\nsecond");
        target.set_layout_rounding_placement(placement);
        let root = root_with(&target, Size::new(300.0, 200.0));

        // Two lines of 13.2 are 26.4 high, 27 with layout rounding: 0.6 above the text.
        let line_height = target.text_layout().text_lines()[0].height();
        let offset = target.layout_rounding_offset();
        let position = Point::new(1.0, line_height + 0.3);

        let mouse = Mouse::new();
        let at = target.translate_point(position, &root).unwrap_or_default();
        mouse.down(&target, MouseButton::Left, at);
        mouse.up(&target, MouseButton::Left, at);
        (offset, target.selection_start())
    };

    let (offset, below) = press(TextLayoutRoundingPlacement::Below);
    assert_eq!(0.0, offset);
    // "first\n" is 6 code units: the press is at the start of the second line.
    assert_eq!(6, below);

    let (offset, above) = press(TextLayoutRoundingPlacement::Above);
    assert!((offset - 0.6).abs() < 1e-9, "{offset}");
    assert_eq!(0, above);
}