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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
|
/*
* Copyright (C) 2020 Open Mobile Platform LLC.
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include <QtGlobal>
#include <QtTest/QtTest>
#include "../../util.h"
#include "testsyncadaptor.h"
#include "qtcontacts-extensions.h"
#include "qcontactcollectionchangesfetchrequest.h"
#include "qcontactcollectionchangesfetchrequest_impl.h"
#include "qcontactchangesfetchrequest.h"
#include "qcontactchangesfetchrequest_impl.h"
#include "qcontactchangessaverequest.h"
#include "qcontactchangessaverequest_impl.h"
#include "qcontactclearchangeflagsrequest.h"
#include "qcontactclearchangeflagsrequest_impl.h"
class tst_synctransactions : public QObject
{
Q_OBJECT
public:
tst_synctransactions();
virtual ~tst_synctransactions();
public slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
public slots:
void addColAccumulationSlot(const QList<QContactCollectionId> &ids);
void addAccumulationSlot(const QList<QContactId> &ids);
private slots:
void singleCollection_noContacts();
void singleCollection_addedContacts();
void singleCollection_multipleCycles();
void singleCollection_unhandledChanges();
void multipleCollections();
void syncRequests();
void twcsa_nodelta();
void twcsa_delta();
void twcsa_oneway();
private:
void waitForSignalPropagation();
QContactManager *m_cm;
QSet<QContactCollectionId> m_createdColIds;
QSet<QContactId> m_createdIds;
QByteArray aggregateAddressbookId()
{
return QtContactsSqliteExtensions::aggregateCollectionId(m_cm->managerUri()).localId();
}
QByteArray localAddressbookId()
{
return QtContactsSqliteExtensions::localCollectionId(m_cm->managerUri()).localId();
}
};
tst_synctransactions::tst_synctransactions()
: m_cm(0)
{
QMap<QString, QString> parameters;
parameters.insert(QString::fromLatin1("autoTest"), QString::fromLatin1("true"));
parameters.insert(QString::fromLatin1("mergePresenceChanges"), QString::fromLatin1("true"));
m_cm = new QContactManager(QString::fromLatin1("org.nemomobile.contacts.sqlite"), parameters);
QTest::qWait(250); // creating self contact etc will cause some signals to be emitted. ignore them.
QObject::connect(m_cm, &QContactManager::collectionsAdded, this, &tst_synctransactions::addColAccumulationSlot);
QObject::connect(m_cm, &QContactManager::contactsAdded, this, &tst_synctransactions::addAccumulationSlot);
}
tst_synctransactions::~tst_synctransactions()
{
}
void tst_synctransactions::initTestCase()
{
registerIdType();
/* Make sure the DB is empty */
QContactCollectionFilter allCollections;
m_cm->removeContacts(m_cm->contactIds(allCollections));
waitForSignalPropagation();
}
void tst_synctransactions::init()
{
m_createdColIds.clear();
m_createdIds.clear();
}
void tst_synctransactions::cleanupTestCase()
{
}
void tst_synctransactions::cleanup()
{
QContactManager::Error err = QContactManager::NoError;
QtContactsSqliteExtensions::ContactManagerEngine *cme = QtContactsSqliteExtensions::contactManagerEngine(*m_cm);
waitForSignalPropagation();
if (!m_createdIds.isEmpty()) {
// purge them one at a time, to avoid "contacts from different collections in single batch" errors.
for (const QContactId &cid : m_createdIds) {
QContact doomed = m_cm->contact(cid);
if (!doomed.id().isNull() && doomed.collectionId().localId() != aggregateAddressbookId()) {
if (!m_cm->removeContact(cid)) {
qWarning() << "Failed to cleanup:" << QString::fromLatin1(cid.localId());
}
cme->clearChangeFlags(QList<QContactId>() << cid, &err);
}
}
m_createdIds.clear();
}
if (!m_createdColIds.isEmpty()) {
for (const QContactCollectionId &colId : m_createdColIds.toList()) {
m_cm->removeCollection(colId);
cme->clearChangeFlags(colId, &err);
}
m_createdColIds.clear();
}
cme->clearChangeFlags(QContactCollectionId(m_cm->managerUri(), localAddressbookId()), &err);
waitForSignalPropagation();
}
void tst_synctransactions::waitForSignalPropagation()
{
// Signals are routed via DBUS, so we need to wait for them to arrive
QTest::qWait(50);
}
void tst_synctransactions::addColAccumulationSlot(const QList<QContactCollectionId> &ids)
{
foreach (const QContactCollectionId &id, ids) {
m_createdColIds.insert(id);
}
}
void tst_synctransactions::addAccumulationSlot(const QList<QContactId> &ids)
{
foreach (const QContactId &id, ids) {
m_createdIds.insert(id);
}
}
void tst_synctransactions::singleCollection_noContacts()
{
QtContactsSqliteExtensions::ContactManagerEngine *cme = QtContactsSqliteExtensions::contactManagerEngine(*m_cm);
QContactManager::Error err = QContactManager::NoError;
QContactCollectionId remoteAddressbookId;
// ensure that initially, no changes are detected.
{
QList<QContactCollection> addedCollections;
QList<QContactCollection> modifiedCollections;
QList<QContactCollection> deletedCollections;
QList<QContactCollection> unmodifiedCollections;
QVERIFY(cme->fetchCollectionChanges(
0, QStringLiteral("tst_synctransactions"),
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(addedCollections.count(), 0);
QCOMPARE(modifiedCollections.count(), 0);
QCOMPARE(deletedCollections.count(), 0);
QCOMPARE(unmodifiedCollections.count(), 0);
}
// simulate a sync cycle which results in an empty remote addressbook being added.
{
QHash<QContactCollection*, QList<QContact> *> additions;
QHash<QContactCollection*, QList<QContact> *> modifications;
QContactCollection remoteAddressbook;
remoteAddressbook.setMetaData(QContactCollection::KeyName, QStringLiteral("test"));
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME, "tst_synctransactions");
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID, 5);
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_REMOTEPATH, "/addressbooks/test");
QList<QContact> addedCollectionContacts;
additions.insert(&remoteAddressbook, &addedCollectionContacts);
const QtContactsSqliteExtensions::ContactManagerEngine::ConflictResolutionPolicy policy(
QtContactsSqliteExtensions::ContactManagerEngine::PreserveLocalChanges);
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>(),
policy, true, &err));
QVERIFY(!remoteAddressbook.id().isNull()); // id should have been set during save operation.
remoteAddressbookId = remoteAddressbook.id();
}
// ensure that no changes are detected, but the collection is reported as unmodified.
{
QList<QContactCollection> addedCollections;
QList<QContactCollection> modifiedCollections;
QList<QContactCollection> deletedCollections;
QList<QContactCollection> unmodifiedCollections;
QVERIFY(cme->fetchCollectionChanges(
5, QStringLiteral("tst_synctransactions"),
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(addedCollections.count(), 0);
QCOMPARE(modifiedCollections.count(), 0);
QCOMPARE(deletedCollections.count(), 0);
QCOMPARE(unmodifiedCollections.count(), 1);
QCOMPARE(unmodifiedCollections.first().id(), remoteAddressbookId);
}
// and ensure that no contact changes are reported for that collection
{
QList<QContact> addedContacts;
QList<QContact> modifiedContacts;
QList<QContact> deletedContacts;
QList<QContact> unmodifiedContacts;
QVERIFY(cme->fetchContactChanges(
remoteAddressbookId,
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(addedContacts.count(), 0);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 0);
QCOMPARE(unmodifiedContacts.count(), 0);
}
// clean up.
QVERIFY(m_cm->removeCollection(remoteAddressbookId));
QVERIFY(cme->clearChangeFlags(remoteAddressbookId, &err));
}
void tst_synctransactions::singleCollection_addedContacts()
{
QtContactsSqliteExtensions::ContactManagerEngine *cme = QtContactsSqliteExtensions::contactManagerEngine(*m_cm);
QContactManager::Error err = QContactManager::NoError;
QContactCollectionId remoteAddressbookId;
QContactId remoteContactId;
// ensure that initially, no changes are detected.
{
QList<QContactCollection> addedCollections;
QList<QContactCollection> modifiedCollections;
QList<QContactCollection> deletedCollections;
QList<QContactCollection> unmodifiedCollections;
QVERIFY(cme->fetchCollectionChanges(
0, QStringLiteral("tst_synctransactions"),
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(addedCollections.count(), 0);
QCOMPARE(modifiedCollections.count(), 0);
QCOMPARE(deletedCollections.count(), 0);
QCOMPARE(unmodifiedCollections.count(), 0);
}
// simulate a sync cycle which results in a non-empty remote addressbook being added.
{
QHash<QContactCollection*, QList<QContact> *> additions;
QHash<QContactCollection*, QList<QContact> *> modifications;
QContactCollection remoteAddressbook;
remoteAddressbook.setMetaData(QContactCollection::KeyName, QStringLiteral("test"));
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME, "tst_synctransactions");
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID, 5);
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_REMOTEPATH, "/addressbooks/test");
QContact syncAlice;
QContactName san;
san.setFirstName("Alice");
san.setMiddleName("In");
san.setLastName("Wonderland");
syncAlice.saveDetail(&san);
QContactPhoneNumber saph;
saph.setNumber("123454321");
syncAlice.saveDetail(&saph);
QContactEmailAddress saem;
saem.setEmailAddress("alice@wonderland.tld");
syncAlice.saveDetail(&saem);
QContactStatusFlags saf;
saf.setFlag(QContactStatusFlags::IsAdded, true);
syncAlice.saveDetail(&saf);
QList<QContact> addedCollectionContacts;
addedCollectionContacts.append(syncAlice);
additions.insert(&remoteAddressbook, &addedCollectionContacts);
const QtContactsSqliteExtensions::ContactManagerEngine::ConflictResolutionPolicy policy(
QtContactsSqliteExtensions::ContactManagerEngine::PreserveLocalChanges);
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>(),
policy, true, &err));
QVERIFY(!remoteAddressbook.id().isNull()); // id should have been set during save operation.
QVERIFY(!addedCollectionContacts.first().id().isNull()); // id should have been set during save operation.
remoteAddressbookId = remoteAddressbook.id();
remoteContactId = addedCollectionContacts.first().id();
}
// ensure that no changes are detected, but the collection is reported as unmodified.
{
QList<QContactCollection> addedCollections;
QList<QContactCollection> modifiedCollections;
QList<QContactCollection> deletedCollections;
QList<QContactCollection> unmodifiedCollections;
QVERIFY(cme->fetchCollectionChanges(
5, QStringLiteral("tst_synctransactions"),
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(addedCollections.count(), 0);
QCOMPARE(modifiedCollections.count(), 0);
QCOMPARE(deletedCollections.count(), 0);
QCOMPARE(unmodifiedCollections.count(), 1);
QCOMPARE(unmodifiedCollections.first().id(), remoteAddressbookId);
}
// and ensure that no contact changes are reported for that collection,
// but the remote contact is reported as unmodified.
{
QList<QContact> addedContacts;
QList<QContact> modifiedContacts;
QList<QContact> deletedContacts;
QList<QContact> unmodifiedContacts;
QVERIFY(cme->fetchContactChanges(
remoteAddressbookId,
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(addedContacts.count(), 0);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 0);
QCOMPARE(unmodifiedContacts.count(), 1);
QCOMPARE(unmodifiedContacts.first().id(), remoteContactId);
}
// clean up.
QVERIFY(m_cm->removeCollection(remoteAddressbookId));
QVERIFY(cme->clearChangeFlags(remoteAddressbookId, &err));
}
void tst_synctransactions::singleCollection_multipleCycles()
{
QtContactsSqliteExtensions::ContactManagerEngine *cme = QtContactsSqliteExtensions::contactManagerEngine(*m_cm);
QContactManager::Error err = QContactManager::NoError;
QHash<QContactCollection*, QList<QContact> *> additions;
QHash<QContactCollection*, QList<QContact> *> modifications;
QContactCollection remoteAddressbook;
remoteAddressbook.setMetaData(QContactCollection::KeyName, QStringLiteral("test"));
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME, "tst_synctransactions");
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID, 5);
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_REMOTEPATH, "/addressbooks/test");
remoteAddressbook.setExtendedMetaData(QStringLiteral("SyncToken"), "synctoken-one");
QContact syncAlice;
QContactName an;
an.setFirstName("Alice");
an.setMiddleName("In");
an.setLastName("Wonderland");
syncAlice.saveDetail(&an);
QContactPhoneNumber aph;
aph.setNumber("123454321");
syncAlice.saveDetail(&aph);
QContactEmailAddress aem;
aem.setEmailAddress("alice@wonderland.tld");
syncAlice.saveDetail(&aem);
QContactStatusFlags af;
af.setFlag(QContactStatusFlags::IsAdded, true);
syncAlice.saveDetail(&af);
QContact syncBob;
QContactName bn;
bn.setFirstName("Bob");
bn.setMiddleName("The");
bn.setLastName("Constructor");
syncBob.saveDetail(&bn);
QContactPhoneNumber bph;
bph.setNumber("543212345");
syncBob.saveDetail(&bph);
QContactEmailAddress bem;
bem.setEmailAddress("bob@construction.tld");
syncBob.saveDetail(&bem);
QContactStatusFlags bf;
bf.setFlag(QContactStatusFlags::IsAdded, true);
syncBob.saveDetail(&bf);
QList<QContact> addedCollectionContacts;
addedCollectionContacts.append(syncAlice);
addedCollectionContacts.append(syncBob);
additions.insert(&remoteAddressbook, &addedCollectionContacts);
const QtContactsSqliteExtensions::ContactManagerEngine::ConflictResolutionPolicy policy(
QtContactsSqliteExtensions::ContactManagerEngine::PreserveLocalChanges);
// initial sync cycle: remote has a non-empty addressbook.
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>(),
policy, true, &err));
QCOMPARE(err, QContactManager::NoError);
QVERIFY(!remoteAddressbook.id().isNull()); // id should have been set during save operation.
QVERIFY(!addedCollectionContacts.at(0).id().isNull()); // id should have been set during save operation.
QVERIFY(!addedCollectionContacts.at(1).id().isNull()); // id should have been set during save operation.
QCOMPARE(addedCollectionContacts.at(0).collectionId(), remoteAddressbook.id());
QCOMPARE(addedCollectionContacts.at(1).collectionId(), remoteAddressbook.id());
syncAlice = addedCollectionContacts.at(0);
syncBob = addedCollectionContacts.at(1);
// wait a while. not necessary but for timestamp debugging purposes...
QTest::qWait(250);
// now perform some local modifications:
// add a contact
QContact syncCharlie;
syncCharlie.setCollectionId(remoteAddressbook.id());
QContactName cn;
cn.setFirstName("Charlie");
cn.setMiddleName("The");
cn.setLastName("Horse");
syncCharlie.saveDetail(&cn);
QContactPhoneNumber cph;
cph.setNumber("987656789");
syncCharlie.saveDetail(&cph);
QContactEmailAddress cem;
cem.setEmailAddress("charlie@horse.tld");
syncCharlie.saveDetail(&cem);
QVERIFY(m_cm->saveContact(&syncCharlie));
// delete a contact
QVERIFY(m_cm->removeContact(syncBob.id()));
// modify a contact
syncAlice = m_cm->contact(syncAlice.id());
aph = syncAlice.detail<QContactPhoneNumber>();
aph.setNumber("111111111");
QVERIFY(syncAlice.saveDetail(&aph));
QVERIFY(m_cm->saveContact(&syncAlice));
// now perform a second sync cycle.
// first, retrieve local changes we need to push to remote server.
QList<QContactCollection> addedCollections;
QList<QContactCollection> modifiedCollections;
QList<QContactCollection> deletedCollections;
QList<QContactCollection> unmodifiedCollections;
QVERIFY(cme->fetchCollectionChanges(
5,
"tst_synctransactions",
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedCollections.count(), 0);
QCOMPARE(modifiedCollections.count(), 0);
QCOMPARE(deletedCollections.count(), 0);
QCOMPARE(unmodifiedCollections.count(), 1);
QCOMPARE(unmodifiedCollections.first().extendedMetaData().value(
QStringLiteral("SyncToken")).toString(),
QStringLiteral("synctoken-one"));
QList<QContact> addedContacts;
QList<QContact> modifiedContacts;
QList<QContact> deletedContacts;
QList<QContact> unmodifiedContacts;
QVERIFY(cme->fetchContactChanges(
remoteAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 1);
QCOMPARE(modifiedContacts.count(), 1);
QCOMPARE(deletedContacts.count(), 1);
QCOMPARE(unmodifiedContacts.count(), 0);
QCOMPARE(addedContacts.first().id(), syncCharlie.id());
QCOMPARE(deletedContacts.first().id(), syncBob.id());
QCOMPARE(modifiedContacts.first().id(), syncAlice.id());
// at this point, Bob should have been marked as deleted,
// and should not be accessible using the normal access API.
QContact deletedBob = m_cm->contact(syncBob.id());
QCOMPARE(m_cm->error(), QContactManager::DoesNotExistError);
QVERIFY(deletedBob.id().isNull());
// but we should still be able to access deleted Bob via specific filter.
QContactCollectionFilter allCollections;
QList<QContactId> deletedContactIds = m_cm->contactIds(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContactIds.size(), 1);
QVERIFY(deletedContactIds.contains(syncBob.id()));
deletedContacts.clear();
deletedContacts = m_cm->contacts(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContacts.size(), 1);
QCOMPARE(deletedContacts.first().detail<QContactPhoneNumber>().number(), QStringLiteral("543212345")); // Bob's phone number.
// now fetch changes from the remote server, and calculate the delta.
// in this case, we simulate that the user added a hobby on the remote server
// for contact Alice, and deleted contact Charlie, and these changes need
// to be stored to the local database.
syncAlice = modifiedContacts.first();
QContactHobby ah;
ah.setHobby("Tennis");
syncAlice.saveDetail(&ah);
af = syncAlice.detail<QContactStatusFlags>();
af.setFlag(QContactStatusFlags::IsModified, true);
syncAlice.saveDetail(&af, QContact::IgnoreAccessConstraints);
syncCharlie = addedContacts.first();
QContactStatusFlags cf = syncCharlie.detail<QContactStatusFlags>();
cf.setFlag(QContactStatusFlags::IsDeleted, true);
syncCharlie.saveDetail(&cf, QContact::IgnoreAccessConstraints);
// specify an updated ctag for the addressbook.
remoteAddressbook.setExtendedMetaData(QStringLiteral("SyncToken"), "synctoken-two");
// write the remote changes to the local database.
additions.clear();
modifications.clear();
QList<QContact> modifiedCollectionContacts;
modifiedCollectionContacts.append(syncAlice); // modification
modifiedCollectionContacts.append(syncCharlie); // deletion
modifications.insert(&remoteAddressbook, &modifiedCollectionContacts);
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>(),
policy, true, &err));
// Alice should have been updated with the new hobby.
// The other details should not have been changed.
syncAlice = m_cm->contact(syncAlice.id());
QCOMPARE(syncAlice.detail<QContactHobby>().hobby(), QStringLiteral("Tennis"));
QCOMPARE(syncAlice.detail<QContactPhoneNumber>().number(), QStringLiteral("111111111"));
// we should no longer be able to access the deleted contacts,
// as the clearChangeFlags parameter was "true" in the above method call.
deletedContactIds.clear();
deletedContactIds = m_cm->contactIds(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContactIds.size(), 0);
// now perform another sync cycle.
// there should be no local changes reported since the last clearChangeFlags()
// (in this case, since the last storeChanges() call).
addedCollections.clear();
modifiedCollections.clear();
deletedCollections.clear();
unmodifiedCollections.clear();
QVERIFY(cme->fetchCollectionChanges(
5,
"tst_synctransactions",
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedCollections.count(), 0);
QCOMPARE(modifiedCollections.count(), 0);
QCOMPARE(deletedCollections.count(), 0);
QCOMPARE(unmodifiedCollections.count(), 1);
QCOMPARE(unmodifiedCollections.first().extendedMetaData().value(
QStringLiteral("SyncToken")).toString(),
QStringLiteral("synctoken-two"));
addedContacts.clear();
modifiedContacts.clear();
deletedContacts.clear();
unmodifiedContacts.clear();
QVERIFY(cme->fetchContactChanges(
remoteAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(addedContacts.size(), 0);
QCOMPARE(modifiedContacts.size(), 0);
QCOMPARE(deletedContacts.size(), 0);
QCOMPARE(unmodifiedContacts.size(), 1);
QCOMPARE(unmodifiedContacts.first().id(), syncAlice.id());
syncAlice = unmodifiedContacts.first();
// report remote deletion of the entire collection and store locally.
additions.clear();
modifications.clear();
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>() << remoteAddressbook.id(),
policy, true, &err));
// attempting to fetch the collection should fail
QContactCollection deletedCollection = m_cm->collection(remoteAddressbook.id());
QCOMPARE(m_cm->error(), QContactManager::DoesNotExistError);
QVERIFY(deletedCollection.id().isNull());
// attempting to fetch deleted contacts should return no results.
// the deletion of the contacts as a result of the deletion of the collection
// will in this case be applied immediately (and purged) due to the
// clearChangeFlags=true parameter to the above storeChanges() call.
deletedContactIds.clear();
deletedContactIds = m_cm->contactIds(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContactIds.size(), 0);
}
void tst_synctransactions::singleCollection_unhandledChanges()
{
QtContactsSqliteExtensions::ContactManagerEngine *cme = QtContactsSqliteExtensions::contactManagerEngine(*m_cm);
QContactManager::Error err = QContactManager::NoError;
QHash<QContactCollection*, QList<QContact> *> additions;
QHash<QContactCollection*, QList<QContact> *> modifications;
QContactCollection remoteAddressbook;
remoteAddressbook.setMetaData(QContactCollection::KeyName, QStringLiteral("test"));
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME, "tst_synctransactions");
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID, 5);
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_REMOTEPATH, "/addressbooks/test");
QContact syncAlice;
QContactName an;
an.setFirstName("Alice");
an.setMiddleName("In");
an.setLastName("Wonderland");
syncAlice.saveDetail(&an);
QContactPhoneNumber aph;
aph.setNumber("123454321");
syncAlice.saveDetail(&aph);
QContactEmailAddress aem;
aem.setEmailAddress("alice@wonderland.tld");
syncAlice.saveDetail(&aem);
QContactStatusFlags af;
af.setFlag(QContactStatusFlags::IsAdded, true);
syncAlice.saveDetail(&af);
QContact syncBob;
QContactName bn;
bn.setFirstName("Bob");
bn.setMiddleName("The");
bn.setLastName("Constructor");
syncBob.saveDetail(&bn);
QContactPhoneNumber bph;
bph.setNumber("543212345");
syncBob.saveDetail(&bph);
QContactEmailAddress bem;
bem.setEmailAddress("bob@construction.tld");
syncBob.saveDetail(&bem);
QContactStatusFlags bf;
bf.setFlag(QContactStatusFlags::IsAdded, true);
syncBob.saveDetail(&bf);
QList<QContact> addedCollectionContacts;
addedCollectionContacts.append(syncAlice);
addedCollectionContacts.append(syncBob);
additions.insert(&remoteAddressbook, &addedCollectionContacts);
const QtContactsSqliteExtensions::ContactManagerEngine::ConflictResolutionPolicy policy(
QtContactsSqliteExtensions::ContactManagerEngine::PreserveLocalChanges);
// initial sync cycle: remote has a non-empty addressbook.
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>(),
policy, true, &err));
QCOMPARE(err, QContactManager::NoError);
QVERIFY(!remoteAddressbook.id().isNull()); // id should have been set during save operation.
QVERIFY(!addedCollectionContacts.at(0).id().isNull()); // id should have been set during save operation.
QVERIFY(!addedCollectionContacts.at(1).id().isNull()); // id should have been set during save operation.
QCOMPARE(addedCollectionContacts.at(0).collectionId(), remoteAddressbook.id());
QCOMPARE(addedCollectionContacts.at(1).collectionId(), remoteAddressbook.id());
syncAlice = addedCollectionContacts.at(0);
syncBob = addedCollectionContacts.at(1);
// wait a while. not necessary but for timestamp debugging purposes...
QTest::qWait(250);
// now perform a local modification:
// add a contact
QContact syncCharlie;
syncCharlie.setCollectionId(remoteAddressbook.id());
QContactName cn;
cn.setFirstName("Charlie");
cn.setMiddleName("The");
cn.setLastName("Horse");
syncCharlie.saveDetail(&cn);
QContactPhoneNumber cph;
cph.setNumber("987656789");
syncCharlie.saveDetail(&cph);
QContactEmailAddress cem;
cem.setEmailAddress("charlie@horse.tld");
syncCharlie.saveDetail(&cem);
QVERIFY(m_cm->saveContact(&syncCharlie));
// now begin a new sync cycle. fetch local changes for push to remote server.
// this should report the local addition of the Charlie contact.
QList<QContact> addedContacts;
QList<QContact> modifiedContacts;
QList<QContact> deletedContacts;
QList<QContact> unmodifiedContacts;
QVERIFY(cme->fetchContactChanges(
remoteAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 1);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 0);
QCOMPARE(unmodifiedContacts.count(), 2);
QCOMPARE(unmodifiedContacts.at(0).id(), syncAlice.id());
QCOMPARE(unmodifiedContacts.at(1).id(), syncBob.id());
QCOMPARE(addedContacts.first().id(), syncCharlie.id());
syncAlice = unmodifiedContacts.at(0);
syncBob = unmodifiedContacts.at(1);
syncCharlie = addedContacts.first();
// now we simulate the case where:
// while the sync plugin is upsyncing the local addition to the remote server,
// the device user modifies another contact locally. This modification is
// "unhandled" in the current sync cycle, as the sync plugin doesn't know that
// this change exists, yet.
syncAlice = m_cm->contact(syncAlice.id());
aph = syncAlice.detail<QContactPhoneNumber>();
aph.setNumber("111111111");
QVERIFY(syncAlice.saveDetail(&aph));
QContactHobby ah = syncAlice.detail<QContactHobby>();
ah.setHobby("Tennis");
QVERIFY(syncAlice.saveDetail(&ah));
aem = syncAlice.detail<QContactEmailAddress>();
QVERIFY(syncAlice.removeDetail(&aem));
QVERIFY(m_cm->saveContact(&syncAlice));
// now the sync plugin has successfully upsynced the local addition change.
// it now downsyncs the remote change: deletion of Bob.
bf = syncBob.detail<QContactStatusFlags>();
bf.setFlag(QContactStatusFlags::IsAdded, false);
bf.setFlag(QContactStatusFlags::IsDeleted, true);
syncBob.saveDetail(&bf, QContact::IgnoreAccessConstraints);
// write the remote changes to the local database.
additions.clear();
modifications.clear();
QList<QContact> modifiedCollectionContacts;
modifiedCollectionContacts.append(syncBob); // deletion
modifications.insert(&remoteAddressbook, &modifiedCollectionContacts);
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>(),
policy, true, &err));
QCOMPARE(err, QContactManager::NoError);
// the previous sync cycle is completed.
// now ensure that the previously unhandled change is reported
// during the next sync cycle.
addedContacts.clear();
modifiedContacts.clear();
deletedContacts.clear();
unmodifiedContacts.clear();
QVERIFY(cme->fetchContactChanges(
remoteAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 0);
QCOMPARE(modifiedContacts.count(), 1);
QCOMPARE(deletedContacts.count(), 0);
QCOMPARE(unmodifiedContacts.count(), 1);
QCOMPARE(modifiedContacts.first().id(), syncAlice.id());
QCOMPARE(unmodifiedContacts.first().id(), syncCharlie.id());
// ensure the specific changes are reported.
syncAlice = modifiedContacts.first();
QCOMPARE(syncAlice.detail<QContactHobby>().hobby(), ah.hobby());
QVERIFY(syncAlice.detail<QContactHobby>().value(QContactDetail__FieldChangeFlags).toInt() & QContactDetail__ChangeFlag_IsAdded);
QCOMPARE(syncAlice.detail<QContactPhoneNumber>().number(), aph.number());
QVERIFY(syncAlice.detail<QContactPhoneNumber>().value(QContactDetail__FieldChangeFlags).toInt() & QContactDetail__ChangeFlag_IsModified);
QVERIFY(syncAlice.detail<QContactEmailAddress>().value(QContactDetail__FieldChangeFlags).toInt() & QContactDetail__ChangeFlag_IsDeleted);
// clean up.
QVERIFY(m_cm->removeCollection(remoteAddressbook.id()));
QVERIFY(cme->clearChangeFlags(remoteAddressbook.id(), &err));
}
void tst_synctransactions::multipleCollections()
{
QtContactsSqliteExtensions::ContactManagerEngine *cme = QtContactsSqliteExtensions::contactManagerEngine(*m_cm);
QContactManager::Error err = QContactManager::NoError;
QHash<QContactCollection*, QList<QContact> *> additions;
QHash<QContactCollection*, QList<QContact> *> modifications;
QContactCollection remoteAddressbook;
remoteAddressbook.setMetaData(QContactCollection::KeyName, QStringLiteral("test"));
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME, "tst_synctransactions");
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID, 5);
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_REMOTEPATH, "/addressbooks/test");
QContactCollection anotherAddressbook;
anotherAddressbook.setMetaData(QContactCollection::KeyName, QStringLiteral("another"));
anotherAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME, "tst_synctransactions");
anotherAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID, 5);
anotherAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_REMOTEPATH, "/addressbooks/another");
QContact syncAlice;
QContactName an;
an.setFirstName("Alice");
an.setMiddleName("In");
an.setLastName("Wonderland");
syncAlice.saveDetail(&an);
QContactPhoneNumber aph;
aph.setNumber("123454321");
syncAlice.saveDetail(&aph);
QContactEmailAddress aem;
aem.setEmailAddress("alice@wonderland.tld");
syncAlice.saveDetail(&aem);
QContactStatusFlags af;
af.setFlag(QContactStatusFlags::IsAdded, true);
syncAlice.saveDetail(&af);
QContact syncBob;
QContactName bn;
bn.setFirstName("Bob");
bn.setMiddleName("The");
bn.setLastName("Constructor");
syncBob.saveDetail(&bn);
QContactPhoneNumber bph;
bph.setNumber("543212345");
syncBob.saveDetail(&bph);
QContactEmailAddress bem;
bem.setEmailAddress("bob@construction.tld");
syncBob.saveDetail(&bem);
QContactStatusFlags bf;
bf.setFlag(QContactStatusFlags::IsAdded, true);
syncBob.saveDetail(&bf);
QList<QContact> addedCollectionContacts;
addedCollectionContacts.append(syncAlice);
addedCollectionContacts.append(syncBob);
additions.insert(&remoteAddressbook, &addedCollectionContacts);
QList<QContact> emptyCollectionContacts;
additions.insert(&anotherAddressbook, &emptyCollectionContacts);
const QtContactsSqliteExtensions::ContactManagerEngine::ConflictResolutionPolicy policy(
QtContactsSqliteExtensions::ContactManagerEngine::PreserveLocalChanges);
// initial sync cycle: remote has a non-empty addressbook.
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>(),
policy, true, &err));
QCOMPARE(err, QContactManager::NoError);
QVERIFY(!remoteAddressbook.id().isNull()); // id should have been set during save operation.
QVERIFY(!anotherAddressbook.id().isNull()); // id should have been set during save operation.
QVERIFY(!addedCollectionContacts.at(0).id().isNull()); // id should have been set during save operation.
QVERIFY(!addedCollectionContacts.at(1).id().isNull()); // id should have been set during save operation.
QCOMPARE(addedCollectionContacts.at(0).collectionId(), remoteAddressbook.id());
QCOMPARE(addedCollectionContacts.at(1).collectionId(), remoteAddressbook.id());
syncAlice = addedCollectionContacts.at(0);
syncBob = addedCollectionContacts.at(1);
// wait a while. not necessary but for timestamp debugging purposes...
QTest::qWait(250);
// modify an addressbook locally.
anotherAddressbook.setMetaData(QContactCollection::KeyDescription, QStringLiteral("another test addressbook"));
QVERIFY(m_cm->saveCollection(&anotherAddressbook));
// and add a contact to it locally.
QContact syncCharlie;
syncCharlie.setCollectionId(anotherAddressbook.id());
QContactName cn;
cn.setFirstName("Charlie");
cn.setMiddleName("The");
cn.setLastName("Horse");
syncCharlie.saveDetail(&cn);
QContactPhoneNumber cph;
cph.setNumber("987656789");
syncCharlie.saveDetail(&cph);
QContactEmailAddress cem;
cem.setEmailAddress("charlie@horse.tld");
syncCharlie.saveDetail(&cem);
QVERIFY(m_cm->saveContact(&syncCharlie));
// also simulate a local deletion of a contact in the other addressbook.
QVERIFY(m_cm->removeContact(syncBob.id()));
// begin a new sync cycle
// first, fetch local collection changes using the sync API.
// note that the remoteAddressbook will be reported as unmodified
// even though its content changed, as this API only reports
// changes to collection metadata.
QList<QContactCollection> addedCollections;
QList<QContactCollection> modifiedCollections;
QList<QContactCollection> deletedCollections;
QList<QContactCollection> unmodifiedCollections;
QVERIFY(cme->fetchCollectionChanges(
5, QString(), // should be able to fetch by accountId
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedCollections.size(), 0);
QCOMPARE(modifiedCollections.size(), 1);
QCOMPARE(deletedCollections.size(), 0);
QCOMPARE(unmodifiedCollections.size(), 1);
QCOMPARE(modifiedCollections.at(0).id(), anotherAddressbook.id());
QCOMPARE(unmodifiedCollections.at(0).id(), remoteAddressbook.id());
// then fetch local contact changes within each collection.
QList<QContact> addedContacts;
QList<QContact> modifiedContacts;
QList<QContact> deletedContacts;
QList<QContact> unmodifiedContacts;
QVERIFY(cme->fetchContactChanges(
remoteAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 0);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 1);
QCOMPARE(unmodifiedContacts.count(), 1);
QCOMPARE(deletedContacts.at(0).id(), syncBob.id());
QCOMPARE(unmodifiedContacts.at(0).id(), syncAlice.id());
addedContacts.clear();
modifiedContacts.clear();
deletedContacts.clear();
unmodifiedContacts.clear();
QVERIFY(cme->fetchContactChanges(
anotherAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 1);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 0);
QCOMPARE(unmodifiedContacts.count(), 0);
QCOMPARE(addedContacts.at(0).id(), syncCharlie.id());
// note: performing that operation multiple times should return the same results,
// as fetching changes should not clear any change flags which are set.
addedContacts.clear();
modifiedContacts.clear();
deletedContacts.clear();
unmodifiedContacts.clear();
QVERIFY(cme->fetchContactChanges(
remoteAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 0);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 1);
QCOMPARE(unmodifiedContacts.count(), 1);
QCOMPARE(deletedContacts.at(0).id(), syncBob.id());
QCOMPARE(unmodifiedContacts.at(0).id(), syncAlice.id());
addedContacts.clear();
modifiedContacts.clear();
deletedContacts.clear();
unmodifiedContacts.clear();
QVERIFY(cme->fetchContactChanges(
anotherAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 1);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 0);
QCOMPARE(unmodifiedContacts.count(), 0);
QCOMPARE(addedContacts.at(0).id(), syncCharlie.id());
// finally, simulate storing remote changes to the local database.
// in this simulated sync cycle, no remote changes occurred, so just clear the change flags
// for the two synced addressbooks. This should also purge the deleted Bob contact.
QVERIFY(cme->clearChangeFlags(anotherAddressbook.id(), &err));
QCOMPARE(err, QContactManager::NoError);
QVERIFY(cme->clearChangeFlags(remoteAddressbook.id(), &err));
QCOMPARE(err, QContactManager::NoError);
// now simulate local deletion of the anotherAddressbook.
QVERIFY(m_cm->removeCollection(anotherAddressbook.id()));
// the contact within that collection should be marked as deleted
// and thus not retrievable using the normal API unless the specific
// IsDeleted filter is set.
QContact deletedContact = m_cm->contact(syncCharlie.id());
QCOMPARE(m_cm->error(), QContactManager::DoesNotExistError);
QVERIFY(deletedContact.id().isNull());
QContactIdFilter idFilter;
idFilter.setIds(QList<QContactId>() << syncCharlie.id());
deletedContacts = m_cm->contacts(idFilter & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContacts.size(), 1);
QCOMPARE(deletedContacts.at(0).id(), syncCharlie.id());
QCOMPARE(deletedContacts.at(0).detail<QContactPhoneNumber>().number(), syncCharlie.detail<QContactPhoneNumber>().number());
QContactCollectionFilter allCollections;
deletedContacts = m_cm->contacts(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContacts.size(), 1); // should not include Bob, who should have been purged due to clearChangeFlags().
QCOMPARE(deletedContacts.at(0).id(), syncCharlie.id());
// now simulate another sync cycle.
// step one: get local collection changes.
addedCollections.clear();
modifiedCollections.clear();
deletedCollections.clear();
unmodifiedCollections.clear();
QVERIFY(cme->fetchCollectionChanges(
0, QStringLiteral("tst_synctransactions"), // should be able to fetch by applicationName
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedCollections.size(), 0);
QCOMPARE(modifiedCollections.size(), 0);
QCOMPARE(deletedCollections.size(), 1);
QCOMPARE(unmodifiedCollections.size(), 1);
QCOMPARE(deletedCollections.at(0).id(), anotherAddressbook.id());
QCOMPARE(unmodifiedCollections.at(0).id(), remoteAddressbook.id());
// step two: get local contact changes.
addedContacts.clear();
modifiedContacts.clear();
deletedContacts.clear();
unmodifiedContacts.clear();
QVERIFY(cme->fetchContactChanges(
remoteAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 0);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 0);
QCOMPARE(unmodifiedContacts.count(), 1);
QCOMPARE(unmodifiedContacts.at(0).id(), syncAlice.id());
syncAlice = unmodifiedContacts.at(0);
addedContacts.clear();
modifiedContacts.clear();
deletedContacts.clear();
unmodifiedContacts.clear();
QVERIFY(cme->fetchContactChanges(
anotherAddressbook.id(),
&addedContacts,
&modifiedContacts,
&deletedContacts,
&unmodifiedContacts,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedContacts.count(), 0);
QCOMPARE(modifiedContacts.count(), 0);
QCOMPARE(deletedContacts.count(), 1);
QCOMPARE(unmodifiedContacts.count(), 0);
QCOMPARE(deletedContacts.at(0).id(), syncCharlie.id());
// step three: store remote changes to local database.
QContactHobby ah;
ah.setHobby("Tennis");
QVERIFY(syncAlice.saveDetail(&ah));
af = syncAlice.detail<QContactStatusFlags>();
af.setFlag(QContactStatusFlags::IsAdded, false);
af.setFlag(QContactStatusFlags::IsModified, true);
QVERIFY(syncAlice.saveDetail(&af, QContact::IgnoreAccessConstraints));
additions.clear();
modifications.clear();
QList<QContact> modifiedCollectionContacts;
modifiedCollectionContacts.append(syncAlice);
modifications.insert(&remoteAddressbook, &modifiedCollectionContacts);
QVERIFY(cme->storeChanges(
&additions,
&modifications,
QList<QContactCollectionId>(),
policy, true, &err));
QCOMPARE(err, QContactManager::NoError);
QVERIFY(cme->clearChangeFlags(anotherAddressbook.id(), &err));
QCOMPARE(err, QContactManager::NoError);
// the above operations should have cleared change flags, causing purge of Charlie etc.
deletedContacts = m_cm->contacts(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContacts.size(), 0);
// clean up.
QVERIFY(m_cm->removeCollection(remoteAddressbook.id()));
QVERIFY(cme->clearChangeFlags(remoteAddressbook.id(), &err));
}
void tst_synctransactions::syncRequests()
{
// Now test that the sync transaction request classes work properly.
// This test does the same that the singleCollection_multipleCycles()
// test does, but with requests rather than raw engine calls.
QContactCollectionFilter allCollections;
QContactCollectionId remoteAddressbookId;
QContactId aliceId, bobId, charlieId;
{
QContactCollection remoteAddressbook;
remoteAddressbook.setMetaData(QContactCollection::KeyName, QStringLiteral("test"));
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME, "tst_synctransactions");
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID, 5);
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_REMOTEPATH, "/addressbooks/test");
QContact syncAlice;
QContactName an;
an.setFirstName("Alice");
an.setMiddleName("In");
an.setLastName("Wonderland");
syncAlice.saveDetail(&an);
QContactPhoneNumber aph;
aph.setNumber("123454321");
syncAlice.saveDetail(&aph);
QContactEmailAddress aem;
aem.setEmailAddress("alice@wonderland.tld");
syncAlice.saveDetail(&aem);
QContactStatusFlags af;
af.setFlag(QContactStatusFlags::IsAdded, true);
syncAlice.saveDetail(&af);
QContact syncBob;
QContactName bn;
bn.setFirstName("Bob");
bn.setMiddleName("The");
bn.setLastName("Constructor");
syncBob.saveDetail(&bn);
QContactPhoneNumber bph;
bph.setNumber("543212345");
syncBob.saveDetail(&bph);
QContactEmailAddress bem;
bem.setEmailAddress("bob@construction.tld");
syncBob.saveDetail(&bem);
QContactStatusFlags bf;
bf.setFlag(QContactStatusFlags::IsAdded, true);
syncBob.saveDetail(&bf);
QList<QContact> addedCollectionContacts;
addedCollectionContacts.append(syncAlice);
addedCollectionContacts.append(syncBob);
QHash<QContactCollection, QList<QContact> > additions;
additions.insert(remoteAddressbook, addedCollectionContacts);
QContactChangesSaveRequest *csr = new QContactChangesSaveRequest;
csr->setManager(m_cm);
csr->setAddedCollections(additions);
csr->setClearChangeFlags(true);
csr->start();
QVERIFY(csr->waitForFinished(5000));
QCOMPARE(csr->error(), QContactManager::NoError);
// ensure that the values have been updated as a result of the operation
// e.g. to include ids etc.
remoteAddressbookId = csr->addedCollections().keys().first().id();
QVERIFY(!remoteAddressbookId.isNull());
aliceId = csr->addedCollections().value(csr->addedCollections().keys().first()).first().id();
bobId = csr->addedCollections().value(csr->addedCollections().keys().first()).last().id();
QVERIFY(!aliceId.isNull());
QVERIFY(!bobId.isNull());
QVERIFY(aliceId != bobId);
QContact reloadAlice = m_cm->contact(aliceId);
QCOMPARE(m_cm->error(), QContactManager::NoError);
QCOMPARE(reloadAlice.detail<QContactPhoneNumber>().number(), syncAlice.detail<QContactPhoneNumber>().number());
QCOMPARE(reloadAlice.detail<QContactEmailAddress>().emailAddress(), syncAlice.detail<QContactEmailAddress>().emailAddress());
QContact reloadBob = m_cm->contact(bobId);
QCOMPARE(m_cm->error(), QContactManager::NoError);
QCOMPARE(reloadBob.detail<QContactPhoneNumber>().number(), syncBob.detail<QContactPhoneNumber>().number());
QCOMPARE(reloadBob.detail<QContactEmailAddress>().emailAddress(), syncBob.detail<QContactEmailAddress>().emailAddress());
}
{
// now perform some local modifications:
// add a contact
QContact syncCharlie;
syncCharlie.setCollectionId(remoteAddressbookId);
QContactName cn;
cn.setFirstName("Charlie");
cn.setMiddleName("The");
cn.setLastName("Horse");
syncCharlie.saveDetail(&cn);
QContactPhoneNumber cph;
cph.setNumber("987656789");
syncCharlie.saveDetail(&cph);
QContactEmailAddress cem;
cem.setEmailAddress("charlie@horse.tld");
syncCharlie.saveDetail(&cem);
QVERIFY(m_cm->saveContact(&syncCharlie));
charlieId = syncCharlie.id();
// delete a contact
QVERIFY(m_cm->removeContact(bobId));
// modify a contact
QContact syncAlice = m_cm->contact(aliceId);
QContactPhoneNumber aph = syncAlice.detail<QContactPhoneNumber>();
aph.setNumber("111111111");
QVERIFY(syncAlice.saveDetail(&aph));
QVERIFY(m_cm->saveContact(&syncAlice));
}
{
// now perform a second sync cycle.
// first, retrieve local collection metadata changes we need to push to remote server.
QContactCollectionChangesFetchRequest *ccfr = new QContactCollectionChangesFetchRequest;
ccfr->setManager(m_cm);
ccfr->setApplicationName(QStringLiteral("tst_synctransactions"));
ccfr->start();
QVERIFY(ccfr->waitForFinished(5000));
QCOMPARE(ccfr->error(), QContactManager::NoError);
QVERIFY(ccfr->addedCollections().isEmpty());
QVERIFY(ccfr->modifiedCollections().isEmpty());
QVERIFY(ccfr->removedCollections().isEmpty());
QCOMPARE(ccfr->unmodifiedCollections().size(), 1);
QCOMPARE(ccfr->unmodifiedCollections().first().id(), remoteAddressbookId);
// second, retrieve local contact changes we need to push to the remote server.
QContactChangesFetchRequest *cfr = new QContactChangesFetchRequest;
cfr->setManager(m_cm);
cfr->setCollectionId(remoteAddressbookId);
cfr->start();
QVERIFY(cfr->waitForFinished(5000));
QCOMPARE(cfr->error(), QContactManager::NoError);
QCOMPARE(cfr->addedContacts().size(), 1);
QCOMPARE(cfr->addedContacts().first().id(), charlieId);
QCOMPARE(cfr->modifiedContacts().size(), 1);
QCOMPARE(cfr->modifiedContacts().first().id(), aliceId);
QCOMPARE(cfr->removedContacts().size(), 1);
QCOMPARE(cfr->removedContacts().first().id(), bobId);
QCOMPARE(cfr->unmodifiedContacts().size(), 0);
// at this point, Bob should have been marked as deleted,
// and should not be accessible using the normal access API.
QContact deletedBob = m_cm->contact(bobId);
QCOMPARE(m_cm->error(), QContactManager::DoesNotExistError);
QVERIFY(deletedBob.id().isNull());
// but we should still be able to access deleted Bob via specific filter.
QList<QContactId> deletedContactIds = m_cm->contactIds(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContactIds.size(), 1);
QVERIFY(deletedContactIds.contains(bobId));
QList<QContact> deletedContacts = m_cm->contacts(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContacts.size(), 1);
QCOMPARE(deletedContacts.first().detail<QContactPhoneNumber>().number(), QStringLiteral("543212345")); // Bob's phone number.
// third, fetch changes from the remote server, and calculate the delta.
// in this case, we simulate that the user added a hobby on the remote server
// for contact Alice, and deleted contact Charlie, and these changes need
// to be stored to the local database.
QContact syncAlice = cfr->modifiedContacts().first();
QContactHobby ah;
ah.setHobby("Tennis");
syncAlice.saveDetail(&ah);
QContactStatusFlags af = syncAlice.detail<QContactStatusFlags>();
af.setFlag(QContactStatusFlags::IsModified, true);
syncAlice.saveDetail(&af, QContact::IgnoreAccessConstraints);
QContact syncCharlie = cfr->addedContacts().first();
QContactStatusFlags cf = syncCharlie.detail<QContactStatusFlags>();
cf.setFlag(QContactStatusFlags::IsDeleted, true);
syncCharlie.saveDetail(&cf, QContact::IgnoreAccessConstraints);
QContactCollection remoteAddressbook = m_cm->collection(remoteAddressbookId);
QHash<QContactCollection, QList<QContact> > modifications;
modifications.insert(remoteAddressbook, QList<QContact>() << syncAlice << syncCharlie);
QContactChangesSaveRequest *csr = new QContactChangesSaveRequest;
csr->setManager(m_cm);
csr->setClearChangeFlags(true);
csr->setModifiedCollections(modifications);
csr->start();
QVERIFY(csr->waitForFinished(5000));
QCOMPARE(csr->error(), QContactManager::NoError);
// Alice should have been updated with the new hobby.
// The other details should not have been changed.
syncAlice = m_cm->contact(aliceId);
QCOMPARE(syncAlice.detail<QContactHobby>().hobby(), QStringLiteral("Tennis"));
QCOMPARE(syncAlice.detail<QContactPhoneNumber>().number(), QStringLiteral("111111111"));
// we should no longer be able to access the deleted contacts,
// as the clearChangeFlags parameter was "true" in the above request.
deletedContactIds.clear();
deletedContactIds = m_cm->contactIds(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContactIds.size(), 0);
}
{
// now perform another sync cycle.
// there should be no local changes reported since the last clearChangeFlags()
// (in this case, since the last storeChanges() call).
// first, retrieve local collection metadata changes we need to push to remote server.
QContactCollectionChangesFetchRequest *ccfr = new QContactCollectionChangesFetchRequest;
ccfr->setManager(m_cm);
ccfr->setApplicationName(QStringLiteral("tst_synctransactions"));
ccfr->start();
QVERIFY(ccfr->waitForFinished(5000));
QCOMPARE(ccfr->error(), QContactManager::NoError);
QVERIFY(ccfr->addedCollections().isEmpty());
QVERIFY(ccfr->modifiedCollections().isEmpty());
QVERIFY(ccfr->removedCollections().isEmpty());
QCOMPARE(ccfr->unmodifiedCollections().size(), 1);
QCOMPARE(ccfr->unmodifiedCollections().first().id(), remoteAddressbookId);
// second, retrieve local contact changes we need to push to the remote server.
QContactChangesFetchRequest *cfr = new QContactChangesFetchRequest;
cfr->setManager(m_cm);
cfr->setCollectionId(remoteAddressbookId);
cfr->start();
QVERIFY(cfr->waitForFinished(5000));
QCOMPARE(cfr->error(), QContactManager::NoError);
QCOMPARE(cfr->addedContacts().size(), 0);
QCOMPARE(cfr->modifiedContacts().size(), 0);
QCOMPARE(cfr->removedContacts().size(), 0);
QCOMPARE(cfr->unmodifiedContacts().size(), 1);
QCOMPARE(cfr->unmodifiedContacts().first().id(), aliceId);
// third, report remote changes and store locally
// in this case, we simulate remote deletion of the entire collection.
QContactChangesSaveRequest *csr = new QContactChangesSaveRequest;
csr->setManager(m_cm);
csr->setClearChangeFlags(true);
csr->setRemovedCollections(QList<QContactCollectionId>() << remoteAddressbookId);
csr->start();
QVERIFY(csr->waitForFinished(5000));
QCOMPARE(csr->error(), QContactManager::NoError);
// attempting to fetch the collection should fail
QContactCollection deletedCollection = m_cm->collection(remoteAddressbookId);
QCOMPARE(m_cm->error(), QContactManager::DoesNotExistError);
QVERIFY(deletedCollection.id().isNull());
// attempting to fetch deleted contacts should return no results.
// the deletion of the contacts as a result of the deletion of the collection
// will in this case be applied immediately (and purged) due to the
// clearChangeFlags=true parameter to the above storeChanges() call.
QList<QContactId> deletedContactIds = m_cm->contactIds(allCollections & QContactStatusFlags::matchFlag(QContactStatusFlags::IsDeleted, QContactFilter::MatchContains));
QCOMPARE(deletedContactIds.size(), 0);
}
}
bool haveExpectedContent(const QContact &c, const QString &phone, TestSyncAdaptor::PhoneModifiability modifiability, const QString &email)
{
const QContactPhoneNumber &phn(c.detail<QContactPhoneNumber>());
TestSyncAdaptor::PhoneModifiability modif = TestSyncAdaptor::ImplicitlyModifiable;
if (phn.values().contains(QContactDetail__FieldModifiable)) {
modif = phn.value<bool>(QContactDetail__FieldModifiable)
? TestSyncAdaptor::ExplicitlyModifiable
: TestSyncAdaptor::ExplicitlyNonModifiable;
}
return phn.number() == phone && modif == modifiability
&& c.detail<QContactEmailAddress>().emailAddress() == email;
}
void tst_synctransactions::twcsa_nodelta()
{
// construct a sync adaptor, and prefill its read-write collection with 3 contacts.
const int accountId = 3;
const QString applicationName = QStringLiteral("tst_synctransactions::twcsa_nodelta");
TestSyncAdaptor tsa(accountId, applicationName, *m_cm);
tsa.addRemoteContact("John", "One", "1111111", TestSyncAdaptor::ImplicitlyModifiable);
tsa.addRemoteContact("Luke", "Two", "2222222", TestSyncAdaptor::ExplicitlyModifiable);
tsa.addRemoteContact("Mark", "Three", "3333333", TestSyncAdaptor::ExplicitlyNonModifiable);
// perform the initial sync cycle.
QSignalSpy finishedSpy(&tsa, SIGNAL(finished()));
QSignalSpy failedSpy(&tsa, SIGNAL(failed()));
tsa.performTwoWaySync();
QTRY_COMPARE(failedSpy.count() + finishedSpy.count(), 1);
QTRY_COMPARE(finishedSpy.count(), 1);
// should have 8 more contacts than we had before:
// two built-in contacts from non-aggregable read-only collection,
// and then 3 constituent contacts from the read-write collection, plus 3 aggregates.
QContactCollectionFilter allCollections;
QList<QContact> allContacts = m_cm->contacts(allCollections);
QContactId aliceId, bobId;
QContactId johnId, lukeId, markId;
QContactId aggJohnId, aggLukeId, aggMarkId;
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
const bool isAggregate = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size() > 0;
const QString fname = c.detail<QContactName>().firstName();
if (!isAggregate && fname == QStringLiteral("Alice")) aliceId = c.id();
if (isAggregate && fname == QStringLiteral("Alice")) QCOMPARE(isAggregate, false); // force failure.
if (!isAggregate && fname == QStringLiteral("Bob")) bobId = c.id();
if (isAggregate && fname == QStringLiteral("Bob")) QCOMPARE(isAggregate, false); // force failure.
if (!isAggregate && fname == QStringLiteral("John")) johnId = c.id();
if (isAggregate && fname == QStringLiteral("John")) aggJohnId = c.id();
if (!isAggregate && fname == QStringLiteral("Luke")) lukeId = c.id();
if (isAggregate && fname == QStringLiteral("Luke")) aggLukeId = c.id();
if (!isAggregate && fname == QStringLiteral("Mark")) markId = c.id();
if (isAggregate && fname == QStringLiteral("Mark")) aggMarkId = c.id();
}
QCOMPARE(allContacts.size(), 8);
QVERIFY(aliceId != QContactId());
QVERIFY(bobId != QContactId());
QVERIFY(johnId != QContactId());
QVERIFY(lukeId != QContactId());
QVERIFY(markId != QContactId());
QVERIFY(aggJohnId != QContactId());
QVERIFY(aggLukeId != QContactId());
QVERIFY(aggMarkId != QContactId());
// ensure that the collections themselves have been downsynced
QContactCollection emptyCollection, readonlyCollection, readwriteCollection;
QString emptyCollectionCtag, readonlyCollectionCtag, readwriteCollectionCtag;
for (const QContactCollection &c : m_cm->collections()) {
const int collectionAccountId = c.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID).toInt();
const QString collectionAppName = c.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME).toString();
const QString collectionName = c.metaData(QContactCollection::KeyName).toString();
if (collectionAccountId == accountId && collectionAppName == applicationName) {
if (collectionName == QStringLiteral("Empty")) {
emptyCollection = c;
emptyCollectionCtag = c.extendedMetaData(QStringLiteral("ctag")).toString();
} else if (collectionName == QStringLiteral("ReadOnly")) {
readonlyCollection = c;
readonlyCollectionCtag = c.extendedMetaData(QStringLiteral("ctag")).toString();
} else {
QCOMPARE(collectionName, QStringLiteral("ReadWrite"));
readwriteCollection = c;
readwriteCollectionCtag = c.extendedMetaData(QStringLiteral("ctag")).toString();
}
}
}
QVERIFY(!emptyCollection.id().isNull());
QVERIFY(!readonlyCollection.id().isNull());
QVERIFY(!readwriteCollection.id().isNull());
QVERIFY(!emptyCollectionCtag.isEmpty());
QVERIFY(!readonlyCollectionCtag.isEmpty());
QVERIFY(!readwriteCollectionCtag.isEmpty());
// ensure that the downsynced contacts have the data we expect
// note that aggregate contact details are explicitly non-modifiable always.
QVERIFY(haveExpectedContent(m_cm->contact(aliceId), QStringLiteral("123123123"), TestSyncAdaptor::ExplicitlyNonModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(bobId), QString(), TestSyncAdaptor::ImplicitlyModifiable, QStringLiteral("bob@constructor.tld")));
QVERIFY(haveExpectedContent(m_cm->contact(johnId), QStringLiteral("1111111"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(aggJohnId), QStringLiteral("1111111"), TestSyncAdaptor::ExplicitlyNonModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(lukeId), QStringLiteral("2222222"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(aggLukeId), QStringLiteral("2222222"), TestSyncAdaptor::ExplicitlyNonModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(markId), QStringLiteral("3333333"), TestSyncAdaptor::ExplicitlyNonModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(aggMarkId), QStringLiteral("3333333"), TestSyncAdaptor::ExplicitlyNonModifiable, QString()));
// and ensure they belong to the collections we expect
QCOMPARE(m_cm->contact(aliceId).collectionId(), readonlyCollection.id());
QCOMPARE(m_cm->contact(bobId).collectionId(), readonlyCollection.id());
QCOMPARE(m_cm->contact(johnId).collectionId(), readwriteCollection.id());
QCOMPARE(m_cm->contact(lukeId).collectionId(), readwriteCollection.id());
QCOMPARE(m_cm->contact(markId).collectionId(), readwriteCollection.id());
QCOMPARE(m_cm->contact(aggJohnId).collectionId(), QContactCollectionId(m_cm->managerUri(), aggregateAddressbookId()));
QCOMPARE(m_cm->contact(aggLukeId).collectionId(), QContactCollectionId(m_cm->managerUri(), aggregateAddressbookId()));
QCOMPARE(m_cm->contact(aggMarkId).collectionId(), QContactCollectionId(m_cm->managerUri(), aggregateAddressbookId()));
// simulate a local addition and local modification
QContact matthew;
QContactName mn;
mn.setFirstName(QStringLiteral("Matthew"));
mn.setLastName(QStringLiteral("Four"));
matthew.saveDetail(&mn);
QContactPhoneNumber mp;
mp.setNumber(QStringLiteral("4444444"));
matthew.saveDetail(&mn);
matthew.saveDetail(&mp);
matthew.setCollectionId(readwriteCollection.id());
QVERIFY(m_cm->saveContact(&matthew));
QContact mark = m_cm->contact(markId);
QContactEmailAddress me;
me.setEmailAddress(QStringLiteral("mark@three.tld"));
mark.saveDetail(&me);
QVERIFY(m_cm->saveContact(&mark));
// simulate a remote modification, and a remote deletion.
tsa.changeRemoteContactPhone(QStringLiteral("John"), QStringLiteral("One"), QStringLiteral("1111123"));
tsa.removeRemoteContact(QStringLiteral("Luke"), QStringLiteral("Two"));
// now perform another sync cycle
tsa.performTwoWaySync();
QTRY_COMPARE(failedSpy.count() + finishedSpy.count(), 2);
QTRY_COMPARE(finishedSpy.count(), 2);
// ensure that the local database now contains the appropriate information
allContacts = m_cm->contacts(allCollections);
QContactId matthewId, aggMatthewId;
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
const bool isAggregate = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size() > 0;
const QString fname = c.detail<QContactName>().firstName();
if (!isAggregate && fname == QStringLiteral("Alice")) QCOMPARE(c.id(), aliceId); // id should not have changed
else if (isAggregate && fname == QStringLiteral("Alice")) QCOMPARE(isAggregate, false); // force failure.
else if (!isAggregate && fname == QStringLiteral("Bob")) QCOMPARE(c.id(), bobId); // id should not have changed
else if (isAggregate && fname == QStringLiteral("Bob")) QCOMPARE(isAggregate, false); // force failure.
else if (!isAggregate && fname == QStringLiteral("John")) QCOMPARE(c.id(), johnId); // id should not have changed
else if (isAggregate && fname == QStringLiteral("John")) QCOMPARE(c.id(), aggJohnId); // id should not have changed
else if (!isAggregate && fname == QStringLiteral("Mark")) QCOMPARE(c.id(), markId); // id should not have changed
else if (isAggregate && fname == QStringLiteral("Mark")) QCOMPARE(c.id(), aggMarkId); // id should not have changed
else if (!isAggregate && fname == QStringLiteral("Matthew")) matthewId = c.id();
else if (isAggregate && fname == QStringLiteral("Matthew")) aggMatthewId = c.id();
else QCOMPARE(fname, QStringLiteral("Alice")); // force failure, unknown/deleted contact seen.
}
QCOMPARE(allContacts.size(), 8);
QVERIFY(aggMarkId != QContactId());
QVERIFY(aggMatthewId != QContactId());
QCOMPARE(m_cm->contact(johnId).detail<QContactPhoneNumber>().number(), QStringLiteral("1111123"));
QCOMPARE(m_cm->contact(aggJohnId).detail<QContactPhoneNumber>().number(), QStringLiteral("1111123"));
QCOMPARE(m_cm->contact(johnId).details<QContactPhoneNumber>().size(), 1);
QCOMPARE(m_cm->contact(aggJohnId).details<QContactPhoneNumber>().size(), 1);
// check the collections are available and that the ctag of the readwrite collection has changed.
for (const QContactCollection &c : m_cm->collections()) {
const int collectionAccountId = c.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID).toInt();
const QString collectionAppName = c.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME).toString();
const QString collectionName = c.metaData(QContactCollection::KeyName).toString();
if (collectionAccountId == accountId && collectionAppName == applicationName) {
if (collectionName == QStringLiteral("Empty")) {
emptyCollection = c;
QCOMPARE(c.extendedMetaData(QStringLiteral("ctag")).toString(), emptyCollectionCtag);
} else if (collectionName == QStringLiteral("ReadOnly")) {
readonlyCollection = c;
QCOMPARE(c.extendedMetaData(QStringLiteral("ctag")).toString(), readonlyCollectionCtag);
} else {
QCOMPARE(collectionName, QStringLiteral("ReadWrite"));
readwriteCollection = c;
QVERIFY(c.extendedMetaData(QStringLiteral("ctag")).toString() != readwriteCollectionCtag);
readwriteCollectionCtag = c.extendedMetaData(QStringLiteral("ctag")).toString();
}
}
}
// and ensure that the remote database contains the appropriate information
QContact remoteMatthew = tsa.remoteContact(QStringLiteral("Matthew"), QStringLiteral("Four"));
QCOMPARE(remoteMatthew.detail<QContactPhoneNumber>().number(), matthew.detail<QContactPhoneNumber>().number());
QContact remoteMark = tsa.remoteContact(QStringLiteral("Mark"), QStringLiteral("Three"));
QCOMPARE(remoteMark.detail<QContactEmailAddress>().emailAddress(), mark.detail<QContactEmailAddress>().emailAddress());
QContact remoteJohn = tsa.remoteContact(QStringLiteral("John"), QStringLiteral("One"));
QCOMPARE(remoteJohn.detail<QContactPhoneNumber>().number(), QStringLiteral("1111123"));
QContact remoteLuke = tsa.remoteContact(QStringLiteral("Luke"), QStringLiteral("Two"));
QCOMPARE(remoteLuke.details<QContactName>().size(), 0); // contact was deleted from remote, shouldn't exist.
// delete the read-write collection locally
QVERIFY(m_cm->removeCollection(readwriteCollection.id()));
// now perform another sync cycle
tsa.performTwoWaySync();
QTRY_COMPARE(failedSpy.count() + finishedSpy.count(), 3);
QTRY_COMPARE(finishedSpy.count(), 3);
// ensure that the local database now contains the appropriate information
allContacts = m_cm->contacts(allCollections);
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
const bool isAggregate = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size() > 0;
const QString fname = c.detail<QContactName>().firstName();
if (!isAggregate && fname == QStringLiteral("Alice")) QCOMPARE(c.id(), aliceId); // id should not have changed
else if (isAggregate && fname == QStringLiteral("Alice")) QCOMPARE(isAggregate, false); // force failure.
else if (!isAggregate && fname == QStringLiteral("Bob")) QCOMPARE(c.id(), bobId); // id should not have changed
else if (isAggregate && fname == QStringLiteral("Bob")) QCOMPARE(isAggregate, false); // force failure.
else QVERIFY(fname == QStringLiteral("Alice") || fname == QStringLiteral("Bob")); // force failure.
}
QCOMPARE(allContacts.size(), 2);
// the readwrite collection should no longer exist locally
for (const QContactCollection &c : m_cm->collections()) {
const int collectionAccountId = c.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID).toInt();
const QString collectionAppName = c.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME).toString();
const QString collectionName = c.metaData(QContactCollection::KeyName).toString();
if (collectionAccountId == accountId && collectionAppName == applicationName) {
if (collectionName == QStringLiteral("Empty")) {
emptyCollection = c;
QCOMPARE(c.extendedMetaData(QStringLiteral("ctag")).toString(), emptyCollectionCtag);
} else if (collectionName == QStringLiteral("ReadOnly")) {
readonlyCollection = c;
QCOMPARE(c.extendedMetaData(QStringLiteral("ctag")).toString(), readonlyCollectionCtag);
} else {
// force a failure, the other collection should have been deleted.
QVERIFY(collectionName == QStringLiteral("Empty") || collectionName == QStringLiteral("ReadOnly"));
}
}
}
// now perform another sync cycle without performing any changes either locally or remotely.
tsa.performTwoWaySync();
QTRY_COMPARE(failedSpy.count() + finishedSpy.count(), 4);
QTRY_COMPARE(finishedSpy.count(), 4);
// no changes should have occurred.
allContacts = m_cm->contacts(allCollections);
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
const bool isAggregate = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size() > 0;
const QString fname = c.detail<QContactName>().firstName();
if (!isAggregate && fname == QStringLiteral("Alice")) QCOMPARE(c.id(), aliceId); // id should not have changed
else if (isAggregate && fname == QStringLiteral("Alice")) QCOMPARE(isAggregate, false); // force failure.
else if (!isAggregate && fname == QStringLiteral("Bob")) QCOMPARE(c.id(), bobId); // id should not have changed
else if (isAggregate && fname == QStringLiteral("Bob")) QCOMPARE(isAggregate, false); // force failure.
else QVERIFY(fname == QStringLiteral("Alice") || fname == QStringLiteral("Bob")); // force failure.
}
QCOMPARE(allContacts.size(), 2);
for (const QContactCollection &c : m_cm->collections()) {
const int collectionAccountId = c.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID).toInt();
const QString collectionAppName = c.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME).toString();
const QString collectionName = c.metaData(QContactCollection::KeyName).toString();
if (collectionAccountId == accountId && collectionAppName == applicationName) {
if (collectionName == QStringLiteral("Empty")) {
emptyCollection = c;
QCOMPARE(c.extendedMetaData(QStringLiteral("ctag")).toString(), emptyCollectionCtag);
} else if (collectionName == QStringLiteral("ReadOnly")) {
readonlyCollection = c;
QCOMPARE(c.extendedMetaData(QStringLiteral("ctag")).toString(), readonlyCollectionCtag);
} else {
// force a failure, the other collection should have been deleted.
QVERIFY(collectionName == QStringLiteral("Empty") || collectionName == QStringLiteral("ReadOnly"));
}
}
}
}
void tst_synctransactions::twcsa_delta()
{
// TODO: a sync plugin which supports delta sync.
}
void tst_synctransactions::twcsa_oneway()
{
// TODO: a sync plugin which only supports to-device sync.
}
/*
void tst_Aggregation::TestSyncAdaptor()
{
QContactDetailFilter allSyncTargets;
setFilterDetail<QContactSyncTarget>(allSyncTargets, QContactSyncTarget::FieldSyncTarget);
QList<QContactId> originalIds = m_cm->contactIds(allSyncTargets);
// add some contacts remotely, and downsync them. It should not result in an upsync.
QString accountId(QStringLiteral("1"));
TestSyncAdaptor tsa(accountId);
tsa.addRemoteContact(accountId, "John", "TsaOne", "1111111", TestSyncAdaptor::ImplicitlyModifiable);
tsa.addRemoteContact(accountId, "Bob", "TsaTwo", "2222222", TestSyncAdaptor::ExplicitlyModifiable);
tsa.addRemoteContact(accountId, "Mark", "TsaThree", "3333333", TestSyncAdaptor::ExplicitlyNonModifiable);
QSignalSpy finishedSpy(&tsa, SIGNAL(finished()));
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 1);
// should have 6 more contacts than we had before (aggregate+synctarget x 3)
QList<QContact> allContacts = m_cm->contacts(allSyncTargets);
QContactId tsaOneStcId, tsaOneAggId, tsaTwoStcId, tsaTwoAggId, tsaThreeStcId, tsaThreeAggId;
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
if (originalIds.contains(c.id())) {
allContacts.removeAt(i);
} else {
bool isAggregate = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size() > 0;
if (c.detail<QContactName>().firstName() == QStringLiteral("John")) {
if (isAggregate) {
tsaOneAggId = c.id();
} else {
tsaOneStcId = c.id();
}
} else if (c.detail<QContactName>().firstName() == QStringLiteral("Bob")) {
if (isAggregate) {
tsaTwoAggId = c.id();
} else {
tsaTwoStcId = c.id();
}
} else if (c.detail<QContactName>().firstName() == QStringLiteral("Mark")) {
if (isAggregate) {
tsaThreeAggId = c.id();
} else {
tsaThreeStcId = c.id();
}
}
}
}
QCOMPARE(allContacts.size(), 6);
QVERIFY(tsaOneStcId != QContactId());
QVERIFY(tsaOneAggId != QContactId());
QVERIFY(tsaTwoStcId != QContactId());
QVERIFY(tsaTwoAggId != QContactId());
QVERIFY(tsaThreeStcId != QContactId());
QVERIFY(tsaThreeAggId != QContactId());
// verify that the added IDs were reported
QSet<QContactId> reportedIds(tsa.modifiedIds(accountId));
QCOMPARE(reportedIds.size(), 3);
QVERIFY(reportedIds.contains(tsaOneStcId));
QVERIFY(reportedIds.contains(tsaTwoStcId));
QVERIFY(reportedIds.contains(tsaThreeStcId));
// and no upsync of local changes should be required (there shouldn't have been any local changes).
QVERIFY(!tsa.upsyncWasRequired(accountId));
QVERIFY(tsa.downsyncWasRequired(accountId));
// ensure that the downsynced contacts have the data we expect
// note that aggregate contacts don't have a modifiability flag, since by definition
// the modification would "actually" occur to some constituent contact detail,
// and thus are considered by the haveExpectedContent function to be ImplicitlyModifiable.
QVERIFY(haveExpectedContent(m_cm->contact(tsaOneStcId), QStringLiteral("1111111"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaOneAggId), QStringLiteral("1111111"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoStcId), QStringLiteral("2222222"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoAggId), QStringLiteral("2222222"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaThreeStcId), QStringLiteral("3333333"), TestSyncAdaptor::ExplicitlyNonModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaThreeAggId), QStringLiteral("3333333"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
// now modify tsaTwo's aggregate - should cause the creation of an incidental local.
// triggering update should then not require downsync but would require upsync.
QContact tsaTwoAggregate = m_cm->contact(tsaTwoAggId);
QContactEmailAddress tsaTwoEmail;
tsaTwoEmail.setEmailAddress("bob@tsatwo.com");
tsaTwoAggregate.saveDetail(&tsaTwoEmail); // new detail, will cause creation of incidental local.
m_cm->saveContact(&tsaTwoAggregate);
allContacts = m_cm->contacts(allSyncTargets);
QContactId tsaTwoLocalId;
for (int i = allContacts.size() - 1; i >= 0; --i) {
if (originalIds.contains(allContacts[i].id())) {
allContacts.removeAt(i);
} else if (allContacts[i].detail<QContactName>().firstName() == QStringLiteral("Bob")
&& allContacts[i].id() != tsaTwoAggId
&& allContacts[i].id() != tsaTwoStcId) {
tsaTwoLocalId = allContacts[i].id();
}
}
QCOMPARE(allContacts.size(), 7); // new incidental local.
QVERIFY(tsaTwoLocalId != QContactId());
// perform the sync.
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 2);
// downsync should not have been required, but upsync should have been.
QVERIFY(!tsa.downsyncWasRequired(accountId));
QVERIFY(tsa.upsyncWasRequired(accountId));
// ensure that the contacts have the data we expect
QVERIFY(haveExpectedContent(m_cm->contact(tsaOneStcId), QStringLiteral("1111111"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaOneAggId), QStringLiteral("1111111"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoStcId), QStringLiteral("2222222"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoLocalId), QString(), TestSyncAdaptor::ImplicitlyModifiable, QStringLiteral("bob@tsatwo.com")));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoAggId), QStringLiteral("2222222"), TestSyncAdaptor::ImplicitlyModifiable, QStringLiteral("bob@tsatwo.com")));
QVERIFY(haveExpectedContent(m_cm->contact(tsaThreeStcId), QStringLiteral("3333333"), TestSyncAdaptor::ExplicitlyNonModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaThreeAggId), QStringLiteral("3333333"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(tsa.remoteContact(accountId, QStringLiteral("Bob"), QStringLiteral("TsaTwo")),
QStringLiteral("2222222"), TestSyncAdaptor::ExplicitlyModifiable, QStringLiteral("bob@tsatwo.com")));
// modify both locally and remotely.
// we modify the phone number locally (ie, the synctarget constituent)
// and the email address remotely (ie, the local constituent)
// and test to ensure that everything is resolved/updated correctly.
QContact tsaTwoStc = m_cm->contact(tsaTwoStcId);
QContactPhoneNumber tsaTwoStcPhn = tsaTwoStc.detail<QContactPhoneNumber>();
tsaTwoStcPhn.setNumber("2222229");
tsaTwoStc.saveDetail(&tsaTwoStcPhn);
QVERIFY(m_cm->saveContact(&tsaTwoStc));
tsa.changeRemoteContactEmail(accountId, "Bob", "TsaTwo", "bob2@tsatwo.com");
// perform the sync.
tsa.performTwoWaySync(accountId);
// ensure that the per-account separation is maintained properly for out of band data etc.
tsa.addRemoteContact(QStringLiteral("2"), QStringLiteral("Jerry"), QStringLiteral("TsaTwoTwo"), QStringLiteral("555"));
tsa.performTwoWaySync(QStringLiteral("2"));
QTRY_COMPARE(finishedSpy.count(), 4); // wait for both to finish
tsa.removeRemoteContact(QStringLiteral("2"), QStringLiteral("Jerry"), QStringLiteral("TsaTwoTwo"));
tsa.performTwoWaySync(QStringLiteral("2"));
QTRY_COMPARE(finishedSpy.count(), 5);
// downsync and upsync should have been required in the original sync for the "main" account.
QVERIFY(tsa.downsyncWasRequired(accountId));
QVERIFY(tsa.upsyncWasRequired(accountId));
// ensure that the contacts have the data we expect
QVERIFY(haveExpectedContent(m_cm->contact(tsaOneStcId), QStringLiteral("1111111"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaOneAggId), QStringLiteral("1111111"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoStcId), QStringLiteral("2222229"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoLocalId), QString(), TestSyncAdaptor::ImplicitlyModifiable, QStringLiteral("bob2@tsatwo.com")));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoAggId), QStringLiteral("2222229"), TestSyncAdaptor::ImplicitlyModifiable, QStringLiteral("bob2@tsatwo.com")));
QVERIFY(haveExpectedContent(m_cm->contact(tsaThreeStcId), QStringLiteral("3333333"), TestSyncAdaptor::ExplicitlyNonModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaThreeAggId), QStringLiteral("3333333"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(tsa.remoteContact(accountId, QStringLiteral("Bob"), QStringLiteral("TsaTwo")),
QStringLiteral("2222229"), TestSyncAdaptor::ExplicitlyModifiable, QStringLiteral("bob2@tsatwo.com")));
// remove a contact locally, ensure that the removal is upsynced.
QVERIFY(tsa.remoteContact(accountId, QStringLiteral("Mark"), QStringLiteral("TsaThree")) != QContact());
QVERIFY(m_cm->removeContact(tsaThreeAggId));
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 6);
QVERIFY(tsa.downsyncWasRequired(accountId)); // the previously upsynced changes which were applied will be returned, hence will be downsynced; but discarded as nonsubstantial / already applied.
QVERIFY(tsa.upsyncWasRequired(accountId));
// ensure that the contacts have the data we expect
QVERIFY(haveExpectedContent(m_cm->contact(tsaOneStcId), QStringLiteral("1111111"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaOneAggId), QStringLiteral("1111111"), TestSyncAdaptor::ImplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoStcId), QStringLiteral("2222229"), TestSyncAdaptor::ExplicitlyModifiable, QString()));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoLocalId), QString(), TestSyncAdaptor::ImplicitlyModifiable, QStringLiteral("bob2@tsatwo.com")));
QVERIFY(haveExpectedContent(m_cm->contact(tsaTwoAggId), QStringLiteral("2222229"), TestSyncAdaptor::ImplicitlyModifiable, QStringLiteral("bob2@tsatwo.com")));
QVERIFY(haveExpectedContent(tsa.remoteContact(accountId, QStringLiteral("Bob"), QStringLiteral("TsaTwo")),
QStringLiteral("2222229"), TestSyncAdaptor::ExplicitlyModifiable, QStringLiteral("bob2@tsatwo.com")));
QVERIFY(tsa.remoteContact(accountId, QStringLiteral("Mark"), QStringLiteral("TsaThree")) == QContact()); // deleted remotely.
// add a contact locally, ensure that the addition is upsynced.
QContact tsaFourLocal;
QContactName tsaFourName;
tsaFourName.setFirstName("Jennifer");
tsaFourName.setLastName("TsaFour");
QContactEmailAddress tsaFourEmail;
tsaFourEmail.setEmailAddress("jennifer@tsafour.com");
tsaFourLocal.saveDetail(&tsaFourName);
tsaFourLocal.saveDetail(&tsaFourEmail);
QVERIFY(m_cm->saveContact(&tsaFourLocal));
QContactId tsaFourLocalId = tsaFourLocal.id();
QContactId tsaFourAggId;
allContacts = m_cm->contacts(allSyncTargets);
for (int i = allContacts.size() - 1; i >= 0; --i) {
if (originalIds.contains(allContacts[i].id())) {
allContacts.removeAt(i);
} else if (allContacts[i].detail<QContactName>().firstName() == QStringLiteral("Jennifer")
&& allContacts[i].id() != tsaFourLocalId) {
tsaFourAggId = allContacts[i].id();
}
}
QVERIFY(tsaFourAggId != QContactId());
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 7);
QVERIFY(!tsa.downsyncWasRequired(accountId)); // there were no remote changes
QVERIFY(tsa.upsyncWasRequired(accountId));
QVERIFY(haveExpectedContent(tsa.remoteContact(accountId, QStringLiteral("Jennifer"), QStringLiteral("TsaFour")),
QString(), TestSyncAdaptor::ImplicitlyModifiable, QStringLiteral("jennifer@tsafour.com")));
// remove some contacts remotely, ensure the removals are downsynced.
QTest::qWait(1);
tsa.removeRemoteContact(accountId, QStringLiteral("Bob"), QStringLiteral("TsaTwo"));
QVERIFY(tsa.remoteContact(accountId, QStringLiteral("Bob"), QStringLiteral("TsaTwo")) == QContact());
tsa.removeRemoteContact(accountId, QStringLiteral("Jennifer"), QStringLiteral("TsaFour"));
QVERIFY(tsa.remoteContact(accountId, QStringLiteral("Jennifer"), QStringLiteral("TsaFour")) == QContact());
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 8);
QVERIFY(tsa.downsyncWasRequired(accountId));
QVERIFY(!tsa.upsyncWasRequired(accountId));
QList<QContactId> allIds = m_cm->contactIds(allSyncTargets);
// the sync target constituents of two and four should be removed.
// the local constituents (and the aggregates) should remain.
QVERIFY(!allIds.contains(tsaTwoStcId));
// the local constituent of tsaTwo should remain even though it's incidental.
QVERIFY(allIds.contains(tsaTwoLocalId));
QVERIFY(allIds.contains(tsaTwoAggId));
// and the tsaFour contact was originally a pure-local addition, so shouldn't be removed.
// it may, after all, have been synced up to other sync sources.
// Note: we should NOT sync up either tsaTwo or tsaFour in subsequent syncs.
QVERIFY(allIds.contains(tsaFourLocalId));
QVERIFY(allIds.contains(tsaFourAggId));
// modify two and four locally, and ensure they don't get synced up.
tsaFourLocal = m_cm->contact(tsaFourLocalId);
tsaFourEmail = tsaFourLocal.detail<QContactEmailAddress>();
tsaFourEmail.setEmailAddress("jennifer2@tsafour.com");
tsaFourLocal.saveDetail(&tsaFourEmail);
m_cm->saveContact(&tsaFourLocal);
tsaTwoAggregate = m_cm->contact(tsaTwoAggId);
tsaTwoEmail = tsaTwoAggregate.detail<QContactEmailAddress>();
tsaTwoEmail.setEmailAddress("bob3@tsatwo.com");
tsaTwoAggregate.saveDetail(&tsaTwoEmail);
m_cm->saveContact(&tsaTwoAggregate);
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 9);
QVERIFY(!tsa.downsyncWasRequired(accountId)); // no remote changes since last sync, and last sync didn't upsync any changes.
QVERIFY(!tsa.upsyncWasRequired(accountId)); // changes shouldn't have been upsynced.
// modify (the only remaining) remote contact, delete the local contacts.
// the conflict should be resolved in favour of the local device, and the
// removal should be upsynced. TODO: support different conflict resolutions.
tsa.changeRemoteContactPhone(accountId, QStringLiteral("John"), QStringLiteral("TsaOne"), QStringLiteral("1111112"));
QVERIFY(m_cm->removeContact(tsaOneAggId));
QVERIFY(m_cm->removeContact(tsaTwoAggId));
QVERIFY(m_cm->removeContact(tsaFourAggId));
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 10);
QVERIFY(tsa.downsyncWasRequired(accountId));
QVERIFY(tsa.upsyncWasRequired(accountId));
allIds = m_cm->contactIds(allSyncTargets);
QVERIFY(!allIds.contains(tsaOneStcId));
QVERIFY(!allIds.contains(tsaOneAggId));
QVERIFY(!allIds.contains(tsaTwoLocalId));
QVERIFY(!allIds.contains(tsaTwoAggId));
QVERIFY(!allIds.contains(tsaFourLocalId));
QVERIFY(!allIds.contains(tsaFourAggId));
// should be back to original set of ids prior to test.
QCOMPARE(originalIds.size(), allIds.size());
foreach (const QContactId &id, allIds) {
QVERIFY(originalIds.contains(id));
}
// now add a new contact remotely, which has a name.
// remove the name locally, and modify it remotely - this will cause a "composed detail" modification update.
tsa.addRemoteContact(accountId, "John", "TsaFive", "555555", TestSyncAdaptor::ImplicitlyModifiable);
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 11);
allIds = m_cm->contactIds(allSyncTargets);
QCOMPARE(allIds.size(), originalIds.size() + 2); // remote + aggregate
allContacts = m_cm->contacts(allSyncTargets);
QContact tsaFiveStc, tsaFiveAgg;
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
if (originalIds.contains(c.id())) {
allContacts.removeAt(i);
} else {
bool isAggregate = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size() > 0;
if (isAggregate) {
tsaFiveAgg = c;
} else {
tsaFiveStc = c;
}
}
}
QVERIFY(tsaFiveAgg.id() != QContactId());
QVERIFY(tsaFiveStc.id() != QContactId());
// now remove the name on the local copy
QContactName tsaFiveName = tsaFiveStc.detail<QContactName>();
tsaFiveStc.removeDetail(&tsaFiveName);
QVERIFY(m_cm->saveContact(&tsaFiveStc));
// now modify the name on the remote server, and trigger sync.
// during this process, the local contact will NOT have a QContactName detail
// so the modification pair will be <null, newName>. This used to trigger a bug.
tsa.changeRemoteContactName(accountId, "John", "TsaFive", "Jonathan", "TsaFive");
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 12);
// ensure that the contact contains the data we expect
// currently, we only support PreserveLocalChanges conflict resolution
tsaFiveStc = m_cm->contact(tsaFiveStc.id());
QCOMPARE(tsaFiveStc.detail<QContactName>().firstName(), QString());
QCOMPARE(tsaFiveStc.detail<QContactName>().lastName(), QString());
QCOMPARE(tsaFiveStc.detail<QContactPhoneNumber>().number(), QStringLiteral("555555"));
// now do the same test as above, but this time remove the name remotely and modify it locally.
tsa.addRemoteContact(accountId, "James", "TsaSix", "666666", TestSyncAdaptor::ImplicitlyModifiable);
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 13);
allIds = m_cm->contactIds(allSyncTargets);
QCOMPARE(allIds.size(), originalIds.size() + 4); // remote + aggregate for Five and Six
allContacts = m_cm->contacts(allSyncTargets);
QContact tsaSixStc, tsaSixAgg;
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
if (originalIds.contains(c.id())) {
allContacts.removeAt(i);
} else {
bool isAggregate = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size() > 0;
if (isAggregate && c.id() != tsaFiveAgg.id()) {
tsaSixAgg = c;
} else if (!isAggregate && c.id() != tsaFiveStc.id()){
tsaSixStc = c;
}
}
}
QVERIFY(tsaSixAgg.id() != QContactId());
QVERIFY(tsaSixStc.id() != QContactId());
// now modify the name on the local copy
QContactName tsaSixName = tsaSixStc.detail<QContactName>();
tsaSixName.setFirstName("Jimmy");
tsaSixStc.saveDetail(&tsaSixName);
QVERIFY(m_cm->saveContact(&tsaSixStc));
// now remove the name on the remote server, and trigger sync.
// during this process, the remote contact will NOT have a QContactName detail
// so the modification pair will be <newName, null>.
tsa.changeRemoteContactName(accountId, "James", "TsaSix", "", "");
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 14);
// ensure that the contact contains the data we expect
// currently, we only support PreserveLocalChanges conflict resolution
tsaSixStc = m_cm->contact(tsaSixStc.id());
QCOMPARE(tsaSixStc.detail<QContactName>().firstName(), QStringLiteral("Jimmy"));
QCOMPARE(tsaSixStc.detail<QContactName>().lastName(), QStringLiteral("TsaSix"));
QCOMPARE(tsaSixStc.detail<QContactPhoneNumber>().number(), QStringLiteral("666666"));
// partial clean up, locally remove all contact aggregates from sync adapter.
QVERIFY(m_cm->removeContact(tsaFiveAgg.id()));
QVERIFY(m_cm->removeContact(tsaSixAgg.id()));
QCOMPARE(m_cm->contactIds(allSyncTargets).size(), originalIds.size());
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 15);
// the following test ensures that "remote duplicate removal" works correctly.
// - have multiple duplicated contacts server-side
// - sync them down
// - remove all but one duplicate server-side
// - sync the changes (removals)
// - ensure that the removals are applied correctly device-side.
tsa.addRemoteDuplicates(accountId, "John", "Duplicate", "1234321");
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 16);
allContacts = m_cm->contacts(allSyncTargets);
int syncTargetConstituentsCount = 0, aggCount = 0;
QContact tsaSevenAgg;
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
if (originalIds.contains(c.id())) {
allContacts.removeAt(i);
} else {
int tempAggCount = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size();
if (tempAggCount > 0) {
aggCount = tempAggCount;
tsaSevenAgg = c;
} else {
syncTargetConstituentsCount += 1;
}
}
}
QVERIFY(tsaSevenAgg.id() != QContactId());
QCOMPARE(aggCount, 3); // the aggregate should aggregate the 3 duplicates
QCOMPARE(syncTargetConstituentsCount, 3); // there should be 3 duplicates
tsa.mergeRemoteDuplicates(accountId);
tsa.performTwoWaySync(accountId);
QTRY_COMPARE(finishedSpy.count(), 17);
allContacts = m_cm->contacts(allSyncTargets);
syncTargetConstituentsCount = 0; aggCount = 0;
for (int i = allContacts.size() - 1; i >= 0; --i) {
const QContact &c(allContacts[i]);
if (originalIds.contains(c.id())) {
allContacts.removeAt(i);
} else {
int tempAggCount = c.relatedContacts(QStringLiteral("Aggregates"), QContactRelationship::Second).size();
if (tempAggCount > 0) {
tsaSevenAgg = c;
aggCount = tempAggCount;
} else {
syncTargetConstituentsCount += 1;
}
}
}
QCOMPARE(aggCount, 1); // now there should be just one sync target constituent.
QCOMPARE(syncTargetConstituentsCount, 1);
// clean up.
QVERIFY(m_cm->removeContact(tsaSevenAgg.id()));
QCOMPARE(m_cm->contactIds(allSyncTargets).size(), originalIds.size());
tsa.removeAllContacts();
}
*/
QTEST_GUILESS_MAIN(tst_synctransactions)
#include "tst_synctransactions.moc"
|