1 /*
2  * Copyright (C) 2018 Damir Porobic <damir.porobic@gmx.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 
21 #include "SelectionHandlesTest.h"
22 
TestIsHandleGrabbed_Should_ReturnTrue_When_ClickedOnHandle()23 void SelectionHandlesTest::TestIsHandleGrabbed_Should_ReturnTrue_When_ClickedOnHandle()
24 {
25 	QRectF selection(0, 0, 500, 500);
26 	auto halfHandle = ResizeHandleSize / 2;
27 	QPointF position(halfHandle, halfHandle);
28 	SelectionHandles handles;
29 	handles.updateHandles(selection);
30 	handles.grabHandle(position, selection);
31 
32 	auto isHandleGrabbed = handles.isHandleGrabbed();
33 
34 	QCOMPARE(isHandleGrabbed, true);
35 }
36 
TestIsHandleGrabbed_Should_ReturnFalse_When_ClickedOutsideHandle()37 void SelectionHandlesTest::TestIsHandleGrabbed_Should_ReturnFalse_When_ClickedOutsideHandle()
38 {
39 	QRectF selection(0, 0, 500, 500);
40 	QPointF position(300, 300);
41 	SelectionHandles handles;
42 	handles.updateHandles(selection);
43 	handles.grabHandle(position, selection);
44 
45 	auto isHandleGrabbed = handles.isHandleGrabbed();
46 
47 	QCOMPARE(isHandleGrabbed, false);
48 }
49 
TestGrabbedIndex_Should_ReturnIndexOfHandle_When_ClickedOnHandle()50 void SelectionHandlesTest::TestGrabbedIndex_Should_ReturnIndexOfHandle_When_ClickedOnHandle()
51 {
52 	QRectF selection(0, 0, 500, 500);
53 	auto halfHandle = ResizeHandleSize / 2;
54 	QPointF position(halfHandle, halfHandle);
55 	SelectionHandles handles;
56 	handles.updateHandles(selection);
57 	handles.grabHandle(position, selection);
58 
59 	auto grabbedIndex = handles.grabbedIndex();
60 
61 	QCOMPARE(grabbedIndex, 0);
62 }
63 
TestGrabbedIndex_Should_ReturnMinusOne_When_ClickedOutsideHandle()64 void SelectionHandlesTest::TestGrabbedIndex_Should_ReturnMinusOne_When_ClickedOutsideHandle()
65 {
66 	QRectF selection(0, 0, 500, 500);
67 	QPointF position(300, 300);
68 	SelectionHandles handles;
69 	handles.updateHandles(selection);
70 	handles.grabHandle(position, selection);
71 
72 	auto grabbedIndex = handles.grabbedIndex();
73 
74 	QCOMPARE(grabbedIndex, -1);
75 }
76 
TestGrabOffset_Should_ReturnClickOffsetForHandle()77 void SelectionHandlesTest::TestGrabOffset_Should_ReturnClickOffsetForHandle()
78 {
79 	QRectF selection(0, 0, 500, 500);
80 	auto halfHandle = ResizeHandleSize / 2;
81 	QPointF position(halfHandle, halfHandle);
82 	SelectionHandles handles;
83 	handles.updateHandles(selection);
84 	handles.grabHandle(position, selection);
85 
86 	auto grabOffset = handles.grabOffset();
87 
88 	QCOMPARE(grabOffset, QPointF(halfHandle, halfHandle));
89 }
90 
TestUpdateHandles_Should_PositionHandlesOnCorrectPlaces()91 void SelectionHandlesTest::TestUpdateHandles_Should_PositionHandlesOnCorrectPlaces()
92 {
93 	QRectF selection(0, 0, 500, 500);
94 	SelectionHandles selectionHandles;
95 
96 	selectionHandles.updateHandles(selection);
97 
98 	auto handles = selectionHandles.handles();
99 
100 	QCOMPARE(handles[0].topLeft(), QPointF(0, 0));
101 	QCOMPARE(handles[1].topLeft(), QPointF(selection.width() / 2 - ResizeHandleSize / 2, 0));
102 	QCOMPARE(handles[2].topLeft(), QPointF(selection.width() - ResizeHandleSize, 0));
103 	QCOMPARE(handles[3].topLeft(), QPointF(selection.width() - ResizeHandleSize, selection.height() / 2 - ResizeHandleSize / 2));
104 	QCOMPARE(handles[4].topLeft(), QPointF(selection.width() - ResizeHandleSize, selection.height() - ResizeHandleSize));
105 	QCOMPARE(handles[5].topLeft(), QPointF(selection.width() / 2 - ResizeHandleSize / 2, selection.height() - ResizeHandleSize));
106 	QCOMPARE(handles[6].topLeft(), QPointF(0, selection.height() - ResizeHandleSize));
107 	QCOMPARE(handles[7].topLeft(), QPointF(0, selection.height() / 2 - ResizeHandleSize / 2));
108 }
109 
110 QTEST_MAIN(SelectionHandlesTest);
111