waft-material 0.18.1

Material Design widgets for Waft
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
//! Flutter counterpart: material_ui's `adaptive_text_selection_toolbar.dart`.
//!
//! `AdaptiveTextSelectionToolbar.selectable` waits on `SelectableText`.

use std::fmt::{self, Debug};

use waft_cupertino::{
    CupertinoDesktopTextSelectionToolbar, CupertinoDesktopTextSelectionToolbarButton,
    CupertinoTextSelectionToolbar, CupertinoTextSelectionToolbarButton,
};
use waft_embedder::TargetPlatform;
use waft_foundation::{App, Handle, Listener};
use waft_painting::AlignmentGeometry;
use waft_widgets::{
    BuildContext, ClipboardStatus, ContextMenuButtonItem, ContextMenuButtonType, EditableText,
    EditableTextState, IntoWidget, KeyRef, SelectableRegionState, SizedBox, StatelessWidget, Text,
    TextSelectionToolbarAnchors, WidgetRef,
};

use crate::debug::debug_check_has_material_localizations;
use crate::desktop_text_selection_toolbar::DesktopTextSelectionToolbar;
use crate::desktop_text_selection_toolbar_button::DesktopTextSelectionToolbarButton;
use crate::material_localizations::MaterialLocalizations;
use crate::text_selection_toolbar::TextSelectionToolbar;
use crate::text_selection_toolbar_text_button::TextSelectionToolbarTextButton;
use crate::theme::Theme;

/// The default context menu for text selection for the current platform.
///
/// Typically, this widget would be passed to `contextMenuBuilder` in a
/// supported parent widget, such as:
///
/// * `EditableText.contextMenuBuilder`
/// * `TextField.contextMenuBuilder`
/// * `CupertinoTextField.contextMenuBuilder`
/// * `SelectionArea.contextMenuBuilder`
/// * `SelectableText.contextMenuBuilder`
///
/// See also:
///
/// * [`EditableText::get_editable_button_items`], which returns the default
///   [`ContextMenuButtonItem`]s for [`EditableText`] on the platform.
/// * [`AdaptiveTextSelectionToolbar::get_adaptive_buttons`], which builds the button
///   Widgets for the current platform given [`ContextMenuButtonItem`]s.
/// * `CupertinoAdaptiveTextSelectionToolbar`, which does the same thing as this
///   widget but only for Cupertino context menus.
/// * [`TextSelectionToolbar`], the default toolbar for Android.
/// * [`DesktopTextSelectionToolbar`], the default toolbar for desktop platforms
///   other than MacOS.
/// * [`CupertinoTextSelectionToolbar`], the default toolbar for iOS.
/// * [`CupertinoDesktopTextSelectionToolbar`], the default toolbar for MacOS.
pub struct AdaptiveTextSelectionToolbar {
    pub key: Option<KeyRef>,
    /// The [`ContextMenuButtonItem`]s that will be turned into the correct button
    /// widgets for the current platform.
    pub button_items: Option<Vec<ContextMenuButtonItem>>,
    /// The children of the toolbar, typically buttons.
    pub children: Option<Vec<WidgetRef>>,
    /// The location on which to anchor the menu.
    pub anchors: TextSelectionToolbarAnchors,
}

impl Debug for AdaptiveTextSelectionToolbar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AdaptiveTextSelectionToolbar")
            .field("button_items", &self.button_items)
            .finish_non_exhaustive()
    }
}

impl AdaptiveTextSelectionToolbar {
    /// Create an instance of [`AdaptiveTextSelectionToolbar`] with the
    /// given [`children`](Self::children).
    ///
    /// See also:
    ///
    /// * [`AdaptiveTextSelectionToolbar::button_items`], which takes a list of
    ///   [`ContextMenuButtonItem`]s instead of [`children`](Self::children) widgets.
    /// * [`AdaptiveTextSelectionToolbar::editable`], which builds the default
    ///   children for an editable field.
    /// * [`AdaptiveTextSelectionToolbar::editable_text`], which builds the default
    ///   children for an [`EditableText`].
    pub fn new(
        children: Option<Vec<WidgetRef>>,
        anchors: TextSelectionToolbarAnchors,
    ) -> AdaptiveTextSelectionToolbar {
        AdaptiveTextSelectionToolbar {
            key: None,
            button_items: None,
            children,
            anchors,
        }
    }

    /// Dart `AdaptiveTextSelectionToolbar(key:)`.
    pub fn key(mut self, key: KeyRef) -> AdaptiveTextSelectionToolbar {
        self.key = Some(key);
        self
    }

    /// Create an instance of [`AdaptiveTextSelectionToolbar`] whose children will
    /// be built from the given [`button_items`](Self::button_items).
    ///
    /// See also:
    ///
    /// * [`AdaptiveTextSelectionToolbar::new`], which takes the children directly as
    ///   a list of widgets.
    /// * [`AdaptiveTextSelectionToolbar::editable`], which builds the default
    ///   children for an editable field.
    /// * [`AdaptiveTextSelectionToolbar::editable_text`], which builds the default
    ///   children for an [`EditableText`].
    pub fn button_items(
        button_items: Vec<ContextMenuButtonItem>,
        anchors: TextSelectionToolbarAnchors,
    ) -> AdaptiveTextSelectionToolbar {
        AdaptiveTextSelectionToolbar {
            key: None,
            button_items: Some(button_items),
            children: None,
            anchors,
        }
    }

    /// Create an instance of [`AdaptiveTextSelectionToolbar`] with the default
    /// children for an editable field.
    ///
    /// If an on* callback parameter is null, then its corresponding button will
    /// not be built.
    ///
    /// These callbacks are called when their corresponding button is activated
    /// and only then. For example, `on_paste` is called when the user taps the
    /// "Paste" button in the context menu and not when the user pastes with the
    /// keyboard.
    ///
    /// See also:
    ///
    /// * [`AdaptiveTextSelectionToolbar::new`], which takes the children directly as
    ///   a list of widgets.
    /// * [`AdaptiveTextSelectionToolbar::editable_text`], which builds the default
    ///   children for an [`EditableText`].
    /// * [`AdaptiveTextSelectionToolbar::button_items`], which takes a list of
    ///   [`ContextMenuButtonItem`]s instead of [`children`](Self::children) widgets.
    #[allow(clippy::too_many_arguments)]
    pub fn editable(
        app: &App,
        clipboard_status: ClipboardStatus,
        on_copy: Option<Listener>,
        on_cut: Option<Listener>,
        on_paste: Option<Listener>,
        on_select_all: Option<Listener>,
        on_look_up: Option<Listener>,
        on_search_web: Option<Listener>,
        on_share: Option<Listener>,
        on_live_text_input: Option<Listener>,
        anchors: TextSelectionToolbarAnchors,
    ) -> AdaptiveTextSelectionToolbar {
        AdaptiveTextSelectionToolbar {
            key: None,
            children: None,
            button_items: Some(EditableText::get_editable_button_items(
                Some(clipboard_status),
                on_copy,
                on_cut,
                on_paste,
                on_select_all,
                on_look_up,
                on_search_web,
                on_share,
                on_live_text_input,
                app.platform().target_platform(),
            )),
            anchors,
        }
    }

    /// Create an instance of [`AdaptiveTextSelectionToolbar`] with the default
    /// children for an [`EditableText`].
    ///
    /// See also:
    ///
    /// * [`AdaptiveTextSelectionToolbar::new`], which takes the children directly as
    ///   a list of widgets.
    /// * [`AdaptiveTextSelectionToolbar::editable`], which builds the default
    ///   children for an editable field.
    /// * [`AdaptiveTextSelectionToolbar::button_items`], which takes a list of
    ///   [`ContextMenuButtonItem`]s instead of [`children`](Self::children) widgets.
    pub fn editable_text(
        app: &mut App,
        editable_text_state: Handle<EditableTextState>,
    ) -> AdaptiveTextSelectionToolbar {
        AdaptiveTextSelectionToolbar {
            key: None,
            children: None,
            button_items: Some(editable_text_state.context_menu_button_items(app)),
            anchors: editable_text_state.context_menu_anchors(app),
        }
    }

    /// Dart `AdaptiveTextSelectionToolbar.selectableRegion`.
    pub fn selectable_region(
        app: &mut App,
        selectable_region_state: Handle<SelectableRegionState>,
    ) -> AdaptiveTextSelectionToolbar {
        AdaptiveTextSelectionToolbar {
            key: None,
            children: None,
            button_items: Some(selectable_region_state.context_menu_button_items(app)),
            anchors: selectable_region_state.context_menu_anchors(app),
        }
    }

    /// Returns the default button label String for the button of the given
    /// [`ContextMenuButtonType`] on any platform.
    pub fn get_button_label(
        app: &mut App,
        context: BuildContext,
        button_item: &ContextMenuButtonItem,
    ) -> String {
        if let Some(label) = &button_item.label {
            return label.clone();
        }

        match Theme::of(app, context).platform {
            TargetPlatform::IOS | TargetPlatform::MacOS => {
                CupertinoTextSelectionToolbarButton::get_button_label(app, context, button_item)
            }
            TargetPlatform::Android
            | TargetPlatform::Fuchsia
            | TargetPlatform::Linux
            | TargetPlatform::Windows => {
                debug_assert!(debug_check_has_material_localizations(app, context));
                let localizations = <dyn MaterialLocalizations>::of(app, context);
                match button_item.r#type {
                    ContextMenuButtonType::Cut => localizations.cut_button_label(),
                    ContextMenuButtonType::Copy => localizations.copy_button_label(),
                    ContextMenuButtonType::Paste => localizations.paste_button_label(),
                    ContextMenuButtonType::SelectAll => localizations.select_all_button_label(),
                    ContextMenuButtonType::Delete => {
                        localizations.delete_button_tooltip().to_uppercase()
                    }
                    ContextMenuButtonType::LookUp => localizations.look_up_button_label(),
                    ContextMenuButtonType::SearchWeb => localizations.search_web_button_label(),
                    ContextMenuButtonType::Share => localizations.share_button_label(),
                    ContextMenuButtonType::LiveTextInput => localizations.scan_text_button_label(),
                    ContextMenuButtonType::Custom => String::new(),
                }
            }
        }
    }

    /// Returns a List of Widgets generated by turning [`button_items`](Self::button_items) into
    /// the default context menu buttons for the current platform.
    ///
    /// This is useful when building a text selection toolbar with the default
    /// button appearance for the given platform, but where the toolbar and/or the
    /// button actions and labels may be custom.
    ///
    /// See also:
    ///
    /// * `CupertinoAdaptiveTextSelectionToolbar::get_adaptive_buttons`, which is the
    ///   Cupertino equivalent of this class and builds only the Cupertino
    ///   buttons.
    pub fn get_adaptive_buttons(
        app: &mut App,
        context: BuildContext,
        button_items: Vec<ContextMenuButtonItem>,
    ) -> Vec<WidgetRef> {
        match Theme::of(app, context).platform {
            TargetPlatform::IOS => button_items
                .into_iter()
                .map(|button_item| {
                    CupertinoTextSelectionToolbarButton::button_item(button_item).into_widget()
                })
                .collect(),
            TargetPlatform::Fuchsia | TargetPlatform::Android => {
                let mut buttons: Vec<WidgetRef> = Vec::new();
                let total = button_items.len();
                for (i, button_item) in button_items.iter().enumerate() {
                    let mut button = TextSelectionToolbarTextButton::new(
                        Text::new(Self::get_button_label(app, context, button_item)),
                        TextSelectionToolbarTextButton::get_padding(i, total),
                    )
                    .alignment(AlignmentGeometry::CENTER_START);
                    if let Some(on_pressed) = button_item.on_pressed.clone() {
                        button = button.on_pressed(on_pressed);
                    }
                    buttons.push(button.into_widget());
                }
                buttons
            }
            TargetPlatform::Linux | TargetPlatform::Windows => button_items
                .iter()
                .map(|button_item| {
                    let text = Self::get_button_label(app, context, button_item);
                    DesktopTextSelectionToolbarButton::text(
                        app,
                        context,
                        button_item.on_pressed.clone(),
                        text,
                    )
                    .into_widget()
                })
                .collect(),
            TargetPlatform::MacOS => button_items
                .iter()
                .map(|button_item| {
                    CupertinoDesktopTextSelectionToolbarButton::text(
                        button_item.on_pressed.clone(),
                        Self::get_button_label(app, context, button_item),
                    )
                    .into_widget()
                })
                .collect(),
        }
    }
}

impl StatelessWidget for AdaptiveTextSelectionToolbar {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
        // If there aren't any buttons to build, build an empty toolbar.
        let empty = match (&self.children, &self.button_items) {
            (Some(children), _) => children.is_empty(),
            (None, Some(button_items)) => button_items.is_empty(),
            (None, None) => true,
        };
        if empty {
            return SizedBox::shrink().into_widget();
        }

        let result_children = match &self.children {
            Some(children) => children.clone(),
            None => Self::get_adaptive_buttons(
                app,
                context,
                self.button_items
                    .clone()
                    .expect("children or buttonItems is given"),
            ),
        };

        let anchor_below = self
            .anchors
            .secondary_anchor
            .unwrap_or(self.anchors.primary_anchor);
        match Theme::of(app, context).platform {
            TargetPlatform::IOS => CupertinoTextSelectionToolbar::new(
                self.anchors.primary_anchor,
                anchor_below,
                result_children,
            )
            .into_widget(),
            TargetPlatform::Android => TextSelectionToolbar::new(
                self.anchors.primary_anchor,
                anchor_below,
                result_children,
            )
            .into_widget(),
            TargetPlatform::Fuchsia | TargetPlatform::Linux | TargetPlatform::Windows => {
                DesktopTextSelectionToolbar::new(self.anchors.primary_anchor, result_children)
                    .into_widget()
            }
            TargetPlatform::MacOS => CupertinoDesktopTextSelectionToolbar::new(
                self.anchors.primary_anchor,
                result_children,
            )
            .into_widget(),
        }
    }
}

#[cfg(test)]
mod tests {
    use waft_embedder::{Offset, Size};
    use waft_widgets::{
        AnyElement, Builder, Center, Container, CustomPaint, GlobalKey, KeyRef, ListView,
        StandardComponentType,
    };

    use waft_widgets::testing::{
        TargetPlatformVariant, WidgetTester, expect, find, finds_n_widgets, finds_one_widget,
        test_widgets, test_widgets_variant,
    };

    use super::*;
    use crate::scaffold::Scaffold;
    use crate::test_support::material_app;
    use crate::theme_data::ThemeData;

    /// Flutter's `TargetPlatformVariant.all()`.
    const ALL_PLATFORMS: [TargetPlatform; 6] = [
        TargetPlatform::Android,
        TargetPlatform::Fuchsia,
        TargetPlatform::IOS,
        TargetPlatform::Linux,
        TargetPlatform::MacOS,
        TargetPlatform::Windows,
    ];

    /// A tester on `platform` with flutter_test's fonts, so a `Text` measures as in Flutter's
    /// tests.
    /// `MaterialApp(home: Scaffold(body: Center(child: child)))` under the default theme.
    fn pump_centered_in_scaffold(tester: &mut WidgetTester, child: WidgetRef) {
        let root = {
            let app = tester.cell.borrow();
            material_app(
                ThemeData::new(&app),
                Scaffold::new()
                    .body(Center::new().child(child))
                    .into_widget(),
            )
        };
        tester.pump_widget(root);
    }

    fn pressed() -> Option<Listener> {
        Some(Listener::new(|_app| {}))
    }

    #[test]
    fn builds_the_right_toolbar_on_each_platform_including_web_and_shows_button_items() {
        for platform in ALL_PLATFORMS {
            test_widgets_variant(TargetPlatformVariant::only(platform), |tester| {
                const BUTTON_TEXT: &str = "Click me";

                pump_centered_in_scaffold(
                    tester,
                    AdaptiveTextSelectionToolbar::button_items(
                        vec![ContextMenuButtonItem::new(pressed()).label(BUTTON_TEXT)],
                        TextSelectionToolbarAnchors::new(Offset::ZERO),
                    )
                    .into_widget(),
                );

                expect(tester, &find::text(BUTTON_TEXT), finds_one_widget());

                let counts = (
                    find::by_type::<TextSelectionToolbar>()
                        .evaluate(tester)
                        .len(),
                    find::by_type::<CupertinoTextSelectionToolbar>()
                        .evaluate(tester)
                        .len(),
                    find::by_type::<DesktopTextSelectionToolbar>()
                        .evaluate(tester)
                        .len(),
                    find::by_type::<CupertinoDesktopTextSelectionToolbar>()
                        .evaluate(tester)
                        .len(),
                );
                let expected = match platform {
                    TargetPlatform::Android => (1, 0, 0, 0),
                    TargetPlatform::IOS => (0, 1, 0, 0),
                    TargetPlatform::MacOS => (0, 0, 0, 1),
                    TargetPlatform::Fuchsia | TargetPlatform::Linux | TargetPlatform::Windows => {
                        (0, 0, 1, 0)
                    }
                };
                assert_eq!(counts, expected, "{platform:?}");
            });
        }
    }

    #[test]
    fn can_build_children_directly_as_well() {
        test_widgets(|tester| {
            let key = GlobalKey::new();

            pump_centered_in_scaffold(
                tester,
                AdaptiveTextSelectionToolbar::new(
                    Some(vec![Container::new().key(key.clone().into()).into_widget()]),
                    TextSelectionToolbarAnchors::new(Offset::ZERO),
                )
                .into_widget(),
            );

            expect(tester, &find::by_key(key.clone()), finds_one_widget());
        });
    }

    /// Dart's `findLiveTextButton()` on the desktop platforms.
    fn find_live_text_button(tester: &WidgetTester, platform: TargetPlatform) -> usize {
        match platform {
            TargetPlatform::MacOS => find::descendant(
                find::by_type::<CupertinoDesktopTextSelectionToolbarButton>(),
                find::text(""),
            )
            .evaluate(tester)
            .len(),
            // The icon is a painted `CustomPaint` in a `SizedBox` on iOS, where `Text`s are not used.
            TargetPlatform::IOS => cupertino_button_paints(tester, Size::ZERO).len(),
            _ => {
                find::text("").evaluate(tester).len()
                    + find::text("Scan text").evaluate(tester).len()
            }
        }
    }

    #[test]
    fn can_build_for_editable_text_from_raw_parameters() {
        for platform in ALL_PLATFORMS {
            test_widgets_variant(TargetPlatformVariant::only(platform), |tester| {
                let key = GlobalKey::new();
                let toolbar = {
                    let app = tester.cell.borrow();
                    AdaptiveTextSelectionToolbar::editable(
                        &app,
                        ClipboardStatus::Pasteable,
                        pressed(),
                        pressed(),
                        pressed(),
                        pressed(),
                        pressed(),
                        pressed(),
                        pressed(),
                        pressed(),
                        TextSelectionToolbarAnchors::new(Offset::ZERO),
                    )
                    .key(key.clone().into())
                    .into_widget()
                };
                pump_centered_in_scaffold(tester, toolbar);

                expect(tester, &find::by_key(key.clone()), finds_one_widget());

                let one = |tester: &WidgetTester, text: &str| {
                    expect(tester, &find::text(text), finds_one_widget());
                };
                match platform {
                    TargetPlatform::Fuchsia => {
                        expect(
                            tester,
                            &find::by_type::<TextSelectionToolbarTextButton>(),
                            finds_n_widgets(8),
                        );
                        one(tester, "Cut");
                        one(tester, "Copy");
                        one(tester, "Paste");
                        one(tester, "Select all");
                        one(tester, "Look Up");
                        one(tester, "Search Web");
                        one(tester, "Share");
                    }
                    TargetPlatform::Linux | TargetPlatform::Windows => {
                        expect(
                            tester,
                            &find::by_type::<DesktopTextSelectionToolbarButton>(),
                            finds_n_widgets(8),
                        );
                        one(tester, "Cut");
                        one(tester, "Copy");
                        one(tester, "Paste");
                        one(tester, "Select all");
                        one(tester, "Look Up");
                        one(tester, "Search Web");
                        one(tester, "Share");
                        assert_eq!(find_live_text_button(tester, platform), 1);
                    }
                    TargetPlatform::MacOS => {
                        expect(
                            tester,
                            &find::by_type::<CupertinoDesktopTextSelectionToolbarButton>(),
                            finds_n_widgets(8),
                        );
                        one(tester, "Cut");
                        one(tester, "Copy");
                        one(tester, "Paste");
                        one(tester, "Select All");
                        one(tester, "Look Up");
                        one(tester, "Search Web");
                        one(tester, "Share...");
                        assert_eq!(find_live_text_button(tester, platform), 1);
                    }
                    TargetPlatform::Android => {
                        expect(
                            tester,
                            &find::by_type::<TextSelectionToolbarTextButton>(),
                            finds_n_widgets(6),
                        );
                        one(tester, "Cut");
                        one(tester, "Copy");
                        one(tester, "Paste");
                        one(tester, "Share");
                        one(tester, "Select all");
                        one(tester, "Look Up");
                        // Material overflow buttons are not TextSelectionToolbarTextButton.
                        expect(
                            tester,
                            &find::by_key(StandardComponentType::MoreButton.key()),
                            finds_one_widget(),
                        );

                        tap_overflow_button(tester, &StandardComponentType::MoreButton.key());

                        expect(
                            tester,
                            &find::by_type::<TextSelectionToolbarTextButton>(),
                            finds_n_widgets(2),
                        );
                        one(tester, "Search Web");
                        assert_eq!(find_live_text_button(tester, platform), 1);
                        expect(
                            tester,
                            &find::by_key(StandardComponentType::BackButton.key()),
                            finds_one_widget(),
                        );
                    }
                    TargetPlatform::IOS => {
                        expect(
                            tester,
                            &find::by_type::<CupertinoTextSelectionToolbarButton>(),
                            finds_n_widgets(6),
                        );
                        one(tester, "Cut");
                        one(tester, "Copy");
                        one(tester, "Paste");
                        one(tester, "Select All");
                        one(tester, "Look Up");
                        assert_eq!(cupertino_chevron_buttons(tester).len(), 1);

                        tap_cupertino_chevron_button(tester);

                        expect(
                            tester,
                            &find::by_type::<CupertinoTextSelectionToolbarButton>(),
                            finds_n_widgets(4),
                        );
                        assert_eq!(cupertino_chevron_buttons(tester).len(), 1);
                        one(tester, "Search Web");
                        one(tester, "Share...");
                        assert_eq!(find_live_text_button(tester, platform), 1);
                    }
                }
            });
        }
    }

    /// Dart's `tester.tapAt(tester.getCenter(find.byKey(key)))` and `pumpAndSettle`
    /// (`tapMaterialOverflowNextButton`).
    fn tap_overflow_button(tester: &mut WidgetTester, key: &KeyRef) {
        let button = tester.element(find::by_key(key.clone()));
        let center = tester.get_rect(button).center();
        tester.tap_at(center, 1);
        tester.pump_and_settle();
    }

    /// The `CustomPaint`s inside the `CupertinoTextSelectionToolbarButton`s whose size is `size`.
    fn cupertino_button_paints(tester: &WidgetTester, size: Size) -> Vec<AnyElement> {
        find::by_type::<CupertinoTextSelectionToolbarButton>()
            .evaluate(tester)
            .into_iter()
            .flat_map(|button| tester.descendants_of_type::<CustomPaint>(button))
            .filter(|paint| tester.widget(*paint, |paint: &CustomPaint| paint.size == size))
            .collect()
    }

    /// The `CupertinoTextSelectionToolbarButton`s that hold a chevron
    /// (`findCupertinoOverflowNextButton` and `findCupertinoOverflowBackButton`): the chevron
    /// painters are private to waft-cupertino, so the `CustomPaint` of the chevron's size (10)
    /// stands for them. A page shows one of the two.
    fn cupertino_chevron_buttons(tester: &WidgetTester) -> Vec<AnyElement> {
        find::by_type::<CupertinoTextSelectionToolbarButton>()
            .evaluate(tester)
            .into_iter()
            .filter(|button| {
                tester
                    .descendants_of_type::<CustomPaint>(*button)
                    .into_iter()
                    .any(|paint| {
                        tester.widget(paint, |paint: &CustomPaint| {
                            paint.size == Size::square(10.0)
                        })
                    })
            })
            .collect()
    }

    /// Dart's `tapCupertinoOverflowNextButton`.
    fn tap_cupertino_chevron_button(tester: &mut WidgetTester) {
        let button = cupertino_chevron_buttons(tester)[0];
        let center = tester.get_rect(button).center();
        tester.tap_at(center, 1);
        tester.pump_and_settle();
    }

    #[test]
    fn builds_empty_toolbar_when_children_and_button_items_are_null() {
        test_widgets(|_tester| {
            for platform in ALL_PLATFORMS {
                let mut tester = WidgetTester::on(platform);
                let root = {
                    let app = tester.cell.borrow();
                    material_app(
                        ThemeData::new(&app),
                        Center::new()
                            .child(AdaptiveTextSelectionToolbar::new(
                                None,
                                TextSelectionToolbarAnchors::new(Offset::ZERO),
                            ))
                            .into_widget(),
                    )
                };
                tester.pump_widget(root);

                let toolbars = find::by_type::<AdaptiveTextSelectionToolbar>().evaluate(&tester);
                let render = tester.render_box_of(toolbars[0]);
                assert_eq!(
                    render.size(&tester.cell.borrow()),
                    Size::ZERO,
                    "{platform:?}"
                );
            }
        });
    }

    #[test]
    fn get_adaptive_buttons_builds_the_correct_button_widgets_per_platform() {
        for platform in ALL_PLATFORMS {
            test_widgets_variant(TargetPlatformVariant::only(platform), |tester| {
                const BUTTON_TEXT: &str = "Click me";

                pump_centered_in_scaffold(
                    tester,
                    Builder::new(|app, context| {
                        let button_items =
                            vec![ContextMenuButtonItem::new(pressed()).label(BUTTON_TEXT)];
                        ListView::new()
                            .children(AdaptiveTextSelectionToolbar::get_adaptive_buttons(
                                app,
                                context,
                                button_items,
                            ))
                            .into_widget()
                    })
                    .into_widget(),
                );

                expect(tester, &find::text(BUTTON_TEXT), finds_one_widget());

                let counts = (
                    find::by_type::<TextSelectionToolbarTextButton>()
                        .evaluate(tester)
                        .len(),
                    find::by_type::<CupertinoTextSelectionToolbarButton>()
                        .evaluate(tester)
                        .len(),
                    find::by_type::<DesktopTextSelectionToolbarButton>()
                        .evaluate(tester)
                        .len(),
                    find::by_type::<CupertinoDesktopTextSelectionToolbarButton>()
                        .evaluate(tester)
                        .len(),
                );
                let expected = match platform {
                    TargetPlatform::Fuchsia | TargetPlatform::Android => (1, 0, 0, 0),
                    TargetPlatform::IOS => (0, 1, 0, 0),
                    TargetPlatform::MacOS => (0, 0, 0, 1),
                    TargetPlatform::Linux | TargetPlatform::Windows => (0, 0, 1, 0),
                };
                assert_eq!(counts, expected, "{platform:?}");
            });
        }
    }
}