File indexing completed on 2025-01-12 04:18:15
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2015 Jarosław Staniek <staniek@kde.org> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 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 * Lesser General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Lesser General Public 0015 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 0016 */ 0017 0018 #include "ElementsTest.h" 0019 #include "KReportTestUtils.h" 0020 #include <KReportElement> 0021 #include <KReportLabelElement> 0022 #include <KReportSection> 0023 #include <KReportUnit> 0024 0025 #include <QRegularExpression> 0026 #include <QTest> 0027 0028 QTEST_MAIN(ElementsTest) 0029 0030 Q_DECLARE_METATYPE(KReportElement) 0031 Q_DECLARE_METATYPE(KReportLabelElement) 0032 Q_DECLARE_METATYPE(KReportSection) 0033 0034 void ElementsTest::testElements() 0035 { 0036 KReportElement e; 0037 QCOMPARE(e, e); 0038 QCOMPARE(e.name(), ""); 0039 QCOMPARE(e.rect(), QRectF()); 0040 QCOMPARE(e.z(), 0.0); 0041 QCOMPARE(e.backgroundColor(), QColor()); 0042 QCOMPARE(e.foregroundColor(), QColor()); 0043 QCOMPARE(e.backgroundOpacity(), 0.0); 0044 0045 e.setZ(10.3); 0046 QCOMPARE(e.z(), 10.3); 0047 e.setName("element1"); 0048 QCOMPARE(e.name(), "element1"); 0049 e.setRect(QRect(1, 2, 3, 4)); 0050 QCOMPARE(e.rect(), QRectF(1, 2, 3, 4)); 0051 e.setBackgroundColor(Qt::red); 0052 QCOMPARE(e.backgroundColor(), QColor(Qt::red)); 0053 e.setForegroundColor(Qt::white); 0054 QCOMPARE(e.foregroundColor(), QColor(Qt::white)); 0055 QCOMPARE(e, e); 0056 0057 KReportElement e2 = e; 0058 e.setName(""); 0059 QCOMPARE(e, e2); 0060 QCOMPARE(e.name(), e2.name()); 0061 QCOMPARE(e.rect(), e2.rect()); 0062 0063 e2.setName("element2"); 0064 KReportLabelElement lbl1(e2); 0065 QCOMPARE(e2.name(), lbl1.name()); 0066 e2.setName(""); 0067 QCOMPARE(lbl1.name(), "element2"); 0068 0069 lbl1.setText("label1"); 0070 QCOMPARE(lbl1.text(), "label1"); 0071 e = lbl1; 0072 0073 KReportLabelElement lbl2(e); // e points to lbl1 so shares lbl1 in fact 0074 QCOMPARE(lbl1, lbl2); 0075 QCOMPARE(KReportLabelElement(e), lbl2); 0076 lbl2 = e; 0077 QCOMPARE(KReportLabelElement(e), lbl2); 0078 0079 lbl1 = KReportLabelElement(); 0080 QCOMPARE(lbl1.alignment(), Qt::AlignLeft | Qt::AlignVCenter); // defaults 0081 lbl1.setAlignment(Qt::AlignHCenter|Qt::AlignBottom); 0082 QCOMPARE(lbl1.alignment(), Qt::AlignHCenter|Qt::AlignBottom); 0083 0084 lbl1.setBackgroundOpacity(20.0); 0085 QCOMPARE(lbl1.backgroundOpacity(), 1.0); // 1.0 is max 0086 lbl1.setBackgroundOpacity(-100.10); 0087 QCOMPARE(lbl1.backgroundOpacity(), 0.0); // 0.0 is min 0088 0089 lbl1.setFont(QApplication::font()); 0090 QFont f = lbl1.font(); 0091 QCOMPARE(f, QApplication::font()); 0092 0093 QCOMPARE(lbl1.borderStyle().weight(), 0.0); // default 0094 QCOMPARE(lbl1.borderStyle().penStyle(), Qt::NoPen); // default 0095 QCOMPARE(lbl1.borderStyle().color(), QColor()); // default 0096 KReportLineStyle lineStyle; 0097 lineStyle.setWeight(2.0); 0098 lineStyle.setPenStyle(Qt::DashLine); 0099 lineStyle.setColor(QColor("brown")); 0100 lbl1.setBorderStyle(lineStyle); 0101 QCOMPARE(lbl1.borderStyle(), lineStyle); 0102 lineStyle.setColor(Qt::yellow); 0103 QCOMPARE(lbl1.borderStyle().color(), QColor(Qt::yellow)); // shared 0104 } 0105 0106 void ElementsTest::testElementCloning() 0107 { 0108 KReportLabelElement lbl1("text1"), lbl2; 0109 lbl1.setName("label1"); 0110 lbl2 = lbl1.clone(); 0111 QVERIFY(lbl1 != lbl2); // a deep copy, not equal 0112 lbl1.setText(""); 0113 QCOMPARE(lbl1.name(), lbl2.name()); 0114 QCOMPARE(lbl1.text(), ""); 0115 QCOMPARE(lbl2.text(), "text1"); // this is another copy, not affected by lbl1.setText("") 0116 0117 lbl1.setName("label1"); 0118 lbl1.setText("text1"); 0119 KReportElement e1 = lbl1; 0120 KReportElement e2 = e1.clone(); 0121 QVERIFY2(e1 != e2, "Cloned element not equal to the original"); 0122 QCOMPARE(e1.name(), "label1"); 0123 QCOMPARE(e2.name(), "label1"); 0124 QVERIFY2(KReportLabelElement(e2) != lbl1, "Reference to original not equal to a clone"); 0125 QVERIFY2(KReportLabelElement(e2) != KReportLabelElement(e1), "Reference to original not equal to reference to a clone"); 0126 lbl1.setName(""); 0127 lbl1.setText(""); 0128 QCOMPARE(e1.name(), ""); // shared with lbl1 0129 QCOMPARE(e2.name(), "label1"); // shared with lbl1's clone 0130 QCOMPARE(KReportLabelElement(e1).text(), ""); 0131 QCOMPARE(KReportLabelElement(e2).text(), "text1"); 0132 KReportLabelElement lbl3 = e2; 0133 0134 // e2 points to a lbl1's clone with name=label1, text=text1, so: 0135 KReportLabelElement(e2).setText("text2"); // This affect e2 and lbl2 because 0136 // KReportLabelElement(e2) upcasts it to the KReportLabelElement type 0137 QCOMPARE(KReportLabelElement(e2).text(), "text2"); 0138 QCOMPARE(lbl3.text(), "text2"); 0139 QCOMPARE(lbl1.text(), ""); // lbl1 unaffected 0140 } 0141 0142 void ElementsTest::testSections() 0143 { 0144 // types 0145 KReportSection sec1; 0146 QCOMPARE(sec1.type(), KReportSection::Type::Invalid); 0147 QCOMPARE(KReportSection(sec1).type(), KReportSection::Type::Invalid); 0148 sec1.setType(KReportSection::Type::PageFooterFirst); 0149 KReportSection sec2(sec1); 0150 QCOMPARE(sec1.type(), sec2.type()); 0151 QCOMPARE(sec1, sec2); 0152 sec1.setType(KReportSection::Type::PageFooterEven); 0153 QCOMPARE(sec1.type(), sec2.type()); // shared 0154 QCOMPARE(sec1, sec2); // shared 0155 0156 // height 0157 KReportSection::setDefaultHeight(-1); // sanity: reset 0158 QCOMPARE(sec1.height(), KReportSection::defaultHeight()); // initially: default height 0159 KReportSection::setDefaultHeight(CM_TO_POINT(10.0)); // sanity: reset 0160 QCOMPARE(KReportSection::defaultHeight(), CM_TO_POINT(10.0)); 0161 QCOMPARE(sec1.height(), KReportSection::defaultHeight()); // still default height 0162 KReportSection::setDefaultHeight(-1); // reset 0163 QCOMPARE(sec1.height(), KReportSection::defaultHeight()); // still default height 0164 sec1.setHeight(CM_TO_POINT(7.0)); 0165 QCOMPARE(sec1.height(), CM_TO_POINT(7.0)); // custom height 0166 0167 // background color 0168 KReportSection::setDefaultBackgroundColor(QColor()); // sanity: reset 0169 QCOMPARE(sec1.backgroundColor(), KReportSection::defaultBackgroundColor()); // initially: default color 0170 KReportSection::setDefaultBackgroundColor(Qt::yellow); // sanity: reset 0171 QCOMPARE(KReportSection::defaultBackgroundColor(), QColor(Qt::yellow)); 0172 QCOMPARE(sec1.backgroundColor(), KReportSection::defaultBackgroundColor()); // still default color 0173 KReportSection::setDefaultBackgroundColor(QColor()); // reset 0174 QCOMPARE(sec1.backgroundColor(), KReportSection::defaultBackgroundColor()); // still default color 0175 sec1.setBackgroundColor(Qt::lightGray); 0176 QCOMPARE(sec1.backgroundColor(), QColor(Qt::lightGray)); // custom color 0177 0178 // elements 0179 QVERIFY2(sec1.elements().isEmpty(), "Initially section has no elements inside"); 0180 QVERIFY2(sec2.elements().isEmpty(), "Initially section has no elements inside"); 0181 KReportLabelElement lbl1("text1"); 0182 QVERIFY(lbl1.name().isEmpty()); 0183 QVERIFY(sec1.addElement(lbl1)); 0184 QCOMPARE(sec1.elements().count(), 1); 0185 QCOMPARE(sec2.elements().count(), 1); 0186 QCOMPARE(sec1.elements().first(), sec1.elements().last()); 0187 QCOMPARE(KReportLabelElement(sec1.elements().first()), lbl1); 0188 QCOMPARE(KReportLabelElement(sec1.elements().at(0)), lbl1); 0189 0190 QRegularExpression sectionAlreadyContainsElement("Section already contains element KReportElement: name=\"\".*"); 0191 QTest::ignoreMessage(QtWarningMsg, sectionAlreadyContainsElement); 0192 QVERIFY2(!sec1.addElement(lbl1), "Adding the same element twice isn't possible"); 0193 QCOMPARE(sec1.elements().count(), 1); 0194 KReportLabelElement lbl2("text2"); 0195 QTest::ignoreMessage(QtWarningMsg, 0196 QRegularExpression("Could not find element KReportElement: name=\"\".* in section")); 0197 QVERIFY2(!sec1.removeElement(lbl2), "Removing not added element isn't possible"); 0198 QCOMPARE(sec1.elements().count(), 1); 0199 QVERIFY(sec1.removeElement(lbl1)); 0200 QCOMPARE(sec1.elements().count(), 0); 0201 QCOMPARE(sec2.elements().count(), 0); 0202 0203 lbl2 = lbl1.clone(); 0204 QVERIFY(sec1.addElement(lbl1)); 0205 QVERIFY(sec1.addElement(lbl2)); 0206 QCOMPARE(sec1.elements().count(), 2); 0207 QCOMPARE(sec2.elements().count(), 2); 0208 0209 // insert 0210 sec1 = KReportSection(); 0211 QVERIFY(sec1.insertElement(0, lbl1)); 0212 QCOMPARE(sec1.elements().count(), 1); 0213 QTest::ignoreMessage(QtWarningMsg, sectionAlreadyContainsElement); 0214 QVERIFY2(!sec1.insertElement(0, lbl1), "Cannot insert the same element twice"); 0215 const QString couldNotInsertElementMessage("Could not insert element KReportElement: name=\"\".* at index %1 into section"); 0216 QTest::ignoreMessage(QtWarningMsg, QRegularExpression(couldNotInsertElementMessage.arg(2))); 0217 QVERIFY2(!sec1.insertElement(2, lbl2), "Cannot insert element at position 2"); 0218 QCOMPARE(sec1.elements().count(), 1); 0219 QTest::ignoreMessage(QtWarningMsg, QRegularExpression(couldNotInsertElementMessage.arg(-1))); 0220 QVERIFY2(!sec1.insertElement(-1, lbl2), "Cannot insert element at position -1"); 0221 QCOMPARE(sec1.elements().count(), 1); 0222 QVERIFY(sec1.insertElement(0, lbl2)); 0223 QCOMPARE(sec1.elements().count(), 2); 0224 0225 // indexOf 0226 QCOMPARE(sec1.elements().indexOf(lbl1), 1); 0227 QCOMPARE(sec1.elements().indexOf(lbl2), 0); 0228 QCOMPARE(KReportLabelElement(sec1.elements().last()), lbl1); 0229 0230 // removeAt 0231 const QString couldNotFindElementMessage("Could not find element at index %1 in section"); 0232 QTest::ignoreMessage(QtWarningMsg, QRegularExpression(couldNotFindElementMessage.arg(2))); 0233 QVERIFY2(!sec1.removeElementAt(2), "Cannot remove element at position 2"); 0234 QTest::ignoreMessage(QtWarningMsg, QRegularExpression(couldNotFindElementMessage.arg(-1))); 0235 QVERIFY2(!sec1.removeElementAt(-1), "Cannot remove element at position -1"); 0236 QVERIFY(sec1.removeElementAt(1)); 0237 QCOMPARE(sec1.elements().count(), 1); 0238 QVERIFY(sec1.removeElementAt(0)); 0239 QCOMPARE(sec1.elements().count(), 0); 0240 0241 // cloning sections 0242 sec1 = KReportSection(); 0243 QVERIFY(sec1.addElement(lbl1)); 0244 QVERIFY(sec1.addElement(lbl2)); 0245 sec2 = sec1.clone(); 0246 QCOMPARE(sec1.elements().count(), sec2.elements().count()); 0247 QVERIFY2(sec1.elements() != sec2.elements(), "Cloned sections have different sets of elements"); 0248 QVERIFY2(sec1 != sec2, "Cloned section not equal to the original"); 0249 QVERIFY2(sec1.elements().first() != sec2.elements().first(), "Elements of cloned sections have different sets of elements"); 0250 }