Completed
Push — master ( d6632a...89ac19 )
by Stephan
01:04
created

TestEnumSafeDict.setUp()   A

Complexity

Conditions 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 14
rs 9.4285
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
"""Unit Test Suite for QuickTile using Nose test discovery"""
4
5
__author__ = "Stephan Sokolow (deitarion/SSokolow)"
6
__license__ = "GNU GPL 2.0 or later"
7
8
import logging, operator, sys
9
import quicktile
10
11
import gtk.gdk, wnck  # pylint: disable=import-error
12
13
log = logging.getLogger(__name__)
14
15
if sys.version_info[0] == 2 and sys.version_info[1] < 7:  # pragma: no cover
16
    import unittest2 as unittest
17
else:                                                     # pragma: no cover
18
    import unittest
19
20
# pylint: disable=too-few-public-methods
21
22
class ComplainingEnum(object):
23
    """A parent class for classes which should raise C{TypeError} when compared
24
25
    (A stricter version of the annoyance I observed in Glib enums.)
26
    """
27
    def __init__(self, testcase):
28
        self.testcase = testcase
29
30
    def __cmp__(self, other):
31
        """Raises an exception if comparing against another type.
32
        @raises TypeError: C{type(self) != type(other)}
33
        @returns: C{id(self) == id(other)}
34
        @rtype: C{bool}
35
        """
36
        if type(self) != type(other):  # pylint: disable=unidiomatic-typecheck
37
            raise TypeError("Should not be comparing heterogeneous enums: "
38
                    "%s != %s" % (type(self), type(other)))
39
        else:
40
            return cmp(id(self), id(other))
41
42
class Thing1(ComplainingEnum):
43
    """See L{ComplainingEnum}"""
44
class Thing2(ComplainingEnum):
45
    """See L{ComplainingEnum}"""
46
47
class TestCommandRegistry(unittest.TestCase):
48
    """Tests for the CommandRegistry class"""
49
    def setUp(self):
50
        self.registry = quicktile.CommandRegistry()
51
52
    # TODO: Implement tests for CommandRegistry
53
54
# TODO: Implement tests for cycle_dimensions
55
# TODO: Implement tests for cycle_monitors
56
# TODO: Implement tests for move_to_position
57
# TODO: Implement tests for toggle_decorated
58
# TODO: Implement tests for toggle_desktop
59
# TODO: Implement tests for toggle_state
60
# TODO: Implement tests for trigger_keyboard_action
61
# TODO: Implement tests for workspace_go
62
# TODO: Implement tests for workspace_send_window
63
64
class TestEnumSafeDict(unittest.TestCase):
65
    """Tests to ensure EnumSafeDict never compares enums of different types"""
66
    def setUp(self):
67
        self.thing1 = Thing1(self)
68
        self.thing2 = Thing2(self)
69
70
        self.test_mappings = [
71
            (self.thing1, 'a'),
72
            (self.thing2, 'b'),
73
            (1, self.thing1),
74
            (2, self.thing2)
75
        ]
76
77
        self.empty = quicktile.EnumSafeDict()
78
        self.full = quicktile.EnumSafeDict(
79
                *[dict([x]) for x in self.test_mappings])
80
81
    def test_testing_shims(self):
82
        """EnumSafeDict: Testing shims function correctly"""
83
        for oper in ('lt', 'le', 'eq', 'ne', 'ge', 'gt'):
84
            with self.assertRaises(TypeError):
85
                print "Testing %s..." % oper
86
                getattr(operator, oper)(self.thing1, self.thing2)
87
88
    def test_init_with_content(self):
89
        """EnumSafeDict: Initialization with content"""
90
91
        test_map = self.test_mappings[:]
92
93
        while test_map:
94
            key, val = test_map.pop()
95
            self.assertEqual(self.full[key], val,
96
                "All things in the input must make it into EnumSafeDict: " +
97
                 str(key))
98
99
        self.assertFalse(test_map, "EnumSafeDict must contain ONLY things from"
100
                " the input.")
101
102
    def test_get_set_del(self):
103
        """EnumSafeDict: get/set/delitem"""
104
105
        # Test the "no matching key" branch of __getitem__
106
        with self.assertRaises(KeyError):
107
            self.empty['nonexist']  # pylint: disable=pointless-statement
108
109
        # Let Thing1 and Thing2 error out if they're compared in __setitem__
110
        for key, val in self.test_mappings:
111
            self.empty[key] = val
112
113
        # Test the "matching key" branch of __getitem__ and __delitem__
114
        for key, val in self.test_mappings:
115
            assert self.empty[key] == val
116
            del self.empty[key]
117
            with self.assertRaises(KeyError):
118
                self.empty[key]  # pylint: disable=pointless-statement
119
120
    # TODO: Complete set of tests which try to trick EnumSafeDict into
121
    #       comparing thing1 and thing2.
122
123
124
# TODO: Implement tests for GravityLayout
125
126
# TODO: Implement tests for KeyBinder
127
128
# TODO: Implement tests for QuickTileApp
129
130
class TestHelpers(unittest.TestCase):
131
    """
132
    @todo: Switch to pytest to get the assertEqual readout from assert in
133
           bare functions.
134
    """
135
    def test_powerset(self):
136
        """Test that powerset() behaves as expected"""
137
        src_set = (1, 2, 3)
138
        expected = [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
139
140
        for test_set in (tuple(src_set), list(src_set), set(src_set)):
141
            result = list(quicktile.powerset(test_set))
142
143
            # Obvious requirements
144
            self.assertIn(tuple(), result)
145
            self.assertIn(tuple(test_set), result)
146
147
            # Check that only subsets are returned
148
            for subset in expected:
149
                for item in subset:
150
                    self.assertIn(item, test_set)
151
152
            # Check that ALL subsets are returned
153
            # FIXME: This shouldn't enforce an ordering constraint.
154
            self.assertEqual(list(quicktile.powerset([1, 2, 3])), expected)
155
156
    # TODO: Test fmt_table
157
158
    # TODO: Test _make_positions
159
160
    def test_xiniterror_str(self):
161
        """XInitError.__str__ output contains provided text"""
162
        self.assertIn("Testing 123", quicktile.XInitError("Testing 123"))
163
164
class TestWindowManagerDetached(unittest.TestCase):
165
    """Tests which exercise L{quicktile.WindowManager} without needing X11."""
166
167
    def setUp(self):
168
        # Shorthand
169
        self.WM = quicktile.WindowManager  # pylint: disable=invalid-name
170
171
        # Set up a nice, oddly-shaped fake desktop made from screens
172
        # I actually have access to (though not all on the same PC)
173
        self.screens = [
174
                gtk.gdk.Rectangle(0, 0, 1280, 1024),
175
                gtk.gdk.Rectangle(1280, 0, 1280, 1024),
176
                gtk.gdk.Rectangle(0, 1024, 1680, 1050),
177
                gtk.gdk.Rectangle(1680, 1024, 1440, 900)
178
        ]
179
180
        # TODO: Also work in some fake panel struts
181
        self.desktop = gtk.gdk.Region()
182
        for rect in self.screens:
183
            self.desktop.union_with_rect(rect)
184
185
    def test_gravity_equivalence(self):
186
        """Gravity Lookup Table: GDK and WNCK constants are equivalent"""
187
        for alignment in ('CENTER', 'NORTH', 'NORTH_WEST', 'SOUTH_EAST',
188
                          'EAST', 'NORTH_EAST', 'SOUTH', 'SOUTH_WEST', 'WEST'):
189
            self.assertEqual(
190
                self.WM.gravities[getattr(wnck, 'WINDOW_GRAVITY_{}'.format(
191
                    alignment.replace('_', '')))],
192
                self.WM.gravities[getattr(gtk.gdk, 'GRAVITY_{}'.format(
193
                    alignment))])
194
195
    def test_gravity_correctness(self):
196
        """Gravity Lookup Table: Constants have correct percentage values"""
197
        for alignment, coords in (
198
                ('NORTH_WEST', (0, 0)), ('NORTH', (0.5, 0)),
199
                ('NORTH_EAST', (1.0, 0.0)), ('WEST', (0.0, 0.5)),
200
                ('CENTER', (0.5, 0.5)), ('EAST', (1, 0.5)),
201
                ('SOUTH_WEST', (0.0, 1.0)), ('SOUTH', (0.5, 1.0)),
202
                ('SOUTH_EAST', (1.0, 1.0))):
203
            self.assertEqual(self.WM.gravities[
204
                getattr(gtk.gdk, 'GRAVITY_%s' % alignment)], coords)
205
206
    def test_win_gravity_noop(self):
207
        """WindowManager.calc_win_gravity: north-west should be a no-op
208
209
        (Might as well use the screen shapes to test this. It saves effort.)
210
        """
211
        for rect in [self.desktop.get_clipbox()] + self.screens:
212
            self.assertEqual((rect.x, rect.y),
213
                self.WM.calc_win_gravity(rect, gtk.gdk.GRAVITY_NORTH_WEST),
214
                "NORTHWEST gravity should be a no-op.")
215
216
    def test_win_gravity_results(self):
217
        """WindowManager.calc_win_gravity: proper results"""
218
        for edge in (100, 200):
219
            ehalf = edge / 2
220
            for gravity, expect in (
221
                    ('NORTH_WEST', (0, 0)), ('NORTH', (-ehalf, 0)),
222
                    ('NORTH_EAST', (-edge, 0)), ('WEST', (0, -ehalf)),
223
                    ('CENTER', (-ehalf, -ehalf)), ('EAST', (-edge, -ehalf)),
224
                    ('SOUTH_WEST', (0, -edge)), ('SOUTH', (-ehalf, -edge)),
225
                    ('SOUTH_EAST', (-edge, -edge))):
226
                rect = gtk.gdk.Rectangle(0, 0, edge, edge)
227
                grav = getattr(gtk.gdk, 'GRAVITY_%s' % gravity)
228
229
                self.assertEqual(self.WM.calc_win_gravity(rect, grav), expect)
230
231
    # TODO: Test the rest of the functionality
232
233
# vim: set sw=4 sts=4 expandtab :
234