glfw.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
  3. import sys
  4. import unittest
  5. from . import BaseTest
  6. _plat = sys.platform.lower()
  7. is_macos = 'darwin' in _plat
  8. class TestGLFW(BaseTest):
  9. def test_os_window_size_calculation(self):
  10. from kitty.utils import get_new_os_window_size
  11. def t(w, h, width=0, height=0, unit='cells', incremental=False):
  12. self.ae((w, h), get_new_os_window_size(metrics, width, height, unit, incremental, has_window_scaling))
  13. with self.subTest(has_window_scaling=False):
  14. has_window_scaling = False
  15. metrics = {
  16. 'width': 200, 'height': 100,
  17. 'framebuffer_width': 200, 'framebuffer_height': 100,
  18. 'xscale': 2.0, 'yscale': 2.0, 'xdpi': 192.0, 'ydpi': 192.0,
  19. 'cell_width': 8, 'cell_height': 16
  20. }
  21. t(80 * metrics['cell_width'], 100, 80)
  22. t(80 * metrics['cell_width'] + metrics['width'], 100, 80, incremental=True)
  23. t(1217, 100, 1217, unit='pixels')
  24. t(1217 + metrics['width'], 100, 1217, unit='pixels', incremental=True)
  25. with self.subTest(has_window_scaling=True):
  26. has_window_scaling = True
  27. metrics['framebuffer_width'] = metrics['width'] * 2
  28. metrics['framebuffer_height'] = metrics['height'] * 2
  29. t(80 * metrics['cell_width'] / metrics['xscale'], 100, 80)
  30. t(80 * metrics['cell_width'] / metrics['xscale'] + metrics['width'], 100, 80, incremental=True)
  31. t(1217, 100, 1217, unit='pixels')
  32. t(1217 + metrics['width'], 100, 1217, unit='pixels', incremental=True)
  33. @unittest.skipIf(is_macos, 'Skipping test on macOS because glfw-cocoa.so is not built with backend_utils')
  34. def test_utf_8_strndup(self):
  35. import ctypes
  36. from kitty.constants import glfw_path
  37. backend_utils = glfw_path('x11')
  38. lib = ctypes.CDLL(backend_utils)
  39. libc = ctypes.CDLL(None)
  40. class allocated_c_char_p(ctypes.c_char_p):
  41. def __del__(self):
  42. libc.free(self)
  43. utf_8_strndup = lib.utf_8_strndup
  44. utf_8_strndup.restype = allocated_c_char_p
  45. utf_8_strndup.argtypes = (ctypes.c_char_p, ctypes.c_size_t)
  46. def test(string):
  47. string_bytes = bytes(string, 'utf-8')
  48. prev_part_bytes = b''
  49. prev_length_bytes = -1
  50. for length in range(len(string) + 1):
  51. part = string[:length]
  52. part_bytes = bytes(part, 'utf-8')
  53. length_bytes = len(part_bytes)
  54. for length_bytes_2 in range(prev_length_bytes + 1, length_bytes):
  55. self.ae(utf_8_strndup(string_bytes, length_bytes_2).value, prev_part_bytes)
  56. self.ae(utf_8_strndup(string_bytes, length_bytes).value, part_bytes)
  57. prev_part_bytes = part_bytes
  58. prev_length_bytes = length_bytes
  59. self.ae(utf_8_strndup(string_bytes, len(string_bytes) + 1).value, string_bytes) # Try to go one character after the end of the string
  60. self.ae(utf_8_strndup(None, 2).value, None)
  61. self.ae(utf_8_strndup(b'', 2).value, b'')
  62. test('ö')
  63. test('>a<')
  64. test('>ä<')
  65. test('>ế<')
  66. test('>𐍈<')
  67. test('∮ E⋅da = Q, n → ∞, 𐍈∑ f(i) = ∏ g(i)')
  68. self.ae(utf_8_strndup(b'\xf0\x9f\x98\xb8', 3).value, b'')
  69. self.ae(utf_8_strndup(b'\xc3\xb6\xf0\x9f\x98\xb8', 4).value, b'\xc3\xb6')