Home | History | Annotate | Download | only in test
      1 # Windows specific tests
      2 
      3 from ctypes import *
      4 from ctypes.test import is_resource_enabled
      5 import unittest, sys
      6 from test import test_support as support
      7 
      8 import _ctypes_test
      9 
     10 if sys.platform == "win32" and sizeof(c_void_p) == sizeof(c_int):
     11     # Only windows 32-bit has different calling conventions.
     12 
     13     class WindowsTestCase(unittest.TestCase):
     14         def test_callconv_1(self):
     15             # Testing stdcall function
     16 
     17             IsWindow = windll.user32.IsWindow
     18             # ValueError: Procedure probably called with not enough arguments (4 bytes missing)
     19             self.assertRaises(ValueError, IsWindow)
     20 
     21             # This one should succeed...
     22             self.assertEqual(0, IsWindow(0))
     23 
     24             # ValueError: Procedure probably called with too many arguments (8 bytes in excess)
     25             self.assertRaises(ValueError, IsWindow, 0, 0, 0)
     26 
     27         def test_callconv_2(self):
     28             # Calling stdcall function as cdecl
     29 
     30             IsWindow = cdll.user32.IsWindow
     31 
     32             # ValueError: Procedure called with not enough arguments (4 bytes missing)
     33             # or wrong calling convention
     34             self.assertRaises(ValueError, IsWindow, None)
     35 
     36 if sys.platform == "win32":
     37     class FunctionCallTestCase(unittest.TestCase):
     38 
     39         if is_resource_enabled("SEH"):
     40             def test_SEH(self):
     41                 # Call functions with invalid arguments, and make sure
     42                 # that access violations are trapped and raise an
     43                 # exception.
     44                 self.assertRaises(WindowsError, windll.kernel32.GetModuleHandleA, 32)
     45 
     46         def test_noargs(self):
     47             # This is a special case on win32 x64
     48             windll.user32.GetDesktopWindow()
     49 
     50     class TestWintypes(unittest.TestCase):
     51         def test_HWND(self):
     52             from ctypes import wintypes
     53             self.assertEqual(sizeof(wintypes.HWND), sizeof(c_void_p))
     54 
     55         def test_PARAM(self):
     56             from ctypes import wintypes
     57             self.assertEqual(sizeof(wintypes.WPARAM),
     58                                  sizeof(c_void_p))
     59             self.assertEqual(sizeof(wintypes.LPARAM),
     60                                  sizeof(c_void_p))
     61 
     62         def test_COMError(self):
     63             from _ctypes import COMError
     64             if support.HAVE_DOCSTRINGS:
     65                 self.assertEqual(COMError.__doc__,
     66                                  "Raised when a COM method call failed.")
     67 
     68             ex = COMError(-1, "text", ("details",))
     69             self.assertEqual(ex.hresult, -1)
     70             self.assertEqual(ex.text, "text")
     71             self.assertEqual(ex.details, ("details",))
     72 
     73 class Structures(unittest.TestCase):
     74 
     75     def test_struct_by_value(self):
     76         class POINT(Structure):
     77             _fields_ = [("x", c_long),
     78                         ("y", c_long)]
     79 
     80         class RECT(Structure):
     81             _fields_ = [("left", c_long),
     82                         ("top", c_long),
     83                         ("right", c_long),
     84                         ("bottom", c_long)]
     85 
     86         dll = CDLL(_ctypes_test.__file__)
     87 
     88         pt = POINT(10, 10)
     89         rect = RECT(0, 0, 20, 20)
     90         self.assertEqual(1, dll.PointInRect(byref(rect), pt))
     91 
     92 if __name__ == '__main__':
     93     unittest.main()
     94