42
43
self.connect_timeouts.append(timeout)
43
44
return self.connect_succeeds
46
def write(self, message):
48
self.message = message
56
def waitForReadyRead(self):
57
"""Fake waitForReadyRead."""
60
"""Fake readAll: return the message."""
46
64
class FakeLocalServer(object):
48
66
"""A fake QLocalServer."""
68
def __init__(self, connected=True):
51
69
self.newConnection = FakeSignal()
52
70
self.listen_args = []
72
self._removed_key = None
73
self._is_connected = connected
54
75
def listen(self, *args, **kwargs):
56
77
self.listen_args.append((args, kwargs))
78
return self._is_connected
80
def nextPendingConnection(self):
81
"""Fake nextPendingConnection."""
84
def removeServer(self, key):
85
"""Fake removeServer."""
86
self._removed_key = key
88
def errorString(self):
89
"""Fake errorString."""
59
93
class FakeApplication(object):
77
111
self.patch(uniqueapp.UniqueApplication, "aboutToQuit", self.fake_quit)
78
112
self.patch(uniqueapp.QtGui, "QApplication", FakeApplication)
114
def test_cleanup_called_on_init(self):
115
"""Check that cleanup is called on initialization."""
116
uniapp = uniqueapp.UniqueApplication([], "key")
117
self.assertEqual("key", uniapp.server._removed_key)
119
def test_on_failed_connection(self):
120
"""Check the flow of the program on connection fail."""
122
local_server = FakeLocalServer(False)
123
self.patch(uniqueapp.QtNetwork, "QLocalServer",
124
lambda parent: local_server)
125
self.patch(uniqueapp.logger, "debug", data.append)
126
uniqueapp.UniqueApplication([], "key")
127
self.assertEqual(data, ['error'])
80
129
def test_client_socket(self):
81
130
"""Check that the client socket is used correctly."""
82
131
self.local_socket.connect_succeeds = True
105
154
app = uniqueapp.UniqueApplication([], "key")
106
155
# Yes, this is ugly. I can't find any other meaningful
107
156
# way to compare them though.
108
self.assertEqual(str(app.server.newConnection.target.__self__),
157
self.assertEqual(str(app.server.newConnection.target[0].__self__),
109
158
str(app.new_instance))
159
self.assertEqual(app.server.newConnection.target[1],
160
app._process_messages)
111
162
def test_cleanup(self):
112
163
"""Check that cleanup is called with the right key."""
113
164
app = uniqueapp.UniqueApplication([], "key")
114
self.assertEqual(self.fake_quit.target, app.cleanup)
165
self.assertEqual(self.fake_quit.target, [app.cleanup])
167
def test_send_messages_valid(self):
168
"""Check the message is created correctly."""
169
self.local_socket.connect_succeeds = True
170
argv = ['python', 'ubuntuone-control-panel-qt',
171
'--switch-to', 'share_links']
172
uniqueapp.UniqueApplication(argv, "key")
173
expected = "--switch-to=share_links"
174
self.assertEqual(self.local_socket.message, expected)
176
def test_send_messages_invalid(self):
177
"""Check the message is created correctly."""
178
self.local_socket.connect_succeeds = True
179
argv = ['python', 'ubuntuone-control-panel-qt']
180
uniqueapp.UniqueApplication(argv, "key")
182
self.assertEqual(self.local_socket.message, expected)
184
def test_process_message_with_message(self):
185
"""Check that we are able to parse the message received."""
187
self.local_socket.connect_succeeds = True
188
argv = ['python', 'ubuntuone-control-panel-qt',
189
'--switch-to', 'share_links']
190
app = uniqueapp.UniqueApplication(argv, "key")
191
self.local_server.socket = self.local_socket
192
app.switch_to.connect(data.append)
193
app.activate_window.connect(lambda: data.append(True))
195
app.server.newConnection.emit()
196
self.assertEqual(data, ["share_links", True])
198
def test_process_message_no_message(self):
199
"""Check that we are able to parse the message received."""
201
self.local_socket.connect_succeeds = True
202
argv = ['python', 'ubuntuone-control-panel-qt']
203
app = uniqueapp.UniqueApplication(argv, "key")
204
self.local_server.socket = self.local_socket
205
app.switch_to.connect(data.append)
206
app.activate_window.connect(lambda: data.append(True))
208
app.server.newConnection.emit()
209
self.assertEqual(data, [True])