|
| 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 | + def test_combobox_navigation(self): |
| 57 | + cb = self.cb |
| 58 | + cb.setCurrentIndex(4) |
| 59 | + self.assertTrue(cb.currentText(), "Four") |
| 60 | + cb.showPopup() |
| 61 | + popup = cb.findChild(QListView) # type: QListView |
| 62 | + self.assertEqual(popup.currentIndex().row(), 4) |
| 63 | + |
| 64 | + QTest.keyClick(popup, Qt.Key_Up) |
| 65 | + self.assertEqual(popup.currentIndex().row(), 2) |
| 66 | + QTest.keyClick(popup, Qt.Key_Escape) |
| 67 | + self.assertFalse(popup.isVisible()) |
| 68 | + self.assertEqual(cb.currentIndex(), 4) |
| 69 | + cb.hidePopup() |
| 70 | + |
| 71 | + def test_click(self): |
| 72 | + cb = self.cb |
| 73 | + cb.showPopup() |
| 74 | + popup = cb.findChild(QListView) # type: QListView |
| 75 | + model = popup.model() |
| 76 | + rect = popup.visualRect(model.index(1, 0)) |
| 77 | + if hasattr(Qt, "WA_DontShowOnScreen"): |
| 78 | + popup.window().setAttribute(Qt.WA_DontShowOnScreen, True) |
| 79 | + spy = QSignalSpy(cb.activated[int]) |
| 80 | + QTest.mouseClick(popup.viewport(), Qt.LeftButton, Qt.NoModifier, |
| 81 | + rect.center(), QApplication.doubleClickInterval() + 10) |
| 82 | + |
| 83 | + self.assertEqual(spy[0], [1]) |
| 84 | + self.assertEqual(cb.currentIndex(), 1) |
| 85 | + self.assertEqual(cb.currentText(), "Two") |
| 86 | + |
| 87 | + def test_focus_out(self): |
| 88 | + cb = self.cb |
| 89 | + cb.showPopup() |
| 90 | + popup = cb.findChild(QListView) |
| 91 | + # Activate some other window to simulate focus out |
| 92 | + w = QListView() |
| 93 | + w.show() |
| 94 | + w.activateWindow() |
| 95 | + w.hide() |
| 96 | + self.assertFalse(popup.isVisible()) |
| 97 | + |
| 98 | + def test_track(self): |
| 99 | + cb = self.cb |
| 100 | + cb.setStyleSheet("combobox-list-mousetracking: 1") |
| 101 | + cb.showPopup() |
| 102 | + popup = cb.findChild(QListView) # type: QListView |
| 103 | + model = popup.model() |
| 104 | + rect = popup.visualRect(model.index(2, 0)) |
| 105 | + mouseMove(popup.viewport(), rect.center()) |
| 106 | + self.assertEqual(popup.currentIndex().row(), 2) |
| 107 | + cb.hidePopup() |
| 108 | + |
| 109 | + def test_empty(self): |
| 110 | + cb = self.cb |
| 111 | + cb.clear() |
| 112 | + cb.showPopup() |
| 113 | + popup = cb.findChild(QListView) # type: QListView |
| 114 | + self.assertIsNone(popup) |
| 115 | + |
| 116 | + def test_popup_util(self): |
| 117 | + geom = QRect(10, 10, 100, 400) |
| 118 | + screen = QRect(0, 0, 600, 600) |
| 119 | + g1 = combobox.dropdown_popup_geometry( |
| 120 | + geom, QRect(200, 100, 100, 20), screen |
| 121 | + ) |
| 122 | + self.assertEqual(g1, QRect(200, 120, 100, 400)) |
| 123 | + g2 = combobox.dropdown_popup_geometry( |
| 124 | + geom, QRect(-10, 0, 100, 20), screen |
| 125 | + ) |
| 126 | + self.assertEqual(g2, QRect(0, 20, 100, 400)) |
| 127 | + g3 = combobox.dropdown_popup_geometry( |
| 128 | + geom, QRect(590, 0, 100, 20), screen |
| 129 | + ) |
| 130 | + self.assertEqual(g3, QRect(600 - 100, 20, 100, 400)) |
| 131 | + g4 = combobox.dropdown_popup_geometry( |
| 132 | + geom, QRect(0, 500, 100, 20), screen |
| 133 | + ) |
| 134 | + self.assertEqual(g4, QRect(0, 500 - 400, 100, 400)) |
| 135 | + |
| 136 | + |
| 137 | +def mouseMove(widget, pos=QPoint(), delay=-1): # pragma: no-cover |
| 138 | + # Like QTest.mouseMove, but functional without QCursor.setPos |
| 139 | + if pos.isNull(): |
| 140 | + pos = widget.rect().center() |
| 141 | + me = QMouseEvent(QMouseEvent.MouseMove, pos, widget.mapToGlobal(pos), |
| 142 | + Qt.NoButton, Qt.MouseButtons(0), Qt.NoModifier) |
| 143 | + if delay > 0: |
| 144 | + QTest.qWait(delay) |
| 145 | + |
| 146 | + QApplication.sendEvent(widget, me) |
0 commit comments