File indexing completed on 2024-12-22 03:52:58
0001 /* 0002 SPDX-FileCopyrightText: 2012 Stefan Majewsky <majewsky@gmx.net> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #include "kgamesvgdocumenttest.h" 0008 #include "kgamesvgdocument_p.h" 0009 0010 // own 0011 #include <config-tests.h> 0012 // Qt 0013 #include <QRegularExpression> 0014 #include <QTest> 0015 0016 void tst_KGameSvgDocument::initTestCase() 0017 { 0018 m_svgDom.load(TESTS_PATH "kgamesvgdocumenttest.svg"); 0019 } 0020 0021 void tst_KGameSvgDocument::cleanupTestCase() 0022 { 0023 // Do nothing. 0024 } 0025 0026 void tst_KGameSvgDocument::style() 0027 { 0028 m_svgDom.elementById(QStringLiteral("digitBackground")); 0029 QVERIFY(!m_svgDom.currentNode().isNull()); 0030 0031 // test style() set & get 0032 QString originalStyle = m_svgDom.style(); 0033 m_svgDom.setStyle(QStringLiteral("test")); 0034 QVERIFY(m_svgDom.style() == "test"); 0035 0036 m_svgDom.setStyle(originalStyle); 0037 QVERIFY(m_svgDom.style() == originalStyle); 0038 0039 // test styleProperties() set & get 0040 QHash<QString, QString> originalStyleProperties = m_svgDom.styleProperties(); 0041 m_svgDom.setStyle(QStringLiteral("test")); 0042 QVERIFY(m_svgDom.style() == "test"); 0043 0044 m_svgDom.setStyleProperties(originalStyleProperties, KGameSvgDocument::UseInkscapeOrder); 0045 QVERIFY(m_svgDom.style() == originalStyle); 0046 0047 // test styleProperty() set & get 0048 QString orgiginalStyleProperty = m_svgDom.styleProperty(QStringLiteral("fill")); 0049 0050 m_svgDom.setStyleProperty(QStringLiteral("fill"), QStringLiteral("ffffff")); 0051 QVERIFY(m_svgDom.styleProperty("fill") == "ffffff"); 0052 0053 m_svgDom.setStyle(originalStyle); 0054 QVERIFY(m_svgDom.styleProperty("fill") == orgiginalStyleProperty); 0055 } 0056 0057 void tst_KGameSvgDocument::scale() 0058 { 0059 m_svgDom.elementById(QStringLiteral("0x11")); 0060 QVERIFY(!m_svgDom.currentNode().isNull()); 0061 0062 QTransform originalTransformMatrix = m_svgDom.transformMatrix(); 0063 0064 m_svgDom.scale(2, 2, KGameSvgDocument::ApplyToCurrentMatrix); 0065 0066 QTransform finalMatrix = m_svgDom.transformMatrix(); 0067 0068 double computedX = originalTransformMatrix.m11() * 2; 0069 double errorX = computedX - finalMatrix.m11(); 0070 QVERIFY(errorX < 0.0000001); // if our margin of error is small, we are probably ok 0071 0072 double computedY = originalTransformMatrix.m22() * 2; 0073 double errorY = computedY - finalMatrix.m22(); 0074 QVERIFY(errorY < 0.0000001); // if our margin of error is small, we are probably ok 0075 0076 // This should output a warning message 0077 m_svgDom.scale(1, 0, KGameSvgDocument::ApplyToCurrentMatrix); 0078 } 0079 0080 void tst_KGameSvgDocument::transformRegex() 0081 { 0082 QRegularExpression rx; 0083 0084 rx.setPattern(QRegularExpression::anchoredPattern(WSP)); 0085 QVERIFY(rx.isValid()); 0086 QVERIFY(rx.match(QLatin1String(" ")).hasMatch()); 0087 QVERIFY(!rx.match("").hasMatch()); 0088 0089 rx.setPattern(QRegularExpression::anchoredPattern(WSP_ASTERISK)); 0090 QVERIFY(rx.isValid()); 0091 QVERIFY(rx.match(QLatin1String(" ")).hasMatch()); 0092 QVERIFY(rx.match("").hasMatch()); 0093 QVERIFY(rx.match(" ").hasMatch()); 0094 QVERIFY(rx.match(" ").hasMatch()); 0095 0096 rx.setPattern(QRegularExpression::anchoredPattern(QString(COMMA))); 0097 QVERIFY(rx.isValid()); 0098 QVERIFY(rx.match(",").hasMatch()); 0099 QVERIFY(!rx.match("").hasMatch()); 0100 QVERIFY(!rx.match(" , ").hasMatch()); 0101 0102 rx.setPattern(QRegularExpression::anchoredPattern(COMMA_WSP)); 0103 QVERIFY(rx.isValid()); 0104 QVERIFY(rx.match(" ,").hasMatch()); 0105 QVERIFY(rx.match(" , ").hasMatch()); 0106 QVERIFY(rx.match(QLatin1String(" ")).hasMatch()); 0107 QVERIFY(rx.match(", ").hasMatch()); 0108 QVERIFY(rx.match(",").hasMatch()); 0109 QVERIFY(rx.match(" , ").hasMatch()); 0110 QVERIFY(rx.match(" , ").hasMatch()); 0111 0112 rx.setPattern(QRegularExpression::anchoredPattern(NUMBER)); 0113 QVERIFY(rx.isValid()); 0114 QVERIFY(rx.match("6").hasMatch()); 0115 QVERIFY(rx.match("64").hasMatch()); 0116 QVERIFY(rx.match("5.2").hasMatch()); 0117 QVERIFY(rx.match("0.003").hasMatch()); 0118 QVERIFY(rx.match("5.1234e9").hasMatch()); 0119 QVERIFY(rx.match("5.1234e-9").hasMatch()); 0120 QVERIFY(rx.match("5.1234e-09").hasMatch()); 0121 QVERIFY(rx.match("-6").hasMatch()); 0122 QVERIFY(rx.match("+5.2").hasMatch()); 0123 QVERIFY(rx.match("-0.003").hasMatch()); 0124 QVERIFY(rx.match("-5.1234e9").hasMatch()); 0125 QVERIFY(rx.match("5.186907e-2").hasMatch()); 0126 QVERIFY(rx.match("-5.1234e-09").hasMatch()); 0127 QVERIFY(rx.match("444.71799").hasMatch()); 0128 QVERIFY(rx.match("16.30829").hasMatch()); 0129 QVERIFY(rx.match("0").hasMatch()); 0130 QVERIFY(rx.match("00002").hasMatch()); 0131 QVERIFY(rx.match(".00002").hasMatch()); 0132 0133 rx.setPattern(QRegularExpression::anchoredPattern(OPEN_PARENS)); 0134 QVERIFY(rx.isValid()); 0135 QVERIFY(rx.match("(").hasMatch()); 0136 QVERIFY(!rx.match("").hasMatch()); 0137 QVERIFY(!rx.match("( ").hasMatch()); 0138 0139 rx.setPattern(QRegularExpression::anchoredPattern(CLOSE_PARENS)); 0140 QVERIFY(rx.isValid()); 0141 QVERIFY(rx.match(")").hasMatch()); 0142 QVERIFY(!rx.match("").hasMatch()); 0143 QVERIFY(!rx.match(") ").hasMatch()); 0144 0145 rx.setPattern(QRegularExpression::anchoredPattern(MATRIX)); 0146 QVERIFY(rx.isValid()); 0147 QVERIFY(rx.match("matrix(1,0,0,1,0,0)").hasMatch()); 0148 QVERIFY(rx.match("matrix(5.186907e-2,0,0,5.186907e-2,444.71799,16.30829)").hasMatch()); 0149 QVERIFY(rx.match("matrix ( 5.186907e-2, 0,0, 5.186907e-2,444.71799,16.30829)").hasMatch()); 0150 QVERIFY(rx.match("matrix ( +5.186907e-2, 0 ,0, -5.186907e-2,444.71799, 16.30829)").hasMatch()); 0151 QVERIFY(!rx.match("matrix ( -5.186907e-2, 0 , -5.186907e-2,444.71799, 16.30829)").hasMatch()); 0152 QVERIFY(!rx.match("matrix ( +5.186907e-2, 0 ,0,, -5.186907e-2,444.71799, 16.30829)").hasMatch()); 0153 QVERIFY(rx.match("matrix ( +.5186907e-2, 0 ,0, -5.186907e-2,444.71799, 16.30829)").hasMatch()); 0154 0155 rx.setPattern(QRegularExpression::anchoredPattern(TRANSLATE)); 0156 QVERIFY(rx.isValid()); 0157 QVERIFY(rx.match("translate(10)").hasMatch()); 0158 QVERIFY(rx.match("translate(-10.2345)").hasMatch()); 0159 QVERIFY(rx.match("translate(10, 23)").hasMatch()); 0160 QVERIFY(rx.match("translate(10 , -.765)").hasMatch()); 0161 QVERIFY(!rx.match("translate(10 , -.765.0)").hasMatch()); 0162 QVERIFY(!rx.match("translate(10 , ,-.7650)").hasMatch()); 0163 0164 rx.setPattern(QRegularExpression::anchoredPattern(SCALE)); 0165 QVERIFY(rx.isValid()); 0166 QVERIFY(rx.match("scale(10)").hasMatch()); 0167 QVERIFY(rx.match("scale(-10.2345)").hasMatch()); 0168 QVERIFY(rx.match("scale(10, 23)").hasMatch()); 0169 QVERIFY(rx.match("scale(10 , -.765)").hasMatch()); 0170 QVERIFY(!rx.match("scale(10 , -.765.0)").hasMatch()); 0171 QVERIFY(!rx.match("scale(10 , ,-.7650)").hasMatch()); 0172 0173 rx.setPattern(QRegularExpression::anchoredPattern(ROTATE)); 0174 QVERIFY(rx.isValid()); 0175 QVERIFY(rx.match("rotate(45)").hasMatch()); 0176 QVERIFY(rx.match("rotate(-26.765)").hasMatch()); 0177 QVERIFY(rx.match("rotate(-26.765, 10, 20)").hasMatch()); 0178 QVERIFY(rx.match("rotate(-26.765, 60, .20)").hasMatch()); 0179 QVERIFY(rx.match("rotate(-26.765, -10, 2.0)").hasMatch()); 0180 QVERIFY(!rx.match("rotate(-26.765, -10)").hasMatch()); 0181 0182 rx.setPattern(QRegularExpression::anchoredPattern(SKEW_X)); 0183 QVERIFY(rx.isValid()); 0184 QVERIFY(rx.match("skewX(10)").hasMatch()); 0185 QVERIFY(rx.match("skewX(-210)").hasMatch()); 0186 QVERIFY(rx.match("skewX(+10.123456)").hasMatch()); 0187 QVERIFY(rx.match("skewX(0.00000345)").hasMatch()); 0188 0189 rx.setPattern(QRegularExpression::anchoredPattern(SKEW_Y)); 0190 QVERIFY(rx.isValid()); 0191 QVERIFY(rx.match("skewY(10)").hasMatch()); 0192 QVERIFY(rx.match("skewY(-210)").hasMatch()); 0193 QVERIFY(rx.match("skewY(+10.12346e4)").hasMatch()); 0194 QVERIFY(rx.match("skewY(-0.00000345)").hasMatch()); 0195 0196 rx.setPattern(QRegularExpression::anchoredPattern(TRANSFORM)); 0197 QVERIFY(rx.isValid()); 0198 QVERIFY(rx.match("matrix(5.186907e-2,0,0,5.186907e-2,444.71799,16.30829)").hasMatch()); 0199 QVERIFY(rx.match("translate(10, 23)").hasMatch()); 0200 QVERIFY(rx.match("scale(10 , -.765)").hasMatch()); 0201 QVERIFY(rx.match("rotate(-26.765, 60, .20)").hasMatch()); 0202 QVERIFY(rx.match("skewX(+10.123456)").hasMatch()); 0203 QVERIFY(rx.match("skewY(-0.00000345)").hasMatch()); 0204 QVERIFY(rx.match("skewY(-0.00000345)").hasMatch()); 0205 0206 rx.setPattern(QRegularExpression::anchoredPattern(TRANSFORMS)); 0207 QVERIFY(rx.isValid()); 0208 QVERIFY(rx.match("matrix(5.186907e-2,0,0,5.186907e-2,444.71799,16.30829)").hasMatch()); 0209 QVERIFY(rx.match("rotate(-26.765, 60, .20)").hasMatch()); 0210 QVERIFY(rx.match("translate(-10,-20) scale(2) rotate(45) translate(5,10)").hasMatch()); 0211 QVERIFY(rx.match("translate(-10,-20), scale(2),rotate(45) translate(5,10), matrix(1,2,3,4,5,6)").hasMatch()); 0212 } 0213 0214 void tst_KGameSvgDocument::transform() 0215 { 0216 // Test transform set & get 0217 QString originalTransform = m_svgDom.transform(); 0218 m_svgDom.setTransform(QStringLiteral("test")); 0219 QVERIFY(m_svgDom.transform() == "test"); 0220 0221 m_svgDom.setTransform(originalTransform); 0222 QVERIFY(m_svgDom.transform() == originalTransform); 0223 0224 // test transformMartix() set & get 0225 QTransform originalMatrix = m_svgDom.transformMatrix(); 0226 QTransform null = QTransform(); 0227 m_svgDom.setTransformMatrix(null, KGameSvgDocument::ReplaceCurrentMatrix); 0228 QVERIFY(m_svgDom.transformMatrix() == null); 0229 0230 m_svgDom.setTransformMatrix(originalMatrix, KGameSvgDocument::ReplaceCurrentMatrix); 0231 QVERIFY(m_svgDom.transformMatrix() == originalMatrix); 0232 } 0233 0234 QTEST_MAIN(tst_KGameSvgDocument) 0235 0236 #include "moc_kgamesvgdocumenttest.cpp"