test_clipboard.py :  » Windows » pyExcelerator » pywin32-214 » win32 » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » win32 » test » test_clipboard.py
# General test module for win32api - please add some :)
import sys, os
import unittest

from win32clipboard import *
import win32gui, win32con
import pywintypes
import array

from pywin32_testutil import str2bytes

custom_format_name = "PythonClipboardTestFormat"

class CrashingTestCase(unittest.TestCase):
    def test_722082(self):
        class crasher(object):
            pass

        obj = crasher()
        OpenClipboard()
        try:
            EmptyClipboard()
            # This used to crash - now correctly raises type error.
            self.assertRaises(TypeError, SetClipboardData, 0, obj )
        finally:
            CloseClipboard()

class TestBitmap(unittest.TestCase):
    def setUp(self):
        self.bmp_handle = None
        try:
            this_file = __file__
        except NameError:
            this_file = sys.argv[0]
        this_dir = os.path.dirname(this_file)
        self.bmp_name = os.path.join(os.path.abspath(this_dir),
                                     "..", "Demos", "images", "smiley.bmp")
        self.failUnless(os.path.isfile(self.bmp_name), self.bmp_name)
        flags = win32con.LR_DEFAULTSIZE | win32con.LR_LOADFROMFILE
        self.bmp_handle = win32gui.LoadImage(0, self.bmp_name,
                                             win32con.IMAGE_BITMAP,
                                             0, 0, flags)
        self.failUnless(self.bmp_handle, "Failed to get a bitmap handle")

    def tearDown(self):
        if self.bmp_handle:
            win32gui.DeleteObject(self.bmp_handle)

    def test_bitmap_roundtrip(self):
        OpenClipboard()
        try:
            SetClipboardData(win32con.CF_BITMAP, self.bmp_handle)
            got_handle = GetClipboardDataHandle(win32con.CF_BITMAP)
            self.failUnlessEqual(got_handle, self.bmp_handle)
        finally:
            CloseClipboard()

class TestStrings(unittest.TestCase):
    def setUp(self):
        OpenClipboard()
    def tearDown(self):
        CloseClipboard()
    def test_unicode(self):
        val = u"test-\a9har"
        SetClipboardData(win32con.CF_UNICODETEXT, val)
        self.failUnlessEqual(GetClipboardData(win32con.CF_UNICODETEXT), val)
    def test_unicode_text(self):
        val = "test-val"
        SetClipboardText(val)
        # GetClipboardData doesn't to auto string conversions - so on py3k,
        # CF_TEXT returns bytes.
        expected = str2bytes(val)
        self.failUnlessEqual(GetClipboardData(win32con.CF_TEXT), expected)
        SetClipboardText(val, win32con.CF_UNICODETEXT)
        self.failUnlessEqual(GetClipboardData(win32con.CF_UNICODETEXT), val)
    def test_string(self):
        val = str2bytes("test")
        SetClipboardData(win32con.CF_TEXT, val)
        self.failUnlessEqual(GetClipboardData(win32con.CF_TEXT), val)

class TestGlobalMemory(unittest.TestCase):
    def setUp(self):
        OpenClipboard()
    def tearDown(self):
        CloseClipboard()
    def test_mem(self):
        val = str2bytes("test")
        expected = str2bytes("test\0")
        SetClipboardData(win32con.CF_TEXT, val)
        # Get the raw data - this will include the '\0'
        raw_data = GetGlobalMemory(GetClipboardDataHandle(win32con.CF_TEXT))
        self.failUnlessEqual(expected, raw_data)
    def test_bad_mem(self):
        self.failUnlessRaises(pywintypes.error, GetGlobalMemory, 0)
        self.failUnlessRaises(pywintypes.error, GetGlobalMemory, -1)
        if sys.getwindowsversion()[0] <= 5:
            # For some reason, the value '1' dies from a 64bit process, but
            # "works" (ie, gives the correct exception) from a 32bit process.
            # just silently skip this value on Vista.
            self.failUnlessRaises(pywintypes.error, GetGlobalMemory, 1)
    def test_custom_mem(self):
        test_data = str2bytes("hello\x00\xff")
        test_buffer = array.array("b", test_data)
        cf = RegisterClipboardFormat(custom_format_name)
        self.failUnlessEqual(custom_format_name, GetClipboardFormatName(cf))
        SetClipboardData(cf, test_buffer)
        hglobal = GetClipboardDataHandle(cf)
        data = GetGlobalMemory(hglobal)
        self.failUnlessEqual(data, test_data)

if __name__ == '__main__':
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.