Back to index

lightning-sunbird  0.9+nobinonly
py_test_component.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 # NOTE: This is a TEST interface, not a DEMO interface :-)
00039 # We try to get as many data-types etc exposed, meaning this
00040 # doesnt really make a good demo of a "simple component"
00041 
00042 
00043 from xpcom import components, verbose
00044 
00045 class PythonTestComponent:
00046     # Note we only list the "child" interface, not our intermediate interfaces
00047     # (which we must, by definition, also support)
00048     _com_interfaces_ = components.interfaces.nsIPythonTestInterfaceDOMStrings
00049     _reg_clsid_ = "{7EE4BDC6-CB53-42c1-A9E4-616B8E012ABA}"
00050     _reg_contractid_ = "Python.TestComponent"
00051     def __init__(self):
00052         self.boolean_value = 1
00053         self.octet_value = 2
00054         self.short_value = 3
00055         self.ushort_value = 4
00056         self.long_value = 5
00057         self.ulong_value = 6
00058         self.long_long_value = 7
00059         self.ulong_long_value = 8
00060         self.float_value = 9.0
00061         self.double_value = 10.0
00062         self.char_value = "a"
00063         self.wchar_value = "b"
00064         self.string_value = "cee"
00065         self.wstring_value = "dee"
00066         self.astring_value = "astring"
00067         self.acstring_value = "acstring"
00068         self.utf8string_value = "utf8string"
00069         self.iid_value = self._reg_clsid_
00070         self.interface_value = None
00071         self.isupports_value = None
00072         self.domstring_value = "dom"
00073 
00074     def __del__(self):
00075         if verbose:
00076             print "Python.TestComponent: __del__ method called - object is destructing"
00077 
00078     def do_boolean(self, p1, p2):
00079         # boolean                  do_boolean(in boolean p1, inout boolean p2, out boolean p3);
00080         ret = p1 ^ p2
00081         return ret, not ret, ret
00082 
00083     def do_octet(self, p1, p2):
00084         # octet                    do_octet(in octet p1, inout octet p2, out octet p3);
00085         return p1+p2, p1-p2, p1*p2
00086 
00087     def do_short(self, p1, p2):
00088         # short                    do_short(in short p1, inout short p2, out short p3);
00089         return p1+p2, p1-p2, p1*p2
00090 
00091     def do_unsigned_short(self, p1, p2):
00092         # unsigned short           do_unsigned_short(in unsigned short p1, inout unsigned short p2, out unsigned short p3);
00093         return p1+p2, p1-p2, p1*p2
00094     def do_long(self, p1, p2):
00095         # long                     do_long(in long p1, inout long p2, out long p3);
00096         return p1+p2, p1-p2, p1*p2
00097 
00098     def do_unsigned_long(self, p1, p2):
00099         # unsigned long            do_unsigned_long(in unsigned long p1, inout unsigned long p2, out unsigned long p3);
00100         return p1+p2, p1-p2, p1*p2
00101     def do_long_long(self, p1, p2):
00102         #  long long                do_long_long(in long long p1, inout long long p2, out long long p3);
00103         return p1+p2, p1-p2, p1*p2
00104     def do_unsigned_long_long(self, p1, p2):
00105         # unsigned long long       do_unsigned_long_long(in unsigned long long p1, inout unsigned long long p2, out unsigned long long p3);
00106         return p1+p2, p1-p2, p1*p2
00107     def do_float(self, p1, p2):
00108         # float                    do_float(in float p1, inout float p2, out float p3);
00109         return p1+p2, p1-p2, p1*p2
00110     def do_double(self, p1, p2):
00111         # double                   do_double(in double p1, inout double p2, out double p3);
00112         return p1+p2, p1-p2, p1*p2
00113     def do_char(self, p1, p2):
00114         # char                     do_char(in char p1, inout char p2, out char p3);
00115         return chr(ord(p1)+ord(p2)), p2, p1
00116     def do_wchar(self, p1, p2):
00117         # wchar                    do_wchar(in wchar p1, inout wchar p2, out wchar p3);
00118         return chr(ord(p1)+ord(p2)), p2, p1
00119     def do_string(self, p1, p2):
00120         # string                   do_string(in string p1, inout string p2, out string p3);
00121         ret = ""
00122         if p1 is not None: ret = ret + p1
00123         if p2 is not None: ret = ret + p2
00124         return ret, p1, p2
00125     def do_wstring(self, p1, p2):
00126         # wstring                  do_wstring(in wstring p1, inout wstring p2, out wstring p3);
00127         ret = u""
00128         if p1 is not None: ret = ret + p1
00129         if p2 is not None: ret = ret + p2
00130         return ret, p1, p2
00131     def do_nsIIDRef(self, p1, p2):
00132         # nsIIDRef                 do_nsIIDRef(in nsIIDRef p1, inout nsIIDRef p2, out nsIIDRef p3);
00133         return p1, self._reg_clsid_, p2
00134     def do_nsIPythonTestInterface(self, p1, p2):
00135         # nsIPythonTestInterface   do_nsIPythonTestInterface(in nsIPythonTestInterface p1, inout nsIPythonTestInterface p2, out nsIPythonTestInterface p3);
00136         return p2, p1, self
00137     def do_nsISupports(self, p1, p2):
00138         # nsISupports              do_nsISupports(in nsISupports p1, inout nsISupports p2, out nsISupports p3);
00139         return self, p1, p2
00140     def do_nsISupportsIs(self, iid):
00141         # void                     do_nsISupportsIs(in nsIIDRef iid, [iid_is(iid),retval] out nsQIResult result)
00142         # Note the framework does the QI etc on us, so there is no real point me doing it.
00143         # (However, user code _should_ do the QI - otherwise any errors are deemed "internal" (as they
00144         # are raised by the C++ framework), and therefore logged to the console, etc.
00145         # A user QI allows the user to fail gracefully, whatever gracefully means for them!
00146         return self
00147 # Do I really need these??
00148 ##    def do_nsISupportsIs2(self, iid, interface):
00149 ##        # void                     do_nsISupportsIs2(inout nsIIDRef iid, [iid_is(iid),retval] inout nsQIResult result);
00150 ##        return iid, interface
00151 ##    def do_nsISupportsIs3(self, interface):
00152 ##        # void                     do_nsISupportsIs3(out nsIIDRef iid, [iid_is(iid)] inout nsQIResult result);
00153 ##        return self._com_interfaces_, interface
00154 ##    def do_nsISupportsIs4(self):
00155 ##        # void                     do_nsISupportsIs4(out nsIIDRef iid, [iid_is(iid)] out nsQIResult result);
00156 ##        return self._com_interfaces_, self
00157 
00158     # Methods from the nsIPythonTestInterfaceExtra interface
00159     #
00160     def MultiplyEachItemInIntegerArray(self, val, valueArray):
00161         # void MultiplyEachItemInIntegerArray(
00162         #                       in PRInt32 val, 
00163         #                       in PRUint32 count, 
00164         #                       [array, size_is(count)] inout PRInt32 valueArray);
00165         # NOTE - the "sizeis" params are never passed to or returned from Python!
00166         results = []
00167         for item in valueArray:
00168             results.append(item * val)
00169         return results
00170     def MultiplyEachItemInIntegerArrayAndAppend(self, val, valueArray):
00171         #void MultiplyEachItemInIntegerArrayAndAppend(
00172         #                       in PRInt32 val, 
00173         #                       inout PRUint32 count, 
00174         #                       [array, size_is(count)] inout PRInt32 valueArray);
00175         results = valueArray[:]
00176         for item in valueArray:
00177             results.append(item * val)
00178         return results
00179     def DoubleStringArray(self, valueArray):
00180         # void DoubleStringArray(inout PRUint32 count, 
00181         #                       [array, size_is(count)] inout string valueArray);
00182         results = []
00183         for item in valueArray:
00184             results.append(item * 2)
00185         return results
00186 
00187     def ReverseStringArray(self, valueArray):
00188         # void ReverseStringArray(in PRUint32 count, 
00189         #                    [array, size_is(count)] inout string valueArray);
00190         valueArray.reverse()
00191         return valueArray
00192 
00193     # Note that this method shares a single "size_is" between 2 params!
00194     def CompareStringArrays(self, ar1, ar2):
00195         # void CompareStringArrays([array, size_is(count)] in string arr1,
00196         #              [array, size_is(count)] in string arr2,
00197         #              in unsigned long count,
00198         #             [retval] out short result);
00199         return cmp(ar1, ar2)
00200 
00201     def DoubleString(self, val):
00202         # void DoubleString(inout PRUint32 count, 
00203         #               [size_is(count)] inout string str);
00204         return val * 2
00205     def DoubleString2(self, val):
00206         # void DoubleString2(in PRUint32 in_count, [size_is(in_count)] in string in_str,
00207         #                out PRUint32 out_count, [size_is(out_count)] out string out_str);
00208         return val * 2
00209     def DoubleString3(self, val):
00210         # void DoubleString3(in PRUint32 in_count, [size_is(in_count)] in string in_str,
00211         #                    out PRUint32 out_count, [size_is(out_count), retval] string out_str);
00212         return val * 2
00213     def DoubleString4(self, val):
00214         # void DoubleString4([size_is(count)] in string in_str, inout PRUint32 count, [size_is(count)] out string out_str);
00215         return val * 2
00216     def UpString(self, val):
00217         # // UpString defines the count as only "in" - meaning the result must be the same size
00218         # void UpString(in PRUint32 count, 
00219         #               [size_is(count)] inout string str);
00220         return val.upper()
00221     UpString2 = UpString
00222         # // UpString2 defines count as only "in", and a string as only "out"
00223         #     void UpString2(in PRUint32 count, 
00224         #               [size_is(count)] inout string in_str,
00225         #               [size_is(count)]out string out_str);
00226 
00227     def GetFixedString(self, count):
00228         # void GetFixedString(in PRUint32 count, [size_is(count)out string out_str);
00229         return "A" * count
00230 
00231     # DoubleWideString functions are identical to DoubleString, except use wide chars!
00232     def DoubleWideString(self, val):
00233         return val * 2
00234     def DoubleWideString2(self, val):
00235         return val * 2
00236     def DoubleWideString3(self, val):
00237         return val * 2
00238     def DoubleWideString4(self, val):
00239         return val * 2
00240     def UpWideString(self, val):
00241         return val.upper()
00242     UpWideString2 = UpWideString
00243     def CopyUTF8String(self, v):
00244         return v
00245     def CopyUTF8String2(self, v):
00246         return v.encode("utf8")
00247     # Test we can get an "out" array with an "in" size (and the size is not used anywhere as a size for an in!)
00248     def GetFixedWideString(self, count):
00249         # void GetFixedWideString(in PRUint32 count, [size_is(count)out string out_str);
00250         return u"A" * count
00251 
00252     def GetStrings(self):
00253         # void GetStrings(out PRUint32 count,
00254         #            [retval, array, size_is(count)] out string str);
00255         return "Hello from the Python test component".split()
00256     # Some tests for our special "PRUint8" support.
00257     def UpOctetArray( self, data ):
00258         # void UpOctetArray(inout PRUint32 count,
00259         #                [array, size_is(count)] inout PRUint8 data);
00260         return data.upper()
00261 
00262     def UpOctetArray2( self, data ):
00263         # void UpOctetArray2(inout PRUint32 count,
00264         #                [array, size_is(count)] inout PRUint8 data);
00265         data = data.upper()
00266         # This time we return a list of integers.
00267         return map( ord, data )
00268 
00269     # Arrays of interfaces
00270     def CheckInterfaceArray(self, interfaces):
00271         # void CheckInterfaceArray(in PRUint32 count,
00272         #                          [array, size_is(count)] in nsISupports data,
00273         #                          [retval] out PRBool all_non_null);
00274         ret = 1
00275         for i in interfaces:
00276             if i is None:
00277                 ret = 0
00278                 break
00279         return ret
00280     def CopyInterfaceArray(self, a):
00281         return a
00282     def GetInterfaceArray(self):
00283         # void GetInterfaceArray(out PRUint32 count,
00284         #                          [array, size_is(count)] out nsISupports data);
00285         return self, self, self, None
00286     def ExtendInterfaceArray(self, data):
00287         # void ExtendInterfaceArray(inout PRUint32 count,
00288         #                          [array, size_is(count)] inout nsISupports data);
00289         return data * 2
00290 
00291     # Arrays of IIDs
00292     def CheckIIDArray(self, data):
00293         # void CheckIIDArray(in PRUint32 count,
00294         #                          [array, size_is(count)] in nsIIDRef data,
00295         #                          [retval] out PRBool all_mine);
00296         ret = 1
00297         for i in data:
00298             if i!= self._com_interfaces_ and i != self._reg_clsid_:
00299                 ret = 0
00300                 break
00301         return ret
00302     def GetIIDArray(self):
00303         # void GetIIDArray(out PRUint32 count,
00304         #                         [array, size_is(count)] out nsIIDRef data);
00305         return self._com_interfaces_, self._reg_clsid_
00306     def ExtendIIDArray(self, data):
00307         # void ExtendIIDArray(inout PRUint32 count,
00308         #                      [array, size_is(count)] inout nsIIDRef data);
00309         return data * 2
00310 
00311     # Test our count param can be shared as an "in" param.
00312     def SumArrays(self, array1, array2):
00313         # void SumArrays(in PRUint32 count, [array, size_is(count)]in array1, [array, size_is(count)]in array2, [retval]result);
00314         if len(array1)!=len(array2):
00315             print "SumArrays - not expecting different lengths!"
00316         result = 0
00317         for i in array1:
00318             result = result + i
00319         for i in array2:
00320             result = result+i
00321         return result
00322 
00323     # Test our count param can be shared as an "out" param.
00324     def GetArrays(self):
00325         # void GetArrays(out PRUint32 count, [array, size_is(count)]out array1, [array, size_is(count)]out array2);
00326         return (1,2,3), (4,5,6)
00327     # Test we can get an "out" array with an "in" size
00328     def GetFixedArray(self, size):
00329         # void GetFixedArray(in PRUint32 count, [array, size_is(count)]out PRInt32 array1]);
00330         return 0 * size
00331     
00332     # Test our "in" count param can be shared as one "in", plus one  "out" param.
00333     def CopyArray(self, array1):
00334         # void CopyArray(in PRUint32 count, [array, size_is(count)]in array1, [array, size_is(count)]out array2);
00335         return array1
00336     # Test our "in-out" count param can be shared as one "in", plus one  "out" param.
00337     def CopyAndDoubleArray(self, array):
00338         # void CopyAndDoubleArray(inout PRUint32 count, [array, size_is(count)]in array1, [array, size_is(count)]out array2);
00339         return array + array
00340     # Test our "in-out" count param can be shared as one "in", plus one  "in-out" param.
00341     def AppendArray(self, array1, array2):
00342         # void AppendArray(inout PRUint32 count, [array, size_is(count)]in array1, [array, size_is(count)]inout array2);
00343         rc = array1
00344         if array2 is not None:
00345             rc.extend(array2)
00346         return rc
00347     # Test nsIVariant support
00348     def AppendVariant(self, invar, inresult):
00349         if type(invar)==type([]):
00350             invar_use = invar[0]
00351             for v in invar[1:]:
00352                 invar_use += v
00353         else:
00354             invar_use = invar
00355         if type(inresult)==type([]):
00356             inresult_use = inresult[0]
00357             for v in inresult[1:]:
00358                 inresult_use += v
00359         else:
00360             inresult_use = inresult
00361         if inresult_use is None and invar_use is None:
00362             return None
00363         return inresult_use + invar_use
00364 
00365     def CopyVariant(self, invar):
00366         return invar
00367 
00368     # Some tests for the "new" (Feb-2001) DOMString type.
00369     def GetDOMStringResult( self, length ):
00370         # Result: DOMString &
00371         if length == -1:
00372             return None
00373         return "P" * length
00374     def GetDOMStringOut( self, length ):
00375         # Result: DOMString &
00376         if length == -1:
00377             return None
00378         return "y" * length
00379     def GetDOMStringLength( self, param0 ):
00380         # Result: uint32
00381         # In: param0: DOMString &
00382         if param0 is None: return -1
00383         return len(param0)
00384 
00385     def GetDOMStringRefLength( self, param0 ):
00386         # Result: uint32
00387         # In: param0: DOMString &
00388         if param0 is None: return -1
00389         return len(param0)
00390 
00391     def GetDOMStringPtrLength( self, param0 ):
00392         # Result: uint32
00393         # In: param0: DOMString *
00394         if param0 is None: return -1
00395         return len(param0)
00396 
00397     def ConcatDOMStrings( self, param0, param1 ):
00398         # Result: void - None
00399         # In: param0: DOMString &
00400         # In: param1: DOMString &
00401         # Out: DOMString &
00402         return param0 + param1
00403     def get_domstring_value( self ):
00404         # Result: DOMString &
00405         return self.domstring_value
00406     def set_domstring_value( self, param0 ):
00407         # Result: void - None
00408         # In: param0: DOMString &
00409         self.domstring_value = param0
00410 
00411     def get_domstring_value_ro( self ):
00412         # Result: DOMString &
00413         return self.domstring_value