Back to index

lightning-sunbird  0.9+nobinonly
test_streams.py
Go to the documentation of this file.
00001 # ***** BEGIN LICENSE BLOCK *****
00002 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003 #
00004 # The contents of this file are subject to the Mozilla Public License Version
00005 # 1.1 (the "License"); you may not use this file except in compliance with
00006 # the License. You may obtain a copy of the License at
00007 # http://www.mozilla.org/MPL/
00008 #
00009 # Software distributed under the License is distributed on an "AS IS" basis,
00010 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011 # for the specific language governing rights and limitations under the
00012 # License.
00013 #
00014 # The Original Code is the Python XPCOM language bindings.
00015 #
00016 # The Initial Developer of the Original Code is
00017 # ActiveState Tool Corp.
00018 # Portions created by the Initial Developer are Copyright (C) 2000,2001
00019 # the Initial Developer. All Rights Reserved.
00020 #
00021 # Contributor(s):
00022 #   Mark Hammond <MarkH@ActiveState.com> (original author)
00023 #
00024 # Alternatively, the contents of this file may be used under the terms of
00025 # either the GNU General Public License Version 2 or later (the "GPL"), or
00026 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027 # in which case the provisions of the GPL or the LGPL are applicable instead
00028 # of those above. If you wish to allow use of your version of this file only
00029 # under the terms of either the GPL or the LGPL, and not to allow others to
00030 # use your version of this file under the terms of the MPL, indicate your
00031 # decision by deleting the provisions above and replace them with the notice
00032 # and other provisions required by the GPL or the LGPL. If you do not delete
00033 # the provisions above, a recipient may use your version of this file under
00034 # the terms of any one of the MPL, the GPL or the LGPL.
00035 #
00036 # ***** END LICENSE BLOCK *****
00037 
00038 import xpcom
00039 from xpcom import _xpcom, components, COMException, ServerException, nsError
00040 from StringIO import StringIO
00041 import unittest
00042 
00043 test_data = "abcdefeghijklmnopqrstuvwxyz"
00044 
00045 class koTestSimpleStream:
00046     _com_interfaces_ = [components.interfaces.nsIInputStream]
00047     # We avoid registering this object - see comments in get_test_inout_? below.
00048 
00049     def __init__(self):
00050         self.data=StringIO(test_data)
00051         self._non_blocking = False
00052 
00053     def close( self ):
00054         pass
00055 
00056     def available( self ):
00057         return self.data.len-self.data.pos
00058 
00059     def readStr( self, amount):
00060         return self.data.read(amount)
00061 
00062     read=readStr
00063 
00064     def get_observer( self ):
00065         raise ServerException(nsError.NS_ERROR_NOT_IMPLEMENTED)
00066 
00067     def set_observer( self, param0 ):
00068         raise ServerException(nsError.NS_ERROR_NOT_IMPLEMENTED)
00069 
00070     def isNonBlocking(self):
00071         return self._non_blocking
00072 
00073 def get_test_input():
00074     # We use a couple of internal hacks here that mean we can avoid having the object
00075     # registered.  This code means that we are still working over the xpcom boundaries, tho
00076     # (and the point of this test is not the registration, etc).
00077     import xpcom.server, xpcom.client
00078     ob = xpcom.server.WrapObject( koTestSimpleStream(), _xpcom.IID_nsISupports)
00079     ob = xpcom.client.Component(ob._comobj_, components.interfaces.nsIInputStream)
00080     return ob
00081 
00082 class StreamTests(unittest.TestCase):
00083     def testInput(self):
00084         self.do_test_input( get_test_input() )
00085 
00086     def do_test_input(self, myStream):
00087         self.failUnlessEqual(str(myStream.read(5)), test_data[:5])
00088         self.failUnlessEqual(str(myStream.read(0)), '')
00089         self.failUnlessEqual(str(myStream.read(5)), test_data[5:10])
00090         self.failUnlessEqual(str(myStream.read(-1)), test_data[10:])
00091         self.failIf(myStream.isNonBlocking(), "Expected default to be blocking")
00092         # stream observer mechanism has changed - we should test that.
00093 
00094 if __name__=='__main__':
00095     unittest.main()