use waft_foundation::App;
use waft_widgets::{BuildContext, Localizations, LookupBoundary, downcast_widget};
use crate::material::Material;
use crate::material_localizations::MaterialLocalizations;
use crate::scaffold::{Scaffold, ScaffoldMessenger};
pub fn debug_check_has_material(app: &App, context: BuildContext) -> bool {
if cfg!(debug_assertions)
&& LookupBoundary::find_ancestor_widget_of_exact_type::<Material>(app, context).is_none()
{
let hidden_by_boundary =
LookupBoundary::debug_is_hiding_ancestor_widget_of_exact_type::<Material>(app, context);
panic!(
"No Material widget found{}.{}\n{} widgets require a Material widget ancestor \
within the closest LookupBoundary.\nIn Material Design, most widgets are \
conceptually \"printed\" on a sheet of material. In Flutter's material library, \
that material is represented by the Material widget. It is the Material widget \
that renders ink splashes, for instance. Because of this, many material library \
widgets require that there be a Material widget in the tree above them.\nTo \
introduce a Material widget, you can either directly include one, or use a widget \
that contains Material itself, such as a Card, Dialog, Drawer, or Scaffold.",
if hidden_by_boundary {
" within the closest LookupBoundary"
} else {
""
},
if hidden_by_boundary {
"\nThere is an ancestor Material widget, but it is hidden by a LookupBoundary."
} else {
""
},
context.widget(app).type_name(),
);
}
true
}
pub fn debug_check_has_material_localizations(app: &mut App, context: BuildContext) -> bool {
if cfg!(debug_assertions)
&& Localizations::of::<dyn MaterialLocalizations>(app, context).is_none()
{
panic!(
"No MaterialLocalizations found.\n{} widgets require MaterialLocalizations to be \
provided by a Localizations widget ancestor.\nThe material library uses \
Localizations to generate messages, labels, and abbreviations.\nTo introduce a \
MaterialLocalizations, either use a MaterialApp at the root of your application to \
include them automatically, or add a Localization widget with a \
MaterialLocalizations delegate.",
context.widget(app).type_name(),
);
}
true
}
pub fn debug_check_has_scaffold(app: &App, context: BuildContext) -> bool {
if cfg!(debug_assertions)
&& downcast_widget::<Scaffold>(&**context.widget(app)).is_none()
&& context
.find_ancestor_widget_of_exact_type::<Scaffold>(app)
.is_none()
{
panic!(
"No Scaffold widget found.\n{} widgets require a Scaffold widget ancestor.\n\
Typically, the Scaffold widget is introduced by the MaterialApp or WidgetsApp \
widget at the top of your application widget tree.",
context.widget(app).type_name(),
);
}
true
}
pub fn debug_check_has_scaffold_messenger(app: &App, context: BuildContext) -> bool {
if cfg!(debug_assertions)
&& context
.find_ancestor_widget_of_exact_type::<ScaffoldMessenger>(app)
.is_none()
{
panic!(
"No ScaffoldMessenger widget found.\n{} widgets require a ScaffoldMessenger \
widget ancestor.\nTypically, the ScaffoldMessenger widget is introduced by the \
MaterialApp at the top of your application widget tree.",
context.widget(app).type_name(),
);
}
true
}
#[cfg(test)]
mod tests {
use waft_embedder::TextDirection;
use waft_widgets::{Directionality, IntoWidget, LookupBoundary, WidgetRef};
use waft_widgets::testing::{WidgetTester, test_widgets};
use super::*;
use crate::drawer_theme::tests::probe;
use crate::material_localizations::tests::us_english;
fn panic_message(panic: Box<dyn std::any::Any + Send>) -> String {
panic
.downcast_ref::<String>()
.cloned()
.or_else(|| panic.downcast_ref::<&str>().map(|text| text.to_string()))
.unwrap_or_default()
}
fn checked(
tester: &mut WidgetTester,
wrap: impl FnOnce(WidgetRef) -> WidgetRef + 'static,
) -> Result<bool, String> {
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
probe(
tester,
move |_app, probe| {
Directionality::new(TextDirection::Ltr, wrap(probe)).into_widget()
},
|app, context| debug_check_has_material(app, context),
)
}));
outcome.map_err(panic_message)
}
#[test]
fn debug_check_has_material_passes_under_a_material() {
test_widgets(|tester| {
let found = checked(tester, |probe| Material::new().child(probe).into_widget());
assert_eq!(found, Ok(true));
});
}
#[test]
fn debug_check_has_material_reports_a_missing_material() {
test_widgets(|tester| {
let message = checked(tester, |probe| probe).expect_err("there is no Material");
assert!(
message.starts_with("No Material widget found."),
"{message}"
);
assert!(
message.contains(
"widgets require a Material widget ancestor within the closest LookupBoundary."
),
"{message}"
);
assert!(
message.contains(
"To introduce a Material widget, you can either directly include one, or use a \
widget that contains Material itself, such as a Card, Dialog, Drawer, or Scaffold."
),
"{message}"
);
});
}
#[test]
fn debug_check_has_material_reports_a_material_hidden_by_a_lookup_boundary() {
test_widgets(|tester| {
let message = checked(tester, |probe| {
Material::new()
.child(LookupBoundary::new(probe))
.into_widget()
})
.expect_err("the boundary hides the Material");
assert!(
message.starts_with("No Material widget found within the closest LookupBoundary."),
"{message}"
);
assert!(
message.contains(
"There is an ancestor Material widget, but it is hidden by a LookupBoundary."
),
"{message}"
);
});
}
#[test]
fn debug_check_has_material_localizations_reports_missing_localizations() {
test_widgets(|tester| {
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
probe(
tester,
|_app, probe| probe,
debug_check_has_material_localizations,
)
}));
let message = panic_message(outcome.expect_err("there are no localizations"));
assert!(
message.starts_with("No MaterialLocalizations found."),
"{message}"
);
assert!(
message.contains(
"widgets require MaterialLocalizations to be provided by a Localizations widget \
ancestor."
),
"{message}"
);
assert!(
message.contains(
"The material library uses Localizations to generate messages, labels, and \
abbreviations."
),
"{message}"
);
});
}
#[test]
fn debug_check_has_scaffold_reports_a_missing_scaffold() {
test_widgets(|_| {
let mut tester = WidgetTester::on(waft_embedder::TargetPlatform::Android);
let home = waft_widgets::Builder::new(|app, context| {
crate::bottom_sheet::ShowBottomSheet::new(std::rc::Rc::new(|_app, _context| {
waft_widgets::Container::new().into_widget()
}))
.show(app, context);
waft_widgets::Container::new().into_widget()
})
.into_widget();
let theme = crate::theme_data::ThemeData::new(&tester.cell.borrow());
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
tester.pump_widget(crate::test_support::material_app(theme, home));
}));
let message = panic_message(outcome.expect_err("there is no Scaffold"));
assert!(
message.starts_with(
"No Scaffold widget found.\nBuilder widgets require a Scaffold widget \
ancestor.\n"
),
"{message}"
);
assert!(
message.ends_with(
"Typically, the Scaffold widget is introduced by the MaterialApp or \
WidgetsApp widget at the top of your application widget tree."
),
"{message}"
);
});
}
#[test]
fn debug_check_has_material_localizations_passes_under_material_localizations() {
test_widgets(|tester| {
let found = probe(
tester,
|_app, probe| us_english(probe),
debug_check_has_material_localizations,
);
assert!(found);
});
}
}