2015-09-21 14:00:35 +00:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-12-21 19:11:11 +00:00
|
|
|
try:
|
|
|
|
from io import BytesIO
|
|
|
|
except ImportError:
|
|
|
|
from cStringIO import StringIO as BytesIO
|
2015-09-21 14:00:35 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import qubesimgconverter
|
|
|
|
|
|
|
|
class TestCaseImage(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.rgba = \
|
2017-12-21 19:11:11 +00:00
|
|
|
b'\x00\x00\x00\xff' b'\xff\x00\x00\xff' \
|
|
|
|
b'\x00\xff\x00\xff' b'\x00\x00\x00\xff'
|
2015-09-21 14:00:35 +00:00
|
|
|
self.size = (2, 2)
|
|
|
|
|
|
|
|
self.image = qubesimgconverter.Image(rgba=self.rgba, size=self.size)
|
|
|
|
|
|
|
|
def test_00_init(self):
|
|
|
|
self.assertEqual(self.image._rgba, self.rgba)
|
|
|
|
self.assertEqual(self.image._size, self.size)
|
|
|
|
|
|
|
|
def test_01_tint(self):
|
|
|
|
image = self.image.tint('#0000ff')
|
|
|
|
|
|
|
|
self.assertEqual(image._rgba,
|
2017-12-21 19:11:11 +00:00
|
|
|
b'\x00\x00\x3f\xff' b'\x00\x00\xff\xff'
|
|
|
|
b'\x00\x00\xff\xff' b'\x00\x00\x3f\xff')
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
def test_10_get_from_stream(self):
|
2017-12-21 19:11:11 +00:00
|
|
|
io = BytesIO('{0[0]} {0[1]}\n'.format(self.size).encode() + self.rgba)
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
image = qubesimgconverter.Image.get_from_stream(io)
|
|
|
|
|
|
|
|
self.assertEqual(image._rgba, self.rgba)
|
|
|
|
self.assertEqual(image._size, self.size)
|
|
|
|
|
|
|
|
def test_11_get_from_stream_malformed(self):
|
2017-12-21 19:11:11 +00:00
|
|
|
io = BytesIO('{0[0]} {0[1]}\n'.format(self.size).encode() +
|
|
|
|
self.rgba[:-1]) # one byte too short
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
image = qubesimgconverter.Image.get_from_stream(io)
|
|
|
|
|
|
|
|
def test_12_get_from_stream_too_big(self):
|
2017-12-21 19:11:11 +00:00
|
|
|
io = BytesIO('{0[0]} {0[1]}\n'.format(self.size).encode() + self.rgba) # 2x2
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
image = qubesimgconverter.Image.get_from_stream(io, max_width=1)
|
|
|
|
|
|
|
|
io.seek(0)
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
image = qubesimgconverter.Image.get_from_stream(io, max_height=1)
|
|
|
|
|
|
|
|
class TestCaseFunctionsAndConstants(unittest.TestCase):
|
|
|
|
def test_00_imghdrlen(self):
|
|
|
|
self.assertEqual(qubesimgconverter.imghdrlen(8, 15), len('8 15\n'))
|
2017-08-11 13:43:41 +00:00
|
|
|
self.assertEqual(qubesimgconverter.imghdrlen(100, 100), len('100 100\n'))
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
def test_01_re_imghdr(self):
|
2017-12-21 19:11:11 +00:00
|
|
|
self.assertTrue(qubesimgconverter.re_imghdr.match(b'8 15\n'))
|
|
|
|
self.assertIsNone(qubesimgconverter.re_imghdr.match(b'8 15'))
|
|
|
|
self.assertIsNone(qubesimgconverter.re_imghdr.match(b'815\n'))
|
|
|
|
self.assertIsNone(qubesimgconverter.re_imghdr.match(b'x yx\n'))
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
def test_10_hex_to_float_result_00(self):
|
2017-12-21 18:44:46 +00:00
|
|
|
self.assertEqual(qubesimgconverter.hex_to_int('#000000'), (0, 0, 0))
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
def test_11_hex_to_float_result_ff(self):
|
2017-12-21 18:44:46 +00:00
|
|
|
self.assertEqual(qubesimgconverter.hex_to_int('0xffffff'),
|
|
|
|
(0xff, 0xff, 0xff))
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
def test_12_hex_to_float_depth_3_not_implemented(self):
|
2017-12-21 18:44:46 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
qubesimgconverter.hex_to_int('123456', depth=3)
|
2015-09-21 14:00:35 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|