1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL21$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 or version 3 as published by the Free
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22** following information to ensure the GNU Lesser General Public License
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** As a special exception, The Qt Company gives you certain additional
27** rights. These rights are described in The Qt Company LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** $QT_END_LICENSE$
31**
32****************************************************************************/
33
34#include <QtTest/QtTest>
35#include <QtCore/QMetaType>
36
37#include <QtContacts/qcontacts.h>
38
39//TESTED_COMPONENT=src/contacts
40
41QTCONTACTS_USE_NAMESPACE
42
43Q_DECLARE_METATYPE(QContact)
44Q_DECLARE_METATYPE(QContactSortOrder)
45
46class tst_QContactSortOrder : public QObject
47{
48Q_OBJECT
49
50public slots:
51 void init();
52 void cleanup();
53
54private slots:
55 void traits();
56
57 void sortObject();
58
59 void compareContact_data();
60 void compareContact();
61
62 void datastream_data();
63 void datastream();
64
65 void debugstream_data();
66 void debugstream();
67};
68
69void tst_QContactSortOrder::init()
70{
71}
72
73void tst_QContactSortOrder::cleanup()
74{
75}
76
77void tst_QContactSortOrder::traits()
78{
79 QTypeInfo<QContactSortOrder> ti;
80 QVERIFY(ti.isComplex);
81 QVERIFY(!ti.isStatic);
82 QVERIFY(!ti.isLarge);
83 QVERIFY(!ti.isPointer);
84 QVERIFY(!ti.isDummy);
85 QCOMPARE(uint(ti.sizeOf), uint(sizeof(void *)));
86}
87
88void tst_QContactSortOrder::sortObject()
89{
90 QContactSortOrder sortorder;
91
92 /* Defaults */
93 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
94 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
95 QVERIFY(sortorder.detailField() == -1);
96 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
97 QVERIFY(sortorder.caseSensitivity() == Qt::CaseSensitive);
98 QVERIFY(!sortorder.isValid());
99 QVERIFY(sortorder == QContactSortOrder());
100
101 /* Blank Policy */
102 sortorder.setBlankPolicy(QContactSortOrder::BlanksFirst);
103 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksFirst);
104 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
105 QVERIFY(sortorder.detailField() == -1);
106 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
107 QVERIFY(sortorder.caseSensitivity() == Qt::CaseSensitive);
108 QVERIFY(!sortorder.isValid());
109 QVERIFY(sortorder != QContactSortOrder());
110
111 sortorder.setBlankPolicy(QContactSortOrder::BlanksLast);
112 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
113 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
114 QVERIFY(sortorder.detailField() == -1);
115 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
116 QVERIFY(sortorder.caseSensitivity() == Qt::CaseSensitive);
117 QVERIFY(!sortorder.isValid());
118 QVERIFY(sortorder == QContactSortOrder());
119
120 /* Direction */
121 sortorder.setDirection(Qt::DescendingOrder);
122 QVERIFY(sortorder.direction() == Qt::DescendingOrder);
123 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
124 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
125 QVERIFY(sortorder.detailField() == -1);
126 QVERIFY(sortorder.caseSensitivity() == Qt::CaseSensitive);
127 QVERIFY(!sortorder.isValid());
128 QVERIFY(sortorder != QContactSortOrder());
129
130 sortorder.setDirection(Qt::AscendingOrder);
131 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
132 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
133 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
134 QVERIFY(sortorder.detailField() == -1);
135 QVERIFY(sortorder.caseSensitivity() == Qt::CaseSensitive);
136 QVERIFY(!sortorder.isValid());
137 QVERIFY(sortorder == QContactSortOrder());
138
139 /* Case sensitivity */
140 sortorder.setCaseSensitivity(Qt::CaseInsensitive);
141 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
142 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
143 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
144 QVERIFY(sortorder.detailField() == -1);
145 QVERIFY(sortorder.caseSensitivity() == Qt::CaseInsensitive);
146 QVERIFY(!sortorder.isValid());
147 QVERIFY(sortorder != QContactSortOrder());
148
149 sortorder.setCaseSensitivity(Qt::CaseSensitive);
150 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
151 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
152 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
153 QVERIFY(sortorder.detailField() == -1);
154 QVERIFY(sortorder.caseSensitivity() == Qt::CaseSensitive);
155 QVERIFY(!sortorder.isValid());
156 QVERIFY(sortorder == QContactSortOrder());
157
158 /* Definitions */
159 sortorder.setDetailType(type: QContactDetail::DetailType(), field: -1);
160 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
161 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
162 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
163 QVERIFY(sortorder.detailField() == -1);
164 QVERIFY(!sortorder.isValid());
165
166 sortorder.setDetailType(type: QContactDetail::TypeUndefined, field: -1);
167 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
168 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
169 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
170 QVERIFY(sortorder.detailField() == -1);
171 QVERIFY(!sortorder.isValid());
172
173 sortorder.setDetailType(type: QContactDetail::TypeAddress, field: QContactAddress::FieldStreet);
174 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
175 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
176 QVERIFY(sortorder.detailType() == QContactDetail::TypeAddress);
177 QVERIFY(sortorder.detailField() == QContactAddress::FieldStreet);
178 QVERIFY(sortorder.isValid());
179
180 sortorder.setDetailType(type: QContactDetail::DetailType(), field: QContactAddress::FieldStreet);
181 QVERIFY(sortorder.direction() == Qt::AscendingOrder);
182 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksLast);
183 QVERIFY(sortorder.detailType() == QContactDetail::TypeUndefined);
184 QVERIFY(sortorder.detailField() == QContactAddress::FieldStreet);
185 QVERIFY(!sortorder.isValid());
186
187 /* Copy ctor */
188 sortorder.setDetailType(type: QContactDetail::TypeAddress, field: QContactAddress::FieldStreet);
189 sortorder.setBlankPolicy(QContactSortOrder::BlanksFirst);
190 sortorder.setDirection(Qt::DescendingOrder);
191 QVERIFY(sortorder.direction() == Qt::DescendingOrder);
192 QVERIFY(sortorder.blankPolicy() == QContactSortOrder::BlanksFirst);
193 QVERIFY(sortorder.detailType() == QContactDetail::TypeAddress);
194 QVERIFY(sortorder.detailField() == QContactAddress::FieldStreet);
195 QVERIFY(sortorder.isValid());
196
197 QContactSortOrder other(sortorder);
198 QVERIFY(other.direction() == Qt::DescendingOrder);
199 QVERIFY(other.blankPolicy() == QContactSortOrder::BlanksFirst);
200 QVERIFY(other.detailType() == QContactDetail::TypeAddress);
201 QVERIFY(other.detailField() == QContactAddress::FieldStreet);
202 QVERIFY(other.isValid());
203 QVERIFY(other == sortorder);
204 QVERIFY(!(other != sortorder));
205
206 other.setDetailType(type: QContactDetail::TypePhoneNumber, field: QContactAddress::FieldStreet);
207 QVERIFY(other != sortorder);
208
209 other.setDetailType(type: QContactDetail::TypeAddress, field: QContactAddress::FieldRegion);
210 QVERIFY(other != sortorder);
211
212 /* Assignment operator */
213 QContactSortOrder another;
214 another = other;
215 QVERIFY(another.direction() == Qt::DescendingOrder);
216 QVERIFY(another.blankPolicy() == QContactSortOrder::BlanksFirst);
217 QVERIFY(another.detailType() == QContactDetail::TypeAddress);
218 QVERIFY(another.detailField() == QContactAddress::FieldRegion);
219 QVERIFY(another.isValid());
220 QVERIFY(another == other);
221 QVERIFY(!(other != another));
222
223 /* Self assignment */
224 another = another;
225 QVERIFY(another.direction() == Qt::DescendingOrder);
226 QVERIFY(another.blankPolicy() == QContactSortOrder::BlanksFirst);
227 QVERIFY(another.detailType() == QContactDetail::TypeAddress);
228 QVERIFY(another.detailField() == QContactAddress::FieldRegion);
229 QVERIFY(another.isValid());
230 QVERIFY(another == other);
231 QVERIFY(!(other != another));
232}
233
234void tst_QContactSortOrder::compareContact_data()
235{
236 QTest::addColumn<QContact>(name: "contact1");
237 QTest::addColumn<QContact>(name: "contact2");
238 QTest::addColumn<QContactSortOrder>(name: "sortOrder");
239 QTest::addColumn<int>(name: "expected");
240
241 QContact emptyContact;
242
243 {
244 QContact contact1;
245 QContactNote note1;
246 note1.setNote(QLatin1String("note"));
247 contact1.saveDetail(detail: &note1);
248 QContactDisplayLabel displayLabel1;
249 displayLabel1.setLabel(QLatin1String("My label"));
250 contact1.saveDetail(detail: &displayLabel1);
251 QContactGuid guid1;
252 guid1.setGuid(QLatin1String("98765"));
253 contact1.saveDetail(detail: &guid1);
254
255 QContact contact2;
256 QContactNote note2;
257 note2.setNote(QLatin1String("NOTE"));
258 contact2.saveDetail(detail: &note2);
259 QContactDisplayLabel displayLabel2;
260 displayLabel2.setLabel(QLatin1String("label"));
261 contact2.saveDetail(detail: &displayLabel2);
262 QContactGuid guid2;
263 guid2.setGuid(QLatin1String("12345"));
264 contact2.saveDetail(detail: &guid2);
265 QContactName name;
266 name.setFirstName("John");
267 contact2.saveDetail(detail: &name);
268
269 QContactSortOrder sortOrder;
270
271 QTest::newRow(dataTag: "empty sortorder") << contact1 << contact2 << sortOrder << 0;
272
273 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
274 QTest::newRow(dataTag: "empty contacts") << emptyContact << emptyContact << sortOrder << 0;
275
276 QTest::newRow(dataTag: "equals") << contact1 << contact1 << sortOrder << 0;
277 QTest::newRow(dataTag: "equals") << contact2 << contact2 << sortOrder << 0;
278
279 sortOrder.setBlankPolicy(QContactSortOrder::BlanksFirst);
280 {
281 sortOrder.setDetailType(type: QContactDetail::TypeName, field: -1);
282 QTest::newRow(dataTag: "blanks first, field presence") << emptyContact << contact2 << sortOrder << -1;
283 QTest::newRow(dataTag: "blanks first, field presence") << contact1 << contact2 << sortOrder << -1;
284
285 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
286 QTest::newRow(dataTag: "blanks first") << emptyContact << contact2 << sortOrder << -1;
287 QTest::newRow(dataTag: "blanks first") << contact2 << emptyContact << sortOrder << 1;
288 sortOrder.setDetailType(type: QContactDetail::TypeName, field: QContactName::FieldFirstName);
289 QTest::newRow(dataTag: "blanks first") << emptyContact << contact2 << sortOrder << -1;
290 sortOrder.setDetailType(type: QContactDetail::TypeTag, field: QContactTag::FieldTag);
291 QTest::newRow(dataTag: "blanks first") << contact1 << contact2 << sortOrder << 0;
292 }
293
294 sortOrder.setBlankPolicy(QContactSortOrder::BlanksLast);
295 {
296 sortOrder.setDetailType(type: QContactDetail::TypeName, field: -1);
297 QTest::newRow(dataTag: "blanks last, field presence") << emptyContact << contact2 << sortOrder << 1;
298 QTest::newRow(dataTag: "blanks last, field presence") << contact1 << contact2 << sortOrder << 1;
299
300 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
301 QTest::newRow(dataTag: "blanks last") << emptyContact << contact2 << sortOrder << 1;
302 QTest::newRow(dataTag: "blanks last") << contact2 << emptyContact << sortOrder << -1;
303 sortOrder.setDetailType(type: QContactDetail::TypeName, field: QContactName::FieldFirstName);
304 QTest::newRow(dataTag: "blanks first") << emptyContact << contact2 << sortOrder << 1;
305 sortOrder.setDetailType(type: QContactDetail::TypeTag, field: QContactTag::FieldTag);
306 QTest::newRow(dataTag: "blanks first") << contact1 << contact2 << sortOrder << 0;
307 }
308
309 sortOrder.setDirection(Qt::AscendingOrder);
310 sortOrder.setCaseSensitivity(Qt::CaseSensitive);
311 {
312 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
313 QTest::newRow(dataTag: "asc, cs") << contact1 << contact2 << sortOrder << -1;
314 QTest::newRow(dataTag: "asc, cs") << contact2 << contact1 << sortOrder << 1;
315
316 sortOrder.setCaseSensitivity(Qt::CaseInsensitive);
317 QTest::newRow(dataTag: "asc, ci") << contact1 << contact2 << sortOrder << 0;
318 sortOrder.setDetailType(type: QContactDetail::TypeDisplayLabel, field: QContactDisplayLabel::FieldLabel);
319 QTest::newRow(dataTag: "asc, ci") << contact1 << contact2 << sortOrder << 1;
320 QTest::newRow(dataTag: "asc, ci") << contact2 << contact1 << sortOrder << -1;
321 }
322
323 sortOrder.setDirection(Qt::DescendingOrder);
324 {
325 sortOrder.setCaseSensitivity(Qt::CaseSensitive);
326 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
327 QTest::newRow(dataTag: "desc, cs") << contact1 << contact2 << sortOrder << 1;
328 QTest::newRow(dataTag: "desc, cs") << contact2 << contact1 << sortOrder << -1;
329
330 sortOrder.setCaseSensitivity(Qt::CaseInsensitive);
331 QTest::newRow(dataTag: "desc, ci") << contact1 << contact2 << sortOrder << 0;
332 sortOrder.setDetailType(type: QContactDetail::TypeDisplayLabel, field: QContactDisplayLabel::FieldLabel);
333 QTest::newRow(dataTag: "desc, ci") << contact1 << contact2 << sortOrder << -1;
334 QTest::newRow(dataTag: "desc, ci") << contact2 << contact1 << sortOrder << 1;
335 }
336 }
337
338 { // ensure non-existence is treated just like blank-ness
339 QContact contact1;
340 QContactName name1;
341 name1.setFirstName("John");
342 name1.setLastName("Smith");
343 contact1.saveDetail(detail: &name1);
344
345 QContact contact2;
346 QContactName name2;
347 name2.setFirstName("John");
348 contact2.saveDetail(detail: &name2);
349
350 QContactSortOrder sortOrder;
351 sortOrder.setDetailType(type: QContactDetail::TypeName, field: QContactName::FieldLastName);
352 sortOrder.setDirection(Qt::AscendingOrder);
353 sortOrder.setCaseSensitivity(Qt::CaseInsensitive);
354
355 sortOrder.setBlankPolicy(QContactSortOrder::BlanksFirst);
356 QTest::newRow(dataTag: "non-existence vs blank-ness, blanks first") << contact1 << contact2 << sortOrder << 1;
357 QTest::newRow(dataTag: "non-existence vs blank-ness, blanks first") << emptyContact << contact1 << sortOrder << -1;
358 QTest::newRow(dataTag: "non-existence vs blank-ness, blanks first") << emptyContact << contact2 << sortOrder << 0;
359
360 sortOrder.setBlankPolicy(QContactSortOrder::BlanksLast);
361 QTest::newRow(dataTag: "non-existence vs blank-ness, blanks last") << contact1 << contact2 << sortOrder << -1;
362 QTest::newRow(dataTag: "non-existence vs blank-ness, blanks last") << emptyContact << contact1 << sortOrder << 1;
363 QTest::newRow(dataTag: "non-existence vs blank-ness, blanks last") << emptyContact << contact2 << sortOrder << 0;
364 }
365}
366
367void tst_QContactSortOrder::compareContact()
368{
369 QFETCH(QContact, contact1);
370 QFETCH(QContact, contact2);
371 QFETCH(QContactSortOrder, sortOrder);
372 QFETCH(int, expected);
373
374 int actual = QContactManagerEngine::compareContact(a: contact1, b: contact2, sortOrders: (QList<QContactSortOrder>() << sortOrder));
375 actual = qBound(min: -1, val: actual, max: 1);
376 QCOMPARE(actual, expected);
377}
378
379void tst_QContactSortOrder::datastream_data()
380{
381 QTest::addColumn<QContactSortOrder>(name: "sortOrderIn");
382
383 {
384 QContactSortOrder sortOrder;
385 QTest::newRow(dataTag: "default") << sortOrder;
386 }
387
388 {
389 QContactSortOrder sortOrder;
390 sortOrder.setBlankPolicy(QContactSortOrder::BlanksFirst);
391 sortOrder.setDirection(Qt::DescendingOrder);
392 sortOrder.setCaseSensitivity(Qt::CaseInsensitive);
393 QTest::newRow(dataTag: "invalid") << sortOrder;
394 }
395
396 {
397 QContactSortOrder sortOrder;
398 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
399 QTest::newRow(dataTag: "detail") << sortOrder;
400 }
401
402 {
403 QContactSortOrder sortOrder;
404 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
405 sortOrder.setBlankPolicy(QContactSortOrder::BlanksFirst);
406 sortOrder.setDirection(Qt::DescendingOrder);
407 sortOrder.setCaseSensitivity(Qt::CaseInsensitive);
408 QTest::newRow(dataTag: "complete") << sortOrder;
409 }
410}
411
412void tst_QContactSortOrder::datastream()
413{
414 QFETCH(QContactSortOrder, sortOrderIn);
415
416 QByteArray buffer;
417 QDataStream stream1(&buffer, QIODevice::WriteOnly);
418 stream1 << sortOrderIn;
419
420 QVERIFY(buffer.size() > 0);
421
422 QDataStream stream2(buffer);
423 QContactSortOrder sortOrderOut;
424 stream2 >> sortOrderOut;
425 QCOMPARE(sortOrderOut, sortOrderIn);
426}
427
428void tst_QContactSortOrder::debugstream_data()
429{
430 QTest::addColumn<QContactSortOrder>(name: "sortOrder");
431 QTest::addColumn<QString>(name: "messageExpected");
432
433 {
434 QContactSortOrder sortOrder;
435 QTest::newRow(dataTag: "default") << sortOrder << "QContactSortOrder(detailType=0,detailField=-1,blankPolicy=1,direction=0,caseSensitivity=1)";
436 }
437
438 {
439 QContactSortOrder sortOrder;
440 sortOrder.setBlankPolicy(QContactSortOrder::BlanksFirst);
441 sortOrder.setDirection(Qt::DescendingOrder);
442 sortOrder.setCaseSensitivity(Qt::CaseInsensitive);
443 QTest::newRow(dataTag: "invalid") << sortOrder << "QContactSortOrder(detailType=0,detailField=-1,blankPolicy=0,direction=1,caseSensitivity=0)";
444 }
445
446 {
447 QContactSortOrder sortOrder;
448 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
449 QTest::newRow(dataTag: "detail") << sortOrder << "QContactSortOrder(detailType=17,detailField=0,blankPolicy=1,direction=0,caseSensitivity=1)";
450 }
451
452 {
453 QContactSortOrder sortOrder;
454 sortOrder.setDetailType(type: QContactDetail::TypeNote, field: QContactNote::FieldNote);
455 sortOrder.setBlankPolicy(QContactSortOrder::BlanksFirst);
456 sortOrder.setDirection(Qt::DescendingOrder);
457 sortOrder.setCaseSensitivity(Qt::CaseInsensitive);
458 QTest::newRow(dataTag: "complete") << sortOrder << "QContactSortOrder(detailType=17,detailField=0,blankPolicy=0,direction=1,caseSensitivity=0)";
459 }
460}
461
462void tst_QContactSortOrder::debugstream()
463{
464 QFETCH(QContactSortOrder, sortOrder);
465 QFETCH(QString, messageExpected);
466
467 QTest::ignoreMessage(type: QtDebugMsg, message: messageExpected.toUtf8());
468 qDebug() << sortOrder;
469}
470
471QTEST_MAIN(tst_QContactSortOrder)
472#include "tst_qcontactsortorder.moc"
473

source code of qtpim/tests/auto/contacts/qcontactsortorder/tst_qcontactsortorder.cpp