Completed
Branch master (6f2cba)
by Gonzalo
01:09
created

enabled_qcombobox_subclass()   A

Complexity

Conditions 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 15
rs 9.4285
1
import os
2
import sys
3
import contextlib
4
5
from qtpy import QtWidgets
6
from qtpy.QtWidgets import QComboBox
7
from qtpy.uic import loadUi
8
9
10
QCOMBOBOX_SUBCLASS = """
11
from qtpy.QtWidgets import QComboBox
12
class _QComboBoxSubclass(QComboBox):
13
    pass
14
"""
15
16
@contextlib.contextmanager
17
def enabled_qcombobox_subclass(tmpdir):
18
    """
19
    Context manager that sets up a temporary module with a QComboBox subclass
20
    and then removes it once we are done.
21
    """
22
23
    with open(tmpdir.join('qcombobox_subclass.py').strpath, 'w') as f:
24
        f.write(QCOMBOBOX_SUBCLASS)
25
26
    sys.path.insert(0, tmpdir.strpath)
27
28
    yield
29
30
    sys.path.pop(0)
31
32
33
def get_qapp(icon_path=None):
34
    """
35
    Helper function to return a QApplication instance
36
    """
37
    qapp = QtWidgets.QApplication.instance()
38
    if qapp is None:
39
        qapp = QtWidgets.QApplication([''])
40
    return qapp
41
42
43
def test_load_ui():
44
    """
45
    Make sure that the patched loadUi function behaves as expected with a
46
    simple .ui file.
47
    """
48
    app = get_qapp()
49
    ui = loadUi(os.path.join(os.path.dirname(__file__), 'test.ui'))
50
    assert isinstance(ui.pushButton, QtWidgets.QPushButton)
51
    assert isinstance(ui.comboBox, QComboBox)
52
53
54
def test_load_ui_custom_auto(tmpdir):
55
    """
56
    Test that we can load a .ui file with custom widgets without having to
57
    explicitly specify a dictionary of custom widgets, even in the case of
58
    PySide.
59
    """
60
61
    app = get_qapp()
62
63
    with enabled_qcombobox_subclass(tmpdir):
64
        from qcombobox_subclass import _QComboBoxSubclass
65
        ui = loadUi(os.path.join(os.path.dirname(__file__), 'test_custom.ui'))
66
67
    assert isinstance(ui.pushButton, QtWidgets.QPushButton)
68
    assert isinstance(ui.comboBox, _QComboBoxSubclass)
69