107 lines
3.4 KiB
Python
107 lines
3.4 KiB
Python
"""Round 116: Preferences › Appearance can hide the menu bar until Alt."""
|
||
import pytest
|
||
from PyQt6.QtCore import Qt
|
||
from PyQt6.QtTest import QTest
|
||
from PyQt6.QtWidgets import QApplication
|
||
|
||
from lintunes.gui import menu_bar
|
||
|
||
ALT = Qt.KeyboardModifier.AltModifier
|
||
|
||
|
||
@pytest.fixture
|
||
def window(qapp, tmp_path):
|
||
from lintunes.gui.main_window import MainWindow
|
||
from lintunes.library_manager import LibraryManager
|
||
from lintunes.models.library import Library
|
||
from lintunes.preferences import Preferences
|
||
prefs = Preferences(tmp_path / "data")
|
||
prefs.set("menu_bar", "alt")
|
||
w = MainWindow(LibraryManager(Library(), tmp_path / "data"), prefs)
|
||
w.show()
|
||
w.activateWindow()
|
||
qapp.processEvents()
|
||
yield w
|
||
QApplication.instance().removeEventFilter(w._menu_bar)
|
||
w.close()
|
||
|
||
|
||
def _tap_alt(qapp, widget):
|
||
QTest.keyPress(widget, Qt.Key.Key_Alt, ALT)
|
||
QTest.keyRelease(widget, Qt.Key.Key_Alt, Qt.KeyboardModifier.NoModifier)
|
||
qapp.processEvents()
|
||
|
||
|
||
def test_mnemonic():
|
||
assert menu_bar._mnemonic("&File") == "f"
|
||
assert menu_bar._mnemonic("Tom && &Jerry") == "j"
|
||
assert menu_bar._mnemonic("Plain") == ""
|
||
|
||
|
||
def test_hidden_until_alt_and_a_second_tap_hides_it(qapp, window):
|
||
bar = window.menuBar()
|
||
assert not bar.isVisible()
|
||
_tap_alt(qapp, window)
|
||
assert bar.isVisible()
|
||
_tap_alt(qapp, window)
|
||
assert not bar.isVisible()
|
||
assert QApplication.focusWidget() is not bar # no keyboard mode left behind
|
||
|
||
|
||
def test_alt_with_another_key_is_not_a_tap(qapp, window):
|
||
QTest.keyPress(window, Qt.Key.Key_Alt, ALT)
|
||
QTest.keyClick(window, Qt.Key.Key_Tab, ALT)
|
||
QTest.keyRelease(window, Qt.Key.Key_Alt, Qt.KeyboardModifier.NoModifier)
|
||
qapp.processEvents()
|
||
assert not window.menuBar().isVisible()
|
||
|
||
|
||
def test_escape_hides_a_peeking_bar(qapp, window):
|
||
_tap_alt(qapp, window)
|
||
QTest.keyClick(window, Qt.Key.Key_Escape)
|
||
qapp.processEvents()
|
||
assert not window.menuBar().isVisible()
|
||
|
||
|
||
def test_alt_letter_opens_that_menu_and_closing_it_hides_the_bar(qapp, window):
|
||
bar = window.menuBar()
|
||
QTest.keyClick(window, Qt.Key.Key_F, ALT)
|
||
qapp.processEvents()
|
||
assert bar.isVisible()
|
||
assert bar.activeAction().text() == "&File"
|
||
bar.activeAction().menu().hide()
|
||
for _ in range(3):
|
||
qapp.processEvents()
|
||
assert not bar.isVisible()
|
||
|
||
|
||
def test_shortcuts_still_work_with_the_bar_hidden(qapp, window):
|
||
hits = []
|
||
window._dark_action.toggled.connect(hits.append)
|
||
QTest.keyClick(window, Qt.Key.Key_D,
|
||
Qt.KeyboardModifier.ControlModifier | Qt.KeyboardModifier.ShiftModifier)
|
||
qapp.processEvents()
|
||
assert hits == [True]
|
||
|
||
|
||
def test_the_pref_applies_live(qapp, window):
|
||
window._prefs.set("menu_bar", "always")
|
||
assert window.menuBar().isVisible()
|
||
_tap_alt(qapp, window) # Alt does nothing of ours now
|
||
assert window.menuBar().isVisible()
|
||
window._prefs.set("menu_bar", "alt")
|
||
assert not window.menuBar().isVisible()
|
||
|
||
|
||
def test_preferences_checkbox_writes_the_pref(qapp, tmp_path):
|
||
from lintunes.gui.preferences_dialog import PreferencesDialog
|
||
from lintunes.preferences import Preferences
|
||
from tests.test_round40 import _FakeLastFm
|
||
prefs = Preferences(tmp_path)
|
||
dialog = PreferencesDialog(prefs, _FakeLastFm())
|
||
assert not dialog._menu_bar_check.isChecked()
|
||
dialog._menu_bar_check.setChecked(True)
|
||
assert prefs.get("menu_bar") == "alt"
|
||
dialog._menu_bar_check.setChecked(False)
|
||
assert prefs.get("menu_bar") == "always"
|