@@ -377,13 +377,50 @@ class TestSetDisplay:
377
377
378
378
def test_set_display (self ):
379
379
"""
380
- Test if pygmt.set_display updates the SHOW_CONFIG variable correctly.
380
+ Test if pygmt.set_display updates the SHOW_CONFIG variable correctly and
381
+ Figure.show opens the preview image in the correct way.
381
382
"""
382
- default_method = SHOW_CONFIG ["method" ] # Current default method
383
-
384
- for method in ("notebook" , "external" , "none" ):
385
- set_display (method = method )
386
- assert SHOW_CONFIG ["method" ] == method
383
+ default_method = SHOW_CONFIG ["method" ] # Store the current default method.
384
+
385
+ fig = Figure ()
386
+ fig .basemap (region = [0 , 3 , 6 , 9 ], projection = "X1c" , frame = True )
387
+
388
+ # Test the "notebook" display method.
389
+ set_display (method = "notebook" )
390
+ assert SHOW_CONFIG ["method" ] == "notebook"
391
+ if _HAS_IPYTHON :
392
+ with (
393
+ patch ("IPython.display.display" ) as mock_display ,
394
+ patch ("pygmt.figure.launch_external_viewer" ) as mock_viewer ,
395
+ ):
396
+ fig .show ()
397
+ assert mock_viewer .call_count == 0
398
+ assert mock_display .call_count == 1
399
+ else :
400
+ with pytest .raises (GMTError ):
401
+ fig .show ()
402
+
403
+ # Test the "external" display method
404
+ set_display (method = "external" )
405
+ assert SHOW_CONFIG ["method" ] == "external"
406
+ with patch ("pygmt.figure.launch_external_viewer" ) as mock_viewer :
407
+ fig .show ()
408
+ assert mock_viewer .call_count == 1
409
+ if _HAS_IPYTHON :
410
+ with patch ("IPython.display.display" ) as mock_display :
411
+ fig .show ()
412
+ assert mock_display .call_count == 0
413
+
414
+ # Test the "none" display method.
415
+ set_display (method = "none" )
416
+ assert SHOW_CONFIG ["method" ] == "none"
417
+ with patch ("pygmt.figure.launch_external_viewer" ) as mock_viewer :
418
+ fig .show ()
419
+ assert mock_viewer .call_count == 0
420
+ if _HAS_IPYTHON :
421
+ with patch ("IPython.display.display" ) as mock_display :
422
+ fig .show ()
423
+ assert mock_display .call_count == 0
387
424
388
425
# Setting method to None should revert it to the default method.
389
426
set_display (method = None )
0 commit comments