~ubuntu-branches/ubuntu/precise/ubuntu-sso-client/precise-proposed

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/main/tests/test_main.py

  • Committer: Package Import Robot
  • Author(s): Natalia Bidart (nessita)
  • Date: 2012-04-03 12:01:29 UTC
  • mfrom: (1.1.35)
  • Revision ID: package-import@ubuntu.com-20120403120129-fg0gw1nnhgexq7gm
Tags: 2.99.92-0ubuntu1
* New upstream release:
  - Restoring transparency for the overlay widget (LP: #969455).
  - Converting to unicode some data returned by webclient in
    QByteArray format (LP: #961315).
  - Fixed the issue where we use an empty proxy url when the proxy
    was not set in gsettings (LP: #969280).
  - Forced white background (LP: #961346).
* debian/watch:
  - Updated url to fetch tarball from latest milestone.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2012 Canonical Ltd.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
 
17
"""Tests for the main module."""
 
18
 
 
19
from PyQt4 import QtCore
 
20
from twisted.internet import defer
 
21
from twisted.trial.unittest import TestCase
 
22
 
 
23
from ubuntu_sso.qt import main
 
24
from ubuntu_sso import tests
 
25
 
 
26
 
 
27
# pylint: disable=C0103
 
28
class FakeUi(object):
 
29
 
 
30
    """A fake UI."""
 
31
 
 
32
    def size(self):
 
33
        """Fake size."""
 
34
        return QtCore.QSize(100, 100)
 
35
 
 
36
    def setGeometry(self, *args):
 
37
        """Fake setGeometry."""
 
38
 
 
39
    show = setGeometry
 
40
 
 
41
 
 
42
class FakeDesktop(object):
 
43
 
 
44
    """Fake Desktop Widget."""
 
45
 
 
46
    def availableGeometry(self):
 
47
        """Fake availableGeometry for desktop.-"""
 
48
        return QtCore.QRect(100, 100, 100, 100)
 
49
 
 
50
 
 
51
class FakeApplication(object):
 
52
 
 
53
    """Fake QApplication."""
 
54
 
 
55
    called = {}
 
56
    __instance = None
 
57
 
 
58
    def __init__(self, args):
 
59
        self.called['args'] = args
 
60
        FakeApplication.__instance = self
 
61
 
 
62
    def setStyleSheet(self, style):
 
63
        """Fake setStyleSheet."""
 
64
        self.called["setStyleSheet"] = style
 
65
 
 
66
    def styleSheet(self):
 
67
        """Fake get style sheet."""
 
68
        return self.called.get("setStyleSheet", '')
 
69
 
 
70
    def desktop(self):
 
71
        """Fake Desktop."""
 
72
        return FakeDesktop()
 
73
 
 
74
    def exec_(self):
 
75
        """Fake exec_."""
 
76
 
 
77
    def exit(self):
 
78
        """Fake exit."""
 
79
 
 
80
    @classmethod
 
81
    def instance(cls):
 
82
        """Fake instance."""
 
83
        return FakeApplication.__instance
 
84
# pylint: enable=C0103
 
85
 
 
86
 
 
87
class BasicTestCase(TestCase):
 
88
    """Test case with a helper tracker."""
 
89
 
 
90
    @defer.inlineCallbacks
 
91
    def setUp(self):
 
92
        yield super(BasicTestCase, self).setUp()
 
93
        self.called = []
 
94
        self._close_callback = None
 
95
        self._app = None
 
96
 
 
97
        def called_ui(method, *args, **kw):
 
98
            """record ui call."""
 
99
            if 'close_callback' in kw:
 
100
                self._close_callback = kw.pop('close_callback')
 
101
            self.called.append((method, args, kw))
 
102
            return FakeUi()
 
103
 
 
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)
 
111
 
 
112
    def test_main(self):
 
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)
 
116
        main.main(**kwargs)
 
117
 
 
118
        expected = [('main', (FakeApplication.instance(),), {}),
 
119
            ('GUI', (), kwargs),
 
120
            ('main_start', (FakeApplication.instance(),), {})]
 
121
        self.assertEqual(self.called, expected)
 
122
 
 
123
    def test_check_close_callback(self):
 
124
        """Check that the close callback is main_quit."""
 
125
 
 
126
        def called_quit(app):
 
127
            """Record the call to quit."""
 
128
            self._app = app
 
129
 
 
130
        self.patch(main.source, 'main_quit', called_quit)
 
131
 
 
132
        kwargs = dict(app_name='APP_NAME', foo='foo', bar='bar',
 
133
            baz='yadda', yadda=0)
 
134
        main.main(**kwargs)
 
135
 
 
136
        expected = [('main', (FakeApplication.instance(),), {}),
 
137
            ('GUI', (), kwargs),
 
138
            ('main_start', (FakeApplication.instance(),), {})]
 
139
        self._close_callback()
 
140
        self.assertEqual(self.called, expected)
 
141
        self.assertEqual(self._app, FakeApplication.instance())
 
142
 
 
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,
 
146
            tests.EMAIL_TOKEN)
 
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'))
 
151
        main.main(**kwargs)
 
152
 
 
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)
 
159
 
 
160
    def test_styles_load(self):
 
161
        """Test that all stylesheets load."""
 
162
        kwargs = dict(foo='foo', bar='bar', baz='yadda', yadda=0)
 
163
        main.main(**kwargs)
 
164
        data = []
 
165
        for qss_name in (main.PLATFORM_QSS, ":/stylesheet.qss"):
 
166
            qss = QtCore.QResource(qss_name)
 
167
            data.append(unicode(qss.data()))
 
168
        self.assertEqual(
 
169
        unicode(main.QtGui.QApplication.instance().styleSheet()),
 
170
            '\n'.join(data))