test_widget_image.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """Test Image widget"""
  4. import io
  5. import os
  6. from ipywidgets import Image
  7. import hashlib
  8. import pkgutil
  9. import tempfile
  10. from contextlib import contextmanager
  11. # Data
  12. @contextmanager
  13. def get_logo_png():
  14. # Once the tests are not in the package, this context manager can be
  15. # replaced with the location of the actual file
  16. LOGO_DATA = pkgutil.get_data('ipywidgets.widgets.tests',
  17. 'data/jupyter-logo-transparent.png')
  18. handle, fname = tempfile.mkstemp()
  19. os.close(handle)
  20. with open(fname, 'wb') as f:
  21. f.write(LOGO_DATA)
  22. yield fname
  23. os.remove(fname)
  24. LOGO_PNG_DIGEST = '3ff9eafd7197083153e83339a72e7a335539bae189c33554c680e4382c98af02'
  25. def test_empty_image():
  26. # Empty images shouldn't raise any errors
  27. Image()
  28. def test_image_value():
  29. random_bytes = b'\x0ee\xca\x80\xcd\x9ak#\x7f\x07\x03\xa7'
  30. Image(value=random_bytes)
  31. def test_image_format():
  32. # Test that these format names don't throw an error
  33. Image(format='png')
  34. Image(format='jpeg')
  35. Image(format='url')
  36. def test_from_filename():
  37. with get_logo_png() as LOGO_PNG:
  38. img = Image.from_file(LOGO_PNG)
  39. assert_equal_hash(img.value, LOGO_PNG_DIGEST)
  40. def test_set_from_filename():
  41. img = Image()
  42. with get_logo_png() as LOGO_PNG:
  43. img.set_value_from_file(LOGO_PNG)
  44. assert_equal_hash(img.value, LOGO_PNG_DIGEST)
  45. def test_from_file():
  46. with get_logo_png() as LOGO_PNG:
  47. with open(LOGO_PNG, 'rb') as f:
  48. img = Image.from_file(f)
  49. assert_equal_hash(img.value, LOGO_PNG_DIGEST)
  50. def test_set_value_from_file():
  51. img = Image()
  52. with get_logo_png() as LOGO_PNG:
  53. with open(LOGO_PNG, 'rb') as f:
  54. img.set_value_from_file(f)
  55. assert_equal_hash(img.value, LOGO_PNG_DIGEST)
  56. def test_from_url_unicode():
  57. img = Image.from_url(u'https://jupyter.org/assets/main-logo.svg')
  58. assert img.value == b'https://jupyter.org/assets/main-logo.svg'
  59. def test_from_url_bytes():
  60. img = Image.from_url(b'https://jupyter.org/assets/main-logo.svg')
  61. assert img.value == b'https://jupyter.org/assets/main-logo.svg'
  62. def test_format_inference_filename():
  63. with tempfile.NamedTemporaryFile(suffix='.svg', delete=False) as f:
  64. name = f.name
  65. f.close() # Allow tests to run on Windows
  66. img = Image.from_file(name)
  67. assert img.format == 'svg+xml'
  68. def test_format_inference_file():
  69. with tempfile.NamedTemporaryFile(suffix='.gif', delete=False) as f:
  70. img = Image.from_file(f)
  71. assert img.format == 'gif'
  72. def test_format_inference_stream():
  73. # There's no way to infer the format, so it should default to png
  74. fstream = io.BytesIO(b'')
  75. img = Image.from_file(fstream)
  76. assert img.format == 'png'
  77. def test_serialize():
  78. fstream = io.BytesIO(b'123')
  79. img = Image.from_file(fstream)
  80. img_state = img.get_state()
  81. # for python27 it is a memoryview
  82. assert isinstance(img_state['value'], (bytes, memoryview))
  83. # make sure it is (for python 3), since that is what it will be once it comes off the wire
  84. img_state['value'] = memoryview(img_state['value'])
  85. # check that we can deserialize it and get back the original value
  86. img_copy = Image()
  87. img_copy.set_state(img_state)
  88. assert img.value == img_copy.value
  89. def test_format_inference_overridable():
  90. with tempfile.NamedTemporaryFile(suffix='.svg', delete=False) as f:
  91. name = f.name
  92. f.close() # Allow tests to run on Windows
  93. img = Image.from_file(name, format='gif')
  94. assert img.format == 'gif'
  95. def test_value_repr_length():
  96. with get_logo_png() as LOGO_PNG:
  97. with open(LOGO_PNG, 'rb') as f:
  98. img = Image.from_file(f)
  99. assert len(img.__repr__()) < 120
  100. assert img.__repr__().endswith("...')")
  101. def test_value_repr_url():
  102. img = Image.from_url(b'https://jupyter.org/assets/main-logo.svg')
  103. assert 'https://jupyter.org/assets/main-logo.svg' in img.__repr__()
  104. # Helper functions
  105. def get_hash_hex(byte_str):
  106. m = hashlib.new('sha256')
  107. m.update(byte_str)
  108. return m.hexdigest()
  109. def assert_equal_hash(byte_str, digest):
  110. assert get_hash_hex(byte_str) == digest