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