~laney/ubuntu-system-settings/upower0.99

« back to all changes in this revision

Viewing changes to tests/test_push_helper.py.in

Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
 
1
#!/usr/bin/python3
3
2
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
4
3
# Copyright 2014 Canonical
5
4
#
14
13
import sys
15
14
import tempfile
16
15
import unittest
 
16
from unittest import mock
 
17
 
 
18
HELPER_DIR = '@CMAKE_CURRENT_SOURCE_DIR@/../push-helper/'
 
19
sys.path.append(HELPER_DIR)
 
20
import software_updates_helper
 
21
 
 
22
 
 
23
class TestingSystemImage(software_updates_helper.SystemImage):
 
24
    def setup(self):
 
25
        pass
17
26
 
18
27
 
19
28
class PushHelperTests(unittest.TestCase):
22
31
    def setUp(self):
23
32
        super(PushHelperTests, self).setUp()
24
33
        self.tmp_dir = tempfile.mkdtemp(suffix='push-helper', prefix='tests')
25
 
        self.helper_path = '@CMAKE_CURRENT_SOURCE_DIR@/../push-helper/' + \
26
 
            'software-updates-helper.py'
 
34
        self.helper_path = HELPER_DIR + 'software_updates_helper.py'
27
35
 
28
36
    def tearDown(self):
29
37
        super(PushHelperTests, self).tearDown()
30
38
        shutil.rmtree(self.tmp_dir)
31
39
 
32
40
    def run_push_helper(self, input_fname, output_fname):
33
 
        subprocess.call([self.helper_path, input_fname, output_fname],
 
41
        subprocess.call(["python3", self.helper_path, input_fname, output_fname],
34
42
                        stdout=subprocess.PIPE)
35
43
 
36
44
    def create_input_file(self, filename, content):
49
57
        self.assertEqual(card['actions'], ['settings:///system/system-update'])
50
58
        self.assertEqual(card['persist'], True)
51
59
        self.assertEqual(card['body'], 'Tap to open the system updater.')
52
 
        self.assertEqual(card['popup'], True)
 
60
        self.assertEqual(card.get('popup', False), False)
53
61
        emblem_counter = notif['notification']['emblem-counter']
54
62
        self.assertEqual(emblem_counter, {'visible': True, 'count': 1})
55
63
        vibrate = notif['notification']['vibrate']
58
66
    def test_update_broadcast(self):
59
67
        """Default system-update broadcast."""
60
68
        input_f = self.create_input_file('bcast_in',
61
 
                                         '{"daily/mako": [200, ""]}')
 
69
                                         '"system-image-update"')
62
70
        output_f = os.path.join(self.tmp_dir, 'bcast_out')
63
71
        self.run_push_helper(input_f, output_f)
64
72
        with open(output_f, 'r') as fd:
67
75
 
68
76
    def test_valid_json(self):
69
77
        """Handle a valid json input."""
70
 
        input_f = self.create_input_file('valid_json_in', '"null"')
 
78
        input_f = self.create_input_file('valid_json_in', '"testing"')
71
79
        output_f = os.path.join(self.tmp_dir, 'valid_json_out')
72
80
        self.run_push_helper(input_f, output_f)
73
81
        with open(output_f, 'r') as fd:
74
82
            output = json.load(fd)
75
 
        self.assertSystemUpdateNotification(output)
76
 
 
 
83
        self.assertEqual(output, {"testing": True})
 
84
 
 
85
    def test_system_image_run(self):
 
86
        """Check that run looks sane"""
 
87
        s = TestingSystemImage()
 
88
        s.sysimg = mock.Mock(name="sysimg")
 
89
        s.loop = mock.Mock(name="loop")
 
90
        s.run()
 
91
        # check the main loop was run
 
92
        s.loop.run.assert_called_once_with()
 
93
        # check CheckForUpdate was called
 
94
        s.sysimg.CheckForUpdate.assert_called_once_with()
 
95
        # and connect_to_signal
 
96
        s.sysimg.connect_to_signal.assert_any_call("UpdateDownloaded",
 
97
                                                   s.downloaded_cb)
 
98
        s.sysimg.connect_to_signal.assert_any_call("UpdateFailed",
 
99
                                                   s.failed_cb)
 
100
        s.sysimg.connect_to_signal.assert_any_call("UpdateAvailableStatus",
 
101
                                                   s.available_cb)
 
102
        self.assertEqual(s.notify, False)
 
103
 
 
104
    def test_available_and_downloading(self):
 
105
        """check that available_cb when available and d'loading just returns"""
 
106
        s = TestingSystemImage()
 
107
        s.quit = mock.Mock(name="quit")
 
108
 
 
109
        self.assertEqual(s.notify, False)
 
110
        # available and downloading; returns without calling quit
 
111
        s.available_cb(True, True)
 
112
        self.assertEqual(s.notify, False)
 
113
        self.assertEqual(s.quit.called, False)
 
114
 
 
115
    def test_available_not_downloading(self):
 
116
        """check that available_cb when available and not downloading
 
117
        sets notify and quits"""
 
118
        s = TestingSystemImage()
 
119
        s.quit = mock.Mock(name="quit")
 
120
 
 
121
        self.assertEqual(s.notify, False)
 
122
        # available and not downloading; quits with notification
 
123
        s.available_cb(True, False)
 
124
        self.assertEqual(s.notify, True)
 
125
        s.quit.assert_called_once_with()
 
126
 
 
127
    def test_not_available(self):
 
128
        """check that available_cb quits when not available"""
 
129
        s = TestingSystemImage()
 
130
        s.quit = mock.Mock(name="quit")
 
131
 
 
132
        self.assertEqual(s.notify, False)
 
133
        # not available; quits without notifying
 
134
        s.available_cb(False, False)
 
135
        self.assertEqual(s.notify, False)
 
136
        s.quit.assert_called_once_with()
 
137
 
 
138
    def test_downloaded_cb(self):
 
139
        """check that on download, notify is set to True and quit is called"""
 
140
        s = TestingSystemImage()
 
141
        s.quit = mock.Mock(name="quit")
 
142
 
 
143
        self.assertEqual(s.notify, False)
 
144
        s.downloaded_cb()
 
145
        self.assertEqual(s.notify, True)
 
146
        s.quit.assert_called_once_with()
 
147
 
 
148
    def test_failed_cb(self):
 
149
        """check that on failure, notify is set to False and quit is called"""
 
150
        s = TestingSystemImage()
 
151
        s.quit = mock.Mock(name="quit")
 
152
 
 
153
        self.assertEqual(s.notify, False)
 
154
        s.failed_cb()
 
155
        self.assertEqual(s.notify, False)
 
156
        s.quit.assert_called_once_with()
 
157
 
 
158
    def test_quit_no_notify(self):
 
159
        """Check that quit withlooks sane"""
 
160
        s = TestingSystemImage()
 
161
        s.postal = mock.Mock(name="sysimg")
 
162
        s.loop = mock.Mock(name="loop")
 
163
        s.notify = False
 
164
        s.quit()
 
165
        self.assertEqual(s.postal.Post.called, False)
 
166
        self.assertEqual(s.postal.ClearPersistent.called, False)
 
167
        s.loop.quit.assert_called_once_with()
 
168
 
 
169
    def test_quit_with_notify(self):
 
170
        """Check that quit withlooks sane"""
 
171
        s = TestingSystemImage()
 
172
        s.postal = mock.Mock(name="sysimg")
 
173
        s.loop = mock.Mock(name="loop")
 
174
        s.notify = True
 
175
        s.quit()
 
176
        s.postal.Post.assert_called_once_with("_ubuntu-system-settings",
 
177
                                              '"system-image-update"')
 
178
        s.postal.ClearPersistent.assert_called_once_with(
 
179
            "_ubuntu-system-settings", "system-image-update")
 
180
        s.loop.quit.assert_called_once_with()
77
181
 
78
182
if __name__ == '__main__':
79
183
    unittest.main(