| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | /* |
| 3 | * This file is part of the LibreOffice project. |
| 4 | * |
| 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 | */ |
| 9 | |
| 10 | #include <sal/config.h> |
| 11 | #include <test/unoapixml_test.hxx> |
| 12 | |
| 13 | #include <o3tl/cppunittraitshelper.hxx> |
| 14 | #include <sfx2/sfxbasemodel.hxx> |
| 15 | |
| 16 | #include <document.hxx> |
| 17 | #include <smdll.hxx> |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include <libxml/xpathInternals.h> |
| 22 | |
| 23 | using namespace ::com::sun::star; |
| 24 | |
| 25 | class MathMLExportTest : public UnoApiXmlTest |
| 26 | { |
| 27 | public: |
| 28 | MathMLExportTest() |
| 29 | : UnoApiXmlTest(u"starmath/qa/extras/data/"_ustr) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void testBlank(); |
| 34 | void testTdf97049(); |
| 35 | void testTdf101022(); |
| 36 | void testMaj(); |
| 37 | void testHadd(); |
| 38 | void testTdf158867(); |
| 39 | |
| 40 | CPPUNIT_TEST_SUITE(MathMLExportTest); |
| 41 | CPPUNIT_TEST(testBlank); |
| 42 | CPPUNIT_TEST(testTdf97049); |
| 43 | CPPUNIT_TEST(testTdf101022); |
| 44 | CPPUNIT_TEST(testMaj); |
| 45 | CPPUNIT_TEST(testHadd); |
| 46 | CPPUNIT_TEST(testTdf158867); |
| 47 | CPPUNIT_TEST_SUITE_END(); |
| 48 | |
| 49 | protected: |
| 50 | virtual void registerNamespaces(xmlXPathContextPtr& pXmlXPathCtx) override; |
| 51 | |
| 52 | void checkMathVariant(SmDocShell& rDocShell, bool bCapital, bool bSmall); |
| 53 | }; |
| 54 | |
| 55 | void MathMLExportTest::registerNamespaces(xmlXPathContextPtr& pXmlXPathCtx) |
| 56 | { |
| 57 | xmlXPathRegisterNs(pXmlXPathCtx, BAD_CAST("m"), BAD_CAST("http://www.w3.org/1998/Math/MathML")); |
| 58 | } |
| 59 | |
| 60 | void MathMLExportTest::testBlank() |
| 61 | { |
| 62 | loadFromURL(u"private:factory/smath"_ustr); |
| 63 | SfxBaseModel* pModel = dynamic_cast<SfxBaseModel*>(mxComponent.get()); |
| 64 | SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell()); |
| 65 | pDocShell->SetText(u"x`y~~z"_ustr); |
| 66 | save(TestFilter::MML); |
| 67 | xmlDocUniquePtr pDoc = parseXml(maTempFile); |
| 68 | CPPUNIT_ASSERT(pDoc); |
| 69 | assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mspace[1]", "width", u"0.5em"); |
| 70 | assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mspace[2]", "width", u"4em"); |
| 71 | } |
| 72 | |
| 73 | void MathMLExportTest::testTdf97049() |
| 74 | { |
| 75 | loadFromURL(u"private:factory/smath"_ustr); |
| 76 | SfxBaseModel* pModel = dynamic_cast<SfxBaseModel*>(mxComponent.get()); |
| 77 | SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell()); |
| 78 | pDocShell->SetText(u"intd {{1 over x} dx}"_ustr); |
| 79 | save(TestFilter::MML); |
| 80 | xmlDocUniquePtr pDoc = parseXml(maTempFile); |
| 81 | CPPUNIT_ASSERT(pDoc); |
| 82 | assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mo[1]", "stretchy", u"true"); |
| 83 | auto aContent = getXPathContent(pDoc, "/m:math/m:semantics/m:mrow/m:mo[1]"); |
| 84 | CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aContent.getLength()); |
| 85 | CPPUNIT_ASSERT_EQUAL(u'\x222B', aContent[0]); |
| 86 | } |
| 87 | |
| 88 | void MathMLExportTest::checkMathVariant(SmDocShell& rDocShell, bool bCapital, bool bSmall) |
| 89 | { |
| 90 | rDocShell.SetText(u"%GAMMA %iGAMMA {ital %GAMMA} {nitalic %iGAMMA} " |
| 91 | "%gamma %igamma {ital %gamma} {nitalic %igamma}"_ustr); |
| 92 | save(TestFilter::MML); |
| 93 | xmlDocUniquePtr pDoc = parseXml(maTempFile); |
| 94 | CPPUNIT_ASSERT(pDoc); |
| 95 | if (bCapital) |
| 96 | assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mi[1]", "mathvariant"); |
| 97 | else |
| 98 | assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mi[1]", "mathvariant", u"normal"); |
| 99 | assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mstyle[1]/m:mi[1]", "mathvariant"); |
| 100 | assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mi[2]", "mathvariant"); |
| 101 | assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mstyle[2]/m:mi[1]", "mathvariant", u"normal"); |
| 102 | if (bSmall) |
| 103 | assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mi[3]", "mathvariant"); |
| 104 | else |
| 105 | assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mi[3]", "mathvariant", u"normal"); |
| 106 | assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mstyle[3]/m:mi[1]", "mathvariant"); |
| 107 | assertXPathNoAttribute(pDoc, "/m:math/m:semantics/m:mrow/m:mi[4]", "mathvariant"); |
| 108 | assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:mstyle[4]/m:mi[1]", "mathvariant", u"normal"); |
| 109 | rDocShell.SetText(u""_ustr); |
| 110 | } |
| 111 | |
| 112 | void MathMLExportTest::testTdf101022() |
| 113 | { |
| 114 | loadFromURL(u"private:factory/smath"_ustr); |
| 115 | SfxBaseModel* pModel = dynamic_cast<SfxBaseModel*>(mxComponent.get()); |
| 116 | SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell()); |
| 117 | |
| 118 | checkMathVariant(*pDocShell, false, true); // default mode 2 |
| 119 | |
| 120 | pDocShell->SetGreekCharStyle(1); // mode 1 |
| 121 | checkMathVariant(*pDocShell, true, true); |
| 122 | |
| 123 | pDocShell->SetGreekCharStyle(0); // mode 0 |
| 124 | checkMathVariant(*pDocShell, false, false); |
| 125 | } |
| 126 | |
| 127 | void MathMLExportTest::testMaj() |
| 128 | { |
| 129 | loadFromURL(u"private:factory/smath"_ustr); |
| 130 | SfxBaseModel* pModel = dynamic_cast<SfxBaseModel*>(mxComponent.get()); |
| 131 | SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell()); |
| 132 | pDocShell->SetText( |
| 133 | u"maj to { \u0661 } from { \U0001EE0A = \u0660 } { \u0661 over \U0001EE0A }"_ustr); |
| 134 | save(TestFilter::MML); |
| 135 | xmlDocUniquePtr pDoc = parseXml(maTempFile); |
| 136 | CPPUNIT_ASSERT(pDoc); |
| 137 | assertXPath(pDoc, "/m:math/m:semantics/m:mrow/m:munderover/m:mo", "stretchy", u"false"); |
| 138 | assertXPathContent(pDoc, "/m:math/m:semantics/m:mrow/m:munderover/m:mo", u"\U0001EEF0"); |
| 139 | } |
| 140 | |
| 141 | void MathMLExportTest::testHadd() |
| 142 | { |
| 143 | loadFromURL(u"private:factory/smath"_ustr); |
| 144 | SfxBaseModel* pModel = dynamic_cast<SfxBaseModel*>(mxComponent.get()); |
| 145 | SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell()); |
| 146 | pDocShell->SetText(u"hadd to { \U0001EE4E } from { \U0001EE4E } \U0001EE4E"_ustr); |
| 147 | save(TestFilter::MML); |
| 148 | xmlDocUniquePtr pDoc = parseXml(maTempFile); |
| 149 | CPPUNIT_ASSERT(pDoc); |
| 150 | assertXPathContent(pDoc, "/m:math/m:semantics/m:mrow/m:munderover/m:mi", u"\U0001EEF1"); |
| 151 | } |
| 152 | |
| 153 | void MathMLExportTest::testTdf158867() |
| 154 | { |
| 155 | loadFromURL(u"private:factory/smath"_ustr); |
| 156 | SfxBaseModel* pModel = dynamic_cast<SfxBaseModel*>(mxComponent.get()); |
| 157 | SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell()); |
| 158 | pDocShell->SetText(u"1,2 over 2 = 0,65"_ustr); |
| 159 | save(TestFilter::MML); |
| 160 | xmlDocUniquePtr pDoc = parseXml(maTempFile); |
| 161 | CPPUNIT_ASSERT(pDoc); |
| 162 | assertXPathContent(pDoc, "/m:math/m:semantics/m:mrow/m:mfrac/m:mn[1]", u"1,2"); |
| 163 | assertXPathContent(pDoc, "/m:math/m:semantics/m:mrow/m:mfrac/m:mn[2]", u"2"); |
| 164 | assertXPathContent(pDoc, "/m:math/m:semantics/m:mrow/m:mo", u"="); |
| 165 | assertXPathContent(pDoc, "/m:math/m:semantics/m:mrow/m:mn", u"0,65"); |
| 166 | } |
| 167 | |
| 168 | CPPUNIT_TEST_SUITE_REGISTRATION(MathMLExportTest); |
| 169 | |
| 170 | CPPUNIT_PLUGIN_IMPLEMENT(); |
| 171 | |
| 172 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |