| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | 
| 4 | ** Contact: http://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | 
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | 
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | 
| 22 | ** following information to ensure the GNU Lesser General Public License | 
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | 
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
| 25 | ** | 
| 26 | ** As a special exception, The Qt Company gives you certain additional | 
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | 
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
| 29 | ** | 
| 30 | ** $QT_END_LICENSE$ | 
| 31 | ** | 
| 32 | ****************************************************************************/ | 
| 33 |  | 
| 34 | #include <QtVersit/qversitcontacthandler.h> | 
| 35 | #include <QtVersit/qversitproperty.h> | 
| 36 | #include <QtContacts/qcontactname.h> | 
| 37 |  | 
| 38 | #include <QObject> | 
| 39 | #include <QtTest/QtTest> | 
| 40 |  | 
| 41 | QTCONTACTS_USE_NAMESPACE | 
| 42 | QTVERSIT_USE_NAMESPACE | 
| 43 |  | 
| 44 | class TestFactory1 : public QVersitContactHandlerFactory | 
| 45 | { | 
| 46 |     public: | 
| 47 |         QString name() const { return "factory1" ; } | 
| 48 |         QVersitContactHandler* createHandler() const { return NULL; } | 
| 49 | }; | 
| 50 |  | 
| 51 | class tst_QVersitContactPlugins : public QObject | 
| 52 | { | 
| 53 |     Q_OBJECT | 
| 54 |  | 
| 55 | public slots: | 
| 56 |     void init(); | 
| 57 |  | 
| 58 | private slots: | 
| 59 |     void testDefaultFactory(); | 
| 60 |     void testImporterPlugins(); | 
| 61 |     void testExporterPlugins(); | 
| 62 | }; | 
| 63 |  | 
| 64 | void tst_QVersitContactPlugins::init() { | 
| 65 |     // on windows, depending on how the unit test is invoked, we need these | 
| 66 |     QCoreApplication::addLibraryPath("./debug/plugins/" ); | 
| 67 |     QCoreApplication::addLibraryPath("./release/plugins/" ); | 
| 68 |     QCoreApplication::addLibraryPath("./plugins/" ); | 
| 69 | } | 
| 70 |  | 
| 71 | void tst_QVersitContactPlugins::testDefaultFactory() { | 
| 72 |     TestFactory1 factory; | 
| 73 |     QCOMPARE(factory.profiles(), QSet<QString>()); | 
| 74 |     QCOMPARE(factory.index(), 0); | 
| 75 | } | 
| 76 |  | 
| 77 | void tst_QVersitContactPlugins::testImporterPlugins() { | 
| 78 |     QVersitContactImporter importer("Test" ); | 
| 79 |     QVersitDocument document; | 
| 80 |     document.setComponentType("VCARD" ); | 
| 81 |     QVersitProperty property; | 
| 82 |     property.setName("FN" ); | 
| 83 |     property.setValue("Bob" ); | 
| 84 |     document.addProperty(property); | 
| 85 |     QVERIFY(importer.importDocuments(QList<QVersitDocument>() << document)); | 
| 86 |     QCOMPARE(importer.contacts().size(), 1); | 
| 87 |     QList<QContactDetail> details(importer.contacts().first().details(type: QContactDetail::TypeExtendedDetail)); | 
| 88 |     QCOMPARE(details.size(), 5); | 
| 89 |     int pluginField = 0; | 
| 90 |     // The plugins have had their index set such that they should be executed in reverse order | 
| 91 |     // Check that they are all loaded, and run in the correct order | 
| 92 |     QCOMPARE(details.at(0).value<int>(pluginField), 5); | 
| 93 |     QCOMPARE(details.at(1).value<int>(pluginField), 4); | 
| 94 |     QCOMPARE(details.at(2).value<int>(pluginField), 3); | 
| 95 |     QCOMPARE(details.at(3).value<int>(pluginField), 2); | 
| 96 |     QCOMPARE(details.at(4).value<int>(pluginField), 1); | 
| 97 | } | 
| 98 |  | 
| 99 | void tst_QVersitContactPlugins::testExporterPlugins() { | 
| 100 |     QVersitContactExporter exporter("Test" ); | 
| 101 |     QContact contact; | 
| 102 |     QContactName name; | 
| 103 |     name.setFirstName("first name" ); | 
| 104 |     contact.saveDetail(detail: &name); | 
| 105 |     QVERIFY(exporter.exportContacts(QList<QContact>() << contact)); | 
| 106 |     QCOMPARE(exporter.documents().size(), 1); | 
| 107 |     QList<QVersitProperty> properties(exporter.documents().first().properties()); | 
| 108 |  | 
| 109 |     // The plugins have had their index set such that they should be executed in reverse order | 
| 110 |     // Check that they are all loaded, and run in the correct order | 
| 111 |     int n = 0; | 
| 112 |     foreach (QVersitProperty property, properties) { | 
| 113 |         if (property.name() == "TEST-PROPERTY" ) { | 
| 114 |             switch (n) { | 
| 115 |                 case 0: QCOMPARE(property.value(), QStringLiteral("5" )); break; | 
| 116 |                 case 1: QCOMPARE(property.value(), QStringLiteral("4" )); break; | 
| 117 |                 case 2: QCOMPARE(property.value(), QStringLiteral("3" )); break; | 
| 118 |                 case 3: QCOMPARE(property.value(), QStringLiteral("2" )); break; | 
| 119 |                 case 4: QCOMPARE(property.value(), QStringLiteral("1" )); break; | 
| 120 |             } | 
| 121 |             n++; | 
| 122 |         } | 
| 123 |     } | 
| 124 |     QCOMPARE(n, 5); | 
| 125 | } | 
| 126 |  | 
| 127 | QTEST_MAIN(tst_QVersitContactPlugins) | 
| 128 |  | 
| 129 | #include "tst_qversitcontactplugins.moc" | 
| 130 |  |