1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL21$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 or version 3 as published by the Free
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22** following information to ensure the GNU Lesser General Public License
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** As a special exception, The Qt Company gives you certain additional
27** rights. These rights are described in The Qt Company LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** $QT_END_LICENSE$
31**
32****************************************************************************/
33
34//TESTED_COMPONENT=src/contacts
35
36#include <QtTest/QtTest>
37
38#include <QtContacts/qcontacts.h>
39
40QTCONTACTS_USE_NAMESPACE
41
42static inline QContactId makeId(const QString &managerName, uint id)
43{
44 return QContactId(QStringLiteral("qtcontacts:basic%1:").arg(a: managerName), QByteArray(reinterpret_cast<const char *>(&id), sizeof(uint)));
45}
46
47
48class tst_QContactRelationship: public QObject
49{
50Q_OBJECT
51
52public:
53 tst_QContactRelationship();
54 virtual ~tst_QContactRelationship();
55
56private slots:
57 void operations();
58 void emptiness();
59 void hash();
60 void datastream();
61};
62
63tst_QContactRelationship::tst_QContactRelationship()
64{
65}
66
67tst_QContactRelationship::~tst_QContactRelationship()
68{
69}
70
71void tst_QContactRelationship::operations()
72{
73 QContactRelationship r1;
74
75 QContact contact1, contact2;
76
77 QVERIFY(r1.first() == QContactId());
78 QVERIFY(r1.second() == QContactId());
79 QVERIFY(r1.relationshipType() == QString());
80
81 r1.setFirst(contact1.id());
82 QVERIFY(r1.first() == contact1.id());
83 r1.setSecond(contact2.id());
84 QVERIFY(r1.second() == contact2.id());
85 QVERIFY(r1.first() == contact1.id());
86 QVERIFY(r1.relationshipType() == QString());
87
88 r1.setRelationshipType(QContactRelationship::HasSpouse());
89 QVERIFY(r1.relationshipType() == QContactRelationship::HasSpouse());
90}
91
92void tst_QContactRelationship::emptiness()
93{
94 QContactRelationship r1, r2, r3;
95
96 QContact contact1, contact2, contact3;
97 contact1.setId(makeId(managerName: "test", id: 1));
98 contact2.setId(makeId(managerName: "test", id: 2));
99 contact3.setId(makeId(managerName: "test", id: 3));
100
101 QVERIFY(r1.first() == QContactId());
102 QVERIFY(r1.second() == QContactId());
103 QVERIFY(r1.relationshipType() == QString());
104
105 r1.setFirst(contact1.id());
106 QVERIFY(r1.first() == contact1.id());
107 r1.setSecond(contact2.id());
108 QVERIFY(r1.second() == contact2.id());
109 QVERIFY(r1.first() == contact1.id());
110 QVERIFY(r1.relationshipType() == QString());
111
112 r1.setRelationshipType(QContactRelationship::HasSpouse());
113 QVERIFY(r1.relationshipType() == QContactRelationship::HasSpouse());
114
115 r2 = r1;
116 QVERIFY(r1 == r2);
117 QVERIFY(r1 != r3);
118 QVERIFY(r2 != r3);
119
120 r3.setFirst(contact3.id());
121 r3.setSecond(contact2.id());
122 r3.setRelationshipType(QContactRelationship::HasAssistant());
123
124 r2.setFirst(contact3.id());
125 QVERIFY(r1 != r2);
126 QVERIFY(r2 != r3);
127 QVERIFY(r3 != r1);
128}
129
130void tst_QContactRelationship::hash()
131{
132 QContactRelationship r1;
133 QContact contact1;
134 contact1.setId(makeId(managerName: "a", id: 1));
135 r1.setFirst(contact1.id());
136 QContact contact2;
137 contact2.setId(makeId(managerName: "b", id: 2));
138 r1.setSecond(contact2.id());
139 r1.setRelationshipType(QContactRelationship::HasMember());
140
141 QContactRelationship r2;
142 r2.setFirst(contact1.id());
143 r2.setSecond(contact2.id());
144 r2.setRelationshipType(QContactRelationship::HasMember());
145
146 QContactRelationship r3;
147 r3.setFirst(contact1.id());
148 QContact contact3;
149 contact1.setId(makeId(managerName: "c", id: 3));
150 r3.setSecond(contact3.id());
151 r3.setRelationshipType(QContactRelationship::HasMember());
152
153 QVERIFY(qHash(r1) == qHash(r2));
154 QVERIFY(qHash(r1) != qHash(r3));
155
156}
157
158void tst_QContactRelationship::datastream()
159{
160 QByteArray buffer;
161 QDataStream stream1(&buffer, QIODevice::WriteOnly);
162 QContactRelationship relationshipIn;
163 QContact contact1;
164 contact1.setId(makeId(managerName: "a", id: 1));
165 relationshipIn.setFirst(contact1.id());
166 QContact contact2;
167 contact2.setId(makeId(managerName: "a", id: 2));
168 relationshipIn.setSecond(contact2.id());
169 relationshipIn.setRelationshipType(QContactRelationship::HasMember());
170 stream1 << relationshipIn;
171
172 QVERIFY(buffer.size() > 0);
173
174 QDataStream stream2(buffer);
175 QContactRelationship relationshipOut;
176 stream2 >> relationshipOut;
177 QCOMPARE(relationshipOut, relationshipIn);
178}
179
180QTEST_MAIN(tst_QContactRelationship)
181#include "tst_qcontactrelationship.moc"
182

source code of qtpim/tests/auto/contacts/qcontactrelationship/tst_qcontactrelationship.cpp