File indexing completed on 2024-05-19 16:08:40

0001 /* This file is part of the KDE project
0002    Copyright 2011 Marijn Kruisselbrink <mkruisselbrink@kde.org>
0003    Copyright 2011 Sebastian Sauer <sebastian.sauer@kdab.com>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "TestCell.h"
0022 
0023 #include <KoStore.h>
0024 #include <KoOdfStylesReader.h>
0025 #include <KoOdfLoadingContext.h>
0026 #include <KoShapeLoadingContext.h>
0027 #include <KoDocumentResourceManager.h>
0028 
0029 #include <sheets/Cell.h>
0030 #include <sheets/CellStorage.h>
0031 #include <sheets/Map.h>
0032 #include <sheets/Sheet.h>
0033 #include <sheets/Style.h>
0034 #include <sheets/odf/OdfLoadingContext.h>
0035 #include <sheets/Value.h>
0036 #include <sheets/odf/SheetsOdf.h>
0037 
0038 #include <QTest>
0039 
0040 using namespace Calligra::Sheets;
0041 
0042 KoXmlDocument CellTest::xmlDocument(const QString &content)
0043 {
0044     KoXmlDocument document;
0045     QString xml = "<table:table-cell xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\" xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\" xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\" xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\" >" + content + "</table:table-cell>";
0046     bool ok = document.setContent(xml, true);
0047     return ok ? document : KoXmlDocument();
0048 }
0049 
0050 void CellTest::testRichText()
0051 {
0052     KoOdfStylesReader stylesReader;
0053 
0054     QBuffer buffer;
0055     buffer.open(QIODevice::ReadOnly);
0056     KoStore *store = KoStore::createStore(&buffer, KoStore::Read);
0057 
0058     KoOdfLoadingContext odfContext(stylesReader, store);
0059     Odf::OdfLoadingContext context(odfContext);
0060 
0061     KoDocumentResourceManager documentResources;
0062     KoShapeLoadingContext shapeContext(odfContext, &documentResources);
0063     context.shapeContext = &shapeContext;
0064 
0065     Styles autoStyles;
0066     QString cellStyleName;
0067 
0068     Map map;
0069     Sheet* sheet = map.addNewSheet();
0070     CellStorage* storage = sheet->cellStorage();
0071     storage->setValue(1, 1, Value(1));
0072 
0073     Cell cell = storage->firstInRow(1);
0074     QVERIFY(!cell.isNull());
0075 
0076     { // Test the simple case. Only one paragraph with some simple text.
0077         KoXmlDocument doc = xmlDocument("<text:p>Some text</text:p>");
0078         KoXmlElement e = doc.documentElement();
0079         QVERIFY(!e.isNull());
0080         Odf::loadCellText(&cell, e, context, autoStyles, cellStyleName);
0081         QVERIFY(!cell.isNull());
0082         QVERIFY(!cell.richText());
0083         QVERIFY(cell.userInput().split('\n').count() == 1);
0084     }
0085 
0086     { // Text in the paragraph and in a child text:span means rich-text.
0087         KoXmlDocument doc = xmlDocument("<text:p>First<text:span>Second<text:span>Theird</text:span></text:span></text:p>");
0088         KoXmlElement e = doc.documentElement();
0089         QVERIFY(!e.isNull());
0090         Odf::loadCellText(&cell, e, context, autoStyles, cellStyleName);
0091         QVERIFY(!cell.isNull());
0092         QVERIFY(cell.richText());
0093         QVERIFY(cell.userInput().split('\n').count() == 1);
0094     }
0095 
0096     { // The text:line-break should be translated into a \n newline and since there is no other rich-text it should not be detected as such.
0097         KoXmlDocument doc = xmlDocument("<text:p>First<text:line-break/>Second</text:p>");
0098         KoXmlElement e = doc.documentElement();
0099         QVERIFY(!e.isNull());
0100         Odf::loadCellText(&cell, e, context, autoStyles, cellStyleName);
0101         QVERIFY(!cell.isNull());
0102         QVERIFY(!cell.richText());
0103         QVERIFY(cell.userInput().split('\n').count() == 2);
0104     }
0105 
0106     { // The text:s and text:tab should be translated into space and tabulator. No rich-text else.
0107         KoXmlDocument doc = xmlDocument("<text:p>First<text:s/>Second<text:tab/>Theird</text:p>");
0108         KoXmlElement e = doc.documentElement();
0109         QVERIFY(!e.isNull());
0110         Odf::loadCellText(&cell, e, context, autoStyles, cellStyleName);
0111         QVERIFY(!cell.isNull());
0112         QVERIFY(!cell.richText());
0113         QVERIFY(cell.userInput().split('\n').count() == 1);
0114     }
0115 }
0116 
0117 QTEST_MAIN(CellTest)