| 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.keyer |
|---|
| 22 |
""" |
|---|
| 23 |
|
|---|
| 24 |
import os, time |
|---|
| 25 |
from twisted.internet import defer |
|---|
| 26 |
from twisted.trial.unittest import TestCase |
|---|
| 27 |
|
|---|
| 28 |
import windictator.linux.keyer as keyer |
|---|
| 29 |
import mock |
|---|
| 30 |
|
|---|
| 31 |
REAL_BINARY = keyer.BINARY |
|---|
| 32 |
DELAY = 0.1 |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
class KeyerTests(object): |
|---|
| 36 |
def tearDown(self): |
|---|
| 37 |
self.keyer.shutdown() |
|---|
| 38 |
|
|---|
| 39 |
@defer.deferredGenerator |
|---|
| 40 |
def keyMultiple(self, keyThis): |
|---|
| 41 |
def pressed(got, expected, keySym): |
|---|
| 42 |
self.failUnlessEqual(got, expected) |
|---|
| 43 |
return self.keyer.releaseKey(keySym) |
|---|
| 44 |
|
|---|
| 45 |
for keySym, expected in keyThis.iteritems(): |
|---|
| 46 |
d = self.keyer.pressKey(keySym) |
|---|
| 47 |
d.addCallback(pressed, expected, keySym) |
|---|
| 48 |
wfd = defer.waitForDeferred(d) |
|---|
| 49 |
yield wfd |
|---|
| 50 |
releaseCode = wfd.getResult() |
|---|
| 51 |
self.failUnlessEqual(releaseCode, expected) |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
class TestKeyerInVitro(KeyerTests, TestCase): |
|---|
| 55 |
def setUp(self): |
|---|
| 56 |
keyer.BINARY = "../test/mocktypist.py" |
|---|
| 57 |
self.keyer = keyer.Keyer() |
|---|
| 58 |
|
|---|
| 59 |
def testSingle(self): |
|---|
| 60 |
keySym = 'a' |
|---|
| 61 |
|
|---|
| 62 |
def first(keycode): |
|---|
| 63 |
self.failUnlessEqual(keycode, 38) |
|---|
| 64 |
d2 = self.keyer.releaseKey(keySym) |
|---|
| 65 |
d2.addCallback(second) |
|---|
| 66 |
return d2 |
|---|
| 67 |
|
|---|
| 68 |
def second(keycode): |
|---|
| 69 |
self.failUnlessEqual(keycode, 38) |
|---|
| 70 |
|
|---|
| 71 |
d = self.keyer.pressKey(keySym) |
|---|
| 72 |
d.addCallback(first) |
|---|
| 73 |
return d |
|---|
| 74 |
|
|---|
| 75 |
def testMultiple(self): |
|---|
| 76 |
keyThis = { |
|---|
| 77 |
'a':38, |
|---|
| 78 |
'b':56, |
|---|
| 79 |
'1':10 |
|---|
| 80 |
} |
|---|
| 81 |
return self.keyMultiple(keyThis) |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
class TestKeyerInVivo(KeyerTests, TestCase): |
|---|
| 85 |
def setUp(self): |
|---|
| 86 |
keyer.BINARY = REAL_BINARY |
|---|
| 87 |
self.keyer = keyer.Keyer() |
|---|
| 88 |
|
|---|
| 89 |
def testSingle(self): |
|---|
| 90 |
keySym = 'a' |
|---|
| 91 |
|
|---|
| 92 |
def first(keycode): |
|---|
| 93 |
self.failUnlessEqual(keycode, 38) |
|---|
| 94 |
d2 = self.keyer.releaseKey(keySym) |
|---|
| 95 |
d2.addCallback(second) |
|---|
| 96 |
return d2 |
|---|
| 97 |
|
|---|
| 98 |
def second(keycode): |
|---|
| 99 |
self.failUnlessEqual(keycode, 38) |
|---|
| 100 |
|
|---|
| 101 |
d = self.keyer.pressKey(keySym) |
|---|
| 102 |
d.addCallback(first) |
|---|
| 103 |
return d |
|---|
| 104 |
|
|---|
| 105 |
def testMultiple(self): |
|---|
| 106 |
keyThis = { |
|---|
| 107 |
'a':38, |
|---|
| 108 |
'b':56, |
|---|
| 109 |
'1':10 |
|---|
| 110 |
} |
|---|
| 111 |
return self.keyMultiple(keyThis) |
|---|