Files
MDHelper/tests/startup_smoke.py
T

90 lines
3.5 KiB
Python

"""Check window/reader identity across startup and the first document load.
Set MDHELPER_SMOKE_NATIVE=1 to run against the Windows display backend.
"""
import os
import sys
import tempfile
import time
from pathlib import Path
NATIVE = os.environ.get("MDHELPER_SMOKE_NATIVE") == "1"
if not NATIVE:
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
os.environ.setdefault("QTWEBENGINE_DISABLE_SANDBOX", "1")
os.environ.setdefault("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-gpu --no-sandbox --log-level=3")
else:
os.environ.setdefault("QTWEBENGINE_CHROMIUM_FLAGS", "--log-level=3")
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from PySide6.QtCore import QEvent, QObject, QSettings, QTimer, Qt
from PySide6.QtWidgets import QApplication
from mdhelper import main_window
class WindowEvents(QObject):
def __init__(self, window):
super().__init__(window)
self.events = []
window.installEventFilter(self)
def eventFilter(self, watched, event):
if event.type() in (QEvent.Type.Show, QEvent.Type.Hide, QEvent.Type.WinIdChange):
self.events.append(event.type())
return False
def main():
QApplication.setAttribute(Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings, True)
app = QApplication.instance() or QApplication([])
app.setQuitOnLastWindowClosed(False)
def wait(condition, seconds=20):
deadline = time.monotonic() + seconds
while not condition() and time.monotonic() < deadline:
app.processEvents()
time.sleep(.01)
assert condition(), "Startup did not finish"
with tempfile.TemporaryDirectory(prefix="mdhelper-startup-") as temporary:
root = Path(temporary)
settings = QSettings(str(root / "settings.ini"), QSettings.Format.IniFormat)
main_window.QSettings = lambda *_: settings
note = root / "first.md"
note.write_text("# First document\n\nFirst **formatted** paragraph.", encoding="utf-8")
window = main_window.MainWindow(None, app_root=ROOT, defer_startup=True)
window.prepare_reader_before_show()
host = window.tabs.currentWidget()
reader = host.reader_view
assert reader is not None
parent = reader.parent()
events = WindowEvents(window)
try:
window.show()
native_id = int(window.winId())
# Inspect late startup callbacks as well as the immediate show.
settled = []
QTimer.singleShot(1200, lambda: settled.append(True))
wait(lambda: bool(settled))
startup_events = list(events.events)
load = []
reader.loadFinished.connect(load.append)
window.open_document(note)
wait(lambda: bool(load) and load[-1])
assert window.isVisible() and int(window.winId()) == native_id
assert window.tabs.currentWidget() is host and host.reader_view is reader
assert reader.parent() is parent and host.stack.currentWidget() is reader
assert events.events.count(QEvent.Type.Show) == 1, events.events
assert QEvent.Type.Hide not in events.events, events.events
assert events.events == startup_events, events.events
print("Startup passed: one show, no hide, stable native window, fixed reader host and browser parent.")
finally:
window.close()
app.processEvents()
return 0
if __name__ == "__main__":
raise SystemExit(main())