16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# ***** BEGIN LICENSE BLOCK *****
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Version: MPL 1.1/GPL 2.0/LGPL 2.1
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# The contents of this file are subject to the Mozilla Public License Version
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# 1.1 (the "License"); you may not use this file except in compliance with
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the License. You may obtain a copy of the License at
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# http://www.mozilla.org/MPL/
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Software distributed under the License is distributed on an "AS IS" basis,
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# for the specific language governing rights and limitations under the
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# License.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# The Original Code is the Python XPCOM language bindings.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# The Initial Developer of the Original Code is
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# ActiveState Tool Corp.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Portions created by the Initial Developer are Copyright (C) 2000,2001
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the Initial Developer. All Rights Reserved.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Contributor(s):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Mark Hammond <MarkH@ActiveState.com> (original author)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Alternatively, the contents of this file may be used under the terms of
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# either the GNU General Public License Version 2 or later (the "GPL"), or
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# in which case the provisions of the GPL or the LGPL are applicable instead
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# of those above. If you wish to allow use of your version of this file only
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# under the terms of either the GPL or the LGPL, and not to allow others to
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# use your version of this file under the terms of the MPL, indicate your
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# decision by deleting the provisions above and replace them with the notice
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# and other provisions required by the GPL or the LGPL. If you do not delete
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the provisions above, a recipient may use your version of this file under
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the terms of any one of the MPL, the GPL or the LGPL.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# ***** END LICENSE BLOCK *****
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncimport xpcom
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfrom xpcom import _xpcom, components, COMException, ServerException, nsError
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfrom StringIO import StringIO
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncimport unittest
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfrom pyxpcom_test_tools import testmain
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsynctest_data = "abcdefeghijklmnopqrstuvwxyz"
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncclass koTestSimpleStream:
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync _com_interfaces_ = [components.interfaces.nsIInputStream]
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync # We avoid registering this object - see comments in get_test_inout_? below.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def __init__(self):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.data=StringIO(test_data)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self._non_blocking = False
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def close( self ):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync pass
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def available( self ):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return self.data.len-self.data.pos
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def readStr( self, amount):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return self.data.read(amount)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync read=readStr
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def get_observer( self ):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync raise ServerException(nsError.NS_ERROR_NOT_IMPLEMENTED)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def set_observer( self, param0 ):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync raise ServerException(nsError.NS_ERROR_NOT_IMPLEMENTED)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def isNonBlocking(self):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return self._non_blocking
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncdef get_test_input():
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync # We use a couple of internal hacks here that mean we can avoid having the object
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync # registered. This code means that we are still working over the xpcom boundaries, tho
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync # (and the point of this test is not the registration, etc).
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync import xpcom.server, xpcom.client
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync ob = xpcom.server.WrapObject( koTestSimpleStream(), _xpcom.IID_nsISupports)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync ob = xpcom.client.Component(ob._comobj_, components.interfaces.nsIInputStream)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return ob
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncclass StreamTests(unittest.TestCase):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def testInput(self):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.do_test_input( get_test_input() )
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def do_test_input(self, myStream):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.failUnlessEqual(str(myStream.read(5)), test_data[:5])
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.failUnlessEqual(str(myStream.read(0)), '')
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.failUnlessEqual(str(myStream.read(5)), test_data[5:10])
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.failUnlessEqual(str(myStream.read(-1)), test_data[10:])
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.failIf(myStream.isNonBlocking(), "Expected default to be blocking")
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync # stream observer mechanism has changed - we should test that.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncif __name__=='__main__':
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync testmain()