File indexing completed on 2024-04-21 04:34:22
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (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 0013 * GNU 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, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 #include "TestMetaPos.h" 0020 0021 #include <QtTest/QTest> 0022 0023 #include <tikz/core/Document.h> 0024 #include <tikz/core/MetaPos.h> 0025 #include <tikz/core/Node.h> 0026 0027 QTEST_MAIN(MetaPosTest) 0028 0029 void MetaPosTest::initTestCase() 0030 { 0031 } 0032 0033 void MetaPosTest::cleanupTestCase() 0034 { 0035 } 0036 0037 void MetaPosTest::testMetaPos() 0038 { 0039 tikz::core::Document doc; 0040 tikz::core::MetaPos m(&doc); 0041 0042 // initially (0, 0) 0043 QCOMPARE(m.pos(), tikz::Pos(0, 0)); 0044 QCOMPARE(m.node(), (tikz::core::Node*)nullptr); 0045 QVERIFY(m == m); 0046 0047 // test changed signal 0048 connect(m.notificationObject(), SIGNAL(changed(tikz::core::MetaPos*)), 0049 this, SLOT(changedEmitted())); 0050 0051 // reset signal counter 0052 m_changeCount = 0; 0053 0054 // perform change 0055 m.setPos(tikz::Pos(1, 1)); 0056 QCOMPARE(m.pos(), tikz::Pos(1, 1)); 0057 QVERIFY(m == m); 0058 0059 // make sure signals are emitted only once 0060 QCOMPARE(m_changeCount, 1); 0061 0062 // make sure setNode with null pointer has no effect 0063 QVERIFY( ! m.setNode(nullptr)); 0064 QVERIFY(m == m); 0065 QCOMPARE(m_changeCount, 1); 0066 } 0067 0068 void MetaPosTest::changedEmitted() 0069 { 0070 ++m_changeCount; 0071 } 0072 0073 void MetaPosTest::testMetaPosWithNode() 0074 { 0075 tikz::core::Document doc; 0076 tikz::core::MetaPos m(&doc); 0077 auto n = doc.createEntity<tikz::core::Node>(tikz::EntityType::Node); 0078 0079 // set distinct node position 0080 n->setPos(tikz::Pos(5, 5)); 0081 QCOMPARE(n->pos(), tikz::Pos(5, 5)); 0082 0083 // connect to changed signal 0084 connect(m.notificationObject(), SIGNAL(changed(tikz::core::MetaPos*)), 0085 this, SLOT(changedEmitted())); 0086 0087 // reset changed counter 0088 m_changeCount = 0; 0089 0090 // check setting node 0091 QVERIFY(m.setNode(n)); 0092 QCOMPARE(m.node(), n); 0093 QVERIFY(m == m); 0094 QCOMPARE(m_changeCount, 1); 0095 0096 // make sure setting same node again does nothing 0097 QVERIFY(! m.setNode(n)); 0098 QCOMPARE(m_changeCount, 1); 0099 0100 // should be same as node pos 0101 QCOMPARE(m.pos(), tikz::Pos(5, 5)); 0102 QVERIFY(m == m); 0103 0104 // reset signal counter 0105 m_changeCount = 0; 0106 0107 // perform change 0108 m.setPos(tikz::Pos(1, 1)); 0109 QCOMPARE(m.pos(), tikz::Pos(1, 1)); 0110 QVERIFY(m == m); 0111 0112 // using setPos() resets node, test this 0113 QCOMPARE(m.node(), (tikz::core::Node*)nullptr); 0114 0115 // make sure signals are emitted only once 0116 QCOMPARE(m_changeCount, 1); 0117 0118 // 0119 // now connect to node again and change node position. 0120 // The MetaPos.pos() should adapt automatically 0121 // 0122 QVERIFY(m.setNode(n)); 0123 QCOMPARE(m.node(), n); 0124 QCOMPARE(n->pos(), tikz::Pos(5, 5)); 0125 QCOMPARE(m.pos(), tikz::Pos(5, 5)); 0126 QVERIFY(m == m); 0127 0128 // now change node position 0129 m_changeCount = 0; 0130 n->setPos(tikz::Pos(10, 10)); 0131 QCOMPARE(n->pos(), tikz::Pos(10, 10)); 0132 QCOMPARE(m.pos(), tikz::Pos(10, 10)); 0133 QVERIFY(m == m); 0134 QCOMPARE(m_changeCount, 1); 0135 } 0136 0137 void MetaPosTest::testSet0() 0138 { 0139 tikz::core::Document doc; 0140 tikz::core::MetaPos m(&doc); 0141 auto n = doc.createEntity<tikz::core::Node>(tikz::EntityType::Node); 0142 0143 0144 // set distinct node position 0145 n->setPos(tikz::Pos(5, 5)); 0146 QCOMPARE(n->pos(), tikz::Pos(5, 5)); 0147 QVERIFY(m == m); 0148 0149 // check setting node 0150 QVERIFY(m.setNode(n)); 0151 QCOMPARE(m.node(), n); 0152 QVERIFY(m == m); 0153 0154 // now set node to 0 again 0155 QVERIFY(m.setNode(nullptr)); 0156 0157 // make sure the node is 0 and the position is kept 0158 QCOMPARE(m.node(), (tikz::core::Node*)nullptr); 0159 QCOMPARE(m.pos(), tikz::Pos(5, 5)); 0160 QVERIFY(m == m); 0161 } 0162 0163 void MetaPosTest::testMetaPosPtr() 0164 { 0165 // test function MetaPos::copy(), and make sure the instance behind the 0166 // ::Ptr is a copy that remains unchanged when pos chages. 0167 tikz::core::Document doc; 0168 tikz::core::MetaPos::Ptr m1(new tikz::core::MetaPos(&doc)); 0169 tikz::core::MetaPos::Ptr m2(new tikz::core::MetaPos(&doc)); 0170 *m2 = *m1; 0171 QVERIFY(*m1 == *m2); 0172 QVERIFY(m1 != m2); 0173 0174 m1->setPos(tikz::Pos(3, 3)); 0175 QCOMPARE(m1->pos(), tikz::Pos(3, 3)); 0176 QCOMPARE(m2->pos(), tikz::Pos(0, 0)); 0177 QVERIFY(*m1 != *m2); 0178 QVERIFY(m1 != m2); 0179 0180 // now test with Node 0181 auto n = doc.createEntity<tikz::core::Node>(tikz::EntityType::Node); 0182 0183 0184 m1->setNode(n); 0185 m2->setPos(tikz::Pos(1, 1)); 0186 QCOMPARE(m1->pos(), tikz::Pos(0, 0)); 0187 QCOMPARE(m2->pos(), tikz::Pos(1, 1)); 0188 QVERIFY(*m1 != *m2); 0189 QVERIFY(m1 != m2); 0190 } 0191 0192 void MetaPosTest::testToString() 0193 { 0194 tikz::core::Document doc; 0195 tikz::core::MetaPos m(&doc); 0196 auto n = doc.createEntity<tikz::core::Node>(tikz::EntityType::Node); 0197 0198 0199 QCOMPARE(n->uid(), tikz::core::Uid(1, &doc)); 0200 QCOMPARE(m.toString(), QString("(0pt, 0pt)")); 0201 0202 m.setPos(tikz::Pos(3, 3, tikz::Unit::Centimeter)); 0203 QCOMPARE(m.toString(), QString("(3cm, 3cm)")); 0204 0205 m.setPos(tikz::Pos(-3.2764, -42.17, tikz::Unit::Centimeter)); 0206 QCOMPARE(m.toString(), QString("(-3.2764cm, -42.17cm)")); 0207 0208 // now test with Node 0209 m.setNode(n); 0210 QVERIFY(m.anchor().isEmpty()); 0211 QCOMPARE(m.toString(), QString("(1)")); 0212 n->setPos(tikz::Pos(-112.2423, 3478.3434, tikz::Unit::Millimeter)); 0213 QCOMPARE(n->metaPos().toString(), QString("(-112.2423mm, 3478.3434mm)")); 0214 QCOMPARE(m.pos().toString(), QString("(-112.2423mm, 3478.3434mm)")); 0215 QCOMPARE(m.toString(), QString("(1)")); 0216 0217 m.setAnchor("center"); 0218 QCOMPARE(m.toString(), QString("(1.center)")); 0219 m.setAnchor("north east"); 0220 QCOMPARE(m.toString(), QString("(1.north east)")); 0221 0222 // unset Node again 0223 m.setPos(tikz::Pos(3, 3, tikz::Unit::Centimeter)); 0224 QCOMPARE(m.node(), (tikz::core::Node*)nullptr); 0225 QVERIFY(m.anchor().isEmpty()); 0226 QCOMPARE(m.toString(), QString("(3cm, 3cm)")); 0227 } 0228 0229 void MetaPosTest::testFromString() 0230 { 0231 tikz::core::Document doc; 0232 tikz::core::MetaPos m(&doc); 0233 auto n = doc.createEntity<tikz::core::Node>(tikz::EntityType::Node); 0234 0235 QCOMPARE(n->uid(), tikz::core::Uid(1, &doc)); 0236 m.fromString("(-3cm, 4cm)"); 0237 QCOMPARE(m.toString(), QString("(-3cm, 4cm)")); 0238 0239 m.fromString("(-3pt, 4cm)"); 0240 QCOMPARE(m.toString(), QString("(-3pt, 4cm)")); 0241 0242 m.fromString("(1)"); 0243 QCOMPARE(m.toString(), QString("(1)")); 0244 QCOMPARE(m.node(), n); 0245 QVERIFY(m.anchor().isEmpty()); 0246 0247 m.fromString("(1.south)"); 0248 QCOMPARE(m.toString(), QString("(1.south)")); 0249 QCOMPARE(m.node(), n); 0250 QCOMPARE(m.anchor(), QString("south")); 0251 0252 m.fromString("(-123345in, +124323in)"); 0253 QCOMPARE(m.toString(), QString("(-123345in, 124323in)")); 0254 QCOMPARE(m.node(), (tikz::core::Node*)nullptr); 0255 QVERIFY(m.anchor().isEmpty()); 0256 } 0257 0258 // kate: indent-width 4; replace-tabs on;