19
19
"""The test suite for the Qt UI for the control panel for Ubuntu One."""
23
from PyQt4 import QtCore
25
from ubuntuone.controlpanel import backend
26
from ubuntuone.controlpanel.tests import TestCase, EXPECTED_ACCOUNT_INFO
25
from PyQt4 import QtGui, QtCore
26
from ubuntuone.devtools.handlers import MementoHandler
28
from ubuntuone.controlpanel import backend, cache
29
from ubuntuone.controlpanel.tests import TestCase, EXPECTED_ACCOUNT_INFO, TOKEN
30
from ubuntuone.controlpanel.gui import qt, UBUNTUONE_FROM_OAUTH
27
31
from ubuntuone.controlpanel.gui.tests import FakedObject, USER_HOME
29
33
# Attribute 'yyy' defined outside __init__, access to a protected member
79
def skip_if_abstract_class(test):
80
"""Decorator to skip a test if is an abstract class."""
83
"""Skip a test if is an abstract class."""
84
abstract = instance.class_ui is None
87
result = test(instance)
94
83
class FakeUi(FakedObject):
95
84
"""A fake Ui object."""
114
103
backend.UPLOAD_KEY: -1, # no limit
117
next_result = object()
118
107
exposed_methods = [
119
108
'account_info', # account
120
109
'devices_info', 'device_names_info', # devices
128
117
'file_sync_settings_info',
129
118
'change_file_sync_settings',
130
119
'restore_file_sync_settings',
135
class FakedConfirmDialog(object):
123
def get_credentials(self):
124
"""Fake credentials retrieval."""
125
self._called['get_credentials'] = ((), {})
129
class CrashyBackendException(Exception):
130
"""A faked backend crash."""
133
class CrashyBackend(FakedControlPanelBackend):
134
"""A faked backend that crashes."""
136
def __init__(self, *args, **kwargs):
137
super(CrashyBackend, self).__init__(*args, **kwargs)
138
for i in self.exposed_methods + ['get_credentials']:
139
setattr(self, i, self._fail(i))
142
"""Crash boom bang."""
144
def inner(*args, **kwargs):
145
"""Raise a custom exception."""
146
raise CrashyBackendException(f)
151
class FakedDialog(object):
136
152
"""Fake a confirmation dialog."""
138
155
response = args = kwargs = None
168
186
innerclass_name = None
172
@skip_if_abstract_class
192
cache.Cache._shared_objects = {}
174
193
super(BaseTestCase, self).setUp()
175
194
self.patch(backend, 'ControlBackend', FakedControlPanelBackend)
176
# self.class_ui is not callable
177
# pylint: disable=E1102
178
self.ui = self.class_ui(**self.kwargs)
179
self.addCleanup(self.ui.destroy)
181
if hasattr(self.ui, 'backend'):
182
# clean backend calls
183
self.ui.backend._called.clear()
197
if self.class_ui is not None:
198
# self.class_ui is not callable
199
# pylint: disable=E1102
200
self.ui = self.class_ui(**self.kwargs)
201
# pylint: enable=E1102
202
self.addCleanup(self.ui.destroy)
204
if getattr(self.ui, 'backend', None) is not None:
205
self.addCleanup(self.ui.backend._called.clear)
207
logger = self.logger if self.logger is not None else \
208
getattr(self.ui, 'logger', None)
210
if logger is not None:
211
self.memento = MementoHandler()
212
self.memento.setLevel(logging.DEBUG)
213
logger.addHandler(self.memento)
214
self.addCleanup(logger.removeHandler, self.memento)
216
# default response if user does nothing
217
FakedFileDialog.response = QtCore.QString('')
218
FakedFileDialog.args = None
219
FakedFileDialog.kwargs = None
220
self.patch(QtGui, 'QFileDialog', FakedFileDialog)
222
FakedDialog.response = None
223
FakedDialog.args = None
224
FakedDialog.kwargs = None
225
self.patch(QtGui, 'QMessageBox', FakedDialog)
185
227
def get_pixmap_data(self, pixmap):
186
228
"""Get the raw data of a QPixmap."""
202
244
self.assertIn(method_name, self.ui.backend._called)
203
245
self.assertEqual(self.ui.backend._called[method_name], (args, kwargs))
247
def assert_uri_hook_called(self, button, url):
248
"""Check that uri_hook was called with 'url' when clicking 'button'."""
249
self.patch(qt, 'uri_hook', self._set_called)
252
self.assertEqual(len(self._called), 2, 'uri_hook must be called.')
253
self.assertEqual(len(self._called[0]), 1, 'uri_hook must be called.')
254
actual_url = self._called[0][0]
255
if actual_url.startswith(UBUNTUONE_FROM_OAUTH):
256
self.assertIn(urllib.urlencode({'next': url}), actual_url)
258
self.assertEqual(actual_url, url)
205
260
def test_init_loads_ui(self, expected_setup_ui=None):
206
261
"""The __init__ method loads the ui."""
207
262
if self.innerclass_ui is None:
219
274
expected_setup_ui = expected_setup_ui(self.ui)
220
275
self.assertEqual(self.ui.ui._called,
221
276
{'setupUi': ((expected_setup_ui,), {})})
278
def test_backend_is_correct(self):
279
"""The backend instance is correct."""
280
if getattr(self.ui, 'backend', None) is not None:
281
self.assertIsInstance(self.ui.backend, FakedControlPanelBackend)