File indexing completed on 2024-05-19 03:50:37
0001 /* 0002 File : AbstractAspectTest.cpp 0003 Project : LabPlot 0004 Description : Tests for AbstractAspect 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2023 Martin Marmsoler <martin.marmsoler@gmail.com> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "AbstractAspectTest.h" 0012 #include "backend/core/Project.h" 0013 #include "backend/spreadsheet/Spreadsheet.h" 0014 #include "backend/worksheet/Worksheet.h" 0015 #include "backend/worksheet/plots/cartesian/CartesianPlot.h" 0016 #include "backend/worksheet/plots/cartesian/XYEquationCurve.h" 0017 0018 #include <QUndoStack> 0019 0020 void AbstractAspectTest::name() { 0021 Project project; 0022 0023 const QString initialName = QStringLiteral("Worksheet"); 0024 const QString secondName = QStringLiteral("New name"); 0025 const QString thirdName = QStringLiteral("Another new name"); 0026 0027 auto* worksheet = new Worksheet(QStringLiteral("Worksheet")); 0028 project.addChild(worksheet); 0029 0030 int aspectDescriptionAboutToChangeCounter = 0; 0031 connect(&project, 0032 &AbstractAspect::aspectDescriptionAboutToChange, 0033 [worksheet, &aspectDescriptionAboutToChangeCounter, initialName, secondName, thirdName](const AbstractAspect* aspect) { 0034 QCOMPARE(aspect, worksheet); 0035 switch (aspectDescriptionAboutToChangeCounter) { 0036 case 0: { 0037 QCOMPARE(aspect->name(), initialName); 0038 break; 0039 } 0040 case 1: { 0041 QCOMPARE(aspect->name(), secondName); 0042 break; 0043 } 0044 case 2: { 0045 QCOMPARE(aspect->name(), thirdName); 0046 break; 0047 } 0048 case 3: { 0049 QCOMPARE(aspect->name(), secondName); 0050 break; 0051 } 0052 case 4: { 0053 QCOMPARE(aspect->name(), initialName); 0054 break; 0055 } 0056 case 5: { 0057 QCOMPARE(aspect->name(), secondName); 0058 break; 0059 } 0060 } 0061 aspectDescriptionAboutToChangeCounter++; 0062 }); 0063 0064 int aspectDescriptionChangedCounter = 0; 0065 connect(&project, 0066 &AbstractAspect::aspectDescriptionChanged, 0067 [worksheet, &aspectDescriptionChangedCounter, initialName, secondName, thirdName](const AbstractAspect* aspect) { 0068 QCOMPARE(aspect, worksheet); 0069 switch (aspectDescriptionChangedCounter) { 0070 case 0: { 0071 QCOMPARE(aspect->name(), secondName); 0072 break; 0073 } 0074 case 1: { 0075 QCOMPARE(aspect->name(), thirdName); 0076 break; 0077 } 0078 case 2: { 0079 QCOMPARE(aspect->name(), secondName); 0080 break; 0081 } 0082 case 3: { 0083 QCOMPARE(aspect->name(), initialName); 0084 break; 0085 } 0086 case 4: { 0087 QCOMPARE(aspect->name(), secondName); 0088 break; 0089 } 0090 case 5: { 0091 QCOMPARE(aspect->name(), thirdName); 0092 break; 0093 } 0094 } 0095 aspectDescriptionChangedCounter++; 0096 }); 0097 0098 worksheet->setName(secondName); 0099 worksheet->setName(thirdName); 0100 0101 worksheet->undoStack()->undo(); 0102 QCOMPARE(worksheet->name(), secondName); 0103 worksheet->undoStack()->undo(); 0104 QCOMPARE(worksheet->name(), initialName); 0105 0106 worksheet->undoStack()->redo(); 0107 QCOMPARE(worksheet->name(), secondName); 0108 worksheet->undoStack()->redo(); 0109 QCOMPARE(worksheet->name(), thirdName); 0110 0111 QCOMPARE(aspectDescriptionAboutToChangeCounter, 6); 0112 QCOMPARE(aspectDescriptionChangedCounter, 6); 0113 } 0114 0115 void AbstractAspectTest::copyPaste() { 0116 Project project; 0117 0118 auto* worksheet = new Worksheet(QStringLiteral("Worksheet")); 0119 project.addChild(worksheet); 0120 0121 auto* plot = new CartesianPlot(QStringLiteral("plot")); 0122 worksheet->addChild(plot); 0123 plot->setType(CartesianPlot::Type::TwoAxes); // Otherwise no axis are created 0124 plot->setNiceExtend(false); 0125 0126 auto* equationCurve{new XYEquationCurve(QStringLiteral("f(x)"))}; 0127 equationCurve->setCoordinateSystemIndex(plot->defaultCoordinateSystemIndex()); 0128 plot->addChild(equationCurve); 0129 0130 XYEquationCurve::EquationData data; 0131 data.min = QStringLiteral("0"); 0132 data.max = QStringLiteral("1"); 0133 data.count = 10; 0134 data.expression1 = QStringLiteral("x"); 0135 equationCurve->setEquationData(data); 0136 equationCurve->recalculate(); 0137 0138 worksheet->copy(); 0139 0140 project.paste(); 0141 0142 const auto& worksheets = project.children(AspectType::Worksheet); 0143 QCOMPARE(worksheets.count(), 2); 0144 QVERIFY(worksheets.at(0)->uuid() != worksheets.at(1)->uuid()); 0145 0146 const auto& childrenWorksheet1 = 0147 worksheets.at(0)->children(AspectType::AbstractAspect, {AbstractAspect::ChildIndexFlag::IncludeHidden, AbstractAspect::ChildIndexFlag::Recursive}); 0148 const auto& childrenWorksheet2 = 0149 worksheets.at(1)->children(AspectType::AbstractAspect, {AbstractAspect::ChildIndexFlag::IncludeHidden, AbstractAspect::ChildIndexFlag::Recursive}); 0150 0151 QCOMPARE(childrenWorksheet1.count(), childrenWorksheet2.count()); 0152 0153 for (int i = 0; i < childrenWorksheet1.count(); i++) { 0154 QVERIFY(childrenWorksheet1.at(i)->type() == childrenWorksheet2.at(i)->type()); 0155 if (childrenWorksheet1.at(i)->type() == AspectType::AbstractAspect) 0156 continue; // unique will change the triggered for those aspects, and therefore when changing for the second from "1" to "2" it happens that "2" 0157 // already exists and therefore it receives "3" 0158 QVERIFY(childrenWorksheet1.at(i)->name() == childrenWorksheet2.at(i)->name()); 0159 QVERIFY(childrenWorksheet1.at(i)->uuid() != childrenWorksheet2.at(i)->uuid()); 0160 } 0161 } 0162 0163 void AbstractAspectTest::saveLoad() { 0164 Project project; 0165 0166 auto* worksheet = new Worksheet(QStringLiteral("Worksheet")); 0167 project.addChild(worksheet); 0168 0169 auto* plot = new CartesianPlot(QStringLiteral("plot")); 0170 worksheet->addChild(plot); 0171 plot->setType(CartesianPlot::Type::TwoAxes); // Otherwise no axis are created 0172 plot->setNiceExtend(false); 0173 0174 auto* equationCurve{new XYEquationCurve(QStringLiteral("f(x)"))}; 0175 equationCurve->setCoordinateSystemIndex(plot->defaultCoordinateSystemIndex()); 0176 plot->addChild(equationCurve); 0177 0178 XYEquationCurve::EquationData data; 0179 data.min = QStringLiteral("0"); 0180 data.max = QStringLiteral("1"); 0181 data.count = 10; 0182 data.expression1 = QStringLiteral("x"); 0183 equationCurve->setEquationData(data); 0184 equationCurve->recalculate(); 0185 0186 QString savePath; 0187 SAVE_PROJECT("testLinkSpreadsheetSaveLoad") 0188 0189 Project project2; 0190 QCOMPARE(project2.load(savePath), true); 0191 0192 const auto& childrenProject1 = 0193 project.children(AspectType::AbstractAspect, {AbstractAspect::ChildIndexFlag::IncludeHidden, AbstractAspect::ChildIndexFlag::Recursive}); 0194 const auto& childrenProject2 = 0195 project2.children(AspectType::AbstractAspect, {AbstractAspect::ChildIndexFlag::IncludeHidden, AbstractAspect::ChildIndexFlag::Recursive}); 0196 0197 QCOMPARE(childrenProject1.count(), childrenProject2.count()); 0198 0199 for (int i = 0; i < childrenProject1.count(); i++) { 0200 QVERIFY(childrenProject1.at(i)->type() == childrenProject2.at(i)->type()); 0201 if (childrenProject1.at(i)->type() == AspectType::AbstractAspect) 0202 continue; // Usually they don't implement the write basic function and therefore they cannot have the same uuid 0203 0204 QVERIFY(childrenProject1.at(i)->name() == childrenProject2.at(i)->name()); 0205 0206 if (childrenProject1.at(i)->path().contains(QStringLiteral("Project/Worksheet/plot/f(x)/x")) 0207 || childrenProject1.at(i)->path().contains(QStringLiteral("Project/Worksheet/plot/f(x)/y"))) 0208 continue; // The columns of the quation curve are not saved 0209 QVERIFY(childrenProject1.at(i)->uuid() == childrenProject2.at(i)->uuid()); 0210 } 0211 } 0212 0213 void AbstractAspectTest::moveUp() { 0214 Project project; 0215 0216 auto* worksheet = new Worksheet(QStringLiteral("Worksheet")); 0217 project.addChild(worksheet); 0218 0219 auto* spreadsheet = new Spreadsheet(QStringLiteral("Spreadsheet")); 0220 project.addChild(spreadsheet); 0221 0222 // check the order of children 0223 QCOMPARE(project.child<AbstractAspect>(0), worksheet); 0224 QCOMPARE(project.child<AbstractAspect>(1), spreadsheet); 0225 0226 // move the spreadsheet in front of the worksheet and check the order again 0227 spreadsheet->moveUp(); 0228 QCOMPARE(project.child<AbstractAspect>(0), spreadsheet); 0229 QCOMPARE(project.child<AbstractAspect>(1), worksheet); 0230 } 0231 0232 void AbstractAspectTest::moveDown() { 0233 Project project; 0234 0235 auto* worksheet = new Worksheet(QStringLiteral("Worksheet")); 0236 project.addChild(worksheet); 0237 0238 auto* spreadsheet = new Spreadsheet(QStringLiteral("Spreadsheet")); 0239 project.addChild(spreadsheet); 0240 0241 // check the order of children 0242 QCOMPARE(project.child<AbstractAspect>(0), worksheet); 0243 QCOMPARE(project.child<AbstractAspect>(1), spreadsheet); 0244 0245 // move the worksheet behing the speadsheet and check the order again 0246 worksheet->moveDown(); 0247 QCOMPARE(project.child<AbstractAspect>(0), spreadsheet); 0248 QCOMPARE(project.child<AbstractAspect>(1), worksheet); 0249 } 0250 0251 QTEST_MAIN(AbstractAspectTest)