1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
* Copyright (C) 2018 Damir Porobic <damir.porobic@gmx.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "RectResizeHandlesTest.h"
void RectResizeHandlesTest::TestInitHandles_Should_PositionEightHandles_When_RectItemProvided()
{
auto properties = PropertiesPtr(new AnnotationProperties(Qt::red, 4));
QRectF rect(QPointF(10, 15), QPointF(20, 25));
AnnotationRect rectItem(rect.topLeft(), properties);
rectItem.addPoint(rect.bottomRight(), false);
RectResizeHandles rectResizeHandles(&rectItem, 1.0);
QCOMPARE(rectResizeHandles.handles().count(), 8);
QCOMPARE(rectResizeHandles.handles()[0].anchor(), rect.topLeft());
QCOMPARE(rectResizeHandles.handles()[1].anchor(), QPointF(rect.center().x(), rect.top()));
QCOMPARE(rectResizeHandles.handles()[2].anchor(), rect.topRight());
QCOMPARE(rectResizeHandles.handles()[3].anchor(), QPointF(rect.right(), rect.center().y()));
QCOMPARE(rectResizeHandles.handles()[4].anchor(), rect.bottomRight());
QCOMPARE(rectResizeHandles.handles()[5].anchor(), QPointF(rect.center().x(), rect.bottom()));
QCOMPARE(rectResizeHandles.handles()[6].anchor(), rect.bottomLeft());
QCOMPARE(rectResizeHandles.handles()[7].anchor(), QPointF(rect.left(), rect.center().y()));
}
void RectResizeHandlesTest::TestIndexOfHandleAt_Should_ReturnIndexOfHandle_When_HandleIsAtProvidedPosition()
{
auto properties = PropertiesPtr(new AnnotationProperties(Qt::red, 2));
QRectF rect(QPointF(10, 15), QPointF(80, 95));
AnnotationRect rectItem(rect.topLeft(), properties);
rectItem.addPoint(rect.bottomRight(), false);
RectResizeHandles rectResizeHandles(&rectItem, 1.0);
auto resultP1 = rectResizeHandles.indexOfHandleAt(rect.topLeft() + QPointF(2, 2));
auto resultP2 = rectResizeHandles.indexOfHandleAt(ShapeHelper::rectTop(rect) + QPointF(-2, -2));
auto resultP3 = rectResizeHandles.indexOfHandleAt(rect.topRight() + QPointF(-2, -2));
auto resultP4 = rectResizeHandles.indexOfHandleAt(ShapeHelper::rectRight(rect) + QPointF(-2, -2));
auto resultP5 = rectResizeHandles.indexOfHandleAt(rect.bottomRight() + QPointF(-2, -2));
auto resultP6 = rectResizeHandles.indexOfHandleAt(ShapeHelper::rectBottom(rect) + QPointF(-2, -2));
auto resultP7 = rectResizeHandles.indexOfHandleAt(rect.bottomLeft() + QPointF(-2, -2));
auto resultP8 = rectResizeHandles.indexOfHandleAt(ShapeHelper::rectLeft(rect) + QPointF(-2, -2));
QCOMPARE(rectResizeHandles.handles().count(), 8);
QCOMPARE(resultP1, 0);
QCOMPARE(resultP2, 1);
QCOMPARE(resultP3, 2);
QCOMPARE(resultP4, 3);
QCOMPARE(resultP5, 4);
QCOMPARE(resultP6, 5);
QCOMPARE(resultP7, 6);
QCOMPARE(resultP8, 7);
}
void RectResizeHandlesTest::TestIndexOfHandleAt_Should_NotReturnAnyIndex_When_HandleIsNotAtProvidedPosition()
{
auto properties = PropertiesPtr(new AnnotationProperties(Qt::red, 2));
QRectF rect(QPointF(10, 15), QPointF(20, 25));
AnnotationRect rectItem(rect.topLeft(), properties);
rectItem.addPoint(rect.bottomRight(), false);
RectResizeHandles rectResizeHandles(&rectItem, 1.0);
auto resultP1 = rectResizeHandles.indexOfHandleAt(QPointF(50, 50));
QCOMPARE(rectResizeHandles.handles().count(), 8);
QCOMPARE(resultP1, -1);
}
void RectResizeHandlesTest::TestHandle_Should_ReturnRectAtIndex_When_HandleAtIndexExists()
{
auto properties = PropertiesPtr(new AnnotationProperties(Qt::red, 2));
QRectF rect(QPointF(10, 15), QPointF(20, 25));
AnnotationRect rectItem(rect.topLeft(), properties);
rectItem.addPoint(rect.bottomRight(), false);
RectResizeHandles rectResizeHandles(&rectItem, 1.0);
auto result = rectResizeHandles.handle(2);
QVERIFY(result != QRectF());
QCOMPARE(result.anchor(), rect.topRight());
}
void RectResizeHandlesTest::TestHandle_Should_NotReturnRect_When_HandleAtIndexDoesntExists()
{
auto properties = PropertiesPtr(new AnnotationProperties(Qt::red, 2));
QRectF rect(QPointF(10, 15), QPointF(20, 25));
AnnotationRect rectItem(rect.topLeft(), properties);
rectItem.addPoint(rect.bottomRight(), false);
RectResizeHandles rectResizeHandles(&rectItem, 1.0);
auto result = rectResizeHandles.handle(10);
QCOMPARE(result, ResizeHandle());
}
void RectResizeHandlesTest::TestGetCursorForHandle_Should_NotReturnDefaultCursor_When_ProvidedPositionOnHandle()
{
auto properties = PropertiesPtr(new AnnotationProperties(Qt::red, 2));
QRectF rect(QPointF(10, 15), QPointF(20, 25));
AnnotationRect rectItem(rect.topLeft(), properties);
rectItem.addPoint(rect.bottomRight(), false);
RectResizeHandles rectResizeHandles(&rectItem, 1.0);
auto result = rectResizeHandles.cursorForPos(rect.bottomLeft());
QVERIFY(result != CursorHelper::defaultCursor());
}
void RectResizeHandlesTest::TestGetCursorForHandle_Should_ReturnDefaultCursor_When_ProvidedPositionNotOnHandle()
{
auto properties = PropertiesPtr(new AnnotationProperties(Qt::red, 2));
QRectF rect(QPointF(10, 15), QPointF(20, 25));
QPointF otherPos(90, 90);
AnnotationRect rectItem(rect.topLeft(), properties);
rectItem.addPoint(rect.bottomRight(), false);
RectResizeHandles rectResizeHandles(&rectItem, 1.0);
auto result = rectResizeHandles.cursorForPos(otherPos);
QCOMPARE(result, CursorHelper::defaultCursor());
}
void RectResizeHandlesTest::TestUpdate_Should_MoveHandlesToNewPosition()
{
auto properties = PropertiesPtr(new AnnotationProperties(Qt::red, 2));
QRectF rect(QPointF(10, 15), QPointF(80, 95));
QRectF newRect(QPointF(60, 75), QPointF(120, 125));
AnnotationRect rectItem(rect.topLeft(), properties);
rectItem.addPoint(rect.bottomRight(), false);
RectResizeHandles rectResizeHandles(&rectItem, 1.0);
QCOMPARE(rectResizeHandles.handles()[0].anchor(), rect.topLeft());
QCOMPARE(rectResizeHandles.handles()[1].anchor(), ShapeHelper::rectTop(rect));
QCOMPARE(rectResizeHandles.handles()[2].anchor(), rect.topRight());
QCOMPARE(rectResizeHandles.handles()[3].anchor(), ShapeHelper::rectRight(rect));
QCOMPARE(rectResizeHandles.handles()[4].anchor(), rect.bottomRight());
QCOMPARE(rectResizeHandles.handles()[5].anchor(), ShapeHelper::rectBottom(rect));
QCOMPARE(rectResizeHandles.handles()[6].anchor(), rect.bottomLeft());
QCOMPARE(rectResizeHandles.handles()[7].anchor(), ShapeHelper::rectLeft(rect));
rectItem.setPointAt(newRect.topLeft(), 0, false);
rectItem.addPoint(newRect.bottomRight(), false);
rectResizeHandles.update();
QCOMPARE(rectResizeHandles.handles()[0].anchor(), newRect.topLeft());
QCOMPARE(rectResizeHandles.handles()[1].anchor(), ShapeHelper::rectTop(newRect));
QCOMPARE(rectResizeHandles.handles()[2].anchor(), newRect.topRight());
QCOMPARE(rectResizeHandles.handles()[3].anchor(), ShapeHelper::rectRight(newRect));
QCOMPARE(rectResizeHandles.handles()[4].anchor(), newRect.bottomRight());
QCOMPARE(rectResizeHandles.handles()[5].anchor(), ShapeHelper::rectBottom(newRect));
QCOMPARE(rectResizeHandles.handles()[6].anchor(), newRect.bottomLeft());
QCOMPARE(rectResizeHandles.handles()[7].anchor(), ShapeHelper::rectLeft(newRect));
}
TEST_MAIN(RectResizeHandlesTest);
|