File indexing completed on 2024-05-12 16:35:58

0001 /* This file is part of the KDE project
0002    Copyright 2010 Marijn Kruisselbrink <mkruisselbrink@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 #include "TestSheet.h"
0020 
0021 #include "MockPart.h"
0022 
0023 #include <KoViewConverter.h>
0024 #include <KoShape.h>
0025 #include <KoShapeSavingContext.h>
0026 #include <KoXmlWriter.h>
0027 #include <KoGenStyles.h>
0028 #include <KoEmbeddedDocumentSaver.h>
0029 
0030 #include <part/Doc.h> // FIXME detach from part
0031 #include <Map.h>
0032 #include <Sheet.h>
0033 #include <CellStorage.h>
0034 
0035 #include <QPainter>
0036 #include <QTest>
0037 
0038 using namespace Calligra::Sheets;
0039 
0040 class MockShape : public KoShape
0041 {
0042 public:
0043     MockShape() : KoShape() {}
0044     void paint(QPainter &painter, const KoViewConverter &converter, KoShapePaintingContext &) override {
0045         Q_UNUSED(painter);
0046         Q_UNUSED(converter);
0047     }
0048     void saveOdf(KoShapeSavingContext &) const override {}
0049     bool loadOdf(const KoXmlElement &, KoShapeLoadingContext &) override {
0050         return true;
0051     }
0052 };
0053 
0054 void SheetTest::init()
0055 {
0056     m_doc = new Doc(new MockPart);
0057     m_doc->map()->addNewSheet();
0058     m_sheet = m_doc->map()->sheet(0);
0059     m_sheet->map()->setDefaultRowHeight(10.0);
0060     m_sheet->map()->setDefaultColumnWidth(10.0);
0061 }
0062 
0063 void SheetTest::cleanup()
0064 {
0065     delete m_doc;
0066 }
0067 
0068 void SheetTest::testRemoveRows_data()
0069 {
0070     QTest::addColumn<int>("col");
0071     QTest::addColumn<int>("row");
0072     QTest::addColumn<QString>("formula");
0073     QTest::addColumn<int>("rowToRemove");
0074     QTest::addColumn<int>("numRows");
0075     QTest::addColumn<QString>("result");
0076 
0077     QTest::newRow("exact row")   << 1 << 1 << "=C4" << 4 << 1 << "=#Dependency!";
0078     QTest::newRow("earlier row") << 1 << 1 << "=C4" << 3 << 1 << "=C3";
0079     QTest::newRow("later row")   << 1 << 1 << "=C4" << 5 << 1 << "=C4";
0080 
0081     QTest::newRow("range before start") << 1 << 1 << "=SUM(C4:C7)" << 3 << 1 << "=SUM(C3:C6)";
0082     QTest::newRow("range start row")    << 1 << 1 << "=SUM(C4:C7)" << 4 << 1 << "=SUM(C4:C6)";
0083     QTest::newRow("range middle row")   << 1 << 1 << "=SUM(C4:C7)" << 5 << 1 << "=SUM(C4:C6)";
0084     QTest::newRow("range end row")      << 1 << 1 << "=SUM(C4:C7)" << 7 << 1 << "=SUM(C4:C6)";
0085     QTest::newRow("range after end")    << 1 << 1 << "=SUM(C4:C7)" << 8 << 1 << "=SUM(C4:C7)";
0086     QTest::newRow("entire range")       << 1 << 1 << "=SUM(C4:C4)" << 4 << 1 << "=SUM(#Dependency!:#Dependency!)";
0087 
0088     QTest::newRow("2d range before start") << 1 << 1 << "=SUM(C4:E7)" << 3 << 1 << "=SUM(C3:E6)";
0089     QTest::newRow("2d range start row")    << 1 << 1 << "=SUM(C4:E7)" << 4 << 1 << "=SUM(C4:E6)";
0090     QTest::newRow("2d range middle row")   << 1 << 1 << "=SUM(C4:E7)" << 5 << 1 << "=SUM(C4:E6)";
0091     QTest::newRow("2d range end row")      << 1 << 1 << "=SUM(C4:E7)" << 7 << 1 << "=SUM(C4:E6)";
0092     QTest::newRow("2d range after end")    << 1 << 1 << "=SUM(C4:E7)" << 8 << 1 << "=SUM(C4:E7)";
0093 
0094     QTest::newRow("refer to last deleted row")      << 1 << 1 << "=A4" << 2 << 3 << "=#Dependency!";
0095     QTest::newRow("refer to first not deleted row") << 1 << 1 << "=A4" << 2 << 2 << "=A2";
0096 
0097     // Bug 313056
0098     QTest::newRow("bug313056_1") << 4 << 5 << "=A1" << 3 << 2 << "=A1";
0099     QTest::newRow("bug313056_2") << 2 << 32 << "=E9" << 5 << 26 << "=#Dependency!";
0100 }
0101 
0102 void SheetTest::testRemoveRows()
0103 {
0104     QFETCH(int, col);
0105     QFETCH(int, row);
0106     QFETCH(QString, formula);
0107     QFETCH(int, rowToRemove);
0108     QFETCH(int, numRows);
0109     QFETCH(QString, result);
0110 
0111     Cell cell(m_sheet, col, row);
0112     cell.setUserInput(formula);
0113     m_sheet->removeRows(rowToRemove, numRows);
0114 
0115     QCOMPARE(cell.userInput(), result);
0116 }
0117 
0118 void SheetTest::testRemoveColumns_data()
0119 {
0120     QTest::addColumn<QString>("formula");
0121     QTest::addColumn<int>("columnToRemove");
0122     QTest::addColumn<QString>("result");
0123 
0124     QTest::newRow("exact col")   << "=C4" << 3 << "=#Dependency!";
0125     QTest::newRow("earlier col") << "=C4" << 2 << "=B4";
0126     QTest::newRow("later col")   << "=C4" << 4 << "=C4";
0127 
0128     QTest::newRow("range before start") << "=SUM(C4:E4)" << 2 << "=SUM(B4:D4)";
0129     QTest::newRow("range start row")    << "=SUM(C4:E4)" << 3 << "=SUM(C4:D4)";
0130     QTest::newRow("range middle row")   << "=SUM(C4:E4)" << 4 << "=SUM(C4:D4)";
0131     QTest::newRow("range end row")      << "=SUM(C4:E4)" << 5 << "=SUM(C4:D4)";
0132     QTest::newRow("range after end")    << "=SUM(C4:E4)" << 6 << "=SUM(C4:E4)";
0133     QTest::newRow("entire range")       << "=SUM(C4:C4)" << 3 << "=SUM(#Dependency!:#Dependency!)";
0134 
0135     QTest::newRow("2d range before start") << "=SUM(C4:E7)" << 2 << "=SUM(B4:D7)";
0136     QTest::newRow("2d range start row")    << "=SUM(C4:E7)" << 3 << "=SUM(C4:D7)";
0137     QTest::newRow("2d range middle row")   << "=SUM(C4:E7)" << 4 << "=SUM(C4:D7)";
0138     QTest::newRow("2d range end row")      << "=SUM(C4:E7)" << 5 << "=SUM(C4:D7)";
0139     QTest::newRow("2d range after end")    << "=SUM(C4:E7)" << 6 << "=SUM(C4:E7)";
0140 }
0141 
0142 void SheetTest::testRemoveColumns()
0143 {
0144     QFETCH(QString, formula);
0145     QFETCH(int, columnToRemove);
0146     QFETCH(QString, result);
0147 
0148     Cell cell(m_sheet, 1, 1);
0149     cell.setUserInput(formula);
0150     m_sheet->removeColumns(columnToRemove, 1);
0151 
0152     QCOMPARE(cell.userInput(), result);
0153 }
0154 
0155 
0156 void SheetTest::testDocumentToCellCoordinates_data()
0157 {
0158     QTest::addColumn<QRectF>("area");
0159     QTest::addColumn<QRect>("result");
0160 
0161     QTest::newRow("simple") << QRectF(5, 5, 10, 10) << QRect(1, 1, 2, 2);
0162     QTest::newRow("bigger") << QRectF(5, 5, 200, 100) << QRect(1, 1, 21, 11);
0163     QTest::newRow("wide") << QRectF(5, 5, 300000, 10) << QRect(1, 1, 30001, 2);
0164     QTest::newRow("tall") << QRectF(5, 5, 10, 300000) << QRect(1, 1, 2, 30001);
0165     QTest::newRow("very tall") << QRectF(5, 5, 10, 10000000) << QRect(1, 1, 2, 1000001);
0166 }
0167 
0168 void SheetTest::testDocumentToCellCoordinates()
0169 {
0170     QFETCH(QRectF, area);
0171     QFETCH(QRect, result);
0172 
0173     QCOMPARE(m_sheet->documentToCellCoordinates(area), result);
0174 }
0175 
0176 #if 0
0177 // test if embedded objects are prepare taken into account (tests for bug 287997)
0178 void SheetTest::testCompareRows()
0179 {
0180     CellStorage* storage = m_sheet->cellStorage();
0181     storage->insertRows(1, 15);
0182 
0183     QBuffer buf;
0184     buf.open(QIODevice::ReadWrite);
0185     KoXmlWriter xmlWriter(&buf);
0186     KoGenStyles mainStyles;
0187     KoEmbeddedDocumentSaver embeddedSaver;
0188     KoShapeSavingContext shapeContext(xmlWriter, mainStyles, embeddedSaver);
0189     OdfSavingContext tableContext(shapeContext);
0190 
0191     MockShape shape;
0192     tableContext.insertCellAnchoredShape(m_sheet, 20, 5, &shape);
0193 
0194     QCOMPARE(m_sheet->compareRows(12,19,1024,tableContext), true);
0195     QCOMPARE(m_sheet->compareRows(12,20,1024,tableContext), false);
0196     QCOMPARE(m_sheet->compareRows(12,21,1024,tableContext), true);
0197 
0198     storage->insertRows(1, 15);
0199 
0200     QCOMPARE(m_sheet->compareRows(12,19,1024,tableContext), true);
0201     QCOMPARE(m_sheet->compareRows(12,20,1024,tableContext), false);
0202     QCOMPARE(m_sheet->compareRows(12,21,1024,tableContext), true);
0203 }
0204 #endif
0205 
0206 QTEST_MAIN(SheetTest)