summaryrefslogtreecommitdiff
path: root/tests/auto/declarative_location_core/tst_placesearchmodel.qml
blob: 9c3652d482285a34292037184f5bbfa2a2e08cef (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import QtQuick
import QtTest
import QtLocation
import QtPositioning
import "utils.js" as Utils

TestCase {
    id: testCase

    name: "PlaceSearchModel"

    Plugin {
        id: testPlugin
        name: "qmlgeo.test.plugin"
        allowExperimental: true
        parameters: [
            PluginParameter {
                name: "initializePlaceData"
                value: true
            }
        ]
    }

    Plugin {
        id: favoritePlugin
        name: "foo"
    }

    Plugin {
        id: uninitializedPlugin
    }

    Category {
        id: testCategory1
        categoryId: "da3606c1-3448-43b3-a4a3-ca24b12dd94a"
        name: "Test Category 1"
    }

    Category {
        id: testCategory2
        categoryId: "bb8ead84-ec2a-48a9-9c8f-d4ffd3134b21"
        name: "Test Category 2"
    }

    function compareArray(a, b) {
        if (a.length !== b.length)
            return false;

        for (var i = 0; i < a.length; ++i) {
            if (b.indexOf(a[i]) < 0)
                return false;
        }

        return true;
    }

    function test_setAndGet_data() {
        var testSearchArea = QtPositioning.circle(QtPositioning.coordinate(10, 20), 5000);

        return [
            { tag: "plugin", property: "plugin", signal: "pluginChanged", value: testPlugin },
            { tag: "searchArea", property: "searchArea", signal: "searchAreaChanged", value: testSearchArea, reset: QtPositioning.shape() },
            { tag: "limit", property: "limit", signal: "limitChanged", value: 10, reset: -1 },

            { tag: "searchTerm", property: "searchTerm", signal: "searchTermChanged", value: "Test term", reset: "" },
            { tag: "recommendationId", property: "recommendationId", signal: "recommendationIdChanged", value: "Test-place-id", reset: "" },
            { tag: "relevanceHint", property: "relevanceHint", signal: "relevanceHintChanged", value: PlaceSearchModel.DistanceHint, reset: PlaceSearchModel.UnspecifiedHint },
            { tag: "visibilityScope", property: "visibilityScope", signal: "visibilityScopeChanged", value: Place.DeviceVisibility, reset: Place.UnspecifiedVisibility },
            { tag: "favoritesPlugin", property: "favoritesPlugin", signal: "favoritesPluginChanged", value: favoritePlugin },
            { tag: "category", property: "categories", signal: "categoriesChanged", value: testCategory1, expectedValue: [ testCategory1 ], reset: [], array: true },
            { tag: "categories", property: "categories", signal: "categoriesChanged", value: [ testCategory1, testCategory2 ], reset: [], array: true },
        ];
    }

    function test_setAndGet(data) {
        var testModel = Qt.createQmlObject('import QtLocation 5.3; PlaceSearchModel {}', testCase, "PlaceSearchModel");
        Utils.testObjectProperties(testCase, testModel, data);
        delete testModel;
    }

    function test_search_data() {
        var park = Qt.createQmlObject('import QtLocation 5.3; Category {name: "Park"; categoryId: "c2e1252c-b997-44fc-8165-e53dd00f66a7"}', testCase, "Category");
        return [
            {
                tag: "searchTerm, multiple results",
                property: "searchTerm",
                value: "view",
                reset: "",
                places: [
                    "4dcc74ce-fdeb-443e-827c-367438017cf1",
                    "8f72057a-54b2-4e95-a7bb-97b4d2b5721e"
                ]
            },
            {
                tag: "searchTerm, single result",
                property: "searchTerm",
                value: "park",
                reset: "",
                places: [
                    "4dcc74ce-fdeb-443e-827c-367438017cf1"
                ]
            },
            {
                tag: "searchTerm, multiple results",
                property: "searchTerm",
                value: "sea",
                reset: "",
                alternate: true,
                places: [
                    "8f72057a-54b2-4e95-a7bb-97b4d2b5721e"
                ]
            },
            {
                tag: "categories, single result",
                property: "categories",
                value: [ park ],
                places: [
                    "dacb2181-3f67-4e6a-bd4d-635e99ad5b03"
                ]
            },
            {
                tag: "recommendations",
                property: "recommendationId",
                value: "4dcc74ce-fdeb-443e-827c-367438017cf1",
                reset: "",
                places: [
                    "8f72057a-54b2-4e95-a7bb-97b4d2b5721e",
                    "dacb2181-3f67-4e6a-bd4d-635e99ad5b03"
                ]
            },
            {
                tag: "no recommendations",
                property: "recommendationId",
                value: "8f72057a-54b2-4e95-a7bb-97b4d2b5721e",
                reset: "",
                places: [ ]
            }
        ];
    }

    function test_search(data) {
        var testModel = Qt.createQmlObject('import QtLocation 5.3; PlaceSearchModel {}', testCase, "PlaceSearchModel");
        testModel.plugin = testPlugin;

        var statusChangedSpy = Qt.createQmlObject('import QtTest 1.0; SignalSpy {}', testCase, "SignalSpy");
        statusChangedSpy.target = testModel;
        statusChangedSpy.signalName = "statusChanged";

        var countChangedSpy = Qt.createQmlObject('import QtTest 1.0; SignalSpy {}', testCase, "SignalSpy");
        countChangedSpy.target = testModel;
        countChangedSpy.signalName = "rowCountChanged";

        compare(testModel.status, PlaceSearchModel.Null);

        testModel[data.property] = data.value;
        testModel.update();

        compare(testModel.status, PlaceSearchModel.Loading);
        compare(statusChangedSpy.count, 1);

        tryCompare(testModel, "status", PlaceSearchModel.Ready);
        compare(statusChangedSpy.count, 2);

        if (data.places.length > 0)
            compare(countChangedSpy.count, 1);
        else
            compare(countChangedSpy.count, 0);

        for (var i = 0; i < testModel.count; ++i) {
            compare(testModel.data(i, "type"), PlaceSearchModel.PlaceResult);

            var place = testModel.data(i, "place");

            verify(data.places.indexOf(place.placeId) >= 0);
        }

        // Test for alternate implementation
        if (data.alternate !== undefined && data.alternate === true) {
            for (var ii = 0; ii < testModel.count; ++ii) {
                var p = testModel.data(ii, "place");
                compare(p.extendedAttributes["x_provider"].text, "QPlacePrivateDefaultAlt")
            }
        }

        testModel.reset();

        compare(statusChangedSpy.count, 3);
        compare(testModel.status, PlaceSearchModel.Null);
        if (data.places.length > 0)
            compare(countChangedSpy.count, 2);
        else
            compare(countChangedSpy.count, 0);
        compare(testModel.count, 0);

        countChangedSpy.destroy();
        statusChangedSpy.destroy();

        if (data.reset === undefined) {
            testModel[data.property] = null;
        } else {
            testModel[data.property] = data.reset;
        }

        delete testModel;
        delete statusChangedSpy;
        delete countChangedSpy;
    }

    function test_cancel() {
        var testModel = Qt.createQmlObject('import QtLocation 5.3; PlaceSearchModel {}', testCase, "PlaceSearchModel");
        testModel.plugin = testPlugin;

        var statusChangedSpy = Qt.createQmlObject('import QtTest 1.0; SignalSpy {}', testCase, "SignalSpy");
        statusChangedSpy.target = testModel;
        statusChangedSpy.signalName = "statusChanged";

        //try cancelling from an initially null state
        compare(testModel.status, PlaceSearchModel.Null);
        testModel.searchTerm = "view";
        testModel.update();
        tryCompare(testModel, "status", PlaceSearchModel.Loading);
        testModel.cancel();
        tryCompare(testModel, "status", PlaceSearchModel.Ready);
        compare(statusChangedSpy.count, 2);

        testModel.update();
        tryCompare(testModel, "status", PlaceSearchModel.Loading);
        tryCompare(testModel, "status", PlaceSearchModel.Ready);
        compare(statusChangedSpy.count, 4);

        var numResults = testModel.count;
        verify(numResults > 0);

        //try cancelling from an initially ready state
        testModel.update();
        tryCompare(testModel, "status", PlaceSearchModel.Loading);
        testModel.cancel();
        tryCompare(testModel, "status", PlaceSearchModel.Ready);
        compare(testModel.count, numResults);
        compare(statusChangedSpy.count, 6);

        //chack that an encountering an error will cause the model
        //to clear its data
        testModel.plugin = null;
        testModel.update();
        tryCompare(testModel, "count", 0);
        compare(testModel.status, PlaceSearchModel.Error);

        delete testModel;
        delete statusChangedSpy;
    }

    function test_error() {
        var testModel = Qt.createQmlObject('import QtLocation 5.3; PlaceSearchModel {}', testCase, "PlaceSearchModel");

        var statusChangedSpy = Qt.createQmlObject('import QtTest 1.0; SignalSpy {}', testCase, "SignalSpy");
        statusChangedSpy.target = testModel;
        statusChangedSpy.signalName = "statusChanged";

        //try searching without a plugin instance
        testModel.update();
        tryCompare(statusChangedSpy, "count", 2);
        compare(testModel.status, PlaceSearchModel.Error);
        statusChangedSpy.clear();
        //Aside: there is some difficulty in checking the transition to the Loading state
        //since the model transitions from Loading to Error before the next event loop
        //iteration.

        //try searching with an uninitialized plugin instance.
        testModel.plugin = uninitializedPlugin;
        testModel.update();
        tryCompare(statusChangedSpy, "count", 2);
        compare(testModel.status, PlaceSearchModel.Error);
        statusChangedSpy.clear();

        //try searching with plugin a instance
        //that has been provided a non-existent name
        testModel.plugin = favoritePlugin;
        testModel.update();
        tryCompare(statusChangedSpy, "count", 2);
        compare(testModel.status, PlaceSearchModel.Error);
    }
}