use freya::prelude::*;
use freya_testing::prelude::*;
#[test]
pub fn scroll_view_wheel() {
fn scroll_view_wheel_app() -> impl IntoElement {
ScrollView::new()
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
}
let mut test = launch_test(scroll_view_wheel_app);
let scrollview = test
.find(|node, element| {
Rect::try_downcast(element)
.filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
.map(move |_| node)
})
.unwrap();
let content = scrollview.children()[0].children()[0].children();
assert!(content[0].is_visible()); assert!(content[1].is_visible()); assert!(content[2].is_visible()); assert!(!content[3].is_visible());
test.scroll((5., 5.), (0., -300.));
assert!(!content[0].is_visible()); assert!(content[1].is_visible()); assert!(content[2].is_visible()); assert!(content[3].is_visible()); }
#[test]
pub fn scroll_view_scrollbar() {
fn scroll_view_scrollbar_app() -> impl IntoElement {
ScrollView::new()
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
}
let mut test = launch_test(scroll_view_scrollbar_app);
let scrollview = test
.find(|node, element| {
Rect::try_downcast(element)
.filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
.map(move |_| node)
})
.unwrap();
let content = scrollview.children()[0].children()[0].children();
assert!(content[0].is_visible()); assert!(content[1].is_visible()); assert!(content[2].is_visible()); assert!(!content[3].is_visible());
test.move_cursor((495., 20.));
test.sync_and_update();
test.press_cursor((495., 20.));
test.sync_and_update();
test.move_cursor((495., 320.));
test.sync_and_update();
test.release_cursor((495., 320.));
test.sync_and_update();
assert!(!content[0].is_visible()); assert!(content[1].is_visible()); assert!(content[2].is_visible()); assert!(content[3].is_visible());
for _ in 0..5 {
test.press_key(Key::Named(NamedKey::ArrowUp));
}
assert!(content[0].is_visible());
assert!(content[1].is_visible());
assert!(content[2].is_visible());
assert!(!content[3].is_visible());
test.press_key(Key::Named(NamedKey::End));
assert!(!content[0].is_visible());
assert!(content[1].is_visible());
assert!(content[2].is_visible());
assert!(content[3].is_visible());
}
#[test]
pub fn scroll_view_drag_scrolling() {
fn scroll_view_drag_scrolling_app() -> impl IntoElement {
ScrollView::new()
.drag_scrolling(true)
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
}
let mut test = launch_test(scroll_view_drag_scrolling_app);
let scrollview = test
.find(|node, element| {
Rect::try_downcast(element)
.filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
.map(move |_| node)
})
.unwrap();
let content = scrollview.children()[0].children()[0].children();
assert!(content[0].is_visible());
assert!(content[1].is_visible());
assert!(content[2].is_visible());
assert!(!content[3].is_visible());
test.press_touch((100., 400.));
test.move_touch((100., 100.));
test.release_touch((100., 100.));
assert!(!content[0].is_visible());
assert!(content[1].is_visible());
assert!(content[2].is_visible());
assert!(content[3].is_visible());
}
#[test]
pub fn scroll_view_drag_scrolling_release_stops() {
fn scroll_view_drag_release_app() -> impl IntoElement {
ScrollView::new()
.drag_scrolling(true)
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
}
let mut test = launch_test(scroll_view_drag_release_app);
let scrollview = test
.find(|node, element| {
Rect::try_downcast(element)
.filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
.map(move |_| node)
})
.unwrap();
let content = scrollview.children()[0].children()[0].children();
test.press_touch((100., 300.));
test.move_touch((100., 200.));
test.release_touch((100., 200.));
assert!(content[0].is_visible());
assert!(content[1].is_visible());
assert!(content[2].is_visible());
assert!(!content[3].is_visible());
test.move_touch((100., 50.));
assert!(content[0].is_visible());
assert!(content[1].is_visible());
assert!(content[2].is_visible());
assert!(!content[3].is_visible());
}
#[test]
pub fn scroll_view_drag_scrolling_horizontal() {
fn scroll_view_drag_horizontal_app() -> impl IntoElement {
ScrollView::new()
.drag_scrolling(true)
.direction(Direction::Horizontal)
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
.child(rect().height(Size::px(200.)).width(Size::px(200.)))
}
let mut test = launch_test(scroll_view_drag_horizontal_app);
let scrollview = test
.find(|node, element| {
Rect::try_downcast(element)
.filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
.map(move |_| node)
})
.unwrap();
let content = scrollview.children()[0].children()[0].children();
assert!(content[0].is_visible());
assert!(content[1].is_visible());
assert!(content[2].is_visible());
assert!(!content[3].is_visible());
test.press_touch((400., 100.));
test.move_touch((100., 100.));
test.release_touch((100., 100.));
assert!(!content[0].is_visible());
assert!(content[1].is_visible());
assert!(content[2].is_visible());
assert!(content[3].is_visible());
}
#[test]
pub fn scroll_view_scrollbar_smaller_than_thumb() {
fn small_scroll_view_app() -> impl IntoElement {
rect()
.height(Size::px(48.))
.width(Size::px(200.))
.child(ScrollView::new().children((0..30).map(|i| {
label()
.key(i)
.height(Size::px(20.))
.text(format!("Row {i}"))
})))
}
let mut test = launch_test(small_scroll_view_app);
test.sync_and_update();
test.move_cursor((100., 24.));
test.sync_and_update();
test.scroll((100., 24.), (0., -20.));
test.sync_and_update();
let scrollview = test
.find(|node, element| {
Rect::try_downcast(element)
.filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
.map(move |_| node)
})
.unwrap();
let scrolled_before = scrollview.children()[0].children()[0].children()[0]
.layout()
.area
.min_y();
test.move_cursor((192., 5.));
test.sync_and_update();
test.press_cursor((192., 5.));
test.sync_and_update();
test.move_cursor((192., 30.));
test.sync_and_update();
test.release_cursor((192., 30.));
test.sync_and_update();
let scrolled_after = scrollview.children()[0].children()[0].children()[0]
.layout()
.area
.min_y();
assert_eq!(scrolled_before, scrolled_after);
}