1# META: timeout=long
2
3import pytest
4
5from tests.support.asserts import assert_dialog_handled, assert_error, assert_success
6from tests.support.inline import inline
7
8
9def get_current_url(session):
10    return session.transport.send("GET", "session/%s/url" % session.session_id)
11
12
13@pytest.fixture
14def check_user_prompt_closed_without_exception(session, create_dialog):
15    def check_user_prompt_closed_without_exception(dialog_type, retval):
16        session.url = inline("<p id=1>")
17        expected_url = session.url
18
19        create_dialog(dialog_type, text=dialog_type)
20
21        response = get_current_url(session)
22        assert_success(response, expected_url)
23
24        assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval)
25
26    return check_user_prompt_closed_without_exception
27
28
29@pytest.fixture
30def check_user_prompt_closed_with_exception(session, create_dialog):
31    def check_user_prompt_closed_with_exception(dialog_type, retval):
32        session.url = inline("<p id=1>")
33
34        create_dialog(dialog_type, text=dialog_type)
35
36        response = get_current_url(session)
37        assert_error(response, "unexpected alert open")
38
39        assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval)
40
41    return check_user_prompt_closed_with_exception
42
43
44@pytest.fixture
45def check_user_prompt_not_closed_but_exception(session, create_dialog):
46    def check_user_prompt_not_closed_but_exception(dialog_type):
47        session.url = inline("<p id=1>")
48
49        create_dialog(dialog_type, text=dialog_type)
50
51        response = get_current_url(session)
52        assert_error(response, "unexpected alert open")
53
54        assert session.alert.text == dialog_type
55        session.alert.dismiss()
56
57    return check_user_prompt_not_closed_but_exception
58
59
60@pytest.mark.capabilities({"unhandledPromptBehavior": "accept"})
61@pytest.mark.parametrize("dialog_type, retval", [
62    ("alert", None),
63    ("confirm", True),
64    ("prompt", ""),
65])
66def test_accept(check_user_prompt_closed_without_exception, dialog_type, retval):
67    check_user_prompt_closed_without_exception(dialog_type, retval)
68
69
70@pytest.mark.capabilities({"unhandledPromptBehavior": "accept and notify"})
71@pytest.mark.parametrize("dialog_type, retval", [
72    ("alert", None),
73    ("confirm", True),
74    ("prompt", ""),
75])
76def test_accept_and_notify(check_user_prompt_closed_with_exception, dialog_type, retval):
77    check_user_prompt_closed_with_exception(dialog_type, retval)
78
79
80@pytest.mark.capabilities({"unhandledPromptBehavior": "dismiss"})
81@pytest.mark.parametrize("dialog_type, retval", [
82    ("alert", None),
83    ("confirm", False),
84    ("prompt", None),
85])
86def test_dismiss(check_user_prompt_closed_without_exception, dialog_type, retval):
87    check_user_prompt_closed_without_exception(dialog_type, retval)
88
89
90@pytest.mark.capabilities({"unhandledPromptBehavior": "dismiss and notify"})
91@pytest.mark.parametrize("dialog_type, retval", [
92    ("alert", None),
93    ("confirm", False),
94    ("prompt", None),
95])
96def test_dismiss_and_notify(check_user_prompt_closed_with_exception, dialog_type, retval):
97    check_user_prompt_closed_with_exception(dialog_type, retval)
98
99
100@pytest.mark.capabilities({"unhandledPromptBehavior": "ignore"})
101@pytest.mark.parametrize("dialog_type", ["alert", "confirm", "prompt"])
102def test_ignore(check_user_prompt_not_closed_but_exception, dialog_type):
103    check_user_prompt_not_closed_but_exception(dialog_type)
104
105
106@pytest.mark.parametrize("dialog_type, retval", [
107    ("alert", None),
108    ("confirm", False),
109    ("prompt", None),
110])
111def test_default(check_user_prompt_closed_with_exception, dialog_type, retval):
112    check_user_prompt_closed_with_exception(dialog_type, retval)
113