Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / test / test_serialport.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for L{twisted.internet.serialport}.
6 """
7
8 from twisted.trial import unittest
9 from twisted.python.failure import Failure
10 from twisted.internet.protocol import Protocol
11 from twisted.internet.error import ConnectionDone
12 try:
13     from twisted.internet import serialport
14 except ImportError:
15     serialport = None
16
17
18
19 class DoNothing(object):
20     """
21     Object with methods that do nothing.
22     """
23
24     def __init__(self, *args, **kwargs):
25         pass
26
27
28     def __getattr__(self, attr):
29         return lambda *args, **kwargs: None
30
31
32
33 class SerialPortTests(unittest.TestCase):
34     """
35     Minimal testing for Twisted's serial port support.
36
37     See ticket #2462 for the eventual full test suite.
38     """
39
40     if serialport is None:
41         skip = "Serial port support is not available."
42
43
44     def test_connectionMadeLost(self):
45         """
46         C{connectionMade} and C{connectionLost} are called on the protocol by
47         the C{SerialPort}.
48         """
49         # Serial port that doesn't actually connect to anything:
50         class DummySerialPort(serialport.SerialPort):
51             _serialFactory = DoNothing
52
53             def _finishPortSetup(self):
54                 pass # override default win32 actions
55
56         events = []
57
58         class SerialProtocol(Protocol):
59             def connectionMade(self):
60                 events.append("connectionMade")
61
62             def connectionLost(self, reason):
63                 events.append(("connectionLost", reason))
64
65         # Creation of port should result in connectionMade call:
66         port = DummySerialPort(SerialProtocol(), "", reactor=DoNothing())
67         self.assertEqual(events, ["connectionMade"])
68
69         # Simulate reactor calling connectionLost on the SerialPort:
70         f = Failure(ConnectionDone())
71         port.connectionLost(f)
72         self.assertEqual(events, ["connectionMade", ("connectionLost", f)])