Skip to content

Commit

Permalink
overlay: Move and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-erjavec committed Aug 16, 2017
1 parent c279fc7 commit 4e05e6d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 49 deletions.
50 changes: 1 addition & 49 deletions Orange/widgets/utils/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
86 changes: 86 additions & 0 deletions Orange/widgets/utils/tests/test_overlay.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 4e05e6d

Please sign in to comment.