source: sasview/src/sas/qtgui/UnitTesting/TestUtils.py @ 99f8760

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_sync_sascalc
Last change on this file since 99f8760 was 53c771e, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Converted unit tests

  • Property mode set to 100755
File size: 1.8 KB
Line 
1from PyQt5.QtCore import *
2from PyQt5.QtGui import *
3from PyQt5.QtWidgets import *
4from PyQt5.QtTest import *
5import inspect
6
7def WarningTestNotImplemented(method_name=None):
8    """
9    Prints warning about a non-implemented test.
10    Test name retrieved from stack trace.
11    """
12    if method_name is not None:
13        print(("\nWARNING: %s needs implementing!"%method_name))
14    else:
15        (frame, filename, line_number,
16            function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[1]
17        print(("\nWARNING: %s needs implementing!"%function_name))
18
19class QtSignalSpy(QObject):
20    """
21    Helper class for testing Qt signals.
22    """
23    def __init__(self, widget, signal, parent=None):
24        """
25        """
26        super(QtSignalSpy, self).__init__(parent)
27
28        self._recorder = {}
29        self._count = 0
30        self._signal = []
31
32        # Assign our own slot to the emitted signal
33        try:
34            if isinstance(signal, pyqtBoundSignal):
35                signal.connect(self.slot)
36            elif hasattr(widget, signal):
37                getattr(widget, signal).connect(self.slot)
38            else:
39                widget.signal.connect(slot)
40        except AttributeError:
41            msg = "Wrong construction of QtSignalSpy instance"
42            raise RuntimeError(msg)
43
44    def slot(self, *args, **kwargs):
45        """
46        Record emitted signal.
47        """
48        self._recorder[self._count] = {
49            'args'   : args,
50            'kwargs' : kwargs,
51            }
52
53        self._count += 1
54        self._signal = [args, kwargs]
55
56    def signal(self, index=None):
57        if index is None:
58            return self._signal
59        else:
60            return self._signal[index]
61
62    def count(self):
63        return self._count
64
65    def called(self):
66        return self._recorder
Note: See TracBrowser for help on using the repository browser.