summaryrefslogtreecommitdiffstats
path: root/tests/auto/qdeclarativegeolocation/tst_qdeclarativegeolocation.cpp
blob: e6818209b173522e9ef4feab4b2c5b34f7086d72 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtTest/private/qpropertytesthelper_p.h>
#include <QtPositioning/QGeoCircle>
#include <QtPositioning/QGeoRectangle>
#include <QtPositioningQuick/private/qdeclarativegeolocation_p.h>

QT_USE_NAMESPACE

class tst_DeclarativeGeoLocation : public QObject
{
    Q_OBJECT

private slots:
    void locationProperty();
    void addressBinding();
    void coordinateBinding();
    void shapeBinding();
    void attributesBinding();
};

void tst_DeclarativeGeoLocation::locationProperty()
{
    // This test calls setLocation() with different preconditions, providing
    // coverage for different branches of the setLocation() method.

    QGeoAddress addr;
    addr.setCountryCode("DEU");
    addr.setCountry("Germany");
    addr.setCity("Berlin");
    addr.setStreet("Erich-Thilo-Str");
    addr.setStreetNumber("10");
    addr.setPostalCode("12489");

    const QGeoCoordinate c(52.43, 13.53);

    const QVariantMap attrs { { "string_proprty", "value" }, { "int_property", 5 } };

    QGeoLocation loc1;
    loc1.setAddress(addr);
    loc1.setCoordinate(c);
    loc1.setBoundingShape(QGeoCircle(c, 100));
    loc1.setExtendedAttributes(attrs);

    QDeclarativeGeoLocation location;
    qsizetype addrChangedCount = 0;
    qsizetype coordChangedCount = 0;
    qsizetype shapeChangedCount = 0;
    qsizetype attrChangedCount = 0;

    auto addrChangedHandler = location.bindableAddress().onValueChanged(
            [&addrChangedCount]() { ++addrChangedCount; });
    Q_UNUSED(addrChangedHandler)
    auto coordChangedHandler = location.bindableCoordinate().onValueChanged(
            [&coordChangedCount]() { ++coordChangedCount; });
    Q_UNUSED(coordChangedHandler)
    auto shapeChangedHandler = location.bindableBoundingShape().onValueChanged(
            [&shapeChangedCount]() { ++shapeChangedCount; });
    Q_UNUSED(shapeChangedHandler)
    auto attrChangedHandler = location.bindableExtendedAttributes().onValueChanged(
            [&attrChangedCount]() { ++attrChangedCount; });
    Q_UNUSED(attrChangedHandler)

    // By default an empty m_address is created in the default constructor.
    // So m_address contains a valid pointer, m_address->parent() == this.
    location.setLocation(loc1);
    QCOMPARE(addrChangedCount, 0); // the pointer didn't change
    QCOMPARE(coordChangedCount, 1);
    QCOMPARE(shapeChangedCount, 1);
    QCOMPARE(attrChangedCount, 1);
    QCOMPARE(location.location(), loc1);
    QCOMPARE(location.address()->parent(), &location);

    // m_address contains a nullptr
    location.setAddress(nullptr);
    QVERIFY(!location.address());
    addrChangedCount = 0;
    coordChangedCount = 0;
    shapeChangedCount = 0;
    attrChangedCount = 0;

    location.setLocation(loc1);
    QCOMPARE(addrChangedCount, 1); // only the pointer has changed
    QCOMPARE(coordChangedCount, 0);
    QCOMPARE(shapeChangedCount, 0);
    QCOMPARE(attrChangedCount, 0);
    QCOMPARE(location.location(), loc1);
    QCOMPARE(location.address()->parent(), &location);

    // m_address contains a valid pointer, m_address->parent() != this
    QGeoAddress addr1;
    addr1.setCountryCode("USA");
    addr1.setCountry("United States");
    addr1.setPostalCode("8900");
    addr1.setState("Oregon");
    addr1.setCity("Springfield");
    addr1.setDistrict("Pressboard Estates");
    addr1.setStreet("Evergreen Tce");
    addr1.setStreetNumber("742");

    QDeclarativeGeoAddress geoAddr(addr1);

    location.setAddress(&geoAddr);
    QVERIFY(location.address());
    QCOMPARE(location.address()->parent(), nullptr);
    addrChangedCount = 0;

    location.setLocation(loc1);
    QCOMPARE(addrChangedCount, 1); // only the pointer has changed
    QCOMPARE(coordChangedCount, 0);
    QCOMPARE(shapeChangedCount, 0);
    QCOMPARE(attrChangedCount, 0);
    QCOMPARE(location.location(), loc1);
    QCOMPARE(location.address()->parent(), &location);
}

void tst_DeclarativeGeoLocation::addressBinding()
{
    QDeclarativeGeoLocation location;
    QDeclarativeGeoAddress addr1;
    QDeclarativeGeoAddress addr2;
    QTestPrivate::testReadWritePropertyBasics<QDeclarativeGeoLocation, QDeclarativeGeoAddress *>(
            location, &addr1, &addr2, "address");
}

void tst_DeclarativeGeoLocation::coordinateBinding()
{
    QDeclarativeGeoLocation location;
    const QGeoCoordinate c1(2.0, 1.0);
    const QGeoCoordinate c2(1.0, 2.0);
    QTestPrivate::testReadWritePropertyBasics<QDeclarativeGeoLocation, QGeoCoordinate>(
            location, c1, c2, "coordinate");
}

void tst_DeclarativeGeoLocation::shapeBinding()
{
    QDeclarativeGeoLocation location;
    const QGeoCircle circle(QGeoCoordinate(2.0, 1.0), 10);
    const QGeoRectangle rectangle(QGeoCoordinate(2.0, 1.0), QGeoCoordinate(1.0, 2.0));
    QTestPrivate::testReadWritePropertyBasics<QDeclarativeGeoLocation, QGeoShape>(
            location, circle, rectangle, "boundingShape");
}

void tst_DeclarativeGeoLocation::attributesBinding()
{
    QDeclarativeGeoLocation location;
    const QVariantMap m1 { { "string_proprty", "value" }, { "int_property", 5 } };
    const QVariantMap m2 { { "int_property", 10 }, { "double_property", 15.5 } };
    QTestPrivate::testReadWritePropertyBasics<QDeclarativeGeoLocation, QVariantMap>(
            location, m1, m2, "extendedAttributes");
}

QTEST_APPLESS_MAIN(tst_DeclarativeGeoLocation)

#include "tst_qdeclarativegeolocation.moc"