tests/auto/qscrollbar/tst_qscrollbar.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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.
       
    15 **
       
    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.
       
    23 **
       
    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.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <QScrollBar>
       
    45 #include <QStyleOptionSlider>
       
    46 #include <QScrollArea>
       
    47 
       
    48 class tst_QScrollBar : public QObject
       
    49 {
       
    50     Q_OBJECT
       
    51 public slots:
       
    52     void initTestCase();
       
    53     void cleanupTestCase();
       
    54     void hideAndShow(int action);
       
    55 
       
    56 private slots:
       
    57     void scrollSingleStep();
       
    58     void task_209492();
       
    59 
       
    60 private:
       
    61     QScrollBar *testWidget;
       
    62 };
       
    63 
       
    64 void tst_QScrollBar::initTestCase()
       
    65 {
       
    66     testWidget = new QScrollBar(Qt::Horizontal);
       
    67     testWidget->resize(100, testWidget->height());
       
    68     testWidget->show();
       
    69 }
       
    70 
       
    71 void tst_QScrollBar::cleanupTestCase()
       
    72 {
       
    73     delete testWidget;
       
    74     testWidget = 0;
       
    75 }
       
    76 
       
    77 void tst_QScrollBar::hideAndShow(int)
       
    78 {
       
    79     testWidget->hide();
       
    80     testWidget->show();
       
    81 }
       
    82 
       
    83 // Check that the scrollbar doesn't scroll after calling hide and show
       
    84 // from a slot connected to the scrollbar's actionTriggered signal.
       
    85 void tst_QScrollBar::scrollSingleStep()
       
    86 {
       
    87     testWidget->setValue(testWidget->minimum());
       
    88     QCOMPARE(testWidget->value(), testWidget->minimum());
       
    89     connect(testWidget, SIGNAL(actionTriggered(int)), this, SLOT(hideAndShow(int)));
       
    90 
       
    91     // Get rect for the area to click on
       
    92     const QStyleOptionSlider opt = qt_qscrollbarStyleOption(testWidget);
       
    93     QRect sr = testWidget->style()->subControlRect(QStyle::CC_ScrollBar, &opt,
       
    94                                                    QStyle::SC_ScrollBarAddLine, testWidget);
       
    95 
       
    96     if (!sr.isValid())
       
    97         QSKIP("SC_ScrollBarAddLine not valid",  SkipAll);
       
    98 
       
    99     QTest::mouseClick(testWidget, Qt::LeftButton, Qt::NoModifier, QPoint(sr.x(), sr.y()));
       
   100     QTest::qWait(510); // initial delay is 500 for setRepeatAction
       
   101     disconnect(testWidget, SIGNAL(actionTriggered(int)), 0, 0);
       
   102     QCOMPARE(testWidget->value(), testWidget->singleStep());
       
   103 }
       
   104 
       
   105 void tst_QScrollBar::task_209492()
       
   106 {
       
   107     class MyScrollArea : public QScrollArea
       
   108     {
       
   109     public:
       
   110         int scrollCount;
       
   111         MyScrollArea(QWidget *parent = 0) : QScrollArea(parent), scrollCount(0) {}
       
   112     protected:
       
   113         void paintEvent(QPaintEvent *) { QTest::qSleep(600); }
       
   114         void scrollContentsBy(int, int) { ++scrollCount; viewport()->update(); }
       
   115     };
       
   116 
       
   117     MyScrollArea scrollArea;
       
   118     QScrollBar *verticalScrollBar = scrollArea.verticalScrollBar();
       
   119     verticalScrollBar->setRange(0, 1000);
       
   120     scrollArea.show();
       
   121     QTest::qWait(300);
       
   122 
       
   123     QSignalSpy spy(verticalScrollBar, SIGNAL(actionTriggered(int)));
       
   124     QCOMPARE(scrollArea.scrollCount, 0);
       
   125     QCOMPARE(spy.count(), 0);
       
   126 
       
   127     // Simulate a mouse click on the "scroll down button".
       
   128     const QPoint pressPoint(verticalScrollBar->width() / 2, verticalScrollBar->height() - 10);
       
   129     const QPoint globalPressPoint = verticalScrollBar->mapToGlobal(globalPressPoint);
       
   130     QMouseEvent mousePressEvent(QEvent::MouseButtonPress, pressPoint, globalPressPoint,
       
   131                                 Qt::LeftButton, Qt::LeftButton, 0);
       
   132     QApplication::sendEvent(verticalScrollBar, &mousePressEvent);
       
   133     QTest::qWait(50);
       
   134     QMouseEvent mouseReleaseEvent(QEvent::MouseButtonRelease, pressPoint, globalPressPoint,
       
   135                                   Qt::LeftButton, Qt::LeftButton, 0);
       
   136     QApplication::sendEvent(verticalScrollBar, &mouseReleaseEvent);
       
   137 
       
   138     // Check that the action was triggered once.
       
   139 #ifdef Q_WS_MAC
       
   140     QEXPECT_FAIL("", "Fix does does not work on Mac due to paint architechure differences.", Abort);
       
   141 #endif
       
   142     QCOMPARE(scrollArea.scrollCount, 1);
       
   143     QCOMPARE(spy.count(), 1);
       
   144 }
       
   145 
       
   146 QTEST_MAIN(tst_QScrollBar)
       
   147 #include "tst_qscrollbar.moc"