Back to index

lightning-sunbird  0.9+nobinonly
test_isupports_primitives.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 # Test our support for the interfaces defined in nsISupportsPrimitives.idl
00039 #
00040 # The framework supports nsISupportsCString and nsISupportsString, but
00041 # only if our class doesnt provide explicit support.
00042 
00043 from xpcom import components
00044 
00045 class NoSupportsString:
00046     _com_interfaces_ = [components.interfaces.nsISupports]
00047     pass
00048 
00049 class ImplicitSupportsString:
00050     _com_interfaces_ = [components.interfaces.nsISupports]
00051     def __str__(self):
00052         return "<MyImplicitStrObject>"
00053 
00054 class ExplicitSupportsString:
00055     _com_interfaces_ = [components.interfaces.nsISupports, components.interfaces.nsISupportsCString]
00056     # __str__ will be ignored by XPCOM, as we have _explicit_ support.
00057     def __str__(self):
00058         return "<MyImplicitStrObject>"
00059     # This is the one that will be used.
00060     def toString(self):
00061         return "<MyExplicitStrObject>"
00062 
00063 class ImplicitSupportsInt:
00064     _com_interfaces_ = [components.interfaces.nsISupports]
00065     def __int__(self):
00066         return 99
00067 
00068 class ExplicitSupportsInt:
00069     _com_interfaces_ = [components.interfaces.nsISupportsPRInt32]
00070     def get_data(self):
00071         return 99
00072 
00073 class ImplicitSupportsLong:
00074     _com_interfaces_ = [components.interfaces.nsISupports]
00075     def __long__(self):
00076         return 99L
00077 
00078 class ExplicitSupportsLong:
00079     _com_interfaces_ = [components.interfaces.nsISupportsPRInt64]
00080     def get_data(self):
00081         return 99
00082 
00083 class ExplicitSupportsFloat:
00084     _com_interfaces_ = [components.interfaces.nsISupportsDouble]
00085     def get_data(self):
00086         return 99.99
00087 
00088 class ImplicitSupportsFloat:
00089     _com_interfaces_ = [components.interfaces.nsISupports]
00090     def __float__(self):
00091         return 99.99
00092 
00093 def test():
00094     import xpcom.server, xpcom.client
00095     ob = xpcom.server.WrapObject( NoSupportsString(), components.interfaces.nsISupports)
00096     if not str(ob).startswith("<XPCOM "):
00097         raise RuntimeError, "Wrong str() value: %s" % (ob,)
00098 
00099     ob = xpcom.server.WrapObject( ImplicitSupportsString(), components.interfaces.nsISupports)
00100     if str(ob) != "<MyImplicitStrObject>":
00101         raise RuntimeError, "Wrong str() value: %s" % (ob,)
00102 
00103     ob = xpcom.server.WrapObject( ExplicitSupportsString(), components.interfaces.nsISupports)
00104     if str(ob) != "<MyExplicitStrObject>":
00105         raise RuntimeError, "Wrong str() value: %s" % (ob,)
00106 
00107     # Try our conversions.
00108     try:
00109         int(ob)
00110         raise RuntimeError, "Expected to get a ValueError converting this COM object to an int"
00111     except ValueError:
00112         pass
00113     ob = xpcom.server.WrapObject( ExplicitSupportsInt(), components.interfaces.nsISupports)
00114     if int(ob) != 99:
00115         raise RuntimeError, "Bad value: %s" % (int(ob),)
00116     if float(ob) != 99.0:
00117         raise RuntimeError, "Bad value: %s" % (float(ob),)
00118 
00119     ob = xpcom.server.WrapObject( ImplicitSupportsInt(), components.interfaces.nsISupports)
00120     if int(ob) != 99:
00121         raise RuntimeError, "Bad value: %s" % (int(ob),)
00122     if float(ob) != 99.0:
00123         raise RuntimeError, "Bad value: %s" % (float(ob),)
00124 
00125     ob = xpcom.server.WrapObject( ExplicitSupportsLong(), components.interfaces.nsISupports)
00126     if long(ob) != 99 or not repr(long(ob)).endswith("L"):
00127         raise RuntimeError, "Bad value: %s" % (repr(long(ob)),)
00128     if float(ob) != 99.0:
00129         raise RuntimeError, "Bad value: %s" % (float(ob),)
00130 
00131     ob = xpcom.server.WrapObject( ImplicitSupportsLong(), components.interfaces.nsISupports)
00132     if long(ob) != 99 or not repr(long(ob)).endswith("L"):
00133         raise RuntimeError, "Bad value: %s" % (repr(long(ob)),)
00134     if float(ob) != 99.0:
00135         raise RuntimeError, "Bad value: %s" % (float(ob),)
00136 
00137     ob = xpcom.server.WrapObject( ExplicitSupportsFloat(), components.interfaces.nsISupports)
00138     if float(ob) != 99.99:
00139         raise RuntimeError, "Bad value: %s" % (float(ob),)
00140     if int(ob) != 99:
00141         raise RuntimeError, "Bad value: %s" % (int(ob),)
00142 
00143     ob = xpcom.server.WrapObject( ImplicitSupportsFloat(), components.interfaces.nsISupports)
00144     if float(ob) != 99.99:
00145         raise RuntimeError, "Bad value: %s" % (float(ob),)
00146     if int(ob) != 99:
00147         raise RuntimeError, "Bad value: %s" % (int(ob),)
00148 
00149     print "The nsISupports primitive interface tests appeared to work"
00150 test()