1
//! Tab walks a transaction form in the order it is filled, and Shift+Tab
2
//! walks it back, at desktop and phone width.
3

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

            
7
use super::driver::{SHIFT_TAB, TAB};
8
use super::harness::{Harness, with_harness};
9
use super::page::{DESKTOP, PHONE, login, open};
10
use super::rows::{Split, fill_splits};
11

            
12
/// The control that has the focus: its name, or the text of a button.
13
const FOCUSED: &str = "const e = document.activeElement;\
14
     return ((e.name || '').replace('label:', '') || e.textContent.trim());";
15

            
16
/// The cells of the first split on screen, in reading order.
17
const READING_ORDER: &str = "const row = document.querySelector('[data-row=\"splits\"]');\
18
     const cells = [...row.querySelectorAll(':scope > label')].filter(l => l.offsetParent);\
19
     const at = (l) => l.getBoundingClientRect();\
20
     return cells.sort((a, b) => at(a).top - at(b).top || at(a).left - at(b).left)\
21
       .map(l => l.dataset.cell);";
22

            
23
const ADD_TAG: &str = "button[data-rows-for$=\"-tags\"]";
24

            
25
10
async fn focus(h: &Harness, script: &str) -> Result<()> {
26
10
    h.driver.run(script, vec![]).await.map(drop)
27
10
}
28

            
29
/// The controls reached by pressing `key` `count` times from the focus.
30
20
async fn walk(h: &Harness, key: &str, count: usize) -> Result<Vec<String>> {
31
20
    let mut stops = Vec::new();
32
20
    for _ in 0..count {
33
244
        let focused = h.driver.active().await?;
34
244
        h.driver.type_into(&focused, key).await?;
35
244
        let stop = h.driver.run(FOCUSED, vec![]).await?;
36
244
        stops.push(stop.as_str().unwrap_or_default().to_string());
37
    }
38
20
    Ok(stops)
39
20
}
40

            
41
20
fn names(list: &[&str]) -> Vec<String> {
42
20
    list.iter().map(ToString::to_string).collect()
43
20
}
44

            
45
8
async fn tab_through(
46
8
    h: &Harness,
47
8
    size: (u32, u32),
48
8
    splits: &[Split],
49
8
    forward: &[&str],
50
8
) -> Result<()> {
51
8
    walk_form(h, size, splits, false, forward).await
52
8
}
53

            
54
10
async fn walk_form(
55
10
    h: &Harness,
56
10
    size: (u32, u32),
57
10
    splits: &[Split],
58
10
    tagged: bool,
59
10
    forward: &[&str],
60
10
) -> Result<()> {
61
10
    login(h).await?;
62
10
    h.driver.set_size(size.0, size.1).await?;
63
10
    open(h, "/transaction/new").await?;
64
10
    fill_splits(h, splits).await?;
65
10
    if tagged {
66
2
        let add = h.driver.find(ADD_TAG).await?;
67
2
        h.driver.click(&add).await?;
68
8
    }
69
10
    focus(
70
10
        h,
71
10
        "document.querySelector('[name=\"note\"]').focus(); return true;",
72
10
    )
73
10
    .await?;
74
10
    let found = walk(h, TAB, forward.len()).await?;
75
10
    ensure!(found == names(forward), "Tab walked {found:#?}");
76
10
    let mut backward = names(forward);
77
10
    backward.pop();
78
10
    backward.insert(0, "note".into());
79
10
    backward.reverse();
80
10
    let back = walk(h, SHIFT_TAB, backward.len()).await?;
81
10
    ensure!(back == backward, "Shift+Tab walked {back:#?}");
82
10
    Ok(())
83
10
}
84

            
85
4
async fn reads_in_tab_order(h: &Harness, expected: &[&str]) -> Result<()> {
86
4
    let order = h.driver.run(READING_ORDER, vec![]).await?;
87
4
    ensure!(order == json!(expected), "the cells read {order}");
88
4
    Ok(())
89
4
}
90

            
91
const ONE: [&str; 9] = [
92
    "splits.0.from",
93
    "splits.0.to",
94
    "splits.0.from-commodity",
95
    "splits.0.value",
96
    "splits.0.to-commodity",
97
    "Add split tag",
98
    "Add tag",
99
    "Add split",
100
    "Create transaction",
101
];
102

            
103
2
async fn one_split(h: &Harness, size: (u32, u32)) -> Result<()> {
104
2
    let split = [Split::plain("Suica", "JR", "199")];
105
2
    tab_through(h, size, &split, &ONE).await?;
106
2
    reads_in_tab_order(
107
2
        h,
108
2
        &["from", "to", "from-commodity", "value", "to-commodity"],
109
2
    )
110
2
    .await
111
2
}
112

            
113
2
async fn converting(h: &Harness, size: (u32, u32)) -> Result<()> {
114
2
    let split = [Split {
115
2
        to_commodity: Some("EUR"),
116
2
        to_amount: Some("2"),
117
2
        ..Split::plain("Suica", "JR", "199")
118
2
    }];
119
2
    let forward = [
120
2
        "splits.0.from",
121
2
        "splits.0.to",
122
2
        "splits.0.from-commodity",
123
2
        "splits.0.value",
124
2
        "splits.0.to-commodity",
125
2
        "splits.0.to-amount",
126
2
        "Add split tag",
127
2
        "Add tag",
128
2
        "Add split",
129
2
        "Create transaction",
130
2
    ];
131
2
    tab_through(h, size, &split, &forward).await?;
132
2
    let order = [
133
2
        "from",
134
2
        "to",
135
2
        "from-commodity",
136
2
        "value",
137
2
        "to-commodity",
138
2
        "to-amount",
139
2
    ];
140
2
    reads_in_tab_order(h, &order).await
141
2
}
142

            
143
2
async fn two_splits(h: &Harness, size: (u32, u32)) -> Result<()> {
144
2
    let splits = [
145
2
        Split::plain("Suica", "JR", "199"),
146
2
        Split::plain("Suica", "McDonalds", "600"),
147
2
    ];
148
2
    let forward = [
149
2
        "splits.0.from",
150
2
        "splits.0.to",
151
2
        "splits.0.from-commodity",
152
2
        "splits.0.value",
153
2
        "splits.0.to-commodity",
154
2
        "Add split tag",
155
2
        "splits.1.from",
156
2
        "splits.1.to",
157
2
        "splits.1.from-commodity",
158
2
        "splits.1.value",
159
2
        "splits.1.to-commodity",
160
2
        "Add split tag",
161
2
        "Add tag",
162
2
        "Add split",
163
2
        "Create transaction",
164
2
    ];
165
2
    tab_through(h, size, &splits, &forward).await
166
2
}
167

            
168
2
async fn with_a_split_tag(h: &Harness, size: (u32, u32)) -> Result<()> {
169
2
    let split = [Split::plain("Suica", "JR", "199").tagged("to")];
170
2
    let forward = [
171
2
        "splits.0.from",
172
2
        "splits.0.to",
173
2
        "splits.0.from-commodity",
174
2
        "splits.0.value",
175
2
        "splits.0.to-commodity",
176
2
        "split-tags.0.name",
177
2
        "split-tags.0.value",
178
2
        "split-tags.0.description",
179
2
        "split-tags.0.side",
180
2
        "Remove split tag",
181
2
        "Add split tag",
182
2
        "Add tag",
183
2
        "Add split",
184
2
        "Create transaction",
185
2
    ];
186
2
    tab_through(h, size, &split, &forward).await
187
2
}
188

            
189
2
async fn with_a_transaction_tag(h: &Harness, size: (u32, u32)) -> Result<()> {
190
2
    let split = [Split::plain("Suica", "JR", "199")];
191
2
    let forward = [
192
2
        "tags.0.name",
193
2
        "tags.0.value",
194
2
        "tags.0.description",
195
2
        "Remove tag",
196
2
        "splits.0.from",
197
2
        "splits.0.to",
198
2
        "splits.0.from-commodity",
199
2
        "splits.0.value",
200
2
        "splits.0.to-commodity",
201
2
        "Add split tag",
202
2
        "Add tag",
203
2
        "Add split",
204
2
        "Create transaction",
205
2
    ];
206
2
    walk_form(h, size, &split, true, &forward).await
207
2
}
208

            
209
#[supp_macro::shared_runtime_test]
210
1
async fn desktop_tabs_a_transaction_tag_before_the_splits() -> Result<()> {
211
1
    with_harness(|h| Box::pin(with_a_transaction_tag(h, DESKTOP))).await
212
}
213

            
214
#[supp_macro::shared_runtime_test]
215
1
async fn phone_tabs_a_transaction_tag_before_the_splits() -> Result<()> {
216
1
    with_harness(|h| Box::pin(with_a_transaction_tag(h, PHONE))).await
217
}
218

            
219
#[supp_macro::shared_runtime_test]
220
1
async fn desktop_tabs_one_split_in_order() -> Result<()> {
221
1
    with_harness(|h| Box::pin(one_split(h, DESKTOP))).await
222
}
223

            
224
#[supp_macro::shared_runtime_test]
225
1
async fn phone_tabs_one_split_in_order() -> Result<()> {
226
1
    with_harness(|h| Box::pin(one_split(h, PHONE))).await
227
}
228

            
229
#[supp_macro::shared_runtime_test]
230
1
async fn desktop_tabs_to_the_converted_amount() -> Result<()> {
231
1
    with_harness(|h| Box::pin(converting(h, DESKTOP))).await
232
}
233

            
234
#[supp_macro::shared_runtime_test]
235
1
async fn phone_tabs_to_the_converted_amount() -> Result<()> {
236
1
    with_harness(|h| Box::pin(converting(h, PHONE))).await
237
}
238

            
239
#[supp_macro::shared_runtime_test]
240
1
async fn desktop_tabs_two_splits_and_back() -> Result<()> {
241
1
    with_harness(|h| Box::pin(two_splits(h, DESKTOP))).await
242
}
243

            
244
#[supp_macro::shared_runtime_test]
245
1
async fn phone_tabs_two_splits_and_back() -> Result<()> {
246
1
    with_harness(|h| Box::pin(two_splits(h, PHONE))).await
247
}
248

            
249
#[supp_macro::shared_runtime_test]
250
1
async fn desktop_tabs_a_split_tag_before_its_add_button() -> Result<()> {
251
1
    with_harness(|h| Box::pin(with_a_split_tag(h, DESKTOP))).await
252
}
253

            
254
#[supp_macro::shared_runtime_test]
255
1
async fn phone_tabs_a_split_tag_before_its_add_button() -> Result<()> {
256
1
    with_harness(|h| Box::pin(with_a_split_tag(h, PHONE))).await
257
}