test_neosegment.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2017-18 Richard Hull and contributors
  4. # See LICENSE.rst for details.
  5. import pytest
  6. from PIL import Image
  7. from luma.led_matrix.device import neosegment
  8. from luma.core.device import dummy
  9. from luma.core.render import canvas
  10. import luma.core.error
  11. from helpers import assert_identical_image, get_reference_image
  12. def test_invalid_dimensions():
  13. with pytest.raises(luma.core.error.DeviceDisplayModeError) as ex:
  14. neosegment(width=3, device=dummy(width=6, height=7))
  15. assert "Unsupported display mode: width=3" in str(ex.value)
  16. def test_overflow():
  17. with pytest.raises(OverflowError) as ex:
  18. neoseg = neosegment(width=6, device=dummy(width=6, height=7))
  19. neoseg.text = "TooBig!"
  20. assert "Device's capabilities insufficient for value 'TooBig!'" in str(ex.value)
  21. def test_settext_nocolor():
  22. neoseg = neosegment(width=6, device=dummy(width=6, height=7))
  23. neoseg.text = "888888"
  24. ref = dummy(width=6, height=7)
  25. with canvas(ref) as draw:
  26. draw.rectangle(ref.bounding_box, fill="white")
  27. assert_identical_image(ref.image, neoseg.device.image)
  28. def test_settext_singlecolor():
  29. neoseg = neosegment(width=6, device=dummy(width=6, height=7))
  30. neoseg.text = "888888"
  31. neoseg.color = "red"
  32. ref = dummy(width=6, height=7)
  33. with canvas(ref) as draw:
  34. draw.rectangle(ref.bounding_box, fill="red")
  35. assert_identical_image(ref.image, neoseg.device.image)
  36. def test_settext_charcolor():
  37. neoseg = neosegment(width=6, device=dummy(width=6, height=7))
  38. neoseg.text = "888888"
  39. neoseg.color[2] = "green"
  40. ref = dummy(width=6, height=7)
  41. with canvas(ref) as draw:
  42. draw.rectangle(ref.bounding_box, fill="white")
  43. draw.rectangle([2, 0, 2, 6], fill="green")
  44. assert_identical_image(ref.image, neoseg.device.image)
  45. def test_settext_replacechars():
  46. neoseg = neosegment(width=6, device=dummy(width=6, height=7))
  47. neoseg.text = "888888"
  48. neoseg.text[2:4] = " "
  49. ref = dummy(width=6, height=7)
  50. with canvas(ref) as draw:
  51. draw.rectangle(ref.bounding_box, fill="white")
  52. draw.rectangle([2, 0, 3, 6], fill="black")
  53. assert_identical_image(ref.image, neoseg.device.image)
  54. def test_segment_mapper():
  55. img_path = get_reference_image('neosegment.png')
  56. with open(img_path, 'rb') as img:
  57. reference = Image.open(img)
  58. neoseg = neosegment(width=6, device=dummy(width=6, height=7))
  59. neoseg.color = ["red", "green", "blue", "yellow", "cyan", "magenta"]
  60. neoseg.text = "012345"
  61. assert_identical_image(reference, neoseg.device.image)
  62. def test_unknown_char():
  63. neoseg = neosegment(width=6, device=dummy(width=6, height=7))
  64. neoseg.text = "888888"
  65. neoseg.text[2:4] = "&\x7f"
  66. neoseg.color[2:4] = ["orange", "orange"]
  67. ref = dummy(width=6, height=7)
  68. with canvas(ref) as draw:
  69. draw.rectangle(ref.bounding_box, fill="white")
  70. draw.rectangle([2, 0, 3, 6], fill="black")
  71. draw.rectangle([2, 1, 3, 1], fill="orange")
  72. assert_identical_image(ref.image, neoseg.device.image)