test_image.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import os
  12. import pathlib
  13. from importlib import util
  14. from taipy.gui import Gui
  15. def test_image_url_md(gui: Gui, test_client, helpers):
  16. gui._bind_var_val("content", "some_url")
  17. md_string = "<|{content}|image|>"
  18. expected_list = [
  19. "<Image",
  20. "content={_TpCi_tpec_TpExPr_content_TPMDL_0}",
  21. 'defaultContent="some_url"',
  22. ]
  23. helpers.test_control_md(gui, md_string, expected_list)
  24. def test_image_file_md(gui: Gui, test_client, helpers):
  25. with open((pathlib.Path(__file__).parent.parent / "resources" / "fred.png").resolve(), "rb") as content:
  26. gui._bind_var_val("content", content.read())
  27. md_string = "<|{content}|image|>"
  28. expected_list = [
  29. "<Image",
  30. 'defaultContent="data:image/png;base64,',
  31. ]
  32. if not util.find_spec("magic"):
  33. expected_list = ["<Image", 'defaultContent="/taipy-content/taipyStatic0/TaiPyContent.', ".bin"]
  34. helpers.test_control_md(gui, md_string, expected_list)
  35. def test_image_path_md(gui: Gui, test_client, helpers):
  36. gui._bind_var_val("content", str((pathlib.Path(__file__).parent.parent / "resources" / "fred.png").resolve()))
  37. md_string = "<|{content}|image|>"
  38. expected_list = [
  39. "<Image",
  40. 'defaultContent="/taipy-content/taipyStatic0/fred.png',
  41. ]
  42. helpers.test_control_md(gui, md_string, expected_list)
  43. def test_image_bad_file_md(gui: Gui, test_client, helpers):
  44. with open(os.path.abspath(__file__), "rb") as content:
  45. gui._bind_var_val("content", content.read())
  46. md_string = "<|{content}|image|>"
  47. expected_list = [
  48. "<Image",
  49. 'defaultContent="Invalid content: text/x',
  50. ]
  51. if not util.find_spec("magic"):
  52. expected_list = ["<Image", 'defaultContent="/taipy-content/taipyStatic0/TaiPyContent.', ".bin"]
  53. helpers.test_control_md(gui, md_string, expected_list)
  54. def test_image_url_html(gui: Gui, test_client, helpers):
  55. gui._bind_var_val("content", "some_url")
  56. html_string = '<taipy:image content="{content}" on_action="action" />'
  57. expected_list = [
  58. "<Image",
  59. 'defaultContent="some_url"',
  60. 'onAction="action"',
  61. ]
  62. helpers.test_control_html(gui, html_string, expected_list)