From 209736097a974bb2eb8c8916251660d4ccf2fda8 Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Mon, 25 Jul 2022 11:41:48 +0100 Subject: [PATCH 1/6] Add regression test for Enable #964. --- kiva/tests/test_quartz.py | 139 ++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 65 deletions(-) diff --git a/kiva/tests/test_quartz.py b/kiva/tests/test_quartz.py index 840fdeebb..35b628d7b 100644 --- a/kiva/tests/test_quartz.py +++ b/kiva/tests/test_quartz.py @@ -8,72 +8,81 @@ # # Thanks for using Enthought open source! import sys +import unittest from kiva.tests._testing import skip_if_not_wx -def test_quartz_importable(): - if sys.platform != "darwin": - from unittest.case import SkipTest - - raise SkipTest("quartz is only built on OS X") - - from kiva.quartz import ABCGI - from kiva.quartz import CTFont - from kiva.quartz import mac_context - del ABCGI - del CTFont - del mac_context - - -@skip_if_not_wx -def test_macport(): - if sys.platform != "darwin": - from unittest.case import SkipTest - - raise SkipTest("macport is only built on OS X") - - import wx - - from kiva.quartz import get_macport - - class SimpleWindow(wx.Frame): - """ - Simple test of get_macport(). - """ - - def __init__(self): - wx.Frame.__init__( - self, - parent=None, - id=-1, - title="foo", - pos=(100, 100), - size=(300, 300), - ) - oldstyle = self.GetWindowStyle() - oldstyle = oldstyle | wx.FULL_REPAINT_ON_RESIZE - self.SetWindowStyle(oldstyle) - self.Show(1) - self.Bind(wx.EVT_PAINT, self.OnPaint) - self.memdc = wx.MemoryDC() - self.bitmap = wx.EmptyBitmap(200, 200) - self.memdc.SelectObject(self.bitmap) - - def OnPaint(self, evt): - dc = wx.PaintDC(self) - print("paintdc.this:", dc.this) - print("paintdc.macport: %x" % get_macport(dc)) - print("memdc.this:", self.memdc.this) - print("memdc.macport: %x" % get_macport(self.memdc)) - - # We're done here - self.Close() - - class MyApp(wx.App): - def OnInit(self): - SimpleWindow() - return 1 - - app = MyApp(False) - app.MainLoop() +class TestQuartz(unittest.TestCase): + def test_quartz_importable(self): + if sys.platform != "darwin": + from unittest.case import SkipTest + + raise SkipTest("quartz is only built on OS X") + + from kiva.quartz import ABCGI + from kiva.quartz import CTFont + from kiva.quartz import mac_context + del ABCGI + del CTFont + del mac_context + + @skip_if_not_wx + def test_macport(self): + if sys.platform != "darwin": + from unittest.case import SkipTest + + raise SkipTest("macport is only built on OS X") + + import wx + + from kiva.quartz import get_macport + + class SimpleWindow(wx.Frame): + """ + Simple test of get_macport(). + """ + + def __init__(self): + wx.Frame.__init__( + self, + parent=None, + id=-1, + title="foo", + pos=(100, 100), + size=(300, 300), + ) + oldstyle = self.GetWindowStyle() + oldstyle = oldstyle | wx.FULL_REPAINT_ON_RESIZE + self.SetWindowStyle(oldstyle) + self.Show(1) + self.Bind(wx.EVT_PAINT, self.OnPaint) + self.memdc = wx.MemoryDC() + self.bitmap = wx.EmptyBitmap(200, 200) + self.memdc.SelectObject(self.bitmap) + + def OnPaint(self, evt): + dc = wx.PaintDC(self) + print("paintdc.this:", dc.this) + print("paintdc.macport: %x" % get_macport(dc)) + print("memdc.this:", self.memdc.this) + print("memdc.macport: %x" % get_macport(self.memdc)) + + # We're done here + self.Close() + + class MyApp(wx.App): + def OnInit(self): + SimpleWindow() + return 1 + + app = MyApp(False) + app.MainLoop() + + def test_font_names(self): + from kiva.quartz.CTFont import default_font_info + + names = default_font_info.names() + + # regression test for Enable#964 + self.assertFalse(any(s.startswith("b'") for s in names)) From fd087da354f887242c4451f2158dd6a72a5021fd Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Mon, 25 Jul 2022 11:43:34 +0100 Subject: [PATCH 2/6] Properly decode strings in Kiva Quartz font code. --- kiva/quartz/CTFont.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kiva/quartz/CTFont.pyx b/kiva/quartz/CTFont.pyx index 50fe0f6de..9cfd1b6cc 100644 --- a/kiva/quartz/CTFont.pyx +++ b/kiva/quartz/CTFont.pyx @@ -21,10 +21,10 @@ cdef object _cf_string_to_pystring(CFStringRef cf_string): if c_string == NULL: success = CFStringGetCString(cf_string, buf, 256, kCFStringEncodingMacRoman) - retval = str(buf) + retval = bytes(buf) else: - retval = str(c_string) - return retval + retval = bytes(c_string) + return retval.decode('mac-roman') cdef CFArrayRef _get_system_fonts(): cdef CFIndex value = 1 From 1b72020e1d7cdf52c701efdd42c69a0a1e0091a9 Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Mon, 25 Jul 2022 11:53:54 +0100 Subject: [PATCH 3/6] Properly guard against running Quartz tests on non-Mac systems. --- kiva/tests/_testing.py | 9 ++++++++- kiva/tests/test_quartz.py | 8 ++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/kiva/tests/_testing.py b/kiva/tests/_testing.py index 285677032..e350cd8ca 100644 --- a/kiva/tests/_testing.py +++ b/kiva/tests/_testing.py @@ -12,6 +12,7 @@ This is not a public module and should not to be used outside of Kiva. """ +import sys import unittest from traits.etsconfig.api import ETSConfig @@ -21,5 +22,11 @@ def is_wx(): """ Return true if the toolkit backend is wx. """ return ETSConfig.toolkit == "wx" - skip_if_not_wx = unittest.skipIf(not is_wx(), "Test only for wx") + + +def is_mac(): + return sys.platform == 'darwin' + + +skip_unless_mac = unittest.skipUnless(is_mac(), "Test only on Mac OS") diff --git a/kiva/tests/test_quartz.py b/kiva/tests/test_quartz.py index 35b628d7b..906d90211 100644 --- a/kiva/tests/test_quartz.py +++ b/kiva/tests/test_quartz.py @@ -10,16 +10,12 @@ import sys import unittest -from kiva.tests._testing import skip_if_not_wx +from kiva.tests._testing import skip_if_not_wx, skip_unless_mac +@skip_unless_mac class TestQuartz(unittest.TestCase): def test_quartz_importable(self): - if sys.platform != "darwin": - from unittest.case import SkipTest - - raise SkipTest("quartz is only built on OS X") - from kiva.quartz import ABCGI from kiva.quartz import CTFont from kiva.quartz import mac_context From 15134fcd1f3598eb75a34f4ccc8e21d8c9ffbca0 Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Mon, 25 Jul 2022 12:21:51 +0100 Subject: [PATCH 4/6] Ensure Wx Quartz test terminates. --- kiva/tests/test_quartz.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kiva/tests/test_quartz.py b/kiva/tests/test_quartz.py index 906d90211..4c1fcccb7 100644 --- a/kiva/tests/test_quartz.py +++ b/kiva/tests/test_quartz.py @@ -59,10 +59,13 @@ def __init__(self): def OnPaint(self, evt): dc = wx.PaintDC(self) - print("paintdc.this:", dc.this) - print("paintdc.macport: %x" % get_macport(dc)) - print("memdc.this:", self.memdc.this) - print("memdc.macport: %x" % get_macport(self.memdc)) + try: + print("paintdc.macport: %x" % get_macport(dc)) + print("memdc.macport: %x" % get_macport(self.memdc)) + self.success = True + except Exception as exc: + print(repr(exc)) + self.success = False # We're done here self.Close() @@ -75,6 +78,8 @@ def OnInit(self): app = MyApp(False) app.MainLoop() + self.assertTrue(app.success) + def test_font_names(self): from kiva.quartz.CTFont import default_font_info From 26fdeb93864a516b31281d37f5841fa4a8176ec4 Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Mon, 25 Jul 2022 12:43:18 +0100 Subject: [PATCH 5/6] Make test success externally visible. --- kiva/tests/test_quartz.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kiva/tests/test_quartz.py b/kiva/tests/test_quartz.py index 4c1fcccb7..cb67e41d2 100644 --- a/kiva/tests/test_quartz.py +++ b/kiva/tests/test_quartz.py @@ -34,6 +34,8 @@ def test_macport(self): from kiva.quartz import get_macport + success = [] + class SimpleWindow(wx.Frame): """ Simple test of get_macport(). @@ -62,10 +64,10 @@ def OnPaint(self, evt): try: print("paintdc.macport: %x" % get_macport(dc)) print("memdc.macport: %x" % get_macport(self.memdc)) - self.success = True + success.append(True) except Exception as exc: print(repr(exc)) - self.success = False + success.append(False) # We're done here self.Close() @@ -78,7 +80,8 @@ def OnInit(self): app = MyApp(False) app.MainLoop() - self.assertTrue(app.success) + self.assertEqual(len(success), 1) + self.assertTrue(success[0]) def test_font_names(self): from kiva.quartz.CTFont import default_font_info From e4f532b19509f84aa77e928600bed86a7b263435 Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Mon, 25 Jul 2022 13:57:56 +0100 Subject: [PATCH 6/6] Fix tests, use UTF-8. --- kiva/quartz/CTFont.pyx | 6 +++--- kiva/tests/test_quartz.py | 5 ----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/kiva/quartz/CTFont.pyx b/kiva/quartz/CTFont.pyx index 9cfd1b6cc..f484ac071 100644 --- a/kiva/quartz/CTFont.pyx +++ b/kiva/quartz/CTFont.pyx @@ -17,14 +17,14 @@ include "CoreText.pxi" cdef object _cf_string_to_pystring(CFStringRef cf_string): cdef char* c_string cdef char buf[256] - c_string = CFStringGetCStringPtr(cf_string, kCFStringEncodingMacRoman) + c_string = CFStringGetCStringPtr(cf_string, kCFStringEncodingUTF8) if c_string == NULL: success = CFStringGetCString(cf_string, buf, 256, - kCFStringEncodingMacRoman) + kCFStringEncodingUTF8) retval = bytes(buf) else: retval = bytes(c_string) - return retval.decode('mac-roman') + return retval.decode('utf-8') cdef CFArrayRef _get_system_fonts(): cdef CFIndex value = 1 diff --git a/kiva/tests/test_quartz.py b/kiva/tests/test_quartz.py index cb67e41d2..b87250a83 100644 --- a/kiva/tests/test_quartz.py +++ b/kiva/tests/test_quartz.py @@ -25,11 +25,6 @@ def test_quartz_importable(self): @skip_if_not_wx def test_macport(self): - if sys.platform != "darwin": - from unittest.case import SkipTest - - raise SkipTest("macport is only built on OS X") - import wx from kiva.quartz import get_macport