~ubuntu-branches/ubuntu/trusty/ubuntuone-control-panel/trusty-proposed

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/gui/qt/uniqueapp/tests/test_unique_app.py

  • Committer: Package Import Robot
  • Author(s): Rodney Dawes
  • Date: 2012-12-10 09:56:24 UTC
  • mfrom: (1.1.41)
  • Revision ID: package-import@ubuntu.com-20121210095624-uqwqrftml2gg2xiq
Tags: 4.1.0-0ubuntu1
* New upstream release.
  - Replace simplejson usage with json. (LP: #1029094)
  - Clear search when clicking (X) icon in search entry. (LP: #1070917)
  - Raise existing window if process already running. (LP: #1063927)
  - Don't auto-publish when selecting a search result. (LP: #1065194)
  - Add a new .desktop file for the GNOME-only remix of Ubuntu.
* debian/control:
  - Switch pylint build dependency to pyflakes.
  - Remove python-simplejson from dependencies.
* debian/ubuntuone-control-panel-qt.install:
  - Add new .desktop file for GNOME remix.
  - Remove the messaging menu integration file.
* debian/watch:
  - Update to use stable-4-2 series for Ubuntu 13.04 releases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from PyQt4 import QtCore
20
20
from twisted.internet.defer import inlineCallbacks
21
21
 
 
22
from ubuntuone.controlpanel.gui.tests import FakeSignal
22
23
from ubuntuone.controlpanel.gui.qt import uniqueapp
23
24
from ubuntuone.controlpanel.tests import TestCase
24
 
from ubuntuone.controlpanel.gui.tests import FakeSignal
25
25
 
26
26
 
27
27
#pylint: disable=C0103
32
32
        self.connect_calls = []
33
33
        self.connect_timeouts = []
34
34
        self.connect_succeeds = True
 
35
        self.message = None
35
36
 
36
37
    def connectToServer(self, *args, **kwargs):
37
38
        """Fake connectToServer."""
42
43
        self.connect_timeouts.append(timeout)
43
44
        return self.connect_succeeds
44
45
 
 
46
    def write(self, message):
 
47
        """Fake write."""
 
48
        self.message = message
 
49
 
 
50
    def flush(self):
 
51
        """Fake flush."""
 
52
 
 
53
    def close(self):
 
54
        """Fake close."""
 
55
 
 
56
    def waitForReadyRead(self):
 
57
        """Fake waitForReadyRead."""
 
58
 
 
59
    def readAll(self):
 
60
        """Fake readAll: return the message."""
 
61
        return self.message
 
62
 
45
63
 
46
64
class FakeLocalServer(object):
47
65
 
48
66
    """A fake QLocalServer."""
49
67
 
50
 
    def __init__(self):
 
68
    def __init__(self, connected=True):
51
69
        self.newConnection = FakeSignal()
52
70
        self.listen_args = []
 
71
        self.socket = None
 
72
        self._removed_key = None
 
73
        self._is_connected = connected
53
74
 
54
75
    def listen(self, *args, **kwargs):
55
76
        """Fake listen."""
56
77
        self.listen_args.append((args, kwargs))
 
78
        return self._is_connected
 
79
 
 
80
    def nextPendingConnection(self):
 
81
        """Fake nextPendingConnection."""
 
82
        return self.socket
 
83
 
 
84
    def removeServer(self, key):
 
85
        """Fake removeServer."""
 
86
        self._removed_key = key
 
87
 
 
88
    def errorString(self):
 
89
        """Fake errorString."""
 
90
        return 'error'
57
91
 
58
92
 
59
93
class FakeApplication(object):
77
111
        self.patch(uniqueapp.UniqueApplication, "aboutToQuit", self.fake_quit)
78
112
        self.patch(uniqueapp.QtGui, "QApplication", FakeApplication)
79
113
 
 
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)
 
118
 
 
119
    def test_on_failed_connection(self):
 
120
        """Check the flow of the program on connection fail."""
 
121
        data = []
 
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'])
 
128
 
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)
110
161
 
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])
 
166
 
 
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)
 
175
 
 
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")
 
181
        expected = ""
 
182
        self.assertEqual(self.local_socket.message, expected)
 
183
 
 
184
    def test_process_message_with_message(self):
 
185
        """Check that we are able to parse the message received."""
 
186
        data = []
 
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))
 
194
 
 
195
        app.server.newConnection.emit()
 
196
        self.assertEqual(data, ["share_links", True])
 
197
 
 
198
    def test_process_message_no_message(self):
 
199
        """Check that we are able to parse the message received."""
 
200
        data = []
 
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))
 
207
 
 
208
        app.server.newConnection.emit()
 
209
        self.assertEqual(data, [True])