| 1 | from pathlib import Path |
| 2 | import runpy |
| 3 | import shutil |
| 4 | from tempfile import TemporaryDirectory |
| 5 | import unittest |
| 6 | import xml.etree.ElementTree as ET |
| 7 | |
| 8 | from native_xml import ns |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parent.parent |
| 11 | FIXTURE = ROOT / 'corpus/styles' |
| 12 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 13 | GALLERY = {'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'PageTitle', 'cite', 'blockquote', 'code', 'p'} |
| 14 | |
| 15 | |
| 16 | def styles(path): |
| 17 | """The page's name and its quick styles by name: font, size, bold, colour.""" |
| 18 | page = ET.parse(path).getroot() |
| 19 | defined = {} |
| 20 | for style in page.findall('one:QuickStyleDef', ns): |
| 21 | defined.setdefault(style.get('name'), []).append( |
| 22 | (style.get('font'), float(style.get('fontSize')), style.get('bold') == 'true', style.get('fontColor'))) |
| 23 | return page.get('name'), defined |
| 24 | |
| 25 | |
| 26 | class StylesTest(unittest.TestCase): |
| 27 | def test_themed_pages_cold_open_as_written(self): |
| 28 | with TemporaryDirectory() as temporary: |
| 29 | read = Path(temporary) / 'read' |
| 30 | shutil.copytree(FIXTURE / 'cold/read', read) |
| 31 | compare(FIXTURE / 'candidate', read) |
| 32 | |
| 33 | def test_onenote_lists_each_theme_under_its_names(self): |
| 34 | heading = { |
| 35 | 'OneNote': ('Calibri', 16.0, True, '#17365D'), |
| 36 | 'Manuscript': ('Georgia', 20.0, False, '#3B2F2A'), |
| 37 | 'Editorial': ('Georgia', 18.0, True, '#9A3B1F'), |
| 38 | 'Modern': ('Arial', 16.0, True, '#2E5CB8'), |
| 39 | } |
| 40 | seen = set() |
| 41 | for path in sorted((FIXTURE / 'cold/read').glob('page-*.xml')): |
| 42 | name, defined = styles(path) |
| 43 | theme = name.removeprefix('Heading 1 in ') |
| 44 | seen.add(theme) |
| 45 | self.assertEqual(set(defined), GALLERY, theme) |
| 46 | self.assertEqual(defined['h1'], [heading[theme]], theme) |
| 47 | if theme == 'Modern': |
| 48 | # OneNote's own page title, in Arial, and text automatic but for the accent. |
| 49 | self.assertEqual(defined['PageTitle'], [('Arial', 17.0, False, 'automatic')]) |
| 50 | self.assertEqual(defined['p'], [('Arial', 10.5, False, 'automatic')]) |
| 51 | self.assertEqual(defined['h2'], [('Arial', 13.0, True, 'automatic')]) |
| 52 | self.assertEqual(seen, set(heading)) |
| 53 | |
| 54 | def test_onenote_s_own_styles_read_under_the_same_names(self): |
| 55 | _, applied = styles(FIXTURE / 'onenote/applied.xml') |
| 56 | self.assertEqual(set(applied), GALLERY) |
| 57 | # OneNote's gallery on a themed page adds its own h2, and Enter its own p. |
| 58 | _, edited = styles(FIXTURE / 'onenote-edit/Manuscript.xml') |
| 59 | self.assertEqual(sorted(edited['h2']), [('Calibri', 13.0, True, '#366092'), ('Georgia', 16.0, True, '#3B2F2A')]) |
| 60 | self.assertEqual(edited['p'], [('Calibri', 11.0, False, 'automatic')]) |
| 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | unittest.main() |