summaryrefslogtreecommitdiff
path: root/tests/auto/declarative/tst_map_objectview.qml
blob: 5c05db128477cfca41596e8a70653f6350156683 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.0
import QtTest 1.0
import Qt.location 5.0
import Qt.location.test 5.0

Item {
    id: masterItem
    width: 200
    height: 350
    // General-purpose elements for the test:
    Plugin { id: testPlugin; name : "nokia"; PluginParameter {name: "mapping.host"; value: "for.nonexistent"}}
    Coordinate{ id: mapDefaultCenter; latitude: 10; longitude: 30}

    MapCircle {
        objectName: "externalCircle"
        id: externalCircle
        radius: 2000000
        center: mapDefaultCenter
    }

    MapRectangle {
        objectName: "externalRectangle"
        id: externalRectangle
    }

    MapPolygon {
        objectName: "externalPolygon"
        id: externalPolygon
    }

    MapText {
        id: externalText
    }

    MapImage {
        id: externalImage
    }

    MapPolyline {
        id: externalPolyline
    }

    TestModel {
        id: testModel
        datatype: 'coordinate'
        datacount: 7
        delay: 0
    }

    TestModel {
        id: testModel2
        datatype: 'coordinate'
        datacount: 3
        delay: 0
    }

    Map {
        id: mapWithPlugin; center: mapDefaultCenter; plugin: testPlugin;
        anchors.fill: parent; size.width: parent.width; size.height: parent.height; zoomLevel: 2
        MapCircle {
            id: internalCircle
            radius: 2000000
            center: mapDefaultCenter
        }
        MapObjectView {
            id: theObjectView
            model: testModel
            delegate: Component {
                MapCircle {
                    radius: 1500000
                    center: Coordinate {
                        latitude: modeldata.coordinate.latitude;
                        longitude: modeldata.coordinate.longitude;
                    }
                }
            }
        }
    }

    Map {
        id: mapWithoutPlugin; center: mapDefaultCenter;
        anchors.fill: parent; size.width: parent.width; size.height: parent.height; zoomLevel: 2
        MapCircle {
            id: internalCircle2
            radius: 2000000
            center: mapDefaultCenter
        }
        MapObjectView {
            id: theObjectView2
            model: testModel
            delegate: Component {
                MapCircle {
                    radius: 1500000
                    center: Coordinate {
                        latitude: modeldata.coordinate.latitude;
                        longitude: modeldata.coordinate.longitude;
                    }
                }
            }
        }
        MapObjectView {
            id: routeObjectView
            model: routeModel
            delegate: Component {
                MapRoute {
                    route:  routeData
                }
            }
        }
    }

    TestCase {
        name: "MapObjectView"
        function test_a_add_and_remove() {
            // Basic adding and removing of static object
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 8)
            mapWithPlugin.addMapObject(internalCircle)
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 8)
            mapWithPlugin.removeMapObject(internalCircle)
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 7)
            mapWithPlugin.removeMapObject(internalCircle)
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 7)
            // Basic adding and removing of dynamic object
            var dynamicCircle = Qt.createQmlObject( "import QtQuick 2.0; import Qt.location 5.0; MapCircle {radius: 4000; center: mapDefaultCenter}", mapWithPlugin, "");
            mapWithPlugin.addMapObject(dynamicCircle)
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 8)
            mapWithPlugin.removeMapObject(dynamicCircle)
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 7)
            mapWithPlugin.removeMapObject(dynamicCircle)
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 7)
        }

        SignalSpy {id: model1Spy; target: testModel; signalName: "modelChanged"}
        SignalSpy {id: model2Spy; target: testModel2; signalName: "modelChanged"}
        function test_b_model_change() {
            // Change the model of an MapObjectView on the fly
            // and verify that object counts change accordingly.
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 7)
            testModel.datacount += 2
            testModel2.datacount += 1
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 9)
            theObjectView.model = testModel
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 9)
            theObjectView.model = testModel2
            compare(mapWithPlugin.testGetDeclarativeMapObjectCount(), 4)
        }

        SignalSpy {id: pluginChangedSpy; target: mapWithoutPlugin; signalName: "pluginChanged"}
        function test_c_plugin_set_later() {
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 0)
            mapWithoutPlugin.plugin = testPlugin
            tryCompare(pluginChangedSpy, "count", 1, 1000)
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 10) // 9 from testModel, + 1 from mapcircle
            mapWithoutPlugin.clearMapObjects();
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 9)  // 9 from testModel
            testModel.reset();
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 0)
        }
        Plugin {
            id: testPlugin_immediate;
            name: "qmlgeo.test.plugin"
            parameters: [
                // Parms to guide the test plugin
                PluginParameter { name: "gc_supported"; value: true},
                PluginParameter { name: "gc_finishRequestImmediately"; value: true},
                PluginParameter { name: "gc_validateWellKnownValues"; value: true}
            ]
        }
        RouteQuery {id: routeQuery;
            waypoints: [
                Coordinate {id: fcoordinate1; latitude: 60; longitude: 60},
                Coordinate {id: fcoordinate2; latitude: 61; longitude: 62},
                Coordinate {id: fcoordinate3; latitude: 63; longitude: 64},
                Coordinate {id: fcoordinate4; latitude: 65; longitude: 66},
                Coordinate {id: fcoordinate5; latitude: 67; longitude: 68}
            ]
        }
        // Test routemodel and object list of map
        RouteModel {id: routeModel; plugin: testPlugin_immediate; query: routeQuery }
        SignalSpy {id: objectsChangedSpy; target: mapWithoutPlugin; signalName: "objectsChanged"}
        function test_d_routemodel() {
            objectsChangedSpy.clear()
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 0) // precondition
            compare(mapWithoutPlugin.objects.length, 0)
            compare(objectsChangedSpy.count, 0)
            routeQuery.numberAlternativeRoutes = 4
            routeModel.update();
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 4)
            compare(mapWithoutPlugin.objects.length, 0)
            routeQuery.numberAlternativeRoutes = 3
            routeModel.update();
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 3)
            compare(mapWithoutPlugin.objects.length, 0)
            routeModel.clear();
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 0)
            compare(mapWithoutPlugin.objects.length, 0)
            routeModel.clear(); // clear empty model
            routeQuery.numberAlternativeRoutes = 3
            routeModel.update();
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 3)
            compare(mapWithoutPlugin.objects.length, 0)
            compare(objectsChangedSpy.count, 0)
            mapWithoutPlugin.addMapObject(externalCircle)
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 4)
            compare(mapWithoutPlugin.objects.length, 1)
            compare(objectsChangedSpy.count, 1)
            compare(mapWithoutPlugin.objects[0], externalCircle)
            routeModel.reset(); // all map objects based on model will be cleared
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 1)
            compare(mapWithoutPlugin.objects.length, 1)
            mapWithoutPlugin.clearMapObjects() // clears objects not stemming from model
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 0)
            compare(mapWithoutPlugin.objects.length, 0)
            compare(objectsChangedSpy.count, 2)

            // Test the objects list
            mapWithoutPlugin.addMapObject(externalCircle)
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 1)
            compare(mapWithoutPlugin.objects.length, 1)
            compare(objectsChangedSpy.count, 3)
            compare(mapWithoutPlugin.objects[0], externalCircle)

            mapWithoutPlugin.addMapObject(externalRectangle)
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 2)
            compare(mapWithoutPlugin.objects.length, 2)
            compare(objectsChangedSpy.count, 4)
            compare(mapWithoutPlugin.objects[1], externalRectangle)

            mapWithoutPlugin.addMapObject(externalRectangle)
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 2)
            compare(mapWithoutPlugin.objects.length, 2)
            compare(objectsChangedSpy.count, 4)
            compare(mapWithoutPlugin.objects[1], externalRectangle)

            mapWithoutPlugin.addMapObject(externalPolygon)
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 3)
            compare(mapWithoutPlugin.objects.length, 3)
            compare(objectsChangedSpy.count, 5)
            compare(mapWithoutPlugin.objects[1], externalRectangle)

            mapWithoutPlugin.removeMapObject(externalCircle)
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 2)
            compare(mapWithoutPlugin.objects.length, 2)
            compare(objectsChangedSpy.count, 6)
            compare(mapWithoutPlugin.objects[0], externalRectangle)

            mapWithoutPlugin.removeMapObject(externalRectangle)
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 1)
            compare(mapWithoutPlugin.objects.length, 1)
            compare(objectsChangedSpy.count, 7)
            compare(mapWithoutPlugin.objects[0], externalPolygon)

            mapWithoutPlugin.clearMapObjects()
            compare(mapWithoutPlugin.testGetDeclarativeMapObjectCount(), 0)
            compare(mapWithoutPlugin.objects.length, 0)
            compare(objectsChangedSpy.count, 8)
        }
        function test_e_map_objects_on_delete() {
            // Test that dynamic map & map object creation works
            var dynamicMap = Qt.createQmlObject("import QtQuick 2.0; import Qt.location 5.0; Map {plugin: testPlugin;}", masterItem, "");
            var dynamicCircle = Qt.createQmlObject("import QtQuick 2.0; import Qt.location 5.0; MapCircle {}", masterItem, "");
            compare (dynamicMap.objects.length, 0)
            dynamicMap.addMapObject(dynamicCircle)
            compare (dynamicMap.objects.length, 1)
            dynamicCircle.destroy(2); wait(100) // circle should remove itself from the map
            compare (dynamicMap.objects.length, 0)

            var dynamicRectangle = Qt.createQmlObject("import QtQuick 2.0; import Qt.location 5.0; MapRectangle {}", masterItem, "");
            dynamicMap.addMapObject(dynamicRectangle)
            dynamicMap.destroy(2); wait(100) // must act gracefully although has objects when destroyed
        }
    }
}