xref: /trunk/test/odf-validate/fixtures/p8/calc_spelling_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/calc_spelling_check.py <port> <outdir> <mode name> <strict: 1|0>
23# The office runs with the save mode of <mode name> (DefaultVersion in its profile). Builds a pivot table with a named group,
24# saves the sheet and checks the element that holds the group members in content.xml: the ODF 1.2 and 1.3 modes write
25# `table:data-pilot-group-member`, every other mode writes `table:data-pilot-member`, which older versions read. The same
26# condition guards `table:database-table-name` (database ranges and pivot sources); no database was available to check those two.
27# Exit code 1 when a check fails.
28import os, re, sys, zipfile
29import uno
30from com.sun.star.table import CellAddress
31from com.sun.star.table import CellRangeAddress
32from unohelp import connect, desktop, pv
33
34port, out, name, strict = int(sys.argv[1]), os.path.abspath(sys.argv[2]), sys.argv[3], sys.argv[4] == "1"
35os.makedirs(out, exist_ok=True)
36desk = desktop(connect(port))
37doc = desk.loadComponentFromURL("private:factory/scalc", "_blank", 0, (pv("Hidden", True),))
38sh = doc.Sheets.getByIndex(0)
39rows = (("Cat", "Val"), ("x", 1), ("y", 2), ("z", 3), ("x", 4))
40for r, row in enumerate(rows):
41    for c, v in enumerate(row):
42        cell = sh.getCellByPosition(c, r)
43        if isinstance(v, str): cell.String = v
44        else: cell.Value = v
45
46dp = sh.DataPilotTables
47desc = dp.createDataPilotDescriptor()
48desc.setSourceRange(CellRangeAddress(0, 0, 0, 1, 4))
49fields = desc.DataPilotFields
50fields.getByIndex(0).Orientation = uno.Enum("com.sun.star.sheet.DataPilotFieldOrientation", "ROW")
51fields.getByIndex(1).Orientation = uno.Enum("com.sun.star.sheet.DataPilotFieldOrientation", "DATA")
52dp.insertNewByName("pv", CellAddress(0, 4, 0), desc)
53table = dp.getByName("pv")
54table.DataPilotFields.getByIndex(0).createNameGroup(("x", "y"))
55
56path = os.path.join(out, "spelling-%s.ods" % name)
57doc.storeToURL(uno.systemPathToFileUrl(path), (pv("FilterName", "calc8"),))
58doc.close(True)
59
60content = zipfile.ZipFile(path).read("content.xml").decode("utf-8")
61group = re.search(r"<table:data-pilot-group\b[^>]*>(.*?)</table:data-pilot-group>", content, re.S)
62inner = group.group(1) if group else ""
63print("group content:", re.findall(r"<table:data-pilot-[a-z-]*", inner))
64
65checks = [
66    ("a named group was written", bool(group)),
67    ("group members use the %s element" % ("data-pilot-group-member" if strict else "data-pilot-member"),
68     ("<table:data-pilot-group-member" in inner) == strict and ("<table:data-pilot-member" in inner) == (not strict)),
69]
70fails = 0
71for label, ok in checks:
72    fails += not ok
73    print("%-4s %-7s %s" % ("ok" if ok else "FAIL", name, label))
74sys.exit(1 if fails else 0)
75