Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / extras / ooxmlexport / ooxmllinks.cxx
blob000f4a607ae554ac003eb8d672b184c5ffa84987
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <swmodeltestbase.hxx>
11 #include <unotools/tempfile.hxx>
12 #include <tools/urlobj.hxx>
13 #include <officecfg/Office/Common.hxx>
14 #include <o3tl/string_view.hxx>
16 // This file contains tests to check relative/absolute hyperlinks handling
18 #define USE_TEMP_DIR true
19 #define DONT_MODIFY_LINK false
21 #define USE_ABSOLUTE true
22 #define USE_RELATIVE false
24 // bAbsolute - decide if output link should be converted to absolute
25 // bUseTempDir - decide if link should be modified to be placed in temp dir - for testing relative links
26 #define DECLARE_LINKS_EXPORT_TEST(TestName, FileName, bAbsolute, bUseTempDir) \
27 class TestName : public Test \
28 { \
29 protected: \
30 virtual OUString getTestName() override { return #TestName; } \
31 virtual void postLoad(const char*) override \
32 { \
33 if (!bUseTempDir) \
34 return; \
36 uno::Reference<text::XTextRange> xParagraph = getParagraph(1); \
37 /* can be changed only after import */ \
38 uno::Reference<text::XTextRange> xText = getRun(xParagraph, 1); \
40 /* Get original link */ \
41 OUString sOriginalFileName = getProperty<OUString>(xText, "HyperLinkURL"); \
42 INetURLObject aOriginalURL(sOriginalFileName); \
43 CPPUNIT_ASSERT(!aOriginalURL.HasError()); \
44 OUString sFileName = aOriginalURL.GetLastName(); \
45 CPPUNIT_ASSERT(!sFileName.isEmpty()); \
47 /* Get temp path */ \
48 OUString sTempDir = utl::GetTempNameBaseDirectory(); \
50 /* Create & apply new URL */ \
51 OUString sOriginalFileInTempDir = sTempDir + sFileName; \
52 uno::Reference<beans::XPropertySet> xPropertySet(xText, css::uno::UNO_QUERY); \
53 xPropertySet->setPropertyValue("HyperLinkURL", css::uno::Any(sOriginalFileInTempDir)); \
54 } \
56 public: \
57 CPPUNIT_TEST_SUITE(TestName); \
58 CPPUNIT_TEST(Import_Export_Import); \
59 CPPUNIT_TEST_SUITE_END(); \
60 void Import_Export_Import() \
61 { \
62 auto xChanges = comphelper::ConfigurationChanges::create(); \
63 officecfg::Office::Common::Save::URL::FileSystem::set(!bAbsolute, xChanges); \
64 xChanges->commit(); \
65 executeLoadReloadVerify(FileName); \
66 } \
67 void verify() override; \
68 }; \
69 CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
70 void TestName::verify()
72 // bAbsolute - decide if relative link should be converted to absolute on import
73 #define DECLARE_LINKS_IMPORT_TEST(TestName, FileName, bAbsolute) \
74 class TestName : public Test \
75 { \
76 protected: \
77 virtual OUString getTestName() override { return #TestName; } \
79 public: \
80 CPPUNIT_TEST_SUITE(TestName); \
81 CPPUNIT_TEST(Import); \
82 CPPUNIT_TEST_SUITE_END(); \
83 void Import() \
84 { \
85 auto xChanges = comphelper::ConfigurationChanges::create(); \
86 officecfg::Office::Common::Save::URL::FileSystem::set(!bAbsolute, xChanges); \
87 xChanges->commit(); \
88 executeImportTest(FileName); \
89 } \
90 void verify() override; \
91 }; \
92 CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
93 void TestName::verify()
95 class Test : public SwModelTestBase
97 public:
98 Test()
99 : SwModelTestBase("/sw/qa/extras/ooxmlexport/data/", "Office Open XML Text")
104 /* IMPORT */
106 DECLARE_LINKS_IMPORT_TEST(testRelativeToRelativeImport, "relative-link.docx", USE_RELATIVE)
108 uno::Reference<text::XTextRange> xParagraph = getParagraph(1);
109 uno::Reference<text::XTextRange> xText = getRun(xParagraph, 1);
110 OUString sTarget = getProperty<OUString>(xText, "HyperLinkURL");
111 CPPUNIT_ASSERT(sTarget.startsWith("file:///"));
112 CPPUNIT_ASSERT(sTarget.endsWith("relative.docx"));
115 DECLARE_LINKS_IMPORT_TEST(testRelativeToAbsoluteImport, "relative-link.docx", USE_ABSOLUTE)
117 uno::Reference<text::XTextRange> xParagraph = getParagraph(1);
118 uno::Reference<text::XTextRange> xText = getRun(xParagraph, 1);
119 OUString sTarget = getProperty<OUString>(xText, "HyperLinkURL");
120 CPPUNIT_ASSERT(sTarget.startsWith("file:///"));
121 CPPUNIT_ASSERT(sTarget.endsWith("relative.docx"));
124 DECLARE_LINKS_IMPORT_TEST(testAbsoluteToAbsoluteImport, "absolute-link.docx", USE_ABSOLUTE)
126 uno::Reference<text::XTextRange> xParagraph = getParagraph(1);
127 uno::Reference<text::XTextRange> xText = getRun(xParagraph, 1);
128 // # should be encoded
129 CPPUNIT_ASSERT_EQUAL(OUString("file:///B:/Users/user/Desktop/a%23b/test.docx"),
130 getProperty<OUString>(xText, "HyperLinkURL"));
133 DECLARE_LINKS_IMPORT_TEST(testAbsoluteToRelativeImport, "absolute-link.docx", USE_RELATIVE)
135 uno::Reference<text::XTextRange> xParagraph = getParagraph(1);
136 uno::Reference<text::XTextRange> xText = getRun(xParagraph, 1);
137 // when target file (B:\\...) & document with link (temp dir) are placed on different partitions, absolute path will be loaded
138 CPPUNIT_ASSERT_EQUAL(OUString("file:///B:/Users/user/Desktop/a%23b/test.docx"),
139 getProperty<OUString>(xText, "HyperLinkURL"));
142 DECLARE_LINKS_IMPORT_TEST(testTdf123627_import, "tdf123627.docx", USE_RELATIVE)
144 uno::Reference<text::XTextRange> xText = getRun(getParagraph(1), 1);
145 OUString sTarget = getProperty<OUString>(xText, "HyperLinkURL");
146 CPPUNIT_ASSERT(sTarget.startsWith("file:///"));
147 CPPUNIT_ASSERT(sTarget.endsWith("New/test.docx"));
150 /* EXPORT */
152 DECLARE_LINKS_EXPORT_TEST(testRelativeToRelativeExport, "relative-link.docx", USE_RELATIVE,
153 USE_TEMP_DIR)
155 xmlDocUniquePtr pXmlDoc = parseExport("word/_rels/document.xml.rels");
157 assertXPath(pXmlDoc, "/rels:Relationships/rels:Relationship[@TargetMode='External']", "Target",
158 "relative.docx");
161 DECLARE_LINKS_EXPORT_TEST(testRelativeToAbsoluteExport, "relative-link.docx", USE_ABSOLUTE,
162 DONT_MODIFY_LINK)
164 xmlDocUniquePtr pXmlDoc = parseExport("word/_rels/document.xml.rels");
166 OUString sTarget = getXPath(pXmlDoc, "/rels:Relationships/rels:Relationship[2]", "Target");
167 CPPUNIT_ASSERT(sTarget.startsWith("file:///"));
168 CPPUNIT_ASSERT(sTarget.endsWith("relative.docx"));
171 DECLARE_LINKS_EXPORT_TEST(testAbsoluteToRelativeExport, "absolute-link.docx", USE_RELATIVE,
172 USE_TEMP_DIR)
174 xmlDocUniquePtr pXmlDoc = parseExport("word/_rels/document.xml.rels");
176 assertXPath(pXmlDoc, "/rels:Relationships/rels:Relationship[2]", "Target", "test.docx");
179 DECLARE_LINKS_EXPORT_TEST(testAbsoluteToAbsoluteExport, "absolute-link.docx", USE_ABSOLUTE,
180 DONT_MODIFY_LINK)
182 xmlDocUniquePtr pXmlDoc = parseExport("word/_rels/document.xml.rels");
184 OUString sTarget = getXPath(pXmlDoc, "/rels:Relationships/rels:Relationship[2]", "Target");
185 CPPUNIT_ASSERT(sTarget.startsWith("file:///"));
186 CPPUNIT_ASSERT(sTarget.endsWith("test.docx"));
189 DECLARE_LINKS_EXPORT_TEST(testTdf123627_export, "tdf123627.docx", USE_RELATIVE, USE_TEMP_DIR)
191 xmlDocUniquePtr pXmlDoc = parseExport("word/_rels/document.xml.rels");
193 assertXPath(pXmlDoc, "/rels:Relationships/rels:Relationship[@TargetMode='External']", "Target",
194 "test.docx");
197 DECLARE_LINKS_EXPORT_TEST(testTdf126590_export, "tdf126590.docx", USE_ABSOLUTE, DONT_MODIFY_LINK)
199 xmlDocUniquePtr pXmlDoc = parseExport("word/_rels/document.xml.rels");
200 // in the original file: Target="file:///C:\TEMP\test.docx" => invalid file URI
201 assertXPath(pXmlDoc, "/rels:Relationships/rels:Relationship[@TargetMode='External']", "Target",
202 "file:///C:/TEMP/test.docx");
205 DECLARE_LINKS_EXPORT_TEST(testTdf126768_export, "tdf126768.docx", USE_ABSOLUTE, DONT_MODIFY_LINK)
207 xmlDocUniquePtr pXmlDoc = parseExport("word/_rels/document.xml.rels");
208 // in the original file: "file:///C:\\TEMP\\test.docx" => invalid file URI
209 assertXPath(pXmlDoc, "/rels:Relationships/rels:Relationship[@TargetMode='External']", "Target",
210 "file:///C:/TEMP/test.docx");
213 DECLARE_LINKS_EXPORT_TEST(testNon_ascii_link_export, "non_ascii_link.docx", USE_ABSOLUTE,
214 DONT_MODIFY_LINK)
216 xmlDocUniquePtr pXmlDoc = parseExport("word/_rels/document.xml.rels");
218 assertXPath(pXmlDoc, "/rels:Relationships/rels:Relationship[@TargetMode='External']", "Target",
219 INetURLObject::decode(u"file:///C:/TEMP/%C3%A9kezet.docx",
220 INetURLObject::DecodeMechanism::ToIUri,
221 RTL_TEXTENCODING_UTF8));
224 CPPUNIT_PLUGIN_IMPLEMENT();
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */