Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / starmath / qa / extras / mmlexport-test.cxx
blobb26fcc1baac14073e9111a9fdd65b1231e5bc689
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 <sal/config.h>
11 #include <test/bootstrapfixture.hxx>
12 #include <test/xmltesttools.hxx>
13 #include <unotools/tempfile.hxx>
15 #include <sfx2/docfile.hxx>
16 #include <sfx2/docfilt.hxx>
17 #include <sfx2/sfxmodelfactory.hxx>
19 #include <document.hxx>
20 #include <smdll.hxx>
22 #include <memory>
24 namespace {
26 using namespace ::com::sun::star;
28 typedef tools::SvRef<SmDocShell> SmDocShellRef;
30 class MathMLExportTest : public test::BootstrapFixture, public XmlTestTools
32 public:
33 virtual void setUp() override;
34 virtual void tearDown() override;
36 void testBlank();
37 void testTdf97049();
38 void testTdf101022();
40 CPPUNIT_TEST_SUITE(MathMLExportTest);
41 CPPUNIT_TEST(testBlank);
42 CPPUNIT_TEST(testTdf97049);
43 CPPUNIT_TEST(testTdf101022);
44 CPPUNIT_TEST_SUITE_END();
46 protected:
47 virtual void registerNamespaces(xmlXPathContextPtr &pXmlXPathCtx) override;
49 private:
50 xmlDocPtr exportAndParse();
52 SmDocShellRef mxDocShell;
55 void MathMLExportTest::setUp()
57 BootstrapFixture::setUp();
58 SmGlobals::ensure();
59 mxDocShell = new SmDocShell(SfxModelFlags::EMBEDDED_OBJECT |
60 SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS |
61 SfxModelFlags::DISABLE_DOCUMENT_RECOVERY);
64 void MathMLExportTest::tearDown()
66 if (mxDocShell.is())
67 mxDocShell->DoClose();
68 BootstrapFixture::tearDown();
71 void MathMLExportTest::registerNamespaces(xmlXPathContextPtr &pXmlXPathCtx)
73 xmlXPathRegisterNs(pXmlXPathCtx, BAD_CAST("m"), BAD_CAST("http://www.w3.org/1998/Math/MathML"));
76 xmlDocPtr MathMLExportTest::exportAndParse()
78 utl::TempFile aTempFile;
79 aTempFile.EnableKillingFile();
80 SfxMedium aStoreMedium(aTempFile.GetURL(), StreamMode::STD_WRITE);
81 std::shared_ptr<const SfxFilter> pExportFilter = SfxFilter::GetFilterByName(MATHML_XML);
82 aStoreMedium.SetFilter(pExportFilter);
83 CPPUNIT_ASSERT(mxDocShell->ConvertTo(aStoreMedium));
84 aStoreMedium.Commit();
85 xmlDocPtr pDoc = parseXml(aTempFile);
86 CPPUNIT_ASSERT(pDoc);
87 return pDoc;
90 void MathMLExportTest::testBlank()
92 mxDocShell->SetText("x`y~~z");
93 xmlDocPtr pDoc = exportAndParse();
94 assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mspace[1]", "width", "0.5em");
95 assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mspace[2]", "width", "4em");
98 void MathMLExportTest::testTdf97049()
100 mxDocShell->SetText("intd {{1 over x} dx}");
101 xmlDocPtr pDoc = exportAndParse();
102 assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mo[1]", "stretchy", "true");
103 auto aContent = getXPathContent(pDoc, "/m:math/m:semantics/m:mrow/m:mo[1]");
104 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aContent.getLength());
105 CPPUNIT_ASSERT_EQUAL(u'\x222B', aContent[0]);
108 void MathMLExportTest::testTdf101022()
110 #define CHECK_MATHVARIANT(capital, small) do \
112 mxDocShell->SetText("%GAMMA %iGAMMA {ital %GAMMA} {nitalic %iGAMMA} " \
113 "%gamma %igamma {ital %gamma} {nitalic %igamma}"); \
114 xmlDocPtr pDoc = exportAndParse(); \
115 if (capital) \
116 assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mi[1]", "mathvariant"); \
117 else \
118 assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mi[1]", "mathvariant", "normal"); \
119 assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mstyle[1]/m:mi[1]", "mathvariant"); \
120 assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mi[2]", "mathvariant"); \
121 assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mstyle[2]/m:mi[1]", "mathvariant", "normal"); \
122 if (small) \
123 assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mi[3]", "mathvariant"); \
124 else \
125 assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mi[3]", "mathvariant", "normal"); \
126 assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mstyle[3]/m:mi[1]", "mathvariant"); \
127 assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mi[4]", "mathvariant"); \
128 assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mstyle[4]/m:mi[1]", "mathvariant", "normal"); \
129 mxDocShell->SetText(""); \
131 while (false)
133 CHECK_MATHVARIANT(false, true); // default mode 2
135 mxDocShell->SetGreekCharStyle(1); // mode 1
136 CHECK_MATHVARIANT(true, true);
138 mxDocShell->SetGreekCharStyle(0); // mode 0
139 CHECK_MATHVARIANT(false, false);
141 #undef CHECK_MATHVARIANT
144 CPPUNIT_TEST_SUITE_REGISTRATION(MathMLExportTest);
148 CPPUNIT_PLUGIN_IMPLEMENT();
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */