1# META: timeout=long
2
3import pytest
4
5from tests.support.asserts import assert_dialog_handled, assert_error, assert_success
6
7
8def get_title(session):
9    return session.transport.send(
10        "GET", "session/{session_id}/title".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("<title>Foo</title>")
17        expected_title = session.title
18
19        create_dialog(dialog_type, text=dialog_type)
20
21        response = get_title(session)
22        assert_success(response, expected_title)
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, inline):
31    def check_user_prompt_closed_with_exception(dialog_type, retval):
32        session.url = inline("<title>Foo</title>")
33
34        create_dialog(dialog_type, text=dialog_type)
35
36        response = get_title(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, inline):
46    def check_user_prompt_not_closed_but_exception(dialog_type):
47        session.url = inline("<title>Foo</title>")
48
49        create_dialog(dialog_type, text=dialog_type)
50
51        response = get_title(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
114
115# The behavior of the `window.print` function is platform-dependent and may not
116# trigger the creation of a dialog at all. Therefore, this test should only be
117# run in contexts that support the dialog (a condition that may not be
118# determined automatically).
119# def test_title_with_non_simple_dialog(session, inline):
120#    document = "<title>With non-simple dialog</title><h2>Hello</h2>"
121#    spawn = """
122#        var done = arguments[0];
123#        setTimeout(function() {
124#            done();
125#        }, 0);
126#        setTimeout(function() {
127#            window['print']();
128#        }, 0);
129#    """
130#    session.url = inline(document)
131#    session.execute_async_script(spawn)
132#
133#    result = get_title(session)
134#    assert_error(result, "unexpected alert open")
135