|
| 1 | +# pylint: disable=all |
| 2 | + |
| 3 | +import unittest |
| 4 | + |
| 5 | +from AnyQt.QtCore import Qt, QPoint, QRect |
| 6 | +from AnyQt.QtGui import QMouseEvent |
| 7 | +from AnyQt.QtWidgets import QListView, QApplication |
| 8 | +from AnyQt.QtTest import QTest, QSignalSpy |
| 9 | +from Orange.widgets.tests.base import GuiTest |
| 10 | + |
| 11 | +from Orange.widgets.utils import combobox |
| 12 | + |
| 13 | + |
| 14 | +class TestComboBoxSearch(GuiTest): |
| 15 | + def setUp(self): |
| 16 | + super().setUp() |
| 17 | + cb = combobox.ComboBoxSearch() |
| 18 | + cb.addItem("One") |
| 19 | + cb.addItem("Two") |
| 20 | + cb.addItem("Three") |
| 21 | + cb.insertSeparator(cb.count()) |
| 22 | + cb.addItem("Four") |
| 23 | + self.cb = cb |
| 24 | + |
| 25 | + def tearDown(self): |
| 26 | + super().tearDown() |
| 27 | + self.cb.deleteLater() |
| 28 | + self.cb = None |
| 29 | + |
| 30 | + def test_combobox(self): |
| 31 | + cb = self.cb |
| 32 | + cb.grab() |
| 33 | + cb.showPopup() |
| 34 | + popup = cb.findChild(QListView) # type: QListView |
| 35 | + # run through paint code for coverage |
| 36 | + popup.grab() |
| 37 | + cb.grab() |
| 38 | + |
| 39 | + model = popup.model() |
| 40 | + self.assertEqual(model.rowCount(), cb.count()) |
| 41 | + QTest.keyClick(popup, Qt.Key_E) |
| 42 | + self.assertEqual(model.rowCount(), 2) |
| 43 | + QTest.keyClick(popup, Qt.Key_Backspace) |
| 44 | + self.assertEqual(model.rowCount(), cb.count()) |
| 45 | + QTest.keyClick(popup, Qt.Key_F) |
| 46 | + self.assertEqual(model.rowCount(), 1) |
| 47 | + popup.setCurrentIndex(model.index(0, 0)) |
| 48 | + spy = QSignalSpy(cb.activated[int]) |
| 49 | + QTest.keyClick(popup, Qt.Key_Enter) |
| 50 | + |
| 51 | + self.assertEqual(spy[0], [4]) |
| 52 | + self.assertEqual(cb.currentIndex(), 4) |
| 53 | + self.assertEqual(cb.currentText(), "Four") |
| 54 | + self.assertFalse(popup.isVisible()) |
| 55 | + |
| 56 | + cb.hidePopup() |
| 57 | + |
| 58 | + def test_click(self): |
| 59 | + cb = self.cb |
| 60 | + cb.showPopup() |
| 61 | + popup = cb.findChild(QListView) # type: QListView |
| 62 | + model = popup.model() |
| 63 | + rect = popup.visualRect(model.index(1, 0)) |
| 64 | + |
| 65 | + spy = QSignalSpy(cb.activated[int]) |
| 66 | + QTest.mouseClick(popup.viewport(), Qt.LeftButton, Qt.NoModifier, |
| 67 | + rect.center(), QApplication.doubleClickInterval()) |
| 68 | + |
| 69 | + self.assertEqual(spy[0], [1]) |
| 70 | + self.assertEqual(cb.currentIndex(), 1) |
| 71 | + self.assertEqual(cb.currentText(), "Two") |
| 72 | + |
| 73 | + def test_track(self): |
| 74 | + cb = self.cb |
| 75 | + cb.setStyleSheet("combobox-list-mousetracking: 1") |
| 76 | + cb.showPopup() |
| 77 | + popup = cb.findChild(QListView) # type: QListView |
| 78 | + model = popup.model() |
| 79 | + rect = popup.visualRect(model.index(2, 0)) |
| 80 | + # QTest.mouseMove(popup.viewport(), rect.center()) |
| 81 | + mouseMove(popup.viewport(), rect.center()) |
| 82 | + self.assertEqual(popup.currentIndex().row(), 2) |
| 83 | + |
| 84 | + def test_popup_util(self): |
| 85 | + geom = QRect(10, 10, 100, 400) |
| 86 | + screen = QRect(0, 0, 600, 600) |
| 87 | + g1 = combobox.dropdown_popup_geometry( |
| 88 | + geom, QRect(200, 100, 100, 20), screen |
| 89 | + ) |
| 90 | + self.assertEqual(g1, QRect(200, 120, 100, 400)) |
| 91 | + g2 = combobox.dropdown_popup_geometry( |
| 92 | + geom, QRect(-10, 0, 100, 20), screen |
| 93 | + ) |
| 94 | + self.assertEqual(g2, QRect(0, 20, 100, 400)) |
| 95 | + g3 = combobox.dropdown_popup_geometry( |
| 96 | + geom, QRect(590, 0, 100, 20), screen |
| 97 | + ) |
| 98 | + self.assertEqual(g3, QRect(600 - 100, 20, 100, 400)) |
| 99 | + g4 = combobox.dropdown_popup_geometry( |
| 100 | + geom, QRect(0, 500, 100, 20), screen |
| 101 | + ) |
| 102 | + self.assertEqual(g4, QRect(0, 500 - 400, 100, 400)) |
| 103 | + |
| 104 | + |
| 105 | +def mouseMove(widget, pos=QPoint(), delay=-1): |
| 106 | + # Like QTest.mouseMove, but functional QCursor.setPos |
| 107 | + if pos.isNull(): |
| 108 | + pos = widget.rect().center() |
| 109 | + me = QMouseEvent(QMouseEvent.MouseMove, pos, widget.mapToGlobal(pos), |
| 110 | + Qt.NoButton, Qt.MouseButtons(0), Qt.NoModifier) |
| 111 | + if delay > 0: |
| 112 | + QTest.qWait(delay) |
| 113 | + |
| 114 | + QApplication.sendEvent(widget, me) |
0 commit comments