xref: /trunk/test/odf-validate/fixtures/p8/gridsnap_check.py (revision 0e16a31d78a652c56c5c0fc79c752f48b9048df9)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22# usage: runpy.sh fixtures/p8/gridsnap_check.py <port> <outdir> <mode name> <strict: 1|0>
23# The office runs with the save mode of <mode name>. Sets a character grid on the default Writer page style, saves, and checks
24# which spelling of the snap-to attribute styles.xml carries: `style:layout-grid-snap-to` in the ODF 1.2 and 1.3 modes,
25# `style:layout-grid-snap-to-characters` (the name released versions read) in every other mode, never both.
26# Exit code 1 when a check fails.
27import os, re, sys, zipfile
28import uno
29from unohelp import connect, desktop, pv
30
31port, out, name, strict = int(sys.argv[1]), os.path.abspath(sys.argv[2]), sys.argv[3], sys.argv[4] == "1"
32os.makedirs(out, exist_ok=True)
33desk = desktop(connect(port))
34doc = desk.loadComponentFromURL("private:factory/swriter", "_blank", 0, (pv("Hidden", True),))
35ps = doc.StyleFamilies.getByName("PageStyles").getByName("Standard")
36ps.setPropertyValue("GridMode", 2)                  # lines and characters
37ps.setPropertyValue("GridSnapToChars", True)
38ps.setPropertyValue("StandardPageMode", True)       # the exporter drops the grid unless this is set
39path = os.path.join(out, "gridsnap-%s.odt" % name)
40doc.storeToURL(uno.systemPathToFileUrl(path), (pv("FilterName", "writer8"),))
41doc.close(True)
42styles = zipfile.ZipFile(path).read("styles.xml").decode("utf-8")
43new = len(re.findall(r'style:layout-grid-snap-to="', styles))
44old = len(re.findall(r'style:layout-grid-snap-to-characters="', styles))
45print("%s: layout-grid-snap-to x%d, layout-grid-snap-to-characters x%d, layout-grid-mode: %s" % (
46    name, new, old, re.findall(r'style:layout-grid-mode="[^"]*"', styles)[:1]))
47checks = []
48if strict:
49    checks.append(("the new spelling (snap-to) only", new > 0 and old == 0))
50else:
51    checks.append(("the old spelling (snap-to-characters) only", old > 0 and new == 0))
52fails = 0
53for label, ok in checks:
54    fails += not ok
55    print("%-4s %-7s %s" % ("ok" if ok else "FAIL", name, label))
56sys.exit(1 if fails else 0)
57