home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / components / py_test_component.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  17.6 KB  |  414 lines

  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, 2001
  19. # the Initial Developer. All Rights Reserved.
  20. #
  21. # Contributor(s):
  22. #       Mark Hammond <MarkH@ActiveState.com> (original author)  
  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.  
  38. # NOTE: This is a TEST interface, not a DEMO interface :-)
  39. # We try to get as many data-types etc exposed, meaning this
  40. # doesnt really make a good demo of a "simple component"
  41.  
  42.  
  43. from xpcom import components, verbose
  44.  
  45. class PythonTestComponent:
  46.     # Note we only list the "child" interface, not our intermediate interfaces
  47.     # (which we must, by definition, also support)
  48.     _com_interfaces_ = components.interfaces.nsIPythonTestInterfaceDOMStrings
  49.     _reg_clsid_ = "{7EE4BDC6-CB53-42c1-A9E4-616B8E012ABA}"
  50.     _reg_contractid_ = "Python.TestComponent"
  51.     def __init__(self):
  52.         self.boolean_value = 1
  53.         self.octet_value = 2
  54.         self.short_value = 3
  55.         self.ushort_value = 4
  56.         self.long_value = 5
  57.         self.ulong_value = 6
  58.         self.long_long_value = 7
  59.         self.ulong_long_value = 8
  60.         self.float_value = 9.0
  61.         self.double_value = 10.0
  62.         self.char_value = "a"
  63.         self.wchar_value = "b"
  64.         self.string_value = "cee"
  65.         self.wstring_value = "dee"
  66.         self.astring_value = "astring"
  67.         self.acstring_value = "acstring"
  68.         self.utf8string_value = "utf8string"
  69.         self.iid_value = self._reg_clsid_
  70.         self.interface_value = None
  71.         self.isupports_value = None
  72.         self.domstring_value = "dom"
  73.  
  74.     def __del__(self):
  75.         if verbose:
  76.             print "Python.TestComponent: __del__ method called - object is destructing"
  77.  
  78.     def do_boolean(self, p1, p2):
  79.         # boolean                  do_boolean(in boolean p1, inout boolean p2, out boolean p3);
  80.         ret = p1 ^ p2
  81.         return ret, not ret, ret
  82.  
  83.     def do_octet(self, p1, p2):
  84.         # octet                    do_octet(in octet p1, inout octet p2, out octet p3);
  85.         return p1+p2, p1-p2, p1*p2
  86.  
  87.     def do_short(self, p1, p2):
  88.         # short                    do_short(in short p1, inout short p2, out short p3);
  89.         return p1+p2, p1-p2, p1*p2
  90.  
  91.     def do_unsigned_short(self, p1, p2):
  92.         # unsigned short           do_unsigned_short(in unsigned short p1, inout unsigned short p2, out unsigned short p3);
  93.         return p1+p2, p1-p2, p1*p2
  94.     def do_long(self, p1, p2):
  95.         # long                     do_long(in long p1, inout long p2, out long p3);
  96.         return p1+p2, p1-p2, p1*p2
  97.  
  98.     def do_unsigned_long(self, p1, p2):
  99.         # unsigned long            do_unsigned_long(in unsigned long p1, inout unsigned long p2, out unsigned long p3);
  100.         return p1+p2, p1-p2, p1*p2
  101.     def do_long_long(self, p1, p2):
  102.         #  long long                do_long_long(in long long p1, inout long long p2, out long long p3);
  103.         return p1+p2, p1-p2, p1*p2
  104.     def do_unsigned_long_long(self, p1, p2):
  105.         # unsigned long long       do_unsigned_long_long(in unsigned long long p1, inout unsigned long long p2, out unsigned long long p3);
  106.         return p1+p2, p1-p2, p1*p2
  107.     def do_float(self, p1, p2):
  108.         # float                    do_float(in float p1, inout float p2, out float p3);
  109.         return p1+p2, p1-p2, p1*p2
  110.     def do_double(self, p1, p2):
  111.         # double                   do_double(in double p1, inout double p2, out double p3);
  112.         return p1+p2, p1-p2, p1*p2
  113.     def do_char(self, p1, p2):
  114.         # char                     do_char(in char p1, inout char p2, out char p3);
  115.         return chr(ord(p1)+ord(p2)), p2, p1
  116.     def do_wchar(self, p1, p2):
  117.         # wchar                    do_wchar(in wchar p1, inout wchar p2, out wchar p3);
  118.         return chr(ord(p1)+ord(p2)), p2, p1
  119.     def do_string(self, p1, p2):
  120.         # string                   do_string(in string p1, inout string p2, out string p3);
  121.         ret = ""
  122.         if p1 is not None: ret = ret + p1
  123.         if p2 is not None: ret = ret + p2
  124.         return ret, p1, p2
  125.     def do_wstring(self, p1, p2):
  126.         # wstring                  do_wstring(in wstring p1, inout wstring p2, out wstring p3);
  127.         ret = u""
  128.         if p1 is not None: ret = ret + p1
  129.         if p2 is not None: ret = ret + p2
  130.         return ret, p1, p2
  131.     def do_nsIIDRef(self, p1, p2):
  132.         # nsIIDRef                 do_nsIIDRef(in nsIIDRef p1, inout nsIIDRef p2, out nsIIDRef p3);
  133.         return p1, self._reg_clsid_, p2
  134.     def do_nsIPythonTestInterface(self, p1, p2):
  135.         # nsIPythonTestInterface   do_nsIPythonTestInterface(in nsIPythonTestInterface p1, inout nsIPythonTestInterface p2, out nsIPythonTestInterface p3);
  136.         return p2, p1, self
  137.     def do_nsISupports(self, p1, p2):
  138.         # nsISupports              do_nsISupports(in nsISupports p1, inout nsISupports p2, out nsISupports p3);
  139.         return self, p1, p2
  140.     def do_nsISupportsIs(self, iid):
  141.         # void                     do_nsISupportsIs(in nsIIDRef iid, [iid_is(iid),retval] out nsQIResult result)
  142.         # Note the framework does the QI etc on us, so there is no real point me doing it.
  143.         # (However, user code _should_ do the QI - otherwise any errors are deemed "internal" (as they
  144.         # are raised by the C++ framework), and therefore logged to the console, etc.
  145.         # A user QI allows the user to fail gracefully, whatever gracefully means for them!
  146.         return self
  147. # Do I really need these??
  148. ##    def do_nsISupportsIs2(self, iid, interface):
  149. ##        # void                     do_nsISupportsIs2(inout nsIIDRef iid, [iid_is(iid),retval] inout nsQIResult result);
  150. ##        return iid, interface
  151. ##    def do_nsISupportsIs3(self, interface):
  152. ##        # void                     do_nsISupportsIs3(out nsIIDRef iid, [iid_is(iid)] inout nsQIResult result);
  153. ##        return self._com_interfaces_, interface
  154. ##    def do_nsISupportsIs4(self):
  155. ##        # void                     do_nsISupportsIs4(out nsIIDRef iid, [iid_is(iid)] out nsQIResult result);
  156. ##        return self._com_interfaces_, self
  157.  
  158.     # Methods from the nsIPythonTestInterfaceExtra interface
  159.     #
  160.     def MultiplyEachItemInIntegerArray(self, val, valueArray):
  161.         # void MultiplyEachItemInIntegerArray(
  162.         #                       in PRInt32 val, 
  163.         #                       in PRUint32 count, 
  164.         #                       [array, size_is(count)] inout PRInt32 valueArray);
  165.         # NOTE - the "sizeis" params are never passed to or returned from Python!
  166.         results = []
  167.         for item in valueArray:
  168.             results.append(item * val)
  169.         return results
  170.     def MultiplyEachItemInIntegerArrayAndAppend(self, val, valueArray):
  171.         #void MultiplyEachItemInIntegerArrayAndAppend(
  172.         #                       in PRInt32 val, 
  173.         #                       inout PRUint32 count, 
  174.         #                       [array, size_is(count)] inout PRInt32 valueArray);
  175.         results = valueArray[:]
  176.         for item in valueArray:
  177.             results.append(item * val)
  178.         return results
  179.     def DoubleStringArray(self, valueArray):
  180.         # void DoubleStringArray(inout PRUint32 count, 
  181.         #                       [array, size_is(count)] inout string valueArray);
  182.         results = []
  183.         for item in valueArray:
  184.             results.append(item * 2)
  185.         return results
  186.  
  187.     def ReverseStringArray(self, valueArray):
  188.         # void ReverseStringArray(in PRUint32 count, 
  189.         #                    [array, size_is(count)] inout string valueArray);
  190.         valueArray.reverse()
  191.         return valueArray
  192.  
  193.     # Note that this method shares a single "size_is" between 2 params!
  194.     def CompareStringArrays(self, ar1, ar2):
  195.         # void CompareStringArrays([array, size_is(count)] in string arr1,
  196.         #              [array, size_is(count)] in string arr2,
  197.         #              in unsigned long count,
  198.         #             [retval] out short result);
  199.         return cmp(ar1, ar2)
  200.  
  201.     def DoubleString(self, val):
  202.         # void DoubleString(inout PRUint32 count, 
  203.         #               [size_is(count)] inout string str);
  204.         return val * 2
  205.     def DoubleString2(self, val):
  206.         # void DoubleString2(in PRUint32 in_count, [size_is(in_count)] in string in_str,
  207.         #                out PRUint32 out_count, [size_is(out_count)] out string out_str);
  208.         return val * 2
  209.     def DoubleString3(self, val):
  210.         # void DoubleString3(in PRUint32 in_count, [size_is(in_count)] in string in_str,
  211.         #                    out PRUint32 out_count, [size_is(out_count), retval] string out_str);
  212.         return val * 2
  213.     def DoubleString4(self, val):
  214.         # void DoubleString4([size_is(count)] in string in_str, inout PRUint32 count, [size_is(count)] out string out_str);
  215.         return val * 2
  216.     def UpString(self, val):
  217.         # // UpString defines the count as only "in" - meaning the result must be the same size
  218.         # void UpString(in PRUint32 count, 
  219.         #               [size_is(count)] inout string str);
  220.         return val.upper()
  221.     UpString2 = UpString
  222.         # // UpString2 defines count as only "in", and a string as only "out"
  223.         #     void UpString2(in PRUint32 count, 
  224.         #               [size_is(count)] inout string in_str,
  225.         #               [size_is(count)]out string out_str);
  226.  
  227.     def GetFixedString(self, count):
  228.         # void GetFixedString(in PRUint32 count, [size_is(count)out string out_str);
  229.         return "A" * count
  230.  
  231.     # DoubleWideString functions are identical to DoubleString, except use wide chars!
  232.     def DoubleWideString(self, val):
  233.         return val * 2
  234.     def DoubleWideString2(self, val):
  235.         return val * 2
  236.     def DoubleWideString3(self, val):
  237.         return val * 2
  238.     def DoubleWideString4(self, val):
  239.         return val * 2
  240.     def UpWideString(self, val):
  241.         return val.upper()
  242.     UpWideString2 = UpWideString
  243.     def CopyUTF8String(self, v):
  244.         return v
  245.     def CopyUTF8String2(self, v):
  246.         return v.encode("utf8")
  247.     # Test we can get an "out" array with an "in" size (and the size is not used anywhere as a size for an in!)
  248.     def GetFixedWideString(self, count):
  249.         # void GetFixedWideString(in PRUint32 count, [size_is(count)out string out_str);
  250.         return u"A" * count
  251.  
  252.     def GetStrings(self):
  253.         # void GetStrings(out PRUint32 count,
  254.         #            [retval, array, size_is(count)] out string str);
  255.         return "Hello from the Python test component".split()
  256.     # Some tests for our special "PRUint8" support.
  257.     def UpOctetArray( self, data ):
  258.         # void UpOctetArray(inout PRUint32 count,
  259.         #                [array, size_is(count)] inout PRUint8 data);
  260.         return data.upper()
  261.  
  262.     def UpOctetArray2( self, data ):
  263.         # void UpOctetArray2(inout PRUint32 count,
  264.         #                [array, size_is(count)] inout PRUint8 data);
  265.         data = data.upper()
  266.         # This time we return a list of integers.
  267.         return map( ord, data )
  268.  
  269.     # Arrays of interfaces
  270.     def CheckInterfaceArray(self, interfaces):
  271.         # void CheckInterfaceArray(in PRUint32 count,
  272.         #                          [array, size_is(count)] in nsISupports data,
  273.         #                          [retval] out PRBool all_non_null);
  274.         ret = 1
  275.         for i in interfaces:
  276.             if i is None:
  277.                 ret = 0
  278.                 break
  279.         return ret
  280.     def CopyInterfaceArray(self, a):
  281.         return a
  282.     def GetInterfaceArray(self):
  283.         # void GetInterfaceArray(out PRUint32 count,
  284.         #                          [array, size_is(count)] out nsISupports data);
  285.         return self, self, self, None
  286.     def ExtendInterfaceArray(self, data):
  287.         # void ExtendInterfaceArray(inout PRUint32 count,
  288.         #                          [array, size_is(count)] inout nsISupports data);
  289.         return data * 2
  290.  
  291.     # Arrays of IIDs
  292.     def CheckIIDArray(self, data):
  293.         # void CheckIIDArray(in PRUint32 count,
  294.         #                          [array, size_is(count)] in nsIIDRef data,
  295.         #                          [retval] out PRBool all_mine);
  296.         ret = 1
  297.         for i in data:
  298.             if i!= self._com_interfaces_ and i != self._reg_clsid_:
  299.                 ret = 0
  300.                 break
  301.         return ret
  302.     def GetIIDArray(self):
  303.         # void GetIIDArray(out PRUint32 count,
  304.         #                         [array, size_is(count)] out nsIIDRef data);
  305.         return self._com_interfaces_, self._reg_clsid_
  306.     def ExtendIIDArray(self, data):
  307.         # void ExtendIIDArray(inout PRUint32 count,
  308.         #                      [array, size_is(count)] inout nsIIDRef data);
  309.         return data * 2
  310.  
  311.     # Test our count param can be shared as an "in" param.
  312.     def SumArrays(self, array1, array2):
  313.         # void SumArrays(in PRUint32 count, [array, size_is(count)]in array1, [array, size_is(count)]in array2, [retval]result);
  314.         if len(array1)!=len(array2):
  315.             print "SumArrays - not expecting different lengths!"
  316.         result = 0
  317.         for i in array1:
  318.             result = result + i
  319.         for i in array2:
  320.             result = result+i
  321.         return result
  322.  
  323.     # Test our count param can be shared as an "out" param.
  324.     def GetArrays(self):
  325.         # void GetArrays(out PRUint32 count, [array, size_is(count)]out array1, [array, size_is(count)]out array2);
  326.         return (1,2,3), (4,5,6)
  327.     # Test we can get an "out" array with an "in" size
  328.     def GetFixedArray(self, size):
  329.         # void GetFixedArray(in PRUint32 count, [array, size_is(count)]out PRInt32 array1]);
  330.         return 0 * size
  331.     
  332.     # Test our "in" count param can be shared as one "in", plus one  "out" param.
  333.     def CopyArray(self, array1):
  334.         # void CopyArray(in PRUint32 count, [array, size_is(count)]in array1, [array, size_is(count)]out array2);
  335.         return array1
  336.     # Test our "in-out" count param can be shared as one "in", plus one  "out" param.
  337.     def CopyAndDoubleArray(self, array):
  338.         # void CopyAndDoubleArray(inout PRUint32 count, [array, size_is(count)]in array1, [array, size_is(count)]out array2);
  339.         return array + array
  340.     # Test our "in-out" count param can be shared as one "in", plus one  "in-out" param.
  341.     def AppendArray(self, array1, array2):
  342.         # void AppendArray(inout PRUint32 count, [array, size_is(count)]in array1, [array, size_is(count)]inout array2);
  343.         rc = array1
  344.         if array2 is not None:
  345.             rc.extend(array2)
  346.         return rc
  347.     # Test nsIVariant support
  348.     def AppendVariant(self, invar, inresult):
  349.         if type(invar)==type([]):
  350.             invar_use = invar[0]
  351.             for v in invar[1:]:
  352.                 invar_use += v
  353.         else:
  354.             invar_use = invar
  355.         if type(inresult)==type([]):
  356.             inresult_use = inresult[0]
  357.             for v in inresult[1:]:
  358.                 inresult_use += v
  359.         else:
  360.             inresult_use = inresult
  361.         if inresult_use is None and invar_use is None:
  362.             return None
  363.         return inresult_use + invar_use
  364.  
  365.     def CopyVariant(self, invar):
  366.         return invar
  367.  
  368.     # Some tests for the "new" (Feb-2001) DOMString type.
  369.     def GetDOMStringResult( self, length ):
  370.         # Result: DOMString &
  371.         if length == -1:
  372.             return None
  373.         return "P" * length
  374.     def GetDOMStringOut( self, length ):
  375.         # Result: DOMString &
  376.         if length == -1:
  377.             return None
  378.         return "y" * length
  379.     def GetDOMStringLength( self, param0 ):
  380.         # Result: uint32
  381.         # In: param0: DOMString &
  382.         if param0 is None: return -1
  383.         return len(param0)
  384.  
  385.     def GetDOMStringRefLength( self, param0 ):
  386.         # Result: uint32
  387.         # In: param0: DOMString &
  388.         if param0 is None: return -1
  389.         return len(param0)
  390.  
  391.     def GetDOMStringPtrLength( self, param0 ):
  392.         # Result: uint32
  393.         # In: param0: DOMString *
  394.         if param0 is None: return -1
  395.         return len(param0)
  396.  
  397.     def ConcatDOMStrings( self, param0, param1 ):
  398.         # Result: void - None
  399.         # In: param0: DOMString &
  400.         # In: param1: DOMString &
  401.         # Out: DOMString &
  402.         return param0 + param1
  403.     def get_domstring_value( self ):
  404.         # Result: DOMString &
  405.         return self.domstring_value
  406.     def set_domstring_value( self, param0 ):
  407.         # Result: void - None
  408.         # In: param0: DOMString &
  409.         self.domstring_value = param0
  410.  
  411.     def get_domstring_value_ro( self ):
  412.         # Result: DOMString &
  413.         return self.domstring_value
  414.