| 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 | //TESTED_COMPONENT=src/versit |
| 35 | |
| 36 | #include "tst_qversitdocument.h" |
| 37 | #include <QtVersit/qversitdocument.h> |
| 38 | #include <QtVersit/qversitproperty.h> |
| 39 | #include <QString> |
| 40 | #include <QtTest/QtTest> |
| 41 | |
| 42 | QTVERSIT_USE_NAMESPACE |
| 43 | |
| 44 | void tst_QVersitDocument::init() |
| 45 | { |
| 46 | mVersitDocument = new QVersitDocument(); |
| 47 | QVERIFY(mVersitDocument); |
| 48 | } |
| 49 | |
| 50 | void tst_QVersitDocument::cleanup() |
| 51 | { |
| 52 | delete mVersitDocument; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void tst_QVersitDocument::testConstructor() |
| 57 | { |
| 58 | QCOMPARE(QVersitDocument::InvalidType, mVersitDocument->type()); |
| 59 | } |
| 60 | |
| 61 | void tst_QVersitDocument::testType() |
| 62 | { |
| 63 | mVersitDocument->setType(QVersitDocument::VCard21Type); |
| 64 | QCOMPARE(QVersitDocument::VCard21Type, mVersitDocument->type()); |
| 65 | |
| 66 | mVersitDocument->setType(QVersitDocument::VCard30Type); |
| 67 | QCOMPARE(QVersitDocument::VCard30Type, mVersitDocument->type()); |
| 68 | } |
| 69 | |
| 70 | void tst_QVersitDocument::testAddProperty() |
| 71 | { |
| 72 | QCOMPARE(0, mVersitDocument->properties().count()); |
| 73 | QVersitProperty property; |
| 74 | mVersitDocument->addProperty(property); |
| 75 | QCOMPARE(1, mVersitDocument->properties().count()); |
| 76 | } |
| 77 | |
| 78 | void tst_QVersitDocument::testRemoveProperty() |
| 79 | { |
| 80 | // Remove an empty property. |
| 81 | QCOMPARE(mVersitDocument->properties().count(), 0); |
| 82 | QVersitProperty property; |
| 83 | mVersitDocument->addProperty(property); |
| 84 | mVersitDocument->removeProperty(property); |
| 85 | QCOMPARE(mVersitDocument->properties().count(), 0); |
| 86 | |
| 87 | // A full property. |
| 88 | property.setName(QStringLiteral("TEL" )); |
| 89 | property.setGroups(QStringList(QStringLiteral("HOME" ))); |
| 90 | QMultiHash<QString, QString> params; |
| 91 | params.insert(QStringLiteral("TYPE" ), QStringLiteral("HOME" )); |
| 92 | property.setParameters(params); |
| 93 | property.setValue(QStringLiteral("123" )); |
| 94 | mVersitDocument->addProperty(property); |
| 95 | QCOMPARE(mVersitDocument->properties().count(), 1); |
| 96 | QVersitProperty property2; |
| 97 | property2.setName(QStringLiteral("TEL" )); |
| 98 | // Remove with a partial property fails. |
| 99 | mVersitDocument->removeProperty(property: property2); |
| 100 | QCOMPARE(mVersitDocument->properties().count(), 1); |
| 101 | property2.setGroups(QStringList(QStringLiteral("HOME" ))); |
| 102 | property2.setParameters(params); |
| 103 | property2.setValue(QStringLiteral("123" )); |
| 104 | // Remove with a fully specified property succeeds. |
| 105 | mVersitDocument->removeProperty(property: property2); |
| 106 | QCOMPARE(mVersitDocument->properties().count(), 0); |
| 107 | } |
| 108 | |
| 109 | void tst_QVersitDocument::testRemoveAllProperties() |
| 110 | { |
| 111 | QString name(QStringLiteral("FN" )); |
| 112 | |
| 113 | // Try to remove from an empty document |
| 114 | QCOMPARE(0, mVersitDocument->properties().count()); |
| 115 | mVersitDocument->removeProperties(name); |
| 116 | QCOMPARE(0, mVersitDocument->properties().count()); |
| 117 | |
| 118 | // Try to remove from a non-empty document, name does not match |
| 119 | QVersitProperty property; |
| 120 | property.setName(QStringLiteral("TEL" )); |
| 121 | mVersitDocument->addProperty(property); |
| 122 | QCOMPARE(1, mVersitDocument->properties().count()); |
| 123 | mVersitDocument->removeProperties(name); |
| 124 | QCOMPARE(1, mVersitDocument->properties().count()); |
| 125 | |
| 126 | // Remove from a non-empty document, name matches |
| 127 | mVersitDocument->removeProperties(QStringLiteral("TEL" )); |
| 128 | QCOMPARE(0, mVersitDocument->properties().count()); |
| 129 | |
| 130 | // Remove from a document with two properties, first matches |
| 131 | property.setName(name); |
| 132 | mVersitDocument->addProperty(property); |
| 133 | property.setName(QStringLiteral("TEL" )); |
| 134 | mVersitDocument->addProperty(property); |
| 135 | QCOMPARE(2, mVersitDocument->properties().count()); |
| 136 | mVersitDocument->removeProperties(name); |
| 137 | QCOMPARE(1, mVersitDocument->properties().count()); |
| 138 | |
| 139 | // Remove from a document with two properties, second matches |
| 140 | property.setName(name); |
| 141 | mVersitDocument->addProperty(property); |
| 142 | QCOMPARE(2, mVersitDocument->properties().count()); |
| 143 | mVersitDocument->removeProperties(name); |
| 144 | QCOMPARE(1, mVersitDocument->properties().count()); |
| 145 | } |
| 146 | |
| 147 | void tst_QVersitDocument::testEquality() |
| 148 | { |
| 149 | QVersitDocument document1; |
| 150 | QVersitDocument document2; |
| 151 | QVERIFY(document1.isEmpty()); |
| 152 | QVERIFY(document1 == document2); |
| 153 | QVERIFY(!(document1 != document2)); |
| 154 | QVersitProperty property; |
| 155 | property.setName(QStringLiteral("FN" )); |
| 156 | property.setValue(QStringLiteral("John Citizen" )); |
| 157 | document2.addProperty(property); |
| 158 | QVERIFY(!(document1 == document2)); |
| 159 | QVERIFY(document1 != document2); |
| 160 | QVERIFY(!document2.isEmpty()); |
| 161 | |
| 162 | document1.addProperty(property); |
| 163 | QVERIFY(document1 == document2); |
| 164 | QVERIFY(!(document1 != document2)); |
| 165 | |
| 166 | document2.clear(); |
| 167 | QVERIFY(document2.isEmpty()); |
| 168 | |
| 169 | document1.clear(); |
| 170 | QVERIFY(document1 == document2); |
| 171 | QVERIFY(!(document1 != document2)); |
| 172 | |
| 173 | document2.setType(QVersitDocument::VCard21Type); |
| 174 | QVERIFY(!(document1 == document2)); |
| 175 | QVERIFY(document1 != document2); |
| 176 | QVERIFY(!document2.isEmpty()); |
| 177 | |
| 178 | document2 = document1; |
| 179 | QVERIFY(document1 == document2); |
| 180 | } |
| 181 | |
| 182 | void tst_QVersitDocument::testHash() |
| 183 | { |
| 184 | QVersitDocument document1; |
| 185 | document1.setType(QVersitDocument::VCard30Type); |
| 186 | QVersitProperty property1; |
| 187 | property1.setName(QStringLiteral("name" )); |
| 188 | property1.setValue(QStringLiteral("value" )); |
| 189 | document1.addProperty(property: property1); |
| 190 | |
| 191 | QVersitDocument document2; |
| 192 | document2.setType(QVersitDocument::VCard30Type); |
| 193 | document2.addProperty(property: property1); |
| 194 | |
| 195 | QVersitDocument document3; |
| 196 | document3.setType(QVersitDocument::VCard30Type); |
| 197 | QVersitProperty property3; |
| 198 | property3.setName(QStringLiteral("name" )); |
| 199 | property3.setValue(QStringLiteral("another value" )); |
| 200 | document3.addProperty(property: property3); |
| 201 | |
| 202 | QVersitDocument document4; // no properties |
| 203 | document4.setType(QVersitDocument::VCard30Type); |
| 204 | |
| 205 | QVersitDocument document5 = document1; |
| 206 | document5.addSubDocument(subdocument: document4); |
| 207 | |
| 208 | QVersitDocument document6 = document1; |
| 209 | document6.setComponentType(QStringLiteral("VEVENT" )); |
| 210 | |
| 211 | QVersitDocument document7 = document1; |
| 212 | document7.addProperty(property: QVersitProperty()); |
| 213 | |
| 214 | QVERIFY(qHash(document1) == qHash(document2)); |
| 215 | QVERIFY(qHash(document1) != qHash(document3)); |
| 216 | QVERIFY(qHash(document1) != qHash(document4)); |
| 217 | QVERIFY(qHash(document1) != qHash(document5)); |
| 218 | QVERIFY(qHash(document1) != qHash(document6)); |
| 219 | QVERIFY(qHash(document1) != qHash(document7)); |
| 220 | } |
| 221 | |
| 222 | QTEST_MAIN(tst_QVersitDocument) |
| 223 | |
| 224 | |