21
21
from PyQt4 import QtGui
22
22
from twisted.internet import defer, reactor
24
from ubuntuone.controlpanel.gui.qt.systray import TrayIcon
24
from ubuntuone.controlpanel.gui.qt import systray
25
25
from ubuntuone.controlpanel.tests import TestCase
26
26
import ubuntuone.controlpanel.gui.qt.gui
29
class FakeSDTool(object):
31
"""Fake SyncDaemonTool."""
29
40
class FakeMainWindow(QtGui.QWidget):
31
42
"""Fake Main Window."""
68
79
"""Test the quit option with no looping call."""
69
80
# Not done on setup, because if I patch stop
70
81
# after instantiation, it doesn't get called.
71
self.patch(TrayIcon, "stop", self._set_called)
82
self.patch(systray.TrayIcon, "stop", self._set_called)
83
tray = systray.TrayIcon()
73
84
tray.quit.trigger()
74
85
self.assertEqual(self._called, ((False,), {}))
76
87
def test_quit_lc_not_running(self):
77
88
"""Test the quit option with a stopped looping call."""
78
89
self.patch(reactor, 'stop', self._set_called)
90
tray = systray.TrayIcon()
80
91
tray.auto_update_lc = self.looping_call
81
92
tray.quit.trigger()
82
93
self.assertEqual(self._called, ((), {}))
86
97
"""Test the quit option with a running looping call."""
87
98
self.patch(reactor, 'stop', self._set_called)
88
99
self.looping_call.running = True
100
tray = systray.TrayIcon()
90
101
tray.auto_update_lc = self.looping_call
91
102
tray.quit.trigger()
92
103
self.assertEqual(self._called, ((), {}))
93
104
self.assertEqual(1, len(self.looping_call.called))
94
105
self.assertTrue('stop' in self.looping_call.called[0])
107
@defer.inlineCallbacks
108
def test_stop_sd(self):
109
"""Quit should call SyncDaemonTool.quit()."""
111
self.patch(systray, "SyncDaemonTool", lambda: st)
112
self.patch(reactor, "stop", lambda: None)
113
tray = systray.TrayIcon()
115
self.assertTrue(st.called)
96
117
def test_restore_no_window(self):
97
118
"""Test the restore window option in the menu, with no window."""
98
119
self.patch(ubuntuone.controlpanel.gui.qt.gui,
99
120
"MainWindow", FakeMainWindow)
121
tray = systray.TrayIcon()
101
122
self.assertEqual(tray.window, None)
102
123
tray.restore.trigger()
103
124
self.assertIsInstance(tray.window, FakeMainWindow)
118
139
def test_restore_window(self):
119
140
"""Test the restore window option in the menu, with a window."""
141
tray = systray.TrayIcon()
121
142
window = FakeMainWindow()
122
143
tray.window = window
123
144
self.assertFalse(tray.window.isVisible())
137
158
def test_delete_no_window(self):
138
159
"""Test deleting without an existing window."""
160
tray = systray.TrayIcon()
140
161
tray.delete_window()
141
162
self.assertEqual(tray.window, None)
143
164
def test_initialization(self):
144
165
"""Test that everything initializes correctly."""
166
tray = systray.TrayIcon()
146
167
self.assertTrue(tray.isVisible())
147
168
self.assertEqual(tray.window, None)
148
169
self.assertIsInstance(tray.context_menu, QtGui.QMenu)
161
182
def fake_clicked_message_cb():
162
183
"""A fake clicked cb."""
164
self.patch(TrayIcon, 'showMessage', fake_qt_show_message)
185
self.patch(systray.TrayIcon, 'showMessage', fake_qt_show_message)
186
tray = systray.TrayIcon()
166
187
tray.show_message(title, message, fake_clicked_message_cb)
167
188
self.assertTrue(tray in called)
168
189
self.assertTrue(title in called)