Debian Codegraph @

qtawesome/tests/test_qtawesome.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 r"""
2 Tests for QtAwesome.
3 """
4
5 # Standard library imports
6 import collections
7 import os
8 import subprocess
9 import sys
10
11 # Test Library imports
12 import pytest
13
14 # Local imports
15 from qtawesome.iconic_font import IconicFont
16 import qtawesome as qta
17
18
19 def test_segfault_import():
20 output_number = subprocess.call(
21 sys.executable + ' -c "import qtawesome ; qtawesome.icon()"', shell=True
22 )
23 assert output_number == 0
24
25
26 def test_unique_font_family_name(qtbot):
27 """
28 Test that each font used by qtawesome has a unique name. If this test
29 fails, this probably means that you need to rename the family name of
30 some fonts. Please see PR #98 for more details on why it is necessary and
31 on how to do this.
32
33 Regression test for Issue #107
34 """
35 resource = qta._instance()
36 assert isinstance(resource, IconicFont)
37
38 # Check that the fonts were loaded successfully.
39 fontnames = resource.fontname.values()
40 assert fontnames
41
42 # Check that qtawesome does not load fonts with duplicate family names.
43 duplicates = [
44 fontname
45 for fontname, count in collections.Counter(fontnames).items()
46 if count > 1
47 ]
48 assert not duplicates
49
50
51 @pytest.mark.skipif(os.name != "nt", reason="Only meant for Windows")
52 def test_bundled_font_user_installation():
53 """
54 Test that the bundled fonts are being installed on Windows for current user.
55
56 See spyder-ide/qtawesome#167 and spyder-ide/spyder#18642
57 """
58 qta._instance()
59 fonts_expected = [
60 font_filename
61 for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
62 ]
63 fonts_command = [
64 "powershell.exe",
65 r'Get-ItemProperty "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"',
66 ]
67 fonts_result = (
68 subprocess.run(fonts_command, capture_output=True, check=True, text=True)
69 .stdout.replace("\n", "")
70 .replace(" ", "")
71 )
72 for font_filename in fonts_expected:
73 assert font_filename in fonts_result
74
75
76 def test_get_fonts_info():
77 """
78 Test that you can get the info of all the bundled fonts.
79 """
80 fonts_expected = [
81 font_filename
82 for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
83 ]
84 fonts_root_dir, fonts_list = qta.get_fonts_info()
85 assert os.path.normcase(fonts_root_dir) == os.path.normcase(
86 os.path.join(
87 os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "fonts"
88 )
89 )
90 assert set(fonts_list) == set(fonts_expected)
91
92
93 @pytest.mark.skipif(os.name != "nt", reason="Only meant for Windows")
94 def test_bundled_font_system_installation():
95 """
96 Test that the bundled fonts can be installed on Windows for all users.
97
98 See spyder-ide/qtawesome#244
99
100 Notes
101 -----
102 * When running this test, it's possible that a prompt for privileges
103 may appear (UAC prompt).
104 """
105 qta.install_bundled_fonts_system_wide()
106 fonts_expected = [
107 font_filename
108 for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
109 ]
110 assert len(fonts_expected) == len(qta._BUNDLED_FONTS)
111 fonts_command = [
112 "powershell.exe",
113 r'Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"',
114 ]
115 fonts_result = (
116 subprocess.run(fonts_command, capture_output=True, check=True, text=True)
117 .stdout.replace("\n", "")
118 .replace(" ", "")
119 )
120 for font_filename in fonts_expected:
121 assert font_filename in fonts_result
122
123
124 def test_font_load_from_system_fonts(monkeypatch):
125 """
126 Test that the bundled fonts can be accessed from the system fonts folder on
127 Windows.
128
129 Notes
130 -----
131 * This test ensures that the logic to load fonts from the system fonts
132 only affects Windows even when it is being forced.
133 * When running this test, it's possible that a prompt for privileges may
134 appear (UAC prompt).
135 """
136 qta.install_bundled_fonts_system_wide()
137 with monkeypatch.context() as m:
138 qta._resource["iconic"] = None
139 m.setenv("QTA_FORCE_SYSTEM_FONTS_LOAD", "true")
140 qta._instance()
141
142
143 def test_the_app_exits_gracefully_after_drawing_a_path():
144 """
145 Test that the app finishes gracefully when the `draw` parameter is `path`.
146
147 See spyder-ide/qtawesome#280
148
149 Notes
150 -----
151 * This `draw="path"` mode causes the icon to be drawn via `QRawFont`.
152 The font is cached for every `QThread`.
153 The cache is cleared when the current thread is over.
154 If the thread is the main one, this may cause a segfault when the app ends.
155 * The issue does not crash the app immediately,
156 but causes a segfault at the end only.
157 So, we start a new app for it to end within the test.
158 """
159 ret_code = subprocess.call(
160 [
161 sys.executable,
162 "-c",
163 ";".join(
164 (
165 "from qtpy.QtWidgets import QApplication",
166 "from qtawesome import IconWidget",
167 "app = QApplication([])",
168 "IconWidget('fa5s.egg', draw='path')",
169 )
170 ),
171 ],
172 )
173 assert ret_code == 0
174
175
176 if __name__ == "__main__":
177 pytest.main()