summaryrefslogtreecommitdiff
path: root/CompizTests.py
blob: ed4dc1b803d361b129e14d3645d36056d7c30bff (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#
# Copyright Eyemagnet Limited
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# 
# Author: Kristian F. Erikson <kristian.erikson@eyemagnet.com>
#

import unittest
from CompizError import *
from CompizTools import *
import CompizManager
import time

def preTestSetup():
    global manager, waterWasActive, placeWasActive, rotateWasActive, blurWasActive, updateNecessary
    manager = CompizManager.CompizManager()
    waterPlugin = manager.getPlugin('water') # These plugins must be active: water, place, rotate, core is assumed
    placePlugin = manager.getPlugin('place') 
    rotatePlugin = manager.getPlugin('rotate')
    blurPlugin = manager.getPlugin('blur')   # This plugin must be inactive: blur - change as not everyone has this plugin!
    # Save states so we can set them back later
    waterWasActive = waterPlugin.active
    placeWasActive = placePlugin.active
    rotateWasActive = rotatePlugin.active
    blurWasActive = blurPlugin.active
    # Ensure states are correct for testing
    activePluginsList = manager.listActivePlugins()
    updateNecessary = False
    if not waterWasActive:
        activePluginsList.append('water')
        updateNecessary = True
    if not placeWasActive:
        activePluginsList.append('place')
        updateNecessary = True
    if not rotateWasActive:
        activePluginsList.append('rotate')
        updateNecessary = True
    if blurWasActive:
        activePluginsList.remove('blur')
        updateNecessary = True
    if updateNecessary:
        manager.setActivePluginList(activePluginsList)
        time.sleep(5)
        waterPlugin.update()
        placePlugin.update()
        rotatePlugin.update()
        blurPlugin.update()
        

def postTestCleanup():
    global updateNecessary
    # Set plugin states back to what they were
    if updateNecessary:
        activePluginsList = manager.listActivePlugins()
        if not waterWasActive: activePluginsList.remove('water')
        if not placeWasActive: activePluginsList.remove('place')
        if not rotateWasActive: activePluginsList.remove('rotate')
        if blurWasActive: activePluginsList.append('blur')            
        manager.setActivePluginList(activePluginsList)
        

class TestCompizManagerClasses(unittest.TestCase):
        
    def testBuildingPlugins(self): # Note, this implementation is not very good... Any suggestions are very welcome :)
        "Test plugins are being built correctly"
        # First check options
        placePlugin = manager.getPlugin("place")
        placeOptionsShouldBe = {'position_constrain_workarea': [type(True)], 'position_y_values': [type(1)], 'viewport_x_values': [type(1)], 'viewport_matches': [type('s')], 'force_placement_match': type('s'), 'position_x_values': [type(1)], 'mode': type(1), 'viewport_y_values': [type(1)], 'position_matches': [type('s')], 'workarounds': type(True), 'multioutput_mode': type(1)}
        placeOptions = placePlugin.listOptions()
        self.assertEquals(placeOptions, placeOptionsShouldBe)
        # Then check actions
        waterPlugin = manager.getPlugin("water")
        waterAtionsShouldBe =  {'initiate': None, 'line': {'y1': type(1), 'y0': type(1), 'x0': type(1), 'x1': type(1), 'amplitude': type(1.00)}, 'toggle_wiper': None, 'toggle_rain': None, 'point': {'y': type(1), 'x': type(1), 'amplitude': type(1.00)}}
        waterActions = waterPlugin.listActions()
        self.assertEquals(waterActions, waterAtionsShouldBe)
        
    def testPluginDoAction(self):
        "Test sending actions over Dbus with various param lengths and ensure they look correct"
        # Assuming water plugin is active
        waterPlugin = manager.getPlugin('water')
        waterPlugin.doAction("line", {'x0':400,'x1':900,'y0':100,'y1':400,'amplitude':10.0})
        waterPlugin.doAction("line", {'x0':400,'x1':900,'y0':300,'y1':600,'amplitude':10.0})
        waterPlugin.doAction("line", {'x0':400,'x1':900,'y0':200,'y1':500,'amplitude':10.0})
        sentString = manager.connection.getLastSentString()
        self.assert_("water" in sentString)
        self.assert_("line" in sentString)
        self.assert_("'root'" in sentString)
        self.assert_("'x0', 400" in sentString)
        self.assert_("'x1', 900" in sentString)
        self.assert_("'y0', 200" in sentString)
        self.assert_("'y1', 500" in sentString)
        self.assert_("'amplitude', 10.0" in sentString)
        waterPlugin.doAction("point", {'x':300,'y':200,'amplitude':2.0})
        sentString = manager.connection.getLastSentString()
        self.assert_("water" in sentString)
        self.assert_("point" in sentString)
        self.assert_("root" in sentString)
        self.assert_("'x', 300" in sentString)
        self.assert_("'y', 200" in sentString)
        self.assert_("'amplitude', 2.0" in sentString)
        waterPlugin.doAction("toggle_rain")
        sentString = manager.connection.getLastSentString()
        self.assert_("water" in sentString)
        self.assert_("toggle_rain_key" in sentString)
        self.assert_("root" in sentString)
        waterPlugin.doAction("toggle_rain")
        
    def testPluginWrongTypeParam(self): # a test to make sure errors are raised if given incorrect type arguments
        "Test exceptions are raised if plugins given incorrect type arguments on calling actions."
        # Assuming firepaint plugin is active
        rotatePlugin = manager.getPlugin('rotate')
        self.assertRaises(CompizPluginWindowNotFoundError, rotatePlugin.doAction, "rotate_window", {'windowtitle':'400','face':'200'})    # should be str + int
        self.assertRaises(CompizPluginTypeArgError, rotatePlugin.doAction, "rotate_window", {'windowtitle':400,'face':200})
        self.assertRaises(CompizPluginTypeArgError, rotatePlugin.doAction, "rotate_window", {'windowtitle':40.0,'face':20.0})
        
    def testPluginSetOption(self):
        "Test the plugin can have their options changed."
        # Assuming water plugin is active
        waterPlugin = manager.getPlugin('water')
        old_option = waterPlugin.getOption('rain_delay')
        new_option = old_option + 1
        waterPlugin.setOption('rain_delay', new_option)
        self.assertEquals(waterPlugin.getOption('rain_delay'), new_option)
        
    def testPluginChangeOptionWrongType(self):
        "Test exceptions are raised if supplying wrong types to change options."
        # Assuming water plugin is active
        waterPlugin = manager.getPlugin('water')
        self.assertRaises(CompizPluginTypeArgError, waterPlugin.setOption, "rain_delay", 250.0) # should be int
        self.assertRaises(CompizPluginTypeArgError, waterPlugin.setOption, "rain_delay", "hello from testing")  # should be int
        self.assertRaises(CompizPluginTypeArgError, waterPlugin.setOption, "offset_scale", 1) # should be float
        
    def testPluginNameOptionWrong(self):
        "Test exceptions are raised if supplying wrong option names to change options."
        corePlugin = manager.getPlugin('core')
        self.assertRaises(CompizPluginOptionError, corePlugin.setOption, 'texxxxture_filter', 5)
        
    def testPluginActionOnInactivePlugin(self):
        "Test plugins raise an exceptions if they are inactive and user attempts to use them"
        # Assuming benchmark plugin is inactive
        blurPlugin = manager.getPlugin('blur')
        self.assertRaises(CompizPluginNotActiveError, blurPlugin.getOption, "johndoe")
        self.assertRaises(CompizPluginNotActiveError, blurPlugin.setOption, "johndoe", 250.0)
        self.assertRaises(CompizPluginNotActiveError, blurPlugin.doAction, "dummy-action")
        
    def testPluginSetOptionWithLists(self):
        "Test changing plugin options with lists."
        # First try lists of ints
        placePlugin = manager.getPlugin('place')
        oldViewportValues = placePlugin.getOption('viewport_x_values')
        placePlugin.setOption('viewport_x_values', [1,1,1])
        newViewportValues = placePlugin.getOption('viewport_x_values')
        self.assert_(oldViewportValues != newViewportValues)    # Assert the two list are not the same
        placePlugin.setOption('viewport_x_values', oldViewportValues)
        newViewportValues = placePlugin.getOption('viewport_x_values')
        self.assertEquals(newViewportValues, oldViewportValues) # Assert they are the same now
        self.assertRaises(CompizPluginTypeArgError, placePlugin.setOption, 'viewport_x_values', True)   #incorrect arg
        self.assertRaises(CompizPluginTypeArgError, placePlugin.setOption, 'viewport_x_values', (123, 23.45))   #incorrect arg
        self.assertRaises(CompizPluginTypeArgError, placePlugin.setOption, 'viewport_x_values', 'testhello')   #incorrect arg
        self.assertRaises(CompizPluginTypeArgError, placePlugin.setOption, 'viewport_x_values', ['#fefefeff',123,'#fefefeff'])    # incorrect arg in list
        # Now try lists of strings
        oldViewportValues = placePlugin.getOption('viewport_matches')
        placePlugin.setOption('viewport_matches', ["title=Hello",'title=from',unicode('title=testing')])
        newViewportValues = placePlugin.getOption('viewport_matches')
        self.assert_(oldViewportValues != newViewportValues)    # Assert the two list are not the same
        placePlugin.setOption('viewport_matches', oldViewportValues)
        newViewportValues = placePlugin.getOption('viewport_matches')
        self.assertEquals(newViewportValues, oldViewportValues) # Assert they are the same now
        self.assertRaises(CompizPluginTypeArgError, placePlugin.setOption, 'viewport_matches', True)   #incorrect arg
        self.assertRaises(CompizPluginTypeArgError, placePlugin.setOption, 'viewport_matches', (123, 23.45))   #incorrect arg
        self.assertRaises(CompizPluginTypeArgError, placePlugin.setOption, 'viewport_matches', 'testhello')   #incorrect arg
        self.assertRaises(CompizPluginTypeArgError, placePlugin.setOption, 'viewport_matches', ['#fefefeff',123,'#fefefeff'])    # incorrect arg in list
        
    def testConversion(self):
        "Test the conversion functions"
        # Test strings and simple types
        self.assertEquals(convertFromPythonToDbus('hello'), dbus.String('hello'))
        self.assertEquals(convertFromPythonToDbus(1), dbus.Int32(1))
        self.assertEquals(convertFromPythonToDbus(2.34), dbus.Double(2.34))
        self.assertEquals(convertFromPythonToDbus(False), dbus.Boolean(False))
        self.assertEquals('hello',convertFromDbusToPython('hello'))
        self.assertEquals(1,convertFromDbusToPython(1))
        self.assertEquals(2.34,convertFromDbusToPython(2.34))
        self.assertEquals(False,convertFromDbusToPython(False))
        # Test a list
        original = ['hello','from','testing']   # Convert one way
        convertsTo = dbus.Array([dbus.String('hello'),dbus.String('from'),dbus.String('testing')], signature=dbus.Signature('s'))
        converted = convertFromPythonToDbus(original)
        self.assertEquals(original, converted) # Assert they are the same now
        converted = convertFromDbusToPython(convertsTo)   # Now convert the other way
        self.assertEquals(convertsTo, converted) # Assert they are the same now
        
    def testGetListActiveWindow(self):
        "Test listing active windows and check list looks resonable"
        windowList = manager.listWindows()
        for window in windowList:
            tempTitle = window['title']
            tempId = window['id']
        self.assertEquals(type(tempTitle), type(u'hello'))
        self.assertEquals(type(tempId), type(1))