| 1 | # Copyright (c) 2006 Twisted Matrix Laboratories. |
|---|
| 2 | # See LICENSE for details. |
|---|
| 3 | |
|---|
| 4 | import os, sys |
|---|
| 5 | import win32api, win32console, win32con |
|---|
| 6 | |
|---|
| 7 | from twisted.trial import unittest |
|---|
| 8 | from twisted.python import filepath |
|---|
| 9 | from twisted.internet import error, defer, protocol, reactor |
|---|
| 10 | |
|---|
| 11 | import _win32stdio as stdio |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | def createKeyEvent(char, repeat=1): |
|---|
| 16 | evt = win32console.PyINPUT_RECORDType(win32console.KEY_EVENT) |
|---|
| 17 | evt.KeyDown = True |
|---|
| 18 | evt.Char = char |
|---|
| 19 | evt.RepeatCount = repeat |
|---|
| 20 | |
|---|
| 21 | return evt |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | stdin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE) |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | class ConIOTestProtocol(protocol.Protocol): |
|---|
| 28 | def __init__(self): |
|---|
| 29 | self.onData = defer.Deferred() |
|---|
| 30 | |
|---|
| 31 | def dataReceived(self, data): |
|---|
| 32 | self.onData.callback(data) |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | class ConIOTestCase(unittest.TestCase): |
|---|
| 36 | def setUp(self): |
|---|
| 37 | p = ConIOTestProtocol() |
|---|
| 38 | self.stdio = stdio.StandardIO(p) |
|---|
| 39 | self.onData = p.onData |
|---|
| 40 | |
|---|
| 41 | def tearDown(self): |
|---|
| 42 | self.stdio._pause() |
|---|
| 43 | try: |
|---|
| 44 | self.stdio._stopPolling() |
|---|
| 45 | except error.AlreadyCalled: |
|---|
| 46 | pass |
|---|
| 47 | |
|---|
| 48 | def testRead(self): |
|---|
| 49 | def cb(result): |
|---|
| 50 | self.failUnlessEqual(result, "hello\r\n") |
|---|
| 51 | |
|---|
| 52 | data = u"hello\r" |
|---|
| 53 | records = [createKeyEvent(c) for c in data] |
|---|
| 54 | |
|---|
| 55 | stdin.WriteConsoleInput(records) |
|---|
| 56 | return self.onData.addCallback(cb) |
|---|
| 57 | |
|---|
| 58 | def testReadMultiple(self): |
|---|
| 59 | def cb(result): |
|---|
| 60 | self.failUnlessEqual(result, "hhheeellllllooo\r\n\r\n\r\n") |
|---|
| 61 | |
|---|
| 62 | data = u"hello\r" |
|---|
| 63 | records = [createKeyEvent(c, 3) for c in data] |
|---|
| 64 | |
|---|
| 65 | stdin.WriteConsoleInput(records) |
|---|
| 66 | return self.onData.addCallback(cb) |
|---|
| 67 | |
|---|
| 68 | def testReadWithDelete(self): |
|---|
| 69 | def cb(result): |
|---|
| 70 | self.failUnlessEqual(result, "world\r\n") |
|---|
| 71 | |
|---|
| 72 | data = u"hello" + u"\b" * 5 + u"world\r" |
|---|
| 73 | records = [createKeyEvent(c) for c in data] |
|---|
| 74 | |
|---|
| 75 | stdin.WriteConsoleInput(records) |
|---|
| 76 | return self.onData.addCallback(cb) |
|---|
| 77 | |
|---|
| 78 | def testDeleteBoundary(self): |
|---|
| 79 | def cb(result): |
|---|
| 80 | self.failUnlessEqual(result, "w\r\n") |
|---|
| 81 | |
|---|
| 82 | data = u"h" + "\b\b" + u"w\r" |
|---|
| 83 | records = [createKeyEvent(c) for c in data] |
|---|
| 84 | |
|---|
| 85 | stdin.WriteConsoleInput(records) |
|---|
| 86 | return self.onData.addCallback(cb) |
|---|
| 87 | |
|---|
| 88 | def testDeleteFullBoundary(self): |
|---|
| 89 | def cb(result): |
|---|
| 90 | self.failUnlessEqual(result, "w\r\n") |
|---|
| 91 | |
|---|
| 92 | data = u"h" * 500 + "\b" * 600 + u"w\r" |
|---|
| 93 | records = [createKeyEvent(c) for c in data] |
|---|
| 94 | |
|---|
| 95 | stdin.WriteConsoleInput(records) |
|---|
| 96 | return self.onData.addCallback(cb) |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | class ConIORawTestCase(unittest.TestCase): |
|---|
| 100 | def setUp(self): |
|---|
| 101 | p = ConIOTestProtocol() |
|---|
| 102 | self.stdio = stdio.StandardIO(p) |
|---|
| 103 | self.stdio.stdin.enableRawMode() |
|---|
| 104 | self.onData = p.onData |
|---|
| 105 | |
|---|
| 106 | def tearDown(self): |
|---|
| 107 | self.stdio.stdin.enableRawMode(False) |
|---|
| 108 | |
|---|
| 109 | self.stdio._pause() |
|---|
| 110 | try: |
|---|
| 111 | self.stdio._stopPolling() |
|---|
| 112 | except error.AlreadyCalled: |
|---|
| 113 | pass |
|---|
| 114 | |
|---|
| 115 | def testRead(self): |
|---|
| 116 | def cb(result): |
|---|
| 117 | self.failUnlessEqual(result, "hello") |
|---|
| 118 | |
|---|
| 119 | data = u"hello" |
|---|
| 120 | records = [createKeyEvent(c) for c in data] |
|---|
| 121 | |
|---|
| 122 | stdin.WriteConsoleInput(records) |
|---|
| 123 | return self.onData.addCallback(cb) |
|---|
| 124 | |
|---|
| 125 | def testReadMultiple(self): |
|---|
| 126 | def cb(result): |
|---|
| 127 | self.failUnlessEqual(result, "hhheeellllllooo") |
|---|
| 128 | |
|---|
| 129 | data = u"hello" |
|---|
| 130 | records = [createKeyEvent(c, 3) for c in data] |
|---|
| 131 | |
|---|
| 132 | stdin.WriteConsoleInput(records) |
|---|
| 133 | return self.onData.addCallback(cb) |
|---|
| 134 | |
|---|
| 135 | def testReadWithDelete(self): |
|---|
| 136 | def cb(result): |
|---|
| 137 | self.failUnlessEqual(result, "hello\b\b\b\b\bworld") |
|---|
| 138 | |
|---|
| 139 | data = u"hello" + u"\b" * 5 + u"world" |
|---|
| 140 | records = [createKeyEvent(c) for c in data] |
|---|
| 141 | |
|---|
| 142 | stdin.WriteConsoleInput(records) |
|---|
| 143 | return self.onData.addCallback(cb) |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | class ConIORawSwitchTestCase(unittest.TestCase): |
|---|
| 147 | def setUp(self): |
|---|
| 148 | p = ConIOTestProtocol() |
|---|
| 149 | self.stdio = stdio.StandardIO(p) |
|---|
| 150 | self.onData = p.onData |
|---|
| 151 | |
|---|
| 152 | def tearDown(self): |
|---|
| 153 | self.stdio.stdin.enableRawMode(False) |
|---|
| 154 | |
|---|
| 155 | self.stdio._pause() |
|---|
| 156 | try: |
|---|
| 157 | self.stdio._stopPolling() |
|---|
| 158 | except error.AlreadyCalled: |
|---|
| 159 | pass |
|---|
| 160 | |
|---|
| 161 | def testEnableRawMode(self): |
|---|
| 162 | def cb(result): |
|---|
| 163 | self.failUnlessEqual(result, "hello") |
|---|
| 164 | |
|---|
| 165 | data = u"hello" |
|---|
| 166 | records = [createKeyEvent(c) for c in data] |
|---|
| 167 | |
|---|
| 168 | stdin.WriteConsoleInput(records) |
|---|
| 169 | self.stdio.stdin.enableRawMode() |
|---|
| 170 | |
|---|
| 171 | return self.onData.addCallback(cb) |
|---|