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

Revision 19, 4.1 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 Mock objects for unit tests
22 """
23
24 import os, time
25 from twisted.internet import defer, reactor, utils
26
27 DELAY_CONFIRMATION = 0.01
28 DELAY_TYPED = 0.005
29 VERBOSE = False
30
31
32 class Mock(object):
33     def __init__(self):
34         self.N = 10000
35         self.checks = 0
36
37     def ok_get(self):
38         self.checks += 1
39         if self.checks <= self.N:
40             return True
41         else:
42             return False
43     ok = property(ok_get)
44
45     def failAfter(self, N):
46         self.N = N
47
48
49 class MockObserver(Mock):
50     def confirm(self, keycode):
51         def confirmNow(d):
52             d.callback(self.ok)
53
54         if isinstance(keycode, int):
55             d = defer.Deferred()
56             reactor.callLater(DELAY_CONFIRMATION, confirmNow, d)
57         else:
58             d = defer.succeed(False)
59         return d
60    
61
62 class MockTransport(object):
63     def close(self):
64         pass
65
66
67 class MockKeyer(Mock):
68     def __init__(self):
69         Mock.__init__(self)
70         self.typed = []
71         self.stdin = MockTransport()
72
73     def whatTyped(self):
74         return self.typed
75    
76     def pressKey(self, keySym):
77         self.typed.append(keySym)
78         return self.key('d', keySym)
79
80     def releaseKey(self, keySym):
81         return self.key('u', keySym)
82
83     def runTypistForOneKeySym(self, keySym):
84         t1 = time.time()
85        
86         def gotResult(result):
87             try:
88                 keycode = int(result[0])
89             except:
90                 keycode = None
91             if VERBOSE:
92                 print "\nKEYED:", time.time() - t1, keycode
93             return keycode
94
95         d = utils.getProcessOutputAndValue(
96             '/bin/bash',
97             args=('-c', "echo 'c %s' |../bin/typist/typist" % keySym),
98             env = os.environ)
99         d.addCallback(gotResult)
100         return d
101
102     def key(self, du, keySym):
103         def gotKeyCode(keycode):
104             self.keycode = keycode
105             d2 = defer.Deferred()
106             d2.addCallback(returnKeyCode)
107             reactor.callLater(DELAY_TYPED, d2.callback, None)
108             return d2
109            
110         def returnKeyCode(null):
111             return self.keycode
112        
113         # Deferred d1 for pressing
114         if self.ok:
115             d1 = self.runTypistForOneKeySym(keySym)
116         else:
117             d1 = defer.succeed(None)
118         d1.addCallback(gotKeyCode)
119         return d1
120
121
122 class MockHistory(object):
123     def __init__(self, N=None):
124         self.events = []
125
126     def backspace(self, N):
127         self.events = self.events[:-N]
128        
129     def reportKeyEvent(self, *args):
130         self.events.append(args)
131
132
133 class MockUpdater(object):
134     def __init__(self, clientRoot):
135         self.clientRoot = clientRoot
136
137     def update(self, currentText):
138         return defer.succeed(True)
139
140
141 class MockClientRoot(object):
142     def __init__(self):
143         self.operations = []
144    
145     def callRemote(self, funcName, *args, **kw):
146         self.verbose = kw.pop('verbose', False)
147         return getattr(self, funcName)(*args, **kw)
148    
149     def backspace(self, N):
150         if self.verbose:
151             print "\nBACKSPACE", N
152         self.operations.append(('backspace', N))
153         return defer.succeed(True)
154
155     def insert(self, text):
156         if self.verbose:
157             print "\nINSERT", text
158         self.operations.append(('insert', text))
159         return defer.succeed(True)
160        
Note: See TracBrowser for help on using the browser.