test_misc.py revision 16a8d09569a2ebd598cef72fa605be6fb4563607
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is the Python XPCOM language bindings.
#
# The Initial Developer of the Original Code is
# Activestate Tool Corp.
# Portions created by the Initial Developer are Copyright (C) 2000
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Mark Hammond <MarkH@ActiveState.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
import xpcom
import xpcom.components
import string
from pyxpcom_test_tools import testmain
import unittest
verbose_level = 0
def get_sample_component_cpp():
global reportedSampleMissing
try:
except xpcom.COMException:
if not reportedSampleMissing:
print "***"
print "*** This test requires an XPCOM sample component,"
print "*** which does not exist. To build this test, you"
print "*** and run 'make', then run this test again."
print "***"
else:
print "(skipping - no C++ sample...) ",
return None
def get_sample_component_js():
# This should *always* exist - no special make process.
def testAllInterfaces(self):
"Dump every interface under the sun!"
if verbose_level:
print "Dumping every interface I can find"
num = 0
while rc==0:
try:
except xpcom.COMException:
if verbose_level:
print "Can't dump", item
continue # Dont bother dumping this.
if verbose_level:
print text
if num < 200:
def testContractIDs(self):
"""Enumerate all the ContractIDs registered"""
n = 0
while enum.hasMoreElements():
n = n + 1
if verbose_level:
if n < 200:
print "Only found", n, "ContractIDs - this seems unusually low!"
"""Test the standard Netscape 'sample' sample"""
c = get_sample_component_cpp()
if c is None:
return
if not test_flat:
c.value = "new value"
c.poke("poked value")
c.writeValue("Python just poked:")
def testSampleComponentFlat(self):
"""Test the standard Netscape 'sample' sample using interface flattening"""
def testSampleComponentOld(self):
"""Test the standard Netscape 'sample' sample using explicit QI"""
def _doTestHash(self, c):
"Test that hashing COM objects works"
d = {}
d[c] = None
if not d.has_key(c):
raise RuntimeError, "Can't get the exact same object back!"
raise RuntimeError, "Can't get back as nsISupports"
# And the same in reverse - stick an nsISupports in, and make sure an explicit interface comes back.
d = {}
# contractid = "@mozilla.org/sample;1" # The C++ version.
# c = xpcom.components.classes[contractid].createInstance() \
# .queryInterface(xpcom.components.interfaces.nsISupports)
d[c] = None
if not d.has_key(c):
raise RuntimeError, "Can't get the exact same object back!"
raise RuntimeError, "Can't get back as nsISupports"
def testHashJS(self):
c = get_sample_component_js()
self._doTestHash(c)
def testHashCPP(self):
c = get_sample_component_cpp()
if c is not None:
self._doTestHash(c)
"Do some basic IID semantic tests."
iid_str = "{7ee4bdc6-cb53-42c1-a9e4-616b8e012aba}"
# If the above work, this shoud too, but WTF
dict = {}
else:
def testReprPython(self):
"Test repr() of Python objects"
self._doTestRepr("Python.TestComponent", "nsIPythonTestInterfaceDOMStrings nsIPythonTestInterfaceExtra nsIPythonTestInterface")
# JS does not provide class-info :(
#def testReprJS(self):
# self._doTestRepr("@mozilla.org/jssample;1", "nsISample")
def testReprSample(self):
"Test repr() of non-Python objects"
if ob is None:
return
"Test the unwrap facilities"
def testUnwrap(self):
# First test that a Python object can be unwrapped.
# This depends on our __repr__ implementation, but that's OK - it
# can be updated should our __repr__ change :)
# Test that a non-Python implemented object can NOT be unwrapped.
if ob is None:
return
# Test we can QI for a non-scriptable interface. We can't *do* much
# with it (other than pass it on), but we should still work and get
# a basic wrapper.
if __name__=='__main__':
testmain()