~mandel/ubuntuone-control-panel/auto-update-looping-call

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/gui/qt/tests/test_gui.py

  • Committer: Manuel de la Pena
  • Date: 2012-03-30 18:25:09 UTC
  • Revision ID: manuel.delapena@canonical.com-20120330182509-pq6v2q34b3343xxq
Added the tests to the correct location. Updated setup.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
        self.called = (args, kwargs)
36
36
 
37
37
 
 
38
class FakeMainWindow(object):
 
39
    """Fake window."""
 
40
 
 
41
    def __init__(self, close_callback=None):
 
42
        """Create a new instance."""
 
43
        self.called = []
 
44
        self.close_callback = None
 
45
 
 
46
    def __call__(self, *args, **kwargs):
 
47
        """Make the object callable to fake a constructor."""
 
48
        return self
 
49
 
 
50
    def show(self):
 
51
        """Fake show the window."""
 
52
        self.called.append(('show',))
 
53
 
 
54
 
 
55
class FakeTrayIcon(object):
 
56
    """Fake tray icon."""
 
57
 
 
58
    def __init__(self, window=None):
 
59
        """Create a new instance."""
 
60
        self.auto_update = None
 
61
        self.window = None
 
62
 
 
63
    def __call__(self, window, *args, **kwargs):
 
64
        """Make the object callable to fake a cosntructor."""
 
65
        self.window = window
 
66
        return self
 
67
 
 
68
    def get_auto_update_lc(self):
 
69
        """Get the auto_update_lc."""
 
70
        return self.auto_update
 
71
 
 
72
    def set_auto_update_lc(self, value):
 
73
        """Set the auto_update_lc."""
 
74
        self.auto_update = value
 
75
 
 
76
    auto_update_lc = property(get_auto_update_lc,
 
77
                              set_auto_update_lc)
 
78
 
 
79
 
 
80
class FakeLoopingCall(object):
 
81
    """Fake a looping call."""
 
82
 
 
83
    def __init__(self):
 
84
        """Create a new instance."""
 
85
        self.function = None
 
86
        self.args = None
 
87
        self.kwargs = None
 
88
        self.called = []
 
89
 
 
90
    def __call__(self, f, *args, **kwargs):
 
91
        """Call looping call."""
 
92
        self.function = f
 
93
        self.args = args
 
94
        self.kwargs = kwargs
 
95
        return self
 
96
 
 
97
    def start(self, interval, now=False):
 
98
        """Fake the start."""
 
99
        self.called.append(('start', interval, now))
 
100
 
 
101
 
38
102
class MainWindowTestCase(BaseTestCase):
39
103
    """Test the qt main window."""
40
104
 
131
195
    def test_add_to_autostart(self):
132
196
        """Test that the add_to_autostart function is called when CP opens."""
133
197
        self.assertTrue(self._called)
 
198
 
 
199
 
 
200
class StartTestCase(BaseTestCase):
 
201
    """Test the start method."""
 
202
 
 
203
    @defer.inlineCallbacks
 
204
    def setUp(self):
 
205
        """Set the tests."""
 
206
        yield super(StartTestCase, self).setUp()
 
207
        self.called = []
 
208
        self.auto_update_interval = 0.5
 
209
        self.looping_call = FakeLoopingCall()
 
210
 
 
211
        def stop():
 
212
            """Fake stop function."""
 
213
            self.called.append(('stop',))
 
214
 
 
215
        def auto_update(title, message, icon, logger):
 
216
            """Fake auto_update_cb."""
 
217
            self.called.append(('auto_update_cb', title, message,
 
218
                                icon, logger))
 
219
 
 
220
        self.stop_cb = stop
 
221
        self.auto_update_cb = auto_update
 
222
        self.main_window = FakeMainWindow()
 
223
        self.tray_icon = FakeTrayIcon()
 
224
 
 
225
    def test_start_not_minimized_no_icon(self):
 
226
        """Test the not minimized mode with no icon."""
 
227
        icon, window = gui.start(self.stop_cb, minimized=False,
 
228
                          with_icon=False)
 
229
        self.assertEqual(1, len(self.main_window.called))
 
230
        self.assertTrue('show' in self.main_window.called[0])
 
231
        self.assertEqual(None, self.looping_call.function)
 
232
        self.assertNotEqual(None, window)
 
233
        self.assertEqual(None, icon)
 
234
 
 
235
    def _assert_minimized_or_icon(self, icon, window):
 
236
        """Assert the start of a minimized of icon mode."""
 
237
        self.assertIsInstance(self.tray_icon.auto_update, FakeLoopingCall)
 
238
        self.assertNotEqual(None, self.looping_call.function)
 
239
        self.assertNotEqual(None, icon)
 
240
 
 
241
    def test_start_with_icon(self):
 
242
        """Test the with icon start."""
 
243
        icon, window = gui.start(self.stop_cb, minimized=False,
 
244
                          with_icon=True)
 
245
        self._assert_minimized_or_icon(icon, window)
 
246
        self.assertEqual(1, len(self.main_window.called))
 
247
        self.assertTrue('show' in self.main_window.called[0])
 
248
        self.assertNotEqual(None, window)
 
249
        self.assertNotEqual(None, self.tray_icon.window)
 
250
 
 
251
    def test_start_minimized(self):
 
252
        """Test the minimized start."""
 
253
        icon, window = gui.start(self.stop_cb, minimized=True,
 
254
                          with_icon=False)
 
255
        self._assert_minimized_or_icon(icon, window)
 
256
        self.assertEqual(0, len(self.main_window.called))
 
257
        self.assertEqual(None, window)
 
258
        self.assertEqual(None, self.tray_icon.window)