use std::fmt;
use std::rc::Rc;
use std::time::Duration;
use waft_foundation::{App, Handle, Listenable, Listener, Ref, UniqueKey, VoidCallback};
use waft_animation::{
Animation, AnimationBehavior, AnimationController, AnimationRef, AnimationStatus,
AnimationStatusListener, AnyAnimation, Curve, CurvedAnimation, Curves, Interval, Threshold,
};
use waft_embedder::{Brightness, Clip, Color, Radius, TextDirection};
use waft_painting::{
AlignmentGeometry, AnyColor, BorderRadiusGeometry, BorderSide, EdgeInsets, EdgeInsetsGeometry,
PaintingBinding, RoundedRectangleBorder, ShapeBorderRef, TextPainter, TextSpan, TextStyle,
};
use waft_rendering::{HitTestBehavior, MainAxisAlignment};
use waft_scheduler::TickerProvider;
use waft_widgets::{
Align, BuildContext, ClipRect, DefaultTextStyle, DismissDirection, Dismissible, Expanded,
FadeTransition, Hero, Icon, IntoWidget, KeyRef, ListenableBuilder, MediaQuery, Padding, Row,
SafeArea, Semantics, SizedBox, StandardComponentType, State, StateData, StatefulWidget, Text,
WidgetRef, WidgetState, WidgetStateColor, WidgetStateProperty, WidgetStatePropertyRef, Wrap,
debug_check_has_media_query,
};
use crate::color_scheme::ColorScheme;
use crate::colors::Colors;
use crate::generated::snackbar_defaults_m3::SnackbarDefaultsM3;
use crate::icon_button::IconButton;
use crate::icons::Icons;
use crate::material::Material;
use crate::material_localizations::MaterialLocalizations;
use crate::scaffold::ScaffoldMessenger;
use crate::snack_bar_theme::{
SnackBarBehavior, SnackBarTheme, SnackBarThemeData, SnackBarThemeDataGetters,
};
use crate::text_button::TextButton;
use crate::text_button_theme::{TextButtonTheme, TextButtonThemeData};
use crate::theme::Theme;
use crate::theme_data::ThemeData;
const SINGLE_LINE_VERTICAL_PADDING: f64 = 14.0;
const SNACK_BAR_TRANSITION_DURATION: Duration = Duration::from_millis(250);
const SNACK_BAR_DISPLAY_DURATION: Duration = Duration::from_millis(4000);
fn snack_bar_height_curve() -> Rc<dyn Curve> {
Curves::fast_out_slow_in()
}
fn snack_bar_m3_height_curve() -> Rc<dyn Curve> {
Curves::ease_in_out_quart()
}
fn snack_bar_fade_in_curve() -> Rc<dyn Curve> {
Rc::new(Interval::new(0.4, 1.0, Curves::linear()))
}
fn snack_bar_m3_fade_in_curve() -> Rc<dyn Curve> {
Rc::new(Interval::new(0.4, 0.6, Curves::ease_in_circ()))
}
fn snack_bar_fade_out_curve() -> Rc<dyn Curve> {
Rc::new(Interval::new(0.72, 1.0, Curves::fast_out_slow_in()))
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SnackBarClosedReason {
Action,
Dismiss,
Swipe,
Hide,
Remove,
Timeout,
}
#[derive(Clone)]
pub struct SnackBarAction {
pub key: Option<KeyRef>,
pub text_color: Option<AnyColor>,
pub background_color: Option<AnyColor>,
pub disabled_text_color: Option<Color>,
pub disabled_background_color: Option<Color>,
pub label: String,
pub on_pressed: VoidCallback,
}
impl SnackBarAction {
pub fn new(
label: impl Into<String>,
on_pressed: impl Fn(&mut App) + 'static,
) -> SnackBarAction {
SnackBarAction {
key: None,
text_color: None,
background_color: None,
disabled_text_color: None,
disabled_background_color: None,
label: label.into(),
on_pressed: Rc::new(on_pressed),
}
}
pub fn key(mut self, key: KeyRef) -> SnackBarAction {
self.key = Some(key);
self
}
pub fn text_color(mut self, text_color: impl Into<AnyColor>) -> SnackBarAction {
self.text_color = Some(text_color.into());
self
}
pub fn disabled_text_color(mut self, disabled_text_color: Color) -> SnackBarAction {
self.disabled_text_color = Some(disabled_text_color);
self
}
pub fn background_color(mut self, background_color: impl Into<AnyColor>) -> SnackBarAction {
let background_color = background_color.into();
debug_assert!(
!is_widget_state_color(&background_color) || self.disabled_background_color.is_none(),
"disabledBackgroundColor must not be provided when background color is a \
WidgetStateColor"
);
self.background_color = Some(background_color);
self
}
pub fn disabled_background_color(mut self, disabled_background_color: Color) -> SnackBarAction {
debug_assert!(
!self
.background_color
.as_ref()
.is_some_and(is_widget_state_color),
"disabledBackgroundColor must not be provided when background color is a \
WidgetStateColor"
);
self.disabled_background_color = Some(disabled_background_color);
self
}
}
impl fmt::Debug for SnackBarAction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SnackBarAction")
.field("key", &self.key)
.field("label", &self.label)
.finish_non_exhaustive()
}
}
impl StatefulWidget for SnackBarAction {
type State = SnackBarActionState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> SnackBarActionState {
SnackBarActionState {
state: StateData::new(),
have_triggered_action: false,
}
}
}
pub struct SnackBarActionState {
state: StateData<SnackBarAction>,
have_triggered_action: bool,
}
impl SnackBarActionState {
fn handle_pressed(self: Handle<Self>, app: &mut App) {
if app.get(self).have_triggered_action {
return;
}
self.set_state(app, |this| {
this.have_triggered_action = true;
});
let on_pressed = Rc::clone(&self.widget(app).on_pressed);
on_pressed(app);
let context = self.context(app);
ScaffoldMessenger::of(app, context)
.hide_current_snack_bar(app, SnackBarClosedReason::Action);
}
}
impl State for SnackBarActionState {
type Widget = SnackBarAction;
waft_widgets::state_accessors!();
fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
let defaults: Box<dyn SnackBarThemeDataGetters> = if Theme::of(app, context).use_material3 {
Box::new(SnackbarDefaultsM3::new(context))
} else {
Box::new(SnackbarDefaultsM2::new(app, context))
};
let snack_bar_theme = SnackBarTheme::of(app, context);
let widget = self.widget(app).clone();
let resolve_foreground_color = |app: &mut App| -> AnyColor {
if let Some(text_color) = &widget.text_color {
if is_widget_state_color(text_color) {
return text_color.clone();
}
} else if let Some(action_text_color) = &snack_bar_theme.action_text_color {
if is_widget_state_color(action_text_color) {
return action_text_color.clone();
}
} else if let Some(action_text_color) = defaults.action_text_color(app)
&& is_widget_state_color(&action_text_color)
{
return action_text_color;
}
let disabled_text_color = widget
.disabled_text_color
.or(snack_bar_theme.disabled_action_text_color)
.or_else(|| defaults.disabled_action_text_color(app));
let text_color = widget
.text_color
.clone()
.or_else(|| snack_bar_theme.action_text_color.clone())
.or_else(|| defaults.action_text_color(app))
.map(|color| color.color());
WidgetStateColor::resolve_with(move |states| {
if states.contains(&WidgetState::Disabled) {
return disabled_text_color
.expect("the defaults have a disabled action text color");
}
text_color.expect("the defaults have an action text color")
})
.into_any()
};
let resolve_background_color = || -> AnyColor {
if let Some(background_color) = &widget.background_color
&& is_widget_state_color(background_color)
{
return background_color.clone();
}
if let Some(action_background_color) = &snack_bar_theme.action_background_color
&& is_widget_state_color(action_background_color)
{
return action_background_color.clone();
}
let disabled_background_color = widget
.disabled_background_color
.or(snack_bar_theme.disabled_action_background_color)
.unwrap_or(Colors::TRANSPARENT);
let background_color = widget
.background_color
.as_ref()
.or(snack_bar_theme.action_background_color.as_ref())
.map_or(Colors::TRANSPARENT, AnyColor::color);
WidgetStateColor::resolve_with(move |states| {
if states.contains(&WidgetState::Disabled) {
return disabled_background_color;
}
background_color
})
.into_any()
};
let style = TextButton::style_from()
.overlay_color(resolve_foreground_color(app).color())
.build()
.copy_with();
let style = crate::button_style::ButtonStyle {
foreground_color: Some(widget_state_color_property(resolve_foreground_color(app))),
background_color: Some(widget_state_color_property(resolve_background_color())),
..style
};
let label = widget.label.clone();
let on_pressed = if app.get(self).have_triggered_action {
None
} else {
Some(Listener::handle_method(self, Self::handle_pressed))
};
TextButton::new(on_pressed, Some(Text::new(label)))
.style(style)
.into_widget()
}
}
fn is_widget_state_color(color: &AnyColor) -> bool {
color.extension::<WidgetStateColor>().is_some()
}
fn widget_state_color_property(color: AnyColor) -> WidgetStatePropertyRef<Option<Color>> {
<dyn WidgetStateProperty<Option<Color>>>::resolve_with(move |states| {
Some(
color
.extension::<WidgetStateColor>()
.expect("resolved as a WidgetStateColor")
.resolve(states),
)
})
}
#[derive(Clone)]
pub struct SnackBar {
pub key: Option<KeyRef>,
pub content: WidgetRef,
pub background_color: Option<Color>,
pub elevation: Option<f64>,
pub margin: Option<EdgeInsetsGeometry>,
pub padding: Option<EdgeInsetsGeometry>,
pub width: Option<f64>,
pub shape: Option<ShapeBorderRef>,
pub hit_test_behavior: Option<HitTestBehavior>,
pub behavior: Option<SnackBarBehavior>,
pub action: Option<SnackBarAction>,
pub action_overflow_threshold: Option<f64>,
pub show_close_icon: Option<bool>,
pub close_icon_color: Option<AnyColor>,
pub duration: Duration,
pub persist: Option<bool>,
pub animation: Option<AnimationRef<f64>>,
pub on_visible: Option<VoidCallback>,
pub dismiss_direction: Option<DismissDirection>,
pub clip_behavior: Clip,
}
impl SnackBar {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(content: impl IntoWidget<K>) -> SnackBar {
SnackBar {
key: None,
content: content.into_widget(),
background_color: None,
elevation: None,
margin: None,
padding: None,
width: None,
shape: None,
hit_test_behavior: None,
behavior: None,
action: None,
action_overflow_threshold: None,
show_close_icon: None,
close_icon_color: None,
duration: SNACK_BAR_DISPLAY_DURATION,
persist: None,
animation: None,
on_visible: None,
dismiss_direction: None,
clip_behavior: Clip::HardEdge,
}
}
pub fn key(mut self, key: KeyRef) -> SnackBar {
self.key = Some(key);
self
}
pub fn background_color(mut self, background_color: Color) -> SnackBar {
self.background_color = Some(background_color);
self
}
pub fn elevation(mut self, elevation: f64) -> SnackBar {
debug_assert!(elevation >= 0.0);
self.elevation = Some(elevation);
self
}
pub fn margin(mut self, margin: impl Into<EdgeInsetsGeometry>) -> SnackBar {
debug_assert!(
self.width.is_none(),
"Width and margin can not be used together"
);
self.margin = Some(margin.into());
self
}
pub fn padding(mut self, padding: impl Into<EdgeInsetsGeometry>) -> SnackBar {
self.padding = Some(padding.into());
self
}
pub fn width(mut self, width: f64) -> SnackBar {
debug_assert!(
self.margin.is_none(),
"Width and margin can not be used together"
);
self.width = Some(width);
self
}
pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> SnackBar {
self.shape = Some(shape.into());
self
}
pub fn hit_test_behavior(mut self, hit_test_behavior: HitTestBehavior) -> SnackBar {
self.hit_test_behavior = Some(hit_test_behavior);
self
}
pub fn behavior(mut self, behavior: SnackBarBehavior) -> SnackBar {
self.behavior = Some(behavior);
self
}
pub fn action(mut self, action: SnackBarAction) -> SnackBar {
self.action = Some(action);
self
}
pub fn action_overflow_threshold(mut self, action_overflow_threshold: f64) -> SnackBar {
debug_assert!(
(0.0..=1.0).contains(&action_overflow_threshold),
"Action overflow threshold must be between 0 and 1 inclusive"
);
self.action_overflow_threshold = Some(action_overflow_threshold);
self
}
pub fn show_close_icon(mut self, show_close_icon: bool) -> SnackBar {
self.show_close_icon = Some(show_close_icon);
self
}
pub fn close_icon_color(mut self, close_icon_color: impl Into<AnyColor>) -> SnackBar {
self.close_icon_color = Some(close_icon_color.into());
self
}
pub fn duration(mut self, duration: Duration) -> SnackBar {
self.duration = duration;
self
}
pub fn persist(mut self, persist: bool) -> SnackBar {
self.persist = Some(persist);
self
}
pub fn animation(mut self, animation: AnimationRef<f64>) -> SnackBar {
self.animation = Some(animation);
self
}
pub fn on_visible(mut self, on_visible: impl Fn(&mut App) + 'static) -> SnackBar {
self.on_visible = Some(Rc::new(on_visible));
self
}
pub fn dismiss_direction(mut self, dismiss_direction: DismissDirection) -> SnackBar {
self.dismiss_direction = Some(dismiss_direction);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> SnackBar {
self.clip_behavior = clip_behavior;
self
}
pub fn effective_persist(&self) -> bool {
self.persist.unwrap_or(self.action.is_some())
}
pub fn create_animation_controller(
app: &mut App,
vsync: impl TickerProvider,
duration: Option<Duration>,
reverse_duration: Option<Duration>,
) -> Ref<AnimationController> {
AnimationController::new(
app,
None,
Some(duration.unwrap_or(SNACK_BAR_TRANSITION_DURATION)),
reverse_duration,
0.0,
1.0,
AnimationBehavior::Normal,
vsync,
)
}
pub fn with_animation(
&self,
new_animation: AnimationRef<f64>,
fallback_key: Option<KeyRef>,
) -> SnackBar {
SnackBar {
key: self.key.clone().or(fallback_key),
content: Rc::clone(&self.content),
background_color: self.background_color,
elevation: self.elevation,
margin: self.margin,
padding: self.padding,
width: self.width,
shape: self.shape.clone(),
hit_test_behavior: self.hit_test_behavior,
behavior: self.behavior,
action: self.action.clone(),
action_overflow_threshold: self.action_overflow_threshold,
show_close_icon: self.show_close_icon,
close_icon_color: self.close_icon_color.clone(),
duration: self.duration,
persist: Some(self.effective_persist()),
animation: Some(new_animation),
on_visible: self.on_visible.clone(),
dismiss_direction: self.dismiss_direction,
clip_behavior: self.clip_behavior,
}
}
}
impl fmt::Debug for SnackBar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SnackBar")
.field("key", &self.key)
.field("content", &self.content)
.field("behavior", &self.behavior)
.field("action", &self.action)
.field("duration", &self.duration)
.finish_non_exhaustive()
}
}
impl StatefulWidget for SnackBar {
type State = SnackBarState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> SnackBarState {
SnackBarState {
state: StateData::new(),
was_visible: false,
height_animation: None,
fade_in_animation: None,
fade_in_m3_animation: None,
fade_out_animation: None,
height_m3_animation: None,
dismissible_key: UniqueKey::new().into(),
}
}
}
pub struct SnackBarState {
state: StateData<SnackBar>,
was_visible: bool,
height_animation: Option<Ref<CurvedAnimation>>,
fade_in_animation: Option<Ref<CurvedAnimation>>,
fade_in_m3_animation: Option<Ref<CurvedAnimation>>,
fade_out_animation: Option<Ref<CurvedAnimation>>,
height_m3_animation: Option<Ref<CurvedAnimation>>,
dismissible_key: KeyRef,
}
impl SnackBarState {
fn animation(self: Handle<Self>, app: &App) -> AnimationRef<f64> {
self.widget(app)
.animation
.clone()
.expect("a SnackBar is shown with an animation")
}
fn set_animations(self: Handle<Self>, app: &mut App) {
debug_assert!(self.widget(app).animation.is_some());
let animation = self.animation(app);
let height_animation =
CurvedAnimation::new(app, animation.clone(), snack_bar_height_curve(), None);
let fade_in_animation =
CurvedAnimation::new(app, animation.clone(), snack_bar_fade_in_curve(), None);
let fade_in_m3_animation =
CurvedAnimation::new(app, animation.clone(), snack_bar_m3_fade_in_curve(), None);
let fade_out_animation = CurvedAnimation::new(
app,
animation.clone(),
snack_bar_fade_out_curve(),
Some(Rc::new(Threshold::new(0.0))),
);
let height_m3_animation = CurvedAnimation::new(
app,
animation,
snack_bar_m3_height_curve(),
Some(Rc::new(Threshold::new(0.0))),
);
let this = app.get_mut(self);
this.height_animation = Some(height_animation);
this.fade_in_animation = Some(fade_in_animation);
this.fade_in_m3_animation = Some(fade_in_m3_animation);
this.fade_out_animation = Some(fade_out_animation);
this.height_m3_animation = Some(height_m3_animation);
}
fn dispose_animations(self: Handle<Self>, app: &mut App) {
let this = app.get_mut(self);
let animations = [
this.height_animation.take(),
this.fade_in_animation.take(),
this.fade_in_m3_animation.take(),
this.fade_out_animation.take(),
this.height_m3_animation.take(),
];
for animation in animations.into_iter().flatten() {
animation.dispose(app);
}
}
fn on_animation_status_changed(
self: Handle<Self>,
app: &mut App,
animation_status: AnimationStatus,
) {
if animation_status.is_completed() {
if let Some(on_visible) = self.widget(app).on_visible.clone()
&& !app.get(self).was_visible
{
on_visible(app);
}
app.get_mut(self).was_visible = true;
}
}
fn status_listener(self: Handle<Self>) -> AnimationStatusListener {
AnimationStatusListener::handle_method(self, Self::on_animation_status_changed)
}
}
impl State for SnackBarState {
type Widget = SnackBar;
waft_widgets::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
let animation = self.animation(app);
animation.add_status_listener(app, self.status_listener());
self.set_animations(app);
}
fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &SnackBar) {
if self.widget(app).animation != old_widget.animation {
let old_animation = old_widget
.animation
.clone()
.expect("a SnackBar is shown with an animation");
old_animation.remove_status_listener(app, &self.status_listener());
let animation = self.animation(app);
animation.add_status_listener(app, self.status_listener());
self.dispose_animations(app);
self.set_animations(app);
}
}
fn dispose(self: Handle<Self>, app: &mut App) {
let animation = self.animation(app);
animation.remove_status_listener(app, &self.status_listener());
self.dispose_animations(app);
}
fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
debug_assert!(debug_check_has_media_query(app, context));
let accessible_navigation = MediaQuery::accessible_navigation_of(app, context);
debug_assert!(self.widget(app).animation.is_some());
let theme = Theme::of(app, context);
let color_scheme = theme.color_scheme.clone();
let snack_bar_theme = SnackBarTheme::of(app, context);
let is_theme_dark = theme.brightness() == Brightness::Dark;
let button_color = if is_theme_dark {
color_scheme.primary()
} else {
color_scheme.secondary()
};
let defaults: Box<dyn SnackBarThemeDataGetters> = if theme.use_material3 {
Box::new(SnackbarDefaultsM3::new(context))
} else {
Box::new(SnackbarDefaultsM2::new(app, context))
};
let brightness = if is_theme_dark {
Brightness::Light
} else {
Brightness::Dark
};
let effective_theme = if theme.use_material3 {
Rc::clone(&theme)
} else {
Rc::new(
theme
.copy_with()
.color_scheme(inverted_color_scheme(
&color_scheme,
button_color,
defaults.background_color(app),
brightness,
))
.build(),
)
};
let widget = self.widget(app).clone();
let content_text_style = snack_bar_theme
.content_text_style
.clone()
.or_else(|| defaults.content_text_style(app));
let snack_bar_behavior = widget
.behavior
.or(snack_bar_theme.behavior)
.or_else(|| defaults.behavior(app))
.expect("the defaults have a behavior");
let width = widget.width.or(snack_bar_theme.width);
if cfg!(debug_assertions) {
if snack_bar_behavior != SnackBarBehavior::Floating {
let message = |parameter: &str| {
let prefix = format!("{parameter} can only be used with floating behavior.");
if widget.behavior.is_some() {
format!(
"{prefix} SnackBarBehavior.fixed was set in the SnackBar constructor."
)
} else if snack_bar_theme.behavior.is_some() {
format!(
"{prefix} SnackBarBehavior.fixed was set by the inherited \
SnackBarThemeData."
)
} else {
format!("{prefix} SnackBarBehavior.fixed was set by default.")
}
};
assert!(widget.margin.is_none(), "{}", message("Margin"));
assert!(width.is_none(), "{}", message("Width"));
}
}
let show_close_icon = widget
.show_close_icon
.or(snack_bar_theme.show_close_icon)
.or_else(|| defaults.show_close_icon(app))
.expect("the defaults have a show close icon");
let is_floating_snack_bar = snack_bar_behavior == SnackBarBehavior::Floating;
let horizontal_padding = if is_floating_snack_bar { 16.0 } else { 24.0 };
let padding = widget.padding.unwrap_or(EdgeInsetsGeometry::directional(
horizontal_padding,
0.0,
if widget.action.is_some() || show_close_icon {
0.0
} else {
horizontal_padding
},
0.0,
));
let action_horizontal_margin = widget.padding.map_or(horizontal_padding, |padding| {
padding.resolve(Some(TextDirection::Ltr)).right
}) / 2.0;
let icon_horizontal_margin = widget.padding.map_or(horizontal_padding, |padding| {
padding.resolve(Some(TextDirection::Ltr)).right
}) / 12.0;
let icon_button = if show_close_icon {
let close_icon_color = widget
.close_icon_color
.clone()
.or(snack_bar_theme.close_icon_color.clone())
.or_else(|| defaults.close_icon_color(app));
let tooltip = <dyn MaterialLocalizations>::of(app, context).close_button_tooltip();
let mut icon_button = IconButton::new(
Some(Listener::new(move |app| {
ScaffoldMessenger::of(app, context)
.hide_current_snack_bar(app, SnackBarClosedReason::Dismiss);
})),
Icon::new(Some(Icons::close())),
)
.key(StandardComponentType::CloseButton.key())
.icon_size(24.0)
.tooltip(tooltip);
if let Some(close_icon_color) = close_icon_color {
icon_button = icon_button.color(close_icon_color.color());
}
Some(icon_button)
} else {
None
};
let mut action_text_span = TextSpan::new().text(
widget
.action
.as_ref()
.map(|action| action.label.clone())
.unwrap_or_default(),
);
if let Some(label_large) = Theme::of(app, context).text_theme.label_large.clone() {
action_text_span = action_text_span.style(label_large);
}
let mut action_text_painter = TextPainter::new();
action_text_painter.set_text(Some(action_text_span.into_span()));
action_text_painter.set_max_lines(Some(1));
action_text_painter.set_text_direction(Some(TextDirection::Ltr));
action_text_painter.set_text_scaler(MediaQuery::text_scaler_of(app, context));
let fonts = PaintingBinding::instance(app).fonts(app);
action_text_painter.layout(app.get_mut(fonts), 0.0, f64::INFINITY);
let action_and_icon_width = action_text_painter.size().width()
+ if widget.action.is_some() {
action_horizontal_margin
} else {
0.0
}
+ if show_close_icon {
icon_button
.as_ref()
.and_then(|icon_button| icon_button.icon_size)
.unwrap_or(0.0 + icon_horizontal_margin)
} else {
0.0
};
action_text_painter.dispose();
let margin: EdgeInsets = widget
.margin
.map(|margin| margin.resolve(Some(TextDirection::Ltr)))
.or(snack_bar_theme.inset_padding)
.or_else(|| defaults.inset_padding(app))
.expect("the defaults have an inset padding");
let snack_bar_width = widget
.width
.unwrap_or_else(|| MediaQuery::width_of(app, context) - (margin.left + margin.right));
let action_overflow_threshold = widget
.action_overflow_threshold
.or(snack_bar_theme.action_overflow_threshold)
.or_else(|| defaults.action_overflow_threshold(app))
.expect("the defaults have an action overflow threshold");
let will_overflow_action =
action_and_icon_width / snack_bar_width > action_overflow_threshold;
let mut maybe_action_and_icon: Vec<WidgetRef> = Vec::new();
if let Some(action) = &widget.action {
maybe_action_and_icon.push(
Padding::new(EdgeInsetsGeometry::symmetric(0.0, action_horizontal_margin))
.child(TextButtonTheme::new(
TextButtonThemeData::new().style(
TextButton::style_from()
.foreground_color(button_color)
.padding(EdgeInsetsGeometry::symmetric(0.0, horizontal_padding))
.build(),
),
action.clone(),
))
.into_widget(),
);
}
if let Some(icon_button) = icon_button {
maybe_action_and_icon.push(
Padding::new(EdgeInsetsGeometry::symmetric(0.0, icon_horizontal_margin))
.child(icon_button)
.into_widget(),
);
}
let mut row_children: Vec<WidgetRef> = vec![
Expanded::new(
Padding::new(if widget.padding.is_none() {
EdgeInsetsGeometry::symmetric(SINGLE_LINE_VERTICAL_PADDING, 0.0)
} else {
EdgeInsetsGeometry::ZERO
})
.child(DefaultTextStyle::new(
content_text_style.expect("the defaults have a content text style"),
Rc::clone(&widget.content),
)),
)
.into_widget(),
];
if !will_overflow_action {
row_children.extend(maybe_action_and_icon.iter().cloned());
}
if will_overflow_action && widget.padding.is_none() {
row_children.push(SizedBox::new().width(horizontal_padding).into_widget());
}
let mut wrap_children: Vec<WidgetRef> =
vec![Row::new().children(row_children).into_widget()];
if will_overflow_action {
wrap_children.push(
Padding::new(EdgeInsetsGeometry::only(
0.0,
0.0,
0.0,
SINGLE_LINE_VERTICAL_PADDING,
))
.child(
Row::new()
.main_axis_alignment(MainAxisAlignment::End)
.children(maybe_action_and_icon),
)
.into_widget(),
);
}
let mut snack_bar: WidgetRef = Padding::new(padding)
.child(Wrap::new().children(wrap_children))
.into_widget();
if !is_floating_snack_bar {
snack_bar = SafeArea::new(snack_bar).top(false).into_widget();
}
let elevation = widget
.elevation
.or(snack_bar_theme.elevation)
.or_else(|| defaults.elevation(app))
.expect("the defaults have an elevation");
let background_color = widget
.background_color
.or(snack_bar_theme.background_color)
.or_else(|| defaults.background_color(app))
.expect("the defaults have a background color");
let shape = widget
.shape
.clone()
.or(snack_bar_theme.shape.clone())
.or_else(|| {
if is_floating_snack_bar {
defaults.shape(app)
} else {
None
}
});
let dismiss_direction = widget
.dismiss_direction
.or(snack_bar_theme.dismiss_direction)
.unwrap_or(DismissDirection::Down);
let themed_child = if accessible_navigation || theme.use_material3 {
snack_bar
} else {
let fade_out_animation = app
.get(self)
.fade_out_animation
.clone()
.expect("set by set_animations");
FadeTransition::new(fade_out_animation.erase())
.child(snack_bar)
.into_widget()
};
let mut material = Material::new()
.elevation(elevation)
.color(background_color)
.clip_behavior(widget.clip_behavior);
if let Some(shape) = shape {
material = material.shape(shape);
}
snack_bar = material
.child(Theme::new(effective_theme, themed_child))
.into_widget();
if is_floating_snack_bar {
if let Some(width) = width {
snack_bar = Padding::new(EdgeInsetsGeometry::only(
0.0,
margin.top,
0.0,
margin.bottom,
))
.child(SizedBox::new().width(width).child(snack_bar))
.into_widget();
} else {
snack_bar = Padding::new(EdgeInsetsGeometry::Insets(margin))
.child(snack_bar)
.into_widget();
}
snack_bar = SafeArea::new(snack_bar)
.top(false)
.bottom(false)
.into_widget();
}
let dismissible_key = app.get(self).dismissible_key.clone();
let dismissible = Dismissible::new(dismissible_key, snack_bar)
.direction(dismiss_direction)
.resize_duration(None)
.behavior(widget.hit_test_behavior.unwrap_or(
if widget.margin.is_some() || snack_bar_theme.inset_padding.is_some() {
HitTestBehavior::DeferToChild
} else {
HitTestBehavior::Opaque
},
))
.on_dismissed(move |app, _direction| {
ScaffoldMessenger::of(app, context)
.remove_current_snack_bar(app, SnackBarClosedReason::Swipe);
});
let snack_bar = Semantics::new()
.container(true)
.live_region(true)
.on_dismiss(Rc::new(move |app: &mut App| {
ScaffoldMessenger::of(app, context)
.remove_current_snack_bar(app, SnackBarClosedReason::Dismiss);
}))
.child(dismissible)
.into_widget();
let snack_bar_transition: WidgetRef = if accessible_navigation {
snack_bar
} else if is_floating_snack_bar && !theme.use_material3 {
let fade_in_animation = app
.get(self)
.fade_in_animation
.clone()
.expect("set by set_animations");
FadeTransition::new(fade_in_animation.erase())
.child(snack_bar)
.into_widget()
} else if is_floating_snack_bar && theme.use_material3 {
let this = app.get(self);
let fade_in_m3_animation = this
.fade_in_m3_animation
.clone()
.expect("set by set_animations");
let height_m3_animation = this
.height_m3_animation
.clone()
.expect("set by set_animations");
FadeTransition::new(fade_in_m3_animation.erase())
.child(
height_factor_builder(height_m3_animation, AlignmentGeometry::BOTTOM_LEFT)
.child(snack_bar),
)
.into_widget()
} else {
let height_animation = app
.get(self)
.height_animation
.clone()
.expect("set by set_animations");
height_factor_builder(height_animation, AlignmentGeometry::TOP_START)
.child(snack_bar)
.into_widget()
};
Hero::new(
Rc::new(format!("<SnackBar Hero tag - {:?}>", widget.content)),
ClipRect::new()
.clip_behavior(widget.clip_behavior)
.child(snack_bar_transition),
)
.transition_on_user_gestures(true)
.into_widget()
}
}
fn height_factor_builder(
animation: Ref<CurvedAnimation>,
alignment: AlignmentGeometry,
) -> ListenableBuilder {
let listenable: Rc<dyn Listenable> = Rc::new(animation.erase::<AnyAnimation<f64>>());
ListenableBuilder::new(listenable, move |app, _context, child| {
let mut align = Align::new()
.alignment(alignment)
.height_factor(animation.value(app));
if let Some(child) = child {
align = align.child(Rc::clone(child));
}
align.into_widget()
})
}
#[allow(deprecated)]
fn inverted_color_scheme(
color_scheme: &ColorScheme,
button_color: Color,
background: Option<Color>,
brightness: Brightness,
) -> ColorScheme {
let mut inverted = ColorScheme::new(
brightness,
color_scheme.on_primary(),
color_scheme.primary(),
button_color,
color_scheme.secondary(),
color_scheme.on_error(),
color_scheme.error(),
color_scheme.on_surface(),
color_scheme.surface(),
)
.with_on_background(color_scheme.background());
if let Some(background) = background {
inverted = inverted.with_background(background);
}
inverted
}
#[allow(deprecated)]
struct SnackbarDefaultsM2 {
snack_bar_theme_data: SnackBarThemeData,
theme: Rc<ThemeData>,
}
impl SnackbarDefaultsM2 {
fn new(app: &mut App, context: BuildContext) -> SnackbarDefaultsM2 {
SnackbarDefaultsM2 {
snack_bar_theme_data: SnackBarThemeData::new().elevation(6.0),
theme: Theme::of(app, context),
}
}
fn colors(&self) -> &ColorScheme {
&self.theme.color_scheme
}
fn is_light(&self) -> bool {
self.theme.brightness() == Brightness::Light
}
}
#[allow(deprecated)]
impl SnackBarThemeDataGetters for SnackbarDefaultsM2 {
fn snack_bar_theme_data(&self) -> &SnackBarThemeData {
&self.snack_bar_theme_data
}
fn background_color(&self, _app: &mut App) -> Option<Color> {
let colors = self.colors();
Some(if self.is_light() {
Color::alpha_blend(colors.on_surface().with_opacity(0.80), colors.surface())
} else {
colors.on_surface()
})
}
fn content_text_style(&self, app: &mut App) -> Option<TextStyle> {
ThemeData::builder()
.use_material3(self.theme.use_material3)
.brightness(if self.is_light() {
Brightness::Dark
} else {
Brightness::Light
})
.build(app)
.text_theme
.title_medium
}
fn behavior(&self, _app: &mut App) -> Option<SnackBarBehavior> {
Some(SnackBarBehavior::Fixed)
}
fn action_text_color(&self, _app: &mut App) -> Option<AnyColor> {
Some(self.colors().secondary().into())
}
fn disabled_action_text_color(&self, _app: &mut App) -> Option<Color> {
Some(
self.colors()
.on_surface()
.with_opacity(if self.is_light() { 0.38 } else { 0.3 }),
)
}
fn shape(&self, _app: &mut App) -> Option<ShapeBorderRef> {
Some(ShapeBorderRef::new(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::all(Radius::circular(4.0)),
)))
}
fn inset_padding(&self, _app: &mut App) -> Option<EdgeInsets> {
Some(EdgeInsets::from_ltrb(15.0, 5.0, 15.0, 10.0))
}
fn show_close_icon(&self, _app: &mut App) -> Option<bool> {
Some(false)
}
fn close_icon_color(&self, _app: &mut App) -> Option<AnyColor> {
Some(self.colors().on_surface().into())
}
fn action_overflow_threshold(&self, _app: &mut App) -> Option<f64> {
Some(0.25)
}
}
#[cfg(test)]
mod tests {
use std::cell::{Cell, RefCell};
use waft_embedder::{Offset, Rect, Size};
use waft_widgets::{Builder, Directionality, GestureDetector, GlobalKey, SizedBox};
use waft_widgets::testing::{
WidgetTester, assert_f32_equals, expect, find, finds_n_widgets, finds_nothing,
finds_one_widget, test_widgets,
};
use super::*;
use crate::scaffold::{Scaffold, ScaffoldFeatureController};
use crate::test_support::material_app;
fn m3(app: &App) -> ThemeData {
ThemeData::builder().build(app)
}
fn m2(app: &App) -> ThemeData {
ThemeData::builder().use_material3(false).build(app)
}
fn pump_app(tester: &mut WidgetTester, theme: impl FnOnce(&App) -> ThemeData, home: WidgetRef) {
let theme = theme(&tester.cell.borrow());
tester.pump_widget(material_app(theme, home));
}
fn tap_target_scaffold(
tap_target: &GlobalKey,
show: impl Fn(&mut App, BuildContext) + 'static,
) -> Scaffold {
let tap_target = tap_target.clone();
let show = Rc::new(show);
Scaffold::new().body(Builder::new(move |_app, context| {
let show = Rc::clone(&show);
GestureDetector::new()
.key(tap_target.clone().into())
.on_tap(Listener::new(move |app| show(app, context)))
.behavior(HitTestBehavior::Opaque)
.child(SizedBox::new().height(100.0).width(100.0))
.into_widget()
}))
}
type SnackBarController = ScaffoldFeatureController<SnackBar, SnackBarClosedReason>;
fn show(app: &mut App, context: BuildContext, snack_bar: SnackBar) -> Rc<SnackBarController> {
ScaffoldMessenger::of(app, context).show_snack_bar(app, snack_bar, None)
}
fn tap_key(tester: &mut WidgetTester, key: &GlobalKey) {
let center = tester.get_center(key);
tester.tap_at(center, 1);
}
fn drag_text(tester: &mut WidgetTester, text: &str, offset: Offset) {
let center = tester.get_rect(find::text(text)).center();
tester.drag_from(center, offset, 1);
}
fn ms(millis: u64) -> Duration {
Duration::from_millis(millis)
}
fn x_scaffold(show: impl Fn(&mut App, BuildContext) + 'static) -> Scaffold {
let show = Rc::new(show);
Scaffold::new().body(Builder::new(move |_app, context| {
let show = Rc::clone(&show);
GestureDetector::new()
.on_tap(Listener::new(move |app| show(app, context)))
.child(Text::new("X"))
.into_widget()
}))
}
fn tap_x_and_settle_entrance(tester: &mut WidgetTester) {
tester.tap(find::text("X"));
tester.pump(); tester.pump_by(ms(750));
}
fn snack_bar_material(tester: &WidgetTester) -> waft_widgets::AnyElement {
let snack_bar = find::by_type::<SnackBar>().evaluate(tester)[0];
find::by_type::<Material>()
.evaluate(tester)
.into_iter()
.find(|material| tester.ancestors(*material).contains(&snack_bar))
.expect("a SnackBar builds a Material")
}
fn snack_bar_material_rect(tester: &WidgetTester) -> Rect {
tester.get_rect(snack_bar_material(tester))
}
fn snack_bar_color(tester: &WidgetTester) -> Color {
use waft_rendering::{RenderPhysicalModel, RenderPhysicalModelBase, RenderPhysicalShape};
let mut render = Some(tester.render_box_of(snack_bar_material(tester)).as_object());
let app = tester.cell.borrow();
while let Some(node) = render {
if let Some(model) = node.downcast::<RenderPhysicalModel>(&app) {
return model.color(&app);
}
if let Some(shape) = node.downcast::<RenderPhysicalShape>(&app) {
return shape.color(&app);
}
let mut child = None;
node.visit_children(&app, &mut |next| child = child.or(Some(next)));
render = child;
}
panic!("a Material paints with a physical model");
}
#[test]
fn a_snack_bar_shows_on_tap_and_leaves_after_its_duration() {
test_widgets(|tester| {
const HELLO_SNACK_BAR: &str = "Hello SnackBar";
let tap_target = GlobalKey::new();
let home = tap_target_scaffold(&tap_target, |app, context| {
show(
app,
context,
SnackBar::new(Text::new(HELLO_SNACK_BAR)).duration(Duration::from_secs(2)),
);
});
pump_app(tester, m3, home.into_widget());
expect(tester, &find::text(HELLO_SNACK_BAR), finds_nothing());
tap_key(tester, &tap_target);
expect(tester, &find::text(HELLO_SNACK_BAR), finds_nothing());
tester.pump(); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump(); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); tester.pump(); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget()); tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_nothing());
});
}
#[test]
fn a_second_snack_bar_waits_for_the_first_to_leave() {
test_widgets(|tester| {
let snack_bar_count = Rc::new(Cell::new(0));
let tap_target = GlobalKey::new();
let home = tap_target_scaffold(&tap_target, {
let snack_bar_count = Rc::clone(&snack_bar_count);
move |app, context| {
snack_bar_count.set(snack_bar_count.get() + 1);
show(
app,
context,
SnackBar::new(Text::new(format!("bar{}", snack_bar_count.get())))
.duration(Duration::from_secs(2)),
);
}
});
pump_app(tester, m3, home.into_widget());
let expect_counts = |tester: &WidgetTester, bar1: usize, bar2: usize| {
expect(tester, &find::text("bar1"), finds_n_widgets(bar1));
expect(tester, &find::text("bar2"), finds_n_widgets(bar2));
};
expect_counts(tester, 0, 0);
tap_key(tester, &tap_target); tap_key(tester, &tap_target); expect_counts(tester, 0, 0);
tester.pump(); expect_counts(tester, 1, 0);
tester.pump(); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); tester.pump(); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); expect_counts(tester, 0, 1);
tester.pump(); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); tester.pump(); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); expect_counts(tester, 0, 0);
});
}
#[test]
fn closing_a_controller_hides_its_snack_bar_before_its_timeout() {
test_widgets(|tester| {
let snack_bar_count = Rc::new(Cell::new(0));
let time = Rc::new(Cell::new(0));
let last_controller: Rc<RefCell<Option<Rc<SnackBarController>>>> =
Rc::new(RefCell::new(None));
let tap_target = GlobalKey::new();
let home = tap_target_scaffold(&tap_target, {
let snack_bar_count = Rc::clone(&snack_bar_count);
let time = Rc::clone(&time);
let last_controller = Rc::clone(&last_controller);
move |app, context| {
snack_bar_count.set(snack_bar_count.get() + 1);
let controller = show(
app,
context,
SnackBar::new(Text::new(format!("bar{}", snack_bar_count.get())))
.duration(Duration::from_secs(time.get())),
);
*last_controller.borrow_mut() = Some(controller);
}
});
pump_app(tester, m3, home.into_widget());
let expect_counts = |tester: &WidgetTester, bar1: usize, bar2: usize| {
expect(tester, &find::text("bar1"), finds_n_widgets(bar1));
expect(tester, &find::text("bar2"), finds_n_widgets(bar2));
};
expect_counts(tester, 0, 0);
time.set(1000);
tap_key(tester, &tap_target); let first_controller = last_controller.borrow().clone().expect("shown");
time.set(2);
tap_key(tester, &tap_target); expect_counts(tester, 0, 0);
tester.pump(); expect_counts(tester, 1, 0);
tester.pump(); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); expect_counts(tester, 1, 0);
tester.pump_by(ms(10000)); expect_counts(tester, 1, 0);
(first_controller.close)(&mut tester.cell.borrow_mut());
tester.pump_by(ms(750)); tester.pump(); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); expect_counts(tester, 0, 1);
tester.pump(); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); tester.pump(); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); expect_counts(tester, 0, 0);
});
}
fn drag_gestures_of_dismiss_directions(
scaffold_width: f64,
) -> Vec<(DismissDirection, Vec<Offset>)> {
vec![
(
DismissDirection::Vertical,
vec![
Offset::new(0.0, 50.0), Offset::new(0.0, -50.0), ],
),
(
DismissDirection::Horizontal,
vec![
Offset::new(scaffold_width, 0.0), Offset::new(-scaffold_width, 0.0), ],
),
(
DismissDirection::EndToStart,
vec![Offset::new(-scaffold_width, 0.0)], ),
(
DismissDirection::StartToEnd,
vec![Offset::new(scaffold_width, 0.0)], ),
(DismissDirection::Up, vec![Offset::new(0.0, -50.0)]), (DismissDirection::Down, vec![Offset::new(0.0, 50.0)]), ]
}
#[test]
fn a_snack_bar_is_dismissed_by_a_drag_in_each_of_its_directions() {
test_widgets(|tester| {
let tap_target = GlobalKey::new();
let dismiss_direction = Rc::new(Cell::new(DismissDirection::Down));
let width = Rc::new(Cell::new(0.0));
let snack_bar_count = Rc::new(Cell::new(0));
let home = Scaffold::new().body(Builder::new({
let tap_target = tap_target.clone();
let dismiss_direction = Rc::clone(&dismiss_direction);
let width = Rc::clone(&width);
let snack_bar_count = Rc::clone(&snack_bar_count);
move |app, context| {
width.set(MediaQuery::size_of(app, context).width());
let dismiss_direction = Rc::clone(&dismiss_direction);
let snack_bar_count = Rc::clone(&snack_bar_count);
GestureDetector::new()
.key(tap_target.clone().into())
.on_tap(Listener::new(move |app| {
snack_bar_count.set(snack_bar_count.get() + 1);
show(
app,
context,
SnackBar::new(Text::new(format!("bar{}", snack_bar_count.get())))
.duration(Duration::from_secs(2))
.dismiss_direction(dismiss_direction.get()),
);
}))
.behavior(HitTestBehavior::Opaque)
.child(SizedBox::new().height(100.0).width(100.0))
.into_widget()
}
}));
pump_app(tester, m3, home.into_widget());
for (direction, gestures) in drag_gestures_of_dismiss_directions(width.get()) {
dismiss_direction.set(direction);
for drag_gesture in gestures {
snack_bar_count.set(0);
let expect_counts = |tester: &WidgetTester, bar1: usize, bar2: usize| {
expect(tester, &find::text("bar1"), finds_n_widgets(bar1));
expect(tester, &find::text("bar2"), finds_n_widgets(bar2));
};
expect_counts(tester, 0, 0);
tap_key(tester, &tap_target); tap_key(tester, &tap_target); expect_counts(tester, 0, 0);
tester.pump(); expect_counts(tester, 1, 0);
tester.pump(); expect_counts(tester, 1, 0);
tester.pump_by(ms(750)); drag_text(tester, "bar1", drag_gesture);
tester.pump(); expect_counts(tester, 0, 1);
tester.pump_by(ms(750)); drag_text(tester, "bar2", drag_gesture);
tester.pump(); expect_counts(tester, 0, 0);
}
}
});
}
fn dismisses_by_dragging(
tester: &mut WidgetTester,
snack_bar: impl Fn() -> SnackBar + 'static,
label: &str,
drag: f64,
) {
let tap_target = GlobalKey::new();
let home = tap_target_scaffold(&tap_target, move |app, context| {
show(app, context, snack_bar());
});
pump_app(
tester,
|app| {
ThemeData::builder()
.snack_bar_theme(
SnackBarThemeData::new().dismiss_direction(DismissDirection::StartToEnd),
)
.build(app)
},
home.into_widget(),
);
expect(tester, &find::text(label), finds_nothing());
tap_key(tester, &tap_target);
expect(tester, &find::text(label), finds_nothing());
tester.pump(); expect(tester, &find::text(label), finds_one_widget());
tester.pump(); expect(tester, &find::text(label), finds_one_widget());
tester.pump_by(ms(750));
drag_text(tester, label, Offset::new(drag * 800.0, 0.0));
tester.pump(); expect(tester, &find::text(label), finds_nothing());
}
#[test]
fn the_theme_sets_the_dismiss_direction() {
test_widgets(|tester| {
dismisses_by_dragging(
tester,
|| SnackBar::new(Text::new("swipe ltr")).duration(Duration::from_secs(2)),
"swipe ltr",
1.0,
);
});
}
#[test]
fn the_snack_bar_s_dismiss_direction_wins_over_the_theme_s() {
test_widgets(|tester| {
dismisses_by_dragging(
tester,
|| {
SnackBar::new(Text::new("swipe rtl"))
.duration(Duration::from_secs(2))
.dismiss_direction(DismissDirection::EndToStart)
},
"swipe rtl",
-1.0,
);
});
}
fn snack_bar_with_action() -> SnackBar {
SnackBar::new(Text::new("I am a snack bar."))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new("ACTION", |_app| {}))
}
#[test]
fn a_snack_bar_action_fires_only_once() {
test_widgets(|tester| {
let tap_count = Rc::new(Cell::new(0));
let home = x_scaffold({
let tap_count = Rc::clone(&tap_count);
move |app, context| {
let tap_count = Rc::clone(&tap_count);
show(
app,
context,
SnackBar::new(Text::new("I am a snack bar."))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new("ACTION", move |_app| {
tap_count.set(tap_count.get() + 1);
})),
);
}
});
pump_app(tester, m3, home.into_widget());
tap_x_and_settle_entrance(tester);
assert_eq!(tap_count.get(), 0);
tester.tap(find::text("ACTION"));
assert_eq!(tap_count.get(), 1);
tester.tap(find::text("ACTION"));
assert_eq!(tap_count.get(), 1);
tester.pump();
tester.tap(find::text("ACTION"));
assert_eq!(tap_count.get(), 1);
});
}
fn with_action_under(tester: &mut WidgetTester, theme: impl FnOnce(&App) -> ThemeData) {
let home = x_scaffold(|app, context| {
show(app, context, snack_bar_with_action());
});
pump_app(tester, theme, home.into_widget());
tap_x_and_settle_entrance(tester);
}
#[test]
fn a_material_2_light_theme_snack_bar_has_a_dark_background() {
test_widgets(|tester| {
with_action_under(tester, |app| ThemeData::light(app, Some(false)));
assert!(crate::test_support::is_same_color_as(
snack_bar_color(tester),
Color::new(0xFF333333)
));
});
}
#[test]
fn a_material_3_light_theme_snack_bar_has_the_inverse_surface_background() {
test_widgets(|tester| {
with_action_under(tester, m3);
let inverse_surface = m3(&tester.cell.borrow()).color_scheme.inverse_surface();
assert_eq!(snack_bar_color(tester), inverse_surface);
});
}
#[test]
fn a_dark_theme_snack_bar_has_a_light_background() {
test_widgets(|tester| {
with_action_under(tester, |app| ThemeData::dark(app, None));
let on_surface = ThemeData::dark(&tester.cell.borrow(), None)
.color_scheme
.on_surface();
assert_eq!(snack_bar_color(tester), on_surface);
});
}
#[test]
fn a_material_2_dark_theme_snack_bar_has_primary_text_buttons() {
test_widgets(|tester| {
with_action_under(tester, |app| ThemeData::dark(app, Some(false)));
let primary = ThemeData::dark(&tester.cell.borrow(), Some(false))
.color_scheme
.primary();
assert_eq!(tester.text_color("ACTION"), primary);
});
}
#[test]
fn a_material_3_dark_theme_snack_bar_has_inverse_primary_text_buttons() {
test_widgets(|tester| {
with_action_under(tester, |app| ThemeData::dark(app, None));
let inverse_primary = ThemeData::dark(&tester.cell.borrow(), None)
.color_scheme
.inverse_primary();
assert_eq!(tester.text_color("ACTION"), inverse_primary);
});
}
#[test]
fn a_snack_bar_inherits_its_ancestor_s_theme_but_for_the_colour_scheme() {
test_widgets(|tester| {
let theme_before_snack_bar: Rc<RefCell<Option<Rc<ThemeData>>>> = Rc::default();
let theme_after_snack_bar: Rc<RefCell<Option<Rc<ThemeData>>>> = Rc::default();
let home = Scaffold::new().body(Builder::new({
let theme_before_snack_bar = Rc::clone(&theme_before_snack_bar);
let theme_after_snack_bar = Rc::clone(&theme_after_snack_bar);
move |app, context| {
*theme_before_snack_bar.borrow_mut() = Some(Theme::of(app, context));
let theme_after_snack_bar = Rc::clone(&theme_after_snack_bar);
GestureDetector::new()
.on_tap(Listener::new(move |app| {
let theme_after_snack_bar = Rc::clone(&theme_after_snack_bar);
show(
app,
context,
SnackBar::new(Builder::new(move |app, context| {
*theme_after_snack_bar.borrow_mut() =
Some(Theme::of(app, context));
Text::new("I am a snack bar.").into_widget()
}))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new("ACTION", |_app| {})),
);
}))
.child(Text::new("X"))
.into_widget()
}
}));
pump_app(
tester,
|app| {
ThemeData::builder()
.color_scheme(
ColorScheme::from_seed(Colors::GREEN.primary())
.brightness(Brightness::Dark)
.build(),
)
.build(app)
},
home.into_widget(),
);
tap_x_and_settle_entrance(tester);
let theme_after_snack_bar = theme_after_snack_bar.borrow().clone().expect("built");
let compared_theme = theme_before_snack_bar
.borrow()
.as_ref()
.expect("built")
.copy_with()
.color_scheme(theme_after_snack_bar.color_scheme.clone())
.build(); assert!(compared_theme == *theme_after_snack_bar);
});
}
fn showing(
tester: &mut WidgetTester,
theme: impl FnOnce(&App) -> ThemeData,
snack_bar: impl Fn() -> SnackBar + 'static,
) {
let home = x_scaffold(move |app, context| {
show(app, context, snack_bar());
});
pump_app(tester, theme, home.into_widget());
tap_x_and_settle_entrance(tester);
}
#[test]
fn a_floating_snack_bar_s_margin_can_be_customized() {
test_widgets(|tester| {
const PADDING: f64 = 20.0;
showing(tester, m3, || {
SnackBar::new(Text::new("I am a snack bar."))
.margin(EdgeInsets::all(PADDING))
.behavior(SnackBarBehavior::Floating)
});
let material = snack_bar_material_rect(tester);
assert_eq!(material.bottom_left().dx(), PADDING);
assert_eq!(material.bottom_left().dy(), 600.0 - PADDING); assert_eq!(material.bottom_right().dx(), 800.0 - PADDING); });
}
#[test]
fn a_floating_snack_bar_is_positioned_within_the_safe_area() {
test_widgets(|tester| {
const VIEW_PADDING: f64 = 50.0;
const FLOATING_SNACK_BAR_DEFAULT_BOTTOM_MARGIN: f64 = 10.0;
let home = waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new().view_padding(EdgeInsets::only(
0.0,
0.0,
0.0,
VIEW_PADDING,
)),
x_scaffold(|app, context| {
show(
app,
context,
SnackBar::new(Text::new("I am a snack bar."))
.behavior(SnackBarBehavior::Floating),
);
}),
);
pump_app(tester, m3, home.into_widget());
tap_x_and_settle_entrance(tester);
assert_eq!(
snack_bar_material_rect(tester).bottom_left().dy(),
600.0 - VIEW_PADDING - FLOATING_SNACK_BAR_DEFAULT_BOTTOM_MARGIN
);
});
}
#[test]
fn a_snack_bar_s_padding_can_be_customized() {
test_widgets(|tester| {
const PADDING: f64 = 20.0;
showing(tester, m3, || {
SnackBar::new(Text::new("I am a snack bar.")).padding(EdgeInsets::all(PADDING))
});
let text = tester.get_rect(find::text("I am a snack bar."));
let material = snack_bar_material_rect(tester);
assert_eq!(
text.bottom_left().dx() - material.bottom_left().dx(),
PADDING
);
assert_eq!(material.top_right().dx() - text.top_right().dx(), PADDING);
assert_eq!(
material.bottom_left().dy() - text.bottom_left().dy(),
PADDING
);
assert_eq!(text.top_right().dy() - material.top_right().dy(), PADDING);
});
}
fn assert_centred_with_width(tester: &WidgetTester, width: f64) {
let material = snack_bar_material_rect(tester);
assert_eq!(material.bottom_left().dx(), (800.0 - width) / 2.0); assert_eq!(material.bottom_right().dx(), (800.0 + width) / 2.0); }
#[test]
fn a_floating_snack_bar_s_width_can_be_customized() {
test_widgets(|tester| {
const WIDTH: f64 = 200.0;
showing(tester, m3, || {
SnackBar::new(Text::new("I am a snack bar."))
.width(WIDTH)
.behavior(SnackBarBehavior::Floating)
});
assert_centred_with_width(tester, WIDTH);
});
}
#[test]
fn a_floating_snack_bar_s_width_can_come_from_the_theme() {
test_widgets(|tester| {
const WIDTH: f64 = 200.0;
showing(
tester,
|app| {
ThemeData::builder()
.snack_bar_theme(
SnackBarThemeData::new()
.width(WIDTH)
.behavior(SnackBarBehavior::Floating),
)
.build(app)
},
|| SnackBar::new(Text::new("Feeling snackish")),
);
assert_centred_with_width(tester, WIDTH);
});
}
#[test]
fn the_snack_bar_s_width_wins_over_the_theme_s() {
test_widgets(|tester| {
const THEME_WIDTH: f64 = 200.0;
const WIDGET_WIDTH: f64 = 400.0;
showing(
tester,
|app| {
ThemeData::builder()
.snack_bar_theme(
SnackBarThemeData::new()
.width(THEME_WIDTH)
.behavior(SnackBarBehavior::Floating),
)
.build(app)
},
|| SnackBar::new(Text::new("Feeling super snackish")).width(WIDGET_WIDTH),
);
assert_centred_with_width(tester, WIDGET_WIDTH);
});
}
fn action_button_style(tester: &WidgetTester) -> crate::button_style::ButtonStyle {
use crate::button_style_button::ButtonStyleButton;
let action = find::text("ACTION").evaluate(tester)[0];
let button = tester.ancestor_of_type::<TextButton>(action);
tester.widget(button, |button: &TextButton| {
button
.button_style_button_data()
.style
.clone()
.expect("the action's style")
})
}
fn states(states: &[WidgetState]) -> waft_widgets::WidgetStates {
states.iter().copied().collect()
}
#[test]
fn a_material_2_snack_bar_action_label_takes_its_text_color() {
test_widgets(|tester| {
showing(tester, m2, || {
SnackBar::new(Text::new("I am a snack bar."))
.duration(Duration::from_secs(2))
.action(
SnackBarAction::new("ACTION", |_app| {})
.text_color(Colors::LIGHT_BLUE.primary())
.disabled_text_color(Colors::RED.primary()),
)
});
assert_eq!(tester.text_color("ACTION"), Colors::LIGHT_BLUE.primary());
});
}
#[test]
fn a_material_3_snack_bar_action_resolves_its_text_color_in_every_state() {
test_widgets(|tester| {
let used_color = Colors::TEAL.primary();
showing(tester, m3, move || {
SnackBar::new(Text::new("I am a snack bar."))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new("ACTION", |_app| {}).text_color(used_color))
});
let style = action_button_style(tester);
assert_eq!(
style
.foreground_color
.expect("a foreground color")
.resolve(&states(&[])),
Some(used_color)
);
});
}
#[test]
fn a_widget_state_text_color_resolves_the_action_label_by_state() {
test_widgets(|tester| {
const COLOR_RED: u32 = 0xFFF44336;
const COLOR_BLUE: u32 = 0xFF2196F3;
showing(tester, m3, || {
let used_color = WidgetStateColor::resolve_with(|states| {
if states.contains(&WidgetState::Pressed) {
return Color::new(COLOR_BLUE);
}
Color::new(COLOR_RED)
});
SnackBar::new(Text::new("I am a snack bar."))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new("ACTION", |_app| {}).text_color(used_color))
});
let foreground_color = action_button_style(tester)
.foreground_color
.expect("a foreground color");
assert_eq!(
foreground_color.resolve(&states(&[WidgetState::Pressed])),
Some(Color::new(COLOR_BLUE))
);
assert_eq!(
foreground_color.resolve(&states(&[])),
Some(Color::new(COLOR_RED))
);
});
}
#[test]
fn a_snack_bar_pushes_the_floating_action_button_above_it() {
test_widgets(|tester| {
let home = waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new()
.padding(EdgeInsets::only(10.0, 20.0, 30.0, 40.0)),
x_scaffold(|app, context| {
show(app, context, snack_bar_with_action());
})
.floating_action_button(
crate::floating_action_button::FloatingActionButton::new(Some(Listener::new(
|_app| {},
)))
.child(Icon::new(Some(Icons::send()))),
),
);
pump_app(tester, m3, home.into_widget());
let fab_rect = |tester: &WidgetTester| {
let fab = find::by_type::<crate::floating_action_button::FloatingActionButton>()
.evaluate(tester)[0];
tester.get_rect(fab)
};
let original_fab_rect = fab_rect(tester);
tap_x_and_settle_entrance(tester);
let fab_rect = fab_rect(tester);
assert!(fab_rect.center().dy() < original_fab_rect.center().dy());
let snack_bar = find::by_type::<SnackBar>().evaluate(tester)[0];
let snack_bar_top_right = tester.get_rect(snack_bar).top_right();
const DEFAULT_FAB_PADDING: f64 = 16.0;
assert_f32_equals(
fab_rect.bottom_right().dy(),
snack_bar_top_right.dy() - DEFAULT_FAB_PADDING,
);
});
}
#[test]
fn the_closed_future_tells_how_the_snack_bar_closed() {
test_widgets(|tester| {
let action_pressed = Rc::new(Cell::new(false));
let closed_reason: Rc<Cell<Option<SnackBarClosedReason>>> = Rc::default();
let body_context: Rc<Cell<Option<BuildContext>>> = Rc::default();
let home = |with_action: bool| {
let action_pressed = Rc::clone(&action_pressed);
let closed_reason = Rc::clone(&closed_reason);
let body_context = Rc::clone(&body_context);
Scaffold::new().body(Builder::new(move |_app, context| {
body_context.set(Some(context));
let action_pressed = Rc::clone(&action_pressed);
let closed_reason = Rc::clone(&closed_reason);
GestureDetector::new()
.on_tap(Listener::new(move |app| {
let mut snack_bar =
SnackBar::new(Text::new("snack")).duration(Duration::from_secs(2));
if with_action {
let action_pressed = Rc::clone(&action_pressed);
snack_bar = snack_bar
.action(SnackBarAction::new("ACTION", move |_app| {
action_pressed.set(true)
}));
}
let closed_reason = Rc::clone(&closed_reason);
show(app, context, snack_bar)
.closed()
.then(app, move |_app, reason| closed_reason.set(Some(reason)));
}))
.child(Text::new("X"))
.into_widget()
}))
};
let messenger = |tester: &WidgetTester| {
let mut app = tester.cell.borrow_mut();
let context = body_context.get().expect("built");
ScaffoldMessenger::of(&mut app, context)
};
pump_app(tester, m3, home(true).into_widget());
tap_x_and_settle_entrance(tester);
assert!(!action_pressed.get());
tester.tap(find::text("ACTION"));
assert!(action_pressed.get());
tester.pump_by(ms(250));
assert_eq!(closed_reason.get(), None);
tester.pump_and_settle_by(Duration::from_secs(1));
assert_eq!(closed_reason.get(), Some(SnackBarClosedReason::Action));
tester.tap(find::text("X"));
tester.pump_by(ms(750));
tester.pump_by(ms(750));
drag_text(tester, "snack", Offset::new(0.0, 50.0));
tester.pump_and_settle_by(Duration::from_secs(1));
assert_eq!(closed_reason.get(), Some(SnackBarClosedReason::Swipe));
tester.tap(find::text("X"));
tester.pump_by(ms(750));
let scaffold_messenger = messenger(tester);
scaffold_messenger.remove_current_snack_bar(
&mut tester.cell.borrow_mut(),
SnackBarClosedReason::Remove,
);
tester.pump_and_settle_by(Duration::from_secs(1));
assert_eq!(closed_reason.get(), Some(SnackBarClosedReason::Remove));
tester.tap(find::text("X"));
tester.pump_by(ms(750));
let scaffold_messenger = messenger(tester);
scaffold_messenger
.hide_current_snack_bar(&mut tester.cell.borrow_mut(), SnackBarClosedReason::Hide);
tester.pump_and_settle_by(Duration::from_secs(1));
assert_eq!(closed_reason.get(), Some(SnackBarClosedReason::Hide));
pump_app(tester, m3, home(false).into_widget());
tester.tap(find::text("X"));
tester.pump_by(ms(750));
tester.pump_by(ms(750));
tester.pump_by(ms(1500));
tester.pump(); tester.pump_and_settle_by(Duration::from_secs(1));
assert_eq!(closed_reason.get(), Some(SnackBarClosedReason::Timeout));
});
}
#[test]
fn under_accessible_navigation_a_snack_bar_with_an_action_shows_at_once_and_stays() {
test_widgets(|tester| {
let scaffold_key = GlobalKey::new();
let home = waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new().accessible_navigation(true),
ScaffoldMessenger::new(Builder::new({
let scaffold_key = scaffold_key.clone();
move |_app, context| {
Scaffold::new()
.key(scaffold_key.clone().into())
.body(
GestureDetector::new()
.on_tap(Listener::new(move |app| {
show(
app,
context,
SnackBar::new(Text::new("snack"))
.duration(Duration::from_secs(1))
.action(SnackBarAction::new("ACTION", |_app| {})),
);
}))
.child(Text::new("X")),
)
.into_widget()
}
})),
);
pump_app(tester, m3, home.into_widget());
tester.tap(find::text("X"));
tester.pump();
expect(tester, &find::text("ACTION"), finds_one_widget());
tester.pump_by(Duration::from_secs(10));
expect(tester, &find::text("ACTION"), finds_one_widget());
tester.tap(find::text("ACTION"));
tester.pump();
expect(tester, &find::text("ACTION"), finds_nothing());
});
}
#[test]
fn a_snack_bar_shows_for_four_seconds_by_default() {
test_widgets(|tester| {
const HELLO_SNACK_BAR: &str = "Hello SnackBar";
let tap_target = GlobalKey::new();
let home = tap_target_scaffold(&tap_target, |app, context| {
show(app, context, SnackBar::new(Text::new(HELLO_SNACK_BAR)));
});
pump_app(tester, m3, home.into_widget());
expect(tester, &find::text(HELLO_SNACK_BAR), finds_nothing());
tap_key(tester, &tap_target);
expect(tester, &find::text(HELLO_SNACK_BAR), finds_nothing());
tester.pump(); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump(); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget());
tester.pump_by(ms(1000)); tester.pump(); expect(tester, &find::text(HELLO_SNACK_BAR), finds_one_widget()); tester.pump_by(ms(750)); expect(tester, &find::text(HELLO_SNACK_BAR), finds_nothing());
});
}
#[test]
fn a_snack_bar_follows_changes_to_accessible_navigation() {
test_widgets(|tester| {
let boilerplate = |tester: &mut WidgetTester, accessible_navigation: bool| {
let theme = m3(&tester.cell.borrow());
tester.pump_widget(
waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new()
.accessible_navigation(accessible_navigation),
material_app(
theme,
Scaffold::new()
.body(Builder::new(|_app, context| {
GestureDetector::new()
.on_tap(Listener::new(move |app| {
show(
app,
context,
SnackBar::new(Text::new("test"))
.action(SnackBarAction::new("foo", |_app| {})),
);
}))
.behavior(HitTestBehavior::Opaque)
.child(Text::new("X"))
.into_widget()
}))
.into_widget(),
),
)
.into_widget(),
);
};
boilerplate(tester, false);
expect(tester, &find::text("test"), finds_nothing());
tester.tap(find::text("X"));
tester.pump(); expect(tester, &find::text("test"), finds_one_widget());
tester.pump(); tester.pump_by(ms(4750)); expect(tester, &find::text("test"), finds_one_widget());
boilerplate(tester, true);
tester.pump_by(ms(4000)); tester.pump();
expect(tester, &find::text("test"), finds_one_widget());
boilerplate(tester, false);
tester.pump_and_settle_by(ms(5750));
expect(tester, &find::text("test"), finds_one_widget());
tester.tap(find::text("foo"));
tester.pump_and_settle();
expect(tester, &find::text("test"), finds_nothing());
});
}
fn count_on_visible(
tester: &mut WidgetTester,
labels: &'static [&'static str],
) -> Rc<Cell<i32>> {
let called = Rc::new(Cell::new(0));
let tap_target = GlobalKey::new();
let home = tap_target_scaffold(&tap_target, {
let called = Rc::clone(&called);
move |app, context| {
for label in labels {
let called = Rc::clone(&called);
show(
app,
context,
SnackBar::new(Text::new(*label))
.duration(Duration::from_secs(1))
.on_visible(move |_app| called.set(called.get() + 1)),
);
}
}
});
pump_app(tester, m3, home.into_widget());
tap_key(tester, &tap_target);
tester.pump(); tester.pump_and_settle();
called
}
#[test]
fn a_snack_bar_calls_on_visible_once() {
test_widgets(|tester| {
let called = count_on_visible(tester, &["hello"]);
expect(tester, &find::text("hello"), finds_one_widget());
assert_eq!(called.get(), 1);
});
}
#[test]
fn a_queued_snack_bar_does_not_call_on_visible() {
test_widgets(|tester| {
let called = count_on_visible(tester, &["hello", "hello 2"]);
expect(tester, &find::text("hello"), finds_one_widget());
assert_eq!(called.get(), 1);
});
}
fn rect_of_type<W: 'static>(tester: &WidgetTester) -> Rect {
let element = find::by_type::<W>().evaluate(tester)[0];
tester.get_rect(element)
}
fn rect_of_key(tester: &WidgetTester, key: &GlobalKey) -> Rect {
tester.get_rect(key)
}
fn show_from_messenger_and_settle(tester: &mut WidgetTester, snack_bar: SnackBar) {
let messenger = find::by_type::<ScaffoldMessenger>().evaluate(tester)[0];
{
let mut app = tester.cell.borrow_mut();
let state = messenger
.state_handle::<crate::scaffold::ScaffoldMessengerState>(&app)
.expect("a ScaffoldMessengerState");
state.show_snack_bar(&mut app, snack_bar, None);
}
tester.pump_and_settle(); }
fn fab() -> crate::floating_action_button::FloatingActionButton {
crate::floating_action_button::FloatingActionButton::new(Some(Listener::new(|_app| {})))
}
fn fab_with_icon() -> crate::floating_action_button::FloatingActionButton {
fab().child(Icon::new(Some(Icons::send())))
}
#[test]
fn a_snack_bar_aligns_with_the_bottom_of_an_empty_scaffold() {
test_widgets(|tester| {
for behavior in [SnackBarBehavior::Fixed, SnackBarBehavior::Floating] {
pump_app(
tester,
m3,
Scaffold::new()
.body(waft_widgets::Container::new())
.into_widget(),
);
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("SnackBar text")).behavior(behavior),
);
let snack_bar = rect_of_type::<SnackBar>(tester);
let scaffold = rect_of_type::<Scaffold>(tester);
assert_eq!(
snack_bar.bottom_right(),
scaffold.bottom_right(),
"{behavior:?}"
);
assert_eq!(
snack_bar.bottom_left(),
scaffold.bottom_left(),
"{behavior:?}"
);
}
});
}
#[test]
fn a_snack_bar_aligns_with_the_top_of_the_bottom_navigation_bar() {
test_widgets(|tester| {
for behavior in [SnackBarBehavior::Fixed, SnackBarBehavior::Floating] {
let box_key = GlobalKey::new();
pump_app(
tester,
m3,
Scaffold::new()
.body(waft_widgets::Container::new())
.bottom_navigation_bar(
SizedBox::new()
.key(box_key.clone().into())
.width(800.0)
.height(60.0),
)
.into_widget(),
);
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("SnackBar text")).behavior(behavior),
);
let snack_bar = rect_of_type::<SnackBar>(tester);
let bottom_navigation_bar = rect_of_key(tester, &box_key);
assert_eq!(snack_bar.bottom_right(), bottom_navigation_bar.top_right());
assert_eq!(snack_bar.bottom_left(), bottom_navigation_bar.top_left());
}
});
}
#[test]
fn a_fixed_snack_bar_s_padding_is_not_consumed_by_view_insets() {
test_widgets(|tester| {
let child = |tester: &WidgetTester| {
material_app(
m3(&tester.cell.borrow()),
Scaffold::new()
.resize_to_avoid_bottom_inset(false)
.floating_action_button(fab_with_icon())
.body(Builder::new(|_app, context| {
GestureDetector::new()
.on_tap(Listener::new(move |app| {
show(
app,
context,
snack_bar_with_action().behavior(SnackBarBehavior::Fixed),
);
}))
.child(Text::new("X"))
.into_widget()
}))
.into_widget(),
)
};
let app = child(tester);
tester.pump_widget(
waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new()
.padding(EdgeInsets::only(0.0, 0.0, 0.0, 20.0)),
app,
)
.into_widget(),
);
tester.tap(find::text("X"));
tester.pump_and_settle(); let initial = rect_of_type::<SnackBar>(tester);
let app = child(tester);
tester.pump_widget(
waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new()
.view_padding(EdgeInsets::all(20.0))
.view_insets(EdgeInsets::all(100.0)),
app,
)
.into_widget(),
);
tester.tap(find::text("X"));
tester.pump_and_settle(); let last = rect_of_type::<SnackBar>(tester);
assert_eq!(initial.bottom_left(), last.bottom_left());
assert_eq!(initial.bottom_right(), last.bottom_right());
});
}
#[test]
fn a_fixed_snack_bar_aligns_with_the_bottom_of_a_scaffold_with_a_floating_action_button() {
test_widgets(|tester| {
pump_app(
tester,
m3,
Scaffold::new()
.body(waft_widgets::Container::new())
.floating_action_button(fab())
.into_widget(),
);
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("Snackbar text")).behavior(SnackBarBehavior::Fixed),
);
let snack_bar = rect_of_type::<SnackBar>(tester);
let scaffold = rect_of_type::<Scaffold>(tester);
assert_eq!(snack_bar.bottom_right(), scaffold.bottom_right());
assert_eq!(snack_bar.bottom_left(), scaffold.bottom_left());
});
}
fn floating_with_fab(
tester: &mut WidgetTester,
fab_location: Option<
crate::floating_action_button_location::FloatingActionButtonLocationRef,
>,
) {
let mut scaffold = x_scaffold(|app, context| {
show(
app,
context,
snack_bar_with_action().behavior(SnackBarBehavior::Floating),
);
})
.floating_action_button(fab_with_icon());
if let Some(fab_location) = fab_location {
scaffold = scaffold.floating_action_button_location(fab_location);
}
pump_app(tester, m3, scaffold.into_widget());
tester.tap(find::text("X"));
tester.pump_and_settle(); }
#[test]
fn a_floating_snack_bar_sits_on_top_of_the_floating_action_button() {
test_widgets(|tester| {
floating_with_fab(tester, None);
assert_eq!(
rect_of_type::<SnackBar>(tester).bottom_left().dy(),
rect_of_type::<crate::floating_action_button::FloatingActionButton>(tester)
.top_left()
.dy()
);
});
}
#[test]
fn a_floating_snack_bar_ignores_a_floating_action_button_at_a_top_location() {
test_widgets(|tester| {
use crate::floating_action_button_location::FloatingActionButtonLocation;
let top_locations = [
<dyn FloatingActionButtonLocation>::start_top(),
<dyn FloatingActionButtonLocation>::center_top(),
<dyn FloatingActionButtonLocation>::end_top(),
<dyn FloatingActionButtonLocation>::mini_start_top(),
<dyn FloatingActionButtonLocation>::mini_center_top(),
<dyn FloatingActionButtonLocation>::mini_end_top(),
];
for location in top_locations {
floating_with_fab(tester, Some(Rc::clone(&location)));
assert_eq!(
rect_of_type::<SnackBar>(tester).bottom_left().dy(),
600.0, "{location:?}"
);
}
});
}
#[derive(Debug)]
struct CustomFloatingActionButtonLocation;
impl crate::floating_action_button_location::FloatingActionButtonLocation
for CustomFloatingActionButtonLocation
{
fn get_offset(
&self,
scaffold_geometry: &crate::scaffold::ScaffoldPrelayoutGeometry,
) -> Offset {
crate::floating_action_button_location::StandardFabLocation::get_offset(
self,
scaffold_geometry,
)
}
}
impl crate::floating_action_button_location::StandardFabLocation
for CustomFloatingActionButtonLocation
{
fn get_offset_x(
&self,
scaffold_geometry: &crate::scaffold::ScaffoldPrelayoutGeometry,
adjustment: f64,
) -> f64 {
crate::floating_action_button_location::FabEndOffsetX::get_offset_x(
self,
scaffold_geometry,
adjustment,
)
}
fn get_offset_y(
&self,
scaffold_geometry: &crate::scaffold::ScaffoldPrelayoutGeometry,
adjustment: f64,
) -> f64 {
crate::floating_action_button_location::FabFloatOffsetY::get_offset_y(
self,
scaffold_geometry,
adjustment,
)
}
}
impl crate::floating_action_button_location::FabEndOffsetX for CustomFloatingActionButtonLocation {}
impl crate::floating_action_button_location::FabFloatOffsetY
for CustomFloatingActionButtonLocation
{
}
#[test]
fn a_floating_snack_bar_sits_on_top_of_a_floating_action_button_at_any_other_location() {
test_widgets(|tester| {
use crate::floating_action_button_location::FloatingActionButtonLocation;
let non_top_locations: Vec<
crate::floating_action_button_location::FloatingActionButtonLocationRef,
> = vec![
<dyn FloatingActionButtonLocation>::start_docked(),
<dyn FloatingActionButtonLocation>::start_float(),
<dyn FloatingActionButtonLocation>::center_docked(),
<dyn FloatingActionButtonLocation>::center_float(),
<dyn FloatingActionButtonLocation>::end_contained(),
<dyn FloatingActionButtonLocation>::end_docked(),
<dyn FloatingActionButtonLocation>::end_float(),
<dyn FloatingActionButtonLocation>::mini_start_docked(),
<dyn FloatingActionButtonLocation>::mini_start_float(),
<dyn FloatingActionButtonLocation>::mini_center_docked(),
<dyn FloatingActionButtonLocation>::mini_center_float(),
<dyn FloatingActionButtonLocation>::mini_end_docked(),
<dyn FloatingActionButtonLocation>::mini_end_float(),
Rc::new(CustomFloatingActionButtonLocation),
];
for location in non_top_locations {
floating_with_fab(tester, Some(Rc::clone(&location)));
assert_eq!(
rect_of_type::<SnackBar>(tester).bottom_left().dy(),
rect_of_type::<crate::floating_action_button::FloatingActionButton>(tester)
.top_left()
.dy(),
"{location:?}"
);
}
});
}
fn with_bottom_bar_and_fab(
box_key: &GlobalKey,
bar_height: f64,
fab_location: Option<
crate::floating_action_button_location::FloatingActionButtonLocationRef,
>,
) -> WidgetRef {
let mut scaffold = Scaffold::new()
.body(waft_widgets::Container::new())
.bottom_navigation_bar(
SizedBox::new()
.key(box_key.clone().into())
.width(800.0)
.height(bar_height),
)
.floating_action_button(fab());
if let Some(fab_location) = fab_location {
scaffold = scaffold.floating_action_button_location(fab_location);
}
scaffold.into_widget()
}
#[test]
fn a_fixed_snack_bar_sits_on_the_bottom_navigation_bar_beside_a_floating_action_button() {
test_widgets(|tester| {
let box_key = GlobalKey::new();
pump_app(tester, m3, with_bottom_bar_and_fab(&box_key, 60.0, None));
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("SnackBar text")).behavior(SnackBarBehavior::Fixed),
);
let snack_bar = rect_of_type::<SnackBar>(tester);
let bottom_navigation_bar = rect_of_key(tester, &box_key);
assert_eq!(snack_bar.bottom_right(), bottom_navigation_bar.top_right());
assert_eq!(snack_bar.bottom_left(), bottom_navigation_bar.top_left());
});
}
#[test]
fn a_floating_snack_bar_sits_on_the_floating_action_button_above_a_bottom_navigation_bar() {
test_widgets(|tester| {
let box_key = GlobalKey::new();
pump_app(tester, m3, with_bottom_bar_and_fab(&box_key, 60.0, None));
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("SnackBar text")).behavior(SnackBarBehavior::Floating),
);
assert_eq!(
rect_of_type::<SnackBar>(tester).bottom_right().dy(),
rect_of_type::<crate::floating_action_button::FloatingActionButton>(tester)
.top_right()
.dy()
);
});
}
#[test]
fn a_floating_snack_bar_sits_on_a_bottom_navigation_bar_higher_than_the_button() {
test_widgets(|tester| {
use crate::floating_action_button_location::FloatingActionButtonLocation;
let box_key = GlobalKey::new();
pump_app(
tester,
m3,
with_bottom_bar_and_fab(
&box_key,
200.0,
Some(<dyn FloatingActionButtonLocation>::end_contained()),
),
);
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("SnackBar text")).behavior(SnackBarBehavior::Floating),
);
let snack_bar_bottom_right = rect_of_type::<SnackBar>(tester).bottom_right();
let fab_top_right =
rect_of_type::<crate::floating_action_button::FloatingActionButton>(tester)
.top_right();
let nav_bar_top_right = rect_of_key(tester, &box_key).top_right();
assert!(fab_top_right.dy() > nav_bar_top_right.dy());
assert_eq!(snack_bar_bottom_right.dy(), nav_bar_top_right.dy());
});
}
fn open_floating_snack_bar(tester: &mut WidgetTester) {
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("SnackBar text")).behavior(SnackBarBehavior::Floating),
);
}
fn with_fab_height(tester: &mut WidgetTester, fab_height: f64) {
pump_app(
tester,
m3,
Scaffold::new()
.floating_action_button(waft_widgets::Container::new().height(fab_height))
.into_widget(),
);
}
const MEDIUM_FAB_HEIGHT: f64 = 100.0;
fn space_above_snack_bar(tester: &mut WidgetTester) -> f64 {
with_fab_height(tester, MEDIUM_FAB_HEIGHT);
open_floating_snack_bar(tester);
rect_of_type::<SnackBar>(tester).top_left().dy()
}
#[test]
fn a_floating_snack_bar_right_at_the_top_of_the_screen_does_not_assert() {
test_widgets(|tester| {
let space_above_snack_bar = space_above_snack_bar(tester);
with_fab_height(tester, space_above_snack_bar + MEDIUM_FAB_HEIGHT);
open_floating_snack_bar(tester);
});
}
#[test]
#[should_panic(
expected = "Floating SnackBar presented off screen.\nA SnackBar with behavior property \
set to SnackBarBehavior.floating is fully or partially off screen because \
some or all the widgets provided to Scaffold.floatingActionButton, \
Scaffold.persistentFooterButtons and Scaffold.bottomNavigationBar take up \
too much vertical space.\nConsider constraining the size of these widgets \
to allow room for the SnackBar to be visible."
)]
fn a_floating_snack_bar_pushed_fully_off_screen_by_a_large_button_asserts() {
test_widgets(|tester| {
let space_above_snack_bar = space_above_snack_bar(tester);
with_fab_height(tester, space_above_snack_bar + MEDIUM_FAB_HEIGHT * 2.0);
open_floating_snack_bar(tester);
});
}
#[test]
#[should_panic(expected = "Floating SnackBar presented off screen.")]
fn a_floating_snack_bar_pushed_partly_off_screen_by_a_large_button_asserts() {
test_widgets(|tester| {
let space_above_snack_bar = space_above_snack_bar(tester);
with_fab_height(tester, space_above_snack_bar + MEDIUM_FAB_HEIGHT + 10.0);
open_floating_snack_bar(tester);
});
}
#[test]
#[should_panic(expected = "Floating SnackBar presented off screen.")]
fn a_material_2_floating_snack_bar_pushed_off_screen_by_large_footer_buttons_asserts() {
test_widgets(|tester| {
pump_app(
tester,
m2,
Scaffold::new()
.persistent_footer_buttons(vec![SizedBox::new().height(1000.0).into_widget()])
.into_widget(),
);
open_floating_snack_bar(tester);
tester.pump_and_settle(); });
}
#[test]
#[should_panic(expected = "Floating SnackBar presented off screen.")]
fn a_material_2_floating_snack_bar_pushed_off_screen_by_a_large_bottom_bar_asserts() {
test_widgets(|tester| {
pump_app(
tester,
m2,
Scaffold::new()
.bottom_navigation_bar(SizedBox::new().height(1000.0))
.into_widget(),
);
open_floating_snack_bar(tester);
tester.pump_and_settle(); });
}
#[test]
#[should_panic(expected = "Floating SnackBar presented off screen.")]
fn a_material_3_floating_snack_bar_pushed_off_screen_by_large_footer_buttons_asserts() {
test_widgets(|tester| {
pump_app(
tester,
m3,
Scaffold::new()
.persistent_footer_buttons(vec![SizedBox::new().height(1000.0).into_widget()])
.into_widget(),
);
open_floating_snack_bar(tester);
tester.pump_and_settle(); });
}
#[test]
#[should_panic(expected = "Floating SnackBar presented off screen.")]
fn a_material_3_floating_snack_bar_pushed_off_screen_by_a_large_bottom_bar_asserts() {
test_widgets(|tester| {
pump_app(
tester,
m3,
Scaffold::new()
.bottom_navigation_bar(SizedBox::new().height(1000.0))
.into_widget(),
);
open_floating_snack_bar(tester);
tester.pump_and_settle(); });
}
fn assert_more_or_less_equals(actual: f64, expected: f64) {
assert!(
(actual - expected).abs() < 1e-3,
"{actual} is not more or less {expected}"
);
}
fn action_end_padding(tester: &mut WidgetTester, behavior: SnackBarBehavior) -> f64 {
let home = x_scaffold(move |app, context| {
show(
app,
context,
SnackBar::new(Text::new("Some content"))
.behavior(behavior)
.action(SnackBarAction::new("ACTION", |_app| {})),
);
});
pump_app(tester, m3, home.into_widget());
tester.tap(find::text("X"));
tester.pump_and_settle();
rect_of_type::<SnackBar>(tester).top_right().dx()
- rect_of_type::<SnackBarAction>(tester).top_right().dx()
}
#[test]
fn a_fixed_snack_bar_s_action_sits_12_from_its_end() {
test_widgets(|tester| {
assert_more_or_less_equals(action_end_padding(tester, SnackBarBehavior::Fixed), 12.0);
});
}
#[test]
fn a_floating_snack_bar_s_action_sits_23_from_its_end() {
test_widgets(|tester| {
assert_more_or_less_equals(
action_end_padding(tester, SnackBarBehavior::Floating),
8.0 + 15.0, );
});
}
fn centred_right_to_left(tester: &mut WidgetTester, theme: fn(&App) -> ThemeData) {
const CUSTOM_WIDTH: f64 = 400.0;
let home = Directionality::new(
TextDirection::Rtl,
x_scaffold(|app, context| {
show(
app,
context,
SnackBar::new(Text::new("Feeling super snackish"))
.behavior(SnackBarBehavior::Floating)
.width(CUSTOM_WIDTH),
);
}),
);
pump_app(tester, theme, home.into_widget());
tap_x_and_settle_entrance(tester);
assert_centred_with_width(tester, CUSTOM_WIDTH);
}
#[test]
fn a_material_3_floating_snack_bar_of_custom_width_is_centred_right_to_left() {
test_widgets(|tester| centred_right_to_left(tester, m3));
}
#[test]
fn a_material_2_floating_snack_bar_of_custom_width_is_centred_right_to_left() {
test_widgets(|tester| centred_right_to_left(tester, m2));
}
#[test]
fn presenting_a_material_banner_does_not_duplicate_the_snack_bar() {
test_widgets(|tester| {
const SNACK_BAR_TEXT: &str = "SnackBar";
const MATERIAL_BANNER_TEXT: &str = "MaterialBanner";
let material_banner_tap_target = GlobalKey::new();
let snack_bar_tap_target = GlobalKey::new();
let home = Scaffold::new().body(Builder::new({
let material_banner_tap_target = material_banner_tap_target.clone();
let snack_bar_tap_target = snack_bar_tap_target.clone();
move |_app, context| {
waft_widgets::Column::new()
.children([
GestureDetector::new()
.key(snack_bar_tap_target.clone().into())
.on_tap(Listener::new(move |app| {
show(app, context, SnackBar::new(Text::new(SNACK_BAR_TEXT)));
}))
.behavior(HitTestBehavior::Opaque)
.child(SizedBox::new().height(100.0).width(100.0))
.into_widget(),
GestureDetector::new()
.key(material_banner_tap_target.clone().into())
.on_tap(Listener::new(move |app| {
let banner = crate::banner::MaterialBanner::new(
Text::new(MATERIAL_BANNER_TEXT),
vec![
TextButton::new(
Some(Listener::new(move |app| {
ScaffoldMessenger::of(app, context)
.hide_current_material_banner(
app,
crate::banner::MaterialBannerClosedReason::Hide,
);
})),
Some(Text::new("DISMISS")),
)
.into_widget(),
],
);
ScaffoldMessenger::of(app, context)
.show_material_banner(app, banner);
}))
.behavior(HitTestBehavior::Opaque)
.child(SizedBox::new().height(100.0).width(100.0))
.into_widget(),
])
.into_widget()
}
}));
pump_app(tester, m3, home.into_widget());
tap_key(tester, &snack_bar_tap_target);
tap_key(tester, &material_banner_tap_target);
tester.pump_and_settle();
expect(tester, &find::text(SNACK_BAR_TEXT), finds_one_widget());
expect(
tester,
&find::text(MATERIAL_BANNER_TEXT),
finds_one_widget(),
);
});
}
fn top_of_snack_bar_over_nested_scaffolds(
tester: &mut WidgetTester,
theme: fn(&App) -> ThemeData,
) -> f64 {
pump_app(
tester,
theme,
Scaffold::new()
.body(Scaffold::new())
.floating_action_button(fab())
.into_widget(),
);
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("ScaffoldMessenger"))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new("ACTION", |_app| {}))
.behavior(SnackBarBehavior::Floating),
);
expect(tester, &find::by_type::<SnackBar>(), finds_one_widget());
rect_of_type::<SnackBar>(tester).top_right().dy()
}
#[test]
fn a_material_2_snack_bar_shows_only_in_the_root_of_nested_scaffolds() {
test_widgets(|tester| {
assert_eq!(top_of_snack_bar_over_nested_scaffolds(tester, m2), 465.0);
});
}
#[test]
fn a_material_3_snack_bar_shows_only_in_the_root_of_nested_scaffolds() {
test_widgets(|tester| {
assert_eq!(top_of_snack_bar_over_nested_scaffolds(tester, m3), 465.0);
});
}
#[test]
fn clear_snack_bars_hides_the_current_snack_bar_and_drops_the_queue() {
test_widgets(|tester| {
const SNACK_BARS: [&str; 3] = ["Hello Snackbar", "Hi Snackbar", "Bye Snackbar"];
let snack_bar_counter = Rc::new(Cell::new(0));
let tap_target = GlobalKey::new();
let scaffold_messenger_key = GlobalKey::new();
let home = ScaffoldMessenger::new(tap_target_scaffold(&tap_target, {
let snack_bar_counter = Rc::clone(&snack_bar_counter);
move |app, context| {
let index = snack_bar_counter.get();
snack_bar_counter.set(index + 1);
show(
app,
context,
SnackBar::new(Text::new(SNACK_BARS[index]))
.duration(Duration::from_secs(2)),
);
}
}))
.key(scaffold_messenger_key.clone().into());
pump_app(tester, m3, home.into_widget());
let expect_counts = |tester: &WidgetTester, counts: [usize; 3]| {
for (text, count) in SNACK_BARS.iter().zip(counts) {
expect(tester, &find::text(text), finds_n_widgets(count));
}
};
expect_counts(tester, [0, 0, 0]);
tap_key(tester, &tap_target);
tap_key(tester, &tap_target);
tap_key(tester, &tap_target);
expect_counts(tester, [0, 0, 0]);
tester.pump(); expect(tester, &find::text(SNACK_BARS[0]), finds_one_widget());
{
let mut app = tester.cell.borrow_mut();
let messenger = scaffold_messenger_key
.current_state::<crate::scaffold::ScaffoldMessengerState>(&app)
.expect("mounted");
messenger.clear_snack_bars(&mut app);
}
expect(tester, &find::text(SNACK_BARS[0]), finds_one_widget());
tester.pump_by(Duration::from_secs(2));
expect_counts(tester, [0, 0, 0]);
});
}
fn do_build_app(tester: &mut WidgetTester, configure: impl Fn(SnackBar) -> SnackBar + 'static) {
let home = x_scaffold(move |app, context| {
show(app, context, configure(snack_bar_with_action()));
})
.floating_action_button(fab_with_icon());
pump_app(tester, m3, home.into_widget());
}
fn shows(tester: &mut WidgetTester, configure: impl Fn(SnackBar) -> SnackBar + 'static) {
do_build_app(tester, configure);
tap_x_and_settle_entrance(tester);
}
#[test]
#[should_panic(
expected = "Margin can only be used with floating behavior. SnackBarBehavior.fixed was \
set in the SnackBar constructor."
)]
fn a_fixed_snack_bar_asserts_against_a_margin() {
test_widgets(|tester| {
shows(tester, |snack_bar| {
snack_bar
.behavior(SnackBarBehavior::Fixed)
.margin(EdgeInsets::all(8.0))
});
});
}
#[test]
#[should_panic(
expected = "Margin can only be used with floating behavior. SnackBarBehavior.fixed was \
set by default."
)]
fn a_snack_bar_fixed_by_default_asserts_against_a_margin() {
test_widgets(|tester| {
shows(tester, |snack_bar| snack_bar.margin(EdgeInsets::all(8.0)));
});
}
#[test]
#[should_panic(
expected = "Width can only be used with floating behavior. SnackBarBehavior.fixed was \
set in the SnackBar constructor."
)]
fn a_fixed_snack_bar_asserts_against_a_width() {
test_widgets(|tester| {
shows(tester, |snack_bar| {
snack_bar.behavior(SnackBarBehavior::Fixed).width(5.0)
});
});
}
#[test]
#[should_panic(
expected = "Width can only be used with floating behavior. SnackBarBehavior.fixed was \
set by default."
)]
fn a_snack_bar_fixed_by_default_asserts_against_a_width() {
test_widgets(|tester| {
shows(tester, |snack_bar| snack_bar.width(5.0));
});
}
#[test]
fn a_snack_bar_asserts_for_an_action_overflow_threshold_outside_0_to_1() {
for overflow_threshold in [-1.0, -0.0001, 1.000001, 5.0] {
let outcome = std::panic::catch_unwind(|| {
test_widgets(|tester| {
shows(tester, move |snack_bar| {
snack_bar
.action_overflow_threshold(overflow_threshold)
.behavior(SnackBarBehavior::Fixed)
});
});
});
let panic = outcome.expect_err("the threshold asserts");
let message = panic
.downcast_ref::<String>()
.cloned()
.or_else(|| {
panic
.downcast_ref::<&str>()
.map(|message| message.to_string())
})
.expect("a message");
assert_eq!(
message, "Action overflow threshold must be between 0 and 1 inclusive",
"{overflow_threshold}"
);
}
}
#[test]
fn a_snack_bar_s_close_button_has_a_tooltip() {
test_widgets(|tester| {
pump_app(
tester,
m2,
Scaffold::new()
.bottom_sheet(
SizedBox::new()
.width(200.0)
.height(50.0)
.child(waft_widgets::ColoredBox::new(Colors::PINK.primary())),
)
.into_widget(),
);
show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("Snackbar with close button"))
.duration(Duration::from_secs(365 * 24 * 60 * 60))
.action(SnackBarAction::new("ACTION", |_app| {}))
.behavior(SnackBarBehavior::Floating)
.show_close_icon(true),
); let close_button_label = {
let messenger = find::by_type::<ScaffoldMessenger>().evaluate(tester)[0];
let mut app = tester.cell.borrow_mut();
let context = messenger
.state_handle::<crate::scaffold::ScaffoldMessengerState>(&app)
.expect("a ScaffoldMessengerState")
.context(&app);
<dyn MaterialLocalizations>::of(&mut app, context).close_button_label()
};
let tooltips = find::by_type::<crate::tooltip::Tooltip>().evaluate(tester);
let with_label = tooltips
.into_iter()
.filter(|tooltip| {
tester.widget(*tooltip, |tooltip: &crate::tooltip::Tooltip| {
tooltip.message.as_deref() == Some(close_button_label.as_str())
})
})
.count();
assert_eq!(with_label, 1);
});
}
#[test]
#[should_panic(
expected = "ScaffoldMessenger.showSnackBar was called, but there are currently no \
descendant Scaffolds to present to."
)]
fn showing_a_snack_bar_with_no_scaffold_to_present_it_asserts() {
test_widgets(|tester| {
pump_app(tester, m3, waft_widgets::Center::new().into_widget());
let messenger = find::by_type::<ScaffoldMessenger>().evaluate(tester)[0];
let mut app = tester.cell.borrow_mut();
let state = messenger
.state_handle::<crate::scaffold::ScaffoldMessengerState>(&app)
.expect("a ScaffoldMessengerState");
state.show_snack_bar(&mut app, SnackBar::new(Text::new("SnackBar")), None);
});
}
fn action_material_color(tester: &WidgetTester) -> Option<Color> {
let action = find::text("ACTION").evaluate(tester)[0];
let button = tester.ancestor_of_type::<TextButton>(action);
let material = find::by_type::<Material>()
.evaluate(tester)
.into_iter()
.find(|material| tester.ancestors(*material).contains(&button))
.expect("a TextButton builds a Material");
tester.widget(material, |material: &Material| material.color)
}
fn action_background_before_and_after_press(
tester: &mut WidgetTester,
action: impl Fn() -> SnackBarAction + 'static,
) -> (Option<Color>, Option<Color>) {
let home = Scaffold::new().body(Builder::new({
let action = Rc::new(action);
move |_app, context| {
let action = Rc::clone(&action);
GestureDetector::new()
.on_tap(Listener::new(move |app| {
show(
app,
context,
SnackBar::new(Text::new("I am a snack bar."))
.duration(Duration::from_secs(2))
.action(action()),
);
}))
.child(Text::new("Tap"))
.into_widget()
}
}));
pump_app(tester, m3, home.into_widget());
tester.tap(find::text("Tap"));
tester.pump_and_settle();
let before = action_material_color(tester);
tester.tap(find::text("ACTION"));
tester.pump();
let after = action_material_color(tester);
(before, after)
}
#[test]
fn a_snack_bar_action_s_background_color_turns_transparent_once_pressed() {
test_widgets(|tester| {
let background_color = Colors::BLUE.primary();
let (before, after) = action_background_before_and_after_press(tester, move || {
SnackBarAction::new("ACTION", |_app| {}).background_color(background_color)
});
assert_eq!(before, Some(background_color));
assert_eq!(after, Some(Colors::TRANSPARENT));
});
}
#[test]
fn a_snack_bar_action_s_widget_state_background_color_resolves_by_state() {
test_widgets(|tester| {
let (before, after) = action_background_before_and_after_press(tester, || {
let background_color = WidgetStateColor::resolve_with(|states| {
if states.contains(&WidgetState::Disabled) {
return Colors::BLUE.primary();
}
Colors::PURPLE.primary()
});
SnackBarAction::new("ACTION", |_app| {}).background_color(background_color)
});
assert_eq!(before, Some(Colors::PURPLE.primary()));
assert_eq!(after, Some(Colors::BLUE.primary()));
});
}
#[test]
fn a_snack_bar_action_shows_its_disabled_background_color_once_pressed() {
test_widgets(|tester| {
let background_color = Colors::BLUE.primary();
let disabled_background_color = Colors::RED.primary();
let (before, after) = action_background_before_and_after_press(tester, move || {
SnackBarAction::new("ACTION", |_app| {})
.background_color(background_color)
.disabled_background_color(disabled_background_color)
});
assert_eq!(before, Some(background_color));
assert_eq!(after, Some(disabled_background_color));
});
}
#[test]
#[should_panic(
expected = "disabledBackgroundColor must not be provided when background color is a \
WidgetStateColor"
)]
fn a_widget_state_action_background_with_a_disabled_background_asserts() {
test_widgets(|tester| {
action_background_before_and_after_press(tester, || {
let background_color = WidgetStateColor::resolve_with(|states| {
if states.contains(&WidgetState::Disabled) {
return Colors::BLUE.primary();
}
Colors::PURPLE.primary()
});
SnackBarAction::new("ACTION", |_app| {})
.background_color(background_color)
.disabled_background_color(Colors::RED.primary())
});
});
}
#[test]
fn a_snack_bar_s_material_takes_its_clip_behavior() {
test_widgets(|tester| {
pump_app(
tester,
m3,
Scaffold::new()
.body(waft_widgets::Container::new())
.into_widget(),
);
let clip_behavior = |tester: &WidgetTester| {
tester.widget(snack_bar_material(tester), |material: &Material| {
material.clip_behavior
})
};
show_from_messenger_and_settle(tester, SnackBar::new(Text::new("I am a snack bar.")));
assert_eq!(clip_behavior(tester), Clip::HardEdge);
{
let messenger = find::by_type::<ScaffoldMessenger>().evaluate(tester)[0];
let mut app = tester.cell.borrow_mut();
let state = messenger
.state_handle::<crate::scaffold::ScaffoldMessengerState>(&app)
.expect("a ScaffoldMessengerState");
state.hide_current_snack_bar(&mut app, SnackBarClosedReason::Hide); }
tester.pump_and_settle(); show_from_messenger_and_settle(
tester,
SnackBar::new(Text::new("I am a snack bar.")).clip_behavior(Clip::AntiAlias),
); assert_eq!(clip_behavior(tester), Clip::AntiAlias);
});
}
fn taps_behind_the_snack_bar(
tester: &mut WidgetTester,
theme: impl FnOnce(&App) -> ThemeData,
snack_bar: impl Fn() -> SnackBar + 'static,
) -> bool {
const BUTTON_TEXT: &str = "Show snackbar";
const SNACKBAR_CONTENT: &str = "Snackbar";
const BUTTON_TEXT2: &str = "Try press me";
let completed = Rc::new(Cell::new(false));
tester.view().set_physical_size(Size::square(200.0));
tester.view().set_device_pixel_ratio(1.0);
let snack_bar = Rc::new(snack_bar);
let home = Scaffold::new().body(Builder::new({
let completed = Rc::clone(&completed);
move |_app, context| {
let snack_bar = Rc::clone(&snack_bar);
let completed = Rc::clone(&completed);
waft_widgets::Column::new()
.main_axis_alignment(MainAxisAlignment::SpaceBetween)
.cross_axis_alignment(waft_widgets::CrossAxisAlignment::Stretch)
.children([
crate::elevated_button::ElevatedButton::new(
Some(Listener::new(move |app| {
show(app, context, snack_bar());
})),
Some(Text::new(BUTTON_TEXT)),
)
.into_widget(),
crate::elevated_button::ElevatedButton::new(
Some(Listener::new(move |_app| completed.set(true))),
Some(Text::new(BUTTON_TEXT2)),
)
.into_widget(),
])
.into_widget()
}
}));
pump_app(tester, theme, home.into_widget());
tester.tap(find::text(BUTTON_TEXT));
tester.pump_and_settle();
expect(tester, &find::text(SNACKBAR_CONTENT), finds_one_widget());
let top_left = tester.get_rect(find::text(BUTTON_TEXT2)).top_left();
tester.tap_at(top_left, 1);
expect(tester, &find::text(SNACKBAR_CONTENT), finds_one_widget());
completed.get()
}
#[test]
fn a_button_beside_a_snack_bar_of_custom_width_can_be_tapped() {
test_widgets(|tester| {
assert!(taps_behind_the_snack_bar(tester, m3, || {
SnackBar::new(Text::new("Snackbar"))
.behavior(SnackBarBehavior::Floating)
.width(100.0)
}));
});
}
#[test]
fn a_button_in_a_snack_bar_s_margin_can_be_tapped() {
test_widgets(|tester| {
assert!(taps_behind_the_snack_bar(tester, m3, || {
SnackBar::new(Text::new("Snackbar"))
.behavior(SnackBarBehavior::Floating)
.margin(EdgeInsets::only(100.0, 0.0, 0.0, 0.0))
}));
});
}
#[test]
fn a_button_in_the_margin_of_an_opaque_snack_bar_cannot_be_tapped() {
test_widgets(|tester| {
assert!(!taps_behind_the_snack_bar(tester, m3, || {
SnackBar::new(Text::new("Snackbar"))
.hit_test_behavior(HitTestBehavior::Opaque)
.behavior(SnackBarBehavior::Floating)
.margin(EdgeInsets::only(100.0, 0.0, 0.0, 0.0))
}));
});
}
#[test]
fn a_button_in_the_theme_s_inset_padding_can_be_tapped() {
test_widgets(|tester| {
assert!(taps_behind_the_snack_bar(
tester,
|app| {
ThemeData::builder()
.snack_bar_theme(
SnackBarThemeData::new()
.inset_padding(EdgeInsets::only(100.0, 0.0, 0.0, 0.0)),
)
.build(app)
},
|| SnackBar::new(Text::new("Snackbar")).behavior(SnackBarBehavior::Floating),
));
});
}
#[test]
#[allow(deprecated)]
fn the_action_s_overlay_is_the_inverse_primary_colour() {
test_widgets(|tester| {
let home = x_scaffold(|app, context| {
show(
app,
context,
SnackBar::new(Text::new("I am a snack bar."))
.action(SnackBarAction::new("ACTION", |_app| {})),
);
});
pump_app(tester, m3, home.into_widget());
tester.tap(find::text("X"));
tester.pump_and_settle();
let theme = m3(&tester.cell.borrow());
let overlay_color = action_button_style(tester)
.overlay_color
.expect("an overlay color")
.resolve(&states(&[WidgetState::Hovered]));
assert_eq!(
overlay_color,
Some(theme.color_scheme.inverse_primary().with_opacity(0.08))
);
});
}
fn elevated_button_scaffold(snack_bar: impl Fn() -> SnackBar + 'static) -> Scaffold {
let snack_bar = Rc::new(snack_bar);
Scaffold::new().body(Builder::new(move |_app, context| {
let snack_bar = Rc::clone(&snack_bar);
crate::elevated_button::ElevatedButton::new(
Some(Listener::new(move |app| {
show(app, context, snack_bar());
})),
Some(Text::new("Show snackbar")),
)
.into_widget()
}))
}
#[test]
fn a_persisting_snack_bar_does_not_time_out() {
test_widgets(|tester| {
const SNACKBAR_CONTENT: &str = "Snackbar";
let home = elevated_button_scaffold(|| {
SnackBar::new(Text::new(SNACKBAR_CONTENT))
.duration(Duration::from_secs(1))
.persist(true)
.show_close_icon(true)
});
pump_app(tester, m3, home.into_widget());
tester.tap(find::text("Show snackbar"));
tester.pump_by(ms(750));
expect(tester, &find::text(SNACKBAR_CONTENT), finds_one_widget());
tester.pump_by(ms(1500));
tester.pump_and_settle();
expect(tester, &find::text(SNACKBAR_CONTENT), finds_one_widget());
let close = tester.element(find::by_icon(&Icons::close()).first());
let center = tester.get_rect(close).center();
tester.tap_at(center, 1);
tester.pump_and_settle();
expect(tester, &find::text(SNACKBAR_CONTENT), finds_nothing());
});
}
#[test]
fn a_snack_bar_that_does_not_persist_times_out_despite_its_close_icon() {
test_widgets(|tester| {
const SNACKBAR_CONTENT: &str = "SnackbarContent";
let home = elevated_button_scaffold(|| {
SnackBar::new(Text::new(SNACKBAR_CONTENT))
.duration(Duration::from_secs(1))
.persist(false)
.show_close_icon(true)
});
pump_app(tester, m3, home.into_widget());
let button = rect_of_type::<crate::elevated_button::ElevatedButton>(tester).center();
tester.tap_at(button, 1);
tester.pump();
tester.pump_by(ms(750));
expect(tester, &find::text(SNACKBAR_CONTENT), finds_one_widget());
tester.pump_by(ms(1500));
tester.pump_and_settle();
expect(tester, &find::text(SNACKBAR_CONTENT), finds_nothing());
});
}
#[test]
fn a_snack_bar_that_does_not_persist_times_out_under_accessible_navigation() {
test_widgets(|tester| {
const SNACKBAR_CONTENT: &str = "SnackbarContent";
let theme = m3(&tester.cell.borrow());
tester.pump_widget(
waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new().accessible_navigation(true),
material_app(
theme,
elevated_button_scaffold(|| {
SnackBar::new(Text::new(SNACKBAR_CONTENT))
.duration(Duration::from_secs(1))
.persist(false)
.action(SnackBarAction::new("Action", |_app| {}))
})
.into_widget(),
),
)
.into_widget(),
);
let button = rect_of_type::<crate::elevated_button::ElevatedButton>(tester).center();
tester.tap_at(button, 1);
tester.pump_and_settle();
expect(tester, &find::text(SNACKBAR_CONTENT), finds_one_widget());
tester.pump_by(Duration::from_secs(10));
tester.pump_and_settle();
expect(tester, &find::text(SNACKBAR_CONTENT), finds_nothing());
});
}
#[test]
fn a_snack_bar_action_lays_out_at_zero_area() {
test_widgets(|tester| {
pump_app(
tester,
m3,
waft_widgets::Center::new()
.child(SizedBox::shrink().child(SnackBarAction::new("X", |_app| {})))
.into_widget(),
);
assert_eq!(
rect_of_type::<SnackBarAction>(tester).size(),
waft_embedder::Size::ZERO
);
});
}
#[test]
fn the_action_overflow_measures_the_label_with_the_text_scaler() {
test_widgets(|tester| {
let build_snack_bar =
|tester: &mut WidgetTester, text_scaler: waft_painting::TextScaler| {
let home = waft_widgets::MediaQuery::new(
waft_widgets::MediaQueryData::new()
.size(waft_embedder::Size::new(800.0, 600.0))
.text_scaler(text_scaler),
Scaffold::new().body(Builder::new(|_app, context| {
GestureDetector::new()
.on_tap(Listener::new(move |app| {
show(
app,
context,
SnackBar::new(Text::new("Message"))
.action(SnackBarAction::new("Action Label", |_app| {})),
);
}))
.child(Text::new("Show"))
.into_widget()
})),
);
pump_app(tester, m3, home.into_widget());
};
build_snack_bar(tester, waft_painting::TextScaler::NO_SCALING);
tester.tap(find::text("Show"));
tester.pump_and_settle();
let action_top_left1 = tester.get_rect(find::text("Action Label")).top_left();
assert!(
action_top_left1.dy() <= tester.get_rect(find::text("Message")).bottom_left().dy()
);
{
let show = find::text("Show").evaluate(tester)[0];
let mut app = tester.cell.borrow_mut();
let context = show;
ScaffoldMessenger::of(&mut app, context).clear_snack_bars(&mut app);
}
tester.pump_and_settle();
build_snack_bar(tester, waft_painting::TextScaler::linear(3.0));
tester.tap(find::text("Show"));
tester.pump_and_settle();
let content_bottom_left2 = tester.get_rect(find::text("Message")).bottom_left();
let action_top_left2 = tester.get_rect(find::text("Action Label")).top_left();
assert!(action_top_left2.dy() >= content_bottom_left2.dy());
});
}
fn overflowing_action(tester: &mut WidgetTester, padding: Option<EdgeInsets>) -> Rect {
let content_key = GlobalKey::new();
tester.view().set_physical_size(Size::new(500.0, 800.0));
tester.view().set_device_pixel_ratio(1.0);
let home = Scaffold::new().body(Builder::new({
let content_key = content_key.clone();
move |_app, context| {
let content_key = content_key.clone();
GestureDetector::new()
.on_tap(Listener::new(move |app| {
let mut snack_bar = SnackBar::new(
SizedBox::new().key(content_key.clone().into()).height(20.0),
)
.action(SnackBarAction::new("Overflow Action", |_app| {}))
.action_overflow_threshold(0.1);
if let Some(padding) = padding {
snack_bar = snack_bar.padding(padding);
}
show(app, context, snack_bar);
}))
.child(Text::new("Show"))
.into_widget()
}
}));
pump_app(tester, m3, home.into_widget());
tester.tap(find::text("Show"));
tester.pump_and_settle();
rect_of_key(tester, &content_key)
}
fn content_row_children(tester: &WidgetTester) -> Vec<WidgetRef> {
let snack_bar = find::by_type::<SnackBar>().evaluate(tester)[0];
let row = find::by_type::<Row>()
.evaluate(tester)
.into_iter()
.find(|row| tester.ancestors(*row).contains(&snack_bar))
.expect("a SnackBar lays its content out in a Row");
tester.widget(row, |row: &Row| row.children.clone())
}
#[test]
fn an_overflowing_action_leaves_only_the_end_padding_beside_the_content() {
test_widgets(|tester| {
const SCREEN_WIDTH: f64 = 500.0;
let content = overflowing_action(tester, None);
assert_eq!(content.width(), 452.0);
assert_eq!(content.top_right().dx(), SCREEN_WIDTH - 24.0);
let children = content_row_children(tester);
assert_eq!(children.len(), 2);
assert!(waft_widgets::downcast_widget::<Expanded>(&*children[0]).is_some());
let spacer = waft_widgets::downcast_widget::<SizedBox>(&*children[1])
.expect("a SizedBox spacer");
assert_eq!(spacer.width, Some(24.0));
});
}
#[test]
fn an_overflowing_action_keeps_a_custom_padding_without_a_spacer() {
test_widgets(|tester| {
const SCREEN_WIDTH: f64 = 500.0;
let custom_padding = EdgeInsets::only(36.0, 0.0, 48.0, 0.0);
let content = overflowing_action(tester, Some(custom_padding));
let children = content_row_children(tester);
assert_eq!(children.len(), 1);
assert!(waft_widgets::downcast_widget::<Expanded>(&*children[0]).is_some());
assert_eq!(
content.width(),
SCREEN_WIDTH - (custom_padding.left + custom_padding.right)
);
assert_eq!(content.top_left().dx(), custom_padding.left);
assert_eq!(
content.top_right().dx(),
SCREEN_WIDTH - custom_padding.right
);
});
}
fn snack_bar_theme(show_close_icon: Option<bool>) -> SnackBarThemeData {
let mut theme = SnackBarThemeData::new()
.background_color(Colors::ORANGE.primary())
.action_text_color(Colors::GREEN.primary())
.content_text_style(waft_painting::TextStyle::new().color(Colors::BLUE.primary()))
.elevation(12.0)
.shape(waft_painting::BeveledRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::all(Radius::circular(12.0)),
));
if let Some(show_close_icon) = show_close_icon {
theme = theme.show_close_icon(show_close_icon);
}
theme
}
fn with_snack_bar_material<R>(tester: &WidgetTester, read: impl FnOnce(&Material) -> R) -> R {
tester.widget(snack_bar_material(tester), read)
}
fn paragraph_style(tester: &WidgetTester, text: &str) -> Option<waft_painting::TextStyle> {
let element = find::text(text).evaluate(tester)[0];
let render = tester.render_box_of(element);
let app = tester.cell.borrow();
let paragraph = render
.as_object()
.downcast::<waft_rendering::RenderParagraph>(&app)
.expect("a Text renders a paragraph");
paragraph.text(&app).style().cloned()
}
fn close_icons_in_snack_bar(tester: &WidgetTester) -> usize {
let snack_bar = find::by_type::<SnackBar>().evaluate(tester)[0];
find::by_type::<Icon>()
.evaluate(tester)
.into_iter()
.filter(|icon| {
tester.ancestors(*icon).contains(&snack_bar)
&& tester.widget(*icon, |icon: &Icon| icon.icon == Some(Icons::close()))
})
.count()
}
#[test]
fn a_material_2_snack_bar_with_no_theme_takes_the_defaults() {
test_widgets(|tester| {
const TEXT: &str = "I am a snack bar.";
let home = x_scaffold(|app, context| {
show(
app,
context,
SnackBar::new(Text::new(TEXT))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new("ACTION", |_app| {})),
);
});
pump_app(tester, m2, home.into_widget());
tester.tap(find::text("X"));
tester.pump_and_settle();
assert_eq!(
paragraph_style(tester, TEXT),
crate::typography::Typography::material2018()
.build()
.white
.title_medium
);
with_snack_bar_material(tester, |material| {
assert!(crate::test_support::is_same_color_as(
material.color.expect("a colour"),
Color::new(0xFF333333)
));
assert_eq!(material.elevation, 6.0);
assert!(material.shape.is_none());
});
});
}
#[test]
fn a_material_3_snack_bar_with_no_theme_takes_the_defaults() {
test_widgets(|tester| {
const TEXT: &str = "I am a snack bar.";
let home = x_scaffold(|app, context| {
show(
app,
context,
SnackBar::new(Text::new(TEXT))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new("ACTION", |_app| {})),
);
});
pump_app(tester, m3, home.into_widget());
tester.tap(find::text("X"));
tester.pump_and_settle();
let theme = m3(&tester.cell.borrow());
let typography = crate::typography::Typography::material2021().build();
let expected = typography
.english_like
.body_medium
.as_ref()
.map(|body_medium| body_medium.merge(typography.black.body_medium.as_ref()))
.map(|style| {
style
.copy_with()
.color(theme.color_scheme.on_inverse_surface())
.decoration_color(theme.color_scheme.on_surface())
});
assert_eq!(paragraph_style(tester, TEXT), expected);
with_snack_bar_material(tester, |material| {
assert_eq!(material.color, Some(theme.color_scheme.inverse_surface()));
assert_eq!(material.elevation, 6.0);
assert!(material.shape.is_none());
});
});
}
#[test]
fn a_snack_bar_takes_its_values_from_the_theme() {
test_widgets(|tester| {
const TEXT: &str = "I am a snack bar.";
const ACTION: &str = "ACTION";
let home = x_scaffold(|app, context| {
show(
app,
context,
SnackBar::new(Text::new(TEXT))
.duration(Duration::from_secs(2))
.action(SnackBarAction::new(ACTION, |_app| {})),
);
});
pump_app(
tester,
|app| {
ThemeData::builder()
.snack_bar_theme(snack_bar_theme(Some(true)))
.build(app)
},
home.into_widget(),
);
tester.tap(find::text("X"));
tester.pump_and_settle();
let theme = snack_bar_theme(Some(true));
assert_eq!(paragraph_style(tester, TEXT), theme.content_text_style);
with_snack_bar_material(tester, |material| {
assert_eq!(material.color, theme.background_color);
assert_eq!(Some(material.elevation), theme.elevation);
assert!(
material
.shape
.as_deref()
.expect("a shape")
.eq_shape(theme.shape.as_deref().expect("a shape"))
);
});
assert_eq!(
Some(tester.text_color(ACTION)),
theme.action_text_color.as_ref().map(AnyColor::color)
);
assert_eq!(close_icons_in_snack_bar(tester), 1);
});
}
fn widget_values_win(
tester: &mut WidgetTester,
theme: impl FnOnce(&App) -> ThemeData,
wrap: impl FnOnce(Scaffold) -> WidgetRef,
configure: impl Fn(SnackBar) -> SnackBar + 'static,
action: impl Fn() -> SnackBarAction + 'static,
) {
const SNACK_BAR_WIDTH: f64 = 400.0;
let background_color = Colors::PURPLE.primary();
let text_color = Colors::PINK.primary();
let elevation = 7.0;
let shape = || {
RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::all(Radius::circular(9.0)),
)
};
let home = x_scaffold(move |app, context| {
show(
app,
context,
configure(SnackBar::new(Text::new("I am a snack bar.")))
.duration(Duration::from_secs(2))
.action(action()),
);
});
pump_app(tester, theme, wrap(home));
tester.tap(find::text("X"));
tester.pump_and_settle();
with_snack_bar_material(tester, |material| {
assert_eq!(material.color, Some(background_color));
assert_eq!(material.elevation, elevation);
assert!(
material
.shape
.as_deref()
.expect("a shape")
.eq_shape(&shape())
);
});
assert_eq!(tester.text_color("ACTION"), text_color);
assert_eq!(close_icons_in_snack_bar(tester), 0);
assert_centred_with_width(tester, SNACK_BAR_WIDTH);
}
fn prioritized(snack_bar: SnackBar) -> SnackBar {
snack_bar
.background_color(Colors::PURPLE.primary())
.behavior(SnackBarBehavior::Floating)
.width(400.0)
.elevation(7.0)
.shape(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::all(Radius::circular(9.0)),
))
.show_close_icon(false)
}
#[test]
fn a_snack_bar_s_own_values_win_over_the_theme_s() {
test_widgets(|tester| {
widget_values_win(
tester,
|app| {
ThemeData::builder()
.snack_bar_theme(snack_bar_theme(Some(true)))
.build(app)
},
|scaffold| scaffold.into_widget(),
prioritized,
|| SnackBarAction::new("ACTION", |_app| {}).text_color(Colors::PINK.primary()),
);
});
}
#[test]
fn a_snack_bar_takes_its_values_from_a_local_snack_bar_theme() {
test_widgets(|tester| {
widget_values_win(
tester,
m3,
|scaffold| {
SnackBarTheme::new(
SnackBarThemeData::new()
.background_color(Colors::PURPLE.primary())
.behavior(SnackBarBehavior::Floating)
.width(400.0)
.elevation(7.0)
.shape(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::all(Radius::circular(9.0)),
))
.show_close_icon(false)
.action_text_color(Colors::PINK.primary()),
scaffold,
)
.into_widget()
},
|snack_bar| snack_bar,
|| SnackBarAction::new("ACTION", |_app| {}),
);
});
}
#[test]
fn a_snack_bar_s_own_values_win_over_a_local_snack_bar_theme_s() {
test_widgets(|tester| {
widget_values_win(
tester,
m3,
|scaffold| SnackBarTheme::new(snack_bar_theme(Some(true)), scaffold).into_widget(),
prioritized,
|| SnackBarAction::new("ACTION", |_app| {}).text_color(Colors::PINK.primary()),
);
});
}
fn themed_action_background_before_and_after_press(
tester: &mut WidgetTester,
theme: SnackBarThemeData,
action: impl Fn() -> SnackBarAction + 'static,
) -> (Option<Color>, Option<Color>) {
let home = x_scaffold(move |app, context| {
show(
app,
context,
SnackBar::new(Text::new("I am a snack bar.")).action(action()),
);
});
pump_app(
tester,
|app| ThemeData::builder().snack_bar_theme(theme).build(app),
home.into_widget(),
);
tester.tap(find::text("X"));
tester.pump_and_settle();
let before = action_material_color(tester);
tester.tap(find::text("ACTION"));
tester.pump();
let after = action_material_color(tester);
(before, after)
}
fn by_disabled(disabled: Color, enabled: Color) -> WidgetStateColor {
WidgetStateColor::resolve_with(move |states| {
if states.contains(&WidgetState::Disabled) {
return disabled;
}
enabled
})
}
#[test]
fn a_snack_bar_action_takes_the_theme_s_action_background_color() {
test_widgets(|tester| {
let (before, after) = themed_action_background_before_and_after_press(
tester,
SnackBarThemeData::new().action_background_color(by_disabled(
Colors::BLUE.primary(),
Colors::PURPLE.primary(),
)),
|| SnackBarAction::new("ACTION", |_app| {}),
);
assert_eq!(before, Some(Colors::PURPLE.primary()));
assert_eq!(after, Some(Colors::BLUE.primary()));
});
}
#[test]
fn a_snack_bar_action_s_background_color_wins_over_the_theme_s() {
test_widgets(|tester| {
let (before, after) = themed_action_background_before_and_after_press(
tester,
SnackBarThemeData::new().action_background_color(by_disabled(
Colors::BLUE.primary(),
Colors::PURPLE.primary(),
)),
|| {
SnackBarAction::new("ACTION", |_app| {}).background_color(by_disabled(
Colors::AMBER.primary(),
Colors::CYAN.primary(),
))
},
);
assert_eq!(before, Some(Colors::CYAN.primary()));
assert_eq!(after, Some(Colors::AMBER.primary()));
});
}
#[test]
#[should_panic(
expected = "disabledBackgroundColor must not be provided when background color is a \
WidgetStateColor"
)]
fn a_theme_with_a_widget_state_action_background_and_a_disabled_one_asserts() {
test_widgets(|tester| {
themed_action_background_before_and_after_press(
tester,
SnackBarThemeData::new()
.action_background_color(by_disabled(
Colors::BLUE.primary(),
Colors::PURPLE.primary(),
))
.disabled_action_background_color(Colors::AMBER.primary()),
|| SnackBarAction::new("ACTION", |_app| {}),
);
});
}
fn first_box_rect<W: 'static>(tester: &WidgetTester) -> Rect {
rect_of_type::<W>(tester)
}
fn themed_behavior(tester: &mut WidgetTester, behavior: SnackBarBehavior) {
let home = x_scaffold(|app, context| {
show(app, context, snack_bar_with_action());
})
.floating_action_button(fab_with_icon());
pump_app(
tester,
move |app| {
ThemeData::builder()
.snack_bar_theme(SnackBarThemeData::new().behavior(behavior))
.build(app)
},
home.into_widget(),
);
}
#[test]
fn a_theme_floating_snack_bar_sits_on_top_of_the_button() {
test_widgets(|tester| {
themed_behavior(tester, SnackBarBehavior::Floating);
tester.tap(find::text("X"));
tester.pump_and_settle();
let snack_bar_bottom_center = first_box_rect::<SnackBar>(tester).bottom_center();
let floating_action_button_top_center =
first_box_rect::<crate::floating_action_button::FloatingActionButton>(tester)
.top_center();
assert!(snack_bar_bottom_center.dy() == floating_action_button_top_center.dy());
});
}
#[test]
fn a_theme_fixed_snack_bar_pushes_the_button_above_it() {
test_widgets(|tester| {
themed_behavior(tester, SnackBarBehavior::Fixed);
let floating_action_button_origin_bottom_center =
first_box_rect::<crate::floating_action_button::FloatingActionButton>(tester)
.bottom_center();
tester.tap(find::text("X"));
tester.pump_and_settle();
let snack_bar_top_center = first_box_rect::<SnackBar>(tester).top_center();
let floating_action_button_bottom_center =
first_box_rect::<crate::floating_action_button::FloatingActionButton>(tester)
.bottom_center();
assert!(
floating_action_button_origin_bottom_center.dy()
> floating_action_button_bottom_center.dy()
);
assert!(snack_bar_top_center.dy() > floating_action_button_bottom_center.dy());
});
}
fn themed_and_shown(
tester: &mut WidgetTester,
behavior: SnackBarBehavior,
configure: fn(SnackBar) -> SnackBar,
) {
let home = x_scaffold(move |app, context| {
show(app, context, configure(snack_bar_with_action()));
})
.floating_action_button(fab_with_icon());
pump_app(
tester,
move |app| {
ThemeData::builder()
.snack_bar_theme(SnackBarThemeData::new().behavior(behavior))
.build(app)
},
home.into_widget(),
);
tap_x_and_settle_entrance(tester);
}
#[test]
fn a_theme_floating_snack_bar_takes_a_margin() {
test_widgets(|tester| {
themed_and_shown(tester, SnackBarBehavior::Floating, |snack_bar| {
snack_bar.margin(EdgeInsets::all(8.0))
});
});
}
#[test]
#[should_panic(
expected = "Margin can only be used with floating behavior. SnackBarBehavior.fixed was \
set by the inherited SnackBarThemeData."
)]
fn a_theme_fixed_snack_bar_asserts_against_a_margin() {
test_widgets(|tester| {
themed_and_shown(tester, SnackBarBehavior::Fixed, |snack_bar| {
snack_bar.margin(EdgeInsets::all(8.0))
});
});
}
#[test]
fn a_theme_floating_snack_bar_takes_a_width() {
test_widgets(|tester| {
themed_and_shown(tester, SnackBarBehavior::Floating, |snack_bar| {
snack_bar.width(5.0)
});
});
}
#[test]
#[should_panic(
expected = "Width can only be used with floating behavior. SnackBarBehavior.fixed was \
set by the inherited SnackBarThemeData."
)]
fn a_theme_fixed_snack_bar_asserts_against_a_width() {
test_widgets(|tester| {
themed_and_shown(tester, SnackBarBehavior::Fixed, |snack_bar| {
snack_bar.width(5.0)
});
});
}
#[test]
fn a_dismissed_snack_bar_frees_its_curved_animations() {
test_widgets(|tester| {
let home = x_scaffold(|app, context| {
show(
app,
context,
SnackBar::new(Text::new("Snack")).duration(Duration::from_secs(1)),
);
});
pump_app(tester, m3, home.into_widget());
tap_x_and_settle_entrance(tester);
let animations = {
let snack_bar = find::by_type::<SnackBar>().evaluate(tester)[0];
let app = tester.cell.borrow();
let state = app.get(
snack_bar
.state_handle::<SnackBarState>(&app)
.expect("a SnackBar is stateful"),
);
[
state.height_animation.clone(),
state.fade_in_animation.clone(),
state.fade_in_m3_animation.clone(),
state.fade_out_animation.clone(),
state.height_m3_animation.clone(),
]
.map(|animation| animation.expect("made in init_state").handle())
};
tester.pump_by(ms(1500)); tester.pump(); tester.pump_by(ms(750)); expect(tester, &find::by_type::<SnackBar>(), finds_nothing());
tester.cell.checkpoint();
let app = tester.cell.borrow();
assert!(animations.iter().all(|animation| !app.contains(*animation)));
});
}
}