-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_remote.py
86 lines (70 loc) · 2.38 KB
/
test_remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
from StringIO import StringIO
import unittest
import remote
class TestRemote(unittest.TestCase):
def setUp(self):
self.remote = remote.Remote(serial_class=MockSerial)
def test_init(self):
self.assertEqual(
self.get_serial_buf().getvalue(),
''.join(chr(i << 4) for i in xrange(2 * remote.ROWS)))
for pixel in remote.PIXELS:
self.assertFalse(self.remote.get_pixel(pixel))
def test_first_block(self):
sb = self.get_serial_buf()
sb.truncate(0)
self.remote.set_pixel((0, 0), True)
self.remote.flush_pixels()
self.assertEqual(sb.getvalue(), '\x08')
def test_second_block(self):
sb = self.get_serial_buf()
sb.truncate(0)
self.remote.set_pixel((4, 0), True)
self.remote.flush_pixels()
self.assertEqual(sb.getvalue(), '\x18')
sb.truncate(0)
self.remote.set_pixel((5, 0), True)
self.remote.flush_pixels()
self.assertEqual(sb.getvalue(), '\x1c')
def test_empty_flush(self):
sb = self.get_serial_buf()
sb.truncate(0)
self.remote.flush_pixels()
self.assertEquals(sb.getvalue(), '')
def test_no_change_flush(self):
sb = self.get_serial_buf()
sb.truncate(0)
self.remote.set_pixel((0, 0), False)
self.remote.flush_pixels()
self.assertEquals(sb.getvalue(), '')
def test_full(self):
sb = self.get_serial_buf()
sb.truncate(0)
for pixel in remote.PIXELS:
self.remote.set_pixel(pixel, True)
for pixel in remote.PIXELS:
self.assertTrue(self.remote.get_pixel(pixel))
self.remote.flush_pixels()
self.assertEqual(
sb.getvalue(),
''.join(chr((i << 4) | 0x0F) for i in xrange(2 * remote.ROWS)))
def get_serial_buf(self):
return self.remote.port.dut2tc
class MockSerial(object):
def __init__(self, port, speed, timeout):
self.dut2tc = StringIO()
self.tried_read = False
def write(self, data):
pp = self.dut2tc.tell()
self.dut2tc.write(data)
self.dut2tc.seek(pp)
def read(self):
if self.tried_read:
self.tried_read = False
return 'B'
else:
self.tried_read = True
return ''
if __name__ == '__main__':
unittest.main()