1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the test suite of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
43
#include <QtTest/QtTest>
44
#include <q3combobox.h>
45
#include <q3listbox.h>
46
#include <qlineedit.h>
47
#include <QValidator>
48
49
class tst_Q3ComboBox : public QObject
50
{
51
    Q_OBJECT
52
53
public slots:
54
    void initTestCase();
55
    void cleanupTestCase();
56
    void init();
57
    void cleanup();
58
59
private slots:
60
    void q3combobox_data();
61
    void q3combobox();
62
    void autoCompletion_data();
63
    void autoCompletion();
64
    void autoResize_data();
65
    void autoResize();
66
    void changeItem_data();
67
    void changeItem();
68
    void clear_data();
69
    void clear();
70
    void clearEdit_data();
71
    void clearEdit();
72
    void count_data();
73
    void count();
74
    void currentItem_data();
75
    void currentItem();
76
    void currentText_data();
77
    void currentText();
78
    void duplicatesEnabled_data();
79
    void duplicatesEnabled();
80
    void editable_data();
81
    void editable();
82
    void insertItem_data();
83
    void insertItem();
84
    void insertStringList_data();
85
    void insertStringList();
86
    void insertStrList_data();
87
    void insertStrList();
88
    void maxCount_data();
89
    void maxCount();
90
    void pixmap();
91
    void removeItem_data();
92
    void removeItem();
93
    void setValidator();
94
    void keyPressEvent_data();
95
    void keyPressEvent();
96
    void wheelEvent_data();
97
    void wheelEvent();
98
    
99
    void task231724_clear();
100
};
101
102
// Subclass that exposes the protected functions.
103
class SubQ3ComboBox : public Q3ComboBox
104
{
105
public:
106
    void call_activated(int index)
107
        { return SubQ3ComboBox::activated(index); }
108
109
    void call_activated(QString const& str)
110
        { return SubQ3ComboBox::activated(str); }
111
112
    void call_focusInEvent(QFocusEvent* e)
113
        { return SubQ3ComboBox::focusInEvent(e); }
114
115
    void call_focusOutEvent(QFocusEvent* e)
116
        { return SubQ3ComboBox::focusOutEvent(e); }
117
118
    void call_highlighted(QString const& str)
119
        { return SubQ3ComboBox::highlighted(str); }
120
121
    void call_highlighted(int index)
122
        { return SubQ3ComboBox::highlighted(index); }
123
124
    void call_keyPressEvent(QKeyEvent* e)
125
        { return SubQ3ComboBox::keyPressEvent(e); }
126
127
    void call_mouseDoubleClickEvent(QMouseEvent* e)
128
        { return SubQ3ComboBox::mouseDoubleClickEvent(e); }
129
130
    void call_mouseMoveEvent(QMouseEvent* e)
131
        { return SubQ3ComboBox::mouseMoveEvent(e); }
132
133
    void call_mousePressEvent(QMouseEvent* e)
134
        { return SubQ3ComboBox::mousePressEvent(e); }
135
136
    void call_mouseReleaseEvent(QMouseEvent* e)
137
        { return SubQ3ComboBox::mouseReleaseEvent(e); }
138
139
    void call_paintEvent(QPaintEvent* e)
140
        { return SubQ3ComboBox::paintEvent(e); }
141
142
    void call_resizeEvent(QResizeEvent* e)
143
        { return SubQ3ComboBox::resizeEvent(e); }
144
145
    void call_styleChange(QStyle& style)
146
        { return SubQ3ComboBox::styleChange(style); }
147
148
    void call_textChanged(QString const& str)
149
        { return SubQ3ComboBox::textChanged(str); }
150
151
    void call_updateMask()
152
        { return SubQ3ComboBox::updateMask(); }
153
154
    void call_wheelEvent(QWheelEvent* e)
155
        { return SubQ3ComboBox::wheelEvent(e); }
156
};
157
158
// This will be called before the first test function is executed.
159
// It is only called once.
160
void tst_Q3ComboBox::initTestCase()
161
{
162
}
163
164
// This will be called after the last test function is executed.
165
// It is only called once.
166
void tst_Q3ComboBox::cleanupTestCase()
167
{
168
}
169
170
// This will be called before each test function is executed.
171
void tst_Q3ComboBox::init()
172
{
173
}
174
175
// This will be called after every test function.
176
void tst_Q3ComboBox::cleanup()
177
{
178
}
179
180
void tst_Q3ComboBox::q3combobox_data()
181
{
182
}
183
184
void tst_Q3ComboBox::q3combobox()
185
{
186
    SubQ3ComboBox box;
187
    box.autoCompletion();
188
    box.autoResize();
189
    box.changeItem(QString(), 0);
190
    box.changeItem(QPixmap(), 0);
191
    box.changeItem(QPixmap(), QString(), 0);
192
    box.clear();
193
    box.clearEdit();
194
    box.clearValidator();
195
    box.count();
196
    box.currentItem();
197
    box.currentText();
198
    box.duplicatesEnabled();
199
    box.editable();
200
    box.hide();
201
    box.insertionPolicy();
202
    box.insertItem(QString());
203
    box.insertItem(QPixmap(), -1);
204
    box.insertItem(QPixmap(), QString(), -1);
205
    box.insertStringList(QStringList());
206
    static const char* items[] = { "red", "green", "blue", 0 };
207
    box.insertStrList(items);
208
    box.lineEdit();
209
    box.listBox();
210
    box.maxCount();
211
    box.pixmap(0);
212
    box.popup();
213
    box.removeItem(0);
214
    box.setAutoCompletion(false);
215
    box.setAutoResize(false);
216
    box.setCurrentItem(0);
217
    box.setCurrentText(QString());
218
    box.setDuplicatesEnabled(true);
219
    box.setEditable(false);
220
    box.setEditText(false);
221
    box.setEnabled(false);
222
    box.setFont(QFont());
223
    box.setInsertionPolicy(Q3ComboBox::NoInsertion);
224
    box.setLineEdit(0);
225
    //box.setListBox(0);
226
    box.setMaxCount(0);
227
    box.setPalette(QPalette());
228
    box.setSizeLimit(0);
229
    box.setValidator(0);
230
    box.sizeHint();
231
    box.sizeLimit();
232
    box.text(0);
233
    box.validator();
234
    box.call_activated(0);
235
    box.call_activated(QString());
236
    box.call_highlighted(0);
237
    box.call_highlighted(QString());
238
    box.call_textChanged(QString());
239
    box.call_updateMask();
240
}
241
242
void tst_Q3ComboBox::autoCompletion_data()
243
{
244
    QTest::addColumn<bool>("autoCompletion");
245
    QTest::newRow("false") << false;
246
    QTest::newRow("true") << true;
247
}
248
249
// public bool autoCompletion() const
250
void tst_Q3ComboBox::autoCompletion()
251
{
252
    QFETCH(bool, autoCompletion);
253
254
    SubQ3ComboBox box;
255
256
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
257
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
258
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
259
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
260
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
261
262
    box.setAutoCompletion(autoCompletion);
263
264
    QCOMPARE(spy0.count(), 0);
265
    QCOMPARE(spy1.count(), 0);
266
    QCOMPARE(spy2.count(), 0);
267
    QCOMPARE(spy3.count(), 0);
268
    QCOMPARE(spy4.count(), 0);
269
270
    QCOMPARE(box.autoCompletion(), autoCompletion);
271
}
272
273
void tst_Q3ComboBox::autoResize_data()
274
{
275
    QTest::addColumn<bool>("autoResize");
276
    QTest::newRow("false") << false;
277
    QTest::newRow("true") << true;
278
}
279
280
// public bool autoResize() const
281
void tst_Q3ComboBox::autoResize()
282
{
283
    QFETCH(bool, autoResize);
284
285
    SubQ3ComboBox box;
286
287
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
288
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
289
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
290
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
291
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
292
293
    QSize oldSize = box.size();
294
    box.setAutoResize(autoResize);
295
296
    QCOMPARE(spy0.count(), 0);
297
    QCOMPARE(spy1.count(), 0);
298
    QCOMPARE(spy2.count(), 0);
299
    QCOMPARE(spy3.count(), 0);
300
    QCOMPARE(spy4.count(), 0);
301
    QCOMPARE(box.autoResize(), autoResize);
302
303
    if (autoResize)
304
        QVERIFY(oldSize != box.size());
305
    else
306
        QCOMPARE(oldSize, box.size());
307
}
308
309
void tst_Q3ComboBox::changeItem_data()
310
{
311
    QTest::addColumn<QPixmap>("pixmap");
312
    QTest::addColumn<QString>("text");
313
    QTest::addColumn<int>("index");
314
    QTest::newRow("null") << QPixmap() << QString("foo") << 0;
315
}
316
317
// public void changeItem(QPixmap const& pixmap, QString const& text, int index)
318
void tst_Q3ComboBox::changeItem()
319
{
320
    QFETCH(QPixmap, pixmap);
321
    QFETCH(QString, text);
322
    QFETCH(int, index);
323
324
    SubQ3ComboBox box;
325
    box.insertItem("stub");
326
327
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
328
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
329
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
330
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
331
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
332
333
    box.changeItem(pixmap, text, index);
334
335
    QCOMPARE(spy0.count(), 0);
336
    QCOMPARE(spy1.count(), 0);
337
    QCOMPARE(spy2.count(), box.listBox() ? 1 : 0);
338
    QCOMPARE(spy3.count(), box.listBox() ? 1 : 0);
339
    QCOMPARE(spy4.count(), 0);
340
341
    QCOMPARE(box.text(index), text);
342
    if (pixmap.isNull()) {
343
        QVERIFY(!box.pixmap(index) || box.pixmap(index)->isNull());
344
    } else {
345
        QCOMPARE(*box.pixmap(index), pixmap);
346
    }
347
}
348
349
void tst_Q3ComboBox::clear_data()
350
{
351
    QTest::addColumn<QStringList>("list");
352
    QTest::newRow("null") << QStringList();
353
    QTest::newRow("1") << (QStringList() << "x");
354
    QTest::newRow("2") << (QStringList() << "x" << "y");
355
    QTest::newRow("3") << (QStringList() << "x" << "y" << "z");
356
}
357
358
// public void clear()
359
void tst_Q3ComboBox::clear()
360
{
361
    QFETCH(QStringList, list);
362
363
    SubQ3ComboBox box;
364
    box.insertStringList(list);
365
366
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
367
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
368
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
369
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
370
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
371
372
    box.clear();
373
    QCOMPARE(box.count(), 0);
374
375
    QCOMPARE(spy0.count(), 0);
376
    QCOMPARE(spy1.count(), 0);
377
    QCOMPARE(spy2.count(), 0);
378
    QCOMPARE(spy3.count(), 0);
379
    QCOMPARE(spy4.count(), 0);
380
}
381
382
void tst_Q3ComboBox::clearEdit_data()
383
{
384
    QTest::addColumn<bool>("editable");
385
    QTest::addColumn<QStringList>("list");
386
    QTest::newRow("non-editable") << false << QStringList();
387
    QTest::newRow("editable-empty") << true << QStringList();
388
    QTest::newRow("editable-3") << true << (QStringList() << "x" << "y" << "z");
389
}
390
391
// public void clearEdit()
392
void tst_Q3ComboBox::clearEdit()
393
{
394
    QFETCH(QStringList, list);
395
    QFETCH(bool, editable);
396
397
    SubQ3ComboBox box;
398
    box.setEditable(editable);
399
    box.insertStringList(list);
400
401
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
402
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
403
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
404
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
405
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
406
407
    box.clearEdit();
408
    QCOMPARE(box.count(), list.count());
409
    for (int i = 0; i < list.count(); ++i)
410
        QCOMPARE(box.text(i), list.at(i));
411
    if (editable) {
412
        QVERIFY(box.lineEdit());
413
        QVERIFY(box.lineEdit()->text().isEmpty());
414
    }
415
    QCOMPARE(spy0.count(), 0);
416
    QCOMPARE(spy1.count(), 0);
417
    QCOMPARE(spy2.count(), 0);
418
    QCOMPARE(spy3.count(), 0);
419
    QCOMPARE(spy4.count(), (editable && !list.isEmpty())? 1 : 0);
420
}
421
422
void tst_Q3ComboBox::count_data()
423
{
424
    QTest::addColumn<bool>("useListBox");
425
    QTest::addColumn<QStringList>("list");
426
    QTest::newRow("popup-empty") << false << QStringList();
427
    QTest::newRow("popup-3") << false << (QStringList() << "x" << "y" << "z");
428
    QTest::newRow("listbox-empty") << true << QStringList();
429
    QTest::newRow("listbox-3") << true << (QStringList() << "x" << "y" << "z");
430
}
431
432
// public int count() const
433
void tst_Q3ComboBox::count()
434
{
435
    QFETCH(bool, useListBox);
436
    QFETCH(QStringList, list);
437
438
    SubQ3ComboBox box;
439
    box.insertStringList(list);
440
    Q3ListBox *listBox = new Q3ListBox(&box);
441
    if (useListBox) {
442
        box.setListBox(listBox);
443
        QCOMPARE(box.listBox(), listBox);
444
        QCOMPARE(box.count(), 0);
445
        listBox->insertStringList(list);
446
        QCOMPARE(box.count(), list.count());
447
    }
448
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
449
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
450
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
451
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
452
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
453
454
    QCOMPARE(box.count(), list.count());
455
456
    QCOMPARE(spy0.count(), 0);
457
    QCOMPARE(spy1.count(), 0);
458
    QCOMPARE(spy2.count(), 0);
459
    QCOMPARE(spy3.count(), 0);
460
    QCOMPARE(spy4.count(), 0);
461
}
462
463
void tst_Q3ComboBox::currentItem_data()
464
{
465
    QTest::addColumn<QStringList>("list");
466
    QTest::addColumn<int>("setCurrentItem");
467
    QTest::addColumn<int>("currentItem");
468
    QTest::addColumn<bool>("signal");
469
    QStringList list = (QStringList() << "x" << "y" << "z");
470
    QTest::newRow("3 0,0") << list << 0 << 0 << false;
471
    QTest::newRow("3 1,1") << list << 1 << 1 << true;
472
    QTest::newRow("3 2,2") << list << 2 << 2 << true;
473
    QTest::newRow("3 3,0") << list << 3 << 0 << false;
474
    QTest::newRow("- 1,0") << QStringList() << 1 << 0 << false;
475
}
476
477
// public int currentItem() const
478
void tst_Q3ComboBox::currentItem()
479
{
480
    QFETCH(QStringList, list);
481
    QFETCH(int, setCurrentItem);
482
    QFETCH(int, currentItem);
483
    QFETCH(bool, signal);
484
485
    SubQ3ComboBox box;
486
    box.insertStringList(list);
487
488
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
489
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
490
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
491
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
492
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
493
494
    box.setCurrentItem(setCurrentItem);
495
    QCOMPARE(box.currentItem(), currentItem);
496
497
    QCOMPARE(spy0.count(), 0);
498
    QCOMPARE(spy1.count(), 0);
499
    QCOMPARE(spy2.count(), signal ? 1 : 0);
500
    QCOMPARE(spy3.count(), signal ? 1 : 0);
501
    QCOMPARE(spy4.count(), 0);
502
}
503
504
void tst_Q3ComboBox::currentText_data()
505
{
506
    QTest::addColumn<QStringList>("list");
507
    QTest::addColumn<int>("currentItem");
508
    QTest::addColumn<QString>("currentText");
509
    QTest::addColumn<int>("expectedCurrentItem");
510
    QTest::addColumn<bool>("signal");
511
    QTest::newRow("null") << QStringList() << 0 << QString("foo") << 0 << false;
512
    QStringList list = (QStringList() << "x" << "y" << "z");
513
    QTest::newRow("3,0") << list << 0 << QString("x") << 0 << false;
514
    QTest::newRow("3,1") << list << 0 << QString("y") << 1 << true;
515
    QTest::newRow("3,2") << list << 0 << QString("z") << 2 << true;
516
}
517
518
// public QString currentText() const
519
void tst_Q3ComboBox::currentText()
520
{
521
    QFETCH(QStringList, list);
522
    QFETCH(int, currentItem);
523
    QFETCH(QString, currentText);
524
    QFETCH(int, expectedCurrentItem);
525
    QFETCH(bool, signal);
526
527
    SubQ3ComboBox box;
528
    QVERIFY(box.currentText().isEmpty());
529
    box.insertStringList(list);
530
    box.setCurrentItem(currentItem);
531
532
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
533
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
534
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
535
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
536
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
537
538
    box.setCurrentText(currentText);
539
    if (list.isEmpty())
540
        QVERIFY(box.currentText().isEmpty());
541
    else {
542
        QCOMPARE(box.currentText(), currentText);
543
        QCOMPARE(box.currentItem(), expectedCurrentItem);
544
    }
545
    QCOMPARE(spy0.count(), 0);
546
    QCOMPARE(spy1.count(), 0);
547
    QCOMPARE(spy2.count(), signal ? 1 : 0);
548
    QCOMPARE(spy3.count(), signal ? 1 : 0);
549
    QCOMPARE(spy4.count(), 0);
550
}
551
552
Q_DECLARE_METATYPE(Q3ComboBox::Policy)
553
void tst_Q3ComboBox::duplicatesEnabled_data()
554
{
555
    QTest::addColumn<QStringList>("list");
556
    QTest::addColumn<bool>("duplicatesEnabled");
557
    QTest::addColumn<int>("count");
558
    QTest::addColumn<QString>("string");
559
    QTest::addColumn<int>("postCount");
560
    QTest::addColumn<Q3ComboBox::Policy>("policy");
561
    QTest::addColumn<bool>("activatedSignal");
562
    QTest::addColumn<bool>("highlightedSignal");
563
564
    QStringList list = (QStringList() << "x" << "y" << "z");
565
566
    QTest::newRow("null") << QStringList() << false << 0 << QString() << 0 << Q3ComboBox::AtTop << false << false;
567
    QTest::newRow("3, no-dup-3") << list << false << list.count() << QString() << list.count() << Q3ComboBox::AtTop << false << false;
568
    QTest::newRow("3, dup-3") << list << true << list.count() << QString() << list.count() << Q3ComboBox::AtTop << false << false;
569
    QStringList duplist = list;
570
    duplist << list[0];
571
572
    int c = duplist.count();
573
    QTest::newRow("4, no-dup-wdup-4") << duplist << false << c << "x" << c << Q3ComboBox::AtTop << true << true;
574
575
    QTest::newRow("4, dup-wdup-4 noIn") << duplist << true << c << "x" << c << Q3ComboBox::NoInsertion << true << false;
576
    QTest::newRow("4, dup-wdup-4 atTop") << duplist << true << c << "x" << c + 1 << Q3ComboBox::AtTop << true << true;
577
}
578
579
// public bool duplicatesEnabled() const
580
void tst_Q3ComboBox::duplicatesEnabled()
581
{
582
    QFETCH(QStringList, list);
583
    QFETCH(bool, duplicatesEnabled);
584
    QFETCH(int, count);
585
    QFETCH(QString, string);
586
    QFETCH(int, postCount);
587
    QFETCH(Q3ComboBox::Policy, policy);
588
    QFETCH(bool, activatedSignal);
589
    QFETCH(bool, highlightedSignal);
590
591
    SubQ3ComboBox box;
592
    box.setEditable(true);
593
    box.insertStringList(list);
594
    box.setInsertionPolicy(policy);
595
    box.setCurrentItem(1);
596
    QCOMPARE(box.insertionPolicy(), policy);
597
598
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
599
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
600
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
601
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
602
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
603
604
    box.setDuplicatesEnabled(duplicatesEnabled);
605
    QCOMPARE(box.duplicatesEnabled(), duplicatesEnabled);
606
    QCOMPARE(box.count(), count);
607
608
    box.setEditText(string);
609
    QCOMPARE(box.lineEdit()->text(), string);
610
    QTest::keyPress(box.lineEdit(), Qt::Key_Return);
611
    QCOMPARE(box.count(), postCount);
612
    if (postCount != list.count()) {
613
        switch (policy) {
614
        case Q3ComboBox::InsertBeforeCurrent:
615
            QCOMPARE(box.text(0), string);
616
            break;
617
        case Q3ComboBox::InsertAtCurrent:
618
            QCOMPARE(box.text(1), string);
619
            break;
620
        case Q3ComboBox::InsertAfterCurrent:
621
            QCOMPARE(box.text(2), string);
622
            break;
623
        case Q3ComboBox::InsertAtTop:
624
            QCOMPARE(box.text(0), string);
625
            break;
626
        case Q3ComboBox::InsertAtBottom:
627
            QCOMPARE(box.text(box.count() - 1), string);
628
            break;
629
        case Q3ComboBox::NoInsert:
630
            break;
631
        }
632
    }
633
634
    //QCOMPARE(spy0.count(), activatedSignal ? 1 : 0);
635
    //QCOMPARE(spy1.count(), activatedSignal ? 1 : 0);
636
    QCOMPARE(spy2.count(), highlightedSignal ? 1 : 0);
637
    QCOMPARE(spy3.count(), highlightedSignal ? 1 : 0);
638
    QCOMPARE(spy4.count(), !list.isEmpty() ? 1 : 0);
639
}
640
641
void tst_Q3ComboBox::editable_data()
642
{
643
    QTest::addColumn<QStringList>("list");
644
    QTest::addColumn<bool>("editable");
645
    QTest::newRow("empty-noneditable") << QStringList() << false;
646
    QTest::newRow("empty-editable") << QStringList() << true;
647
}
648
649
// public bool editable() const
650
void tst_Q3ComboBox::editable()
651
{
652
    QFETCH(QStringList, list);
653
    QFETCH(bool, editable);
654
655
    SubQ3ComboBox box;
656
    box.insertStringList(list);
657
658
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
659
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
660
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
661
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
662
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
663
664
    box.setEditable(editable);
665
    QCOMPARE(box.editable(), editable);
666
    if (box.editable()) {
667
        QVERIFY(box.lineEdit());
668
        if (!list.empty())
669
            QCOMPARE(box.lineEdit()->text(), list[box.currentItem()]);
670
671
        box.setEditable(false);
672
        QVERIFY(!box.lineEdit());
673
    }
674
675
    QCOMPARE(spy0.count(), 0);
676
    QCOMPARE(spy1.count(), 0);
677
    QCOMPARE(spy2.count(), 0);
678
    QCOMPARE(spy3.count(), 0);
679
    QCOMPARE(spy4.count(), 0);
680
}
681
682
void tst_Q3ComboBox::insertItem_data()
683
{
684
    QTest::addColumn<QStringList>("init");
685
    QTest::addColumn<QString>("string");
686
    QTest::addColumn<int>("index");
687
    QTest::addColumn<QStringList>("expected");
688
    QTest::newRow("null") << QStringList() << QString() << 0 << (QStringList() << QString());
689
    QTest::newRow("front") << (QStringList() << "x") << QString() << 0 << (QStringList() << QString() << "x");
690
    QTest::newRow("back") << (QStringList() << "x") << QString() << 1 << (QStringList() << "x" << QString());
691
}
692
693
// public void insertItem(QPixmap const& pixmap, int index = -1)
694
void tst_Q3ComboBox::insertItem()
695
{
696
    QFETCH(QStringList, init);
697
    QFETCH(QString, string);
698
    QFETCH(int, index);
699
    QFETCH(QStringList, expected);
700
701
    SubQ3ComboBox box;
702
    box.insertStringList(init);
703
704
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
705
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
706
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
707
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
708
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
709
710
    box.insertItem(string, index);
711
    for (int i = 0; i < expected.count(); ++i)
712
        QCOMPARE(box.text(i), expected.at(i));
713
714
    QCOMPARE(spy0.count(), 0);
715
    QCOMPARE(spy1.count(), 0);
716
    QCOMPARE(spy2.count(), 0);
717
    QCOMPARE(spy3.count(), 0);
718
    QCOMPARE(spy4.count(), 0);
719
}
720
721
Q_DECLARE_METATYPE(QStringList)
722
void tst_Q3ComboBox::insertStringList_data()
723
{
724
    QTest::addColumn<QStringList>("init");
725
    QTest::addColumn<QStringList>("stringlist");
726
    QTest::addColumn<int>("index");
727
    QTest::addColumn<QStringList>("expected");
728
    QTest::newRow("null") << QStringList() << QStringList() << 0 << (QStringList() << QString());
729
    QTest::newRow("front") << (QStringList() << "x") << (QStringList() << "y") << 0 << (QStringList() << "y" << "x");
730
    QTest::newRow("back") << (QStringList() << "x") << (QStringList() << "y") << 1 << (QStringList() << "x" << "y");
731
}
732
733
// public void insertStringList(QStringList const& , int index = -1)
734
void tst_Q3ComboBox::insertStringList()
735
{
736
    QFETCH(QStringList, init);
737
    QFETCH(QStringList, stringlist);
738
    QFETCH(int, index);
739
    QFETCH(QStringList, expected);
740
741
    SubQ3ComboBox box;
742
    box.insertStringList(init);
743
744
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
745
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
746
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
747
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
748
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
749
750
    box.insertStringList(stringlist, index);
751
    for (int i = 0; i < expected.count(); ++i)
752
        QCOMPARE(box.text(i), expected.at(i));
753
754
    QCOMPARE(spy0.count(), 0);
755
    QCOMPARE(spy1.count(), 0);
756
    QCOMPARE(spy2.count(), 0);
757
    QCOMPARE(spy3.count(), 0);
758
    QCOMPARE(spy4.count(), 0);
759
}
760
761
void tst_Q3ComboBox::insertStrList_data()
762
{
763
    QTest::addColumn<QStringList>("init");
764
    QTest::addColumn<int>("index");
765
    QTest::addColumn<QStringList>("expected");
766
    QTest::newRow("front") << (QStringList() << "x") << 0 << (QStringList() << "y" << "x");
767
    QTest::newRow("back") << (QStringList() << "x") << 1 << (QStringList() << "x" << "y");
768
}
769
770
// public void insertStrList(Q3StrList const* , int index = -1)
771
void tst_Q3ComboBox::insertStrList()
772
{
773
    QFETCH(QStringList, init);
774
    static const char* strings[] = { "y", 0 };
775
    QFETCH(int, index);
776
    QFETCH(QStringList, expected);
777
778
    SubQ3ComboBox box;
779
    box.insertStringList(init);
780
781
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
782
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
783
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
784
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
785
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
786
787
    box.insertStrList(strings, -1, index);
788
    for (int i = 0; i < expected.count(); ++i)
789
        QCOMPARE(box.text(i), expected.at(i));
790
791
    QCOMPARE(spy0.count(), 0);
792
    QCOMPARE(spy1.count(), 0);
793
    QCOMPARE(spy2.count(), 0);
794
    QCOMPARE(spy3.count(), 0);
795
    QCOMPARE(spy4.count(), 0);
796
}
797
798
void tst_Q3ComboBox::maxCount_data()
799
{
800
    QTest::addColumn<QStringList>("init");
801
    QTest::addColumn<int>("maxCount");
802
    QTest::addColumn<QStringList>("expected");
803
    QStringList empty;
804
    QTest::newRow("remove-all-1") << (QStringList(empty) << "x") << 0 << (QStringList(empty));
805
    QTest::newRow("remove-all-2") << (QStringList(empty) << "x" << "y") << 0 << (QStringList(empty));
806
    QTest::newRow("remove-one") << (QStringList(empty) << "x" << "y") << 1 << (QStringList(empty) << "x");
807
    QTest::newRow("remove-two") << (QStringList(empty) << "x" << "y" << "z") << 1 << (QStringList(empty) << "x");
808
    QTest::newRow("do-nothing-1") << (QStringList(empty) << "x") << 1 << (QStringList(empty) << "x");
809
    QTest::newRow("do-nothing-2") << (QStringList(empty) << "x") << 2 << (QStringList(empty) << "x");
810
    QTest::newRow("do-nothing-3") << (QStringList(empty) << "x" << "y") << 2 << (QStringList(empty) << "x" << "y");
811
}
812
813
// public int maxCount() const
814
void tst_Q3ComboBox::maxCount()
815
{
816
    QFETCH(QStringList, init);
817
    QFETCH(int, maxCount);
818
    QFETCH(QStringList, expected);
819
820
    SubQ3ComboBox box;
821
    box.insertStringList(init);
822
823
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
824
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
825
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
826
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
827
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
828
829
    box.setMaxCount(maxCount);
830
    QCOMPARE(box.maxCount(), maxCount);
831
    //QCOMPARE(box.count(), expected.count());
832
    for (int i = 0; i < expected.count(); ++i)
833
        QCOMPARE(box.text(i), expected.at(i));
834
835
    QCOMPARE(spy0.count(), 0);
836
    QCOMPARE(spy1.count(), 0);
837
    QCOMPARE(spy2.count(), 0);
838
    QCOMPARE(spy3.count(), 0);
839
    QCOMPARE(spy4.count(), 0);
840
841
    //box.setEditable(true);
842
    //box.lineEdit()->setText("foo");
843
    //QTest::keyPress(box.lineEdit(), Qt::Key_Return);
844
    //QVERIFY(box.count() <= maxCount);
845
}
846
847
// public QPixmap const* pixmap(int index) const
848
void tst_Q3ComboBox::pixmap()
849
{
850
    SubQ3ComboBox box;
851
    QCOMPARE(box.pixmap(0), static_cast<const QPixmap*>(0));
852
    QPixmap pixmap(1, 1);
853
    box.insertItem(pixmap, "foo", 0);
854
    QVERIFY(box.pixmap(0) != 0);
855
}
856
857
void tst_Q3ComboBox::removeItem_data()
858
{
859
    QTest::addColumn<QStringList>("init");
860
    QTest::addColumn<int>("removeItem");
861
    QTest::addColumn<QStringList>("expected");
862
    QStringList empty;
863
    QTest::newRow("remove-1") << (QStringList(empty) << "x") << 0 << (QStringList(empty));
864
    QTest::newRow("remove-2") << (QStringList(empty) << "x" << "y") << 0 << (QStringList(empty) << "y");
865
    QTest::newRow("remove-3") << (QStringList(empty) << "x" << "y") << 1 << (QStringList(empty) << "x");
866
    QTest::newRow("remove-4") << (QStringList(empty) << "x" << "y" << "z") << 1 << (QStringList(empty) << "x" << "z");
867
    QTest::newRow("do-nothing") << (QStringList(empty) << "x" << "y") << -2 << (QStringList(empty) << "x" << "y");
868
}
869
870
// public void removeItem(int index)
871
void tst_Q3ComboBox::removeItem()
872
{
873
    QFETCH(QStringList, init);
874
    QFETCH(int, removeItem);
875
    QFETCH(QStringList, expected);
876
877
    SubQ3ComboBox box;
878
    box.insertStringList(init);
879
880
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
881
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
882
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
883
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
884
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
885
886
    box.removeItem(removeItem);
887
    QCOMPARE(box.count(), expected.count());
888
    for (int i = 0; i < expected.count(); ++i)
889
        QCOMPARE(box.text(i), expected.at(i));
890
891
    QCOMPARE(spy0.count(), 0);
892
    QCOMPARE(spy1.count(), 0);
893
    QCOMPARE(spy2.count(), 0);
894
    QCOMPARE(spy3.count(), 0);
895
    QCOMPARE(spy4.count(), 0);
896
}
897
898
// public void setValidator(QValidator const* )
899
void tst_Q3ComboBox::setValidator()
900
{
901
    SubQ3ComboBox box;
902
    const QValidator *v = new QRegExpValidator(&box);
903
    box.setValidator(v);
904
    QCOMPARE(box.validator(), static_cast<const QValidator*>(0));
905
    box.setEditable(true);
906
    box.setValidator(v);
907
    QCOMPARE(box.validator(), v);
908
}
909
910
Q_DECLARE_METATYPE(Qt::Key)
911
void tst_Q3ComboBox::keyPressEvent_data()
912
{
913
    QTest::addColumn<QStringList>("init");
914
    QTest::addColumn<int>("currentItem");
915
    QTest::addColumn<Qt::Key>("key");
916
    QTest::addColumn<int>("expectedCurrent");
917
    QTest::newRow("go-nowhere-1") << (QStringList() << "x") << 0 << Qt::Key_Up << 0;
918
    QTest::newRow("go-nowhere-1") << (QStringList() << "x") << 0 << Qt::Key_Down << 0;
919
920
    QTest::newRow("move-1") << (QStringList() << "x" << "y") << 1 << Qt::Key_Up << 0;
921
    QTest::newRow("move-2") << (QStringList() << "x" << "y") << 0 << Qt::Key_Down << 1;
922
    QTest::newRow("move-3") << (QStringList() << "x" << "y") << 1 << Qt::Key_Home << 0;
923
    QTest::newRow("move-4") << (QStringList() << "x" << "y") << 0 << Qt::Key_End << 1;
924
925
    QTest::newRow("move-let-1") << (QStringList() << "x" << "y") << 0 << Qt::Key_X << 0;
926
    QTest::newRow("move-let-2") << (QStringList() << "x" << "y") << 0 << Qt::Key_Y << 1;
927
    QTest::newRow("move-let-3") << (QStringList() << "x" << "y") << 1 << Qt::Key_X << 0;
928
    QTest::newRow("move-let-4") << (QStringList() << "x" << "y") << 1 << Qt::Key_Y << 1;
929
}
930
931
// protected void keyPressEvent(QKeyEvent* e)
932
void tst_Q3ComboBox::keyPressEvent()
933
{
934
    QFETCH(QStringList, init);
935
    QFETCH(int, currentItem);
936
    QFETCH(Qt::Key, key);
937
    QFETCH(int, expectedCurrent);
938
939
    SubQ3ComboBox box;
940
    box.insertStringList(init);
941
    box.setCurrentItem(currentItem);
942
943
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
944
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
945
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
946
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
947
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
948
949
    QTest::keyPress(&box, key);
950
    QCOMPARE(box.currentItem(), expectedCurrent);
951
952
    // Signal behavior is inconsistent :( so only check that the two signals
953
    // match if it ever emits, sometimes it does, sometimes it doesn't.
954
    QCOMPARE(spy0.count(), spy1.count());
955
956
    int spyCount = (currentItem == expectedCurrent) ? 0 : 1;
957
    QCOMPARE(spy2.count(), spyCount);
958
    QCOMPARE(spy3.count(), spyCount);
959
    QCOMPARE(spy4.count(), 0);
960
}
961
962
void tst_Q3ComboBox::wheelEvent_data()
963
{
964
    QTest::addColumn<QStringList>("init");
965
    QTest::addColumn<int>("currentItem");
966
    QTest::addColumn<int>("delta");
967
    QTest::addColumn<int>("expectedCurrent");
968
    QTest::newRow("go-nowhere-1") << (QStringList() << "x") << 0 << 10 << 0;
969
    QTest::newRow("go-nowhere-1") << (QStringList() << "x") << 0 << -10 << 0;
970
971
    QTest::newRow("move-1") << (QStringList() << "x" << "y") << 1 << 10 << 0;
972
    QTest::newRow("move-2") << (QStringList() << "x" << "y") << 0 << -10 << 1;
973
}
974
975
// protected void wheelEvent(QWheelEvent* e)
976
void tst_Q3ComboBox::wheelEvent()
977
{
978
    QFETCH(QStringList, init);
979
    QFETCH(int, currentItem);
980
    QFETCH(int, delta);
981
    QFETCH(int, expectedCurrent);
982
983
    SubQ3ComboBox box;
984
    box.insertStringList(init);
985
    box.setCurrentItem(currentItem);
986
987
    QSignalSpy spy0(&box, SIGNAL(activated(int)));
988
    QSignalSpy spy1(&box, SIGNAL(activated(QString)));
989
    QSignalSpy spy2(&box, SIGNAL(highlighted(int)));
990
    QSignalSpy spy3(&box, SIGNAL(highlighted(QString)));
991
    QSignalSpy spy4(&box, SIGNAL(textChanged(QString)));
992
993
    QWheelEvent event(QPoint(0,0), delta, Qt::LeftButton, Qt::NoModifier);
994
    box.call_wheelEvent(&event);
995
    QCOMPARE(box.currentItem(), expectedCurrent);
996
997
    int spyCount = (currentItem == expectedCurrent) ? 0 : 1;
998
    QCOMPARE(spy0.count(), spyCount);
999
    QCOMPARE(spy1.count(), spyCount);
1000
    QCOMPARE(spy2.count(), spyCount);
1001
    QCOMPARE(spy3.count(), spyCount);
1002
    QCOMPARE(spy4.count(), 0);
1003
}
1004
1005
void tst_Q3ComboBox::task231724_clear()
1006
{
1007
#ifdef Q_WS_MACX
1008
    return; // On Mac, we don't use a ListBox for the popup
1009
#endif
1010
1011
    Q3ComboBox box;
1012
    
1013
    for ( int i = 0; i <50; i++ ) {
1014
        box.insertItem(QString( "value %1" ).arg( i ));
1015
    }
1016
    
1017
    box.show();
1018
    QTest::qWait(20);
1019
    QTest::mouseClick (&box, Qt::LeftButton);
1020
    QTest::qWait(500);
1021
    Q3ListBox *popup = box.findChild<Q3ListBox *>("in-combo");
1022
    QVERIFY(popup);
1023
    QVERIFY(popup->isVisible());
1024
    QVERIFY(!popup->testAttribute(Qt::WA_OutsideWSRange));
1025
    QTest::qWait(20);
1026
    
1027
    box.clear();
1028
    for ( int i = 0; i <50; i++ ) {
1029
        box.insertItem(QString( "value %1" ).arg( i ));
1030
    }
1031
    
1032
    QTest::qWait(20);
1033
    QTest::mouseClick (&box, Qt::LeftButton);
1034
    QTest::qWait(500);
1035
1036
    popup = box.findChild<Q3ListBox *>("in-combo");
1037
    QVERIFY(popup);
1038
    QVERIFY(popup->isVisible());
1039
    QVERIFY(!popup->testAttribute(Qt::WA_OutsideWSRange));
1040
1041
}
1042
1043
QTEST_MAIN(tst_Q3ComboBox)
1044
#include "tst_q3combobox.moc"