tests/auto/macgui/tst_macgui.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 <QApplication>
       
    44 #include <QMessageBox> 
       
    45 #include <QtTest/QtTest>
       
    46 #include <QSplashScreen>
       
    47 #include <QScrollBar>
       
    48 #include <QProgressDialog>
       
    49 #include <QSpinBox>
       
    50 
       
    51 #include <guitest.h>
       
    52 
       
    53 #ifdef Q_OS_MAC
       
    54 
       
    55 class tst_MacGui : public GuiTester
       
    56 {
       
    57 Q_OBJECT
       
    58 private slots:
       
    59     void scrollbarPainting();
       
    60     
       
    61     void dummy();
       
    62     void splashScreenModality();
       
    63     void dialogModality();
       
    64     void nonModalOrder();
       
    65 
       
    66     void spinBoxArrowButtons();
       
    67 };
       
    68 
       
    69 
       
    70 QPixmap grabWindowContents(QWidget * widget)
       
    71 {
       
    72     return QPixmap::grabWindow(widget->winId());
       
    73 }
       
    74 
       
    75 /*
       
    76     Test that vertical and horizontal mac-style scrollbars paint their
       
    77     entire area.
       
    78 */
       
    79 void tst_MacGui::scrollbarPainting()
       
    80 {
       
    81     ColorWidget colorWidget;
       
    82     colorWidget.resize(400, 400);
       
    83 
       
    84     QSize scrollBarSize;
       
    85 
       
    86     QScrollBar verticalScrollbar(&colorWidget);
       
    87     verticalScrollbar.move(10, 10);
       
    88     scrollBarSize = verticalScrollbar.sizeHint();
       
    89     scrollBarSize.setHeight(200);
       
    90     verticalScrollbar.resize(scrollBarSize);
       
    91 
       
    92     QScrollBar horizontalScrollbar(&colorWidget);
       
    93     horizontalScrollbar.move(30, 10);
       
    94     horizontalScrollbar.setOrientation(Qt::Horizontal);
       
    95     scrollBarSize = horizontalScrollbar.sizeHint();
       
    96     scrollBarSize.setWidth(200);
       
    97     horizontalScrollbar.resize(scrollBarSize);
       
    98 
       
    99     colorWidget.show();
       
   100     colorWidget.raise();
       
   101     QTest::qWait(100);
       
   102 
       
   103     QPixmap pixmap = grabWindowContents(&colorWidget);
       
   104 
       
   105     QVERIFY(isContent(pixmap.toImage(), verticalScrollbar.geometry(), GuiTester::Horizontal));
       
   106     QVERIFY(isContent(pixmap.toImage(), horizontalScrollbar.geometry(), GuiTester::Vertical));
       
   107 }
       
   108 
       
   109 // When running the auto-tests on scruffy, the first enter-the-event-loop-and-wait-for-a-click
       
   110 // test that runs always times out, so we have this dummy test.
       
   111 void tst_MacGui::dummy()
       
   112 {
       
   113     QPixmap pix(100, 100);
       
   114     QSplashScreen splash(pix);
       
   115     splash.show();
       
   116 
       
   117     QMessageBox *box = new QMessageBox();
       
   118     box->setText("accessible?");
       
   119     box->show();
       
   120 
       
   121     // Find the "OK" button and schedule a press.
       
   122     InterfaceChildPair interface = wn.find(QAccessible::Name, "OK", box);
       
   123     QVERIFY(interface.iface);
       
   124     const int delay = 1000;
       
   125     clickLater(interface, Qt::LeftButton, delay);
       
   126 
       
   127     // Show dialog and and enter event loop.
       
   128     connect(wn.getWidget(interface), SIGNAL(clicked()), SLOT(exitLoopSlot()));
       
   129     const int timeout = 4;
       
   130     QTestEventLoop::instance().enterLoop(timeout);
       
   131 }
       
   132 
       
   133 /*
       
   134     Test that a message box pops up in front of a QSplashScreen.
       
   135 */
       
   136 void tst_MacGui::splashScreenModality()
       
   137 {
       
   138     QPixmap pix(300, 300);
       
   139     QSplashScreen splash(pix);
       
   140     splash.show();
       
   141 
       
   142     QMessageBox box;
       
   143     //box.setWindowFlags(box.windowFlags() | Qt::WindowStaysOnTopHint);
       
   144     box.setText("accessible?");
       
   145     box.show();
       
   146 
       
   147     // Find the "OK" button and schedule a press.
       
   148     InterfaceChildPair interface = wn.find(QAccessible::Name, "OK", &box);
       
   149     QVERIFY(interface.iface);
       
   150     const int delay = 1000;
       
   151     clickLater(interface, Qt::LeftButton, delay);
       
   152 
       
   153     // Show dialog and and enter event loop.
       
   154     connect(wn.getWidget(interface), SIGNAL(clicked()), SLOT(exitLoopSlot()));
       
   155     const int timeout = 4;
       
   156     QTestEventLoop::instance().enterLoop(timeout);
       
   157     QVERIFY(QTestEventLoop::instance().timeout() == false);
       
   158 }
       
   159 
       
   160 
       
   161 /*
       
   162     Test that a non-modal dialog created as a child of a modal dialog is
       
   163     shown in front.
       
   164 */
       
   165 void tst_MacGui::dialogModality()
       
   166 { 
       
   167     QDialog d;
       
   168     d.setModal(true);
       
   169     d.show();
       
   170     
       
   171     QProgressDialog progress(&d);
       
   172     progress.setValue(2);
       
   173 
       
   174     InterfaceChildPair interface = wn.find(QAccessible::Name, "Cancel", &progress);
       
   175     QVERIFY(interface.iface);
       
   176     const int delay = 2000;
       
   177     clickLater(interface, Qt::LeftButton, delay);
       
   178     
       
   179     connect(&progress, SIGNAL(canceled()), SLOT(exitLoopSlot()));
       
   180 
       
   181     const int timeout = 3;
       
   182     QTestEventLoop::instance().enterLoop(timeout);
       
   183     QVERIFY(QTestEventLoop::instance().timeout() == false);
       
   184 }
       
   185 
       
   186 class PrimaryWindowDialog : public QDialog
       
   187 {
       
   188 Q_OBJECT
       
   189 public:
       
   190     PrimaryWindowDialog();
       
   191     QWidget *secondaryWindow;
       
   192     QWidget *frontWidget;
       
   193 public slots:
       
   194     void showSecondaryWindow();
       
   195     void test();
       
   196 };
       
   197 
       
   198 PrimaryWindowDialog::PrimaryWindowDialog() : QDialog(0)
       
   199 {
       
   200     frontWidget = 0;
       
   201     secondaryWindow = new ColorWidget(this);
       
   202     secondaryWindow->setWindowFlags(Qt::Window);
       
   203     secondaryWindow->resize(400, 400);
       
   204     secondaryWindow->move(100, 100);
       
   205     QTimer::singleShot(1000, this, SLOT(showSecondaryWindow()));
       
   206     QTimer::singleShot(2000, this, SLOT(test()));
       
   207     QTimer::singleShot(3000, this, SLOT(close()));
       
   208 }
       
   209 
       
   210 void PrimaryWindowDialog::showSecondaryWindow()
       
   211 {
       
   212     secondaryWindow->show();
       
   213 }
       
   214 
       
   215 void PrimaryWindowDialog::test()
       
   216 {
       
   217     frontWidget = QApplication::widgetAt(secondaryWindow->mapToGlobal(QPoint(100, 100)));
       
   218 }
       
   219 
       
   220 /*
       
   221     Test that a non-modal child window of a modal dialog is shown in front
       
   222     of the dialog even if the dialog becomes modal after the child window
       
   223     is created.
       
   224 */
       
   225 void tst_MacGui::nonModalOrder()
       
   226 {
       
   227     clearSequence();
       
   228     PrimaryWindowDialog primary;
       
   229     primary.resize(400, 400);
       
   230     primary.move(100, 100);
       
   231     primary.exec();
       
   232     QCOMPARE(primary.frontWidget, primary.secondaryWindow);
       
   233 }
       
   234 
       
   235 /*
       
   236     Test that the QSpinBox buttons are correctly positioned with the Mac style.
       
   237 */
       
   238 void tst_MacGui::spinBoxArrowButtons()
       
   239 {
       
   240     ColorWidget colorWidget;
       
   241     colorWidget.resize(200, 200);
       
   242     QSpinBox spinBox(&colorWidget);
       
   243     QSpinBox spinBox2(&colorWidget);
       
   244     spinBox2.move(0, 100);
       
   245     colorWidget.show();
       
   246     QTest::qWait(100);
       
   247     
       
   248     // Grab an unfocused spin box.
       
   249     const QImage noFocus = grabWindowContents(&colorWidget).toImage();
       
   250 
       
   251     // Set focus by clicking the less button.
       
   252     InterfaceChildPair lessInterface = wn.find(QAccessible::Name, "Less", &spinBox);
       
   253     QVERIFY(lessInterface.iface);
       
   254     const int delay = 500;
       
   255     clickLater(lessInterface, Qt::LeftButton, delay);
       
   256     const int timeout = 1;
       
   257     QTestEventLoop::instance().enterLoop(timeout);
       
   258 
       
   259     // Grab a focused spin box.
       
   260     const QImage focus = grabWindowContents(&colorWidget).toImage();
       
   261 
       
   262     // Compare the arrow area of the less button to see if it moved.
       
   263     const QRect lessRect = lessInterface.iface->rect(lessInterface.possibleChild);
       
   264     const QRect lessLocalRect(colorWidget.mapFromGlobal(lessRect.topLeft()), colorWidget.mapFromGlobal(lessRect.bottomRight()));
       
   265     const QRect compareRect = lessLocalRect.adjusted(5, 3, -5, -7);
       
   266     QVERIFY(noFocus.copy(compareRect) == focus.copy(compareRect));
       
   267 }
       
   268 
       
   269 QTEST_MAIN(tst_MacGui)
       
   270 
       
   271 #else
       
   272 
       
   273 QTEST_NOOP_MAIN
       
   274 
       
   275 #endif
       
   276 
       
   277 #include "tst_macgui.moc"
       
   278