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
//! Port of `LayoutableTests.cs` (base unit tests). Most tests use
//! decorators, panels, borders and the test root, so they live with the
//! controls.
//!
//! The mock layout manager of the upstream tests (`Mock<ILayoutManager>`,
//! whose members do nothing) maps to [`MockLayoutManager`], which records the
//! calls the tests verify.

use crate::layout_test_control::LayoutTestRoot;
use crate::test_support::{test_scope, TestRoot};
use crate::testing::{TestServices, UnitTestApplication};
use ferroui_base::media::text_formatting::testing::TextTestScope;
use crate::{Border, Decorator, StackPanel, TextBlock};
use ferroui_base::layout::{
    HorizontalAlignment, ILayoutManager, Layoutable, LayoutableImpl, LayoutableImplExt, VerticalAlignment,
};
use ferroui_base::{
    ferro_class, ferro_impl_classes, instantiate, FerroObjectImpl, Rect, Ref, Size, StyledElementImpl,
    StyledProperty, Thickness, VisualImpl,
};
use std::cell::{Cell, RefCell};
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::rc::Rc;

/// Declares one test per data row of a parameterized test.
macro_rules! theory {
    ($func:ident: $($name:ident($($arg:expr),* $(,)?));+ $(;)?) => {
        $(
            #[test]
            fn $name() {
                $func($($arg),*)
            }
        )+
    };
}

/// A layout manager whose members do nothing and record the calls.
#[derive(Default)]
struct MockLayoutManager {
    invalidate_measure_calls: RefCell<Vec<Ref<Layoutable>>>,
    invalidate_arrange_calls: RefCell<Vec<Ref<Layoutable>>>,
    layout_updated_adds: Cell<i32>,
    layout_updated_removes: Cell<i32>,
}

impl MockLayoutManager {
    /// C# `target.Invocations.Clear()`.
    fn clear_invocations(&self) {
        self.invalidate_measure_calls.borrow_mut().clear();
        self.invalidate_arrange_calls.borrow_mut().clear();
        self.layout_updated_adds.set(0);
        self.layout_updated_removes.set(0);
    }

    fn invalidate_measure_count(&self, control: &Layoutable) -> usize {
        self.invalidate_measure_calls.borrow().iter().filter(|x| std::ptr::eq(&***x, control)).count()
    }

    fn invalidate_arrange_count(&self, control: &Layoutable) -> usize {
        self.invalidate_arrange_calls.borrow().iter().filter(|x| std::ptr::eq(&***x, control)).count()
    }
}

impl ILayoutManager for MockLayoutManager {
    fn add_layout_updated(&self, _handler: Rc<dyn Fn()>) -> u64 {
        self.layout_updated_adds.set(self.layout_updated_adds.get() + 1);
        0
    }

    fn remove_layout_updated(&self, _token: u64) {
        self.layout_updated_removes.set(self.layout_updated_removes.get() + 1);
    }

    fn invalidate_measure(&self, control: &Layoutable) {
        self.invalidate_measure_calls.borrow_mut().push(control.to_ref());
    }

    fn invalidate_arrange(&self, control: &Layoutable) {
        self.invalidate_arrange_calls.borrow_mut().push(control.to_ref());
    }

    fn execute_layout_pass(&self) {}

    fn execute_initial_layout_pass(&self) {}

    fn register_effective_viewport_listener(&self, _control: &Layoutable) {}

    fn unregister_effective_viewport_listener(&self, _control: &Layoutable) {}

    fn dispose(&self) {}
}

fn margin_is_applied_to_measure_override_size(
    l: f64,
    t: f64,
    r: f64,
    b: f64,
    expected_width: f64,
    expected_height: f64,
) {
    let target = TestLayoutable::new();
    target.set_margin(Thickness::new(l, t, r, b));

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

    assert_eq!(Size::new(expected_width, expected_height), target.measure_size.get());
}

theory!(margin_is_applied_to_measure_override_size:
    margin_is_applied_to_measure_override_size_1(0.0, 0.0, 0.0, 0.0, 100.0, 100.0);
    margin_is_applied_to_measure_override_size_2(10.0, 0.0, 0.0, 0.0, 90.0, 100.0);
    margin_is_applied_to_measure_override_size_3(10.0, 0.0, 5.0, 0.0, 85.0, 100.0);
    margin_is_applied_to_measure_override_size_4(0.0, 10.0, 0.0, 0.0, 100.0, 90.0);
    margin_is_applied_to_measure_override_size_5(0.0, 10.0, 0.0, 5.0, 100.0, 85.0);
    margin_is_applied_to_measure_override_size_6(4.0, 4.0, 6.0, 7.0, 90.0, 89.0));

fn horizontal_alignment_is_applied_to_arrange_override_size(h: HorizontalAlignment, expected_width: f64) {
    let target = TestLayoutable::new();
    target.set_horizontal_alignment(h);

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

    assert_eq!(Size::new(expected_width, 100.0), target.arrange_size.get());
}

theory!(horizontal_alignment_is_applied_to_arrange_override_size:
    horizontal_alignment_is_applied_to_arrange_override_size_1(HorizontalAlignment::Stretch, 100.0);
    horizontal_alignment_is_applied_to_arrange_override_size_2(HorizontalAlignment::Left, 10.0);
    horizontal_alignment_is_applied_to_arrange_override_size_3(HorizontalAlignment::Center, 10.0);
    horizontal_alignment_is_applied_to_arrange_override_size_4(HorizontalAlignment::Right, 10.0));

fn vertical_alignment_is_applied_to_arrange_override_size(v: VerticalAlignment, expected_height: f64) {
    let target = TestLayoutable::new();
    target.set_vertical_alignment(v);

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

    assert_eq!(Size::new(100.0, expected_height), target.arrange_size.get());
}

theory!(vertical_alignment_is_applied_to_arrange_override_size:
    vertical_alignment_is_applied_to_arrange_override_size_1(VerticalAlignment::Stretch, 100.0);
    vertical_alignment_is_applied_to_arrange_override_size_2(VerticalAlignment::Top, 10.0);
    vertical_alignment_is_applied_to_arrange_override_size_3(VerticalAlignment::Center, 10.0);
    vertical_alignment_is_applied_to_arrange_override_size_4(VerticalAlignment::Bottom, 10.0));

fn margin_is_applied_to_arrange_override_size(
    l: f64,
    t: f64,
    r: f64,
    b: f64,
    expected_width: f64,
    expected_height: f64,
) {
    let target = TestLayoutable::new();
    target.set_margin(Thickness::new(l, t, r, b));

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

    assert_eq!(Size::new(expected_width, expected_height), target.arrange_size.get());
}

theory!(margin_is_applied_to_arrange_override_size:
    margin_is_applied_to_arrange_override_size_1(0.0, 0.0, 0.0, 0.0, 100.0, 100.0);
    margin_is_applied_to_arrange_override_size_2(10.0, 0.0, 0.0, 0.0, 90.0, 100.0);
    margin_is_applied_to_arrange_override_size_3(10.0, 0.0, 5.0, 0.0, 85.0, 100.0);
    margin_is_applied_to_arrange_override_size_4(0.0, 10.0, 0.0, 0.0, 100.0, 90.0);
    margin_is_applied_to_arrange_override_size_5(0.0, 10.0, 0.0, 5.0, 100.0, 85.0);
    margin_is_applied_to_arrange_override_size_6(4.0, 4.0, 6.0, 7.0, 90.0, 89.0));

/// `new LayoutTestRoot { Child = control, LayoutManager = target }`.
fn layout_test_root_with_mock(control: &Ref<Decorator>, target: &Rc<MockLayoutManager>) -> Ref<LayoutTestRoot> {
    let root = LayoutTestRoot::new();
    root.set_child(control);
    root.set_layout_manager(target.clone());
    root
}

#[test]
fn only_calls_layout_manager_invalidate_measure_once() {
    let _scope = test_scope();
    let target = Rc::new(MockLayoutManager::default());
    let control = Decorator::new();
    let root = layout_test_root_with_mock(&control, &target);

    root.measure(Size::INFINITY);
    root.arrange(Rect::from_size(root.desired_size()));
    target.clear_invocations();

    control.invalidate_measure();
    control.invalidate_measure();

    assert_eq!(1, target.invalidate_measure_count(&control));
}

#[test]
fn only_calls_layout_manager_invalidate_arrange_once() {
    let _scope = test_scope();
    let target = Rc::new(MockLayoutManager::default());
    let control = Decorator::new();
    let root = layout_test_root_with_mock(&control, &target);

    root.measure(Size::INFINITY);
    root.arrange(Rect::from_size(root.desired_size()));
    target.clear_invocations();

    control.invalidate_arrange();
    control.invalidate_arrange();

    assert_eq!(1, target.invalidate_arrange_count(&control));
}

#[test]
fn attaching_control_to_tree_invalidates_parent_measure() {
    let _scope = test_scope();
    let target = Rc::new(MockLayoutManager::default());
    let control = Decorator::new();
    let root = layout_test_root_with_mock(&control, &target);

    root.measure(Size::INFINITY);
    root.arrange(Rect::from_size(root.desired_size()));
    assert!(control.is_measure_valid());

    root.set_child(None);
    root.measure(Size::INFINITY);
    root.arrange(Rect::from_size(root.desired_size()));

    assert!(!control.is_measure_valid());
    assert!(root.is_measure_valid());

    target.clear_invocations();

    root.set_child(&control);

    assert!(!root.is_measure_valid());
    assert!(!control.is_measure_valid());
    assert_eq!(1, target.invalidate_measure_count(&root));
}

#[test]
fn layout_updated_is_called_at_end_of_layout_pass() {
    let _scope = test_scope();
    let border2 = Border::new();
    let border1 = Border::new();
    border1.set_child(&border2);
    let root = TestRoot::new();
    root.set_child(&border1);
    let raised = Rc::new(Cell::new(0));

    let validate_bounds = {
        let border1 = border1.clone();
        let border2 = border2.clone();
        let raised = raised.clone();
        move || {
            assert_eq!(Rect::new(0.0, 0.0, 100.0, 100.0), border1.bounds());
            assert_eq!(Rect::new(0.0, 0.0, 100.0, 100.0), border2.bounds());
            raised.set(raised.get() + 1);
        }
    };

    let _s1 = root.layout_updated(validate_bounds.clone());
    let _s2 = border1.layout_updated(validate_bounds.clone());
    let _s3 = border2.layout_updated(validate_bounds);

    root.measure(Size::new(100.0, 100.0));
    root.arrange(Rect::new(0.0, 0.0, 100.0, 100.0));

    root.layout_manager().execute_layout_pass();

    assert_eq!(3, raised.get());
    assert_eq!(Rect::new(0.0, 0.0, 100.0, 100.0), border1.bounds());
    assert_eq!(Rect::new(0.0, 0.0, 100.0, 100.0), border2.bounds());
}

/// `new TestRoot { Child = child, LayoutManager = layoutManager }`.
fn test_root_with_mock(child: &Ref<Border>, layout_manager: &Rc<MockLayoutManager>) -> Ref<TestRoot> {
    let root = TestRoot::new();
    root.set_child(child);
    root.set_layout_manager(layout_manager.clone());
    root
}

#[test]
fn layout_updated_subscribes_to_layout_manager() {
    let _scope = test_scope();
    let target = Border::new();
    let child = Border::new();
    child.set_child(&target);
    let layout_manager = Rc::new(MockLayoutManager::default());

    let _root = test_root_with_mock(&child, &layout_manager);

    layout_manager.clear_invocations();
    let handler = target.layout_updated(|| {});

    assert_eq!(1, layout_manager.layout_updated_adds.get());

    layout_manager.clear_invocations();
    handler.dispose();

    assert_eq!(1, layout_manager.layout_updated_removes.get());
}

#[test]
fn layout_manager_layout_updated_is_subscribed_when_attached_to_tree() {
    let _scope = test_scope();
    let border1 = Border::new();
    let layout_manager = Rc::new(MockLayoutManager::default());

    let _root = test_root_with_mock(&border1, &layout_manager);

    let border2 = Border::new();
    let _subscription = border2.layout_updated(|| {});

    layout_manager.clear_invocations();
    border1.set_child(&border2);

    assert_eq!(1, layout_manager.layout_updated_adds.get());
}

#[test]
fn layout_manager_layout_updated_is_unsubscribed_when_detached_from_tree() {
    let _scope = test_scope();
    let border1 = Border::new();
    let layout_manager = Rc::new(MockLayoutManager::default());

    let _root = test_root_with_mock(&border1, &layout_manager);

    let border2 = Border::new();
    let _subscription = border2.layout_updated(|| {});
    border1.set_child(&border2);

    layout_manager.clear_invocations();
    border1.set_child(None);

    assert_eq!(1, layout_manager.layout_updated_removes.get());
}

#[test]
fn layout_manager_layout_updated_should_not_be_subscribed_twice_in_attached_to_visual_tree() {
    let _scope = test_scope();
    let border1 = Border::new();
    let layout_manager = Rc::new(MockLayoutManager::default());

    let _root = test_root_with_mock(&border1, &layout_manager);

    let border2 = Border::new();
    let subscriptions = Rc::new(RefCell::new(Vec::new()));
    let _attached = border2.attached_to_visual_tree({
        let border2 = border2.downgrade();
        let subscriptions = subscriptions.clone();
        move |_| {
            let border2 = border2.upgrade().unwrap();
            subscriptions.borrow_mut().push(border2.layout_updated(|| {}));
        }
    });

    layout_manager.clear_invocations();
    border1.set_child(&border2);

    assert_eq!(1, layout_manager.layout_updated_adds.get());
}

#[test]
fn making_control_invisible_should_invalidate_parent_measure() {
    let _scope = test_scope();
    let child = Border::new();
    child.set_width(100.0);
    let target = StackPanel::new();
    target.children().add(child.clone());

    target.measure(Size::new(f64::INFINITY, f64::INFINITY));
    target.arrange(Rect::from_size(target.desired_size()));

    assert!(target.is_measure_valid());
    assert!(target.is_arrange_valid());
    assert!(child.is_measure_valid());
    assert!(child.is_arrange_valid());

    child.set_is_visible(false);

    assert!(!target.is_measure_valid());
    assert!(!target.is_arrange_valid());
    assert!(child.is_measure_valid());
    assert!(child.is_arrange_valid());
}

#[test]
fn making_control_visible_should_invalidate_own_and_parent_measure() {
    let _scope = test_scope();
    let child = Border::new();
    child.set_width(100.0);
    child.set_is_visible(false);
    let target = StackPanel::new();
    target.children().add(child.clone());

    target.measure(Size::new(f64::INFINITY, f64::INFINITY));
    target.arrange(Rect::from_size(target.desired_size()));

    assert!(target.is_measure_valid());
    assert!(target.is_arrange_valid());
    assert!(child.is_measure_valid());
    assert!(!child.is_arrange_valid());

    child.set_is_visible(true);

    assert!(!target.is_measure_valid());
    assert!(!target.is_arrange_valid());
    assert!(!child.is_measure_valid());
    assert!(!child.is_arrange_valid());
}

#[test]
fn measuring_invisible_control_should_not_invalidate_parent_measure() {
    let _scope = test_scope();
    let child = Border::new();
    child.set_width(100.0);
    let target = StackPanel::new();
    target.children().add(child.clone());

    target.measure(Size::new(f64::INFINITY, f64::INFINITY));
    target.arrange(Rect::from_size(target.desired_size()));

    assert!(target.is_measure_valid());
    assert!(target.is_arrange_valid());
    assert_eq!(Size::new(100.0, 0.0), child.desired_size());

    child.set_is_visible(false);
    assert_eq!(Size::default(), child.desired_size());

    target.measure(Size::new(f64::INFINITY, f64::INFINITY));
    target.arrange(Rect::from_size(target.desired_size()));
    child.measure(Size::new(f64::INFINITY, f64::INFINITY));

    assert!(target.is_measure_valid());
    assert!(target.is_arrange_valid());
    assert_eq!(Size::default(), child.desired_size());
}

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

    let set_should_throw = |properies: &[&'static StyledProperty<f64>], value: f64| {
        for prop in properies {
            let result = catch_unwind(AssertUnwindSafe(|| target.set_value(*prop, value)));
            assert!(result.is_err(), "{} accepted {value}", prop.name());
        }
    };

    set_should_throw(&[Layoutable::width_property(), Layoutable::height_property()], f64::INFINITY);
    set_should_throw(&[Layoutable::width_property(), Layoutable::height_property()], -10.0);

    set_should_throw(&[Layoutable::min_width_property(), Layoutable::min_height_property()], f64::INFINITY);
    set_should_throw(&[Layoutable::min_width_property(), Layoutable::min_height_property()], -10.0);

    set_should_throw(&[Layoutable::max_width_property(), Layoutable::max_height_property()], -10.0);
}

#[test]
fn constraint_and_negative_margin() {
    // The preset of this crate has no font manager or text shaper; the text
    // test scope of the base crate supplies the ones of the upstream preset.
    let _app = UnitTestApplication::start(TestServices::mock_platform_render_interface());
    let _text = TextTestScope::new();

    let text_block = TextBlock::new();
    text_block.set_margin(Thickness::uniform(-10.0));
    text_block.set_text(Some("Lorem ipsum dolor sit amet"));

    let border = Border::new();
    border.set_max_width(100.0);
    border.set_child(&text_block);

    border.measure(Size::new(f64::INFINITY, f64::INFINITY));
    border.arrange(Rect::from_position_size(Default::default(), border.desired_size()));

    assert_eq!(Size::new(100.0, 0.0), border.desired_size());
    assert_eq!(Rect::new(0.0, 0.0, 100.0, 0.0), border.bounds());
    assert_eq!(Size::new(100.0, 0.0), text_block.desired_size());
    assert_eq!(Rect::new(-10.0, -10.0, 120.0, 20.0), text_block.bounds());
}

#[repr(C)]
struct TestLayoutable {
    base: Layoutable,
    arrange_size: Cell<Size>,
    measure_result: Cell<Size>,
    measure_size: Cell<Size>,
}

ferro_class!(TestLayoutable: Layoutable);
ferro_impl_classes!(TestLayoutable: FerroObjectImpl, StyledElementImpl, VisualImpl);

impl LayoutableImpl for TestLayoutable {
    fn measure_override(this: &Self, available_size: Size) -> Size {
        this.measure_size.set(available_size);
        this.measure_result.get()
    }

    fn arrange_override(this: &Self, final_size: Size) -> Size {
        this.arrange_size.set(final_size);
        Self::parent_arrange_override(this, final_size)
    }
}

impl TestLayoutable {
    fn new() -> Ref<Self> {
        instantiate(Self {
            base: Layoutable::construct(),
            arrange_size: Cell::new(Size::default()),
            measure_result: Cell::new(Size::new(10.0, 10.0)),
            measure_size: Cell::new(Size::default()),
        })
    }
}