From 4e05e6dcd645ba16c8bdd1ccdc505b95b06fc6f8 Mon Sep 17 00:00:00 2001 From: Ales Erjavec Date: Wed, 6 Jul 2016 17:44:09 +0200 Subject: [PATCH] overlay: Move and improve tests --- Orange/widgets/utils/overlay.py | 50 +------------ Orange/widgets/utils/tests/test_overlay.py | 86 ++++++++++++++++++++++ 2 files changed, 87 insertions(+), 49 deletions(-) create mode 100644 Orange/widgets/utils/tests/test_overlay.py diff --git a/Orange/widgets/utils/overlay.py b/Orange/widgets/utils/overlay.py index 62acc0ad320..4d9bb9629c7 100644 --- a/Orange/widgets/utils/overlay.py +++ b/Orange/widgets/utils/overlay.py @@ -17,7 +17,7 @@ QStyleOptionButton, QStylePainter, QFocusFrame, QWidget, QStyleOption ) from AnyQt.QtGui import QIcon, QPixmap, QPainter -from AnyQt.QtCore import Qt, QSize, QRect, QPoint, QEvent, QTimer +from AnyQt.QtCore import Qt, QSize, QRect, QPoint, QEvent from AnyQt.QtCore import pyqtSignal as Signal, pyqtSlot as Slot @@ -606,51 +606,3 @@ def buttonRole(self, button): @proxydoc(MessageWidget.button) def button(self, standardButton): return self.__msgwidget.button(standardButton) - - -import unittest - - -class TestOverlay(unittest.TestCase): - def setUp(self): - from AnyQt.QtWidgets import QApplication - app = QApplication.instance() - if app is None: - app = QApplication([]) - self.app = app - - def _exec(self, timeout): - QTimer.singleShot(timeout, self.app.quit) - return self.app.exec_() - - def tearDown(self): - del self.app - - def test_overlay(self): - container = QWidget() - overlay = MessageOverlayWidget(parent=container) - overlay.setWidget(container) - overlay.setIcon(QStyle.SP_MessageBoxInformation) - container.show() - container.raise_() - self._exec(500) - self.assertTrue(overlay.isVisible()) - - overlay.setText("Hello world! It's so nice here") - self._exec(500) - button_ok = overlay.addButton(MessageOverlayWidget.Ok) - button_close = overlay.addButton(MessageOverlayWidget.Close) - button_help = overlay.addButton(MessageOverlayWidget.Help) - - self.assertTrue(all([button_ok, button_close, button_help])) - self.assertIs(overlay.button(MessageOverlayWidget.Ok), button_ok) - self.assertIs(overlay.button(MessageOverlayWidget.Close), button_close) - self.assertIs(overlay.button(MessageOverlayWidget.Help), button_help) - - button = overlay.addButton("Click Me!", - MessageOverlayWidget.AcceptRole) - self.assertIsNot(button, None) - self.assertTrue(overlay.buttonRole(button), - MessageOverlayWidget.AcceptRole) - - self._exec(10000) diff --git a/Orange/widgets/utils/tests/test_overlay.py b/Orange/widgets/utils/tests/test_overlay.py new file mode 100644 index 00000000000..f797e6f6fb7 --- /dev/null +++ b/Orange/widgets/utils/tests/test_overlay.py @@ -0,0 +1,86 @@ + +import unittest.mock +from AnyQt.QtCore import Qt, QEvent +from AnyQt.QtTest import QTest +from AnyQt.QtWidgets import QWidget, QHBoxLayout, QStyle, QApplication + +from Orange.widgets.tests.base import GuiTest +from Orange.widgets.utils.overlay import ( + OverlayWidget, MessageOverlayWidget +) + +class TestOverlay(GuiTest): + def test_overlay_message(self): + container = QWidget() + overlay = MessageOverlayWidget(parent=container) + overlay.setWidget(container) + overlay.setIcon(QStyle.SP_MessageBoxInformation) + container.show() + QTest.qWaitForWindowExposed(container) + + self.assertTrue(overlay.isVisible()) + + overlay.setText("Hello world! It's so nice here") + QApplication.sendPostedEvents(overlay, QEvent.LayoutRequest) + self.assertTrue(overlay.geometry().isValid()) + + button_ok = overlay.addButton(MessageOverlayWidget.Ok) + button_close = overlay.addButton(MessageOverlayWidget.Close) + button_help = overlay.addButton(MessageOverlayWidget.Help) + + self.assertTrue(all([button_ok, button_close, button_help])) + self.assertIs(overlay.button(MessageOverlayWidget.Ok), button_ok) + self.assertIs(overlay.button(MessageOverlayWidget.Close), button_close) + self.assertIs(overlay.button(MessageOverlayWidget.Help), button_help) + + button = overlay.addButton("Click Me!", + MessageOverlayWidget.AcceptRole) + self.assertIsNot(button, None) + self.assertTrue(overlay.buttonRole(button), + MessageOverlayWidget.AcceptRole) + + mock = unittest.mock.MagicMock() + overlay.accepted.connect(mock) + QTest.mouseClick(button, Qt.LeftButton) + self.assertFalse(overlay.isVisible()) + + mock.assert_called_once_with() + + def test_layout(self): + container = QWidget() + container.setLayout(QHBoxLayout()) + container1 = QWidget() + container.layout().addWidget(container1) + container.show() + QTest.qWaitForWindowExposed(container) + container.resize(600, 600) + + overlay = OverlayWidget(parent=container) + overlay.setWidget(container) + overlay.resize(20, 20) + overlay.show() + + center = overlay.geometry().center() + self.assertTrue(290 < center.x() < 310) + self.assertTrue(290 < center.y() < 310) + + overlay.setAlignment(Qt.AlignTop | Qt.AlignHCenter) + geom = overlay.geometry() + self.assertEqual(geom.top(), 0) + self.assertTrue(290 < geom.center().x() < 310) + + overlay.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) + geom = overlay.geometry() + self.assertEqual(geom.left(), 0) + self.assertTrue(290 < geom.center().y() < 310) + + overlay.setAlignment(Qt.AlignBottom | Qt.AlignRight) + geom = overlay.geometry() + self.assertEqual(geom.right(), 600 - 1) + self.assertEqual(geom.bottom(), 600 - 1) + + overlay.setWidget(container1) + geom = overlay.geometry() + + self.assertEqual(geom.right(), container1.geometry().right()) + self.assertEqual(geom.bottom(), container1.geometry().bottom())