xref: /trunk/test/odf-validate/fixtures/t16/check.py (revision 0e16a31d78a652c56c5c0fc79c752f48b9048df9)
1#!/usr/bin/env python3
2#**************************************************************
3#
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#    http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing,
15#  software distributed under the License is distributed on an
16#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17#  KIND, either express or implied.  See the License for the
18#  specific language governing permissions and limitations
19#  under the License.
20#
21#**************************************************************
22#
23# usage: runpy.sh fixtures/t16/check.py <port> <outdir>
24# A chart title whose text sits in text:span elements must be read like the same text without them. Prints PASS/FAIL lines.
25import sys, os, re, zipfile, uno
26from unohelp import connect, desktop, pv
27
28port, out = int(sys.argv[1]), os.path.abspath(sys.argv[2])
29os.makedirs(out, exist_ok=True)
30desk = desktop(connect(port))
31
32def check(name, ok, detail=""):
33    print(("PASS " + name) if ok else ("FAIL %s %s" % (name, detail)), flush=True)
34
35doc = desk.loadComponentFromURL("private:factory/scalc", "_blank", 0, (pv("Hidden", True),))
36sh = doc.Sheets.getByIndex(0)
37for r, row in enumerate(((1, 2), (3, 4), (5, 6))):
38    for c, v in enumerate(row):
39        sh.getCellByPosition(c, r).Value = v
40rect = uno.createUnoStruct("com.sun.star.awt.Rectangle"); rect.X, rect.Y, rect.Width, rect.Height = 1000, 4000, 8000, 6000
41rng = uno.createUnoStruct("com.sun.star.table.CellRangeAddress"); rng.Sheet, rng.StartColumn, rng.StartRow, rng.EndColumn, rng.EndRow = 0, 0, 0, 1, 2
42sh.Charts.addNewByName("c1", rect, (rng,), False, False)
43chart = sh.Charts.getByName("c1").EmbeddedObject
44chart.HasMainTitle = True
45chart.Title.String = "My Title"
46base = os.path.join(out, "chart-title.ods")
47doc.storeToURL(uno.systemPathToFileUrl(base), (pv("FilterName", "calc8"),)); doc.close(True)
48
49with zipfile.ZipFile(base) as zin:
50    entries = [(i.filename, zin.read(i.filename)) for i in zin.infolist()]
51part = [n for n, d in entries if n.endswith("/content.xml") and b"<chart:title" in d][0]
52orig = dict(entries)[part].decode("utf8")
53m = re.search(r"(<chart:title\b[^>]*>)<text:p>My Title</text:p>(</chart:title>)", orig)
54check("chart title written as plain text:p", m is not None)
55
56def title_of(markup, name):
57    p = os.path.join(out, name + ".ods")
58    with zipfile.ZipFile(p, "w") as zo:
59        for n, d in entries:
60            if n == part:
61                d = orig.replace(m.group(0), m.group(1) + markup + m.group(2)).encode("utf8")
62            zo.writestr(n, d, compress_type=zipfile.ZIP_STORED if n == "mimetype" else zipfile.ZIP_DEFLATED)
63    d = desk.loadComponentFromURL(uno.systemPathToFileUrl(p), "_blank", 0, (pv("Hidden", True),))
64    c = d.Sheets.getByIndex(0).Charts.getByIndex(0).EmbeddedObject
65    t = c.Title.String if c.HasMainTitle else None
66    d.close(True)
67    return t
68
69check("plain paragraph", title_of("<text:p>My Title</text:p>", "v0") == "My Title")
70check("one span", title_of('<text:p><text:span text:style-name="T1">My Title</text:span></text:p>', "v1") == "My Title")
71check("two spans", title_of("<text:p><text:span>My </text:span><text:span>Title</text:span></text:p>", "v2") == "My Title")
72plain = title_of("<text:p>ABC<text:tab/>D<text:line-break/>E</text:p>", "v4")
73check("nested spans match the same text without spans", title_of("<text:p>A<text:span>B<text:span>C</text:span></text:span><text:tab/>D<text:span><text:line-break/>E</text:span></text:p>", "v3") == plain and plain is not None, plain)
74