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