Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import threading
2import unittest
3from unittest import mock
4from pi3bar.io import read_sys, write_sys, I3BarEventThread
7@mock.patch('sys.stdin')
8class ReadSysTestCase(unittest.TestCase):
9 def test_i3bar_first_line(self, mock_stdin):
10 mock_stdin.readline.return_value = '[\n'
11 self.assertEqual('[', read_sys())
13 def test_i3bar_second_line(self, mock_stdin):
14 mock_stdin.readline.return_value = '[{"name": "plugin name", "button": 1, "x": 1831, "y": 1072}]\n'
15 self.assertEqual(
16 '[{"name": "plugin name", "button": 1, "x": 1831, "y": 1072}]',
17 read_sys()
18 )
20 def test_i3bar_event(self, mock_stdin):
21 mock_stdin.readline.return_value = ',[{"name": "plugin name", "button": 1, "x": 1831, "y": 1072}]\n'
22 self.assertEqual(
23 '[{"name": "plugin name", "button": 1, "x": 1831, "y": 1072}]',
24 read_sys()
25 )
28@mock.patch('sys.stdout')
29class WriteSysTestCase(unittest.TestCase):
30 def test(self, mock_stdout):
31 write_sys('test')
32 mock_stdout.write.assert_called_with('test')
33 self.assertTrue(mock_stdout.flush.called)
36@mock.patch('select.select')
37@mock.patch('sys.stdin')
38class I3BarEventThreadTestCase(unittest.TestCase):
39 def setUp(self):
40 self.mock_on_click = mock.Mock()
41 self.i3bar_event_thread = I3BarEventThread(self.mock_on_click, mock.MagicMock())
43 def test_init(self, mock_stdin, mock_select):
44 self.assertEqual(self.mock_on_click, self.i3bar_event_thread.on_click)
45 self.assertEqual(True, self.i3bar_event_thread.CONTINUE)
46 self.assertIsInstance(self.i3bar_event_thread, threading.Thread)
48 def test(self, mock_stdin, mock_select):
49 values = [
50 '[\n',
51 '{"name": "test", "instance": null, "button": 1, "x": 1872, "y": 1076}\n'
52 ]
54 def readline_side_effect():
55 self.i3bar_event_thread.CONTINUE = False
56 mock_select.return_value = ([], [], [])
57 return values.pop(0)
58 mock_stdin.fileno.return_value = 0
59 mock_select.return_value = ([mock_stdin], [], [])
60 mock_stdin.readline.side_effect = readline_side_effect
61 self.i3bar_event_thread.run()
63 self.mock_on_click.assert_called_once_with('test', None, 1, 1872, 1076)
65 def test_second_line(self, mock_stdin, mock_select):
66 values = [
67 '[\n',
68 '{"name": "test", "instance": null, "button": 1, "x": 1872, "y": 1076}\n',
69 '{"name": "test", "instance": "2", "button": 3, "x": 5, "y": 8}\n'
70 ]
72 def readline_side_effect():
73 if len(values) == 1:
74 self.i3bar_event_thread.CONTINUE = False
75 mock_select.return_value = ([], [], [])
76 return values.pop(0)
77 mock_stdin.fileno.return_value = 0
78 mock_select.return_value = ([mock_stdin], [], [])
79 mock_stdin.readline.side_effect = readline_side_effect
80 self.i3bar_event_thread.run()
82 self.assertEqual(2, self.mock_on_click.call_count)
83 self.mock_on_click.assert_has_calls([
84 mock.call('test', None, 1, 1872, 1076),
85 mock.call('test', "2", 3, 5, 8),
86 ])
88 def test_close(self, mock_stdin, mock_select):
89 def readline_side_effect():
90 mock_select.return_value = ([], [], [])
91 return '\n'
92 mock_select.return_value = ([mock_stdin], [], [])
93 mock_stdin.fileno.return_value = 0
94 mock_stdin.readline.side_effect = readline_side_effect
95 self.i3bar_event_thread.run()
97 self.assertEqual(False, self.i3bar_event_thread.CONTINUE)
98 self.assertEqual(2, mock_select.call_count)
99 self.assertEqual(1, mock_stdin.readline.call_count)