aboutsummaryrefslogtreecommitdiff
path: root/test/unit/test_dialog.py
blob: 63af79ee84a1443d746f5cbc621c277ab776d2d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# SPDX-License-Identifier: CC0-1.0

"""
Haketilo unit tests - showing an error/info/question dalog
"""

# This file is part of Haketilo
#
# Copyright (C) 2022, Wojtek Kosior <koszko@koszko.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the CC0 1.0 Universal License as published by
# the Creative Commons Corporation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# CC0 1.0 Universal License for more details.

import pytest

from ..extension_crafting import ExtraHTML
from ..script_loader import load_script

@pytest.mark.ext_data({
    'extra_html': ExtraHTML('html/dialog.html', {}),
    'navigate_to': 'html/dialog.html'
})
@pytest.mark.usefixtures('webextension')
def test_dialog_show_close(driver, execute_in_page):
    """
    A test case of basic dialog showing/closing.
    """
    execute_in_page(load_script('html/dialog.js'))
    buts = execute_in_page(
        '''
        let cb_calls, call_prom;
        const dialog_context = make(() => cb_calls.push("show"),
                                    () => cb_calls.push("hide"));
        document.body.append(dialog_context.main_div);
        const buts = {};
        for (const but of document.getElementsByTagName("button"))
            buts[but.textContent] = but;
        returnval(buts);
        ''')

    for i, (dialog_function, but_text, hidden, expected_result) in enumerate([
            ('info',   'Ok',  ['Yes', 'No'],       None),
            ('error',  'Ok',  ['Yes', 'No'],       None),
            ('error',  None,  ['Yes', 'No'],       None),
            ('loader', None,  ['Yes', 'No', 'Ok'], None),
            ('ask',    'Yes', ['Ok'],              True),
            ('ask',    None,  ['Ok'],              None),
            ('ask',    'No',  ['Ok'],              False)
    ]):
        cb_calls, is_shown = execute_in_page(
            f'''
            cb_calls = [];
            call_prom = {dialog_function}(dialog_context,
                                          `sample_text_${{arguments[0]}}`);
            returnval([cb_calls, dialog_context.shown]);
            ''',
            i)
        assert cb_calls == ['show']
        assert is_shown == True

        page_source = driver.page_source
        assert f'sample_text_{i}' in page_source
        assert f'sample_text_{i - 1}' not in page_source

        # Verify the right buttons are displayed.
        for text, but in buts.items():
            if text in hidden:
                assert not but.is_displayed()
                # Verify clicking a hidden button does nothing.
                execute_in_page('buts[arguments[0]].click();', text)
                assert execute_in_page('returnval(cb_calls);') == cb_calls
            else:
                assert but.is_displayed()

        if but_text is None:
            execute_in_page('close_dialog(dialog_context);')
        else:
            buts[but_text].click()

        cb_calls, result, is_shown = execute_in_page(
            '''{
            const values_cb = r => [cb_calls, r, dialog_context.shown];
            returnval(call_prom.then(values_cb));
            }''')
        assert cb_calls == ['show', 'hide']
        assert result == expected_result
        assert is_shown == False

@pytest.mark.ext_data({
    'extra_html': ExtraHTML('html/dialog.html', {}),
    'navigate_to': 'html/dialog.html'
})
@pytest.mark.usefixtures('webextension')
def test_dialog_queue(driver, execute_in_page):
    """
    A test case of queuing dialog display operations.
    """
    execute_in_page(load_script('html/dialog.js'))
    execute_in_page(
        '''
        let cb_calls = [], call_proms = [];
        const dialog_context = make(() => cb_calls.push("show"),
                                    () => cb_calls.push("hide"));
        document.body.append(dialog_context.main_div);
        ''')

    buts = driver.find_elements_by_tag_name('button')
    buts = dict([(but.text, but) for but in buts])

    for i in range(5):
        cb_calls, is_shown, msg_elem = execute_in_page(
            '''
            call_proms.push(ask(dialog_context, "somequestion" + arguments[0]));
            returnval([cb_calls, dialog_context.shown, dialog_context.msg]);
            ''',
            i)
        assert cb_calls == ['show']
        assert is_shown == True
        assert msg_elem.text == 'somequestion0'

    for i in range(5):
        buts['Yes' if i & 1 else 'No'].click()
        cb_calls, is_shown, msg_elem, result = execute_in_page(
            '''{
            const values_cb =
                r => [cb_calls, dialog_context.shown, dialog_context.msg, r];
            returnval(call_proms.splice(0, 1)[0].then(values_cb));
            }''')
        if i < 4:
            assert cb_calls == ['show']
            assert is_shown == True
            assert msg_elem.text == f'somequestion{i + 1}'
        else:
            assert cb_calls == ['show', 'hide']
            assert is_shown == False

        assert result == bool(i & 1)