1
|
# SPDX-License-Identifier: CC0-1.0
|
2
|
|
3
|
"""
|
4
|
Haketilo unit tests - showing an error/info/question dalog
|
5
|
"""
|
6
|
|
7
|
# This file is part of Haketilo
|
8
|
#
|
9
|
# Copyright (C) 2022, Wojtek Kosior <koszko@koszko.org>
|
10
|
#
|
11
|
# This program is free software: you can redistribute it and/or modify
|
12
|
# it under the terms of the CC0 1.0 Universal License as published by
|
13
|
# the Creative Commons Corporation.
|
14
|
#
|
15
|
# This program is distributed in the hope that it will be useful,
|
16
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
# CC0 1.0 Universal License for more details.
|
19
|
|
20
|
import pytest
|
21
|
|
22
|
from ..extension_crafting import ExtraHTML
|
23
|
from ..script_loader import load_script
|
24
|
|
25
|
@pytest.mark.ext_data({
|
26
|
'extra_html': ExtraHTML('html/dialog.html', {}),
|
27
|
'navigate_to': 'html/dialog.html'
|
28
|
})
|
29
|
@pytest.mark.usefixtures('webextension')
|
30
|
def test_dialog_show_close(driver, execute_in_page):
|
31
|
"""
|
32
|
A test case of basic dialog showing/closing.
|
33
|
"""
|
34
|
execute_in_page(load_script('html/dialog.js'))
|
35
|
buts = execute_in_page(
|
36
|
'''
|
37
|
let cb_calls, call_prom;
|
38
|
const dialog_context = make(() => cb_calls.push("show"),
|
39
|
() => cb_calls.push("hide"));
|
40
|
document.body.append(dialog_context.main_div);
|
41
|
const buts = {};
|
42
|
for (const but of document.getElementsByTagName("button"))
|
43
|
buts[but.textContent] = but;
|
44
|
returnval(buts);
|
45
|
''')
|
46
|
|
47
|
for i, (dialog_function, but_text, hidden, expected_result) in enumerate([
|
48
|
('info', 'Ok', ['Yes', 'No'], None),
|
49
|
('error', 'Ok', ['Yes', 'No'], None),
|
50
|
('error', None, ['Yes', 'No'], None),
|
51
|
('loader', None, ['Yes', 'No', 'Ok'], None),
|
52
|
('ask', 'Yes', ['Ok'], True),
|
53
|
('ask', None, ['Ok'], None),
|
54
|
('ask', 'No', ['Ok'], False)
|
55
|
]):
|
56
|
cb_calls, is_shown = execute_in_page(
|
57
|
f'''
|
58
|
cb_calls = [];
|
59
|
call_prom = {dialog_function}(dialog_context,
|
60
|
`sample_text_${{arguments[0]}}`);
|
61
|
returnval([cb_calls, dialog_context.shown]);
|
62
|
''',
|
63
|
i)
|
64
|
assert cb_calls == ['show']
|
65
|
assert is_shown == True
|
66
|
|
67
|
page_source = driver.page_source
|
68
|
assert f'sample_text_{i}' in page_source
|
69
|
assert f'sample_text_{i - 1}' not in page_source
|
70
|
|
71
|
# Verify the right buttons are displayed.
|
72
|
for text, but in buts.items():
|
73
|
if text in hidden:
|
74
|
assert not but.is_displayed()
|
75
|
# Verify clicking a hidden button does nothing.
|
76
|
execute_in_page('buts[arguments[0]].click();', text)
|
77
|
assert execute_in_page('returnval(cb_calls);') == cb_calls
|
78
|
else:
|
79
|
assert but.is_displayed()
|
80
|
|
81
|
if but_text is None:
|
82
|
execute_in_page('close_dialog(dialog_context);')
|
83
|
else:
|
84
|
buts[but_text].click()
|
85
|
|
86
|
cb_calls, result, is_shown = execute_in_page(
|
87
|
'''{
|
88
|
const values_cb = r => [cb_calls, r, dialog_context.shown];
|
89
|
returnval(call_prom.then(values_cb));
|
90
|
}''')
|
91
|
assert cb_calls == ['show', 'hide']
|
92
|
assert result == expected_result
|
93
|
assert is_shown == False
|
94
|
|
95
|
@pytest.mark.ext_data({
|
96
|
'extra_html': ExtraHTML('html/dialog.html', {}),
|
97
|
'navigate_to': 'html/dialog.html'
|
98
|
})
|
99
|
@pytest.mark.usefixtures('webextension')
|
100
|
def test_dialog_queue(driver, execute_in_page):
|
101
|
"""
|
102
|
A test case of queuing dialog display operations.
|
103
|
"""
|
104
|
execute_in_page(load_script('html/dialog.js'))
|
105
|
execute_in_page(
|
106
|
'''
|
107
|
let cb_calls = [], call_proms = [];
|
108
|
const dialog_context = make(() => cb_calls.push("show"),
|
109
|
() => cb_calls.push("hide"));
|
110
|
document.body.append(dialog_context.main_div);
|
111
|
''')
|
112
|
|
113
|
buts = driver.find_elements_by_tag_name('button')
|
114
|
buts = dict([(but.text, but) for but in buts])
|
115
|
|
116
|
for i in range(5):
|
117
|
cb_calls, is_shown, msg_elem = execute_in_page(
|
118
|
'''
|
119
|
call_proms.push(ask(dialog_context, "somequestion" + arguments[0]));
|
120
|
returnval([cb_calls, dialog_context.shown, dialog_context.msg]);
|
121
|
''',
|
122
|
i)
|
123
|
assert cb_calls == ['show']
|
124
|
assert is_shown == True
|
125
|
assert msg_elem.text == 'somequestion0'
|
126
|
|
127
|
for i in range(5):
|
128
|
buts['Yes' if i & 1 else 'No'].click()
|
129
|
cb_calls, is_shown, msg_elem, result = execute_in_page(
|
130
|
'''{
|
131
|
const values_cb =
|
132
|
r => [cb_calls, dialog_context.shown, dialog_context.msg, r];
|
133
|
returnval(call_proms.splice(0, 1)[0].then(values_cb));
|
134
|
}''')
|
135
|
if i < 4:
|
136
|
assert cb_calls == ['show']
|
137
|
assert is_shown == True
|
138
|
assert msg_elem.text == f'somequestion{i + 1}'
|
139
|
else:
|
140
|
assert cb_calls == ['show', 'hide']
|
141
|
assert is_shown == False
|
142
|
|
143
|
assert result == bool(i & 1)
|