Back to index

lightning-sunbird  0.9+nobinonly
test_components.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 """Tests the "xpcom.components" object.
00039 """
00040 
00041 import xpcom.components
00042 
00043 if not __debug__:
00044     raise RuntimeError, "This test uses assert, so must be run in debug mode"
00045 
00046 def test_interfaces():
00047     "Test the xpcom.components.interfaces object"
00048 
00049     iid = xpcom.components.interfaces.nsISupports
00050     assert iid == xpcom._xpcom.IID_nsISupports, "Got the wrong IID!"
00051     iid = xpcom.components.interfaces['nsISupports']
00052     assert iid == xpcom._xpcom.IID_nsISupports, "Got the wrong IID!"
00053     
00054     # Test dictionary semantics
00055     num_fetched = num_nsisupports = 0
00056     for name, iid in xpcom.components.interfaces.items():
00057         num_fetched = num_fetched + 1
00058         if name == "nsISupports":
00059             num_nsisupports = num_nsisupports + 1
00060             assert iid == xpcom._xpcom.IID_nsISupports, "Got the wrong IID!"
00061         assert xpcom.components.interfaces[name] == iid
00062     # Check all the lengths match.
00063     assert len(xpcom.components.interfaces.keys()) == len(xpcom.components.interfaces.values()) == \
00064                len(xpcom.components.interfaces.items()) == len(xpcom.components.interfaces) == \
00065                num_fetched, "The collection lengths were wrong"
00066     if num_nsisupports != 1:
00067         print "Didnt find exactly 1 nsiSupports!"
00068     print "The interfaces object appeared to work!"
00069 
00070 def test_classes():
00071     # Need a well-known contractID here?
00072     prog_id = "@mozilla.org/supports-array;1"
00073     clsid = xpcom.components.ID("{bda17d50-0d6b-11d3-9331-00104ba0fd40}")
00074 
00075     # Check we can create the instance (dont check we can do anything with it tho!)
00076     klass = xpcom.components.classes[prog_id]
00077     instance = klass.createInstance()
00078     
00079     # Test dictionary semantics
00080     num_fetched = num_mine = 0
00081     for name, klass in xpcom.components.classes.items():
00082         num_fetched = num_fetched + 1
00083         if name == prog_id:
00084             if klass.clsid != clsid:
00085                 print "Eeek - didn't get the correct IID - got", klass.clsid
00086             num_mine = num_mine + 1
00087 
00088 # xpcom appears to add charset info to the contractid!?         
00089 #        assert xpcom.components.classes[name].contractid == prog_id, "Expected '%s', got '%s'" % (prog_id, xpcom.components.classes[name].contractid)
00090     # Check all the lengths match.
00091     if len(xpcom.components.classes.keys()) == len(xpcom.components.classes.values()) == \
00092                len(xpcom.components.classes.items()) == len(xpcom.components.classes) == \
00093                num_fetched:
00094         pass
00095     else:
00096         raise RuntimeError, "The collection lengths were wrong"
00097     if num_fetched <= 0:
00098         raise RuntimeError, "Didnt get any classes!!!"
00099     if num_mine != 1:
00100         raise RuntimeError, "Didnt find exactly 1 of my contractid! (%d)" % (num_mine,)
00101     print "The classes object appeared to work!"
00102     
00103 def test_id():
00104     id = xpcom.components.ID(str(xpcom._xpcom.IID_nsISupports))
00105     assert id == xpcom._xpcom.IID_nsISupports
00106     print "The ID function appeared to work!"
00107     
00108 
00109 # regrtest doesnt like if __name__=='__main__' blocks - it fails when running as a test!
00110 test_interfaces()
00111 test_classes()
00112 test_id()