summaryrefslogtreecommitdiffstats
path: root/plugins/organizer/symbian/tsrc/tst_symbianasynchapis/tst_symbianasynchcollections/tst_symbianasynchcollections.cpp
blob: 06e9719a06e76b488607639486c123c8f3f6ff6a (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** 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$
**
****************************************************************************/

#include <QObject>
#include <qmobilityglobal.h>
#include <qtorganizer.h>
#include <QtTest/QtTest>
#include <QDebug>
#include <QColor>

QTM_USE_NAMESPACE

const QString managerNameSymbian("symbian");
const int KTimeToWait = 1000;
const int KNumberOfEntries = 2;

#ifndef QTRY_COMPARE
#define QTRY_COMPARE(__expr, __expected) \
    do { \
        const int __step = 50; \
        const int __timeout = 5000; \
        if ((__expr) != (__expected)) { \
            QTest::qWait(0); \
        } \
        for (int __i = 0; __i < __timeout && ((__expr) != (__expected)); __i+=__step) { \
            QTest::qWait(__step); \
        } \
        QCOMPARE(__expr, __expected); \
    } while(0)
#endif

#define QTRY_COMPARE_SIGNAL_COUNTS() \
    QTRY_COMPARE(addedSpy1.count(), addedCount); \
    QTRY_COMPARE(addedSpy2.count(), addedCount); \
    QTRY_COMPARE(changedSpy1.count(), changedCount); \
    QTRY_COMPARE(changedSpy2.count(), changedCount); \
    QTRY_COMPARE(removedSpy1.count(), removedCount); \
    QTRY_COMPARE(removedSpy2.count(), removedCount);
    
Q_DECLARE_METATYPE(QList<QOrganizerItemId>)
Q_DECLARE_METATYPE(QList<QOrganizerCollectionId>)

class tst_symbianasynchcollections : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void cleanupTestCase();

private slots:
    void saveCollection();
    void fetchCollection();    
        
private slots:
    void saveItem();
    void removeCollection();
    void addCollectionSignals();
    void modifyCollectionSignals();
    void deleteCollectionSignals();
public slots:
   void requestStateChanged(QOrganizerAbstractRequest::State currentState);
   void requestResultsAvailable();
    
private:
   QList<QOrganizerItem> createItems(int noOfItems);
private:
    QOrganizerManager*                  m_om;
    QOrganizerAbstractRequest*          m_itemRequest;
    QList<QOrganizerCollectionId>           m_collectionIds;
};


void tst_symbianasynchcollections::initTestCase()
{
    m_om = new QOrganizerManager(managerNameSymbian);
    m_itemRequest = 0;
}

void tst_symbianasynchcollections::cleanupTestCase()
{
    delete m_om;
    m_om = 0;
    if(m_itemRequest) {
       delete m_itemRequest;
       m_itemRequest = 0;
    }
}

void tst_symbianasynchcollections::fetchCollection()
{
    // Make sure to delete the old request, if any
    delete m_itemRequest;
    // Create new request
    m_itemRequest = new QOrganizerCollectionFetchRequest(this);
    // Set manager
    m_itemRequest->setManager(m_om);

    // Connect for the state change signal 
    connect(m_itemRequest, 
        SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), 
        this, 
        SLOT(requestStateChanged(QOrganizerAbstractRequest::State)));
    connect(m_itemRequest, SIGNAL(resultsAvailable()), 
        this, SLOT(requestResultsAvailable()));

    // Type cast the new request to appropriate type
    QOrganizerCollectionFetchRequest * collectionFetchRequest(
        (QOrganizerCollectionFetchRequest*)m_itemRequest);

    // Start the request
    collectionFetchRequest->start();
    // Wait for KTimeToWait millisecs or until request is finished
    collectionFetchRequest->waitForFinished(KTimeToWait);
    // Verify if the request is finished
    QVERIFY(collectionFetchRequest->isFinished());
}

void tst_symbianasynchcollections::saveCollection()
{
    // Make sure to delete the old request, if any
    delete m_itemRequest;
    // Create new request
    m_itemRequest = new QOrganizerCollectionSaveRequest(this);
    // Set manager
    m_itemRequest->setManager(m_om);

    // Connect for the state change signal 
    connect(m_itemRequest, 
        SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), 
        this, 
        SLOT(requestStateChanged(QOrganizerAbstractRequest::State)));
    connect(m_itemRequest, SIGNAL(resultsAvailable()), 
        this, SLOT(requestResultsAvailable()));

    // Type cast the new request to appropriate type
    QOrganizerCollectionSaveRequest * collectionSaveRequest(
        (QOrganizerCollectionSaveRequest*)m_itemRequest);

    // Set collections
    QList<QOrganizerCollection> collections;
    // Create a collection
    QOrganizerCollection collection;
    collection.setMetaData("Name", "saveCollection");
    collection.setMetaData("FileName", "c:saveCollection");
    collection.setMetaData("Description", "saveCollection test");
    collection.setMetaData("Color", QColor(Qt::red));
    collection.setMetaData("Enabled", true);
    //collections.append(collections);
    // Set collection
    collectionSaveRequest->setCollection(collection);

    // Start the request
    collectionSaveRequest->start();
    // Wait for KTimeToWait millisecs or until request is finished
    collectionSaveRequest->waitForFinished(KTimeToWait);
    // Verify if the request is finished
    QVERIFY(collectionSaveRequest->isFinished());
}

void tst_symbianasynchcollections::removeCollection()
{
    // Make sure to delete the old request, if any
    delete m_itemRequest;
    // Create new request
    m_itemRequest = new QOrganizerCollectionRemoveRequest(this);
    // Set manager
    m_itemRequest->setManager(m_om);

    // Connect for the state change signal 
    connect(m_itemRequest, 
        SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), 
        this, 
        SLOT(requestStateChanged(QOrganizerAbstractRequest::State)));
    connect(m_itemRequest, SIGNAL(resultsAvailable()), 
        this, SLOT(requestResultsAvailable()));

    // Type cast the new request to appropriate type
    QOrganizerCollectionRemoveRequest * collectionRemoveRequest(
        (QOrganizerCollectionRemoveRequest*)m_itemRequest);
    
    // Set collections
    collectionRemoveRequest->setCollectionIds(m_collectionIds);
    // Start the request
    collectionRemoveRequest->start();
    // Wait for KTimeToWait millisecs or until request is finished
    collectionRemoveRequest->waitForFinished(KTimeToWait);
    // Verify if the request is finished
    QVERIFY(collectionRemoveRequest->isFinished());
}

void tst_symbianasynchcollections::saveItem()
{
    // Make sure to delete the old request, if any
    delete m_itemRequest;
    
    // Create new request
    m_itemRequest = new QOrganizerItemSaveRequest(this);
    // Set manager
    m_itemRequest->setManager(m_om);

    // Connect for the state change signal 
    connect(m_itemRequest, 
        SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), 
        this, 
        SLOT(requestStateChanged(QOrganizerAbstractRequest::State)));
    connect(m_itemRequest, SIGNAL(resultsAvailable()), 
        this, SLOT(requestResultsAvailable()));

    // Type cast the new request to appropriate type
    QOrganizerItemSaveRequest * itemSaveRequest(
        (QOrganizerItemSaveRequest*)m_itemRequest);

    itemSaveRequest->setItems(createItems(KNumberOfEntries));
    itemSaveRequest->start();
    itemSaveRequest->waitForFinished(KTimeToWait);
}

QList<QOrganizerItem> tst_symbianasynchcollections::createItems(int noOfItems)
{
    QList<QOrganizerItem> itemsList;
    
    for (int index(0); index < noOfItems; index++) {
        // Create a new organizer item
        QOrganizerItem organizerItem;
        // Set the organizer item type
        organizerItem.setType(QOrganizerItemType::TypeEvent);
        // Create description string 
        QString description("myDescription");
        // Set organizer item description
        organizerItem.setDescription(description);
        // Create desplay label
        QString desplaylabel("myDescription");
        // Set display label
        organizerItem.setDisplayLabel(desplaylabel);
        
        // Set current time
        QOrganizerEventTime timeRange;
        QDateTime startTime;
        startTime.currentDateTime();
        timeRange.setStartDateTime(startTime.currentDateTime());
        
        QVERIFY(organizerItem.saveDetail(&timeRange));
        
        itemsList.append(organizerItem);
    }
    
    return itemsList;
}

void tst_symbianasynchcollections::requestStateChanged(
    QOrganizerAbstractRequest::State currentState)
{
    switch(currentState) {
    case QOrganizerAbstractRequest::InactiveState: {
        // Verify if the request is in inactive state
        QVERIFY(m_itemRequest->isInactive());
        // Compare the request state is set rightly
        QCOMPARE(m_itemRequest->state(), 
            QOrganizerAbstractRequest::InactiveState);
        // Operation not yet started start the operation
        m_itemRequest->start();
    }
    break;
    case QOrganizerAbstractRequest::ActiveState: {
        // Verify if the request is in active state
        QVERIFY(m_itemRequest->isActive());
        // Operation started, not yet finished operation already started
        // Compare the request state is set rightly
        QCOMPARE(m_itemRequest->state(), 
            QOrganizerAbstractRequest::ActiveState);
    }
    break;
    case QOrganizerAbstractRequest::CanceledState: {
        // Verify if the request is in canceled state
        QVERIFY(m_itemRequest->isCanceled());
        // Operation is finished due to cancellation test not completed, 
        // failed Compare the request state is set rightly
        QCOMPARE(m_itemRequest->state(), 
            QOrganizerAbstractRequest::CanceledState);
    }
    break;
    case QOrganizerAbstractRequest::FinishedState: {
        // Verify if the request is in finished state
        QVERIFY(m_itemRequest->isFinished());
        // Operation either completed successfully or failed.  
        // No further results will be available.
        // test completed, compare the results
        // Compare the request state is set rightly
        QCOMPARE(m_itemRequest->state(), 
            QOrganizerAbstractRequest::FinishedState);
    }
    break;
    default: {
        // Not handled
    }
    break;
    }
}

void tst_symbianasynchcollections::requestResultsAvailable()
{
    QOrganizerAbstractRequest::RequestType reqType(m_itemRequest->type());
    switch (reqType) {
    case QOrganizerAbstractRequest::CollectionFetchRequest : {
        // Check error map
        QList<QOrganizerCollection> collections(
            ((QOrganizerCollectionFetchRequest*)(m_itemRequest))->collections());
        // Error map should contain zero errors to indicate successful deletion
        // of all the items
        int count(collections.count());
        QCOMPARE(count, 2);
    }
    break;
    case QOrganizerAbstractRequest::CollectionSaveRequest : {
        QList<QOrganizerCollection> savedCollections(
            ((QOrganizerCollectionSaveRequest*)(m_itemRequest))->collections());
        int count(savedCollections.count());
        m_collectionIds.clear();
        for (int index(0); index < count; index++) {
            m_collectionIds.append(savedCollections.at(index).id());
        }
        // Check the number of requests saved. magic number to be changed to a
        // constant
        QCOMPARE(count, 1);
        
        QMap<int, QOrganizerManager::Error> errorMap(
            ((QOrganizerCollectionSaveRequest*)(m_itemRequest))->errorMap());
        // Error map should contain zero errors to indicate successful saving
        // of all the collections
        QCOMPARE(0, errorMap.count());
    }
    break;
    case QOrganizerAbstractRequest::CollectionRemoveRequest : {
        // Check error map
        QMap<int, QOrganizerManager::Error> erroMap(
            ((QOrganizerCollectionRemoveRequest*)(m_itemRequest))->errorMap());
        // Error map should contain zero errors to indicate successful deletion
        // of all the items
        int count(erroMap.count());
        QCOMPARE(0, count);
    }
    break;
    }
}

//Signal emission testcases
void tst_symbianasynchcollections::addCollectionSignals()
{        
    qRegisterMetaType<QOrganizerCollectionId>("QOrganizerCollectionId");
    qRegisterMetaType<QList<QOrganizerCollectionId> >("QList<QOrganizerCollectionId>");
    qRegisterMetaType<QOrganizerAbstractRequest::State>("QOrganizerAbstractRequest::State");
    
    // Create a second manager
    QScopedPointer<QOrganizerManager> om2(new QOrganizerManager(m_om->managerName()));

    // Create request
    QOrganizerCollectionSaveRequest *req = new QOrganizerCollectionSaveRequest(this);
    req->setManager(m_om);

    // Setup signal spies
    QSignalSpy stateSpy(req, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)));
    QSignalSpy resultSpy(req, SIGNAL(resultsAvailable()));
    QSignalSpy addedSpy1(m_om, SIGNAL(collectionsAdded(QList<QOrganizerCollectionId>)));
    QSignalSpy addedSpy2(om2.data(), SIGNAL(collectionsAdded(QList<QOrganizerCollectionId>)));
  
    // Create a collection
    QOrganizerCollection collection;
    collection.setMetaData("Name", "saveEmission");
    collection.setMetaData("FileName", "c:saveEmission");
    collection.setMetaData("Description", "saveEmission test");
    collection.setMetaData("Color", QColor(Qt::red));
    collection.setMetaData("Enabled", true);
    req->setCollection(collection);

    // Start the request
    QVERIFY(req->start());
    QCOMPARE(req->state(), QOrganizerAbstractRequest::ActiveState);
    QCOMPARE(stateSpy.count(), 1);
    QTRY_COMPARE(resultSpy.count(), 1);
    QCOMPARE(req->collections().count(), 1);
    QVERIFY(!req->collections().at(0).id().isNull());
    QVERIFY(!req->collections().at(0).id().managerUri().isEmpty());
    // Verify the count of collectionsAdded signals on both managers
    QTRY_COMPARE(addedSpy1.count(), 1);
    QTRY_COMPARE(addedSpy2.count(), 1);
    // Verify the argument counts of the collectionsAdded signals
    QCOMPARE(addedSpy1.last().count(), 1);
    QCOMPARE(addedSpy2.last().count(), 1);
    // Verify the arguments contain the id of the new collection
    QCOMPARE(addedSpy1.last().at(0).value<QList<QOrganizerCollectionId> >().count(), 1);
    QCOMPARE(addedSpy2.last().at(0).value<QList<QOrganizerCollectionId> >().count(), 1);
    QCOMPARE(addedSpy1.last().at(0).value<QList<QOrganizerCollectionId> >().at(0), req->collections().at(0).id());
    QCOMPARE(addedSpy2.last().at(0).value<QList<QOrganizerCollectionId> >().at(0), req->collections().at(0).id());
    delete req;
}

void tst_symbianasynchcollections::modifyCollectionSignals()
{
    qRegisterMetaType<QOrganizerCollectionId>("QOrganizerCollectionId");
    qRegisterMetaType<QList<QOrganizerCollectionId> >("QList<QOrganizerCollectionId>");
    qRegisterMetaType<QList<QOrganizerAbstractRequest::State> >("QOrganizerAbstractRequest::State>");
    
    // Create a second manager
    QScopedPointer<QOrganizerManager> om2(new QOrganizerManager(m_om->managerName()));

    // Create request
    QOrganizerCollectionSaveRequest *req = new QOrganizerCollectionSaveRequest(this);
    req->setManager(m_om);

    // Setup signal spies
    QSignalSpy stateSpy(req, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)));
    QSignalSpy resultSpy(req, SIGNAL(resultsAvailable()));
    QSignalSpy changedSpy1(m_om, SIGNAL(collectionsChanged(QList<QOrganizerCollectionId>)));
    QSignalSpy changedSpy2(om2.data(), SIGNAL(collectionsChanged(QList<QOrganizerCollectionId>)));

     
    //Fetch the saved collection
    QOrganizerCollection savedCollection = m_om->collections().at(1);
    
    savedCollection.setMetaData("Name", "modifyEmission");
    req->setCollection(savedCollection);

    // Start the request
    QVERIFY(req->start());
    QCOMPARE(req->state(), QOrganizerAbstractRequest::ActiveState);
    QCOMPARE(stateSpy.count(), 1);
    QTRY_COMPARE(resultSpy.count(), 1);
    QCOMPARE(req->collections().count(), 1);
    QVERIFY(!req->collections().at(0).id().isNull());
    QVERIFY(!req->collections().at(0).id().managerUri().isEmpty());
    // Verify the count of collectionsChanged signals on both managers
    QTRY_COMPARE(changedSpy1.count(), 1);
    QTRY_COMPARE(changedSpy2.count(), 1);
    // Verify the argument counts of the collectionsChanged signals
    QCOMPARE(changedSpy1.last().count(), 1);
    QCOMPARE(changedSpy2.last().count(), 1);
    // Verify the arguments contain the id of the modified collection
    QCOMPARE(changedSpy1.last().at(0).value<QList<QOrganizerCollectionId> >().count(), 1);
    QCOMPARE(changedSpy2.last().at(0).value<QList<QOrganizerCollectionId> >().count(), 1);
    QCOMPARE(changedSpy1.last().at(0).value<QList<QOrganizerCollectionId> >().at(0), req->collections().at(0).id());
    QCOMPARE(changedSpy2.last().at(0).value<QList<QOrganizerCollectionId> >().at(0), req->collections().at(0).id());
    //QCOMPARE(req.collections().at(0).metaData().value("Name"),savedCollection.metaData().value("Name"));
    
    delete req;
}

void tst_symbianasynchcollections::deleteCollectionSignals()
{
    // Make sure to delete the old request, if any
    delete m_itemRequest;
    m_itemRequest =0;
    qRegisterMetaType<QOrganizerCollectionId>("QOrganizerCollectionId");
    qRegisterMetaType<QList<QOrganizerCollectionId> >("QList<QOrganizerCollectionId>");
    qRegisterMetaType<QOrganizerAbstractRequest::State>("QOrganizerAbstractRequest::State");
    
    // Create a second manager
    QScopedPointer<QOrganizerManager> om2(new QOrganizerManager(m_om->managerName()));

    // Create request
    QOrganizerCollectionRemoveRequest *deleteReq = new QOrganizerCollectionRemoveRequest(this);
    deleteReq->setManager(m_om);

    // Setup signal spies
    QSignalSpy stateSpy(deleteReq, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)));
    QSignalSpy resultSpy(deleteReq, SIGNAL(resultsAvailable()));
    QSignalSpy removedSpy1(m_om, SIGNAL(collectionsRemoved(QList<QOrganizerCollectionId>)));
    QSignalSpy removedSpy2(om2.data(), SIGNAL(collectionsRemoved(QList<QOrganizerCollectionId>)));
     
     //Fetch the saved collection
     QOrganizerCollectionId savedCollectionId = m_om->collections().at(1).id();
     int countBeforeDeletion = m_om->collections().count();
     qWarning() << countBeforeDeletion << "calendar/s are present currently for deletion";
    
    deleteReq->setCollectionId(savedCollectionId);

    // Start the request
    QVERIFY(deleteReq->start());
    QCOMPARE(deleteReq->state(), QOrganizerAbstractRequest::ActiveState);
    QCOMPARE(stateSpy.count(), 1);
    QTRY_COMPARE(resultSpy.count(), 1);
    
    // Verify the count of collectionsRemoved signals on both managers
    QTRY_COMPARE(removedSpy1.count(), 1);
    QTRY_COMPARE(removedSpy2.count(), 1);
    // Verify the argument counts of the collectionsRemoved signals
    QCOMPARE(removedSpy1.last().count(), 1);
    QCOMPARE(removedSpy2.last().count(), 1);
    // Verify the arguments contain the id of the removed collection
    QCOMPARE(removedSpy1.last().at(0).value<QList<QOrganizerCollectionId> >().count(), 1);
    QCOMPARE(removedSpy2.last().at(0).value<QList<QOrganizerCollectionId> >().count(), 1);
    QCOMPARE(removedSpy1.last().at(0).value<QList<QOrganizerCollectionId> >().at(0), savedCollectionId);
    QCOMPARE(removedSpy2.last().at(0).value<QList<QOrganizerCollectionId> >().at(0), savedCollectionId);
    QCOMPARE(m_om->collections().count(), 1);
    delete deleteReq;
}

QTEST_MAIN(tst_symbianasynchcollections);
#include "tst_symbianasynchcollections.moc"