~ubuntu-branches/ubuntu/precise/ubuntuone-control-panel/precise-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
#
# Copyright 2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Tests for the control panel's Qt main."""

from twisted.internet.defer import inlineCallbacks

from ubuntuone.controlpanel.gui.qt import main
from ubuntuone.controlpanel.tests import TestCase


class FakeApplication(object):

    """A fake application."""

    def __init__(self):
        self.args = None
        self.style = None

    def __call__(self, argv, *args, **kwargs):
        """Fake arg filtering function."""
        if '-title' in argv:
            argv.remove('-title')
        self.args = (argv, args, kwargs)
        return self

    # pylint: disable=C0103
    def setStyleSheet(self, *args, **kwargs):
        """Fake setStyleSheet."""
        self.style = (args, kwargs)
    # pylint: enable=C0103

    def arguments(self):
        """Fake arguments function."""
        return self.args[0]

    def exec_(self):
        """Fake event loop."""
        pass


class FakeStart(object):

    """Fake start function."""

    def __init__(self):
        self.args = None

    def __call__(self, *args, **kwargs):
        self.args = (args, kwargs)
        return None, None


class MainTestCase(TestCase):

    """Test the argument parsing and passing in main."""

    @inlineCallbacks
    def setUp(self):
        yield super(MainTestCase, self).setUp()
        self.app = FakeApplication()
        self.start = FakeStart()
        self.patch(main, "UniqueApplication", self.app)
        self.patch(main, "start", self.start)

    def test_wm_class(self):
        """Test that we set the 1st argument, used for WM_CLASS, correctly."""
        main.main([])
        self.assertEqual(self.app.args,
            (['ubuntuone-installer'], ('ubuntuone-control-panel',), {}))

    def test_title_not_fail(self):
        """Ensure -title is removed before it gets to OptParser."""
        main.main(["blah", "-title"])
        # Did not crash!

    def test_minimized_option(self):
        """Ensure the --minimized option is parsed and passed correctly."""
        main.main(["blah", "--minimized"])
        self.assertEqual(self.start.args[1],
            {'minimized': True, 'with_icon': False})

    def test_with_icon_option(self):
        """Ensure the --minimized option is parsed and passed correctly."""
        main.main(["blah", "--with-icon"])
        self.assertEqual(self.start.args[1],
            {'minimized': False, 'with_icon': True})

    def test_style_loads(self):
        """Ensure the stylesheet is loaded."""
        main.main([])
        self.assertTrue(self.app.style)