|
| 1 | +import unittest.mock |
| 2 | + |
| 3 | +from AnyQt.QtCore import Qt, QEvent |
| 4 | +from AnyQt.QtTest import QTest |
| 5 | +from AnyQt.QtWidgets import QWidget, QApplication |
| 6 | + |
| 7 | +from Orange.canvas.utils.overlay import NotificationWidget, NotificationOverlay |
| 8 | +from Orange.widgets.tests.base import GuiTest |
| 9 | + |
| 10 | + |
| 11 | +class TestOverlay(GuiTest): |
| 12 | + def setUp(self) -> None: |
| 13 | + self.container = QWidget() |
| 14 | + stdb = NotificationWidget.Ok | NotificationWidget.Close |
| 15 | + self.overlay = NotificationOverlay(self.container) |
| 16 | + self.notif = NotificationWidget(title="lol", |
| 17 | + text="hihi", |
| 18 | + standardButtons=stdb) |
| 19 | + self.container.show() |
| 20 | + QTest.qWaitForWindowExposed(self.container) |
| 21 | + self.overlay.show() |
| 22 | + QTest.qWaitForWindowExposed(self.overlay) |
| 23 | + |
| 24 | + def tearDown(self) -> None: |
| 25 | + NotificationOverlay.overlayInstances = [] |
| 26 | + NotificationOverlay.notifQueue = [] |
| 27 | + self.container = None |
| 28 | + self.overlay = None |
| 29 | + self.notif = None |
| 30 | + |
| 31 | + def test_notification_dismiss(self): |
| 32 | + mock = unittest.mock.MagicMock() |
| 33 | + self.notif.clicked.connect(mock) |
| 34 | + NotificationOverlay.registerNotification(self.notif) |
| 35 | + QTest.mouseClick(self.notif._dismiss_button, Qt.LeftButton) |
| 36 | + mock.assert_called_once_with(self.notif._dismiss_button) |
| 37 | + |
| 38 | + def test_notification_message(self): |
| 39 | + self.notif.setText("Hello world! It's so nice here") |
| 40 | + QApplication.sendPostedEvents(self.notif, QEvent.LayoutRequest) |
| 41 | + self.assertTrue(self.notif.geometry().isValid()) |
| 42 | + |
| 43 | + button_ok = self.notif.button(NotificationWidget.Ok) |
| 44 | + button_close = self.notif.button(NotificationWidget.Close) |
| 45 | + |
| 46 | + self.assertTrue(all([button_ok, button_close])) |
| 47 | + self.assertIs(self.notif.button(NotificationWidget.Ok), button_ok) |
| 48 | + self.assertIs(self.notif.button(NotificationWidget.Close), button_close) |
| 49 | + |
| 50 | + button = self.notif.button(NotificationWidget.Ok) |
| 51 | + self.assertIsNot(button, None) |
| 52 | + self.assertTrue(self.notif.buttonRole(button), |
| 53 | + NotificationWidget.AcceptRole) |
| 54 | + |
| 55 | + mock = unittest.mock.MagicMock() |
| 56 | + self.notif.accepted.connect(mock) |
| 57 | + |
| 58 | + NotificationOverlay.registerNotification(self.notif) |
| 59 | + |
| 60 | + cloned = NotificationOverlay.overlayInstances[0].currentWidget() |
| 61 | + self.assertTrue(cloned.isVisible()) |
| 62 | + button = cloned._msgwidget.button(NotificationWidget.Ok) |
| 63 | + QTest.mouseClick(button, Qt.LeftButton) |
| 64 | + self.assertFalse(cloned.isVisible()) |
| 65 | + |
| 66 | + mock.assert_called_once() |
| 67 | + |
| 68 | + def test_queued_notifications(self): |
| 69 | + surveyDialogButtons = NotificationWidget.Ok | NotificationWidget.Close |
| 70 | + surveyDialog = NotificationWidget(title="Survey", |
| 71 | + text="We want to understand our users better.\n" |
| 72 | + "Would you like to take a short survey?", |
| 73 | + standardButtons=surveyDialogButtons) |
| 74 | + |
| 75 | + def handle_survey_response(button): |
| 76 | + pass |
| 77 | + |
| 78 | + surveyDialog.clicked.connect(handle_survey_response) |
| 79 | + |
| 80 | + NotificationOverlay.registerNotification(self.notif) |
| 81 | + notif1 = NotificationOverlay.overlayInstances[0]._widgets[0] |
| 82 | + button = notif1._dismiss_button |
| 83 | + |
| 84 | + NotificationOverlay.registerNotification(surveyDialog) |
| 85 | + notif2 = NotificationOverlay.overlayInstances[0]._widgets[1] |
| 86 | + |
| 87 | + self.assertTrue(notif1.isVisible()) |
| 88 | + self.assertFalse(notif2.isVisible()) |
| 89 | + |
| 90 | + QTest.mouseClick(button, Qt.LeftButton) |
| 91 | + |
| 92 | + self.assertFalse(notif1.isVisible()) |
| 93 | + self.assertTrue(notif2.isVisible()) |
| 94 | + |
0 commit comments