|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Copyright © QtAppUtils Project Contributors |
| 4 | +# https://github.com/geo-stack/qtapputils |
| 5 | +# |
| 6 | +# This file is part of QtAppUtils. |
| 7 | +# Licensed under the terms of the MIT License. |
| 8 | +# ----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +import pytest |
| 11 | +from qtpy.QtGui import QIcon, QPixmap |
| 12 | +from qtpy.QtCore import Qt |
| 13 | +from qtpy.QtTest import QSignalSpy |
| 14 | + |
| 15 | +from qtapputils.widgets.buttons import MultiStateToolButton |
| 16 | +from qtapputils.testing import icons_are_equal |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def icons(): |
| 21 | + # Create dummy QIcons for testing |
| 22 | + pixmap1 = QPixmap(16, 16) |
| 23 | + pixmap1.fill(Qt.red) |
| 24 | + pixmap2 = QPixmap(16, 16) |
| 25 | + pixmap2.fill(Qt.green) |
| 26 | + pixmap3 = QPixmap(16, 16) |
| 27 | + pixmap3.fill(Qt.blue) |
| 28 | + return [QIcon(pixmap1), QIcon(pixmap2), QIcon(pixmap3)] |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def button(icons, qtbot): |
| 33 | + |
| 34 | + btn = MultiStateToolButton(icons) |
| 35 | + qtbot.addWidget(btn) |
| 36 | + btn.show() |
| 37 | + qtbot.waitUntil(btn.isVisible) |
| 38 | + |
| 39 | + assert btn.current_index() == 0 |
| 40 | + assert icons_are_equal(btn.icon(), icons[0]) |
| 41 | + |
| 42 | + return btn |
| 43 | + |
| 44 | + |
| 45 | +def test_cycle_icons(button, qtbot, icons): |
| 46 | + """Test icon cycles forward with clicks and wraps around.""" |
| 47 | + signal_spy = QSignalSpy(button.sig_index_changed) |
| 48 | + |
| 49 | + qtbot.mouseClick(button, Qt.LeftButton) |
| 50 | + |
| 51 | + assert len(signal_spy) == 1 |
| 52 | + assert signal_spy[-1] == [1] |
| 53 | + assert icons_are_equal(button.icon(), icons[1]) |
| 54 | + |
| 55 | + qtbot.mouseClick(button, Qt.LeftButton) |
| 56 | + |
| 57 | + assert len(signal_spy) == 2 |
| 58 | + assert signal_spy[-1] == [2] |
| 59 | + assert icons_are_equal(button.icon(), icons[2]) |
| 60 | + |
| 61 | + # Should wrap back to 0. |
| 62 | + qtbot.mouseClick(button, Qt.LeftButton) |
| 63 | + |
| 64 | + assert len(signal_spy) == 3 |
| 65 | + assert signal_spy[-1] == [0] |
| 66 | + assert icons_are_equal(button.icon(), icons[0]) |
| 67 | + |
| 68 | + |
| 69 | +def test_set_index(button, qtbot, icons): |
| 70 | + """Test icon is set as expected when index is set programattically.""" |
| 71 | + signal_spy = QSignalSpy(button.sig_index_changed) |
| 72 | + |
| 73 | + # Should wrap back to len(icons) - 1. |
| 74 | + button.set_current_index(-1) |
| 75 | + |
| 76 | + assert len(signal_spy) == 1 |
| 77 | + assert signal_spy[-1] == [2] |
| 78 | + assert icons_are_equal(button.icon(), icons[2]) |
| 79 | + |
| 80 | + # Should wrap back to 0. |
| 81 | + button.set_current_index(100) |
| 82 | + |
| 83 | + assert len(signal_spy) == 2 |
| 84 | + assert signal_spy[-1] == [0] |
| 85 | + assert icons_are_equal(button.icon(), icons[0]) |
| 86 | + |
| 87 | + # Should do nothing. |
| 88 | + button.set_current_index(100) |
| 89 | + |
| 90 | + assert len(signal_spy) == 2 |
| 91 | + assert signal_spy[-1] == [0] |
| 92 | + assert icons_are_equal(button.icon(), icons[0]) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + pytest.main(['-x', __file__, '-vv', '-rw']) |
0 commit comments