run_android_tests.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/python
  2. """Test assumptions that Android relies on."""
  3. import glob
  4. import json
  5. import unittest
  6. from fontTools import ttLib
  7. from nototools import coverage
  8. from nototools import font_data
  9. from nototools import render
  10. from nototools import unicode_data
  11. def load_fonts():
  12. """Load all fonts built for Android."""
  13. all_font_files = glob.glob('out/android/*.ttf')
  14. all_fonts = [ttLib.TTFont(font) for font in all_font_files]
  15. assert len(all_font_files) == 18
  16. return all_font_files, all_fonts
  17. class TestVerticalMetrics(unittest.TestCase):
  18. """Test the vertical metrics of fonts."""
  19. def setUp(self):
  20. _, self.fonts = load_fonts()
  21. def test_ymin_ymax(self):
  22. """Tests yMin and yMax to be equal to what Android expects."""
  23. for font in self.fonts:
  24. head_table = font['head']
  25. self.assertEqual(head_table.yMin, -555)
  26. self.assertEqual(head_table.yMax, 2163)
  27. class TestDigitWidths(unittest.TestCase):
  28. """Tests the width of digits."""
  29. def setUp(self):
  30. _, self.fonts = load_fonts()
  31. self.digits = [
  32. 'zero', 'one', 'two', 'three', 'four',
  33. 'five', 'six', 'seven', 'eight', 'nine']
  34. def test_digit_widths(self):
  35. """Tests all decimal digits to make sure they have the same width."""
  36. for font in self.fonts:
  37. hmtx_table = font['hmtx']
  38. widths = [hmtx_table[digit][0] for digit in self.digits]
  39. self.assertEqual(len(set(widths)), 1)
  40. class TestCharacterCoverage(unittest.TestCase):
  41. """Tests character coverage."""
  42. def setUp(self):
  43. _, self.fonts = load_fonts()
  44. self.LEGACY_PUA = frozenset({0xEE01, 0xEE02, 0xF6C3})
  45. def test_lack_of_arrows_and_combining_keycap(self):
  46. """Tests that arrows and combining keycap are not in the fonts."""
  47. for font in self.fonts:
  48. charset = coverage.character_set(font)
  49. self.assertNotIn(0x20E3, charset) # COMBINING ENCLOSING KEYCAP
  50. self.assertNotIn(0x2191, charset) # UPWARDS ARROW
  51. self.assertNotIn(0x2193, charset) # DOWNWARDS ARROW
  52. def test_lack_of_unassigned_chars(self):
  53. """Tests that unassigned characters are not in the fonts."""
  54. for font in self.fonts:
  55. charset = coverage.character_set(font)
  56. self.assertNotIn(0x2072, charset)
  57. self.assertNotIn(0x2073, charset)
  58. self.assertNotIn(0x208F, charset)
  59. def test_inclusion_of_sound_recording_copyright(self):
  60. """Tests that sound recording copyright symbol is in the fonts."""
  61. for font in self.fonts:
  62. charset = coverage.character_set(font)
  63. self.assertIn(
  64. 0x2117, charset, # SOUND RECORDING COPYRIGHT
  65. 'U+2117 not found in %s.' % font_data.font_name(font))
  66. def test_inclusion_of_legacy_pua(self):
  67. """Tests that legacy PUA characters remain in the fonts."""
  68. for font in self.fonts:
  69. charset = coverage.character_set(font)
  70. for char in self.LEGACY_PUA:
  71. self.assertIn(char, charset)
  72. def test_non_inclusion_of_other_pua(self):
  73. """Tests that there are not other PUA characters except legacy ones."""
  74. for font in self.fonts:
  75. charset = coverage.character_set(font)
  76. pua_chars = {
  77. char for char in charset
  78. if 0xE000 <= char <= 0xF8FF or 0xF0000 <= char <= 0x10FFFF}
  79. self.assertTrue(pua_chars <= self.LEGACY_PUA)
  80. class TestSpacingMarks(unittest.TestCase):
  81. """Tests that spacing marks are indeed spacing."""
  82. def setUp(self):
  83. self.font_files, _ = load_fonts()
  84. charset = coverage.character_set(self.font_files[0])
  85. self.marks_to_test = [char for char in charset
  86. if unicode_data.category(char) in ['Lm', 'Sk']]
  87. self.advance_cache = {}
  88. def get_advances(self, text, font):
  89. """Get a list of horizontal advances for text rendered in a font."""
  90. try:
  91. return self.advance_cache[(text, font)]
  92. except KeyError:
  93. hb_output = render.run_harfbuzz_on_text(text, font, '')
  94. hb_output = json.loads(hb_output)
  95. advances = [glyph['ax'] for glyph in hb_output]
  96. self.advance_cache[(text, font)] = advances
  97. return advances
  98. def test_individual_spacing_marks(self):
  99. """Tests that spacing marks are spacing by themselves."""
  100. for font in self.font_files:
  101. print 'Testing %s for stand-alone spacing marks...' % font
  102. for mark in self.marks_to_test:
  103. mark = unichr(mark)
  104. advances = self.get_advances(mark, font)
  105. assert len(advances) == 1
  106. self.assertNotEqual(advances[0], 0)
  107. def test_spacing_marks_in_combination(self):
  108. """Tests that spacing marks do not combine with base letters."""
  109. for font in self.font_files:
  110. print 'Testing %s for spacing marks in combination...' % font
  111. for base_letter in (u'A\u00C6BCDEFGHIJKLMNO\u00D8\u01A0PRST'
  112. u'U\u01AFVWXYZ'
  113. u'a\u00E6bcdefghi\u0131j\u0237klmn'
  114. u'o\u00F8\u01A1prs\u017Ftu\u01B0vwxyz'
  115. u'\u03D2'):
  116. print 'Testing %s combinations' % base_letter
  117. for mark in self.marks_to_test:
  118. mark = unichr(mark)
  119. advances = self.get_advances(base_letter + mark, font)
  120. self.assertEqual(len(advances), 2,
  121. 'The sequence <%04X, %04X> combines, '
  122. 'but it should not' % (ord(base_letter), ord(mark)))
  123. if __name__ == '__main__':
  124. unittest.main()