Debian Codegraph @

qtawesome/tests/test_qtawesome.py

raw symbols ◀ ▶ SCIP
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 set(fonts_list) == set(fonts_expected)
86
87
88 @pytest.mark.skipif(os.name != "nt", reason="Only meant for Windows")
89 def test_bundled_font_system_installation():
90 """
91 Test that the bundled fonts can be installed on Windows for all users.
92
93 See spyder-ide/qtawesome#244
94
95 Notes
96 -----
97 * When running this test, it's possible that a prompt for privileges
98 may appear (UAC prompt).
99 """
100 qta.install_bundled_fonts_system_wide()
101 fonts_expected = [
102 font_filename
103 for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
104 ]
105 assert len(fonts_expected) == len(qta._BUNDLED_FONTS)
106 fonts_command = [
107 "powershell.exe",
108 r'Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"',
109 ]
110 fonts_result = (
111 subprocess.run(fonts_command, capture_output=True, check=True, text=True)
112 .stdout.replace("\n", "")
113 .replace(" ", "")
114 )
115 for font_filename in fonts_expected:
116 assert font_filename in fonts_result
117
118
119 def test_font_load_from_system_fonts(monkeypatch):
120 """
121 Test that the bundled fonts can be accessed from the system fonts folder on
122 Windows.
123
124 Notes
125 -----
126 * This test ensures that the logic to load fonts from the system fonts
127 only affects Windows even when it is being forced.
128 * When running this test, it's possible that a prompt for privileges may
129 appear (UAC prompt).
130 """
131 qta.install_bundled_fonts_system_wide()
132 with monkeypatch.context() as m:
133 qta._resource["iconic"] = None
134 m.setenv("QTA_FORCE_SYSTEM_FONTS_LOAD", "true")
135 qta._instance()
136
137
138 def test_the_app_exits_gracefully_after_drawing_a_path():
139 """
140 Test that the app finishes gracefully when the `draw` parameter is `path`.
141
142 See spyder-ide/qtawesome#280
143
144 Notes
145 -----
146 * This `draw="path"` mode causes the icon to be drawn via `QRawFont`.
147 The font is cached for every `QThread`.
148 The cache is cleared when the current thread is over.
149 If the thread is the main one, this may cause a segfault when the app ends.
150 * The issue does not crash the app immediately,
151 but causes a segfault at the end only.
152 So, we start a new app for it to end within the test.
153 """
154 ret_code = subprocess.call(
155 [
156 sys.executable,
157 "-c",
158 ";".join(
159 (
160 "from qtpy.QtWidgets import QApplication",
161 "from qtawesome import IconWidget",
162 "app = QApplication([])",
163 "IconWidget('fa5s.egg', draw='path')",
164 )
165 ),
166 ],
167 )
168 assert ret_code == 0
169
170
171 if __name__ == "__main__":
172 pytest.main()