| 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 <QtTest/QtTest> |
| 35 | #include <QtContacts/QContactDetail> |
| 36 | #include <QtContacts/qcontacts.h> |
| 37 | |
| 38 | #include <QSet> |
| 39 | |
| 40 | //TESTED_COMPONENT=src/contacts |
| 41 | |
| 42 | QTCONTACTS_USE_NAMESPACE |
| 43 | class tst_QContactDetail : public QObject |
| 44 | { |
| 45 | Q_OBJECT |
| 46 | |
| 47 | public: |
| 48 | tst_QContactDetail(); |
| 49 | virtual ~tst_QContactDetail(); |
| 50 | |
| 51 | public slots: |
| 52 | void init(); |
| 53 | void cleanup(); |
| 54 | |
| 55 | private slots: |
| 56 | void classHierarchy(); |
| 57 | void assignment(); |
| 58 | void templates(); |
| 59 | void contexts(); |
| 60 | void values(); |
| 61 | void hash(); |
| 62 | void datastream(); |
| 63 | void traits(); |
| 64 | void keys(); |
| 65 | void detailUris(); |
| 66 | }; |
| 67 | |
| 68 | tst_QContactDetail::tst_QContactDetail() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | tst_QContactDetail::~tst_QContactDetail() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void tst_QContactDetail::init() |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | void tst_QContactDetail::cleanup() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | void tst_QContactDetail::classHierarchy() |
| 85 | { |
| 86 | QContactDetail f1; |
| 87 | QContactDetail f2; |
| 88 | |
| 89 | QVERIFY(f1.isEmpty()); |
| 90 | QVERIFY(f2.isEmpty()); |
| 91 | |
| 92 | QContactPhoneNumber p1; |
| 93 | p1.setNumber("123456" ); |
| 94 | p1.setSubTypes(QList<int>() << QContactPhoneNumber::SubTypeFax << QContactPhoneNumber::SubTypeDtmfMenu); |
| 95 | QVERIFY(!p1.isEmpty()); |
| 96 | QVERIFY(p1.type() == QContactPhoneNumber::Type); |
| 97 | |
| 98 | QContactName m1; |
| 99 | m1.setFirstName("Bob" ); |
| 100 | QVERIFY(!m1.isEmpty()); |
| 101 | QVERIFY(m1.type() == QContactName::Type); |
| 102 | |
| 103 | QVERIFY(p1 != m1); |
| 104 | QVERIFY(f1 == f2); |
| 105 | |
| 106 | f1 = p1; // f1 is a phonenumber |
| 107 | QVERIFY(f1 == p1); |
| 108 | |
| 109 | f1 = f1; // assign to itself |
| 110 | QVERIFY(f1 == f1); |
| 111 | QVERIFY(f1 == p1); |
| 112 | QVERIFY(f1 != f2); |
| 113 | QVERIFY(p1 != f2); |
| 114 | |
| 115 | p1 = p1; // assign leaf class to itself |
| 116 | QVERIFY(p1 == p1); |
| 117 | QVERIFY(f1 == p1); |
| 118 | QVERIFY(p1 == f1); |
| 119 | |
| 120 | f2 = f1; // f2 = f1 = phonenumber |
| 121 | QVERIFY(f1 == f2); |
| 122 | QVERIFY(f2 == f1); |
| 123 | QVERIFY(f2 == p1); |
| 124 | QVERIFY(f1 == p1); |
| 125 | |
| 126 | f1 = m1; // f1 = name, f2 = phonenumber |
| 127 | QVERIFY(f1 == m1); |
| 128 | QVERIFY(f1 != f2); |
| 129 | QVERIFY(f2 == p1); |
| 130 | |
| 131 | QContactPhoneNumber p2(f2); // p2 = f2 = phonenumber |
| 132 | QVERIFY(p1 == p2); |
| 133 | QVERIFY(p1 == f2); |
| 134 | QCOMPARE(p2.number(), p1.number()); |
| 135 | QCOMPARE(p2.number(), QString("123456" )); |
| 136 | QCOMPARE(p2.subTypes(), QList<int>() << QContactPhoneNumber::SubTypeFax << QContactPhoneNumber::SubTypeDtmfMenu); |
| 137 | |
| 138 | p2 = p1; // phone number to phone number |
| 139 | QVERIFY(p1 == p2); |
| 140 | QVERIFY(p1 == f2); |
| 141 | QCOMPARE(p2.number(), p1.number()); |
| 142 | QCOMPARE(p2.number(), QString("123456" )); |
| 143 | |
| 144 | p2.setNumber("5678" ); // NOTE: implicitly shared, this has caused a detach so p1 != 2 |
| 145 | QVERIFY(p1 != p2); |
| 146 | QVERIFY(p1 == f2); |
| 147 | QVERIFY(p2 != f2); |
| 148 | QCOMPARE(p2.number(), QString("5678" )); |
| 149 | QCOMPARE(p1.number(), QString("123456" )); |
| 150 | |
| 151 | p2.setNumber(QString("123456" )); |
| 152 | QVERIFY(p1 == p2); |
| 153 | p2.setSubTypes(QList<int>() << QContactPhoneNumber::SubTypeLandline); |
| 154 | QVERIFY(p1 != p2); |
| 155 | p1.setSubTypes(QList<int>() << QContactPhoneNumber::SubTypeLandline); |
| 156 | QVERIFY(p1 == p2); |
| 157 | |
| 158 | /* Bad assignment */ |
| 159 | p2 = m1; // assign a name to a phone number |
| 160 | QVERIFY(p2 != m1); |
| 161 | QVERIFY(p2.type() == QContactPhoneNumber::Type); // should still be a phone number though |
| 162 | QVERIFY(p2.isEmpty()); |
| 163 | |
| 164 | /* copy ctor */ |
| 165 | QContactName m2(m1); |
| 166 | QVERIFY(m2 == m1); |
| 167 | |
| 168 | /* another bad assignment */ |
| 169 | m2 = p2; // phone number to a name |
| 170 | QVERIFY(m2 != m1); |
| 171 | QVERIFY(m2.type() == QContactName::Type); |
| 172 | QVERIFY(m2.isEmpty()); |
| 173 | |
| 174 | /* Check contexts are considered for equality */ |
| 175 | p2 = QContactPhoneNumber(); // new id / detach |
| 176 | p2.setNumber(p1.number()); |
| 177 | p2.setSubTypes(p1.subTypes()); |
| 178 | p2.setContexts(QContactDetail::ContextHome); |
| 179 | QVERIFY(p1 != p2); |
| 180 | p2.removeValue(field: QContactDetail::FieldContext); // note, context is a value. |
| 181 | QVERIFY(p1 == p2); // different ids but same values should be equal |
| 182 | |
| 183 | /* Copy ctor from valid type */ |
| 184 | QContactDetail f3(p2); |
| 185 | QVERIFY(f3 == p2); |
| 186 | QVERIFY(f3.type() == QContactPhoneNumber::Type); |
| 187 | |
| 188 | /* Copy ctor from invalid type */ |
| 189 | QContactPhoneNumber p3(m1); |
| 190 | QVERIFY(p3 != m1); |
| 191 | QVERIFY(p3.type() == QContactPhoneNumber::Type); |
| 192 | QVERIFY(p3.isEmpty()); |
| 193 | |
| 194 | /* Copy ctore from invalid type, through base type */ |
| 195 | f3 = m1; |
| 196 | QContactPhoneNumber p4(f3); |
| 197 | QVERIFY(p4 != f3); |
| 198 | QVERIFY(p4.type() == QContactPhoneNumber::Type); |
| 199 | QVERIFY(p4.isEmpty()); |
| 200 | |
| 201 | /* Try a reference */ |
| 202 | p1.setNumber("123456" ); |
| 203 | QContactDetail& ref = p1; |
| 204 | QVERIFY(p1.number() == "123456" ); |
| 205 | QVERIFY(p1.value(QContactPhoneNumber::FieldNumber) == "123456" ); |
| 206 | QVERIFY(ref.value(QContactPhoneNumber::FieldNumber) == "123456" ); |
| 207 | QVERIFY(p1 == ref); |
| 208 | QVERIFY(ref == p1); |
| 209 | |
| 210 | /* Try changing the original */ |
| 211 | p1.setNumber("56789" ); |
| 212 | QVERIFY(p1.number() == "56789" ); |
| 213 | QVERIFY(p1.value(QContactPhoneNumber::FieldNumber) == "56789" ); |
| 214 | QVERIFY(ref.value(QContactPhoneNumber::FieldNumber) == "56789" ); |
| 215 | QVERIFY(p1 == ref); |
| 216 | QVERIFY(ref == p1); |
| 217 | |
| 218 | /* Try changing the reference */ |
| 219 | ref.setValue(field: QContactPhoneNumber::FieldNumber, value: "654321" ); |
| 220 | QVERIFY(p1.number() == "654321" ); |
| 221 | QVERIFY(p1.value(QContactPhoneNumber::FieldNumber) == "654321" ); |
| 222 | QVERIFY(ref.value(QContactPhoneNumber::FieldNumber) == "654321" ); |
| 223 | QVERIFY(p1 == ref); |
| 224 | QVERIFY(ref == p1); |
| 225 | } |
| 226 | |
| 227 | void tst_QContactDetail::assignment() |
| 228 | { |
| 229 | QContactPhoneNumber p1, p2; |
| 230 | p1.setNumber("12345" ); |
| 231 | p2.setNumber("54321" ); |
| 232 | QVERIFY(p1 != p2); |
| 233 | |
| 234 | p1 = p2; |
| 235 | QVERIFY(p1 == p2); |
| 236 | |
| 237 | QContactManagerEngine::setDetailProvenance(detail: &p2, QStringLiteral("test-provenance" )); |
| 238 | QCOMPARE(p2.provenance(), QStringLiteral("test-provenance" )); |
| 239 | QCOMPARE(p1.provenance(), QString()); |
| 240 | QVERIFY(p1 == p2); // provenance shouldn't affect comparison |
| 241 | p1 = p2; // but should be copied on copy/assignment |
| 242 | QCOMPARE(p2.provenance(), p1.provenance()); |
| 243 | |
| 244 | QContactEmailAddress e1; |
| 245 | e1.setEmailAddress("test@nokia.com" ); |
| 246 | QVERIFY(e1 != p1); |
| 247 | e1 = p1; |
| 248 | QVERIFY(e1 != p1); // assignment across types shouldn't work |
| 249 | QVERIFY(e1.emailAddress() == QString()); // should reset the detail |
| 250 | QCOMPARE(e1, QContactEmailAddress()); |
| 251 | |
| 252 | QContactManagerEngine::setDetailAccessConstraints(detail: &p2, constraints: QContactDetail::Irremovable); |
| 253 | QVERIFY(p1 != p2); |
| 254 | } |
| 255 | |
| 256 | void tst_QContactDetail::templates() |
| 257 | { |
| 258 | QContact c; |
| 259 | QContactPhoneNumber p1, p2; |
| 260 | p1.setNumber("1234" ); |
| 261 | p2.setNumber("5678" ); |
| 262 | QVERIFY(c.saveDetail(&p1)); |
| 263 | QVERIFY(c.saveDetail(&p2)); |
| 264 | |
| 265 | QList<QContactDetail> l = c.details(type: QContactPhoneNumber::Type); |
| 266 | |
| 267 | QCOMPARE(l.count(), 2); |
| 268 | QCOMPARE(QContactPhoneNumber(l.at(0)), p1); |
| 269 | QCOMPARE(QContactPhoneNumber(l.at(1)), p2); |
| 270 | |
| 271 | QList<QContactPhoneNumber> l2 = c.details<QContactPhoneNumber>(); |
| 272 | QCOMPARE(l2.count(), 2); |
| 273 | QCOMPARE(l2.at(0), p1); |
| 274 | QCOMPARE(l2.at(1), p2); |
| 275 | } |
| 276 | |
| 277 | void tst_QContactDetail::contexts() |
| 278 | { |
| 279 | QContactDetail d; |
| 280 | QVERIFY(d.contexts().count() == 0); |
| 281 | |
| 282 | // test set contexts |
| 283 | d.setContexts(QContactDetail::ContextWork); |
| 284 | QVERIFY(d.contexts().count() == 1); |
| 285 | QVERIFY(d.contexts().contains(QContactDetail::ContextWork)); |
| 286 | QVERIFY(!d.contexts().contains(QContactDetail::ContextOther)); |
| 287 | QVERIFY(!d.contexts().contains(QContactDetail::ContextHome)); |
| 288 | |
| 289 | QList<int> contexts; |
| 290 | contexts.append(t: QContactDetail::ContextHome); |
| 291 | contexts.append(t: QContactDetail::ContextOther); |
| 292 | d.setContexts(contexts); |
| 293 | QVERIFY(d.contexts().count() == 2); |
| 294 | QVERIFY(!d.contexts().contains(QContactDetail::ContextWork)); |
| 295 | QVERIFY(d.contexts().contains(QContactDetail::ContextOther)); |
| 296 | QVERIFY(d.contexts().contains(QContactDetail::ContextHome)); |
| 297 | QCOMPARE(d.contexts(), contexts); |
| 298 | |
| 299 | // test that contexts are values. |
| 300 | QCOMPARE(d.value<QList<int> >(QContactDetail::FieldContext), d.contexts()); |
| 301 | } |
| 302 | |
| 303 | void tst_QContactDetail::values() |
| 304 | { |
| 305 | QContactDetail p; |
| 306 | QMap<int, QVariant> emptyValues; |
| 307 | QCOMPARE(p.values(), emptyValues); |
| 308 | |
| 309 | QDateTime dt = QDateTime::currentDateTime(); |
| 310 | QTime t = dt.time(); |
| 311 | t.setHMS(h: t.hour(), m: t.minute(), s: t.second(), ms: 0); // milliseconds don't round trip through ISODate |
| 312 | dt.setTime(t); |
| 313 | QDate d = dt.date(); |
| 314 | |
| 315 | QDateTime ddt(d); // DateTime version of a Date (QTime()) |
| 316 | |
| 317 | QVERIFY(p.setValue(QContactAddress::FieldStreet, "This is a string" )); |
| 318 | QVERIFY(p.setValue(QContactAddress::FieldLocality, d)); |
| 319 | QVERIFY(p.setValue(QContactAddress::FieldRegion, dt)); |
| 320 | QVERIFY(p.setValue(QContactAddress::FieldPostcode, (int)6)); |
| 321 | QVERIFY(p.setValue(QContactAddress::FieldSubTypes, d.toString(Qt::ISODate))); |
| 322 | QVERIFY(p.setValue(QContactAddress::FieldPostOfficeBox, dt.toString(Qt::ISODate))); |
| 323 | |
| 324 | // Presence test (const char * version) |
| 325 | QVERIFY(p.hasValue(QContactAddress::FieldStreet)); |
| 326 | QVERIFY(p.hasValue(QContactAddress::FieldLocality)); |
| 327 | QVERIFY(p.hasValue(QContactAddress::FieldRegion)); |
| 328 | QVERIFY(p.hasValue(QContactAddress::FieldPostcode)); |
| 329 | QVERIFY(p.hasValue(QContactAddress::FieldSubTypes)); |
| 330 | QVERIFY(p.hasValue(QContactAddress::FieldPostOfficeBox)); |
| 331 | QVERIFY(!p.hasValue(QContactAddress::FieldCountry)); |
| 332 | |
| 333 | // string accessors with const char* key |
| 334 | QCOMPARE(p.value(QContactAddress::FieldStreet).toString(), QString("This is a string" )); |
| 335 | QCOMPARE(p.value(QContactAddress::FieldLocality).toString(), d.toString(Qt::ISODate)); |
| 336 | QCOMPARE(p.value(QContactAddress::FieldRegion).toString(), dt.toString(Qt::ISODate)); |
| 337 | QCOMPARE(p.value(QContactAddress::FieldPostcode).toString(), QString("6" )); |
| 338 | QCOMPARE(p.value(QContactAddress::FieldSubTypes).toString(), d.toString(Qt::ISODate)); |
| 339 | QCOMPARE(p.value(QContactAddress::FieldPostOfficeBox).toString(), dt.toString(Qt::ISODate)); |
| 340 | |
| 341 | // Variant accessor with const char * key |
| 342 | QCOMPARE(p.value(QContactAddress::FieldStreet), QVariant(QString("This is a string" ))); |
| 343 | QCOMPARE(p.value(QContactAddress::FieldLocality), QVariant(d)); |
| 344 | QCOMPARE(p.value(QContactAddress::FieldRegion), QVariant(dt)); |
| 345 | QCOMPARE(p.value(QContactAddress::FieldPostcode), QVariant((int)6)); |
| 346 | QCOMPARE(p.value(QContactAddress::FieldSubTypes), QVariant(d.toString(Qt::ISODate))); |
| 347 | QCOMPARE(p.value(QContactAddress::FieldPostOfficeBox), QVariant(dt.toString(Qt::ISODate))); |
| 348 | |
| 349 | /* Typed accessors, string first, const char* key */ |
| 350 | QCOMPARE(p.value<QString>(QContactAddress::FieldStreet), QString("This is a string" )); |
| 351 | QCOMPARE(p.value<QString>(QContactAddress::FieldLocality), d.toString(Qt::ISODate)); |
| 352 | QCOMPARE(p.value<QString>(QContactAddress::FieldRegion), dt.toString(Qt::ISODate)); |
| 353 | QCOMPARE(p.value<QString>(QContactAddress::FieldPostcode), QString("6" )); |
| 354 | QCOMPARE(p.value<QString>(QContactAddress::FieldSubTypes), d.toString(Qt::ISODate)); |
| 355 | QCOMPARE(p.value<QString>(QContactAddress::FieldPostOfficeBox), dt.toString(Qt::ISODate)); |
| 356 | |
| 357 | /* Now individual original types */ |
| 358 | QCOMPARE(p.value<QDate>(QContactAddress::FieldLocality), d); |
| 359 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldRegion), dt); |
| 360 | QCOMPARE(p.value<int>(QContactAddress::FieldPostcode), 6); |
| 361 | |
| 362 | /* Now cross types that should fail */ |
| 363 | QDate id; |
| 364 | QDateTime idt; |
| 365 | QCOMPARE(p.value<QDate>(QContactAddress::FieldStreet), id); |
| 366 | QCOMPARE(p.value<QDate>(QContactAddress::FieldPostcode), id); |
| 367 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldStreet), idt); |
| 368 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldPostcode), idt); |
| 369 | QCOMPARE(p.value<int>(QContactAddress::FieldLocality), 0); |
| 370 | QCOMPARE(p.value<int>(QContactAddress::FieldRegion), 0); |
| 371 | QCOMPARE(p.value<int>(QContactAddress::FieldStreet), 0); |
| 372 | QCOMPARE(p.value<int>(QContactAddress::FieldSubTypes), 0); |
| 373 | QCOMPARE(p.value<int>(QContactAddress::FieldPostOfficeBox), 0); |
| 374 | |
| 375 | /* Cross types that should work.. */ |
| 376 | QCOMPARE(p.value<int>(QContactAddress::FieldPostcode), 6); |
| 377 | QCOMPARE(p.value<QDate>(QContactAddress::FieldSubTypes), d); |
| 378 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldPostOfficeBox), dt); |
| 379 | QCOMPARE(p.value<QDate>(QContactAddress::FieldRegion), d); |
| 380 | QCOMPARE(p.value<QDate>(QContactAddress::FieldPostOfficeBox), d); |
| 381 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldLocality), ddt); |
| 382 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldSubTypes), ddt); |
| 383 | |
| 384 | /* Now set everything again */ |
| 385 | QMap<int, QVariant> values = p.values(); |
| 386 | QList<int> keys = values.keys(); |
| 387 | foreach (int key, keys) |
| 388 | QVERIFY(p.setValue(key, QVariant())); |
| 389 | |
| 390 | QCOMPARE(p.values(), emptyValues); |
| 391 | QVERIFY(p.values().count() == 0); |
| 392 | QVERIFY(!p.hasValue(QContactAddress::FieldStreet)); |
| 393 | QVERIFY(!p.hasValue(QContactAddress::FieldLocality)); |
| 394 | QVERIFY(!p.hasValue(QContactAddress::FieldRegion)); |
| 395 | QVERIFY(!p.hasValue(QContactAddress::FieldPostcode)); |
| 396 | QVERIFY(!p.hasValue(QContactAddress::FieldSubTypes)); |
| 397 | QVERIFY(!p.hasValue(QContactAddress::FieldPostOfficeBox)); |
| 398 | |
| 399 | QVERIFY(p.value(QContactAddress::FieldStreet).toString() == QString()); |
| 400 | QVERIFY(p.value(QContactAddress::FieldStreet) == QVariant()); |
| 401 | |
| 402 | values.insert(akey: QContactAddress::FieldStreet, avalue: "This is a string" ); |
| 403 | values.insert(akey: QContactAddress::FieldLocality, avalue: d); |
| 404 | values.insert(akey: QContactAddress::FieldRegion, avalue: dt); |
| 405 | values.insert(akey: QContactAddress::FieldPostcode, avalue: (int)6); |
| 406 | |
| 407 | values.insert(akey: QContactAddress::FieldSubTypes, avalue: d.toString(format: Qt::ISODate)); |
| 408 | values.insert(akey: QContactAddress::FieldPostOfficeBox, avalue: dt.toString(format: Qt::ISODate)); |
| 409 | values.insert(akey: QContactAddress::FieldStreet, avalue: QString("This is a string" )); |
| 410 | |
| 411 | /* Set values */ |
| 412 | keys = values.keys(); |
| 413 | foreach (int key, keys) |
| 414 | QVERIFY(p.setValue(key, values.value(key))); |
| 415 | |
| 416 | /* Now repeat the tests with our bulk set map */ |
| 417 | QVERIFY(p.hasValue(QContactAddress::FieldStreet)); |
| 418 | QVERIFY(p.hasValue(QContactAddress::FieldLocality)); |
| 419 | QVERIFY(p.hasValue(QContactAddress::FieldRegion)); |
| 420 | QVERIFY(p.hasValue(QContactAddress::FieldPostcode)); |
| 421 | QVERIFY(p.hasValue(QContactAddress::FieldSubTypes)); |
| 422 | QVERIFY(p.hasValue(QContactAddress::FieldPostOfficeBox)); |
| 423 | |
| 424 | /* String accessors */ |
| 425 | QCOMPARE(p.value(QContactAddress::FieldStreet).toString(), QString("This is a string" )); |
| 426 | QCOMPARE(p.value(QContactAddress::FieldLocality).toString(), d.toString(Qt::ISODate)); |
| 427 | QCOMPARE(p.value(QContactAddress::FieldRegion).toString(), dt.toString(Qt::ISODate)); |
| 428 | QCOMPARE(p.value(QContactAddress::FieldPostcode).toString(), QString("6" )); |
| 429 | QCOMPARE(p.value(QContactAddress::FieldSubTypes).toString(), d.toString(Qt::ISODate)); |
| 430 | QCOMPARE(p.value(QContactAddress::FieldPostOfficeBox).toString(), dt.toString(Qt::ISODate)); |
| 431 | |
| 432 | /* Typed accessors, string first */ |
| 433 | QCOMPARE(p.value<QString>(QContactAddress::FieldStreet), QString("This is a string" )); |
| 434 | QCOMPARE(p.value<QString>(QContactAddress::FieldLocality), d.toString(Qt::ISODate)); |
| 435 | QCOMPARE(p.value<QString>(QContactAddress::FieldRegion), dt.toString(Qt::ISODate)); |
| 436 | QCOMPARE(p.value<QString>(QContactAddress::FieldPostcode), QString("6" )); |
| 437 | QCOMPARE(p.value<QString>(QContactAddress::FieldSubTypes), d.toString(Qt::ISODate)); |
| 438 | QCOMPARE(p.value<QString>(QContactAddress::FieldPostOfficeBox), dt.toString(Qt::ISODate)); |
| 439 | |
| 440 | /* Now individual original types */ |
| 441 | QCOMPARE(p.value<QDate>(QContactAddress::FieldLocality), d); |
| 442 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldRegion), dt); |
| 443 | QCOMPARE(p.value<int>(QContactAddress::FieldPostcode), 6); |
| 444 | |
| 445 | /* Now cross types that should fail */ |
| 446 | QCOMPARE(p.value<QDate>(QContactAddress::FieldStreet), id); |
| 447 | QCOMPARE(p.value<QDate>(QContactAddress::FieldPostcode), id); |
| 448 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldStreet), idt); |
| 449 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldPostcode), idt); |
| 450 | QCOMPARE(p.value<int>(QContactAddress::FieldLocality), 0); |
| 451 | QCOMPARE(p.value<int>(QContactAddress::FieldRegion), 0); |
| 452 | QCOMPARE(p.value<int>(QContactAddress::FieldStreet), 0); |
| 453 | QCOMPARE(p.value<int>(QContactAddress::FieldSubTypes), 0); |
| 454 | QCOMPARE(p.value<int>(QContactAddress::FieldPostOfficeBox), 0); |
| 455 | |
| 456 | /* Cross types that should work.. */ |
| 457 | QCOMPARE(p.value<QDate>(QContactAddress::FieldSubTypes), d); |
| 458 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldPostOfficeBox), dt); |
| 459 | QCOMPARE(p.value<QDate>(QContactAddress::FieldRegion), d); |
| 460 | QCOMPARE(p.value<QDate>(QContactAddress::FieldPostOfficeBox), d); |
| 461 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldLocality), ddt); |
| 462 | QCOMPARE(p.value<QDateTime>(QContactAddress::FieldSubTypes), ddt); |
| 463 | |
| 464 | /* Reset again */ |
| 465 | values = p.values(); |
| 466 | keys = values.keys(); |
| 467 | foreach (int key, keys) |
| 468 | QVERIFY(p.setValue(key, QVariant())); |
| 469 | QCOMPARE(p.values(), emptyValues); |
| 470 | |
| 471 | /* Check adding a null value removes the field */ |
| 472 | p.setValue(field: QContactAddress::FieldStreet, value: "stringvalue" ); |
| 473 | QVERIFY(p.values().contains(QContactAddress::FieldStreet)); |
| 474 | QVERIFY(p.value(QContactAddress::FieldStreet) == QString("stringvalue" )); |
| 475 | p.setValue(field: QContactAddress::FieldStreet, value: QVariant()); |
| 476 | QVERIFY(!p.values().contains(QContactAddress::FieldStreet)); |
| 477 | |
| 478 | /* Check adding a field whose value is an empty string */ |
| 479 | p.setValue(field: QContactAddress::FieldStreet, value: "" ); |
| 480 | QVERIFY(p.values().contains(QContactAddress::FieldStreet)); |
| 481 | QVERIFY(p.value(QContactAddress::FieldStreet) == QString("" )); |
| 482 | |
| 483 | /* Check accessing a missing value */ |
| 484 | QCOMPARE(p.value(QContactAddress::FieldStreet).toString(), QString()); |
| 485 | QVERIFY(p.setValue(QContactAddress::FieldStreet, "changed my mind" )); |
| 486 | QCOMPARE(p.value(QContactAddress::FieldStreet).toString(), QString("changed my mind" )); |
| 487 | |
| 488 | /* Check removing a missing value */ |
| 489 | QVERIFY(!p.removeValue(QContactAddress::FieldCountry)); |
| 490 | |
| 491 | p.setValue(field: QContactAddress::FieldCountry, value: "555" ); |
| 492 | p.setValue(field: QContactAddress::FieldPostOfficeBox, value: "1234" ); |
| 493 | |
| 494 | /* Check removing a real value */ |
| 495 | QVERIFY(p.removeValue(QContactAddress::FieldStreet)); |
| 496 | QVERIFY(p.removeValue(QContactAddress::FieldCountry)); |
| 497 | QVERIFY(p.removeValue(QContactAddress::FieldPostOfficeBox)); |
| 498 | } |
| 499 | |
| 500 | void tst_QContactDetail::hash() |
| 501 | { |
| 502 | QContactExtendedDetail detail1; |
| 503 | detail1.setName("key" ); |
| 504 | detail1.setData(QVariant("value" )); |
| 505 | QContactExtendedDetail detail2; |
| 506 | detail2.setName("key" ); |
| 507 | detail2.setData(QVariant("value" )); |
| 508 | QContactExtendedDetail detail3; |
| 509 | detail3.setName("key" ); |
| 510 | detail3.setData(QVariant("different value" )); |
| 511 | QVERIFY(qHash(detail1) == qHash(detail2)); |
| 512 | QVERIFY(qHash(detail1) != qHash(detail3)); |
| 513 | QSet<QContactDetail> set; |
| 514 | set.insert(value: detail1); |
| 515 | set.insert(value: detail2); |
| 516 | set.insert(value: detail3); |
| 517 | QCOMPARE(set.size(), 2); |
| 518 | } |
| 519 | |
| 520 | void tst_QContactDetail::datastream() |
| 521 | { |
| 522 | QByteArray buffer; |
| 523 | QDataStream stream1(&buffer, QIODevice::WriteOnly); |
| 524 | QContactExtendedDetail detailIn; |
| 525 | detailIn.setName("key1" ); |
| 526 | detailIn.setData(QVariant("value1" )); |
| 527 | detailIn.setName("key2" ); |
| 528 | detailIn.setData(QVariant("value2" )); |
| 529 | stream1 << detailIn; |
| 530 | |
| 531 | QVERIFY(buffer.size() > 0); |
| 532 | |
| 533 | QDataStream stream2(buffer); |
| 534 | QContactExtendedDetail detailOut; |
| 535 | stream2 >> detailOut; |
| 536 | QCOMPARE(detailOut, detailIn); |
| 537 | } |
| 538 | |
| 539 | void tst_QContactDetail::traits() |
| 540 | { |
| 541 | QCOMPARE(sizeof(QContactDetail), sizeof(void *)); |
| 542 | QVERIFY(QTypeInfo<QContactDetail>::isComplex); |
| 543 | QVERIFY(!QTypeInfo<QContactDetail>::isStatic); |
| 544 | QVERIFY(!QTypeInfo<QContactDetail>::isLarge); |
| 545 | QVERIFY(!QTypeInfo<QContactDetail>::isPointer); |
| 546 | QVERIFY(!QTypeInfo<QContactDetail>::isDummy); |
| 547 | } |
| 548 | |
| 549 | void tst_QContactDetail::keys() |
| 550 | { |
| 551 | QContactDetail d; |
| 552 | QContactDetail d2; |
| 553 | QVERIFY(d.key() != d2.key()); |
| 554 | |
| 555 | d = d2; |
| 556 | QVERIFY(d.key() == d2.key()); |
| 557 | d.resetKey(); |
| 558 | QVERIFY(d.key() != d2.key()); |
| 559 | } |
| 560 | |
| 561 | void tst_QContactDetail::detailUris() |
| 562 | { |
| 563 | QContactDetail d; |
| 564 | QVERIFY(d.detailUri().isEmpty()); |
| 565 | |
| 566 | d.setDetailUri("I'm a detail uri" ); |
| 567 | QVERIFY(d.detailUri() == "I'm a detail uri" ); |
| 568 | |
| 569 | d.setDetailUri(QString()); |
| 570 | QVERIFY(d.detailUri().isEmpty()); |
| 571 | |
| 572 | QVERIFY(d.linkedDetailUris().isEmpty()); |
| 573 | |
| 574 | d.setLinkedDetailUris("5555" ); |
| 575 | QVERIFY(d.linkedDetailUris().count() == 1); |
| 576 | QVERIFY(d.linkedDetailUris().count("5555" ) == 1); |
| 577 | |
| 578 | QStringList sl; |
| 579 | sl << "6666" << "7777" ; |
| 580 | d.setLinkedDetailUris(sl); |
| 581 | QVERIFY(d.linkedDetailUris().count() == 2); |
| 582 | QVERIFY(d.linkedDetailUris() == sl); |
| 583 | } |
| 584 | |
| 585 | QTEST_MAIN(tst_QContactDetail) |
| 586 | #include "tst_qcontactdetail.moc" |
| 587 | |