1
# -*- coding: utf-8 -*-
3
# Copyright 2012 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
"""Tests for the main module."""
19
from PyQt4 import QtCore
20
from twisted.internet import defer
21
from twisted.trial.unittest import TestCase
23
from ubuntu_sso.qt import main
24
from ubuntu_sso import tests
27
# pylint: disable=C0103
34
return QtCore.QSize(100, 100)
36
def setGeometry(self, *args):
37
"""Fake setGeometry."""
42
class FakeDesktop(object):
44
"""Fake Desktop Widget."""
46
def availableGeometry(self):
47
"""Fake availableGeometry for desktop.-"""
48
return QtCore.QRect(100, 100, 100, 100)
51
class FakeApplication(object):
53
"""Fake QApplication."""
58
def __init__(self, args):
59
self.called['args'] = args
60
FakeApplication.__instance = self
62
def setStyleSheet(self, style):
63
"""Fake setStyleSheet."""
64
self.called["setStyleSheet"] = style
67
"""Fake get style sheet."""
68
return self.called.get("setStyleSheet", '')
83
return FakeApplication.__instance
84
# pylint: enable=C0103
87
class BasicTestCase(TestCase):
88
"""Test case with a helper tracker."""
90
@defer.inlineCallbacks
92
yield super(BasicTestCase, self).setUp()
94
self._close_callback = None
97
def called_ui(method, *args, **kw):
99
if 'close_callback' in kw:
100
self._close_callback = kw.pop('close_callback')
101
self.called.append((method, args, kw))
104
self.patch(main, 'UbuntuSSOClientGUI',
105
lambda *arg, **kw: called_ui('GUI', *arg, **kw))
106
self.patch(main.source, 'main',
107
lambda *arg, **kw: called_ui('main', *arg, **kw))
108
self.patch(main.source, 'main_start',
109
lambda *arg, **kw: called_ui('main_start', *arg, **kw))
110
self.patch(main.QtGui, 'QApplication', FakeApplication)
113
"""Calling main.main() a UI instance is created."""
114
kwargs = dict(app_name='APP_NAME', foo='foo', bar='bar',
115
baz='yadda', yadda=0)
118
expected = [('main', (FakeApplication.instance(),), {}),
120
('main_start', (FakeApplication.instance(),), {})]
121
self.assertEqual(self.called, expected)
123
def test_check_close_callback(self):
124
"""Check that the close callback is main_quit."""
126
def called_quit(app):
127
"""Record the call to quit."""
130
self.patch(main.source, 'main_quit', called_quit)
132
kwargs = dict(app_name='APP_NAME', foo='foo', bar='bar',
133
baz='yadda', yadda=0)
136
expected = [('main', (FakeApplication.instance(),), {}),
138
('main_start', (FakeApplication.instance(),), {})]
139
self._close_callback()
140
self.assertEqual(self.called, expected)
141
self.assertEqual(self._app, FakeApplication.instance())
143
def test_main_args(self):
144
"""Calling main.main() a UI instance is created."""
145
arg_list = (tests.APP_NAME, tests.NAME, tests.PASSWORD,
147
kwargs = dict(app_name=arg_list[0].encode('utf-8'),
148
foo=arg_list[1].encode('utf-8'),
149
bar=arg_list[2].encode('utf-8'),
150
baz=arg_list[3].encode('utf-8'))
153
args_unicode = dict(app_name=arg_list[0], foo=arg_list[1],
154
bar=arg_list[2], baz=arg_list[3])
155
expected = [('main', (FakeApplication.instance(),), {}),
156
('GUI', (), args_unicode),
157
('main_start', (FakeApplication.instance(),), {})]
158
self.assertEqual(self.called, expected)
160
def test_styles_load(self):
161
"""Test that all stylesheets load."""
162
kwargs = dict(foo='foo', bar='bar', baz='yadda', yadda=0)
165
for qss_name in (main.PLATFORM_QSS, ":/stylesheet.qss"):
166
qss = QtCore.QResource(qss_name)
167
data.append(unicode(qss.data()))
169
unicode(main.QtGui.QApplication.instance().styleSheet()),