Lines
92.86 %
Functions
100 %
Branches
//! On a short screen, where an on-screen keyboard leaves little room, the
//! suggestion list opens inside the visible area and still picks by click.
use anyhow::{Result, ensure};
use serde_json::{Value, json};
use super::driver::ARROW_DOWN;
use super::harness::{Harness, with_harness};
use super::page::{PHONE, click_suggestion, login, open, reference, visible, wait_for};
const SHORT: u32 = 480;
/// Scrolls the page until the bottom of the From control sits just above
/// the bottom of the visible area.
const TO_BOTTOM: &str = "const i = document.querySelector('input[name=\"label:splits.0.from\"]');\
const v = window.visualViewport;\
window.scrollBy(0, i.getBoundingClientRect().bottom - (v.offsetTop + v.height - 20));\
return true;";
/// The open list's extent and the visible area, once the list shows.
const EXTENTS: &str = "const p = document.querySelector('.autocomplete-results');\
if (!p || !p.querySelector('.autocomplete-item')) return null;\
const r = p.getBoundingClientRect(); const v = window.visualViewport;\
return [r.top, r.bottom, v.offsetTop, v.offsetTop + v.height];";
fn number(extents: &Value, at: usize) -> f64 {
extents[at].as_f64().unwrap_or(f64::NAN)
}
async fn list_inside_the_view(h: &Harness) -> Result<()> {
login(h).await?;
h.driver.set_size(PHONE.0, SHORT).await?;
open(h, "/transaction/new").await?;
h.driver.run(TO_BOTTOM, vec![]).await?;
let from = visible(h, "splits.0.from").await?;
h.driver.click(&from).await?;
h.driver.type_into(&from, "Suica").await?;
let extents = wait_for(&h.driver, EXTENTS, vec![], "the Suica suggestion").await?;
let [top, bottom, view_top, view_bottom] = [0, 1, 2, 3].map(|at| number(&extents, at));
ensure!(
top >= view_top && bottom <= view_bottom,
"the list spans {top}..{bottom}, outside the visible {view_top}..{view_bottom}"
);
click_suggestion(h, 0).await?;
let (shown, id) = reference(h, "splits.0.from").await?;
shown == "Suica" && id == h.account("Suica").to_string(),
"the click picked {shown:?} {id:?}"
h.driver
.run(
"return document.querySelector('.autocomplete-item');",
vec![]
)
.await?
== json!(null),
"the list stays open after the pick"
Ok(())
#[supp_macro::shared_runtime_test]
async fn a_list_near_the_bottom_opens_inside_the_visible_area() -> Result<()> {
with_harness(|h| Box::pin(list_inside_the_view(h))).await
/// Tags the open list's items so a rebuilt node shows.
const TAG_ITEMS: &str = "[...document.querySelectorAll('.autocomplete-item')]\
.forEach((i, n) => { i.__kept = n; });\
/// Scrolls the page, then reports whether every item is still its tagged node.
const SCROLL_KEEPS_ITEMS: &str = "const done = arguments[arguments.length - 1];\
const count = document.querySelectorAll('.autocomplete-item').length;\
window.scrollBy(0, 40);\
setTimeout(() => {\
const now = [...document.querySelectorAll('.autocomplete-item')];\
done(count > 0 && now.length === count && now.every((i, n) => i.__kept === n));\
}, 300);";
/// Presses the first item, scrolls the page, then releases the press.
const PRESS_THEN_SCROLL: &str = "const item = document.querySelector('.autocomplete-item');\
const fire = (name) => item.dispatchEvent(new MouseEvent(name, { bubbles: true, cancelable: true, button: 0 }));\
const done = arguments[arguments.length - 1];\
fire('pointerdown');\
setTimeout(() => { fire('pointerup'); done(true); }, 300);";
async fn open_short_list(h: &Harness) -> Result<()> {
wait_for(&h.driver, EXTENTS, vec![], "the Suica suggestion")
.await
.map(drop)
async fn refit_keeps_items(h: &Harness) -> Result<()> {
open_short_list(h).await?;
h.driver.run(TAG_ITEMS, vec![]).await?;
h.driver.type_into(&from, ARROW_DOWN).await?;
let kept = h.driver.run_async(SCROLL_KEEPS_ITEMS, vec![]).await?;
kept == json!(true),
"a scroll rebuilt the list items: {kept}"
async fn press_survives_scroll(h: &Harness) -> Result<()> {
h.driver.run_async(PRESS_THEN_SCROLL, vec![]).await?;
"the press picked {shown:?} {id:?}"
async fn a_scroll_never_rebuilds_the_list_items() -> Result<()> {
with_harness(|h| Box::pin(refit_keeps_items(h))).await
async fn a_press_in_progress_survives_a_scroll() -> Result<()> {
with_harness(|h| Box::pin(press_survives_scroll(h))).await