#![allow(deprecated)]
use std::fmt::{self, Debug};
use std::rc::Rc;
use waft_embedder::Color;
use waft_foundation::{App, Ref, ValueChanged};
use waft_gestures::{DragStartBehavior, GestureTapCallback};
use waft_painting::{EdgeInsetsGeometry, ShapeBorderRef};
use waft_services::MouseCursorRef;
use waft_widgets::{
BuildContext, ExcludeFocus, FocusNodeRef, Icon, IntoWidget, KeyRef, MergeSemantics,
StatelessWidget, WidgetRef, WidgetState, WidgetStatePropertyRef, WidgetStates,
WidgetStatesController,
};
use crate::list_tile::{ListTile, ListTileControlAffinity};
use crate::list_tile_theme::ListTileTheme;
use crate::switch::Switch;
use crate::switch_theme::SwitchTheme;
use crate::theme::Theme;
use crate::theme_data::{MaterialTapTargetSize, VisualDensity};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SwitchListTileType {
Material,
Adaptive,
}
pub struct SwitchListTile {
pub key: Option<KeyRef>,
pub value: bool,
pub on_changed: Option<ValueChanged<bool>>,
#[deprecated(
note = "Use activeThumbColor or activeTrackColor instead. This feature was deprecated after v3.31.0-2.0.pre."
)]
pub active_color: Option<Color>,
pub active_thumb_color: Option<Color>,
pub active_track_color: Option<Color>,
pub inactive_thumb_color: Option<Color>,
pub inactive_track_color: Option<Color>,
pub thumb_color: Option<WidgetStatePropertyRef<Option<Color>>>,
pub track_color: Option<WidgetStatePropertyRef<Option<Color>>>,
pub track_outline_color: Option<WidgetStatePropertyRef<Option<Color>>>,
pub thumb_icon: Option<WidgetStatePropertyRef<Option<Rc<Icon>>>>,
pub material_tap_target_size: Option<MaterialTapTargetSize>,
pub drag_start_behavior: DragStartBehavior,
pub mouse_cursor: Option<MouseCursorRef>,
pub overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>,
pub splash_radius: Option<f64>,
pub focus_node: Option<FocusNodeRef>,
pub states_controller: Option<Ref<WidgetStatesController>>,
pub on_focus_change: Option<ValueChanged<bool>>,
pub autofocus: bool,
pub tile_color: Option<Color>,
pub title: Option<WidgetRef>,
pub subtitle: Option<WidgetRef>,
pub secondary: Option<WidgetRef>,
pub is_three_line: Option<bool>,
pub dense: Option<bool>,
pub content_padding: Option<EdgeInsetsGeometry>,
pub selected: bool,
switch_list_tile_type: SwitchListTileType,
pub control_affinity: Option<ListTileControlAffinity>,
pub shape: Option<ShapeBorderRef>,
pub selected_tile_color: Option<Color>,
pub visual_density: Option<VisualDensity>,
pub enable_feedback: Option<bool>,
pub horizontal_title_gap: Option<f64>,
pub min_vertical_padding: Option<f64>,
pub min_leading_width: Option<f64>,
pub min_tile_height: Option<f64>,
pub hover_color: Option<Color>,
pub apply_cupertino_theme: Option<bool>,
pub internal_add_semantic_for_on_tap: bool,
}
macro_rules! switch_list_tile_setters {
($($field:ident: $ty:ty => $name:literal),* $(,)?) => {
$(
#[doc = concat!("Dart `SwitchListTile(", $name, ":)`.")]
pub fn $field(mut self, $field: impl Into<$ty>) -> SwitchListTile {
self.$field = Some($field.into());
self
}
)*
};
}
macro_rules! switch_list_tile_widget_setters {
($($field:ident => $name:literal),* $(,)?) => {
$(
#[doc = concat!("Dart `SwitchListTile(", $name, ":)`.")]
pub fn $field<K>(mut self, $field: impl IntoWidget<K>) -> SwitchListTile {
self.$field = Some($field.into_widget());
self
}
)*
};
}
impl SwitchListTile {
pub fn new(value: bool, on_changed: Option<ValueChanged<bool>>) -> SwitchListTile {
SwitchListTile::with_type(value, on_changed, SwitchListTileType::Material)
}
pub fn adaptive(value: bool, on_changed: Option<ValueChanged<bool>>) -> SwitchListTile {
SwitchListTile::with_type(value, on_changed, SwitchListTileType::Adaptive)
}
fn with_type(
value: bool,
on_changed: Option<ValueChanged<bool>>,
switch_list_tile_type: SwitchListTileType,
) -> SwitchListTile {
SwitchListTile {
key: None,
value,
on_changed,
active_color: None,
active_thumb_color: None,
active_track_color: None,
inactive_thumb_color: None,
inactive_track_color: None,
thumb_color: None,
track_color: None,
track_outline_color: None,
thumb_icon: None,
material_tap_target_size: None,
drag_start_behavior: DragStartBehavior::Start,
mouse_cursor: None,
overlay_color: None,
splash_radius: None,
focus_node: None,
states_controller: None,
on_focus_change: None,
autofocus: false,
tile_color: None,
title: None,
subtitle: None,
secondary: None,
is_three_line: None,
dense: None,
content_padding: None,
selected: false,
switch_list_tile_type,
control_affinity: None,
shape: None,
selected_tile_color: None,
visual_density: None,
enable_feedback: None,
horizontal_title_gap: None,
min_vertical_padding: None,
min_leading_width: None,
min_tile_height: None,
hover_color: None,
apply_cupertino_theme: None,
internal_add_semantic_for_on_tap: false,
}
}
pub fn key(mut self, key: KeyRef) -> SwitchListTile {
self.key = Some(key);
self
}
#[deprecated(
note = "Use activeThumbColor or activeTrackColor instead. This feature was deprecated after v3.31.0-2.0.pre."
)]
pub fn active_color(mut self, active_color: Color) -> SwitchListTile {
self.active_color = Some(active_color);
self
}
switch_list_tile_setters!(
active_thumb_color: Color => "activeThumbColor",
active_track_color: Color => "activeTrackColor",
inactive_thumb_color: Color => "inactiveThumbColor",
inactive_track_color: Color => "inactiveTrackColor",
thumb_color: WidgetStatePropertyRef<Option<Color>> => "thumbColor",
track_color: WidgetStatePropertyRef<Option<Color>> => "trackColor",
track_outline_color: WidgetStatePropertyRef<Option<Color>> => "trackOutlineColor",
thumb_icon: WidgetStatePropertyRef<Option<Rc<Icon>>> => "thumbIcon",
material_tap_target_size: MaterialTapTargetSize => "materialTapTargetSize",
mouse_cursor: MouseCursorRef => "mouseCursor",
overlay_color: WidgetStatePropertyRef<Option<Color>> => "overlayColor",
splash_radius: f64 => "splashRadius",
focus_node: FocusNodeRef => "focusNode",
states_controller: Ref<WidgetStatesController> => "statesController",
on_focus_change: ValueChanged<bool> => "onFocusChange",
apply_cupertino_theme: bool => "applyCupertinoTheme",
tile_color: Color => "tileColor",
is_three_line: bool => "isThreeLine",
dense: bool => "dense",
content_padding: EdgeInsetsGeometry => "contentPadding",
control_affinity: ListTileControlAffinity => "controlAffinity",
shape: ShapeBorderRef => "shape",
selected_tile_color: Color => "selectedTileColor",
visual_density: VisualDensity => "visualDensity",
enable_feedback: bool => "enableFeedback",
horizontal_title_gap: f64 => "horizontalTitleGap",
min_vertical_padding: f64 => "minVerticalPadding",
min_leading_width: f64 => "minLeadingWidth",
min_tile_height: f64 => "minTileHeight",
hover_color: Color => "hoverColor",
);
switch_list_tile_widget_setters!(
title => "title",
subtitle => "subtitle",
secondary => "secondary",
);
pub fn drag_start_behavior(mut self, drag_start_behavior: DragStartBehavior) -> SwitchListTile {
self.drag_start_behavior = drag_start_behavior;
self
}
pub fn autofocus(mut self, autofocus: bool) -> SwitchListTile {
self.autofocus = autofocus;
self
}
pub fn internal_add_semantic_for_on_tap(
mut self,
internal_add_semantic_for_on_tap: bool,
) -> SwitchListTile {
self.internal_add_semantic_for_on_tap = internal_add_semantic_for_on_tap;
self
}
pub fn selected(mut self, selected: bool) -> SwitchListTile {
self.selected = selected;
self
}
pub fn effective_apply_cupertino_theme(&self) -> Option<bool> {
match self.switch_list_tile_type {
SwitchListTileType::Material => Some(false),
SwitchListTileType::Adaptive => self.apply_cupertino_theme,
}
}
fn build_switch(&self) -> Switch {
let mut switch = match self.switch_list_tile_type {
SwitchListTileType::Adaptive => {
let mut switch = Switch::adaptive(self.value, self.on_changed.clone());
switch.apply_cupertino_theme = self.effective_apply_cupertino_theme();
switch
}
SwitchListTileType::Material => Switch::new(self.value, self.on_changed.clone()),
}
.material_tap_target_size(
self.material_tap_target_size
.unwrap_or(MaterialTapTargetSize::ShrinkWrap),
)
.autofocus(self.autofocus)
.drag_start_behavior(self.drag_start_behavior);
switch.active_color = self.active_color;
switch.active_thumb_color = self.active_thumb_color;
switch.active_track_color = self.active_track_color;
switch.inactive_track_color = self.inactive_track_color;
switch.inactive_thumb_color = self.inactive_thumb_color;
switch.on_focus_change = self.on_focus_change.clone();
switch.thumb_color = self.thumb_color.clone();
switch.track_color = self.track_color.clone();
switch.track_outline_color = self.track_outline_color.clone();
switch.thumb_icon = self.thumb_icon.clone();
switch.mouse_cursor = self.mouse_cursor.clone();
switch.splash_radius = self.splash_radius;
switch.overlay_color = self.overlay_color.clone();
switch
}
fn handle_tap(&self) -> Option<GestureTapCallback> {
let on_changed = Rc::clone(self.on_changed.as_ref()?);
let value = self.value;
Some(GestureTapCallback::new(move |app: &mut App| {
on_changed(app, !value);
}))
}
}
impl Debug for SwitchListTile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SwitchListTile")
.field("value", &if self.value { "on" } else { "off" })
.field(
"on_changed",
&if self.on_changed.is_some() {
"enabled"
} else {
"disabled"
},
)
.finish_non_exhaustive()
}
}
impl StatelessWidget for SwitchListTile {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn debug_check_constructor(&self) {
debug_assert!(self.is_three_line != Some(true) || self.subtitle.is_some());
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
let control = ExcludeFocus::new(self.build_switch()).into_widget();
let list_tile_theme = ListTileTheme::of(app, context);
let effective_control_affinity = self
.control_affinity
.or(list_tile_theme.control_affinity)
.unwrap_or(ListTileControlAffinity::Platform);
let (leading, trailing) = match effective_control_affinity {
ListTileControlAffinity::Leading => (Some(control), self.secondary.clone()),
ListTileControlAffinity::Trailing | ListTileControlAffinity::Platform => {
(self.secondary.clone(), Some(control))
}
};
let theme = Theme::of(app, context);
let switch_theme = SwitchTheme::of(app, context);
let mut states = WidgetStates::new();
if self.selected {
states.insert(WidgetState::Selected);
}
let effective_active_color = self
.active_thumb_color
.or(self.active_color)
.or_else(|| {
switch_theme
.thumb_color
.as_ref()
.and_then(|thumb_color| thumb_color.resolve(&states))
})
.unwrap_or(theme.color_scheme.secondary);
let mut list_tile = ListTile::new()
.selected_color(effective_active_color)
.enabled(self.on_changed.is_some())
.selected(self.selected)
.autofocus(self.autofocus);
list_tile.leading = leading;
list_tile.title = self.title.clone();
list_tile.subtitle = self.subtitle.clone();
list_tile.trailing = trailing;
list_tile.is_three_line = self.is_three_line;
list_tile.dense = self.dense;
list_tile.content_padding = self.content_padding;
list_tile.on_tap = self.handle_tap();
list_tile.selected_tile_color = self.selected_tile_color;
list_tile.shape = self.shape.clone();
list_tile.tile_color = self.tile_color;
list_tile.visual_density = self.visual_density;
list_tile.focus_node = self.focus_node.clone();
list_tile.states_controller = self.states_controller.clone();
list_tile.on_focus_change = self.on_focus_change.clone();
list_tile.enable_feedback = self.enable_feedback;
list_tile.horizontal_title_gap = self.horizontal_title_gap;
list_tile.min_vertical_padding = self.min_vertical_padding;
list_tile.min_leading_width = self.min_leading_width;
list_tile.min_tile_height = self.min_tile_height;
list_tile.hover_color = self.hover_color;
list_tile.internal_add_semantic_for_on_tap = self.internal_add_semantic_for_on_tap;
MergeSemantics::new().child(list_tile).into_widget()
}
}
#[cfg(test)]
mod tests {
use std::cell::{Cell, RefCell};
use waft_embedder::{Offset, PointerChange, Size, TargetPlatform, TextDirection};
use waft_painting::{AlignmentGeometry, EdgeInsets, EdgeInsetsDirectional};
use waft_services::SystemMouseCursors;
use waft_widgets::{
Center, Container, Directionality, FocusHighlightStrategy, GlobalKey, ListView, SizedBox,
StatefulBuilder, Text, WidgetStateProperty, downcast_widget,
};
use waft_widgets::testing::{
TestPointer, WidgetTester, expect, find, finds_one_widget, test_widgets,
};
use super::*;
use crate::checkbox_list_tile::tests::{
IS_THREE_LINE_FRAMES, check_control_is_skipped_by_traversal,
check_default_horizontal_title_gap, check_horizontal_title_gap_0,
check_horizontal_title_gap_with_visual_density, check_min_leading_width_60,
check_min_tile_height, check_min_vertical_padding_80, check_on_focus_change,
expected_three_line_rects, focus_of, has_primary_focus, ink_well_custom_border,
ink_well_enable_feedback, is_three_line_rects, nth, pump_in_material, pump_wrapped,
rect_of, right_rounded_border, text_center,
};
use crate::colors::Colors;
use crate::constants::K_RADIAL_REACTION_ALPHA;
use crate::icons::Icons;
use crate::list_tile::tests::{m2, m3};
use crate::list_tile_theme::ListTileThemeData;
use crate::material::Material;
use crate::scaffold::Scaffold;
use crate::switch::tests::{
c, circles, color_of, drawn, glyph_origins, hover, ignore, press, stroke_of, tap,
};
use crate::switch_theme::SwitchThemeData;
use crate::test_support::PaintInspection;
use crate::theme_data::ThemeData;
use crate::tooltip::tests::pump_app;
fn thumb(tester: &WidgetTester) -> Option<u32> {
drawn(tester).last().and_then(color_of)
}
fn track(tester: &WidgetTester) -> Option<u32> {
drawn(tester).first().and_then(color_of)
}
fn strokes(tester: &WidgetTester) -> Vec<u32> {
drawn(tester)
.iter()
.filter_map(|drawn| stroke_of(drawn).map(|(color, ..)| color))
.collect()
}
fn stateful_tile(
value: &Rc<Cell<bool>>,
configure: impl Fn(SwitchListTile) -> SwitchListTile + 'static,
) -> WidgetRef {
let value = Rc::clone(value);
StatefulBuilder::new(move |_app, _context, set_state| {
let value_now = Rc::clone(&value);
configure(SwitchListTile::new(
value.get(),
Some(Rc::new(move |app: &mut App, new_value: bool| {
let value = Rc::clone(&value_now);
set_state(app, &mut |_app| value.set(new_value));
})),
))
.into_widget()
})
.into_widget()
}
const STATE_FRAMES: [(bool, bool); 4] =
[(false, false), (false, true), (true, false), (true, true)];
fn state_colors(
[
inactive_disabled,
active_disabled,
inactive_enabled,
active_enabled,
]: [Color; 4],
) -> WidgetStatePropertyRef<Option<Color>> {
<dyn WidgetStateProperty<Option<Color>>>::resolve_with(move |states| {
Some(if states.contains(&WidgetState::Disabled) {
if states.contains(&WidgetState::Selected) {
active_disabled
} else {
inactive_disabled
}
} else if states.contains(&WidgetState::Selected) {
active_enabled
} else {
inactive_enabled
})
})
}
fn hovered_colors(hovered: Color) -> WidgetStatePropertyRef<Option<Color>> {
<dyn WidgetStateProperty<Option<Color>>>::resolve_with(move |states| {
Some(if states.contains(&WidgetState::Hovered) {
hovered
} else {
Colors::TRANSPARENT
})
})
}
const STATE_COLORS: [Color; 4] = [
Color::new(0xFF000004),
Color::new(0xFF000002),
Color::new(0xFF000003),
Color::new(0xFF000001),
];
#[test]
fn switch_list_tile_control_test() {
test_widgets(|tester| {
let log = Rc::new(RefCell::new(Vec::new()));
let on_changed: ValueChanged<bool> = {
let log = Rc::clone(&log);
Rc::new(move |_app, value| log.borrow_mut().push(Some(value)))
};
pump_wrapped(tester, |_app| {
SwitchListTile::new(true, Some(on_changed))
.title(Text::new("Hello"))
.into_widget()
});
let hello = text_center(tester, "Hello");
tester.tap_at(hello, 1);
log.borrow_mut().push(None);
tap(tester);
assert_eq!(*log.borrow(), [Some(false), None, Some(false)]);
});
}
#[test]
fn material2_switch_list_tile_has_the_right_colors() {
test_widgets(|tester| {
use crate::switch::tests::{M2_SHADOWS, colors};
let value = Rc::new(Cell::new(false));
let host = stateful_tile(&value, |tile| {
tile.active_color(Colors::RED.shade500())
.active_track_color(Colors::GREEN.shade500())
.inactive_thumb_color(Colors::YELLOW.shade500())
.inactive_track_color(Colors::BLUE.shade500())
});
let root = {
let app = tester.cell.borrow();
waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new().padding(EdgeInsets::all(8.0)),
crate::theme::Theme::new(
m2(&app),
Directionality::new(TextDirection::Ltr, Material::new().child(host)),
),
)
.into_widget()
};
let sequence =
|track: Color, thumb: Color| [&[c(track)][..], &M2_SHADOWS, &[c(thumb)]].concat();
tester.pump_widget(root);
assert_eq!(
colors(&drawn(tester)),
sequence(Colors::BLUE.shade500(), Colors::YELLOW.shade500())
);
tap(tester);
tester.pump_and_settle();
assert_eq!(
colors(&drawn(tester)),
sequence(Colors::GREEN.shade500(), Colors::RED.shade500())
);
});
}
#[test]
fn material3_switch_list_tile_has_the_right_colors() {
test_widgets(|tester| {
let value = Rc::new(Cell::new(false));
let host = stateful_tile(&value, |tile| {
tile.active_color(Colors::RED.shade500())
.active_track_color(Colors::GREEN.shade500())
.inactive_thumb_color(Colors::YELLOW.shade500())
.inactive_track_color(Colors::BLUE.shade500())
});
let root = {
let app = tester.cell.borrow();
waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new().padding(EdgeInsets::all(8.0)),
crate::theme::Theme::new(
m3(&app),
Directionality::new(TextDirection::Ltr, Material::new().child(host)),
),
)
.into_widget()
};
tester.pump_widget(root);
assert_eq!(track(tester), Some(c(Colors::BLUE.shade500())));
assert_eq!(thumb(tester), Some(c(Colors::YELLOW.shade500())));
tap(tester);
tester.pump_and_settle();
assert_eq!(track(tester), Some(c(Colors::GREEN.shade500())));
assert_eq!(thumb(tester), Some(c(Colors::RED.shade500())));
});
}
#[test]
fn switch_list_tile_adaptive_only_uses_material_switch() {
test_widgets(|tester| {
let value = Rc::new(Cell::new(false));
for platform in [
TargetPlatform::IOS,
TargetPlatform::MacOS,
TargetPlatform::Android,
TargetPlatform::Fuchsia,
TargetPlatform::Linux,
TargetPlatform::Windows,
] {
value.set(false);
let host = {
let value = Rc::clone(&value);
StatefulBuilder::new(move |_app, _context, set_state| {
let value_now = Rc::clone(&value);
Material::new()
.child(Center::new().child(SwitchListTile::adaptive(
value.get(),
Some(Rc::new(move |app: &mut App, new_value: bool| {
let value = Rc::clone(&value_now);
set_state(app, &mut |_app| value.set(new_value));
})),
)))
.into_widget()
})
.into_widget()
};
pump_app(
tester,
move |app| ThemeData::builder().platform(platform).build(app),
|_app| host,
);
assert!(
find::by_type::<waft_cupertino::CupertinoSwitch>()
.evaluate(tester)
.is_empty()
);
expect(tester, &find::by_type::<Switch>(), finds_one_widget());
assert!(!value.get(), "on {platform:?}");
let at = rect_of::<SwitchListTile>(tester, 0).center();
tester.tap_at(at, 1);
assert!(value.get(), "on {platform:?}");
}
});
}
#[test]
fn switch_list_tile_content_padding() {
test_widgets(|tester| {
let frame = |tester: &mut WidgetTester, text_direction: TextDirection| {
let theme = m3(&tester.cell.borrow());
tester.pump_widget(crate::list_tile::tests::bare(
theme,
Directionality::new(
text_direction,
Material::new().child(
Container::new()
.alignment(AlignmentGeometry::TOP_LEFT)
.child(
SwitchListTile::new(true, ignore())
.content_padding(EdgeInsetsDirectional::only(
10.0, 30.0, 20.0, 40.0,
))
.secondary(Text::new("L"))
.title(Text::new("title")),
),
),
),
));
};
frame(tester, TextDirection::Ltr);
assert_eq!(tester.get_rect(find::text("L")).left, 10.0); assert_eq!(rect_of::<Switch>(tester, 0).right, 780.0);
frame(tester, TextDirection::Rtl);
assert_eq!(rect_of::<Switch>(tester, 0).left, 20.0); assert_eq!(tester.get_rect(find::text("L")).right, 790.0); });
}
#[test]
fn switch_list_tile_forwards_states_controller_to_list_tile() {
test_widgets(|tester| {
let controller = WidgetStatesController::new(&mut tester.cell.borrow_mut(), None);
pump_in_material(tester, m3, |_app| {
ListView::new()
.children([SwitchListTile::new(true, ignore())
.title(Text::new("Switch"))
.states_controller(controller.clone())
.into_widget()])
.into_widget()
});
let forwarded = tester.widget(nth::<ListTile>(tester, 0), |tile: &ListTile| {
tile.states_controller.clone()
});
assert_eq!(forwarded, Some(controller));
});
}
#[test]
fn switch_list_tile_can_autofocus_unless_disabled() {
test_widgets(|tester| {
let child_key = GlobalKey::new();
for (on_changed, focused) in [(ignore(), true), (None, false)] {
let key = child_key.clone();
pump_in_material(tester, m3, move |_app| {
ListView::new()
.children([SwitchListTile::new(true, on_changed)
.title(Text::new("A").key(key.into()))
.autofocus(true)
.into_widget()])
.into_widget()
});
tester.pump();
let node = focus_of(tester, &child_key);
assert_eq!(has_primary_focus(tester, node), focused);
}
});
}
fn slots(tester: &WidgetTester) -> (Option<WidgetRef>, Option<WidgetRef>) {
tester.widget(nth::<ListTile>(tester, 0), |tile: &ListTile| {
(tile.leading.clone(), tile.trailing.clone())
})
}
fn is_excluded_switch(slot: &Option<WidgetRef>) -> bool {
slot.as_ref()
.and_then(|slot| downcast_widget::<ExcludeFocus>(&**slot))
.is_some_and(|exclude_focus| downcast_widget::<Switch>(&*exclude_focus.child).is_some())
}
fn is_icon(slot: &Option<WidgetRef>) -> bool {
slot.as_ref()
.is_some_and(|slot| downcast_widget::<Icon>(&**slot).is_some())
}
fn info_tile() -> SwitchListTile {
SwitchListTile::new(true, None)
.secondary(Icon::new(Some(Icons::info())))
.title(Text::new("Title"))
}
#[test]
fn switch_list_tile_control_affinity_test() {
test_widgets(|tester| {
pump_in_material(tester, m3, |_app| {
info_tile()
.control_affinity(ListTileControlAffinity::Leading)
.into_widget()
});
let (leading, trailing) = slots(tester);
assert!(is_excluded_switch(&leading));
assert!(is_icon(&trailing));
});
}
#[test]
fn switch_list_tile_control_affinity_default_value_test() {
test_widgets(|tester| {
pump_in_material(tester, m3, |_app| info_tile().into_widget());
let (leading, trailing) = slots(tester);
assert!(is_excluded_switch(&trailing));
assert!(is_icon(&leading));
});
}
#[test]
fn switch_list_tile_respects_shape() {
test_widgets(|tester| {
pump_in_material(tester, m3, |_app| {
SwitchListTile::new(true, None)
.title(Text::new("Title"))
.shape(ShapeBorderRef::new(right_rounded_border()))
.into_widget()
});
let shape: ShapeBorderRef = ShapeBorderRef::new(right_rounded_border());
assert!(ink_well_custom_border(tester).as_deref() == Some(&*shape));
});
}
#[test]
fn switch_list_tile_respects_tile_color() {
test_widgets(|tester| {
let tile_color = Colors::RED.shade500();
pump_wrapped(tester, |_app| {
Center::new()
.child(
SwitchListTile::new(false, None)
.title(Text::new("Title"))
.tile_color(tile_color),
)
.into_widget()
});
waft_rendering::testing::assert_paints(
tester.ink_list().ops(),
&[waft_rendering::testing::PaintCall::rect(Some(tile_color))],
);
});
}
#[test]
fn switch_list_tile_respects_selected_tile_color() {
test_widgets(|tester| {
let selected_tile_color = Colors::GREEN.shade500();
pump_wrapped(tester, |_app| {
Center::new()
.child(
SwitchListTile::new(false, None)
.title(Text::new("Title"))
.selected(true)
.selected_tile_color(selected_tile_color),
)
.into_widget()
});
waft_rendering::testing::assert_paints(
tester.ink_list().ops(),
&[waft_rendering::testing::PaintCall::rect(Some(
selected_tile_color,
))],
);
});
}
#[test]
fn switch_list_tile_selected_item_text_color() {
test_widgets(|tester| {
const ACTIVE_COLOR: Color = Color::new(0xff00ff00);
for _ in 0..2 {
pump_app(
tester,
|app| {
ThemeData::builder()
.switch_theme(SwitchThemeData::new().thumb_color(
<dyn WidgetStateProperty<Option<Color>>>::resolve_with(|_| None),
))
.build(app)
},
|_app| {
Scaffold::new()
.body(
Center::new().child(
SwitchListTile::new(true, ignore())
.active_color(ACTIVE_COLOR)
.selected(true)
.title(Text::new("title")),
),
)
.into_widget()
},
);
assert_eq!(tester.text_color("title"), ACTIVE_COLOR);
}
});
}
#[test]
fn switch_list_tile_respects_visual_density() {
test_widgets(|tester| {
pump_wrapped(tester, |_app| {
Center::new()
.child(
SwitchListTile::new(false, ignore())
.autofocus(true)
.visual_density(VisualDensity::STANDARD),
)
.into_widget()
});
tester.pump_and_settle();
assert_eq!(
rect_of::<SwitchListTile>(tester, 0).size(),
Size::new(800.0, 56.0)
);
});
}
#[test]
fn switch_list_tile_respects_focus_node() {
test_widgets(|tester| {
let child_key = GlobalKey::new();
let key = child_key.clone();
pump_wrapped(tester, move |_app| {
Center::new()
.child(
SwitchListTile::new(false, ignore()).title(Text::new("A").key(key.into())),
)
.into_widget()
});
tester.pump();
let tile_node = focus_of(tester, &child_key);
tile_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump(); let node = focus_of(tester, &child_key);
assert!(has_primary_focus(tester, node));
assert!(has_primary_focus(tester, tile_node));
});
}
#[test]
fn switch_list_tile_on_focus_change_callback() {
test_widgets(|tester| {
check_on_focus_change(tester, |node, on_focus_change| {
SwitchListTile::new(true, ignore())
.focus_node(node)
.on_focus_change(on_focus_change)
.into_widget()
});
});
}
#[test]
fn switch_list_tile_adaptive_on_focus_change_callback() {
test_widgets(|tester| {
check_on_focus_change(tester, |node, on_focus_change| {
SwitchListTile::adaptive(true, ignore())
.focus_node(node)
.on_focus_change(on_focus_change)
.into_widget()
});
});
}
#[test]
fn switch_list_tile_respects_enable_feedback() {
test_widgets(|tester| {
for enable_feedback in [false, true] {
pump_wrapped(tester, |_app| {
Center::new()
.child(
SwitchListTile::new(false, ignore()).enable_feedback(enable_feedback),
)
.into_widget()
});
assert_eq!(ink_well_enable_feedback(tester), enable_feedback);
}
});
}
#[test]
fn switch_list_tile_respects_hover_color() {
test_widgets(|tester| {
let key = GlobalKey::new();
let orange = Colors::ORANGE.shade500();
let tile_key = key.clone();
pump_wrapped(tester, move |_app| {
Center::new()
.child(
SizedBox::new().width(500.0).height(100.0).child(
Material::new().color(Colors::WHITE).child(
SwitchListTile::new(false, ignore())
.key(tile_key.into())
.hover_color(orange)
.title(Text::new("A")),
),
),
)
.into_widget()
});
let mut gesture = TestPointer::mouse(1, 0);
let center = tester.get_center(&key);
tester.add_pointer(&mut gesture, center);
tester.pump();
tester.pump_and_settle();
let materials = tester.all::<crate::material::RenderInkFeatures>();
let mut canvas = waft_embedder::Canvas::new();
materials[1].paint_ink_features(
&mut tester.cell.borrow_mut(),
&mut canvas,
Offset::ZERO,
);
waft_rendering::testing::assert_paints(
canvas.build().ops(),
&[waft_rendering::testing::PaintCall::rect_at(
waft_embedder::Rect::from_ltrb(0.0, 0.0, 500.0, 100.0),
Some(orange),
)],
);
tester.send(&gesture, PointerChange::Remove);
});
}
#[test]
fn material2_switch_list_tile_respects_thumb_color_in_active_enabled_states() {
test_widgets(|tester| {
let thumb_color = state_colors(STATE_COLORS);
for ((enabled, selected), expected) in STATE_FRAMES.into_iter().zip(STATE_COLORS) {
let thumb_color = Rc::clone(&thumb_color);
pump_in_material(tester, m2, move |_app| {
SwitchListTile::new(selected, if enabled { ignore() } else { None })
.thumb_color(thumb_color)
.into_widget()
});
tester.pump_and_settle();
assert_eq!(thumb(tester), Some(c(expected)), "{enabled} {selected}");
}
});
}
#[test]
fn material3_switch_list_tile_respects_thumb_color_in_active_enabled_states() {
test_widgets(|tester| {
let thumb_color = state_colors(STATE_COLORS);
for ((enabled, selected), expected) in STATE_FRAMES.into_iter().zip(STATE_COLORS) {
let thumb_color = Rc::clone(&thumb_color);
pump_in_material(tester, m3, move |_app| {
SwitchListTile::new(selected, if enabled { ignore() } else { None })
.thumb_color(thumb_color)
.into_widget()
});
tester.pump_and_settle();
assert_eq!(thumb(tester), Some(c(expected)), "{enabled} {selected}");
}
});
}
#[test]
fn material2_switch_list_tile_respects_thumb_color_in_hovered_pressed_states() {
test_widgets(|tester| {
tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
const HOVERED_THUMB_COLOR: Color = Color::new(0xFF4CAF50);
const PRESSED_THUMB_COLOR: Color = Color::new(0xFFF44336);
let thumb_color = <dyn WidgetStateProperty<Option<Color>>>::resolve_with(|states| {
Some(if states.contains(&WidgetState::Pressed) {
PRESSED_THUMB_COLOR
} else if states.contains(&WidgetState::Hovered) {
HOVERED_THUMB_COLOR
} else {
Colors::TRANSPARENT
})
});
pump_in_material(tester, m2, |_app| {
SwitchListTile::new(false, ignore())
.thumb_color(thumb_color)
.into_widget()
});
tester.pump_and_settle();
let gesture = hover(tester);
tester.pump_and_settle();
assert_eq!(thumb(tester), Some(c(HOVERED_THUMB_COLOR)));
let touch = press(tester);
tester.pump_and_settle();
assert_eq!(thumb(tester), Some(c(PRESSED_THUMB_COLOR)));
tester.up(&touch);
tester.send(&gesture, PointerChange::Remove);
});
}
#[test]
fn material3_switch_list_tile_respects_thumb_color_in_hovered_pressed_states() {
test_widgets(|tester| {
tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
const HOVERED_THUMB_COLOR: Color = Color::new(0xFF4CAF50);
const PRESSED_THUMB_COLOR: Color = Color::new(0xFFF44336);
let thumb_color = <dyn WidgetStateProperty<Option<Color>>>::resolve_with(|states| {
Some(if states.contains(&WidgetState::Pressed) {
PRESSED_THUMB_COLOR
} else if states.contains(&WidgetState::Hovered) {
HOVERED_THUMB_COLOR
} else {
Colors::TRANSPARENT
})
});
pump_in_material(tester, m3, |_app| {
SwitchListTile::new(false, ignore())
.thumb_color(thumb_color)
.into_widget()
});
tester.pump_and_settle();
let gesture = hover(tester);
tester.pump_and_settle();
assert_eq!(thumb(tester), Some(c(HOVERED_THUMB_COLOR)));
let touch = press(tester);
tester.pump_and_settle();
assert_eq!(thumb(tester), Some(c(PRESSED_THUMB_COLOR)));
tester.up(&touch);
tester.send(&gesture, PointerChange::Remove);
});
}
#[test]
fn switch_list_tile_respects_track_color_in_active_enabled_states() {
test_widgets(|tester| {
let track_color = state_colors(STATE_COLORS);
for (index, ((enabled, selected), expected)) in
STATE_FRAMES.into_iter().zip(STATE_COLORS).enumerate()
{
let track_color = Rc::clone(&track_color);
pump_wrapped(tester, move |_app| {
SwitchListTile::new(selected, if enabled { ignore() } else { None })
.track_color(track_color)
.into_widget()
});
if index > 0 {
tester.pump_and_settle();
}
assert_eq!(track(tester), Some(c(expected)), "{enabled} {selected}");
}
});
}
#[test]
fn switch_list_tile_respects_track_color_in_hovered_states() {
test_widgets(|tester| {
tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
const HOVERED_TRACK_COLOR: Color = Color::new(0xFF4CAF50);
pump_wrapped(tester, |_app| {
SwitchListTile::new(false, ignore())
.track_color(hovered_colors(HOVERED_TRACK_COLOR))
.into_widget()
});
tester.pump_and_settle();
let gesture = hover(tester);
tester.pump_and_settle();
assert_eq!(track(tester), Some(c(HOVERED_TRACK_COLOR)));
tester.send(&gesture, PointerChange::Remove);
});
}
#[test]
fn switch_list_tile_respects_thumb_icon_m3() {
test_widgets(|tester| {
crate::icons::install_material_icon_font(&mut tester.cell.borrow_mut());
let active_icon = Rc::new(Icon::new(Some(Icons::check())));
let inactive_icon = Rc::new(Icon::new(Some(Icons::close())));
let thumb_icon = |active_icon: Option<Rc<Icon>>, inactive_icon: Option<Rc<Icon>>| {
<dyn WidgetStateProperty<Option<Rc<Icon>>>>::resolve_with(move |states| {
if states.contains(&WidgetState::Selected) {
return active_icon.clone();
}
inactive_icon.clone()
})
};
let frame = |tester: &mut WidgetTester,
active: bool,
active_icon: Option<Rc<Icon>>,
inactive_icon: Option<Rc<Icon>>| {
let thumb_icon = thumb_icon(active_icon, inactive_icon);
pump_app(tester, m3, |app| {
let theme = m3(app);
crate::list_tile::tests::bare(
theme,
Material::new()
.child(SwitchListTile::new(active, ignore()).thumb_icon(thumb_icon)),
)
});
tester.pump_and_settle();
let resolved = |states: &WidgetStates| {
tester.widget(nth::<Switch>(tester, 0), |switch: &Switch| {
switch
.thumb_icon
.as_ref()
.and_then(|thumb_icon| thumb_icon.resolve(states))
})
};
let selected: WidgetStates = [WidgetState::Selected].into_iter().collect();
let glyphs: Vec<(f64, f64)> = glyph_origins(&drawn(tester));
(resolved(&selected), resolved(&WidgetStates::new()), glyphs)
};
let inside = |glyphs: &[(f64, f64)], left: f64, top: f64| matches!(glyphs, [(x, y)] if *x >= left && *x < left + 16.0 && *y >= top && *y < top + 16.0);
let same = |icon: Option<Rc<Icon>>, expected: &Rc<Icon>| {
icon.is_some_and(|icon| Rc::ptr_eq(&icon, expected))
};
let (selected, _, glyphs) = frame(tester, true, Some(Rc::clone(&active_icon)), None);
assert!(same(selected, &active_icon));
assert!(inside(&glyphs, 32.0, 12.0), "{glyphs:?}");
let (_, unselected, glyphs) =
frame(tester, false, None, Some(Rc::clone(&inactive_icon)));
assert!(same(unselected, &inactive_icon));
assert!(inside(&glyphs, 12.0, 12.0), "{glyphs:?}");
let (selected, _, glyphs) = frame(tester, false, Some(Rc::clone(&active_icon)), None);
assert!(same(selected, &active_icon));
assert_eq!(glyphs, []);
let (_, unselected, glyphs) =
frame(tester, true, None, Some(Rc::clone(&inactive_icon)));
assert!(same(unselected, &inactive_icon));
assert_eq!(glyphs, []);
let (_, _, glyphs) = frame(tester, false, None, None);
assert_eq!(glyphs, []);
});
}
fn check_material_tap_target_size(
tester: &mut WidgetTester,
theme: fn(&App) -> ThemeData,
width: f64,
) {
for (size, height) in [
(MaterialTapTargetSize::Padded, 48.0),
(MaterialTapTargetSize::ShrinkWrap, 40.0),
] {
pump_in_material(tester, theme, move |_app| {
SwitchListTile::new(false, ignore())
.material_tap_target_size(size)
.into_widget()
});
let given = tester.widget(nth::<Switch>(tester, 0), |switch: &Switch| {
switch.material_tap_target_size
});
assert_eq!(given, Some(size));
assert_eq!(
rect_of::<Switch>(tester, 0).size(),
Size::new(width, height)
);
}
}
#[test]
fn material2_switch_list_tile_respects_material_tap_target_size() {
test_widgets(|tester| check_material_tap_target_size(tester, m2, 59.0));
}
#[test]
fn material3_switch_list_tile_respects_material_tap_target_size() {
test_widgets(|tester| check_material_tap_target_size(tester, m3, 60.0));
}
#[test]
fn material2_switch_list_tile_adaptive_respects_apply_cupertino_theme() {
test_widgets(|tester| {
for platform in [TargetPlatform::IOS, TargetPlatform::MacOS] {
for (apply_cupertino_theme, expected) in
[(true, 0xFF2196F3_u32), (false, 0xFF34C759)]
{
pump_in_material(
tester,
move |app| {
ThemeData::builder()
.use_material3(false)
.platform(platform)
.build(app)
},
move |_app| {
SwitchListTile::adaptive(true, ignore())
.apply_cupertino_theme(apply_cupertino_theme)
.into_widget()
},
);
tester.pump_and_settle();
expect(tester, &find::by_type::<Switch>(), finds_one_widget());
assert!(
drawn(tester)
.iter()
.any(|drawn| color_of(drawn) == Some(expected)),
"{platform:?} {apply_cupertino_theme}: {:?}",
drawn(tester)
);
}
}
});
}
#[test]
fn material3_switch_list_tile_adaptive_respects_apply_cupertino_theme() {
test_widgets(|tester| {
for platform in [TargetPlatform::IOS, TargetPlatform::MacOS] {
for (apply_cupertino_theme, expected) in
[(true, 0xFF6750A4_u32), (false, 0xFF34C759)]
{
pump_in_material(
tester,
move |app| ThemeData::builder().platform(platform).build(app),
move |_app| {
SwitchListTile::adaptive(true, ignore())
.apply_cupertino_theme(apply_cupertino_theme)
.into_widget()
},
);
tester.pump_and_settle();
expect(tester, &find::by_type::<Switch>(), finds_one_widget());
assert!(
drawn(tester)
.iter()
.any(|drawn| color_of(drawn) == Some(expected)),
"{platform:?} {apply_cupertino_theme}: {:?}",
drawn(tester)
);
}
}
});
}
#[test]
fn switch_list_tile_passes_the_value_of_drag_start_behavior_to_switch() {
test_widgets(|tester| {
for drag_start_behavior in [DragStartBehavior::Start, DragStartBehavior::Down] {
pump_wrapped(tester, move |_app| {
SwitchListTile::new(false, ignore())
.drag_start_behavior(drag_start_behavior)
.into_widget()
});
let given = tester.widget(nth::<Switch>(tester, 0), |switch: &Switch| {
switch.drag_start_behavior
});
assert_eq!(given, drag_start_behavior);
}
});
}
#[test]
fn switch_on_switch_list_tile_changes_mouse_cursor_when_hovered() {
test_widgets(|tester| {
pump_wrapped(tester, |_app| {
SwitchListTile::adaptive(false, ignore())
.mouse_cursor(MouseCursorRef::from(SystemMouseCursors::TEXT))
.into_widget()
});
let mut gesture = TestPointer::mouse(1, 0);
let center = rect_of::<Switch>(tester, 0).center();
tester.add_pointer(&mut gesture, center);
tester.pump();
assert_eq!(tester.active_cursor(1), SystemMouseCursors::TEXT.into());
pump_wrapped(tester, |_app| {
SwitchListTile::new(false, ignore())
.mouse_cursor(MouseCursorRef::from(SystemMouseCursors::FORBIDDEN))
.into_widget()
});
let center = rect_of::<Switch>(tester, 0).center();
tester.move_to(&mut gesture, center, false);
assert_eq!(
tester.active_cursor(1),
SystemMouseCursors::FORBIDDEN.into()
);
pump_wrapped(tester, |_app| {
SwitchListTile::new(false, ignore()).into_widget()
});
assert_eq!(tester.active_cursor(1), SystemMouseCursors::CLICK.into());
pump_wrapped(tester, |_app| {
SwitchListTile::new(false, None).into_widget()
});
assert_eq!(tester.active_cursor(1), SystemMouseCursors::BASIC.into());
tester.send(&gesture, PointerChange::Remove);
});
}
#[test]
fn switch_with_splash_radius_set() {
test_widgets(|tester| {
tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
const SPLASH_RADIUS: f64 = 35.0;
pump_wrapped(tester, |_app| {
SwitchListTile::new(false, ignore())
.splash_radius(SPLASH_RADIUS)
.into_widget()
});
tester.pump_and_settle();
let gesture = hover(tester);
tester.pump_and_settle();
assert!(!circles(&drawn(tester), SPLASH_RADIUS).is_empty());
tester.send(&gesture, PointerChange::Remove);
});
}
#[test]
fn the_overlay_color_for_the_thumb_of_the_switch_resolves_in_active_pressed_hovered_states() {
test_widgets(|tester| {
tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
const ACTIVE_THUMB_COLOR: Color = Color::new(0xFF000000);
const INACTIVE_THUMB_COLOR: Color = Color::new(0xFF000010);
const ACTIVE_PRESSED_OVERLAY_COLOR: Color = Color::new(0xFF000001);
const INACTIVE_PRESSED_OVERLAY_COLOR: Color = Color::new(0xFF000002);
const HOVER_OVERLAY_COLOR: Color = Color::new(0xFF000003);
const HOVER_COLOR: Color = Color::new(0xFF000005);
let frame = |tester: &mut WidgetTester, active: bool, use_overlay: bool| {
pump_app(tester, m3, move |_app| {
let mut tile = SwitchListTile::new(active, ignore())
.thumb_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
|states| {
Some(if states.contains(&WidgetState::Selected) {
ACTIVE_THUMB_COLOR
} else {
INACTIVE_THUMB_COLOR
})
},
))
.hover_color(HOVER_COLOR);
if use_overlay {
tile = tile.overlay_color(
<dyn WidgetStateProperty<Option<Color>>>::resolve_with(|states| {
if states.contains(&WidgetState::Pressed) {
if states.contains(&WidgetState::Selected) {
return Some(ACTIVE_PRESSED_OVERLAY_COLOR);
}
return Some(INACTIVE_PRESSED_OVERLAY_COLOR);
}
if states.contains(&WidgetState::Hovered) {
return Some(HOVER_OVERLAY_COLOR);
}
None
}),
);
}
Scaffold::new().body(tile).into_widget()
});
};
for (active, use_overlay, expected) in [
(
false,
false,
INACTIVE_THUMB_COLOR.with_alpha(K_RADIAL_REACTION_ALPHA),
),
(
true,
false,
ACTIVE_THUMB_COLOR.with_alpha(K_RADIAL_REACTION_ALPHA),
),
(false, true, INACTIVE_PRESSED_OVERLAY_COLOR),
(true, true, ACTIVE_PRESSED_OVERLAY_COLOR),
] {
frame(tester, active, use_overlay);
let touch = press(tester);
tester.pump_and_settle();
assert_eq!(
circles(&drawn(tester), crate::constants::K_RADIAL_REACTION_RADIUS),
[c(expected)],
"active {active}, overlay {use_overlay}"
);
tester.up(&touch);
tester.pump_and_settle();
}
frame(tester, false, true);
tester.pump_and_settle();
let gesture = hover(tester);
tester.pump_and_settle();
assert_eq!(
circles(&drawn(tester), crate::constants::K_RADIAL_REACTION_RADIUS),
[c(HOVER_OVERLAY_COLOR)]
);
tester.send(&gesture, PointerChange::Remove);
});
}
#[test]
fn switch_list_tile_respects_track_outline_color_in_active_enabled_states() {
test_widgets(|tester| {
let track_outline_color = state_colors(STATE_COLORS);
for ((enabled, selected), expected) in STATE_FRAMES.into_iter().zip(STATE_COLORS) {
let track_outline_color = Rc::clone(&track_outline_color);
pump_wrapped(tester, move |_app| {
SwitchListTile::new(selected, if enabled { ignore() } else { None })
.track_outline_color(track_outline_color)
.into_widget()
});
tester.pump_and_settle();
assert!(
drawn(tester)
.first()
.is_some_and(|op| stroke_of(op).is_none() && color_of(op).is_some())
);
assert_eq!(strokes(tester), [c(expected)], "{enabled} {selected}");
}
});
}
#[test]
fn switch_list_tile_respects_track_outline_color_in_hovered_state() {
test_widgets(|tester| {
tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
const HOVERED_TRACK_COLOR: Color = Color::new(0xFF4CAF50);
pump_wrapped(tester, |_app| {
SwitchListTile::new(false, ignore())
.track_outline_color(hovered_colors(HOVERED_TRACK_COLOR))
.into_widget()
});
tester.pump_and_settle();
let gesture = hover(tester);
tester.pump_and_settle();
assert_eq!(strokes(tester), [c(HOVERED_TRACK_COLOR)]);
tester.send(&gesture, PointerChange::Remove);
});
}
#[test]
fn switch_list_tile_control_widget_should_not_request_focus_on_traversal() {
test_widgets(|tester| {
check_control_is_skipped_by_traversal(tester, |title| {
let mut tile = SwitchListTile::new(true, ignore());
tile.title = Some(title);
tile.into_widget()
});
});
}
#[test]
fn switch_list_tile_uses_list_tile_theme_control_affinity() {
test_widgets(|tester| {
for (control_affinity, expected) in [
(ListTileControlAffinity::Leading, Offset::new(92.0, 16.0)),
(ListTileControlAffinity::Trailing, Offset::new(16.0, 16.0)),
(ListTileControlAffinity::Platform, Offset::new(16.0, 16.0)),
] {
pump_in_material(tester, m3, |_app| {
ListTileTheme::new(
SwitchListTile::new(true, ignore()).title(Text::new("SwitchListTile")),
)
.data(ListTileThemeData::new().control_affinity(control_affinity))
.into_widget()
});
assert_eq!(
tester.get_rect(find::text("SwitchListTile")).top_left(),
expected
);
}
});
}
fn three_line_tile(is_three_line: Option<bool>, subtitle: &str) -> WidgetRef {
three_line_tile_of(SwitchListTile::new(false, None), is_three_line, subtitle)
}
fn adaptive_three_line_tile(is_three_line: Option<bool>, subtitle: &str) -> WidgetRef {
three_line_tile_of(
SwitchListTile::adaptive(false, None),
is_three_line,
subtitle,
)
}
fn three_line_tile_of(
tile: SwitchListTile,
is_three_line: Option<bool>,
subtitle: &str,
) -> WidgetRef {
let mut tile = tile.title(Text::new("A")).subtitle(Text::new(subtitle));
tile.is_three_line = is_three_line;
tile.into_widget()
}
fn check_is_three_line(
tester: &mut WidgetTester,
platform: TargetPlatform,
tile: fn(Option<bool>, &str) -> WidgetRef,
) {
for (frame, three_line) in IS_THREE_LINE_FRAMES {
assert_eq!(
is_three_line_rects::<Switch, SwitchListTile>(tester, platform, frame, tile),
expected_three_line_rects(
three_line,
800.0 - 60.0 - 24.0,
Size::new(60.0, 40.0),
[130.0, 300.0 + 16.0]
),
"{frame:?}"
);
}
}
#[test]
fn switch_list_tile_is_three_line() {
test_widgets(|tester| {
check_is_three_line(tester, TargetPlatform::Android, three_line_tile)
});
}
#[test]
fn switch_list_tile_adaptive_is_three_line() {
test_widgets(|tester| {
check_is_three_line(tester, TargetPlatform::IOS, adaptive_three_line_tile)
});
}
fn check_active_thumb_color(
tester: &mut WidgetTester,
make: fn(bool, Option<ValueChanged<bool>>) -> SwitchListTile,
) {
let root = {
let app = tester.cell.borrow();
crate::list_tile::tests::bare(
m3(&app),
Material::new().child(
make(true, ignore())
.selected(true)
.active_color(Colors::RED.shade500())
.active_thumb_color(Colors::YELLOW.shade500())
.active_track_color(Colors::GREEN.shade500())
.title(Text::new("title")),
),
)
};
tester.pump_widget(root);
assert_eq!(track(tester), Some(c(Colors::GREEN.shade500())));
assert_eq!(thumb(tester), Some(c(Colors::YELLOW.shade500())));
assert_eq!(tester.text_color("title"), Colors::YELLOW.shade500());
}
#[test]
fn material3_switch_list_tile_active_thumb_color() {
test_widgets(|tester| check_active_thumb_color(tester, SwitchListTile::new));
}
#[test]
fn material3_switch_list_tile_adaptive_active_thumb_color() {
test_widgets(|tester| check_active_thumb_color(tester, SwitchListTile::adaptive));
}
#[test]
fn switch_list_tile_does_not_crash_at_zero_area() {
test_widgets(|tester| {
pump_app(tester, m3, |_app| {
Scaffold::new()
.body(
Center::new()
.child(SizedBox::shrink().child(SwitchListTile::new(true, ignore()))),
)
.into_widget()
});
assert_eq!(rect_of::<SwitchListTile>(tester, 0).size(), Size::ZERO);
});
}
fn leading_title_tile() -> SwitchListTile {
SwitchListTile::new(true, ignore())
.control_affinity(ListTileControlAffinity::Leading)
.title(Text::new("title"))
}
#[test]
fn switch_list_tile_horizontal_title_gap_0() {
test_widgets(|tester| {
check_horizontal_title_gap_0(tester, [76.0, 724.0], |gap| {
let mut tile = leading_title_tile();
tile.horizontal_title_gap = gap;
tile.into_widget()
});
});
}
#[test]
fn switch_list_tile_default_horizontal_title_gap_and_min_leading_width() {
test_widgets(|tester| {
check_default_horizontal_title_gap(tester, [92.0, 708.0], || {
leading_title_tile().into_widget()
});
});
}
#[test]
fn switch_list_tile_horizontal_title_gap_with_visual_density() {
test_widgets(|tester| {
check_horizontal_title_gap_with_visual_density(tester, 78.0, || {
leading_title_tile()
.visual_density(VisualDensity::new(VisualDensity::MINIMUM_DENSITY, 0.0))
.horizontal_title_gap(10.0)
.into_widget()
});
});
}
fn min_vertical_padding_tile(padding: Option<f64>) -> WidgetRef {
let mut tile = SwitchListTile::new(true, ignore()).title(Text::new("title"));
tile.min_vertical_padding = padding;
tile.into_widget()
}
#[test]
fn switch_list_tile_min_vertical_padding_80_material_3() {
test_widgets(|tester| {
check_min_vertical_padding_80(tester, m3, 184.0, min_vertical_padding_tile);
});
}
#[test]
fn switch_list_tile_min_vertical_padding_80_material_2() {
test_widgets(|tester| {
check_min_vertical_padding_80(tester, m2, 176.0, min_vertical_padding_tile);
});
}
#[test]
fn switch_list_tile_min_leading_width_60() {
test_widgets(|tester| {
check_min_leading_width_60(tester, |width| {
let mut tile = leading_title_tile();
tile.min_leading_width = width;
tile.into_widget()
});
});
}
#[test]
fn switch_list_tile_min_tile_height() {
test_widgets(|tester| {
check_min_tile_height(tester, |min_tile_height| {
let mut tile = SwitchListTile::new(true, ignore());
tile.min_tile_height = min_tile_height;
tile.into_widget()
});
});
}
}