@@ -179,3 +179,57 @@ def test_flash_without_drive_errors_when_none_detected(qapp, tmp_path):
179179 assert "plug one in first" in w ._progress ._error .text ()
180180 finally :
181181 w ._shutdown ()
182+
183+
184+ def _screen_area ():
185+ from PyQt6 .QtGui import QGuiApplication
186+
187+ screen = QGuiApplication .primaryScreen ()
188+ return screen .availableGeometry () if screen is not None else None
189+
190+
191+ def test_restored_geometry_clamped_inside_screen (qapp , tmp_path ):
192+ """A window whose saved position hangs off the right edge must be
193+ pulled back so nothing is cut off at the border."""
194+ available = _screen_area ()
195+ if available is None :
196+ pytest .skip ("no screen in this environment" )
197+
198+ w = _make_window (qapp , tmp_path )
199+ try :
200+ w .resize (
201+ min (900 , available .width () - 20 ),
202+ min (580 , available .height () - 20 ),
203+ )
204+ w .move (available .right () - w .width () // 2 + 150 , available .top () + 40 )
205+ w ._clamp_to_screen ()
206+ frame = w .frameGeometry ()
207+ assert frame .left () >= available .left ()
208+ assert frame .right () <= available .right ()
209+ assert frame .top () >= available .top ()
210+ assert frame .bottom () <= available .bottom ()
211+ finally :
212+ w ._shutdown ()
213+
214+
215+ def test_fully_offscreen_geometry_centered_on_screen (qapp , tmp_path ):
216+ """A saved position outside every screen must fall back to a centered
217+ placement instead of opening unreachable."""
218+ available = _screen_area ()
219+ if available is None :
220+ pytest .skip ("no screen in this environment" )
221+
222+ w = _make_window (qapp , tmp_path )
223+ try :
224+ w .resize (
225+ min (900 , available .width () - 20 ),
226+ min (580 , available .height () - 20 ),
227+ )
228+ w .move (available .right () + 500 , available .top () + 100 )
229+ w ._clamp_to_screen ()
230+ frame = w .frameGeometry ()
231+ assert frame .intersects (available )
232+ assert abs (frame .center ().x () - available .center ().x ()) <= 3
233+ assert abs (frame .center ().y () - available .center ().y ()) <= 3
234+ finally :
235+ w ._shutdown ()
0 commit comments