tests/auto/qcombobox/tst_qcombobox.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    55 #include <qheaderview.h>
    55 #include <qheaderview.h>
    56 #include <qlistwidget.h>
    56 #include <qlistwidget.h>
    57 #include <qtreewidget.h>
    57 #include <qtreewidget.h>
    58 #include <qtablewidget.h>
    58 #include <qtablewidget.h>
    59 #include <qscrollbar.h>
    59 #include <qscrollbar.h>
       
    60 #include <qboxlayout.h>
    60 #ifdef Q_WS_MAC
    61 #ifdef Q_WS_MAC
    61 #include <qmacstyle_mac.h>
    62 #include <qmacstyle_mac.h>
    62 #elif defined Q_WS_X11
    63 #elif defined Q_WS_X11
    63 #include <private/qt_x11_p.h>
    64 #include <private/qt_x11_p.h>
    64 #endif
    65 #endif
   150     void task253944_itemDelegateIsReset();
   151     void task253944_itemDelegateIsReset();
   151     void subControlRectsWithOffset_data();
   152     void subControlRectsWithOffset_data();
   152     void subControlRectsWithOffset();
   153     void subControlRectsWithOffset();
   153     void task260974_menuItemRectangleForComboBoxPopup();
   154     void task260974_menuItemRectangleForComboBoxPopup();
   154     void removeItem();
   155     void removeItem();
       
   156     void resetModel();
       
   157     void keyBoardNavigationWithMouse();
       
   158     void task_QTBUG_1071_changingFocusEmitsActivated();
   155 
   159 
   156 protected slots:
   160 protected slots:
   157     void onEditTextChanged( const QString &newString );
   161     void onEditTextChanged( const QString &newString );
   158 
   162 
   159 private:
   163 private:
   809     testWidget->addItem("abCdef");
   813     testWidget->addItem("abCdef");
   810     testWidget->setEditable(true);
   814     testWidget->setEditable(true);
   811 
   815 
   812     // case insensitive
   816     // case insensitive
   813     testWidget->clearEditText();
   817     testWidget->clearEditText();
       
   818     QSignalSpy spyReturn(testWidget, SIGNAL(activated(int)));
   814     testWidget->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive);
   819     testWidget->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive);
   815     QVERIFY(testWidget->autoCompletionCaseSensitivity() == Qt::CaseInsensitive);
   820     QVERIFY(testWidget->autoCompletionCaseSensitivity() == Qt::CaseInsensitive);
   816 
   821 
   817     QTest::keyClick(testWidget->lineEdit(), Qt::Key_A);
   822     QTest::keyClick(testWidget->lineEdit(), Qt::Key_A);
   818     qApp->processEvents();
   823     qApp->processEvents();
   819     QCOMPARE(testWidget->currentText(), QString("aww"));
   824     QCOMPARE(testWidget->currentText(), QString("aww"));
       
   825     QCOMPARE(spyReturn.count(), 0);
   820 
   826 
   821     QTest::keyClick(testWidget->lineEdit(), Qt::Key_B);
   827     QTest::keyClick(testWidget->lineEdit(), Qt::Key_B);
   822     qApp->processEvents();
   828     qApp->processEvents();
   823     // autocompletions preserve userkey-case from 4.2
   829     // autocompletions preserve userkey-case from 4.2
   824     QCOMPARE(testWidget->currentText(), QString("abCDEF"));
   830     QCOMPARE(testWidget->currentText(), QString("abCDEF"));
       
   831     QCOMPARE(spyReturn.count(), 0);
   825 
   832 
   826     QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
   833     QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
   827     qApp->processEvents();
   834     qApp->processEvents();
   828     QCOMPARE(testWidget->currentText(), QString("aBCDEF")); // case restored to item's case
   835     QCOMPARE(testWidget->currentText(), QString("aBCDEF")); // case restored to item's case
       
   836     QCOMPARE(spyReturn.count(), 1);
   829 
   837 
   830     testWidget->clearEditText();
   838     testWidget->clearEditText();
   831     QTest::keyClick(testWidget->lineEdit(), 'c');
   839     QTest::keyClick(testWidget->lineEdit(), 'c');
   832     QCOMPARE(testWidget->currentText(), QString("cow"));
   840     QCOMPARE(testWidget->currentText(), QString("cow"));
   833     QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
   841     QTest::keyClick(testWidget->lineEdit(), Qt::Key_Enter);
  2280 void tst_QComboBox::setItemDelegate()
  2288 void tst_QComboBox::setItemDelegate()
  2281 {
  2289 {
  2282     QComboBox comboBox;
  2290     QComboBox comboBox;
  2283     QStyledItemDelegate *itemDelegate = new QStyledItemDelegate;
  2291     QStyledItemDelegate *itemDelegate = new QStyledItemDelegate;
  2284     comboBox.setItemDelegate(itemDelegate);
  2292     comboBox.setItemDelegate(itemDelegate);
  2285 #ifdef Q_CC_MWERKS
  2293     // the cast is a workaround for the XLC and Metrowerks compilers
  2286     QCOMPARE(static_cast<QStyledItemDelegate *>(comboBox.itemDelegate()), itemDelegate);
  2294     QCOMPARE(static_cast<QStyledItemDelegate *>(comboBox.itemDelegate()), itemDelegate);
  2287 #else
       
  2288     QCOMPARE(comboBox.itemDelegate(), itemDelegate);
       
  2289 #endif
       
  2290 }
  2295 }
  2291 
  2296 
  2292 void tst_QComboBox::task253944_itemDelegateIsReset()
  2297 void tst_QComboBox::task253944_itemDelegateIsReset()
  2293 {
  2298 {
  2294     QComboBox comboBox;
  2299     QComboBox comboBox;
  2295     QStyledItemDelegate *itemDelegate = new QStyledItemDelegate;
  2300     QStyledItemDelegate *itemDelegate = new QStyledItemDelegate;
  2296     comboBox.setItemDelegate(itemDelegate);
  2301     comboBox.setItemDelegate(itemDelegate);
  2297 
  2302 
       
  2303     // the casts are workarounds for the XLC and Metrowerks compilers
       
  2304 
  2298     comboBox.setEditable(true);
  2305     comboBox.setEditable(true);
  2299 #ifdef Q_CC_MWERKS
       
  2300     QCOMPARE(static_cast<QStyledItemDelegate *>(comboBox.itemDelegate()), itemDelegate);
  2306     QCOMPARE(static_cast<QStyledItemDelegate *>(comboBox.itemDelegate()), itemDelegate);
  2301 #else
       
  2302     QCOMPARE(comboBox.itemDelegate(), itemDelegate);
       
  2303 #endif
       
  2304 
  2307 
  2305     comboBox.setStyleSheet("QComboBox { border: 1px solid gray; }");
  2308     comboBox.setStyleSheet("QComboBox { border: 1px solid gray; }");
  2306 #ifdef Q_CC_MWERKS
       
  2307     QCOMPARE(static_cast<QStyledItemDelegate *>(comboBox.itemDelegate()), itemDelegate);
  2309     QCOMPARE(static_cast<QStyledItemDelegate *>(comboBox.itemDelegate()), itemDelegate);
  2308 #else
       
  2309     QCOMPARE(comboBox.itemDelegate(), itemDelegate);
       
  2310 #endif
       
  2311 }
  2310 }
  2312 
  2311 
  2313 
  2312 
  2314 void tst_QComboBox::subControlRectsWithOffset_data()
  2313 void tst_QComboBox::subControlRectsWithOffset_data()
  2315 {
  2314 {
  2414     QCOMPARE(cb.count(), 1);
  2413     QCOMPARE(cb.count(), 1);
  2415     cb.removeItem(0);
  2414     cb.removeItem(0);
  2416     QCOMPARE(cb.count(), 0);
  2415     QCOMPARE(cb.count(), 0);
  2417 }
  2416 }
  2418 
  2417 
       
  2418 void tst_QComboBox::resetModel()
       
  2419 {
       
  2420     class StringListModel : public QStringListModel
       
  2421     {
       
  2422     public:
       
  2423         StringListModel(const QStringList &list) : QStringListModel(list)
       
  2424         {
       
  2425         }
       
  2426 
       
  2427         void reset()
       
  2428         {
       
  2429             QStringListModel::reset();
       
  2430         }
       
  2431     };
       
  2432     QComboBox cb;
       
  2433     StringListModel model( QStringList() << "1" << "2");
       
  2434     QSignalSpy spy(&cb, SIGNAL(currentIndexChanged(int)));
       
  2435     QCOMPARE(spy.count(), 0);
       
  2436     QCOMPARE(cb.currentIndex(), -1); //no selection
       
  2437 
       
  2438     cb.setModel(&model);
       
  2439 
       
  2440     QCOMPARE(spy.count(), 1);
       
  2441     QCOMPARE(cb.currentIndex(), 0); //first item selected
       
  2442 
       
  2443     model.reset();
       
  2444     QCOMPARE(spy.count(), 2);
       
  2445     QCOMPARE(cb.currentIndex(), -1); //no selection
       
  2446 
       
  2447 }
       
  2448 
       
  2449 void tst_QComboBox::keyBoardNavigationWithMouse()
       
  2450 {
       
  2451     QComboBox combo;
       
  2452     combo.setEditable(false);
       
  2453     for (int i = 0; i < 80; i++)
       
  2454         combo.addItem( QString::number(i));
       
  2455     combo.show();
       
  2456     QApplication::setActiveWindow(&combo);
       
  2457     QTest::qWaitForWindowShown(&combo);
       
  2458     QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&combo));
       
  2459 
       
  2460     QCOMPARE(combo.currentText(), QLatin1String("0"));
       
  2461 
       
  2462     combo.setFocus();
       
  2463     QTRY_VERIFY(combo.hasFocus());
       
  2464 
       
  2465     QTest::keyClick(testWidget->lineEdit(), Qt::Key_Space);
       
  2466     QTest::qWait(30);
       
  2467     QTRY_VERIFY(combo.view());
       
  2468     QTRY_VERIFY(combo.view()->isVisible());
       
  2469     QTest::qWait(130);
       
  2470 
       
  2471     QCOMPARE(combo.currentText(), QLatin1String("0"));
       
  2472 
       
  2473 #ifdef Q_OS_WINCE
       
  2474     QSKIP("When calling cursor function, Windows CE responds with: This function is not supported on this system.", SkipAll);
       
  2475 #endif
       
  2476 
       
  2477     QCursor::setPos(combo.view()->mapToGlobal(combo.view()->rect().center()));
       
  2478     QTest::qWait(200);
       
  2479 
       
  2480 #define GET_SELECTION(SEL) \
       
  2481     QCOMPARE(combo.view()->selectionModel()->selection().count(), 1); \
       
  2482     QCOMPARE(combo.view()->selectionModel()->selection().indexes().count(), 1); \
       
  2483     SEL = combo.view()->selectionModel()->selection().indexes().first().row()
       
  2484 
       
  2485     int selection;
       
  2486     GET_SELECTION(selection);
       
  2487 
       
  2488     //since we moved the mouse is in the middle it should even be around 5;
       
  2489     QVERIFY(selection > 3);
       
  2490 
       
  2491     static const int final = 40;
       
  2492     for (int i = selection + 1;  i <= final; i++)
       
  2493     {
       
  2494         QTest::keyClick(combo.view(), Qt::Key_Down);
       
  2495         QTest::qWait(20);
       
  2496         GET_SELECTION(selection);
       
  2497         QCOMPARE(selection, i);
       
  2498     }
       
  2499 
       
  2500     QTest::keyClick(combo.view(), Qt::Key_Enter);
       
  2501     QTRY_COMPARE(combo.currentText(), QString::number(final));
       
  2502 }
       
  2503 
       
  2504 void tst_QComboBox::task_QTBUG_1071_changingFocusEmitsActivated()
       
  2505 {
       
  2506     QWidget w;
       
  2507     QVBoxLayout layout(&w);
       
  2508     QComboBox cb;
       
  2509     cb.setEditable(true);
       
  2510     QSignalSpy spy(&cb, SIGNAL(activated(int)));
       
  2511     cb.addItem("0");
       
  2512     cb.addItem("1");
       
  2513     cb.addItem("2");
       
  2514     QLineEdit edit;
       
  2515     layout.addWidget(&cb);
       
  2516     layout.addWidget(&edit);
       
  2517 
       
  2518     w.show();
       
  2519     QApplication::setActiveWindow(&w);
       
  2520     QTest::qWaitForWindowShown(&w);
       
  2521     cb.clearEditText();
       
  2522     cb.setFocus();
       
  2523     QApplication::processEvents();
       
  2524     QTRY_VERIFY(cb.hasFocus());
       
  2525     QTest::keyClick(0, '1');
       
  2526     QCOMPARE(spy.count(), 0);
       
  2527     edit.setFocus();
       
  2528     QTRY_VERIFY(edit.hasFocus());
       
  2529     QTRY_COMPARE(spy.count(), 1);
       
  2530 }
       
  2531 
  2419 QTEST_MAIN(tst_QComboBox)
  2532 QTEST_MAIN(tst_QComboBox)
  2420 #include "tst_qcombobox.moc"
  2533 #include "tst_qcombobox.moc"