243 lines
10 KiB
Python
243 lines
10 KiB
Python
"""Material policy and fallback must not leave a transparent, unreadable window."""
|
|
import sys
|
|
import pytest
|
|
from PySide6.QtWidgets import QWidget
|
|
from dx_productivity.trial_task_center.ui.material import AcrylicController
|
|
|
|
|
|
class Backend:
|
|
def __init__(self, allowed=True, succeeds=True):
|
|
self.allowed=allowed
|
|
self.succeeds=succeeds
|
|
self.calls=[]
|
|
def available(self):
|
|
return self.allowed
|
|
def enable(self, hwnd):
|
|
self.calls.append(('enable',hwnd))
|
|
return self.succeeds
|
|
def disable(self, hwnd):
|
|
self.calls.append(('disable',hwnd))
|
|
|
|
|
|
def test_disabled_system_policy_never_enables_native_blur(qtbot):
|
|
window=QWidget();qtbot.addWidget(window)
|
|
backend=Backend(allowed=False)
|
|
material=AcrylicController(window,backend=backend)
|
|
material.refresh()
|
|
assert not window.property('acrylicEnabled')
|
|
assert not backend.calls
|
|
|
|
|
|
def test_native_api_rejection_keeps_an_opaque_fallback(qtbot):
|
|
window=QWidget();qtbot.addWidget(window)
|
|
backend=Backend(succeeds=False)
|
|
material=AcrylicController(window,backend=backend)
|
|
material.refresh()
|
|
assert backend.calls[0][0]=='enable'
|
|
assert not window.property('acrylicEnabled')
|
|
|
|
|
|
def test_reducing_transparency_disables_native_composition_and_can_restore(qtbot):
|
|
window=QWidget();qtbot.addWidget(window)
|
|
backend=Backend()
|
|
material=AcrylicController(window,backend=backend)
|
|
material.refresh()
|
|
assert window.property('acrylicEnabled')
|
|
material.set_reduced_transparency(True)
|
|
assert not window.property('acrylicEnabled')
|
|
assert backend.calls[-1][0]=='disable'
|
|
material.set_reduced_transparency(False)
|
|
assert window.property('acrylicEnabled')
|
|
assert backend.calls[-1][0]=='enable'
|
|
|
|
|
|
def test_transparency_toggle_batches_native_transition_without_intermediate_repaint(qtbot):
|
|
window=QWidget();qtbot.addWidget(window)
|
|
observed=[]
|
|
class ObservingBackend(Backend):
|
|
def enable(self, hwnd):
|
|
observed.append(('enable',window.updatesEnabled()))
|
|
return super().enable(hwnd)
|
|
def disable(self, hwnd):
|
|
observed.append(('disable',window.updatesEnabled()))
|
|
return super().disable(hwnd)
|
|
backend=ObservingBackend()
|
|
material=AcrylicController(window,backend=backend)
|
|
material.refresh()
|
|
observed.clear()
|
|
material.set_reduced_transparency(True)
|
|
assert observed==[('disable',False)]
|
|
observed.clear()
|
|
material.set_reduced_transparency(False)
|
|
assert observed==[('enable',False)]
|
|
|
|
|
|
def test_live_system_policy_change_falls_back_without_touching_business_state(qtbot):
|
|
window=QWidget();qtbot.addWidget(window)
|
|
window.setProperty('selectedTask','task-a')
|
|
backend=Backend()
|
|
material=AcrylicController(window,backend=backend)
|
|
material.refresh()
|
|
backend.allowed=False
|
|
material.refresh()
|
|
assert not window.property('acrylicEnabled')
|
|
assert window.property('selectedTask')=='task-a'
|
|
assert backend.calls[-1][0]=='disable'
|
|
|
|
|
|
def test_repeated_refresh_does_not_reconfigure_composition(qtbot):
|
|
window=QWidget();qtbot.addWidget(window)
|
|
backend=Backend()
|
|
material=AcrylicController(window,backend=backend)
|
|
material.refresh();material.refresh()
|
|
assert len(backend.calls)==1
|
|
|
|
|
|
def test_deferred_material_refresh_never_uses_a_destroyed_window(qtbot,qapp):
|
|
from PySide6.QtCore import QCoreApplication,QEvent
|
|
window=QWidget() # This test owns destruction rather than pytest-qt teardown.
|
|
backend=Backend()
|
|
controller=AcrylicController(window,backend=backend)
|
|
controller.eventFilter(window,QEvent(QEvent.Type.WindowActivate))
|
|
window.deleteLater()
|
|
QCoreApplication.sendPostedEvents(None,QEvent.Type.DeferredDelete)
|
|
controller.refresh()
|
|
qapp.processEvents()
|
|
assert not backend.calls
|
|
|
|
|
|
def test_backend_exception_uses_solid_surface(qtbot):
|
|
window=QWidget();qtbot.addWidget(window)
|
|
class Broken(Backend):
|
|
def enable(self, hwnd):
|
|
raise OSError('composition unavailable')
|
|
material=AcrylicController(window,backend=Broken())
|
|
material.refresh()
|
|
assert not window.property('acrylicEnabled')
|
|
|
|
|
|
def test_recreated_native_handle_releases_old_composition(qtbot,monkeypatch):
|
|
window=QWidget();qtbot.addWidget(window)
|
|
handle=[123]
|
|
monkeypatch.setattr(window,'winId',lambda:handle[0])
|
|
backend=Backend()
|
|
material=AcrylicController(window,backend=backend)
|
|
material.refresh()
|
|
handle[0]=456
|
|
material.refresh()
|
|
assert backend.calls==[('enable',123),('disable',123),('enable',456)]
|
|
|
|
|
|
@pytest.mark.skipif(sys.platform!='win32',reason='Windows policy API')
|
|
def test_windows_material_respects_transparency_high_contrast_and_remote(monkeypatch):
|
|
import winreg
|
|
from types import SimpleNamespace
|
|
from dx_productivity.trial_task_center.ui import material
|
|
class User:
|
|
high_contrast=False
|
|
remote=False
|
|
def SystemParametersInfoW(self,action,size,pointer,flags):
|
|
pointer._obj.flags=int(self.high_contrast)
|
|
return True
|
|
def GetSystemMetrics(self,index):
|
|
return int(self.remote)
|
|
user=User()
|
|
native=material.WindowsAcrylic.__new__(material.WindowsAcrylic)
|
|
native._user=user
|
|
monkeypatch.setattr(material.sys,'getwindowsversion',lambda:SimpleNamespace(build=26200))
|
|
monkeypatch.setattr(material.QGuiApplication,'platformName',lambda:'windows')
|
|
monkeypatch.setattr(winreg,'QueryValueEx',lambda *args:(1,winreg.REG_DWORD))
|
|
assert native.available()
|
|
user.high_contrast=True
|
|
assert not native.available()
|
|
user.high_contrast=False;user.remote=True
|
|
assert not native.available()
|
|
user.remote=False
|
|
monkeypatch.setattr(winreg,'QueryValueEx',lambda *args:(0,winreg.REG_DWORD))
|
|
assert not native.available()
|
|
|
|
|
|
def test_real_window_has_svg_controls_and_persistent_material_preference(ui):
|
|
window=ui.window
|
|
assert not window.run_panel.quick_run_button.icon().isNull()
|
|
assert not window.task_center_navigation.icon().isNull()
|
|
assert window.project_summary.parent() is not window.run_panel.parent()
|
|
window.reduced_transparency_button.setChecked(True)
|
|
assert not window.property('acrylicEnabled')
|
|
assert window._layout_settings.value('appearance/reduced_transparency',False,type=bool)
|
|
surface=window.grab().toImage().pixelColor(2,window.menuWidget().height()+2)
|
|
assert surface.alpha()==255 and surface.blue()>=235
|
|
window.mapping_tabs.setCurrentIndex(1)
|
|
image=window.plan_panel.table.viewport().grab().toImage()
|
|
color=image.pixelColor(image.width()-8,image.height()-8)
|
|
assert color.alpha()==255 and min(color.red(),color.green(),color.blue())>=245
|
|
|
|
|
|
@pytest.mark.parametrize('reduced',[False,True])
|
|
def test_high_contrast_fallback_uses_window_palette_and_tracks_policy_change(qtbot,qapp,request,reduced):
|
|
from PySide6.QtGui import QColor,QPalette
|
|
from PySide6.QtWidgets import QLabel,QLineEdit
|
|
from dx_productivity.trial_task_center.ui.material import MaterialMainWindow
|
|
from dx_productivity.trial_task_center.ui.styles import APP_STYLESHEET
|
|
original_palette=qapp.palette()
|
|
request.addfinalizer(lambda:qapp.setPalette(original_palette))
|
|
palette=QPalette(original_palette)
|
|
palette.setColor(QPalette.ColorRole.Window,QColor('#181818'))
|
|
palette.setColor(QPalette.ColorRole.WindowText,QColor('#fafafa'))
|
|
palette.setColor(QPalette.ColorRole.Base,QColor('#181818'))
|
|
palette.setColor(QPalette.ColorRole.Text,QColor('#fafafa'))
|
|
qapp.setPalette(palette)
|
|
window=MaterialMainWindow();qtbot.addWidget(window)
|
|
window.setStyleSheet(APP_STYLESHEET)
|
|
search=QLineEdit('Readable query',window,objectName='taskSearch')
|
|
search.setGeometry(30,30,180,30)
|
|
title=QLabel('Project name',window,objectName='projectTitle')
|
|
class ContrastBackend(Backend):
|
|
contrast=True
|
|
def high_contrast(self):return self.contrast
|
|
def available(self):return not self.contrast
|
|
backend=ContrastBackend()
|
|
controller=AcrylicController(window,backend=backend)
|
|
controller.set_reduced_transparency(reduced)
|
|
controller.refresh()
|
|
window.resize(240,160);window.show()
|
|
assert window.property('highContrastFallback')
|
|
assert not window.property('acrylicEnabled')
|
|
color=window.grab().toImage().pixelColor(10,10)
|
|
assert color==QColor('#181818')
|
|
search.setFocus()
|
|
assert search.palette().color(QPalette.ColorRole.Base)==QColor('#181818')
|
|
assert search.palette().color(QPalette.ColorRole.Text)==QColor('#fafafa')
|
|
assert title.palette().color(QPalette.ColorRole.WindowText)==QColor('#fafafa')
|
|
backend.contrast=False
|
|
controller.refresh()
|
|
assert not window.property('highContrastFallback')
|
|
assert window.property('acrylicEnabled') is not reduced
|
|
assert window.styleSheet()==APP_STYLESHEET
|
|
|
|
|
|
@pytest.mark.parametrize('selected',[False,True])
|
|
def test_high_contrast_task_titles_render_with_system_text_colors(qtbot,selected):
|
|
from PySide6.QtCore import QRect,Qt
|
|
from PySide6.QtGui import QColor,QImage,QPainter,QPalette,QStandardItem,QStandardItemModel
|
|
from PySide6.QtWidgets import QListWidget,QStyle,QStyleOptionViewItem
|
|
from dx_productivity.trial_task_center.ui.task_list import TaskItemDelegate
|
|
window=QWidget();qtbot.addWidget(window)
|
|
window.setProperty('highContrastFallback',True)
|
|
listing=QListWidget(window)
|
|
model=QStandardItemModel();model.appendRow(QStandardItem('01. Board Name\nReady'))
|
|
option=QStyleOptionViewItem();option.widget=listing
|
|
option.rect=QRect(0,0,220,76);option.font=listing.font()
|
|
option.state=QStyle.StateFlag.State_Enabled
|
|
palette=QPalette(listing.palette())
|
|
palette.setColor(QPalette.ColorRole.Text,QColor('#ffff00'))
|
|
palette.setColor(QPalette.ColorRole.HighlightedText,QColor('#00ff00'))
|
|
option.palette=palette
|
|
if selected:option.state|=QStyle.StateFlag.State_Selected
|
|
picture=QImage(220,76,QImage.Format.Format_ARGB32);picture.fill(QColor('#000000'))
|
|
painter=QPainter(picture)
|
|
TaskItemDelegate(listing).paint(painter,option,model.index(0,0));painter.end()
|
|
expected=QColor('#00ff00'if selected else'#ffff00')
|
|
# Check actual painted title glyphs, not only the widget's palette property.
|
|
assert sum(picture.pixelColor(x,y)==expected for x in range(43,190)for y in range(11,34))>5
|