summaryrefslogtreecommitdiff
path: root/xmloff/qa/unit
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@collabora.com>2022-05-26 08:25:50 +0200
committerMiklos Vajna <vmiklos@collabora.com>2022-05-30 14:26:07 +0200
commitc2bc478f217c5dd898321dfba19078ae1c32b0c4 (patch)
tree5e731922a31a065890269bd981c0c8329c356b46 /xmloff/qa/unit
parent87061501b4030736fd448c0df2f1e8fd007594fa (diff)
sw content controls, date: add ODT filter
Map the Date, DateFormat and DateLanguage UNO properties to: <loext:content-control loext:date="..." loext:date-format="..." loext:date-rfc-language-tag="..."> And do the opposite on import. (cherry picked from commit b3c6511659c58bcc75a9742e0ae0cc436b6f6825) Change-Id: I16d3f755d77ed20380d877ba65dfe6d063f2bec2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135117 Tested-by: Miklos Vajna <vmiklos@collabora.com> Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Diffstat (limited to 'xmloff/qa/unit')
-rw-r--r--xmloff/qa/unit/data/content-control-date.fodt8
-rw-r--r--xmloff/qa/unit/text.cxx75
2 files changed, 83 insertions, 0 deletions
diff --git a/xmloff/qa/unit/data/content-control-date.fodt b/xmloff/qa/unit/data/content-control-date.fodt
new file mode 100644
index 000000000000..dd3749a02e99
--- /dev/null
+++ b/xmloff/qa/unit/data/content-control-date.fodt
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<office:document xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
+ <office:body>
+ <office:text>
+ <text:p><loext:content-control loext:date="true" loext:date-format="YYYY-MM-DD" loext:date-rfc-language-tag="en-US">choose a date</loext:content-control></text:p>
+ </office:text>
+ </office:body>
+</office:document>
diff --git a/xmloff/qa/unit/text.cxx b/xmloff/qa/unit/text.cxx
index e91f71d64cc7..4f32c23025de 100644
--- a/xmloff/qa/unit/text.cxx
+++ b/xmloff/qa/unit/text.cxx
@@ -648,6 +648,81 @@ CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testPictureContentControlImport)
CPPUNIT_ASSERT(bPicture);
}
+CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testDateContentControlExport)
+{
+ // Given a document with a date content control around a text portion:
+ getComponent() = loadFromDesktop("private:factory/swriter");
+ uno::Reference<lang::XMultiServiceFactory> xMSF(getComponent(), uno::UNO_QUERY);
+ uno::Reference<text::XTextDocument> xTextDocument(getComponent(), uno::UNO_QUERY);
+ uno::Reference<text::XText> xText = xTextDocument->getText();
+ uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
+ xText->insertString(xCursor, "choose a date", /*bAbsorb=*/false);
+ xCursor->gotoStart(/*bExpand=*/false);
+ xCursor->gotoEnd(/*bExpand=*/true);
+ uno::Reference<text::XTextContent> xContentControl(
+ xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY);
+ uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
+ xContentControlProps->setPropertyValue("Date", uno::Any(true));
+ xContentControlProps->setPropertyValue("DateFormat", uno::Any(OUString("YYYY-MM-DD")));
+ xContentControlProps->setPropertyValue("DateLanguage", uno::Any(OUString("en-US")));
+ xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
+
+ // When exporting to ODT:
+ uno::Reference<frame::XStorable> xStorable(getComponent(), uno::UNO_QUERY);
+ uno::Sequence<beans::PropertyValue> aStoreProps = comphelper::InitPropertySequence({
+ { "FilterName", uno::Any(OUString("writer8")) },
+ });
+ utl::TempFile aTempFile;
+ aTempFile.EnableKillingFile();
+ xStorable->storeToURL(aTempFile.GetURL(), aStoreProps);
+ validate(aTempFile.GetFileName(), test::ODF);
+
+ // Then make sure the expected markup is used:
+ std::unique_ptr<SvStream> pStream = parseExportStream(aTempFile, "content.xml");
+ // Without the accompanying fix in place, this test would have failed with:
+ // - XPath '//loext:content-control' no attribute 'date' exist
+ xmlDocUniquePtr pXmlDoc = parseXmlStream(pStream.get());
+ assertXPath(pXmlDoc, "//loext:content-control", "date", "true");
+ assertXPath(pXmlDoc, "//loext:content-control", "date-format", "YYYY-MM-DD");
+ assertXPath(pXmlDoc, "//loext:content-control", "date-rfc-language-tag", "en-US");
+}
+
+CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testDateContentControlImport)
+{
+ // Given an ODF document with a date content control:
+ OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "content-control-date.fodt";
+
+ // When loading that document:
+ getComponent() = loadFromDesktop(aURL);
+
+ // Then make sure that the content control is not lost on import:
+ uno::Reference<text::XTextDocument> xTextDocument(getComponent(), uno::UNO_QUERY);
+ uno::Reference<container::XEnumerationAccess> xParagraphsAccess(xTextDocument->getText(),
+ uno::UNO_QUERY);
+ uno::Reference<container::XEnumeration> xParagraphs = xParagraphsAccess->createEnumeration();
+ uno::Reference<container::XEnumerationAccess> xParagraph(xParagraphs->nextElement(),
+ uno::UNO_QUERY);
+ uno::Reference<container::XEnumeration> xPortions = xParagraph->createEnumeration();
+ uno::Reference<beans::XPropertySet> xTextPortion(xPortions->nextElement(), uno::UNO_QUERY);
+ OUString aPortionType;
+ xTextPortion->getPropertyValue("TextPortionType") >>= aPortionType;
+ CPPUNIT_ASSERT_EQUAL(OUString("ContentControl"), aPortionType);
+ uno::Reference<text::XTextContent> xContentControl;
+ xTextPortion->getPropertyValue("ContentControl") >>= xContentControl;
+ uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
+ bool bDate{};
+ xContentControlProps->getPropertyValue("Date") >>= bDate;
+ // Without the accompanying fix in place, this test would have failed, the content control was
+ // imported as a default rich text one.
+ CPPUNIT_ASSERT(bDate);
+ OUString aDateFormat;
+ xContentControlProps->getPropertyValue("DateFormat") >>= aDateFormat;
+ CPPUNIT_ASSERT_EQUAL(OUString("YYYY-MM-DD"), aDateFormat);
+ OUString aDateLanguage;
+ xContentControlProps->getPropertyValue("DateLanguage") >>= aDateLanguage;
+ CPPUNIT_ASSERT_EQUAL(OUString("en-US"), aDateLanguage);
+}
+
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */