File indexing completed on 2025-01-26 04:05:02

0001 /*
0002  *  SPDX-FileCopyrightText: 2006-2010 Thomas Zander <zander@kde.org>
0003  *  SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 #include "TestPosition.h"
0006 
0007 #include <MockShapes.h>
0008 #include <QPointF>
0009 #include <simpletest.h>
0010 
0011 TestPosition::TestPosition()
0012         : shape1(0),
0013         shape2(0),
0014         childShape1(0),
0015         childShape2(0),
0016         container(0),
0017         container2(0)
0018 {
0019 }
0020 
0021 void TestPosition::init()
0022 {
0023     shape1 = new MockShape();
0024     shape1->setPosition(QPointF(50, 50));
0025     shape1->setSize(QSize(50, 50));
0026     shape2 = new MockShape();
0027     shape2->setPosition(QPointF(20, 20));
0028     shape2->setSize(QSize(50, 50));
0029 
0030     childShape1 = new MockShape();
0031     childShape1->setPosition(QPointF(20, 20));
0032     childShape1->setSize(QSize(50, 50));
0033     container = new MockContainer();
0034     container->setPosition(QPointF(100, 100));
0035     container->addShape(childShape1);
0036     container->setInheritsTransform(childShape1, false);
0037 
0038     childShape2 = new MockShape();
0039     childShape2->setPosition(QPointF(25, 25));
0040     childShape2->setSize(QSizeF(10, 15));
0041     container2 = new MockContainer();
0042     container2->setPosition(QPointF(100, 200));
0043     container2->setSize(QSizeF(100, 100));
0044     container2->rotate(90);
0045     container2->addShape(childShape2);
0046 }
0047 
0048 void TestPosition::cleanup()
0049 {
0050     delete container;
0051     delete container2;
0052     delete shape1;
0053     delete shape2;
0054 }
0055 
0056 void TestPosition::testBasePosition()
0057 {
0058     // internal consistency tests.
0059     QCOMPARE(shape1->position(), QPointF(50, 50));
0060     QCOMPARE(shape2->position(), QPointF(20, 20));
0061     QCOMPARE(childShape1->position(), QPointF(20, 20));
0062     QCOMPARE(container->position(), QPointF(100, 100));
0063 }
0064 
0065 void TestPosition::testAbsolutePosition()
0066 {
0067     QCOMPARE(shape1->absolutePosition(), QPointF(75, 75));
0068     QCOMPARE(shape2->absolutePosition(), QPointF(45, 45));
0069 
0070     // translated
0071     QCOMPARE(childShape1->absolutePosition(), QPointF(100 + 20 + 25, 100 + 20 + 25));
0072 
0073     // rotated
0074     container2->setInheritsTransform(childShape2, false);
0075     QCOMPARE(container2->absolutePosition(), QPointF(150, 250));
0076     QCOMPARE(childShape2->absolutePosition(), QPointF(130, 232.5));
0077     container2->setInheritsTransform(childShape2, true);
0078     QCOMPARE(childShape2->absolutePosition(), QPointF(167.5, 230));
0079 
0080     shape1->rotate(90);
0081     shape1->setPosition(QPointF(10, 10));
0082 
0083     QCOMPARE(shape1->absolutePosition(), QPointF(10 + 25, 10 + 25));
0084     QCOMPARE(shape1->absolutePosition(KoFlake::Center), QPointF(10 + 25, 10 + 25));
0085     QCOMPARE(shape1->absolutePosition(KoFlake::TopLeft), QPointF(10 + 50, 10));
0086     QCOMPARE(shape1->absolutePosition(KoFlake::BottomRight), QPointF(10, 10 + 50));
0087 
0088     QCOMPARE(container2->absolutePosition(KoFlake::TopLeft), QPointF(200, 200));
0089 }
0090 
0091 void TestPosition::testSetAbsolutePosition()
0092 {
0093     shape1->rotate(-90); // rotate back as we are rotating relative
0094     shape1->setPosition(QPointF(10, 10));
0095     QCOMPARE(shape1->absolutePosition(), QPointF(10 + 25, 10 + 25));
0096     shape1->setAbsolutePosition(QPointF(10, 10));
0097     QCOMPARE(shape1->absolutePosition(), QPointF(10, 10));
0098     shape1->rotate(45);
0099     QCOMPARE(shape1->absolutePosition(), QPointF(10, 10));
0100 
0101     childShape1->setAbsolutePosition(QPointF(0, 0));
0102     QCOMPARE(childShape1->position(), QPointF(-125, -125));
0103     QCOMPARE(childShape1->absolutePosition(), QPointF(0, 0));
0104 
0105     QCOMPARE(container2->position(), QPointF(100, 200));  // make sure nobody changed it
0106     container2->setInheritsTransform(childShape2, false);
0107     childShape2->setAbsolutePosition(QPointF(0, 0));
0108     QCOMPARE(childShape2->position(), QPointF(-100 - 5, -200 - 7.5));
0109     QCOMPARE(childShape2->absolutePosition(), QPointF(0, 0));
0110 
0111     container2->setInheritsTransform(childShape2, true);
0112     childShape2->setAbsolutePosition(QPointF(0, 0));
0113     QCOMPARE(childShape2->absolutePosition(), QPointF(0, 0));
0114     QCOMPARE(childShape2->position(), QPointF(-200 - 5, 200 - 7.5));
0115 }
0116 
0117 void TestPosition::testSetAbsolutePosition2()
0118 {
0119     shape1->rotate(90);
0120     shape1->setAbsolutePosition(QPointF(100, 100));
0121     QCOMPARE(shape1->absolutePosition(), QPointF(100, 100));
0122 
0123     shape1->setAbsolutePosition(QPointF(100, 100), KoFlake::TopLeft);
0124     QCOMPARE(shape1->absolutePosition(KoFlake::TopLeft), QPointF(100, 100));
0125 
0126     childShape1->setAbsolutePosition(QPointF(0, 0), KoFlake::BottomRight);
0127     QCOMPARE(childShape1->position(), QPointF(-150, -150));
0128 
0129     childShape1->setAbsolutePosition(QPointF(0, 0), KoFlake::BottomLeft);
0130     QCOMPARE(childShape1->position(), QPointF(-100, -150));
0131 
0132     childShape1->setAbsolutePosition(QPointF(0, 0), KoFlake::TopRight);
0133     QCOMPARE(childShape1->position(), QPointF(-150, -100));
0134 
0135     container2->setInheritsTransform(childShape2, true);
0136     childShape2->setAbsolutePosition(QPointF(0, 0), KoFlake::TopLeft);
0137     QCOMPARE(childShape2->position(), QPointF(-200, 200));
0138 }
0139 
0140 void TestPosition::testSetAndGetRotation()
0141 {
0142     shape1->rotate(180);
0143     QCOMPARE(shape1->rotation(), 180.0);
0144     shape1->rotate(2);
0145     QCOMPARE(shape1->rotation(), 182.0);
0146     shape1->rotate(4);
0147     QCOMPARE(shape1->rotation(), 186.0);
0148     shape1->rotate(358);
0149     QCOMPARE(shape1->rotation(), 184.0);
0150 }
0151 
0152 QTEST_GUILESS_MAIN(TestPosition)