1
# ***** BEGIN LICENSE BLOCK *****
2
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
# The contents of this file are subject to the Mozilla Public License Version
5
# 1.1 (the "License"); you may not use this file except in compliance with
6
# the License. You may obtain a copy of the License at
7
# http://www.mozilla.org/MPL/
9
# Software distributed under the License is distributed on an "AS IS" basis,
10
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
# for the specific language governing rights and limitations under the
14
# The Original Code is the Python XPCOM language bindings.
16
# The Initial Developer of the Original Code is
17
# Activestate Tool Corp.
18
# Portions created by the Initial Developer are Copyright (C) 2000
19
# the Initial Developer. All Rights Reserved.
22
# Mark Hammond <MarkH@ActiveState.com>
24
# Alternatively, the contents of this file may be used under the terms of
25
# either the GNU General Public License Version 2 or later (the "GPL"), or
26
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
# in which case the provisions of the GPL or the LGPL are applicable instead
28
# of those above. If you wish to allow use of your version of this file only
29
# under the terms of either the GPL or the LGPL, and not to allow others to
30
# use your version of this file under the terms of the MPL, indicate your
31
# decision by deleting the provisions above and replace them with the notice
32
# and other provisions required by the GPL or the LGPL. If you do not delete
33
# the provisions above, a recipient may use your version of this file under
34
# the terms of any one of the MPL, the GPL or the LGPL.
36
# ***** END LICENSE BLOCK *****
42
import xpcom.components
44
from pyxpcom_test_tools import testmain
48
import traceback, getopt, sys
52
reportedSampleMissing = 0
54
def get_sample_component_cpp():
55
global reportedSampleMissing
56
contractid = "@mozilla.org/sample;1" # The C++ version.
58
return xpcom.components.classes[contractid].createInstance()
59
except xpcom.COMException:
60
if not reportedSampleMissing:
62
print "*** This test requires an XPCOM sample component,"
63
print "*** which does not exist. To build this test, you"
64
print "*** should change to the 'mozilla/xpcom/sample' directory,"
65
print "*** and run 'make', then run this test again."
67
reportedSampleMissing = 1
69
print "(skipping - no C++ sample...) ",
72
def get_sample_component_js():
73
# This should *always* exist - no special make process.
74
contractid = "@mozilla.org/jssample;1" # the JS version
75
return xpcom.components.classes[contractid].createInstance()
77
class TestDumpInterfaces(unittest.TestCase):
78
def testAllInterfaces(self):
79
"Dump every interface under the sun!"
80
import xpcom, xpcom.xpt, xpcom._xpcom
81
iim = xpcom._xpcom.XPTI_GetInterfaceInfoManager()
84
print "Dumping every interface I can find"
85
enum = iim.EnumerateInterfaces()
89
item = enum.CurrentItem(xpcom._xpcom.IID_nsIInterfaceInfo)
92
except xpcom.COMException:
94
print "Can't dump", item
95
continue # Dont bother dumping this.
96
interface = xpcom.xpt.Interface(iid)
98
text = interface.Describe()
104
print "Only found", num, "interfaces - this seems unusually low!"
106
class TestEnumContractIDs(unittest.TestCase):
107
def testContractIDs(self):
108
"""Enumerate all the ContractIDs registered"""
109
enum = xpcom.components.registrar.enumerateContractIDs()
111
while enum.hasMoreElements():
112
item = enum.getNext(xpcom.components.interfaces.nsISupportsCString)
115
print "ContractID:", item.data
117
print "Only found", n, "ContractIDs - this seems unusually low!"
119
class TestSampleComponent(unittest.TestCase):
120
def _doTestSampleComponent(self, test_flat = 0):
121
"""Test the standard Netscape 'sample' sample"""
122
c = get_sample_component_cpp()
126
c = c.queryInterface(xpcom.components.interfaces.nsISample)
127
self.failUnlessEqual(c.value, "initial value")
128
c.value = "new value"
129
self.failUnlessEqual(c.value, "new value")
130
c.poke("poked value")
131
self.failUnlessEqual(c.value, "poked value")
132
c.writeValue("Python just poked:")
134
def testSampleComponentFlat(self):
135
"""Test the standard Netscape 'sample' sample using interface flattening"""
136
self._doTestSampleComponent(1)
138
def testSampleComponentOld(self):
139
"""Test the standard Netscape 'sample' sample using explicit QI"""
140
self._doTestSampleComponent(0)
142
def _doTestHash(self, c):
143
"Test that hashing COM objects works"
147
raise RuntimeError, "Can't get the exact same object back!"
148
if not d.has_key(c.queryInterface(xpcom.components.interfaces.nsISupports)):
149
raise RuntimeError, "Can't get back as nsISupports"
151
# And the same in reverse - stick an nsISupports in, and make sure an explicit interface comes back.
153
# contractid = "@mozilla.org/sample;1" # The C++ version.
154
# c = xpcom.components.classes[contractid].createInstance() \
155
# .queryInterface(xpcom.components.interfaces.nsISupports)
158
raise RuntimeError, "Can't get the exact same object back!"
159
if not d.has_key(c.queryInterface(xpcom.components.interfaces.nsISample)):
160
raise RuntimeError, "Can't get back as nsISupports"
162
def testHashJS(self):
163
c = get_sample_component_js()
166
def testHashCPP(self):
167
c = get_sample_component_cpp()
172
class TestIIDs(unittest.TestCase):
174
"Do some basic IID semantic tests."
175
iid_str = "{7ee4bdc6-cb53-42c1-a9e4-616b8e012aba}"
176
IID = xpcom._xpcom.IID
177
self.failUnlessEqual(IID(iid_str), IID(iid_str))
178
self.failUnlessEqual(hash(IID(iid_str)), hash(IID(iid_str)))
179
self.failUnlessEqual(IID(iid_str), IID(iid_str.upper()))
180
self.failUnlessEqual(hash(IID(iid_str)), hash(IID(iid_str.upper())))
181
# If the above work, this shoud too, but WTF
183
dict[IID(iid_str)] = None
184
self.failUnless(dict.has_key(IID(iid_str)), "hashes failed in dictionary")
185
self.failUnless(dict.has_key(IID(iid_str.upper())), "uppercase hash failed in dictionary")
187
class TestRepr(unittest.TestCase):
188
def _doTestRepr(self, progid, interfaces):
189
if isinstance(progid, str):
190
ob = xpcom.components.classes[progid].createInstance()
193
self.failUnless(repr(ob).find(str(progid)) >= 0, repr(ob))
194
for interface_name in interfaces.split():
195
self.failUnless(repr(ob).find(interface_name) >= 0, repr(ob))
197
def testReprPython(self):
198
"Test repr() of Python objects"
199
self._doTestRepr("Python.TestComponent", "nsIPythonTestInterfaceDOMStrings nsIPythonTestInterfaceExtra nsIPythonTestInterface")
201
# JS does not provide class-info :(
202
#def testReprJS(self):
203
# self._doTestRepr("@mozilla.org/jssample;1", "nsISample")
205
def testReprSample(self):
206
"Test repr() of non-Python objects"
207
ob = get_sample_component_cpp()
210
self._doTestRepr(ob, "nsISample")
212
class TestUnwrap(unittest.TestCase):
213
"Test the unwrap facilities"
214
def testUnwrap(self):
215
# First test that a Python object can be unwrapped.
216
ob = xpcom.components.classes["Python.TestComponent"].createInstance()
217
pyob = xpcom.server.UnwrapObject(ob)
218
# This depends on our __repr__ implementation, but that's OK - it
219
# can be updated should our __repr__ change :)
220
self.failUnless(str(pyob).startswith("<component:py_test_component.PythonTestComponent"))
221
# Test that a non-Python implemented object can NOT be unwrapped.
222
ob = get_sample_component_cpp()
225
self.failUnlessRaises(ValueError, xpcom.server.UnwrapObject, ob)
227
class TestNonScriptable(unittest.TestCase):
229
# Test we can QI for a non-scriptable interface. We can't *do* much
230
# with it (other than pass it on), but we should still work and get
232
ob = xpcom.components.classes["Python.TestComponent"].createInstance()
233
ob = ob.queryInterface(xpcom._xpcom.IID_nsIInternalPython)
235
if __name__=='__main__':