VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/test/test_misc.py@ 53409

Last change on this file since 53409 was 11746, checked in by vboxsync, 16 years ago

export python lib to OSE

  • Property svn:eol-style set to native
File size: 9.2 KB
Line 
1# ***** BEGIN LICENSE BLOCK *****
2# Version: MPL 1.1/GPL 2.0/LGPL 2.1
3#
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/
8#
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
12# License.
13#
14# The Original Code is the Python XPCOM language bindings.
15#
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.
20#
21# Contributor(s):
22# Mark Hammond <[email protected]>
23#
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.
35#
36# ***** END LICENSE BLOCK *****
37
38import xpcom
39import xpcom.client
40import xpcom.server
41import xpcom._xpcom
42import xpcom.components
43import string
44from pyxpcom_test_tools import testmain
45
46import unittest
47
48import traceback, getopt, sys
49
50verbose_level = 0
51
52reportedSampleMissing = 0
53
54def get_sample_component_cpp():
55 global reportedSampleMissing
56 contractid = "@mozilla.org/sample;1" # The C++ version.
57 try:
58 return xpcom.components.classes[contractid].createInstance()
59 except xpcom.COMException:
60 if not reportedSampleMissing:
61 print "***"
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."
66 print "***"
67 reportedSampleMissing = 1
68 else:
69 print "(skipping - no C++ sample...) ",
70 return None
71
72def 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()
76
77class 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()
82
83 if verbose_level:
84 print "Dumping every interface I can find"
85 enum = iim.EnumerateInterfaces()
86 rc = enum.First()
87 num = 0
88 while rc==0:
89 item = enum.CurrentItem(xpcom._xpcom.IID_nsIInterfaceInfo)
90 try:
91 iid = item.GetIID()
92 except xpcom.COMException:
93 if verbose_level:
94 print "Can't dump", item
95 continue # Dont bother dumping this.
96 interface = xpcom.xpt.Interface(iid)
97 num = num + 1
98 text = interface.Describe()
99 if verbose_level:
100 print text
101
102 rc = enum.Next()
103 if num < 200:
104 print "Only found", num, "interfaces - this seems unusually low!"
105
106class TestEnumContractIDs(unittest.TestCase):
107 def testContractIDs(self):
108 """Enumerate all the ContractIDs registered"""
109 enum = xpcom.components.registrar.enumerateContractIDs()
110 n = 0
111 while enum.hasMoreElements():
112 item = enum.getNext(xpcom.components.interfaces.nsISupportsCString)
113 n = n + 1
114 if verbose_level:
115 print "ContractID:", item.data
116 if n < 200:
117 print "Only found", n, "ContractIDs - this seems unusually low!"
118
119class TestSampleComponent(unittest.TestCase):
120 def _doTestSampleComponent(self, test_flat = 0):
121 """Test the standard Netscape 'sample' sample"""
122 c = get_sample_component_cpp()
123 if c is None:
124 return
125 if not test_flat:
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:")
133
134 def testSampleComponentFlat(self):
135 """Test the standard Netscape 'sample' sample using interface flattening"""
136 self._doTestSampleComponent(1)
137
138 def testSampleComponentOld(self):
139 """Test the standard Netscape 'sample' sample using explicit QI"""
140 self._doTestSampleComponent(0)
141
142 def _doTestHash(self, c):
143 "Test that hashing COM objects works"
144 d = {}
145 d[c] = None
146 if not d.has_key(c):
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"
150
151 # And the same in reverse - stick an nsISupports in, and make sure an explicit interface comes back.
152 d = {}
153# contractid = "@mozilla.org/sample;1" # The C++ version.
154# c = xpcom.components.classes[contractid].createInstance() \
155# .queryInterface(xpcom.components.interfaces.nsISupports)
156 d[c] = None
157 if not d.has_key(c):
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"
161
162 def testHashJS(self):
163 c = get_sample_component_js()
164 self._doTestHash(c)
165
166 def testHashCPP(self):
167 c = get_sample_component_cpp()
168 if c is not None:
169 self._doTestHash(c)
170
171
172class TestIIDs(unittest.TestCase):
173 def TestIIDs(self):
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
182 dict = {}
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")
186
187class TestRepr(unittest.TestCase):
188 def _doTestRepr(self, progid, interfaces):
189 if isinstance(progid, str):
190 ob = xpcom.components.classes[progid].createInstance()
191 else:
192 ob = progid
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))
196
197 def testReprPython(self):
198 "Test repr() of Python objects"
199 self._doTestRepr("Python.TestComponent", "nsIPythonTestInterfaceDOMStrings nsIPythonTestInterfaceExtra nsIPythonTestInterface")
200
201 # JS does not provide class-info :(
202 #def testReprJS(self):
203 # self._doTestRepr("@mozilla.org/jssample;1", "nsISample")
204
205 def testReprSample(self):
206 "Test repr() of non-Python objects"
207 ob = get_sample_component_cpp()
208 if ob is None:
209 return
210 self._doTestRepr(ob, "nsISample")
211
212class 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()
223 if ob is None:
224 return
225 self.failUnlessRaises(ValueError, xpcom.server.UnwrapObject, ob)
226
227class TestNonScriptable(unittest.TestCase):
228 def testQI(self):
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
231 # a basic wrapper.
232 ob = xpcom.components.classes["Python.TestComponent"].createInstance()
233 ob = ob.queryInterface(xpcom._xpcom.IID_nsIInternalPython)
234
235if __name__=='__main__':
236 testmain()
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette