1
//! A press on the `[■]` of a dialog closes it on release, as Cancel does.
2

            
3
use super::click_tests::{now, press};
4
use super::*;
5
use crate::form::Form;
6
use crate::hits::Target;
7
use crate::modal::ConfirmAction;
8
use crate::test_support::redraw;
9

            
10
const LEFT: MouseButton = MouseButton::Left;
11

            
12
/// The middle of the `[■]` of the dialog on top, drawn after any window's.
13
7
fn middle(app: &App) -> Position {
14
7
    let found = app
15
7
        .hits
16
7
        .entries()
17
7
        .into_iter()
18
49
        .rfind(|(_, target)| *target == Target::Close);
19
7
    let (area, _) = found.expect("a close icon was drawn");
20
7
    Position::new(area.x + area.width / 2, area.y)
21
7
}
22

            
23
4
fn press_close(app: &mut App) {
24
4
    redraw(app);
25
4
    let at = middle(app);
26
4
    press(app, at, MouseEventKind::Down(LEFT), now());
27
4
}
28

            
29
3
fn dialog_app() -> App {
30
3
    let mut app = crate::test_support::app();
31
3
    crate::test_support::open_overlay(&mut app);
32
3
    redraw(&mut app);
33
3
    app
34
3
}
35

            
36
#[test]
37
1
fn a_release_on_the_close_icon_closes_the_dialog() {
38
1
    let mut app = dialog_app();
39
1
    press_close(&mut app);
40
1
    assert!(app.overlays.top().is_some());
41
1
    let at = middle(&app);
42
1
    press(&mut app, at, MouseEventKind::Up(LEFT), now());
43
1
    assert!(app.overlays.is_empty());
44
1
}
45

            
46
#[test]
47
1
fn a_release_off_the_close_icon_does_nothing() {
48
1
    let mut app = dialog_app();
49
1
    press_close(&mut app);
50
1
    press(
51
1
        &mut app,
52
1
        Position::new(0, 0),
53
1
        MouseEventKind::Up(LEFT),
54
1
        now(),
55
1
    );
56
1
    assert!(app.overlays.top().is_some());
57
1
}
58

            
59
#[test]
60
1
fn the_close_icon_of_a_confirm_cancels_it_and_leaves_the_form_below_open() {
61
1
    let mut app = crate::test_support::app();
62
1
    app.overlays.push(Modal::Form(Form::config_set()));
63
1
    app.overlays.push(Modal::Confirm {
64
1
        prompt: "Discard changes?".to_string(),
65
1
        action: ConfirmAction::DiscardForm,
66
1
        focus: None,
67
1
    });
68
1
    press_close(&mut app);
69
1
    let at = middle(&app);
70
1
    press(&mut app, at, MouseEventKind::Up(LEFT), now());
71
1
    assert_eq!(app.overlays.len(), 1);
72
1
    assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
73
1
}
74

            
75
#[test]
76
1
fn an_overlay_opened_between_the_press_and_the_release_keeps_the_dialog() {
77
1
    let mut app = dialog_app();
78
1
    press_close(&mut app);
79
1
    let at = middle(&app);
80
1
    app.overlays.push(Modal::Form(Form::config_set()));
81
1
    press(&mut app, at, MouseEventKind::Up(LEFT), now());
82
1
    assert_eq!(app.overlays.len(), 2);
83
1
}