1
//! On a short screen, where an on-screen keyboard leaves little room, the
2
//! suggestion list opens inside the visible area and still picks by click.
3

            
4
use anyhow::{Result, ensure};
5
use serde_json::{Value, json};
6

            
7
use super::driver::ARROW_DOWN;
8
use super::harness::{Harness, with_harness};
9
use super::page::{PHONE, click_suggestion, login, open, reference, visible, wait_for};
10

            
11
const SHORT: u32 = 480;
12

            
13
/// Scrolls the page until the bottom of the From control sits just above
14
/// the bottom of the visible area.
15
const TO_BOTTOM: &str = "const i = document.querySelector('input[name=\"label:splits.0.from\"]');\
16
     const v = window.visualViewport;\
17
     window.scrollBy(0, i.getBoundingClientRect().bottom - (v.offsetTop + v.height - 20));\
18
     return true;";
19

            
20
/// The open list's extent and the visible area, once the list shows.
21
const EXTENTS: &str = "const p = document.querySelector('.autocomplete-results');\
22
     if (!p || !p.querySelector('.autocomplete-item')) return null;\
23
     const r = p.getBoundingClientRect(); const v = window.visualViewport;\
24
     return [r.top, r.bottom, v.offsetTop, v.offsetTop + v.height];";
25

            
26
4
fn number(extents: &Value, at: usize) -> f64 {
27
4
    extents[at].as_f64().unwrap_or(f64::NAN)
28
4
}
29

            
30
1
async fn list_inside_the_view(h: &Harness) -> Result<()> {
31
1
    login(h).await?;
32
1
    h.driver.set_size(PHONE.0, SHORT).await?;
33
1
    open(h, "/transaction/new").await?;
34
1
    h.driver.run(TO_BOTTOM, vec![]).await?;
35
1
    let from = visible(h, "splits.0.from").await?;
36
1
    h.driver.click(&from).await?;
37
1
    h.driver.type_into(&from, "Suica").await?;
38
1
    let extents = wait_for(&h.driver, EXTENTS, vec![], "the Suica suggestion").await?;
39
4
    let [top, bottom, view_top, view_bottom] = [0, 1, 2, 3].map(|at| number(&extents, at));
40
1
    ensure!(
41
1
        top >= view_top && bottom <= view_bottom,
42
        "the list spans {top}..{bottom}, outside the visible {view_top}..{view_bottom}"
43
    );
44
1
    click_suggestion(h, 0).await?;
45
1
    let (shown, id) = reference(h, "splits.0.from").await?;
46
1
    ensure!(
47
1
        shown == "Suica" && id == h.account("Suica").to_string(),
48
        "the click picked {shown:?} {id:?}"
49
    );
50
1
    ensure!(
51
1
        h.driver
52
1
            .run(
53
1
                "return document.querySelector('.autocomplete-item');",
54
1
                vec![]
55
1
            )
56
1
            .await?
57
1
            == json!(null),
58
        "the list stays open after the pick"
59
    );
60
1
    Ok(())
61
1
}
62

            
63
#[supp_macro::shared_runtime_test]
64
1
async fn a_list_near_the_bottom_opens_inside_the_visible_area() -> Result<()> {
65
1
    with_harness(|h| Box::pin(list_inside_the_view(h))).await
66
}
67

            
68
/// Tags the open list's items so a rebuilt node shows.
69
const TAG_ITEMS: &str = "[...document.querySelectorAll('.autocomplete-item')]\
70
     .forEach((i, n) => { i.__kept = n; });\
71
     return true;";
72

            
73
/// Scrolls the page, then reports whether every item is still its tagged node.
74
const SCROLL_KEEPS_ITEMS: &str = "const done = arguments[arguments.length - 1];\
75
     const count = document.querySelectorAll('.autocomplete-item').length;\
76
     window.scrollBy(0, 40);\
77
     setTimeout(() => {\
78
       const now = [...document.querySelectorAll('.autocomplete-item')];\
79
       done(count > 0 && now.length === count && now.every((i, n) => i.__kept === n));\
80
     }, 300);";
81

            
82
/// Presses the first item, scrolls the page, then releases the press.
83
const PRESS_THEN_SCROLL: &str = "const item = document.querySelector('.autocomplete-item');\
84
     const fire = (name) => item.dispatchEvent(new MouseEvent(name, { bubbles: true, cancelable: true, button: 0 }));\
85
     const done = arguments[arguments.length - 1];\
86
     fire('pointerdown');\
87
     window.scrollBy(0, 40);\
88
     setTimeout(() => { fire('pointerup'); done(true); }, 300);";
89

            
90
2
async fn open_short_list(h: &Harness) -> Result<()> {
91
2
    login(h).await?;
92
2
    h.driver.set_size(PHONE.0, SHORT).await?;
93
2
    open(h, "/transaction/new").await?;
94
2
    h.driver.run(TO_BOTTOM, vec![]).await?;
95
2
    let from = visible(h, "splits.0.from").await?;
96
2
    h.driver.click(&from).await?;
97
2
    h.driver.type_into(&from, "Suica").await?;
98
2
    wait_for(&h.driver, EXTENTS, vec![], "the Suica suggestion")
99
2
        .await
100
2
        .map(drop)
101
2
}
102

            
103
1
async fn refit_keeps_items(h: &Harness) -> Result<()> {
104
1
    open_short_list(h).await?;
105
1
    h.driver.run(TAG_ITEMS, vec![]).await?;
106
1
    let from = visible(h, "splits.0.from").await?;
107
1
    h.driver.type_into(&from, ARROW_DOWN).await?;
108
1
    let kept = h.driver.run_async(SCROLL_KEEPS_ITEMS, vec![]).await?;
109
1
    ensure!(
110
1
        kept == json!(true),
111
        "a scroll rebuilt the list items: {kept}"
112
    );
113
1
    Ok(())
114
1
}
115

            
116
1
async fn press_survives_scroll(h: &Harness) -> Result<()> {
117
1
    open_short_list(h).await?;
118
1
    h.driver.run_async(PRESS_THEN_SCROLL, vec![]).await?;
119
1
    let (shown, id) = reference(h, "splits.0.from").await?;
120
1
    ensure!(
121
1
        shown == "Suica" && id == h.account("Suica").to_string(),
122
        "the press picked {shown:?} {id:?}"
123
    );
124
1
    Ok(())
125
1
}
126

            
127
#[supp_macro::shared_runtime_test]
128
1
async fn a_scroll_never_rebuilds_the_list_items() -> Result<()> {
129
1
    with_harness(|h| Box::pin(refit_keeps_items(h))).await
130
}
131

            
132
#[supp_macro::shared_runtime_test]
133
1
async fn a_press_in_progress_survives_a_scroll() -> Result<()> {
134
1
    with_harness(|h| Box::pin(press_survives_scroll(h))).await
135
}