xref: /trunk/test/odf-validate/fixtures/t13/prompt_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/t13/prompt_check.py <port> <package.odt|ods|odp|odg> <outdir>
23# Loads copies of one ODF 1.2 package whose office:version and manifest:version are rewritten to 1.2, 1.3 and 1.4, with an
24# interaction handler, and reports whether the "created with a newer version" request (FutureDocumentVersionProductUpdateRequest)
25# was raised, counted as a call of the handler (the documents used raise no other request). Expected: none for 1.2 and 1.3, one for
26# 1.4 (the negative control). Needs Load/ShowOfficeUpdateDialog = true.
27import os, re, sys, zipfile
28import uno, unohelper
29from com.sun.star.task import XInteractionHandler
30from unohelp import connect, desktop, pv
31
32port, src, out = int(sys.argv[1]), os.path.abspath(sys.argv[2]), os.path.abspath(sys.argv[3])
33os.makedirs(out, exist_ok=True)
34
35
36class Handler(unohelper.Base, XInteractionHandler):
37    # getRequest() cannot be used from pyuno here (it raises, the request being an exception value), so a call counts
38    def __init__(self):
39        self.calls = 0
40
41    def handle(self, request):
42        self.calls += 1
43        for c in request.getContinuations():
44            if c.queryInterface(uno.getTypeByName("com.sun.star.task.XInteractionApprove")):
45                c.select()
46                return
47
48
49def rewrite(version, dst):
50    zi, zo = zipfile.ZipFile(src), zipfile.ZipFile(dst, "w")
51    for it in zi.infolist():
52        d = zi.read(it.filename)
53        if it.filename.endswith(".xml") and (it.filename == "META-INF/manifest.xml" or it.filename.rsplit("/", 1)[-1] in ("content.xml", "styles.xml", "meta.xml", "settings.xml")):
54            t = d.decode("utf-8")
55            attr = "manifest:version" if it.filename == "META-INF/manifest.xml" else "office:version"
56            t = re.sub(r'(%s=")[0-9.]+(")' % attr, r"\g<1>%s\g<2>" % version, t)
57            d = t.encode("utf-8")
58        zo.writestr(it, d, compress_type=it.compress_type)
59    zo.close()
60
61
62ctx = connect(port)
63desk = desktop(ctx)
64ext = os.path.splitext(src)[1]
65results = {}
66for version, want in (("1.2", False), ("1.3", False), ("1.4", True)):
67    dst = os.path.join(out, "v" + version.replace(".", "") + ext)
68    rewrite(version, dst)
69    h = Handler()
70    doc = desk.loadComponentFromURL(uno.systemPathToFileUrl(dst), "_blank", 0,
71            (pv("Hidden", True), pv("MacroExecutionMode", 0), pv("ReadOnly", True), pv("UpdateDocMode", 0), pv("InteractionHandler", h)))
72    loaded = doc is not None
73    if loaded:
74        doc.close(True)
75    got = h.calls > 0
76    results[version] = (loaded, got, want)
77    print("%s: version %s, loaded=%s, update request raised=%s (handler calls: %d)" % ("PASS" if loaded and got == want else "FAIL", version, loaded, got, h.calls))
78print("prompt_check: %d/%d pass" % (sum(1 for l, g, w in results.values() if l and g == w), len(results)))
79sys.exit(0 if all(l and g == w for l, g, w in results.values()) else 1)
80