test_backend_macosx.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os
  2. import threading
  3. from pathlib import Path
  4. import pytest
  5. from unittest import mock
  6. import matplotlib as mpl
  7. import matplotlib.pyplot as plt
  8. from matplotlib.testing import subprocess_run_helper
  9. _test_timeout = 60
  10. def _test_cached_renderer():
  11. # Make sure that figures have an associated renderer after
  12. # a fig.canvas.draw() call
  13. fig = plt.figure(1)
  14. fig.canvas.draw()
  15. assert fig.canvas.get_renderer()._renderer is not None
  16. fig = plt.figure(2)
  17. fig.draw_without_rendering()
  18. assert fig.canvas.get_renderer()._renderer is not None
  19. @pytest.mark.backend('macosx', skip_on_importerror=True)
  20. def test_cached_renderer():
  21. subprocess_run_helper(_test_cached_renderer, timeout=_test_timeout,
  22. extra_env={"MPLBACKEND": "macosx"})
  23. def _test_savefig_rcparam():
  24. tmp_path = Path(os.environ["TEST_SAVEFIG_PATH"])
  25. def new_choose_save_file(title, directory, filename):
  26. # Replacement function instead of opening a GUI window
  27. # Make a new directory for testing the update of the rcParams
  28. assert directory == str(tmp_path)
  29. os.makedirs(f"{directory}/test")
  30. return f"{directory}/test/{filename}"
  31. fig = plt.figure()
  32. with (mock.patch("matplotlib.backends._macosx.choose_save_file",
  33. new_choose_save_file),
  34. mpl.rc_context({"savefig.directory": tmp_path})):
  35. fig.canvas.toolbar.save_figure()
  36. # Check the saved location got created
  37. save_file = f"{tmp_path}/test/{fig.canvas.get_default_filename()}"
  38. assert os.path.exists(save_file)
  39. # Check the savefig.directory rcParam got updated because
  40. # we added a subdirectory "test"
  41. assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test"
  42. @pytest.mark.backend('macosx', skip_on_importerror=True)
  43. def test_savefig_rcparam(tmp_path):
  44. subprocess_run_helper(
  45. _test_savefig_rcparam, timeout=_test_timeout,
  46. extra_env={"MPLBACKEND": "macosx", "TEST_SAVEFIG_PATH": tmp_path})
  47. @pytest.mark.backend('macosx', skip_on_importerror=True)
  48. def test_ipython():
  49. from matplotlib.testing import ipython_in_subprocess
  50. ipython_in_subprocess("osx", {(8, 24): "macosx", (7, 0): "MacOSX"})
  51. def _test_save_figure_return():
  52. fig, ax = plt.subplots()
  53. ax.imshow([[1]])
  54. prop = "matplotlib.backends._macosx.choose_save_file"
  55. with mock.patch(prop, return_value="foobar.png"):
  56. fname = fig.canvas.manager.toolbar.save_figure()
  57. os.remove("foobar.png")
  58. assert fname == "foobar.png"
  59. with mock.patch(prop, return_value=None):
  60. fname = fig.canvas.manager.toolbar.save_figure()
  61. assert fname is None
  62. @pytest.mark.backend('macosx', skip_on_importerror=True)
  63. def test_save_figure_return():
  64. subprocess_run_helper(_test_save_figure_return, timeout=_test_timeout,
  65. extra_env={"MPLBACKEND": "macosx"})
  66. def _test_create_figure_on_worker_thread_fails():
  67. def create_figure():
  68. warn_msg = "Matplotlib GUI outside of the main thread will likely fail."
  69. err_msg = "Cannot create a GUI FigureManager outside the main thread"
  70. with pytest.warns(UserWarning, match=warn_msg):
  71. with pytest.raises(RuntimeError, match=err_msg):
  72. plt.gcf()
  73. worker = threading.Thread(target=create_figure)
  74. worker.start()
  75. worker.join()
  76. @pytest.mark.backend('macosx', skip_on_importerror=True)
  77. def test_create_figure_on_worker_thread_fails():
  78. subprocess_run_helper(
  79. _test_create_figure_on_worker_thread_fails,
  80. timeout=_test_timeout,
  81. extra_env={"MPLBACKEND": "macosx"}
  82. )