1# META: timeout=long
2from base64 import decodebytes
3
4import pytest
5
6from tests.support.asserts import assert_dialog_handled, assert_error, assert_success
7from .printcmd import do_print, assert_pdf
8
9
10@pytest.fixture
11def check_user_prompt_closed_without_exception(session, create_dialog, inline):
12    def check_user_prompt_closed_without_exception(dialog_type, retval):
13        session.url = inline("<input/>")
14
15        create_dialog(dialog_type, text=dialog_type)
16
17        response = do_print(session, {})
18        value = assert_success(response)
19
20        pdf = decodebytes(value.encode())
21        assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval)
22
23        assert_pdf(pdf)
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("<input/>")
32
33        create_dialog(dialog_type, text=dialog_type)
34
35        response = do_print(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("<input/>")
47
48        create_dialog(dialog_type, text=dialog_type)
49
50        response = do_print(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