101 lines
3.9 KiB
Python
101 lines
3.9 KiB
Python
"""Opt-in Windows native WebEngine lifecycle regression; never load provider pages.
|
|
|
|
Run outside a process-restricted sandbox with ESIGN_NATIVE_QT_TEST=1 and
|
|
QT_QPA_PLATFORM=windows. Ordinary headless test runs skip this module.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from PySide6.QtCore import QElapsedTimer
|
|
from PySide6.QtTest import QTest
|
|
from PySide6.QtWidgets import QApplication
|
|
|
|
from doctor_workstation.ui.dialogs import prescription_esign as module
|
|
from doctor_workstation.ui.dialogs.esign_webview import EmbeddedIdentityView
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
sys.platform != "win32" or os.getenv("ESIGN_NATIVE_QT_TEST") != "1",
|
|
reason="opt-in native Windows Qt process test",
|
|
)
|
|
|
|
|
|
def status(value: str = "pending") -> dict[str, Any]:
|
|
return dict(enabled=True, required=True, environment="sandbox", status=value,
|
|
can_sign=True, can_download=False, auth_url="https://smlt.esign.cn/no-request")
|
|
|
|
|
|
@pytest.mark.parametrize("hide_before_ready", [False, True])
|
|
def test_real_webengine_initialization_and_user_hide(
|
|
monkeypatch: pytest.MonkeyPatch, hide_before_ready: bool
|
|
) -> None:
|
|
app = QApplication.instance() or QApplication([])
|
|
calls: list[str] = []
|
|
loaded_urls: list[str] = []
|
|
queued: list[dict[str, Any]] = []
|
|
|
|
class Repository:
|
|
def get_prescription_esign_status(self, prescription_id: int) -> dict[str, Any]:
|
|
calls.append("status")
|
|
return status()
|
|
|
|
def dispatch(function: Any, **callbacks: Any) -> None:
|
|
if not calls:
|
|
callbacks["on_success"](function())
|
|
callbacks["on_finished"]()
|
|
else:
|
|
queued.append(callbacks)
|
|
|
|
monkeypatch.setattr(module, "run_async", dispatch)
|
|
monkeypatch.setattr(EmbeddedIdentityView, "load_authentication", lambda self, url: loaded_urls.append(url))
|
|
dialog = module.PrescriptionEsignDialog(Repository(), {"id": 7})
|
|
dialog.show()
|
|
QTest.qWait(100)
|
|
original_id = int(dialog.winId())
|
|
generation = dialog._generation
|
|
dialog.start_button.click()
|
|
assert len(queued) == 1 and dialog.identity_view is None
|
|
queued[0]["on_success"](status("authenticating"))
|
|
queued[0]["on_finished"]()
|
|
assert len(queued) == 2 and dialog.identity_view is None
|
|
queued[1]["on_success"]({**status("authenticating"), "identity_status": "ING", "can_complete": False})
|
|
if hide_before_ready:
|
|
dialog.hide()
|
|
queued[1]["on_finished"]()
|
|
timer = QElapsedTimer()
|
|
timer.start()
|
|
while timer.elapsed() < 8000:
|
|
QTest.qWait(20)
|
|
if hide_before_ready:
|
|
if not dialog._native_hide_pending:
|
|
break
|
|
elif int(dialog.winId()) != original_id and not dialog._native_hide_pending:
|
|
break
|
|
assert len(queued) == 2
|
|
QTest.qWait(100)
|
|
if hide_before_ready:
|
|
assert not dialog.isVisible()
|
|
assert dialog.identity_view is None
|
|
assert not dialog.poll_timer.isActive()
|
|
else:
|
|
assert int(dialog.winId()) != original_id, "native recreation must actually be exercised"
|
|
assert dialog._generation == generation
|
|
assert dialog.isVisible() and dialog.state["status"] == "authenticating"
|
|
assert loaded_urls == ["https://smlt.esign.cn/no-request"]
|
|
assert dialog.identity_view is not None and dialog.poll_timer.isActive()
|
|
dialog.poll_timer.timeout.emit()
|
|
assert len(queued) == 3
|
|
queued[2]["on_success"]({**status("authenticating"), "identity_status": "SUCCESS", "can_complete": True})
|
|
queued[2]["on_finished"]()
|
|
assert len(queued) == 3 and dialog.sign_button.isVisible()
|
|
assert dialog.identity_view is None and not dialog.poll_timer.isActive()
|
|
dialog.setVisible(False)
|
|
assert not dialog._intent and not dialog.sign_button.isEnabled() and not dialog.poll_timer.isActive()
|
|
assert calls == ["status"]
|
|
dialog.close()
|
|
app.processEvents()
|