summaryrefslogtreecommitdiff
path: root/tests/auto/placemanager_utils/placemanager_utils.cpp
blob: 00386a12a0a9f2198b161aceae006eeb6ecf9d75 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "placemanager_utils.h"

#include <QtCore/QDebug>
#include <QtLocation/QPlace>
#include <QtLocation/QPlaceCategory>
#include <QtLocation/QPlaceContentReply>
#include <QtLocation/QPlaceDetailsReply>
#include <QtLocation/QPlaceIdReply>
#include <QtLocation/QPlaceManager>
#include <QtLocation/QPlaceMatchReply>
#include <QtLocation/QPlaceSearchReply>
#include <QtLocation/QPlaceSearchSuggestionReply>
#include <QtLocation/QPlaceResult>
#include <QtTest/QSignalSpy>
#include <QtTest/QTest>

//constant for timeout to verify signals
const int PlaceManagerUtils::Timeout(10000);

PlaceManagerUtils::PlaceManagerUtils(QObject *parent)
    : QObject(parent), placeManager(0)
{
}

bool PlaceManagerUtils::doSavePlace(QPlaceManager *manager,
                                    const QPlace &place,
                                    QPlaceReply::Error expectedError,
                                    QString *placeId)
{
    Q_ASSERT(manager);
    QPlaceIdReply *saveReply = manager->savePlace(place);
    bool isSuccessful = checkSignals(saveReply, expectedError, manager);
    if (placeId != 0) {
        *placeId = saveReply->id();
    }

    if (saveReply->id().isEmpty() && expectedError == QPlaceReply::NoError) {
        qWarning("ID is empty in reply for save operation");
        qWarning() << "Error string = " << saveReply->errorString();
        isSuccessful = false;
    }

    if (!isSuccessful)
        qWarning() << "Error string = " << saveReply->errorString();

    return isSuccessful;
}

void PlaceManagerUtils::doSavePlaces(QPlaceManager *manager, QList<QPlace> &places)
{
    QPlaceIdReply *saveReply;

    for (const auto &place : places) {
        saveReply = manager->savePlace(place);
        QSignalSpy saveSpy(saveReply, SIGNAL(finished()));
        QTRY_VERIFY_WITH_TIMEOUT(saveSpy.count() == 1, Timeout);
        QCOMPARE(saveReply->error(), QPlaceReply::NoError);
        saveSpy.clear();
    }
}

void PlaceManagerUtils::doSavePlaces(QPlaceManager *manager, const QList<QPlace *> &places)
{
    QPlaceIdReply *saveReply;

    static int count= 0;
    for (QPlace *place : places) {
        count++;
        saveReply = manager->savePlace(*place);
        QSignalSpy saveSpy(saveReply, SIGNAL(finished()));
        QTRY_VERIFY_WITH_TIMEOUT(saveSpy.count() == 1, Timeout);
        QCOMPARE(saveReply->error(), QPlaceReply::NoError);
        place->setPlaceId(saveReply->id());
        saveSpy.clear();
    }
}

bool PlaceManagerUtils::doSearch(QPlaceManager *manager,
                                const QPlaceSearchRequest &request,
                                 QList<QPlaceSearchResult> *results,
                                 QPlaceReply::Error expectedError)
{
    QPlaceSearchReply *searchReply= manager->search(request);
    bool success = checkSignals(searchReply, expectedError, manager);
    *results = searchReply->results();
    return success;
}

bool PlaceManagerUtils::doSearch(QPlaceManager *manager,
                                       const QPlaceSearchRequest &request,
                                       QList<QPlace> *results, QPlaceReply::Error expectedError)
{
    bool success = false;
    results->clear();
    QList<QPlaceSearchResult> searchResults;
    success = doSearch(manager, request, &searchResults, expectedError);
    for (const QPlaceSearchResult &searchResult : searchResults) {
        if (searchResult.type() == QPlaceSearchResult::PlaceResult) {
            QPlaceResult placeResult = searchResult;
            results->append(placeResult.place());
        }
    }
    return success;
}

bool PlaceManagerUtils::doSearchSuggestions(QPlaceManager *manager,
                                            const QPlaceSearchRequest &request,
                                            QStringList *results,
                                            QPlaceReply::Error expectedError)
{
    QPlaceSearchSuggestionReply *reply = manager->searchSuggestions(request);
    bool success = checkSignals(reply, expectedError, manager);
    *results = reply->suggestions();

    if (!success)
        qDebug() << "Error string = " << reply->errorString();

    return success;
}

bool PlaceManagerUtils::doRemovePlace(QPlaceManager *manager,
                                      const QPlace &place,
                                      QPlaceReply::Error expectedError)
{
    QPlaceIdReply *removeReply = manager->removePlace(place.placeId());
    bool isSuccessful = false;
    isSuccessful = checkSignals(removeReply, expectedError, manager)
                    && (removeReply->id() == place.placeId());

    if (!isSuccessful)
        qWarning() << "Place removal unsuccessful errorString = " << removeReply->errorString();

    return isSuccessful;
}

bool PlaceManagerUtils::doFetchDetails(QPlaceManager *manager,
                                             QString placeId, QPlace *place,
                                             QPlaceReply::Error expectedError)
{
    QPlaceDetailsReply *detailsReply = manager->getPlaceDetails(placeId);
    bool success = checkSignals(detailsReply, expectedError, manager);
    *place = detailsReply->place();

    if (!success)
        qDebug() << "Error string = " << detailsReply->errorString();

    return success;
}

bool PlaceManagerUtils::doInitializeCategories(QPlaceManager *manager,
                                               QPlaceReply::Error expectedError)
{
    QPlaceReply *reply = manager->initializeCategories();
    bool success = checkSignals(reply, expectedError, manager);

    if (!success)
        qDebug() << "Error string = " << reply->errorString();

    delete reply;
    return success;
}

bool PlaceManagerUtils::doSaveCategory(QPlaceManager *manager,
                                       const QPlaceCategory &category,
                                       const QString &parentId,
                                       QPlaceReply::Error expectedError,
                                       QString *categoryId)
{
    QPlaceIdReply *idReply = manager->saveCategory(category, parentId);
    bool isSuccessful = checkSignals(idReply, expectedError, manager)
                        && (idReply->error() == expectedError);

    if (categoryId != 0)
        *categoryId = idReply->id();

    if (!isSuccessful)
        qDebug() << "Error string =" << idReply->errorString();
    return isSuccessful;
}

bool PlaceManagerUtils::doRemoveCategory(QPlaceManager *manager,
                                         const QPlaceCategory &category,
                                         QPlaceReply::Error expectedError)
{
    QPlaceIdReply *idReply = manager->removeCategory(category.categoryId());

    bool isSuccessful = checkSignals(idReply, expectedError, manager) &&
                        (idReply->error() == expectedError);
    return isSuccessful;
}

bool PlaceManagerUtils::doFetchCategory(QPlaceManager *manager,
                                        const QString &categoryId,
                                        QPlaceCategory *category,
                                        QPlaceReply::Error expectedError)
{
    Q_ASSERT(category);
    QPlaceReply * catInitReply = manager->initializeCategories();
    bool isSuccessful = checkSignals(catInitReply, expectedError, manager);
    *category = manager->category(categoryId);

    if (!isSuccessful)
        qDebug() << "Error initializing categories, error string = "
                 << catInitReply->errorString();

    if (category->categoryId() != categoryId)
        isSuccessful = false;
    return isSuccessful;
}

bool PlaceManagerUtils::doFetchContent(QPlaceManager *manager,
                                       const QPlaceContentRequest &request,
                                       QPlaceContent::Collection *results,
                                       QPlaceReply::Error expectedError)
{
    Q_ASSERT(results);
    QPlaceContentReply *reply = manager->getPlaceContent(request);
    bool isSuccessful = checkSignals(reply, expectedError, manager);
    *results = reply->content();

    if (!isSuccessful)
        qDebug() << "Error during content fetch, error string = "
                 << reply->errorString();

    return isSuccessful;
}

bool PlaceManagerUtils::doMatch(QPlaceManager *manager,
                                const QPlaceMatchRequest &request,
                                QList<QPlace> *places,
                                QPlaceReply::Error expectedError)
{
    QPlaceMatchReply *reply = manager->matchingPlaces(request);
    bool isSuccessful = checkSignals(reply, expectedError, manager) &&
            (reply->error() == expectedError);
    *places = reply->places();
    if (!isSuccessful)
        qDebug() << "Error for matching operation, error string = "
                 << reply->errorString();
    return isSuccessful;
}

bool PlaceManagerUtils::checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError,
                                           QPlaceManager *manager)
{
    Q_ASSERT(reply);
    QSignalSpy finishedSpy(reply, SIGNAL(finished()));
    QSignalSpy errorSpy(reply, SIGNAL(errorOccurred(QPlaceReply::Error,QString)));
    QSignalSpy managerFinishedSpy(manager, SIGNAL(finished(QPlaceReply*)));
    QSignalSpy managerErrorSpy(manager,SIGNAL(errorOccurred(QPlaceReply*,QPlaceReply::Error,QString)));

    if (expectedError != QPlaceReply::NoError) {
        //check that we get an error signal from the reply
        WAIT_UNTIL(errorSpy.count() == 1);
        if (errorSpy.count() != 1) {
            qWarning() << "Error signal for search operation not received";
            return false;
        }

        //check that we get the correct error from the reply's signal
        QPlaceReply::Error actualError = qvariant_cast<QPlaceReply::Error>(errorSpy.at(0).at(0));
        if (actualError != expectedError) {
            qWarning() << "Actual error code in reply signal does not match expected error code";
            qWarning() << "Actual error code = " << actualError;
            qWarning() << "Expected error coe =" << expectedError;
            return false;
        }

        //check that we get an error  signal from the manager
        WAIT_UNTIL(managerErrorSpy.count() == 1);
        if (managerErrorSpy.count() !=1) {
           qWarning() << "Error signal from manager for search operation not received";
           return false;
        }

        //check that we get the correct reply instance in the error signal from the manager
        if (qvariant_cast<QPlaceReply*>(managerErrorSpy.at(0).at(0)) != reply)  {
            qWarning() << "Reply instance in error signal from manager is incorrect";
            return false;
        }

        //check that we get the correct error from the signal of the manager
        actualError = qvariant_cast<QPlaceReply::Error>(managerErrorSpy.at(0).at(1));
        if (actualError != expectedError) {
            qWarning() << "Actual error code from manager signal does not match expected error code";
            qWarning() << "Actual error code =" << actualError;
            qWarning() << "Expected error code = " << expectedError;
            return false;
        }
    }

    //check that we get a finished signal
    WAIT_UNTIL(finishedSpy.count() == 1);
    if (finishedSpy.count() !=1) {
        qWarning() << "Finished signal from reply not received";
        return false;
    }

    if (reply->error() != expectedError) {
        qWarning() << "Actual error code does not match expected error code";
        qWarning() << "Actual error code: " << reply->error();
        qWarning() << "Expected error code" << expectedError;
        return false;
    }

    if (expectedError == QPlaceReply::NoError && !reply->errorString().isEmpty()) {
        qWarning() << "Expected error was no error but error string was not empty";
        qWarning() << "Error string=" << reply->errorString();
        return false;
    }

    //check that we get the finished signal from the manager
    WAIT_UNTIL(managerFinishedSpy.count() == 1);
    if (managerFinishedSpy.count() != 1) {
        qWarning() << "Finished signal from manager not received";
        return false;
    }

    //check that the reply instance in the finished signal from the manager is correct
    if (qvariant_cast<QPlaceReply *>(managerFinishedSpy.at(0).at(0)) != reply) {
        qWarning() << "Reply instance in finished signal from manager is incorrect";
        return false;
    }

    return true;
}

bool PlaceManagerUtils::compare(const QList<QPlace> &actualResults,
                                      const QList<QPlace> &expectedResults)
{
    QSet<QString> actualIds;
    for (const QPlace &place : actualResults)
        actualIds.insert(place.placeId());

    QSet<QString> expectedIds;
    for (const QPlace &place : expectedResults)
        expectedIds.insert(place.placeId());

    bool isMatch = (actualIds == expectedIds);
    if (actualResults.count() != expectedResults.count() || !isMatch) {
        qWarning() << "comparison of results by name does not match";
        qWarning() << "actual result ids: " << actualIds;
        qWarning() << "expected result ids : " << expectedIds;
        return false;
    }

    return isMatch;
}

void PlaceManagerUtils::setVisibility(QList<QPlace *> places, QLocation::Visibility visibility)
{
    for (QPlace *place : places)
        place->setVisibility(visibility);
}