File: test_qtawesome.py

package info (click to toggle)
python-qtawesome 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,948 kB
  • sloc: python: 1,693; makefile: 177; sh: 10
file content (139 lines) | stat: -rw-r--r-- 4,011 bytes parent folder | download
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
r"""
Tests for QtAwesome.
"""

# Standard library imports
import collections
import os
import subprocess
import sys

# Test Library imports
import pytest

# Local imports
from qtawesome.iconic_font import IconicFont
import qtawesome as qta


def test_segfault_import():
    output_number = subprocess.call(
        sys.executable + ' -c "import qtawesome ; qtawesome.icon()"', shell=True
    )
    assert output_number == 0


def test_unique_font_family_name(qtbot):
    """
    Test that each font used by qtawesome has a unique name. If this test
    fails, this probably means that you need to rename the family name of
    some fonts. Please see PR #98 for more details on why it is necessary and
    on how to do this.

    Regression test for Issue #107
    """
    resource = qta._instance()
    assert isinstance(resource, IconicFont)

    # Check that the fonts were loaded successfully.
    fontnames = resource.fontname.values()
    assert fontnames

    # Check that qtawesome does not load fonts with duplicate family names.
    duplicates = [
        fontname
        for fontname, count in collections.Counter(fontnames).items()
        if count > 1
    ]
    assert not duplicates


@pytest.mark.skipif(os.name != "nt", reason="Only meant for Windows")
def test_bundled_font_user_installation():
    """
    Test that the bundled fonts are being installed on Windows for current user.

    See spyder-ide/qtawesome#167 and spyder-ide/spyder#18642
    """
    qta._instance()
    fonts_expected = [
        font_filename
        for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
    ]
    fonts_command = [
        "powershell.exe",
        r'Get-ItemProperty "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"',
    ]
    fonts_result = (
        subprocess.run(fonts_command, capture_output=True, check=True, text=True)
        .stdout.replace("\n", "")
        .replace(" ", "")
    )
    for font_filename in fonts_expected:
        assert font_filename in fonts_result


def test_get_fonts_info():
    """
    Test that you can get the info of all the bundled fonts.
    """
    fonts_expected = [
        font_filename
        for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
    ]
    fonts_root_dir, fonts_list = qta.get_fonts_info()
    assert set(fonts_list) == set(fonts_expected)


@pytest.mark.skipif(os.name != "nt", reason="Only meant for Windows")
def test_bundled_font_system_installation():
    """
    Test that the bundled fonts can be installed on Windows for all users.

    See spyder-ide/qtawesome#244

    Notes
    -----
    * When running this test, it's possible that a prompt for privileges
      may appear (UAC prompt).
    """
    qta.install_bundled_fonts_system_wide()
    fonts_expected = [
        font_filename
        for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
    ]
    assert len(fonts_expected) == len(qta._BUNDLED_FONTS)
    fonts_command = [
        "powershell.exe",
        r'Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"',
    ]
    fonts_result = (
        subprocess.run(fonts_command, capture_output=True, check=True, text=True)
        .stdout.replace("\n", "")
        .replace(" ", "")
    )
    for font_filename in fonts_expected:
        assert font_filename in fonts_result


def test_font_load_from_system_fonts(monkeypatch):
    """
    Test that the bundled fonts can be accessed from the system fonts folder on
    Windows.

    Notes
    -----
    * This test ensures that the logic to load fonts from the system fonts
      only affects Windows even when it is being forced.
    * When running this test, it's possible that a prompt for privileges may
      appear (UAC prompt).
    """
    qta.install_bundled_fonts_system_wide()
    with monkeypatch.context() as m:
        qta._resource["iconic"] = None
        m.setenv("QTA_FORCE_SYSTEM_FONTS_LOAD", "true")
        qta._instance()


if __name__ == "__main__":
    pytest.main()