Fixing the autotest for other platforms, hopefully...
[qt-netbsd.git] / tests / auto / qcombobox / tst_qcombobox.cpp
blob1b35939b9dbd7422fccecf47106d1562c73e9755
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #include "qcombobox.h"
45 #include <private/qcombobox_p.h>
47 #include <qfontcombobox.h>
48 #include <qdesktopwidget.h>
49 #include <qapplication.h>
50 #include <qpushbutton.h>
51 #include <qdialog.h>
52 #include <qevent.h>
53 #include <qlineedit.h>
54 #include <qlistview.h>
55 #include <qheaderview.h>
56 #include <qlistwidget.h>
57 #include <qtreewidget.h>
58 #include <qtablewidget.h>
59 #include <qscrollbar.h>
60 #ifdef Q_WS_MAC
61 #include <qmacstyle_mac.h>
62 #elif defined Q_WS_X11
63 #include <private/qt_x11_p.h>
64 #endif
66 #include <qstandarditemmodel.h>
67 #include <qstringlistmodel.h>
68 #include <qcombobox.h>
69 #include <qpushbutton.h>
70 #include <qdialog.h>
71 #include <qstringlist.h>
72 #include <qvalidator.h>
73 #include <qcompleter.h>
74 #ifndef QT_NO_STYLE_CLEANLOOKS
75 #include <qcleanlooksstyle.h>
76 #endif
77 #include <qabstractitemview.h>
78 #include "../../shared/util.h"
79 #include <qstyleditemdelegate.h>
81 //TESTED_CLASS=
82 //TESTED_FILES=
84 class tst_QComboBox : public QObject
86 Q_OBJECT
88 public:
89 tst_QComboBox();
90 ~tst_QComboBox();
92 public slots:
93 void initTestCase();
94 void cleanupTestCase();
95 void init();
96 void cleanup();
98 private slots:
99 void getSetCheck();
100 void ensureReturnIsIgnored();
101 void setEditable();
102 void setPalette();
103 void sizeAdjustPolicy();
104 void clear();
105 void insertPolicy_data();
106 void insertPolicy();
107 void virtualAutocompletion();
108 void autoCompletionCaseSensitivity();
109 void hide();
110 void currentIndex_data();
111 void currentIndex();
112 void insertItems_data();
113 void insertItems();
114 void insertItem_data();
115 void insertItem();
116 void insertOnCurrentIndex();
117 void textpixmapdata_data();
118 void textpixmapdata();
119 void editTextChanged();
120 void setModel();
121 void modelDeleted();
122 void setMaxCount();
123 void setCurrentIndex();
124 void convenienceViews();
125 void findText_data();
126 void findText();
127 void flaggedItems_data();
128 void flaggedItems();
129 void pixmapIcon();
130 void mouseWheel_data();
131 void mouseWheel();
132 void layoutDirection();
133 void itemListPosition();
134 void separatorItem_data();
135 void separatorItem();
136 void task190351_layout();
137 void task191329_size();
138 void task166349_setEditableOnReturn();
139 void task190205_setModelAdjustToContents();
140 void task248169_popupWithMinimalSize();
141 void task247863_keyBoardSelection();
142 void setModelColumn();
143 void noScrollbar_data();
144 void noScrollbar();
145 void setItemDelegate();
146 void task253944_itemDelegateIsReset();
147 void paintingWithOffset();
148 void subControlRectsWithOffset_data();
149 void subControlRectsWithOffset();
151 protected slots:
152 void onEditTextChanged( const QString &newString );
154 private:
155 QComboBox *testWidget;
156 QDialog *parent;
157 QPushButton* ok;
158 int editTextCount;
159 QString editText;
162 class MyAbstractItemDelegate : public QAbstractItemDelegate
164 public:
165 MyAbstractItemDelegate() : QAbstractItemDelegate() {};
166 void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const {}
167 QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const { return QSize(); }
170 class MyAbstractItemModel: public QAbstractItemModel
172 public:
173 MyAbstractItemModel() : QAbstractItemModel() {};
174 QModelIndex index(int, int, const QModelIndex &) const { return QModelIndex(); }
175 QModelIndex parent(const QModelIndex &) const { return QModelIndex(); }
176 int rowCount(const QModelIndex &) const { return 0; }
177 int columnCount(const QModelIndex &) const { return 0; }
178 bool hasChildren(const QModelIndex &) const { return false; }
179 QVariant data(const QModelIndex &, int) const { return QVariant(); }
180 bool setData(const QModelIndex &, const QVariant &, int) { return false; }
181 bool insertRows(int, int, const QModelIndex &) { return false; }
182 bool insertColumns(int, int, const QModelIndex &) { return false; }
183 void setPersistent(const QModelIndex &, const QModelIndex &) {}
184 bool removeRows (int, int, const QModelIndex &) { return false; }
185 bool removeColumns(int, int, const QModelIndex &) { return false; }
186 void reset() {}
189 class MyAbstractItemView : public QAbstractItemView
191 public:
192 MyAbstractItemView() : QAbstractItemView() {}
193 QRect visualRect(const QModelIndex &) const { return QRect(); }
194 void scrollTo(const QModelIndex &, ScrollHint) {}
195 QModelIndex indexAt(const QPoint &) const { return QModelIndex(); }
196 protected:
197 QModelIndex moveCursor(CursorAction, Qt::KeyboardModifiers) { return QModelIndex(); }
198 int horizontalOffset() const { return 0; }
199 int verticalOffset() const { return 0; }
200 bool isIndexHidden(const QModelIndex &) const { return false; }
201 void setSelection(const QRect &, QItemSelectionModel::SelectionFlags) {}
202 QRegion visualRegionForSelection(const QItemSelection &) const { return QRegion(); }
205 // Testing get/set functions
206 void tst_QComboBox::getSetCheck()
208 QComboBox obj1;
209 // int QComboBox::maxVisibleItems()
210 // void QComboBox::setMaxVisibleItems(int)
211 obj1.setMaxVisibleItems(100);
212 QCOMPARE(100, obj1.maxVisibleItems());
213 obj1.setMaxVisibleItems(0);
214 QCOMPARE(obj1.maxVisibleItems(), 0);
215 QTest::ignoreMessage(QtWarningMsg, "QComboBox::setMaxVisibleItems: "
216 "Invalid max visible items (-2147483648) must be >= 0");
217 obj1.setMaxVisibleItems(INT_MIN);
218 QCOMPARE(obj1.maxVisibleItems(), 0); // Cannot be set to something negative => old value
219 obj1.setMaxVisibleItems(INT_MAX);
220 QCOMPARE(INT_MAX, obj1.maxVisibleItems());
222 // int QComboBox::maxCount()
223 // void QComboBox::setMaxCount(int)
224 obj1.setMaxCount(0);
225 QCOMPARE(0, obj1.maxCount());
226 #ifndef QT_DEBUG
227 QTest::ignoreMessage(QtWarningMsg, "QComboBox::setMaxCount: Invalid count (-2147483648) must be >= 0");
228 obj1.setMaxCount(INT_MIN);
229 QCOMPARE(0, obj1.maxCount()); // Setting a value below 0 makes no sense, and shouldn't be allowed
230 #endif
231 obj1.setMaxCount(INT_MAX);
232 QCOMPARE(INT_MAX, obj1.maxCount());
234 // bool QComboBox::autoCompletion()
235 // void QComboBox::setAutoCompletion(bool)
236 obj1.setAutoCompletion(false);
237 QCOMPARE(false, obj1.autoCompletion());
238 obj1.setAutoCompletion(true);
239 QCOMPARE(true, obj1.autoCompletion());
241 // bool QComboBox::duplicatesEnabled()
242 // void QComboBox::setDuplicatesEnabled(bool)
243 obj1.setDuplicatesEnabled(false);
244 QCOMPARE(false, obj1.duplicatesEnabled());
245 obj1.setDuplicatesEnabled(true);
246 QCOMPARE(true, obj1.duplicatesEnabled());
248 // InsertPolicy QComboBox::insertPolicy()
249 // void QComboBox::setInsertPolicy(InsertPolicy)
250 obj1.setInsertPolicy(QComboBox::InsertPolicy(QComboBox::NoInsert));
251 QCOMPARE(QComboBox::InsertPolicy(QComboBox::NoInsert), obj1.insertPolicy());
252 obj1.setInsertPolicy(QComboBox::InsertPolicy(QComboBox::InsertAtTop));
253 QCOMPARE(QComboBox::InsertPolicy(QComboBox::InsertAtTop), obj1.insertPolicy());
254 obj1.setInsertPolicy(QComboBox::InsertPolicy(QComboBox::InsertAtCurrent));
255 QCOMPARE(QComboBox::InsertPolicy(QComboBox::InsertAtCurrent), obj1.insertPolicy());
256 obj1.setInsertPolicy(QComboBox::InsertPolicy(QComboBox::InsertAtBottom));
257 QCOMPARE(QComboBox::InsertPolicy(QComboBox::InsertAtBottom), obj1.insertPolicy());
258 obj1.setInsertPolicy(QComboBox::InsertPolicy(QComboBox::InsertAfterCurrent));
259 QCOMPARE(QComboBox::InsertPolicy(QComboBox::InsertAfterCurrent), obj1.insertPolicy());
260 obj1.setInsertPolicy(QComboBox::InsertPolicy(QComboBox::InsertBeforeCurrent));
261 QCOMPARE(QComboBox::InsertPolicy(QComboBox::InsertBeforeCurrent), obj1.insertPolicy());
262 obj1.setInsertPolicy(QComboBox::InsertPolicy(QComboBox::InsertAlphabetically));
263 QCOMPARE(QComboBox::InsertPolicy(QComboBox::InsertAlphabetically), obj1.insertPolicy());
265 // SizeAdjustPolicy QComboBox::sizeAdjustPolicy()
266 // void QComboBox::setSizeAdjustPolicy(SizeAdjustPolicy)
267 obj1.setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy(QComboBox::AdjustToContents));
268 QCOMPARE(QComboBox::SizeAdjustPolicy(QComboBox::AdjustToContents), obj1.sizeAdjustPolicy());
269 obj1.setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow));
270 QCOMPARE(QComboBox::SizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow), obj1.sizeAdjustPolicy());
271 obj1.setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength));
272 QCOMPARE(QComboBox::SizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength), obj1.sizeAdjustPolicy());
273 obj1.setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon));
274 QCOMPARE(QComboBox::SizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon), obj1.sizeAdjustPolicy());
276 // int QComboBox::minimumContentsLength()
277 // void QComboBox::setMinimumContentsLength(int)
278 obj1.setMinimumContentsLength(0);
279 QCOMPARE(0, obj1.minimumContentsLength());
280 obj1.setMinimumContentsLength(100);
281 QCOMPARE(100, obj1.minimumContentsLength());
282 obj1.setMinimumContentsLength(INT_MIN);
283 QCOMPARE(100, obj1.minimumContentsLength()); // Cannot be set to something negative => old value
284 obj1.setMinimumContentsLength(INT_MAX);
285 QCOMPARE(INT_MAX, obj1.minimumContentsLength());
287 // QLineEdit * QComboBox::lineEdit()
288 // void QComboBox::setLineEdit(QLineEdit *)
289 QLineEdit *var8 = new QLineEdit(0);
290 obj1.setLineEdit(var8);
291 QCOMPARE(var8, obj1.lineEdit());
292 QTest::ignoreMessage(QtWarningMsg, "QComboBox::setLineEdit: cannot set a 0 line edit");
293 obj1.setLineEdit((QLineEdit *)0);
294 QCOMPARE(var8, obj1.lineEdit());
295 // delete var8; // No delete, since QComboBox takes ownership
297 // const QValidator * QComboBox::validator()
298 // void QComboBox::setValidator(const QValidator *)
299 QIntValidator *var9 = new QIntValidator(0);
300 obj1.setValidator(var9);
301 QCOMPARE(obj1.validator(), (const QValidator *)var9);
302 obj1.setValidator((QValidator *)0);
303 QCOMPARE(obj1.validator(), (const QValidator *)0);
304 delete var9;
306 // QAbstractItemDelegate * QComboBox::itemDelegate()
307 // void QComboBox::setItemDelegate(QAbstractItemDelegate *)
308 MyAbstractItemDelegate *var10 = new MyAbstractItemDelegate;
309 obj1.setItemDelegate(var10);
310 QCOMPARE(obj1.itemDelegate(), (QAbstractItemDelegate *)var10);
311 QTest::ignoreMessage(QtWarningMsg, "QComboBox::setItemDelegate: cannot set a 0 delegate");
312 obj1.setItemDelegate((QAbstractItemDelegate *)0);
313 QCOMPARE(obj1.itemDelegate(), (QAbstractItemDelegate *)var10);
314 // delete var10; // No delete, since QComboBox takes ownership
316 // QAbstractItemModel * QComboBox::model()
317 // void QComboBox::setModel(QAbstractItemModel *)
318 MyAbstractItemModel *var11 = new MyAbstractItemModel;
319 obj1.setModel(var11);
320 QCOMPARE(obj1.model(), (QAbstractItemModel *)var11);
321 QTest::ignoreMessage(QtWarningMsg, "QComboBox::setModel: cannot set a 0 model");
322 obj1.setModel((QAbstractItemModel *)0);
323 QCOMPARE(obj1.model(), (QAbstractItemModel *)var11);
324 delete var11;
325 obj1.model();
327 // int QComboBox::modelColumn()
328 // void QComboBox::setModelColumn(int)
329 obj1.setModelColumn(0);
330 QCOMPARE(0, obj1.modelColumn());
331 obj1.setModelColumn(INT_MIN);
332 // QCOMPARE(0, obj1.modelColumn()); // Cannot be set to something negative => column 0
333 obj1.setModelColumn(INT_MAX);
334 QCOMPARE(INT_MAX, obj1.modelColumn());
335 obj1.setModelColumn(0); // back to normal
337 // QAbstractItemView * QComboBox::view()
338 // void QComboBox::setView(QAbstractItemView *)
339 MyAbstractItemView *var13 = new MyAbstractItemView;
340 obj1.setView(var13);
341 QCOMPARE(obj1.view(), (QAbstractItemView *)var13);
342 QTest::ignoreMessage(QtWarningMsg, "QComboBox::setView: cannot set a 0 view");
343 obj1.setView((QAbstractItemView *)0);
344 QCOMPARE(obj1.view(), (QAbstractItemView *)var13);
345 delete var13;
347 // int QComboBox::currentIndex()
348 // void QComboBox::setCurrentIndex(int)
349 obj1.setEditable(false);
350 obj1.setCurrentIndex(0);
351 QCOMPARE(-1, obj1.currentIndex());
352 obj1.setCurrentIndex(INT_MIN);
353 QCOMPARE(-1, obj1.currentIndex());
354 obj1.setCurrentIndex(INT_MAX);
355 QCOMPARE(-1, obj1.currentIndex());
356 obj1.addItems(QStringList() << "1" << "2" << "3" << "4" << "5");
357 obj1.setCurrentIndex(0);
358 QCOMPARE(0, obj1.currentIndex()); // Valid
359 obj1.setCurrentIndex(INT_MIN);
360 QCOMPARE(-1, obj1.currentIndex()); // Invalid => -1
361 obj1.setCurrentIndex(4);
362 QCOMPARE(4, obj1.currentIndex()); // Valid
363 obj1.setCurrentIndex(INT_MAX);
364 QCOMPARE(-1, obj1.currentIndex()); // Invalid => -1
367 typedef QList<QVariant> VariantList;
368 typedef QList<QIcon> IconList;
370 Q_DECLARE_METATYPE(VariantList)
371 Q_DECLARE_METATYPE(IconList)
372 Q_DECLARE_METATYPE(QComboBox::InsertPolicy)
374 tst_QComboBox::tst_QComboBox()
376 qRegisterMetaType<QModelIndex>("QModelIndex");
377 parent = 0;
380 tst_QComboBox::~tst_QComboBox()
385 void tst_QComboBox::initTestCase()
387 // Create the test class
388 parent = new QDialog(0);
389 parent->setObjectName("parent");
390 parent->resize(400, 400);
391 testWidget = new QComboBox(parent);
392 testWidget->setObjectName("testObject");
393 testWidget->setGeometry(0, 0, 100, 100);
394 editTextCount = 0;
395 editText.clear();
396 connect(testWidget, SIGNAL(editTextChanged(const QString&)),
397 this, SLOT(onEditTextChanged(const QString&)));
398 parent->show();
401 void tst_QComboBox::cleanupTestCase()
403 delete parent;
404 parent = 0;
408 void tst_QComboBox::init()
410 // all tests starts with a clean non-editable combobox
411 testWidget->setEditable(false);
412 testWidget->clear();
413 #ifdef Q_OS_WINCE //disable magic for WindowsCE
414 qApp->setAutoMaximizeThreshold(-1);
415 #endif
418 void tst_QComboBox::cleanup()
420 //nothing
424 void tst_QComboBox::setEditable()
426 // make sure we have no lineedit
427 QVERIFY(!testWidget->lineEdit());
428 // test setEditable(true)
429 testWidget->setEditable(true);
430 QVERIFY(testWidget->lineEdit());
431 testWidget->addItem("foo");
432 QCOMPARE(testWidget->lineEdit()->text(), QString("foo"));
433 // test setEditable(false)
435 QLineEdit *lineEdit = testWidget->lineEdit();
436 // line edit is visible when combobox is editable
437 QVERIFY(lineEdit->isVisible());
438 testWidget->setEditable(false);
439 QVERIFY(!testWidget->lineEdit());
440 // line edit should have been explicitly hidden when editable was turned off
441 QVERIFY(!lineEdit->isVisible());
445 void tst_QComboBox::setPalette()
447 #ifdef Q_WS_MAC
448 if (qobject_cast<QMacStyle *>(testWidget->style())) {
449 QSKIP("This test doesn't make sense for pixmap-based styles", SkipAll);
451 #endif
452 QPalette pal = testWidget->palette();
453 pal.setColor(QPalette::Base, Qt::red);
454 testWidget->setPalette(pal);
455 testWidget->setEditable(!testWidget->isEditable());
457 pal.setColor(QPalette::Base, Qt::blue);
458 testWidget->setPalette(pal);
460 const QObjectList comboChildren = testWidget->children();
461 for (int i = 0; i < comboChildren.size(); ++i) {
462 QObject *o = comboChildren.at(i);
463 if (o->isWidgetType()) {
464 QVERIFY(((QWidget*)o)->palette() == pal);
469 void tst_QComboBox::sizeAdjustPolicy()
471 // test that adding new items will not change the sizehint for AdjustToContentsOnFirstShow
472 QVERIFY(!testWidget->count());
473 QVERIFY(testWidget->sizeAdjustPolicy() == QComboBox::AdjustToContentsOnFirstShow);
474 QVERIFY(testWidget->isVisible());
475 QSize firstShow = testWidget->sizeHint();
476 testWidget->addItem("normal item");
477 QCOMPARE(testWidget->sizeHint(), firstShow);
479 // check that with minimumContentsLength/AdjustToMinimumContentsLength sizehint changes
480 testWidget->setMinimumContentsLength(30);
481 QCOMPARE(testWidget->sizeHint(), firstShow);
482 testWidget->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
483 QSize minimumContentsLength = testWidget->sizeHint();
484 QVERIFY(minimumContentsLength.width() > firstShow.width());
485 testWidget->setMinimumContentsLength(60);
486 QVERIFY(minimumContentsLength.width() < testWidget->sizeHint().width());
488 // check that with minimumContentsLength/AdjustToMinimumContentsLengthWithIcon sizehint changes
489 testWidget->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
490 testWidget->setMinimumContentsLength(30);
491 minimumContentsLength = testWidget->sizeHint();
492 QVERIFY(minimumContentsLength.width() > firstShow.width());
493 testWidget->setMinimumContentsLength(60);
494 QVERIFY(minimumContentsLength.width() < testWidget->sizeHint().width());
495 minimumContentsLength = testWidget->sizeHint();
496 testWidget->setIconSize(QSize(128,128));
497 QVERIFY(minimumContentsLength.width() < testWidget->sizeHint().width());
499 // check AdjustToContents changes with content
500 testWidget->setSizeAdjustPolicy(QComboBox::AdjustToContents);
501 QSize content = testWidget->sizeHint();
502 testWidget->addItem("small");
503 QCOMPARE(testWidget->sizeHint(), content);
504 testWidget->addItem("looooooooooooooooooooooong item");
505 // minimumContentsLength() > sizeof("looooooooooooooooooooooong item"), so the sizeHint()
506 // stays the same
507 QCOMPARE(testWidget->sizeHint(), content);
508 // over 60 characters (cf. setMinimumContentsLength() call above)
509 testWidget->addItem("loooooooooooooooooooooooooooooooooooooooooooooo"
510 "ooooooooooooooooooooooooooooooooooooooooooooooo"
511 "ooooooooooooooooooooooooooooong item");
512 QVERIFY(testWidget->sizeHint().width() > content.width());
514 // check AdjustToContents also shrinks when item changes
515 content = testWidget->sizeHint();
516 for (int i=0; i<testWidget->count(); ++i)
517 testWidget->setItemText(i, "XXXXXXXXXX");
518 QVERIFY(testWidget->sizeHint().width() < content.width());
520 // check AdjustToContents shrinks when items are removed
521 content = testWidget->sizeHint();
522 while (testWidget->count())
523 testWidget->removeItem(0);
524 QCOMPARE(testWidget->sizeHint(), content);
525 testWidget->setMinimumContentsLength(0);
526 QVERIFY(testWidget->sizeHint().width() < content.width());
529 void tst_QComboBox::clear()
531 // first non editable combobox
532 testWidget->addItem("foo");
533 testWidget->addItem("bar");
534 QVERIFY(testWidget->count() > 0);
535 QCOMPARE(testWidget->currentIndex(), 0);
537 testWidget->clear();
538 QCOMPARE(testWidget->count(), 0);
539 QCOMPARE(testWidget->currentIndex(), -1);
540 QVERIFY(testWidget->currentText().isEmpty());
542 // then editable combobox
543 testWidget->clear();
544 testWidget->setEditable(true);
545 testWidget->addItem("foo");
546 testWidget->addItem("bar");
547 QVERIFY(testWidget->count() > 0);
548 QCOMPARE(testWidget->currentIndex(), 0);
549 QVERIFY(testWidget->lineEdit());
550 QVERIFY(!testWidget->lineEdit()->text().isEmpty());
551 testWidget->clear();
552 QCOMPARE(testWidget->count(), 0);
553 QCOMPARE(testWidget->currentIndex(), -1);
554 QVERIFY(testWidget->currentText().isEmpty());
555 QVERIFY(testWidget->lineEdit()->text().isEmpty());
558 void tst_QComboBox::insertPolicy_data()
560 QTest::addColumn<QStringList>("initialEntries");
561 QTest::addColumn<QComboBox::InsertPolicy>("insertPolicy");
562 QTest::addColumn<int>("currentIndex");
563 QTest::addColumn<QString>("userInput");
564 QTest::addColumn<QStringList>("result");
566 /* Each insertPolicy should test at least:
567 no initial entries
568 one initial entry
569 five initial entries, current is first item
570 five initial entries, current is third item
571 five initial entries, current is last item
574 /* QComboBox::NoInsert - the string will not be inserted into the combobox.
575 QComboBox::InsertAtTop - insert the string as the first item in the combobox.
576 QComboBox::InsertAtCurrent - replace the previously selected item with the string the user has entered.
577 QComboBox::InsertAtBottom - insert the string as the last item in the combobox.
578 QComboBox::InsertAfterCurrent - insert the string after the previously selected item.
579 QComboBox::InsertBeforeCurrent - insert the string before the previously selected item.
580 QComboBox::InsertAlphabetically - insert the string at the alphabetic position.
582 QStringList initial;
583 QStringList oneEntry("One");
584 QStringList fiveEntries;
585 fiveEntries << "One" << "Two" << "Three" << "Four" << "Five";
586 QString input("insert");
589 QTest::newRow("NoInsert-NoInitial") << initial << QComboBox::NoInsert << 0 << input << initial;
590 QTest::newRow("NoInsert-OneInitial") << oneEntry << QComboBox::NoInsert << 0 << input << oneEntry;
591 QTest::newRow("NoInsert-FiveInitial-FirstCurrent") << fiveEntries << QComboBox::NoInsert << 0 << input << fiveEntries;
592 QTest::newRow("NoInsert-FiveInitial-ThirdCurrent") << fiveEntries << QComboBox::NoInsert << 2 << input << fiveEntries;
593 QTest::newRow("NoInsert-FiveInitial-LastCurrent") << fiveEntries << QComboBox::NoInsert << 4 << input << fiveEntries;
597 QStringList initialAtTop("insert");
598 QStringList oneAtTop;
599 oneAtTop << "insert" << "One";
600 QStringList fiveAtTop;
601 fiveAtTop << "insert" << "One" << "Two" << "Three" << "Four" << "Five";
603 QTest::newRow("AtTop-NoInitial") << initial << QComboBox::InsertAtTop << 0 << input << initialAtTop;
604 QTest::newRow("AtTop-OneInitial") << oneEntry << QComboBox::InsertAtTop << 0 << input << oneAtTop;
605 QTest::newRow("AtTop-FiveInitial-FirstCurrent") << fiveEntries << QComboBox::InsertAtTop << 0 << input << fiveAtTop;
606 QTest::newRow("AtTop-FiveInitial-ThirdCurrent") << fiveEntries << QComboBox::InsertAtTop << 2 << input << fiveAtTop;
607 QTest::newRow("AtTop-FiveInitial-LastCurrent") << fiveEntries << QComboBox::InsertAtTop << 4 << input << fiveAtTop;
611 QStringList initialAtCurrent("insert");
612 QStringList oneAtCurrent("insert");
613 QStringList fiveAtCurrentFirst;
614 fiveAtCurrentFirst << "insert" << "Two" << "Three" << "Four" << "Five";
615 QStringList fiveAtCurrentThird;
616 fiveAtCurrentThird << "One" << "Two" << "insert" << "Four" << "Five";
617 QStringList fiveAtCurrentLast;
618 fiveAtCurrentLast << "One" << "Two" << "Three" << "Four" << "insert";
620 QTest::newRow("AtCurrent-NoInitial") << initial << QComboBox::InsertAtCurrent << 0 << input << initialAtCurrent;
621 QTest::newRow("AtCurrent-OneInitial") << oneEntry << QComboBox::InsertAtCurrent << 0 << input << oneAtCurrent;
622 QTest::newRow("AtCurrent-FiveInitial-FirstCurrent") << fiveEntries << QComboBox::InsertAtCurrent << 0 << input << fiveAtCurrentFirst;
623 QTest::newRow("AtCurrent-FiveInitial-ThirdCurrent") << fiveEntries << QComboBox::InsertAtCurrent << 2 << input << fiveAtCurrentThird;
624 QTest::newRow("AtCurrent-FiveInitial-LastCurrent") << fiveEntries << QComboBox::InsertAtCurrent << 4 << input << fiveAtCurrentLast;
628 QStringList initialAtBottom("insert");
629 QStringList oneAtBottom;
630 oneAtBottom << "One" << "insert";
631 QStringList fiveAtBottom;
632 fiveAtBottom << "One" << "Two" << "Three" << "Four" << "Five" << "insert";
634 QTest::newRow("AtBottom-NoInitial") << initial << QComboBox::InsertAtBottom << 0 << input << initialAtBottom;
635 QTest::newRow("AtBottom-OneInitial") << oneEntry << QComboBox::InsertAtBottom << 0 << input << oneAtBottom;
636 QTest::newRow("AtBottom-FiveInitial-FirstCurrent") << fiveEntries << QComboBox::InsertAtBottom << 0 << input << fiveAtBottom;
637 QTest::newRow("AtBottom-FiveInitial-ThirdCurrent") << fiveEntries << QComboBox::InsertAtBottom << 2 << input << fiveAtBottom;
638 QTest::newRow("AtBottom-FiveInitial-LastCurrent") << fiveEntries << QComboBox::InsertAtBottom << 4 << input << fiveAtBottom;
642 QStringList initialAfterCurrent("insert");
643 QStringList oneAfterCurrent;
644 oneAfterCurrent << "One" << "insert";
645 QStringList fiveAfterCurrentFirst;
646 fiveAfterCurrentFirst << "One" << "insert" << "Two" << "Three" << "Four" << "Five";
647 QStringList fiveAfterCurrentThird;
648 fiveAfterCurrentThird << "One" << "Two" << "Three" << "insert" << "Four" << "Five";
649 QStringList fiveAfterCurrentLast;
650 fiveAfterCurrentLast << "One" << "Two" << "Three" << "Four" << "Five" << "insert";
652 QTest::newRow("AfterCurrent-NoInitial") << initial << QComboBox::InsertAfterCurrent << 0 << input << initialAfterCurrent;
653 QTest::newRow("AfterCurrent-OneInitial") << oneEntry << QComboBox::InsertAfterCurrent << 0 << input << oneAfterCurrent;
654 QTest::newRow("AfterCurrent-FiveInitial-FirstCurrent") << fiveEntries << QComboBox::InsertAfterCurrent << 0 << input << fiveAfterCurrentFirst;
655 QTest::newRow("AfterCurrent-FiveInitial-ThirdCurrent") << fiveEntries << QComboBox::InsertAfterCurrent << 2 << input << fiveAfterCurrentThird;
656 QTest::newRow("AfterCurrent-FiveInitial-LastCurrent") << fiveEntries << QComboBox::InsertAfterCurrent << 4 << input << fiveAfterCurrentLast;
660 QStringList initialBeforeCurrent("insert");
661 QStringList oneBeforeCurrent;
662 oneBeforeCurrent << "insert" << "One";
663 QStringList fiveBeforeCurrentFirst;
664 fiveBeforeCurrentFirst << "insert" << "One" << "Two" << "Three" << "Four" << "Five";
665 QStringList fiveBeforeCurrentThird;
666 fiveBeforeCurrentThird << "One" << "Two" << "insert" << "Three" << "Four" << "Five";
667 QStringList fiveBeforeCurrentLast;
668 fiveBeforeCurrentLast << "One" << "Two" << "Three" << "Four" << "insert" << "Five";
670 QTest::newRow("BeforeCurrent-NoInitial") << initial << QComboBox::InsertBeforeCurrent << 0 << input << initialBeforeCurrent;
671 QTest::newRow("BeforeCurrent-OneInitial") << oneEntry << QComboBox::InsertBeforeCurrent << 0 << input << oneBeforeCurrent;
672 QTest::newRow("BeforeCurrent-FiveInitial-FirstCurrent") << fiveEntries << QComboBox::InsertBeforeCurrent << 0 << input << fiveBeforeCurrentFirst;
673 QTest::newRow("BeforeCurrent-FiveInitial-ThirdCurrent") << fiveEntries << QComboBox::InsertBeforeCurrent << 2 << input << fiveBeforeCurrentThird;
674 QTest::newRow("BeforeCurrent-FiveInitial-LastCurrent") << fiveEntries << QComboBox::InsertBeforeCurrent << 4 << input << fiveBeforeCurrentLast;
678 oneEntry.clear();
679 oneEntry << "foobar";
680 fiveEntries.clear();
681 fiveEntries << "bar" << "foo" << "initial" << "Item" << "stamp";
683 QStringList initialAlphabetically("insert");
684 QStringList oneAlphabetically;
685 oneAlphabetically << "foobar" << "insert";
686 QStringList fiveAlphabetically;
687 fiveAlphabetically << "bar" << "foo" << "initial" << "insert" << "Item" << "stamp";
689 QTest::newRow("Alphabetically-NoInitial") << initial << QComboBox::InsertAlphabetically << 0 << input << initialAlphabetically;
690 QTest::newRow("Alphabetically-OneInitial") << oneEntry << QComboBox::InsertAlphabetically << 0 << input << oneAlphabetically;
691 QTest::newRow("Alphabetically-FiveInitial-FirstCurrent") << fiveEntries << QComboBox::InsertAlphabetically << 0 << input << fiveAlphabetically;
692 QTest::newRow("Alphabetically-FiveInitial-ThirdCurrent") << fiveEntries << QComboBox::InsertAlphabetically << 2 << input << fiveAlphabetically;
693 QTest::newRow("Alphabetically-FiveInitial-LastCurrent") << fiveEntries << QComboBox::InsertAlphabetically << 4 << input << fiveAlphabetically;
697 void tst_QComboBox::insertPolicy()
699 QFETCH(QStringList, initialEntries);
700 QFETCH(QComboBox::InsertPolicy, insertPolicy);
701 QFETCH(int, currentIndex);
702 QFETCH(QString, userInput);
703 QFETCH(QStringList, result);
705 testWidget->clear();
706 testWidget->setInsertPolicy(insertPolicy);
707 testWidget->addItems(initialEntries);
708 testWidget->setEditable(true);
709 if (initialEntries.count() > 0)
710 testWidget->setCurrentIndex(currentIndex);
712 // clear
713 QTest::mouseDClick(testWidget->lineEdit(), Qt::LeftButton);
714 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Delete);
716 QTest::keyClicks(testWidget->lineEdit(), userInput);
717 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Return);
719 // First check that there is the right number of entries, or
720 // we may unwittingly pass
721 QVERIFY((int)result.count() == testWidget->count());
723 // No need to compare if there are no strings to compare
724 if (result.count() > 0) {
725 for (int i=0; i<testWidget->count(); ++i) {
726 QCOMPARE(testWidget->itemText(i), result.at(i));
732 void tst_QComboBox::virtualAutocompletion()
734 testWidget->clear();
735 testWidget->setAutoCompletion(true);
736 testWidget->addItem("Foo");
737 testWidget->addItem("Bar");
738 testWidget->addItem("Boat");
739 testWidget->addItem("Boost");
740 testWidget->clearEditText();
742 // We need to set the keyboard input interval to a higher value
743 // as the processEvent() call takes too much time, so it restarts
744 // the keyboard search then
745 #if defined(QT_ARCH_ARM) || defined(QT_ARCH_MIPS)
746 int oldInterval = QApplication::keyboardInputInterval();
747 QApplication::setKeyboardInputInterval(1500);
748 #endif
750 // NOTE:
751 // Cannot use keyClick for this test, as it simulates keyclicks too well
752 // The virtual keyboards we're trying to catch here, do not perform that
753 // well, and send a keypress & keyrelease right after each other.
754 // This provokes the actual error, as there's no events in between to do
755 // the text completion.
756 QKeyEvent kp1(QEvent::KeyPress, Qt::Key_B, 0, "b");
757 QKeyEvent kr1(QEvent::KeyRelease, Qt::Key_B, 0, "b");
758 QApplication::sendEvent(testWidget, &kp1);
759 QApplication::sendEvent(testWidget, &kr1);
761 qApp->processEvents(); // Process events to trigger autocompletion
762 QTRY_VERIFY(testWidget->currentIndex() == 1);
764 #ifdef QTEST_RUNNING_USING_VALGRIND
765 QSKIP("App running with valgrind are not fast enough", SkipAll);
766 #endif
768 QKeyEvent kp2(QEvent::KeyPress, Qt::Key_O, 0, "o");
769 QKeyEvent kr2(QEvent::KeyRelease, Qt::Key_O, 0, "o");
771 QApplication::sendEvent(testWidget, &kp2);
772 QApplication::sendEvent(testWidget, &kr2);
774 qApp->processEvents(); // Process events to trigger autocompletion
775 QTRY_COMPARE(testWidget->currentIndex(), 2);
777 QApplication::sendEvent(testWidget, &kp2);
778 QApplication::sendEvent(testWidget, &kr2);
779 qApp->processEvents(); // Process events to trigger autocompletion
780 QTRY_COMPARE(testWidget->currentIndex(), 3);
781 #if defined(QT_ARCH_ARM) || defined(QT_ARCH_MIPS)
782 QApplication::setKeyboardInputInterval(oldInterval);
783 #endif
786 void tst_QComboBox::autoCompletionCaseSensitivity()
788 //we have put the focus because the completer
789 //is only used when the widget actually has the focus
790 testWidget->setFocus();
791 qApp->setActiveWindow(testWidget);
792 QTRY_COMPARE(qApp->focusWidget(), (QWidget *)testWidget);
794 testWidget->clear();
795 testWidget->setAutoCompletion(true);
796 testWidget->addItem("Cow");
797 testWidget->addItem("irrelevant1");
798 testWidget->addItem("aww");
799 testWidget->addItem("A*");
800 testWidget->addItem("irrelevant2");
801 testWidget->addItem("aBCDEF");
802 testWidget->addItem("irrelevant3");
803 testWidget->addItem("abcdef");
804 testWidget->addItem("abCdef");
805 testWidget->setEditable(true);
807 // case insensitive
808 testWidget->clearEditText();
809 testWidget->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive);
810 QVERIFY(testWidget->autoCompletionCaseSensitivity() == Qt::CaseInsensitive);
812 QTest::keyClick(testWidget->lineEdit(), Qt::Key_A);
813 qApp->processEvents();
814 QCOMPARE(testWidget->currentText(), QString("aww"));
816 QTest::keyClick(testWidget->lineEdit(), Qt::Key_B);
817 qApp->processEvents();
818 // autocompletions preserve userkey-case from 4.2
819 QCOMPARE(testWidget->currentText(), QString("abCDEF"));
821 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
822 qApp->processEvents();
823 QCOMPARE(testWidget->currentText(), QString("aBCDEF")); // case restored to item's case
825 testWidget->clearEditText();
826 QTest::keyClick(testWidget->lineEdit(), 'c');
827 QCOMPARE(testWidget->currentText(), QString("cow"));
828 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
829 QCOMPARE(testWidget->currentText(), QString("Cow")); // case restored to item's case
831 testWidget->clearEditText();
832 QTest::keyClick(testWidget->lineEdit(), 'a');
833 QTest::keyClick(testWidget->lineEdit(), '*');
834 QCOMPARE(testWidget->currentText(), QString("a*"));
835 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
836 QCOMPARE(testWidget->currentText(), QString("A*"));
838 // case sensitive
839 testWidget->clearEditText();
840 testWidget->setAutoCompletionCaseSensitivity(Qt::CaseSensitive);
841 QVERIFY(testWidget->autoCompletionCaseSensitivity() == Qt::CaseSensitive);
842 QTest::keyClick(testWidget->lineEdit(), Qt::Key_A);
843 qApp->processEvents();
844 QCOMPARE(testWidget->currentText(), QString("aww"));
845 QTest::keyClick(testWidget->lineEdit(), Qt::Key_B);
846 qApp->processEvents();
847 QCOMPARE(testWidget->currentText(), QString("abcdef"));
849 testWidget->setCurrentIndex(0); // to reset the completion's "start"
850 testWidget->clearEditText();
851 QTest::keyClick(testWidget->lineEdit(), 'a');
852 QTest::keyClick(testWidget->lineEdit(), 'b');
853 QCOMPARE(testWidget->currentText(), QString("abcdef"));
854 QTest::keyClick(testWidget->lineEdit(), 'C');
855 qApp->processEvents();
856 QCOMPARE(testWidget->currentText(), QString("abCdef"));
857 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
858 qApp->processEvents();
859 QCOMPARE(testWidget->currentText(), QString("abCdef")); // case restored to item's case
861 testWidget->clearEditText();
862 QTest::keyClick(testWidget->lineEdit(), 'c');
863 QCOMPARE(testWidget->currentText(), QString("c"));
864 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Backspace);
865 QTest::keyClick(testWidget->lineEdit(), 'C');
866 QCOMPARE(testWidget->currentText(), QString("Cow"));
867 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
868 QCOMPARE(testWidget->currentText(), QString("Cow"));
870 testWidget->clearEditText();
871 QTest::keyClick(testWidget->lineEdit(), 'a');
872 QTest::keyClick(testWidget->lineEdit(), '*');
873 QCOMPARE(testWidget->currentText(), QString("a*"));
874 QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
875 QCOMPARE(testWidget->currentText(), QString("a*")); // A* not matched
878 void tst_QComboBox::hide()
880 testWidget->addItem("foo");
881 testWidget->showPopup();
882 QTest::qWait(500); //allow combobox effect to complete
883 QVERIFY(testWidget->view());
884 QVERIFY(testWidget->view()->isVisible());
885 testWidget->hidePopup();
886 QTest::qWait(500); //allow combobox effect to complete
887 QVERIFY(!testWidget->view()->isVisible());
888 testWidget->hide();
889 QVERIFY(!testWidget->isVisible());
894 void tst_QComboBox::currentIndex_data()
896 QTest::addColumn<QStringList>("initialItems");
897 QTest::addColumn<int>("setCurrentIndex");
898 QTest::addColumn<int>("removeIndex");
899 QTest::addColumn<int>("insertIndex");
900 QTest::addColumn<QString>("insertText");
901 QTest::addColumn<int>("expectedCurrentIndex");
902 QTest::addColumn<QString>("expectedCurrentText");
903 QTest::addColumn<int>("expectedSignalCount");
905 QStringList initialItems;
906 int setCurrentIndex;
907 int removeIndex;
908 int insertIndex;
909 QString insertText;
910 int expectedCurrentIndex;
911 QString expectedCurrentText;
912 int expectedSignalCount;
915 initialItems.clear();
916 initialItems << "foo" << "bar";
917 setCurrentIndex = -2;
918 removeIndex = -1;
919 insertIndex = -1;
920 insertText = "";
921 expectedCurrentIndex = 0;
922 expectedCurrentText = "foo";
923 expectedSignalCount = 1;
924 QTest::newRow("first added item is set to current if there is no current")
925 << initialItems << setCurrentIndex << removeIndex
926 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
927 << expectedSignalCount;
930 initialItems.clear();
931 initialItems << "foo" << "bar";
932 setCurrentIndex = 1;
933 removeIndex = -1;
934 insertIndex = -1;
935 insertText = "";
936 expectedCurrentIndex = 1;
937 expectedCurrentText = "bar";
938 expectedSignalCount = 2;
939 QTest::newRow("check that setting the index works")
940 << initialItems << setCurrentIndex << removeIndex
941 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
942 << expectedSignalCount;
946 initialItems.clear();
947 initialItems << "foo" << "bar";
948 setCurrentIndex = -1; // will invalidate the currentIndex
949 removeIndex = -1;
950 insertIndex = -1;
951 insertText = "";
952 expectedCurrentIndex = -1;
953 expectedCurrentText = "";
954 expectedSignalCount = 2;
955 QTest::newRow("check that isetting the index to -1 works")
956 << initialItems << setCurrentIndex << removeIndex
957 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
958 << expectedSignalCount;
962 initialItems.clear();
963 initialItems << "foo";
964 setCurrentIndex = 0;
965 removeIndex = 0;
966 insertIndex = -1;
967 insertText = "";
968 expectedCurrentIndex = -1;
969 expectedCurrentText = "";
970 expectedSignalCount = 2;
971 QTest::newRow("check that current index is invalid when removing the only item")
972 << initialItems << setCurrentIndex << removeIndex
973 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
974 << expectedSignalCount;
977 initialItems.clear();
978 initialItems << "foo" << "bar";
979 setCurrentIndex = 1;
980 removeIndex = 0;
981 insertIndex = -1;
982 insertText = "";
983 expectedCurrentIndex = 0;
984 expectedCurrentText = "bar";
985 expectedSignalCount = 3;
986 QTest::newRow("check that the current index follows the item when removing an item above")
987 << initialItems << setCurrentIndex << removeIndex
988 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
989 << expectedSignalCount;
993 initialItems.clear();
994 initialItems << "foo" << "bar" << "baz";
995 setCurrentIndex = 1;
996 removeIndex = 1;
997 insertIndex = -1;
998 insertText = "";
999 expectedCurrentIndex = 1;
1000 expectedCurrentText = "baz";
1001 expectedSignalCount = 3;
1002 QTest::newRow("check that the current index uses the next item if current is removed")
1003 << initialItems << setCurrentIndex << removeIndex
1004 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
1005 << expectedSignalCount;
1008 initialItems.clear();
1009 initialItems << "foo" << "bar" << "baz";
1010 setCurrentIndex = 2;
1011 removeIndex = 2;
1012 insertIndex = -1;
1013 insertText = "";
1014 expectedCurrentIndex = 1;
1015 expectedCurrentText = "bar";
1016 expectedSignalCount = 3;
1017 QTest::newRow("check that the current index is moved to the one before if current is removed")
1018 << initialItems << setCurrentIndex << removeIndex
1019 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
1020 << expectedSignalCount;
1023 initialItems.clear();
1024 initialItems << "foo" << "bar" << "baz";
1025 setCurrentIndex = 1;
1026 removeIndex = 2;
1027 insertIndex = -1;
1028 insertText = "";
1029 expectedCurrentIndex = 1;
1030 expectedCurrentText = "bar";
1031 expectedSignalCount = 2;
1032 QTest::newRow("check that the current index is unchanged if you remove an item after")
1033 << initialItems << setCurrentIndex << removeIndex
1034 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
1035 << expectedSignalCount;
1038 initialItems.clear();
1039 initialItems << "foo" << "bar";
1040 setCurrentIndex = 1;
1041 removeIndex = -1;
1042 insertIndex = 0;
1043 insertText = "baz";
1044 expectedCurrentIndex = 2;
1045 expectedCurrentText = "bar";
1046 expectedSignalCount = 3;
1047 QTest::newRow("check that the current index follows the item if you insert before current")
1048 << initialItems << setCurrentIndex << removeIndex
1049 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
1050 << expectedSignalCount;
1053 initialItems.clear();
1054 initialItems << "foo";
1055 setCurrentIndex = 0;
1056 removeIndex = -1;
1057 insertIndex = 0;
1058 insertText = "bar";
1059 expectedCurrentIndex = 1;
1060 expectedCurrentText = "foo";
1061 expectedSignalCount = 2;
1062 QTest::newRow("check that the current index follows the item if you insert on the current")
1063 << initialItems << setCurrentIndex << removeIndex
1064 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
1065 << expectedSignalCount;
1068 initialItems.clear();
1069 initialItems << "foo";
1070 setCurrentIndex = 0;
1071 removeIndex = -1;
1072 insertIndex = 1;
1073 insertText = "bar";
1074 expectedCurrentIndex = 0;
1075 expectedCurrentText = "foo";
1076 expectedSignalCount = 1;
1077 QTest::newRow("check that the current index stays the same if you insert after the current")
1078 << initialItems << setCurrentIndex << removeIndex
1079 << insertIndex << insertText << expectedCurrentIndex << expectedCurrentText
1080 << expectedSignalCount;
1084 void tst_QComboBox::currentIndex()
1086 QFETCH(QStringList, initialItems);
1087 QFETCH(int, setCurrentIndex);
1088 QFETCH(int, removeIndex);
1089 QFETCH(int, insertIndex);
1090 QFETCH(QString, insertText);
1091 QFETCH(int, expectedCurrentIndex);
1092 QFETCH(QString, expectedCurrentText);
1093 QFETCH(int, expectedSignalCount);
1095 // test both editable/non-editable combobox
1096 for (int edit = 0; edit < 2; ++edit) {
1097 testWidget->clear();
1098 testWidget->setEditable(edit ? true : false);
1099 if (edit)
1100 QVERIFY(testWidget->lineEdit());
1102 // verify it is empty, has no current index and no current text
1103 QCOMPARE(testWidget->count(), 0);
1104 QCOMPARE(testWidget->currentIndex(), -1);
1105 QVERIFY(testWidget->currentText().isEmpty());
1107 // spy on currentIndexChanged
1108 QSignalSpy indexChangedInt(testWidget, SIGNAL(currentIndexChanged(int)));
1109 QSignalSpy indexChangedString(testWidget, SIGNAL(currentIndexChanged(const QString&)));
1111 // stuff items into it
1112 foreach(QString text, initialItems) {
1113 testWidget->addItem(text);
1115 QCOMPARE(testWidget->count(), initialItems.count());
1117 // set current index, remove and/or insert
1118 if (setCurrentIndex >= -1) {
1119 testWidget->setCurrentIndex(setCurrentIndex);
1120 QCOMPARE(testWidget->currentIndex(), setCurrentIndex);
1123 if (removeIndex >= 0)
1124 testWidget->removeItem(removeIndex);
1125 if (insertIndex >= 0)
1126 testWidget->insertItem(insertIndex, insertText);
1128 // compare with expected index and text
1129 QCOMPARE(testWidget->currentIndex(), expectedCurrentIndex);
1130 QCOMPARE(testWidget->currentText(), expectedCurrentText);
1132 // check that signal count is correct
1133 QCOMPARE(indexChangedInt.count(), expectedSignalCount);
1134 QCOMPARE(indexChangedString.count(), expectedSignalCount);
1136 // compare with last sent signal values
1137 if (indexChangedInt.count())
1138 QCOMPARE(indexChangedInt.at(indexChangedInt.count() - 1).at(0).toInt(),
1139 testWidget->currentIndex());
1140 if (indexChangedString.count())
1141 QCOMPARE(indexChangedString.at(indexChangedString.count() - 1).at(0).toString(),
1142 testWidget->currentText());
1144 if (edit) {
1145 testWidget->setCurrentIndex(-1);
1146 testWidget->setInsertPolicy(QComboBox::InsertAtBottom);
1147 QTest::keyPress(testWidget, 'a');
1148 QTest::keyPress(testWidget, 'b');
1149 QCOMPARE(testWidget->currentText(), QString("ab"));
1150 QCOMPARE(testWidget->currentIndex(), -1);
1151 int numItems = testWidget->count();
1152 QTest::keyPress(testWidget, Qt::Key_Return);
1153 QCOMPARE(testWidget->count(), numItems + 1);
1154 QCOMPARE(testWidget->currentIndex(), numItems);
1155 testWidget->setCurrentIndex(-1);
1156 QTest::keyPress(testWidget, 'a');
1157 QTest::keyPress(testWidget, 'b');
1158 QCOMPARE(testWidget->currentIndex(), -1);
1163 void tst_QComboBox::insertItems_data()
1165 QTest::addColumn<QStringList>("initialItems");
1166 QTest::addColumn<QStringList>("insertedItems");
1167 QTest::addColumn<int>("insertIndex");
1168 QTest::addColumn<int>("expectedIndex");
1170 QStringList initialItems;
1171 QStringList insertedItems;
1173 initialItems << "foo" << "bar";
1174 insertedItems << "mongo";
1176 QTest::newRow("prepend") << initialItems << insertedItems << 0 << 0;
1177 QTest::newRow("prepend with negative value") << initialItems << insertedItems << -1 << 0;
1178 QTest::newRow("append") << initialItems << insertedItems << initialItems.count() << initialItems.count();
1179 QTest::newRow("append with too high value") << initialItems << insertedItems << 999 << initialItems.count();
1180 QTest::newRow("insert") << initialItems << insertedItems << 1 << 1;
1183 void tst_QComboBox::insertItems()
1185 QFETCH(QStringList, initialItems);
1186 QFETCH(QStringList, insertedItems);
1187 QFETCH(int, insertIndex);
1188 QFETCH(int, expectedIndex);
1190 testWidget->insertItems(0, initialItems);
1191 QCOMPARE(testWidget->count(), initialItems.count());
1193 testWidget->insertItems(insertIndex, insertedItems);
1195 QCOMPARE(testWidget->count(), initialItems.count() + insertedItems.count());
1196 for (int i=0; i<insertedItems.count(); ++i)
1197 QCOMPARE(testWidget->itemText(expectedIndex + i), insertedItems.at(i));
1200 void tst_QComboBox::insertItem_data()
1202 QTest::addColumn<QStringList>("initialItems");
1203 QTest::addColumn<int>("insertIndex");
1204 QTest::addColumn<QString>("itemLabel");
1205 QTest::addColumn<int>("expectedIndex");
1206 QTest::addColumn<bool>("testQt3Support");
1207 QTest::addColumn<bool>("editable");
1209 QStringList initialItems;
1210 initialItems << "foo" << "bar";
1211 for(int e = 0 ; e<2 ; e++) {
1212 bool editable = (e==0);
1213 QTest::newRow("Insert less then 0") << initialItems << -1 << "inserted" << 0 << false << editable;
1214 QTest::newRow("Insert at 0") << initialItems << 0 << "inserted" << 0 << false << editable;
1215 QTest::newRow("Insert beyond count") << initialItems << 3 << "inserted" << 2 << false << editable;
1216 QTest::newRow("Insert at count") << initialItems << 2 << "inserted" << 2 << false << editable;
1217 QTest::newRow("Insert in the middle") << initialItems << 1 << "inserted" << 1 << false << editable;
1218 #if defined(QT3_SUPPORT)
1219 QTest::newRow("Qt3Support: Insert less then 0") << initialItems << -1 << "inserted" << 2 << true << editable;
1220 #endif
1224 void tst_QComboBox::insertItem()
1226 QFETCH(QStringList, initialItems);
1227 QFETCH(int, insertIndex);
1228 QFETCH(QString, itemLabel);
1229 QFETCH(int, expectedIndex);
1230 QFETCH(bool, testQt3Support);
1231 QFETCH(bool, editable);
1233 testWidget->insertItems(0, initialItems);
1234 QCOMPARE(testWidget->count(), initialItems.count());
1236 testWidget->setEditable(true);
1237 if (editable)
1238 testWidget->setEditText("FOO");
1240 #if defined (QT3_SUPPORT)
1241 if (testQt3Support)
1242 testWidget->insertItem(itemLabel, insertIndex);
1243 else
1244 testWidget->insertItem(insertIndex, itemLabel);
1245 #else
1246 Q_UNUSED(testQt3Support);
1247 testWidget->insertItem(insertIndex, itemLabel);
1248 #endif
1251 QCOMPARE(testWidget->count(), initialItems.count() + 1);
1252 QCOMPARE(testWidget->itemText(expectedIndex), itemLabel);
1254 if (editable)
1255 QCOMPARE(testWidget->currentText(), QString("FOO"));
1258 void tst_QComboBox::insertOnCurrentIndex()
1260 testWidget->setEditable(true);
1261 testWidget->addItem("first item");
1262 testWidget->setCurrentIndex(0);
1263 testWidget->insertItem(0, "second item");
1264 QCOMPARE(testWidget->lineEdit()->text(), QString::fromAscii("first item"));
1267 void tst_QComboBox::textpixmapdata_data()
1269 QTest::addColumn<QStringList>("text");
1270 QTest::addColumn<IconList>("icons");
1271 QTest::addColumn<VariantList>("variant");
1273 QStringList text;
1274 IconList icon;
1275 VariantList variant;
1278 text.clear(); icon.clear(); variant.clear();
1279 text << "foo" << "bar";
1280 icon << QIcon() << QIcon();
1281 variant << QVariant() << QVariant();
1282 QTest::newRow("just text") << text << icon << variant;
1285 text.clear(); icon.clear(); variant.clear();
1286 text << QString() << QString();
1287 icon << QIcon(QPixmap("qtlogo.png")) << QIcon(QPixmap("qtlogoinverted.png"));
1288 variant << QVariant() << QVariant();
1289 QTest::newRow("just icons") << text << icon << variant;
1292 text.clear(); icon.clear(); variant.clear();
1293 text << QString() << QString();
1294 icon << QIcon() << QIcon();
1295 variant << 12 << "bingo";
1296 QTest::newRow("just user data") << text << icon << variant;
1299 text.clear(); icon.clear(); variant.clear();
1300 text << "foo" << "bar";
1301 icon << QIcon(QPixmap("qtlogo.png")) << QIcon(QPixmap("qtlogoinverted.png"));
1302 variant << 12 << "bingo";
1303 QTest::newRow("text, icons and user data") << text << icon << variant;
1307 void tst_QComboBox::textpixmapdata()
1309 QFETCH(QStringList, text);
1310 QFETCH(IconList, icons);
1311 QFETCH(VariantList, variant);
1313 QVERIFY(text.count() == icons.count() && text.count() == variant.count());
1315 for (int i = 0; i<text.count(); ++i) {
1316 testWidget->insertItem(i, text.at(i));
1317 testWidget->setItemIcon(i, icons.at(i));
1318 testWidget->setItemData(i, variant.at(i), Qt::UserRole);
1321 QCOMPARE(testWidget->count(), text.count());
1323 for (int i = 0; i<text.count(); ++i) {
1324 QIcon icon = testWidget->itemIcon(i);
1325 QVERIFY(icon.serialNumber() == icons.at(i).serialNumber());
1326 QPixmap original = icons.at(i).pixmap(1024);
1327 QPixmap pixmap = icon.pixmap(1024);
1328 QVERIFY(pixmap.toImage() == original.toImage());
1331 for (int i = 0; i<text.count(); ++i) {
1332 QCOMPARE(testWidget->itemText(i), text.at(i));
1333 // ### we should test icons/pixmap as well, but I need to fix the api mismatch first
1334 QCOMPARE(testWidget->itemData(i, Qt::UserRole), variant.at(i));
1338 void tst_QComboBox::setCurrentIndex()
1340 QCOMPARE(testWidget->count(), 0);
1341 testWidget->addItem("foo");
1342 testWidget->addItem("bar");
1343 QCOMPARE(testWidget->count(), 2);
1345 QCOMPARE(testWidget->currentIndex(), 0);
1346 testWidget->setCurrentIndex(0);
1347 QCOMPARE(testWidget->currentText(), QString("foo"));
1349 testWidget->setCurrentIndex(1);
1350 QCOMPARE(testWidget->currentText(), QString("bar"));
1352 testWidget->setCurrentIndex(0);
1353 QCOMPARE(testWidget->currentText(), QString("foo"));
1356 void tst_QComboBox::editTextChanged()
1358 QCOMPARE(testWidget->count(), 0);
1359 testWidget->addItem("foo");
1360 testWidget->addItem("bar");
1361 QCOMPARE(testWidget->count(), 2);
1363 // first we test non editable
1364 testWidget->setEditable(false);
1365 QCOMPARE(testWidget->isEditable(), false);
1367 // no signal should be sent when current is set to the same
1368 QCOMPARE(testWidget->currentIndex(), 0);
1369 editTextCount = 0;
1370 editText.clear();
1371 testWidget->setCurrentIndex(0);
1372 QCOMPARE(testWidget->currentIndex(), 0);
1373 QCOMPARE(editTextCount, 0);
1374 QCOMPARE(editText.isEmpty(), true);
1376 // no signal should be sent when changing to other index because we are not editable
1377 QCOMPARE(testWidget->currentIndex(), 0);
1378 editTextCount = 0;
1379 editText.clear();
1380 testWidget->setCurrentIndex(1);
1381 QCOMPARE(testWidget->currentIndex(), 1);
1382 QCOMPARE(editTextCount, 0);
1383 QCOMPARE(editText.isEmpty(), true);
1385 // now set to editable and reset current index
1386 testWidget->setEditable(true);
1387 QCOMPARE(testWidget->isEditable(), true);
1388 testWidget->setCurrentIndex(0);
1390 // no signal should be sent when current is set to the same
1391 QCOMPARE(testWidget->currentIndex(), 0);
1392 editTextCount = 0;
1393 editText.clear();
1394 testWidget->setCurrentIndex(0);
1395 QCOMPARE(testWidget->currentIndex(), 0);
1396 QCOMPARE(editTextCount, 0);
1397 QCOMPARE(editText.isEmpty(), true);
1399 // signal should be sent when changing to other index
1400 QCOMPARE(testWidget->currentIndex(), 0);
1401 editTextCount = 0;
1402 editText.clear();
1403 testWidget->setCurrentIndex(1);
1404 QCOMPARE(testWidget->currentIndex(), 1);
1405 QCOMPARE(editTextCount, 1);
1406 QCOMPARE(editText, QString("bar"));
1408 // insert some keys and notice they are all signaled
1409 editTextCount = 0;
1410 editText.clear();
1411 QTest::keyClicks(testWidget, "bingo");
1412 QCOMPARE(editTextCount, 5);
1413 QCOMPARE(editText, QString("barbingo"));
1416 void tst_QComboBox::onEditTextChanged(const QString &text)
1418 editTextCount++;
1419 editText = text;
1422 void tst_QComboBox::setModel()
1424 QComboBox box;
1425 QCOMPARE(box.currentIndex(), -1);
1426 box.addItems((QStringList() << "foo" << "bar"));
1427 QCOMPARE(box.currentIndex(), 0);
1428 box.setCurrentIndex(1);
1429 QCOMPARE(box.currentIndex(), 1);
1431 // check that currentIndex is set to invalid
1432 QAbstractItemModel *oldModel = box.model();
1433 box.setModel(new QStandardItemModel(&box));
1434 QCOMPARE(box.currentIndex(), -1);
1435 QVERIFY(box.model() != oldModel);
1437 // check that currentIndex is set to first item
1438 oldModel = box.model();
1439 box.setModel(new QStandardItemModel(2,1, &box));
1440 QCOMPARE(box.currentIndex(), 0);
1441 QVERIFY(box.model() != oldModel);
1444 void tst_QComboBox::modelDeleted()
1446 QComboBox box;
1447 QStandardItemModel *model = new QStandardItemModel;
1448 box.setModel(model);
1449 QCOMPARE(box.model(), static_cast<QAbstractItemModel *>(model));
1450 delete model;
1451 QVERIFY(box.model());
1452 QCOMPARE(box.findText("bubu"), -1);
1454 delete box.model();
1455 QVERIFY(box.model());
1456 delete box.model();
1457 QVERIFY(box.model());
1460 void tst_QComboBox::setMaxCount()
1462 QStringList items;
1463 items << "1" << "2" << "3" << "4" << "5";
1465 QComboBox box;
1466 box.addItems(items);
1467 QCOMPARE(box.count(), 5);
1469 box.setMaxCount(4);
1470 QCOMPARE(box.count(), 4);
1471 QCOMPARE(box.itemText(0), QString("1"));
1472 QCOMPARE(box.itemText(1), QString("2"));
1473 QCOMPARE(box.itemText(2), QString("3"));
1474 QCOMPARE(box.itemText(3), QString("4"));
1476 // appending should do nothing
1477 box.addItem("foo");
1478 QCOMPARE(box.count(), 4);
1479 QCOMPARE(box.findText("foo"), -1);
1481 // inserting one item at top should remove the last
1482 box.insertItem(0, "0");
1483 QCOMPARE(box.count(), 4);
1484 QCOMPARE(box.itemText(0), QString("0"));
1485 QCOMPARE(box.itemText(1), QString("1"));
1486 QCOMPARE(box.itemText(2), QString("2"));
1487 QCOMPARE(box.itemText(3), QString("3"));
1489 // insert 5 items in a box with maxCount 4
1490 box.insertItems(0, items);
1491 QCOMPARE(box.count(), 4);
1492 QCOMPARE(box.itemText(0), QString("1"));
1493 QCOMPARE(box.itemText(1), QString("2"));
1494 QCOMPARE(box.itemText(2), QString("3"));
1495 QCOMPARE(box.itemText(3), QString("4"));
1497 // insert 5 items at pos 2. Make sure only two get inserted
1498 QSignalSpy spy(box.model(), SIGNAL(rowsInserted(QModelIndex,int,int)));
1499 box.insertItems(2, items);
1500 QCOMPARE(spy.count(), 1);
1501 QCOMPARE(spy.at(0).at(1).toInt(), 2);
1502 QCOMPARE(spy.at(0).at(2).toInt(), 3);
1504 QCOMPARE(box.count(), 4);
1505 QCOMPARE(box.itemText(0), QString("1"));
1506 QCOMPARE(box.itemText(1), QString("2"));
1507 QCOMPARE(box.itemText(2), QString("1"));
1508 QCOMPARE(box.itemText(3), QString("2"));
1510 box.insertItems(0, QStringList());
1511 QCOMPARE(box.count(), 4);
1513 box.setMaxCount(0);
1514 QCOMPARE(box.count(), 0);
1515 box.addItem("foo");
1516 QCOMPARE(box.count(), 0);
1517 box.addItems(items);
1518 QCOMPARE(box.count(), 0);
1521 void tst_QComboBox::convenienceViews()
1523 // QListWidget
1524 QComboBox listCombo;
1525 QListWidget *list = new QListWidget();
1526 listCombo.setModel(list->model());
1527 listCombo.setView(list);
1528 // add items
1529 list->addItem("list0");
1530 listCombo.addItem("list1");
1531 QCOMPARE(listCombo.count(), 2);
1532 QCOMPARE(listCombo.itemText(0), QString("list0"));
1533 QCOMPARE(listCombo.itemText(1), QString("list1"));
1535 // QTreeWidget
1536 QComboBox treeCombo;
1537 QTreeWidget *tree = new QTreeWidget();
1538 tree->setColumnCount(1);
1539 tree->header()->hide();
1540 treeCombo.setModel(tree->model());
1541 treeCombo.setView(tree);
1542 // add items
1543 tree->addTopLevelItem(new QTreeWidgetItem(QStringList("tree0")));
1544 treeCombo.addItem("tree1");
1545 QCOMPARE(treeCombo.count(), 2);
1546 QCOMPARE(treeCombo.itemText(0), QString("tree0"));
1547 QCOMPARE(treeCombo.itemText(1), QString("tree1"));
1549 // QTableWidget
1550 QComboBox tableCombo;
1551 QTableWidget *table = new QTableWidget(0,1);
1552 table->verticalHeader()->hide();
1553 table->horizontalHeader()->hide();
1554 tableCombo.setModel(table->model());
1555 tableCombo.setView(table);
1556 // add items
1557 table->setRowCount(table->rowCount() + 1);
1558 table->setItem(0, table->rowCount() - 1, new QTableWidgetItem("table0"));
1559 tableCombo.addItem("table1");
1560 QCOMPARE(tableCombo.count(), 2);
1561 QCOMPARE(tableCombo.itemText(0), QString("table0"));
1562 QCOMPARE(tableCombo.itemText(1), QString("table1"));
1565 class ReturnClass : public QWidget
1567 Q_OBJECT
1568 public:
1569 ReturnClass(QWidget *parent = 0)
1570 : QWidget(parent), received(false)
1572 QComboBox *box = new QComboBox(this);
1573 box->setEditable(true);
1574 edit = box->lineEdit();
1575 box->setGeometry(rect());
1578 void keyPressEvent(QKeyEvent *e)
1580 received = (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter);
1583 QLineEdit *edit;
1585 bool received;
1591 void tst_QComboBox::ensureReturnIsIgnored()
1593 ReturnClass r;
1594 r.show();
1596 QTest::keyClick(r.edit, Qt::Key_Return);
1597 QVERIFY(r.received);
1598 r.received = false;
1599 QTest::keyClick(r.edit, Qt::Key_Enter);
1600 QVERIFY(r.received);
1604 void tst_QComboBox::findText_data()
1606 QTest::addColumn<QStringList>("items");
1607 QTest::addColumn<int>("matchflags");
1608 QTest::addColumn<QString>("search");
1609 QTest::addColumn<int>("result");
1611 QStringList list;
1612 list << "One" << "Two" << "Three" << "Four" << "Five" << "Six" << "one";
1613 QTest::newRow("CaseSensitive_1") << list << (int)(Qt::MatchExactly|Qt::MatchCaseSensitive)
1614 << QString("Two") << 1;
1615 QTest::newRow("CaseSensitive_2") << list << (int)(Qt::MatchExactly|Qt::MatchCaseSensitive)
1616 << QString("two") << -1;
1617 QTest::newRow("CaseSensitive_3") << list << (int)(Qt::MatchExactly|Qt::MatchCaseSensitive)
1618 << QString("One") << 0;
1619 QTest::newRow("CaseSensitive_4") << list << (int)(Qt::MatchExactly|Qt::MatchCaseSensitive)
1620 << QString("one") << 6;
1621 QTest::newRow("CaseInsensitive_1") << list << (int)(Qt::MatchExactly) << QString("Two") << 1;
1622 QTest::newRow("CaseInsensitive_2") << list << (int)(Qt::MatchExactly) << QString("two") << -1;
1623 QTest::newRow("CaseInsensitive_3") << list << (int)(Qt::MatchExactly) << QString("One") << 0;
1624 QTest::newRow("CaseInsensitive_4") << list << (int)(Qt::MatchExactly) << QString("one") << 6;
1626 void tst_QComboBox::findText()
1628 QFETCH(QStringList, items);
1629 QFETCH(int, matchflags);
1630 QFETCH(QString, search);
1631 QFETCH(int, result);
1633 testWidget->clear();
1634 testWidget->addItems(items);
1636 QCOMPARE(testWidget->findText(search, (Qt::MatchFlags)matchflags), result);
1639 typedef QList<int> IntList;
1640 typedef QList<Qt::Key> KeyList;
1641 Q_DECLARE_METATYPE(IntList)
1642 Q_DECLARE_METATYPE(KeyList)
1644 void tst_QComboBox::flaggedItems_data()
1646 QTest::addColumn<QStringList>("itemList");
1647 QTest::addColumn<IntList>("deselectFlagList");
1648 QTest::addColumn<IntList>("disableFlagList");
1649 QTest::addColumn<KeyList>("keyMovementList");
1650 QTest::addColumn<bool>("editable");
1651 QTest::addColumn<int>("expectedIndex");
1653 for (int editable=0;editable<2;editable++) {
1654 QString testCase = editable ? "editable:" : "non-editable:";
1655 QStringList itemList;
1656 itemList << "One" << "Two" << "Three" << "Four" << "Five" << "Six" << "Seven" << "Eight";
1657 IntList deselectFlagList;
1658 IntList disableFlagList;
1659 KeyList keyMovementList;
1661 keyMovementList << Qt::Key_Down << Qt::Key_Down << Qt::Key_Down << Qt::Key_Down;
1662 QTest::newRow(testCase.toAscii() + "normal") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 4;
1664 deselectFlagList.clear();
1665 disableFlagList.clear();
1666 deselectFlagList << 1 << 3;
1667 QTest::newRow(testCase.toAscii() + "non-selectable") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 4;
1669 deselectFlagList.clear();
1670 disableFlagList.clear();
1671 disableFlagList << 2;
1672 QTest::newRow(testCase.toAscii() + "disabled") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 5;
1674 deselectFlagList.clear();
1675 disableFlagList.clear();
1676 deselectFlagList << 1 << 3;
1677 disableFlagList << 2 << 3;
1678 QTest::newRow(testCase.toAscii() + "mixed") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 6;
1679 deselectFlagList.clear();
1680 disableFlagList.clear();
1681 disableFlagList << 0 << 1 << 2 << 3 << 4 << 5 << 6;
1682 QTest::newRow(testCase.toAscii() + "nearly-empty") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 7;
1684 deselectFlagList.clear();
1685 disableFlagList.clear();
1686 disableFlagList << 0 << 1 << 2 << 3 << 5 << 6 << 7;
1687 keyMovementList.clear();
1688 QTest::newRow(testCase.toAscii() + "only one enabled") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 4;
1690 if (!editable) {
1691 deselectFlagList.clear();
1692 disableFlagList.clear();
1693 keyMovementList.clear();
1694 disableFlagList << 0 << 2 << 3;
1695 keyMovementList << Qt::Key_Down << Qt::Key_Home;
1696 QTest::newRow(testCase.toAscii() + "home-disabled") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 1;
1698 keyMovementList.clear();
1699 keyMovementList << Qt::Key_End;
1700 QTest::newRow(testCase.toAscii() + "end-key") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 7;
1702 disableFlagList.clear();
1703 disableFlagList << 1 ;
1704 keyMovementList << Qt::Key_T;
1705 QTest::newRow(testCase.toAscii() + "keyboard-search") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 2;
1707 itemList << "nine" << "ten";
1708 keyMovementList << Qt::Key_T;
1709 QTest::newRow(testCase.toAscii() + "search same start letter") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 9;
1711 keyMovementList.clear();
1712 keyMovementList << Qt::Key_T << Qt::Key_H;
1713 QTest::newRow(testCase.toAscii() + "keyboard search item") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 2;
1715 disableFlagList.clear();
1716 disableFlagList << 1 << 3 << 5 << 7 << 9;
1717 keyMovementList.clear();
1718 keyMovementList << Qt::Key_End << Qt::Key_Up << Qt::Key_Up << Qt::Key_PageDown << Qt::Key_PageUp << Qt::Key_PageUp << Qt::Key_Down;
1719 QTest::newRow(testCase.toAscii() + "all key combinations") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 4;
1720 } else {
1721 deselectFlagList.clear();
1722 disableFlagList.clear();
1723 disableFlagList << 1;
1724 keyMovementList.clear();
1725 keyMovementList << Qt::Key_T << Qt::Key_Enter;
1726 QTest::newRow(testCase.toAscii() + "disabled") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 2;
1727 QTest::newRow(testCase.toAscii() + "broken autocompletion") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 2;
1732 void tst_QComboBox::flaggedItems()
1734 QFETCH(QStringList, itemList);
1735 QFETCH(IntList, deselectFlagList);
1736 QFETCH(IntList, disableFlagList);
1737 QFETCH(KeyList, keyMovementList);
1738 QFETCH(bool, editable);
1739 QFETCH(int, expectedIndex);
1741 QComboBox comboBox;
1742 QListWidget listWidget;
1743 listWidget.addItems(itemList);
1745 comboBox.setEditable(editable);
1746 foreach (int index, deselectFlagList)
1747 listWidget.item(index)->setFlags(listWidget.item(index)->flags() & ~Qt::ItemIsSelectable);
1749 foreach (int index, disableFlagList)
1750 listWidget.item(index)->setFlags(listWidget.item(index)->flags() & ~Qt::ItemIsEnabled);
1752 comboBox.setModel(listWidget.model());
1753 comboBox.setView(&listWidget);
1754 comboBox.show();
1755 QApplication::setActiveWindow(&comboBox);
1756 comboBox.activateWindow();
1757 comboBox.setFocus();
1758 QTRY_VERIFY(comboBox.isVisible());
1759 QTRY_VERIFY(comboBox.hasFocus());
1761 if (editable)
1762 comboBox.lineEdit()->selectAll();
1764 QSignalSpy indexChangedInt(&comboBox, SIGNAL(currentIndexChanged(int)));
1765 for (int i = 0; i < keyMovementList.count(); ++i) {
1766 Qt::Key key = keyMovementList[i];
1767 QTest::keyClick(&comboBox, key);
1768 if (indexChangedInt.count() != i + 1) {
1769 QTest::qWait(400);
1773 QCOMPARE(comboBox.currentIndex() , expectedIndex);
1776 void tst_QComboBox::pixmapIcon()
1778 QComboBox box;
1779 QStandardItemModel *model = new QStandardItemModel(2, 1, &box);
1781 QPixmap pix(10, 10);
1782 pix.fill(Qt::red);
1783 model->setData(model->index(0, 0), "Element 1");
1784 model->setData(model->index(0, 0), pix, Qt::DecorationRole);
1786 QIcon icon(pix);
1787 model->setData(model->index(1, 0), "Element 2");
1788 model->setData(model->index(1, 0), icon, Qt::DecorationRole);
1790 box.setModel(model);
1792 QCOMPARE( box.itemIcon(0).isNull(), false );
1793 QCOMPARE( box.itemIcon(1).isNull(), false );
1796 // defined to be 120 by the wheel mouse vendors according to the docs
1797 #define WHEEL_DELTA 120
1799 void tst_QComboBox::mouseWheel_data()
1801 QTest::addColumn<IntList>("disabledItems");
1802 QTest::addColumn<int>("startIndex");
1803 QTest::addColumn<int>("wheelDirection");
1804 QTest::addColumn<int>("expectedIndex");
1806 IntList disabled;
1807 disabled << 0 << 1 << 2 << 4;
1808 int start = 3;
1809 int wheel = 1;
1810 int expected = 3;
1811 QTest::newRow("upper locked") << disabled << start << wheel << expected;
1813 wheel = -1;
1814 expected = 5;
1815 QTest::newRow("jump over") << disabled << start << wheel << expected;
1817 disabled.clear();
1818 disabled << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9;
1819 start = 0;
1820 wheel = -1;
1821 expected = 0;
1822 QTest::newRow("single Item enabled") << disabled << start << wheel << expected;
1825 void tst_QComboBox::mouseWheel()
1827 QFETCH(IntList, disabledItems);
1828 QFETCH(int, startIndex);
1829 QFETCH(int, wheelDirection);
1830 QFETCH(int, expectedIndex);
1832 QCoreApplication *applicationInstance = QCoreApplication::instance();
1833 QVERIFY(applicationInstance != 0);
1835 QComboBox box;
1836 QStringList list;
1837 list << "one" << "two" << "three" << "four" << "five" << "six" << "seven" << "eight" << "nine" << "ten";
1839 QListWidget listWidget;
1840 listWidget.addItems(list);
1842 foreach (int index, disabledItems)
1843 listWidget.item(index)->setFlags(listWidget.item(index)->flags() & ~Qt::ItemIsEnabled);
1845 box.setModel(listWidget.model());
1846 box.setView(&listWidget);
1847 for (int i=0; i < 2; ++i) {
1848 box.setEditable(i==0?false:true);
1849 box.setCurrentIndex(startIndex);
1851 QWheelEvent event = QWheelEvent(box.rect().bottomRight() , WHEEL_DELTA * wheelDirection, Qt::NoButton, Qt::NoModifier);
1852 QVERIFY(applicationInstance->sendEvent(&box,&event));
1854 QCOMPARE(box.currentIndex(), expectedIndex);
1858 void tst_QComboBox::layoutDirection()
1860 QComboBox box;
1861 Qt::LayoutDirection dir;
1862 QLineEdit *lineEdit;
1864 // RTL
1865 box.setLayoutDirection(Qt::RightToLeft);
1866 QStyleOptionComboBox opt;
1867 opt.direction = Qt::RightToLeft;
1868 dir = (Qt::LayoutDirection)box.style()->styleHint(QStyle::SH_ComboBox_LayoutDirection, &opt, &box);
1870 QCOMPARE(box.view()->layoutDirection(), dir);
1871 box.setEditable(true);
1872 QCOMPARE(box.lineEdit()->layoutDirection(), dir);
1873 lineEdit = new QLineEdit;
1874 QCOMPARE(lineEdit->layoutDirection(), qApp->layoutDirection());
1875 box.setLineEdit(lineEdit);
1876 QCOMPARE(lineEdit->layoutDirection(), dir);
1878 // LTR
1879 box.setLayoutDirection(Qt::LeftToRight);
1880 qApp->setLayoutDirection(Qt::RightToLeft);
1882 opt.direction = Qt::LeftToRight;
1883 dir = (Qt::LayoutDirection)box.style()->styleHint(QStyle::SH_ComboBox_LayoutDirection, &opt, &box);
1885 QCOMPARE(box.view()->layoutDirection(), dir);
1886 box.setEditable(true);
1887 QCOMPARE(box.lineEdit()->layoutDirection(), dir);
1888 lineEdit = new QLineEdit;
1889 QCOMPARE(lineEdit->layoutDirection(), qApp->layoutDirection());
1890 box.setLineEdit(lineEdit);
1891 QCOMPARE(lineEdit->layoutDirection(), dir);
1895 void tst_QComboBox::itemListPosition()
1897 //tests that the list is not out of the screen boundaries
1899 //put the QApplication layout back
1900 qApp->setLayoutDirection(Qt::LeftToRight);
1902 //we test QFontComboBox because it has the specific behaviour to set a fixed size
1903 //the the list view
1904 QFontComboBox combo;
1906 //the code to get the avaialbe screen space is copied from QComboBox code
1907 const int scrNumber = QApplication::desktop()->screenNumber(&combo);
1908 QRect screen;
1909 #ifdef Q_WS_WIN
1910 screen = QApplication::desktop()->screenGeometry(scrNumber);
1911 #elif defined Q_WS_X11
1912 if (X11->desktopEnvironment == DE_KDE)
1913 screen = QApplication::desktop()->screenGeometry(scrNumber);
1914 else
1915 screen = QApplication::desktop()->availableGeometry(scrNumber);
1916 #else
1917 screen = QApplication::desktop()->availableGeometry(scrNumber);
1918 #endif
1920 combo.move(screen.width()-combo.sizeHint().width(), 0); //puts the combo the the top-right corner
1922 combo.show();
1923 QTest::qWait(100); //wait because the window manager can move the window if there is a right panel
1924 combo.showPopup();
1925 QTest::qWait(100);
1927 QVERIFY( combo.view()->window()->x() + combo.view()->window()->width() <= screen.x() + screen.width() );
1931 void tst_QComboBox::separatorItem_data()
1933 QTest::addColumn<QStringList>("items");
1934 QTest::addColumn<IntList>("separators");
1936 QTest::newRow("test") << (QStringList() << "one" << "two" << "three" << "other...")
1937 << (IntList() << 4);
1940 void tst_QComboBox::separatorItem()
1942 QFETCH(QStringList, items);
1943 QFETCH(IntList, separators);
1945 QComboBox box;
1946 box.addItems(items);
1947 foreach(int index, separators)
1948 box.insertSeparator(index);
1949 QCOMPARE(box.count(), (items.count() + separators.count()));
1950 for (int i = 0, s = 0; i < box.count(); ++i) {
1951 if (i == separators.at(s)) {
1952 QCOMPARE(box.itemText(i), QString());
1953 ++s;
1954 } else {
1955 QCOMPARE(box.itemText(i), items.at(i - s));
1960 void tst_QComboBox::task190351_layout()
1962 #ifndef QT_NO_STYLE_CLEANLOOKS
1963 const QString oldStyle = QApplication::style()->objectName();
1964 QApplication::setStyle(new QCleanlooksStyle);
1966 QComboBox listCombo;
1967 QListWidget *list = new QListWidget();
1968 listCombo.setModel(list->model());
1969 listCombo.setView(list);
1970 for(int i = 1; i < 150; i++)
1971 list->addItem(QLatin1String("list") + QString::number(i));
1973 listCombo.show();
1974 QTest::qWait(100);
1975 listCombo.setCurrentIndex(70);
1976 listCombo.showPopup();
1977 QTest::qWait(100);
1979 QFrame *container = qFindChild<QComboBoxPrivateContainer *>(&listCombo);
1980 QVERIFY(container);
1981 QCOMPARE(static_cast<QAbstractItemView *>(list), qFindChild<QAbstractItemView *>(container));
1982 QWidget *top = qFindChild<QComboBoxPrivateScroller *>(container);
1983 QVERIFY(top);
1984 QVERIFY(top->isVisible());
1985 QCOMPARE(top->mapToGlobal(QPoint(0, top->height())).y(), list->mapToGlobal(QPoint()).y());
1987 QApplication::setStyle(oldStyle);
1988 #else
1989 QSKIP("Qt configured without cleanlooks style", SkipAll);
1990 #endif
1993 class task166349_ComboBox : public QComboBox
1995 Q_OBJECT
1996 public:
1997 task166349_ComboBox(QWidget *parent = 0) : QComboBox(parent)
1999 QStringList list;
2000 list << "one" << "two";
2001 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
2002 addItems(list);
2004 public slots:
2005 void onCurrentIndexChanged(int index)
2007 setEditable(index % 2 == 1);
2011 void tst_QComboBox::task166349_setEditableOnReturn()
2013 task166349_ComboBox comboBox;
2014 QTest::keyClick(&comboBox, Qt::Key_Down);
2015 QTest::keyClick(&comboBox, Qt::Key_1);
2016 QTest::keyClick(&comboBox, Qt::Key_Enter);
2017 QCOMPARE(QLatin1String("two1"), comboBox.itemText(comboBox.count() - 1));
2020 void tst_QComboBox::task191329_size()
2022 #ifndef QT_NO_STYLE_CLEANLOOKS
2023 const QString oldStyle = QApplication::style()->objectName();
2024 QApplication::setStyle(new QCleanlooksStyle);
2026 QComboBox tableCombo;
2027 int rows;
2028 if (QApplication::desktop()->screenGeometry().height() < 480)
2029 rows = 8;
2030 else
2031 rows = 15;
2033 QStandardItemModel model(rows, 2);
2034 for (int row = 0; row < model.rowCount(); ++row) {
2035 for (int column = 0; column < model.columnCount(); ++column) {
2036 QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
2037 model.setItem(row, column, item);
2040 QTableView *table = new QTableView();
2041 table->verticalHeader()->hide();
2042 table->horizontalHeader()->hide();
2043 tableCombo.setView(table);
2044 tableCombo.setModel(&model);
2046 tableCombo.show();
2047 QTest::qWait(100);
2048 tableCombo.showPopup();
2049 QTest::qWait(100);
2051 QFrame *container = qFindChild<QComboBoxPrivateContainer *>(&tableCombo);
2052 QVERIFY(container);
2053 QCOMPARE(static_cast<QAbstractItemView *>(table), qFindChild<QAbstractItemView *>(container));
2054 foreach (QWidget *button, qFindChildren<QComboBoxPrivateScroller *>(container)) {
2055 //the popup should be large enough to contains everithing so the top and left button are hidden
2056 QVERIFY(!button->isVisible());
2059 QApplication::setStyle(oldStyle);
2060 #else
2061 QSKIP("Qt configured without cleanlooks style", SkipAll);
2062 #endif
2065 void tst_QComboBox::task190205_setModelAdjustToContents()
2067 QStringList initialContent;
2068 QStringList finalContent;
2069 initialContent << "foo" << "bar";
2070 finalContent << "bar" << "foooooooobar";
2072 QComboBox box;
2073 box.setSizeAdjustPolicy(QComboBox::AdjustToContents);
2074 box.addItems(initialContent);
2075 box.show();
2077 QTest::qWait(100); //wait needed in order to get the combo initial size
2079 box.setModel(new QStringListModel(finalContent));
2081 QTest::qWait(500); //wait needed in order to resize the combo
2083 QComboBox correctBox;
2084 correctBox.addItems(finalContent);
2085 correctBox.show();
2087 QCoreApplication::processEvents();
2088 #ifdef Q_WS_X11
2089 qt_x11_wait_for_window_manager(&box);
2090 qt_x11_wait_for_window_manager(&correctBox);
2091 #endif
2093 // box should be resized to the same size as correctBox
2094 QCOMPARE(box.size(), correctBox.size());
2097 void tst_QComboBox::task248169_popupWithMinimalSize()
2099 QStringList initialContent;
2100 initialContent << "foo" << "bar" << "foobar";
2102 QComboBox comboBox;
2103 comboBox.addItems(initialContent);
2104 QDesktopWidget desktop;
2105 QRect desktopSize = desktop.availableGeometry();
2106 comboBox.view()->setMinimumWidth(desktopSize.width() / 2);
2108 comboBox.setGeometry(desktopSize.width() - (desktopSize.width() / 4), (desktopSize.width() / 4), (desktopSize.width() / 2), (desktopSize.width() / 4));
2110 comboBox.show();
2111 QTest::qWait(100);
2112 comboBox.showPopup();
2113 QTest::qWait(100);
2115 QFrame *container = qFindChild<QComboBoxPrivateContainer *>(&comboBox);
2116 QVERIFY(container);
2117 QVERIFY(desktop.screenGeometry(container).contains(container->geometry()));
2120 void tst_QComboBox::task247863_keyBoardSelection()
2122 QComboBox combo;
2123 combo.setEditable(false);
2124 combo.addItem( QLatin1String("111"));
2125 combo.addItem( QLatin1String("222"));
2126 combo.show();
2127 QApplication::setActiveWindow(&combo);
2128 QTest::qWait(100);
2130 QSignalSpy spy(&combo, SIGNAL(activated(const QString &)));
2131 qApp->setEffectEnabled(Qt::UI_AnimateCombo, false);
2132 QTest::keyClick(&combo, Qt::Key_Space);
2133 qApp->setEffectEnabled(Qt::UI_AnimateCombo, true);
2134 QTest::keyClick(0, Qt::Key_Down);
2135 QTest::keyClick(0, Qt::Key_Enter);
2136 QCOMPARE(combo.currentText(), QLatin1String("222"));
2137 QCOMPARE(spy.count(), 1);
2140 void tst_QComboBox::setModelColumn()
2142 QStandardItemModel model(5,3);
2143 model.setItem(0,0, new QStandardItem("0"));
2144 model.setItem(1,0, new QStandardItem("1"));
2145 model.setItem(2,0, new QStandardItem("2"));
2146 model.setItem(3,0, new QStandardItem("3"));
2147 model.setItem(4,0, new QStandardItem("4"));
2148 model.setItem(0,1, new QStandardItem("zero"));
2149 model.setItem(1,1, new QStandardItem("un"));
2150 model.setItem(2,1, new QStandardItem("deux"));
2151 model.setItem(3,1, new QStandardItem("trois"));
2152 model.setItem(4,1, new QStandardItem("quatre"));
2153 model.setItem(0,2, new QStandardItem("a"));
2154 model.setItem(1,2, new QStandardItem("b"));
2155 model.setItem(2,2, new QStandardItem("c"));
2156 model.setItem(3,2, new QStandardItem("d"));
2157 model.setItem(4,2, new QStandardItem("e"));
2159 QComboBox box;
2160 box.setModel(&model);
2161 QCOMPARE(box.currentText(), QString("0"));
2162 box.setModelColumn(1);
2163 QCOMPARE(box.currentText(), QString("zero"));
2166 void tst_QComboBox::noScrollbar_data()
2168 QTest::addColumn<QString>("stylesheet");
2170 QTest::newRow("normal") << QString();
2171 QTest::newRow("border") << QString::fromLatin1("QAbstractItemView { border: 12px solid blue;}");
2172 QTest::newRow("margin") << QString::fromLatin1("QAbstractItemView { margin: 12px 15px 13px 10px; }");
2173 QTest::newRow("padding") << QString::fromLatin1("QAbstractItemView { padding: 12px 15px 13px 10px;}");
2174 QTest::newRow("everything") << QString::fromLatin1("QAbstractItemView { border: 12px solid blue; "
2175 " padding: 12px 15px 13px 10px; margin: 12px 15px 13px 10px; }");
2176 QTest::newRow("everything and more") << QString::fromLatin1("QAbstractItemView { border: 1px 3px 5px 1px solid blue; "
2177 " padding: 2px 5px 3px 1px; margin: 2px 5px 3px 1px; } "
2178 " QAbstractItemView::item { border: 2px solid green; "
2179 " padding: 1px 1px 2px 2px; margin: 1px; } " );
2182 void tst_QComboBox::noScrollbar()
2184 QStringList initialContent;
2185 initialContent << "foo" << "bar" << "foobar" << "moo";
2186 QFETCH(QString, stylesheet);
2187 QString oldCss = qApp->styleSheet();
2188 qApp->setStyleSheet(stylesheet);
2191 QComboBox comboBox;
2192 comboBox.addItems(initialContent);
2193 comboBox.show();
2194 comboBox.resize(200, comboBox.height());
2195 QTest::qWait(100);
2196 comboBox.showPopup();
2197 QTest::qWait(100);
2198 QVERIFY(!comboBox.view()->horizontalScrollBar()->isVisible());
2199 QVERIFY(!comboBox.view()->verticalScrollBar()->isVisible());
2203 QTableWidget *table = new QTableWidget(2,2);
2204 QComboBox comboBox;
2205 comboBox.setModel(table->model());
2206 comboBox.setView(table);
2207 comboBox.show();
2208 QTest::qWait(100);
2209 comboBox.resize(200, comboBox.height());
2210 comboBox.showPopup();
2211 QTest::qWait(100);
2212 QVERIFY(!comboBox.view()->horizontalScrollBar()->isVisible());
2213 QVERIFY(!comboBox.view()->verticalScrollBar()->isVisible());
2216 qApp->setStyleSheet(oldCss);
2219 void tst_QComboBox::setItemDelegate()
2221 QComboBox comboBox;
2222 QStyledItemDelegate *itemDelegate = new QStyledItemDelegate;
2223 comboBox.setItemDelegate(itemDelegate);
2224 QCOMPARE(comboBox.itemDelegate(), itemDelegate);
2227 void tst_QComboBox::task253944_itemDelegateIsReset()
2229 QComboBox comboBox;
2230 QStyledItemDelegate *itemDelegate = new QStyledItemDelegate;
2231 comboBox.setItemDelegate(itemDelegate);
2233 comboBox.setEditable(true);
2234 QCOMPARE(comboBox.itemDelegate(), itemDelegate);
2236 comboBox.setStyleSheet("QComboBox { border: 1px solid gray; }");
2237 QCOMPARE(comboBox.itemDelegate(), itemDelegate);
2240 static void paintCombo(QImage *image, const QRect &rect)
2242 class FriendlyCombo : public QComboBox {
2243 public:
2244 void styleOption(QStyleOptionComboBox *optCombo) {
2245 initStyleOption(optCombo);
2247 } combo;
2248 combo.setEditable(true);
2250 QStyleOptionComboBox optCombo;
2251 combo.styleOption(&optCombo);
2252 optCombo.rect = rect;
2253 optCombo.palette.setCurrentColorGroup(QPalette::Active);
2254 optCombo.state = QStyle::State_None;
2256 QPainter painter(image);
2257 painter.fillRect(image->rect(), Qt::white);
2258 QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &optCombo, &painter, 0);
2261 void tst_QComboBox::paintingWithOffset()
2263 // The painting of the combobox should not depend on its position in
2264 // the widget. Some style are making the assumuption that the combobox
2265 // start at 0,0
2266 const QSize comboSize(80, 30);
2267 QImage noOffsetImage(comboSize, QImage::Format_ARGB32);
2268 const QRect noOffsetRect(QPoint(0, 0), comboSize);
2269 paintCombo(&noOffsetImage, noOffsetRect);
2271 QImage offsetImage(105, 80, QImage::Format_ARGB32);
2272 const QRect offsetRect(QPoint(25, 50), comboSize);
2273 paintCombo(&offsetImage, offsetRect);
2275 QImage translatedOffsetImage(comboSize, QImage::Format_ARGB32);
2277 QPainter painter(&translatedOffsetImage);
2278 painter.drawImage(noOffsetRect, offsetImage, offsetRect);
2280 QCOMPARE(noOffsetImage, translatedOffsetImage);
2283 void tst_QComboBox::subControlRectsWithOffset_data()
2285 QTest::addColumn<bool>("editable");
2287 QTest::newRow("editable = true") << true;
2288 QTest::newRow("editable = false") << false;
2291 void tst_QComboBox::subControlRectsWithOffset()
2293 // The sub control rect relative position should not depends
2294 // on the position of the combobox
2296 class FriendlyCombo : public QComboBox {
2297 public:
2298 void styleOption(QStyleOptionComboBox *optCombo) {
2299 initStyleOption(optCombo);
2301 } combo;
2302 QStyleOptionComboBox optCombo;
2303 combo.styleOption(&optCombo);
2306 const QRect rectAtOrigin(0, 0, 80, 30);
2307 const QPoint offset(25, 50);
2308 const QRect rectWithOffset = rectAtOrigin.translated(offset);
2310 QStyle *style = combo.style();
2312 QFETCH(bool, editable);
2313 optCombo.editable = editable;
2315 optCombo.rect = rectAtOrigin;
2316 QRect editFieldRect = style->subControlRect(QStyle::CC_ComboBox, &optCombo, QStyle::SC_ComboBoxEditField, 0);
2317 QRect arrowRect = style->subControlRect(QStyle::CC_ComboBox, &optCombo, QStyle::SC_ComboBoxArrow, 0);
2318 QRect listboxRect = style->subControlRect(QStyle::CC_ComboBox, &optCombo, QStyle::SC_ComboBoxListBoxPopup, 0);
2320 optCombo.rect = rectWithOffset;
2321 QRect editFieldRectWithOffset = style->subControlRect(QStyle::CC_ComboBox, &optCombo, QStyle::SC_ComboBoxEditField, 0);
2322 QRect arrowRectWithOffset = style->subControlRect(QStyle::CC_ComboBox, &optCombo, QStyle::SC_ComboBoxArrow, 0);
2323 QRect listboxRectWithOffset = style->subControlRect(QStyle::CC_ComboBox, &optCombo, QStyle::SC_ComboBoxListBoxPopup, 0);
2325 QCOMPARE(editFieldRect, editFieldRectWithOffset.translated(-offset));
2326 QCOMPARE(arrowRect, arrowRectWithOffset.translated(-offset));
2327 QCOMPARE(listboxRect, listboxRectWithOffset.translated(-offset));
2331 QTEST_MAIN(tst_QComboBox)
2332 #include "tst_qcombobox.moc"