1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <QtTest/QtTest>
30 #include <QtPositioning/QGeoShape>
31 #include <QtCore/QDebug>
32 #include <QtPositioning/QGeoRectangle>
33 #include <QtPositioning/QGeoCircle>
34 
35 QString tst_qgeoshape_debug;
36 
tst_qgeoshape_messageHandler(QtMsgType type,const QMessageLogContext &,const QString & msg)37 void tst_qgeoshape_messageHandler(QtMsgType type, const QMessageLogContext&,
38                                   const QString &msg)
39 {
40     switch (type) {
41         case QtDebugMsg :
42             tst_qgeoshape_debug = msg;
43             break;
44         default:
45             break;
46     }
47 }
48 
49 class tst_qgeoshape : public QObject
50 {
51     Q_OBJECT
52 
53 private slots:
54     void testArea();
55     void debug_data();
56     void debug();
57     void conversions();
58 };
59 
testArea()60 void tst_qgeoshape::testArea()
61 {
62     QGeoShape area;
63     QVERIFY(!area.isValid());
64     QVERIFY(area.isEmpty());
65     QCOMPARE(area.type(), QGeoShape::UnknownType);
66     QVERIFY(!area.contains(QGeoCoordinate()));
67 
68     // QGeoShape never constructs a QGeoShapePrivate.  Hence d_ptr is always 0.
69 
70     QGeoShape area2;
71 
72     QCOMPARE(area, area2);
73 
74     area = area2;
75 
76     QCOMPARE(area, area2);
77 
78     QGeoShape area3(area2);
79 
80     QCOMPARE(area2, area3);
81 }
82 
debug_data()83 void tst_qgeoshape::debug_data()
84 {
85     QTest::addColumn<QGeoShape>("shape");
86     QTest::addColumn<int>("nextValue");
87     QTest::addColumn<QString>("debugString");
88 
89     QTest::newRow("uninitialized") << QGeoShape() << 45
90             << QString("QGeoShape(Unknown) 45");
91     QTest::newRow("uninitialized") << QGeoShape(QGeoRectangle()) << 45
92             << QString("QGeoShape(Rectangle) 45");
93     QTest::newRow("uninitialized") << QGeoShape(QGeoCircle()) << 45
94             << QString("QGeoShape(Circle) 45");
95 }
96 
97 
debug()98 void tst_qgeoshape::debug()
99 {
100     QFETCH(QGeoShape, shape);
101     QFETCH(int, nextValue);
102     QFETCH(QString, debugString);
103 
104     qInstallMessageHandler(tst_qgeoshape_messageHandler);
105     qDebug() << shape << nextValue;
106     qInstallMessageHandler(0);
107     QCOMPARE(tst_qgeoshape_debug, debugString);
108 }
109 
conversions()110 void tst_qgeoshape::conversions()
111 {
112     QVariant varShape = QVariant::fromValue(QGeoShape());
113     QVariant varRect = QVariant::fromValue(QGeoRectangle(
114                                                QGeoCoordinate(1, 1),
115                                                QGeoCoordinate(2, 2)));
116     QVariant varCircle = QVariant::fromValue(QGeoCircle(QGeoCoordinate(3, 3), 1000));
117 
118     QVERIFY(varShape.canConvert<QGeoShape>());
119     QVERIFY(varShape.canConvert<QGeoCircle>());
120     QVERIFY(varShape.canConvert<QGeoRectangle>());
121     QVERIFY(!varRect.canConvert<QGeoCircle>());
122     QVERIFY(varRect.canConvert<QGeoRectangle>());
123     QVERIFY(varRect.canConvert<QGeoShape>());
124     QVERIFY(varCircle.canConvert<QGeoCircle>());
125     QVERIFY(!varCircle.canConvert<QGeoRectangle>());
126     QVERIFY(varCircle.canConvert<QGeoShape>());
127 }
128 
129 QTEST_MAIN(tst_qgeoshape)
130 #include "tst_qgeoshape.moc"
131