root/projects/WinDictator/trunk/test/typist.py

Revision 19, 4.2 kB (checked in by edsuom, 1 year ago)

Importing windictator into the global FOSS repo

Line 
1 # WinDictator:
2 # Dictate in Windows, have the text typed in Linux via X faked keystroke events
3 #
4 # Copyright (C) 2005-2006 by Edwin A. Suominen, http://www.eepatents.com
5 #
6 # This program is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE.  See the file COPYING for more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # this program; if not, write to the Free Software Foundation, Inc., 51
17 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18
19
20 """
21 Unit tests for windicator.linux.typist
22 """
23
24 import time
25 from twisted.trial.unittest import TestCase
26
27 import windictator.linux.typist as typist
28 import mock
29
30
31 class TestTypist(TestCase):
32     def setUp(self):
33         # Our mock stuff
34         self.observer = mock.MockObserver()
35         self.history = mock.MockHistory()
36         # The typist object we're testing
37         self.typist = typist.Typist()
38         # Put our mock stuff in the typist object
39         self.typist.observer = self.observer
40         self.typist.history = self.history
41         self.typist.keyer = mock.MockKeyer()
42
43     def typed(self, ok, keyList):
44         self.failUnless(
45             ok, "Expected 'True' confirmation, got '%s'" % ok)
46         self.failUnlessEqual(self.typist.keyer.whatTyped(), keyList)
47         tdiff = time.time() - self.t1
48         expectedDelay = len(keyList)*mock.DELAY_CONFIRMATION
49         self.failUnless(
50             tdiff >= expectedDelay,
51             "Expected delay of %f seconds, only took %f seconds" % \
52             (expectedDelay, tdiff))
53
54     def testSendOneSimple(self):
55         self.t1 = time.time()
56         text = 'a'
57        
58         d = self.typist.insert(text)
59         d.addCallback(self.typed, ['a'])
60         return d
61    
62     def testSendMultipleSimple(self):
63         self.t1 = time.time()
64         text = 'bcd123'
65
66         d = self.typist.insert(text)
67         d.addCallback(self.typed, list(text))
68         return d
69
70     def testSendMultipleMixed(self):
71         self.t1 = time.time()
72         text = 'UnIt TeStInG\nIs FuN!'
73         expectedKeys = [
74             'Shift_L', 'u',
75             'n',
76             'Shift_L', 'i',
77             't',
78
79             'space',
80
81             'Shift_L', 't',
82             'e',
83             'Shift_L', 's',
84             't',
85             'Shift_L', 'i',
86             'n',
87             'Shift_L', 'g',
88
89             'Return',
90
91             'Shift_L', 'i',
92             's',
93
94             'space',
95            
96             'Shift_L', 'f',
97             'u',
98             'Shift_L', 'n',
99             'Shift_L', '1',
100             ]
101
102         d = self.typist.insert(text)
103         d.addCallback(self.typed, expectedKeys)
104         return d
105
106     def testQuitAfterKeyerFail(self):
107         def typed(allTypedOK, keyList):
108             self.failIf(allTypedOK)
109             self.failUnlessEqual(self.typist.keyer.whatTyped(), keyList)
110            
111         self.typist.keyer.failAfter(2)
112         d = self.typist.insert('abc')
113         d.addCallback(typed, ['a', 'b'])
114         return d
115
116     def testQuitAfterObserverFail(self):
117         def typed(allTypedOK, keyList):
118             self.failIf(allTypedOK)
119             self.failUnlessEqual(self.typist.keyer.whatTyped(), keyList)
120            
121         self.observer.failAfter(2)
122         d = self.typist.insert('abc')
123         d.addCallback(typed, ['a', 'b'])
124         return d
125
126     def testBackspace(self):
127         def nowBackspace(status):
128             self.failUnless(status)
129             d = self.typist.backspace(2)
130             d.addCallback(check)
131             return d
132
133         def check(status):
134             self.failUnless(status)
135             self.failUnlessEqual(
136                 self.typist.keyer.whatTyped(),
137                 ['a', 'b', 'c', 'space', 'd', 'BackSpace', 'BackSpace'])
138
139         self.t1 = time.time()
140         text = 'abc d'
141         d = self.typist.insert(text)
142         d.addCallback(nowBackspace)
143         return d
144        
Note: See TracBrowser for help on using the browser.