Hide keyboard shortcuts

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 unittest 

2from unittest import mock 

3from pi3bar.plugins.battery import Battery 

4 

5 

6class BatteryTestCase(unittest.TestCase): 

7 def test_default_values(self): 

8 plugin = Battery() 

9 self.assertEqual('/sys/class/power_supply/BAT0/capacity', plugin.capacity_path) 

10 self.assertEqual('/sys/class/power_supply/BAT0/status', plugin.status_path) 

11 self.assertEqual(30, plugin.warning_capacity) 

12 self.assertEqual(15, plugin.critical_capacity) 

13 self.assertEqual('#ffff00', plugin.warning_color) 

14 self.assertIsNone(plugin.warning_background) 

15 self.assertIsNone(plugin.critical_color) 

16 self.assertEqual('#ff0000', plugin.critical_background) 

17 

18 def test_get_capacity(self): 

19 plugin = Battery() 

20 

21 m = mock.mock_open(read_data='100') 

22 m.return_value.readline.return_value = '100' # py33 

23 with mock.patch('pi3bar.plugins.battery.open', m, create=True): 

24 capacity = plugin.get_capacity() 

25 

26 m.assert_called_once_with('/sys/class/power_supply/BAT0/capacity', 'r') 

27 self.assertEqual(100, capacity) 

28 

29 def test_get_status(self): 

30 plugin = Battery() 

31 

32 m = mock.mock_open(read_data='Full') 

33 m.return_value.readline.return_value = 'Full' # py33 

34 with mock.patch('pi3bar.plugins.battery.open', m, create=True): 

35 status = plugin.get_status() 

36 

37 m.assert_called_once_with('/sys/class/power_supply/BAT0/status', 'r') 

38 self.assertEqual('Full', status) 

39 

40 

41@mock.patch.object(Battery, 'get_capacity') 

42@mock.patch.object(Battery, 'get_status') 

43class BatteryCycleTestCase(unittest.TestCase): 

44 def test(self, mock_get_status, mock_get_capacity): 

45 mock_get_capacity.return_value = 100 

46 mock_get_status.return_value = 'Full' 

47 plugin = Battery() 

48 plugin.cycle() 

49 self.assertEqual('Full 100%', plugin.full_text) 

50 self.assertEqual('100%', plugin.short_text) 

51 self.assertIsNone(plugin.color) 

52 self.assertIsNone(plugin.background) 

53 

54 def test_warning_capacity(self, mock_get_status, mock_get_capacity): 

55 mock_get_capacity.return_value = 25 

56 mock_get_status.return_value = 'Discharging' 

57 plugin = Battery() 

58 plugin.cycle() 

59 self.assertEqual('Discharging 25%', plugin.full_text) 

60 self.assertEqual('25%', plugin.short_text) 

61 self.assertEqual(plugin.warning_color, plugin.color) 

62 self.assertEqual(plugin.warning_background, plugin.background) 

63 self.assertEqual('#ffff00', plugin.color) 

64 self.assertIsNone(plugin.background) 

65 

66 def test_critical_capacity(self, mock_get_status, mock_get_capacity): 

67 mock_get_capacity.return_value = 10 

68 mock_get_status.return_value = 'Discharging' 

69 plugin = Battery() 

70 plugin.cycle() 

71 self.assertEqual('Discharging 10%', plugin.full_text) 

72 self.assertEqual('10%', plugin.short_text) 

73 self.assertEqual(plugin.critical_color, plugin.color) 

74 self.assertEqual(plugin.critical_background, plugin.background) 

75 self.assertIsNone(plugin.color) 

76 self.assertEqual('#ff0000', plugin.background)