tests/auto/qtoolbutton/tst_qtoolbutton.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 
       
    45 #include <qcoreapplication.h>
       
    46 #include <qdebug.h>
       
    47 #include <qtoolbutton.h>
       
    48 #include <qmenu.h>
       
    49 #include <qaction.h>
       
    50 #include <qwindowsstyle.h>
       
    51 #include <qstyleoption.h>
       
    52 
       
    53 //TESTED_CLASS=
       
    54 //TESTED_FILES=
       
    55 
       
    56 class tst_QToolButton : public QObject
       
    57 {
       
    58 Q_OBJECT
       
    59 
       
    60 public:
       
    61     tst_QToolButton();
       
    62     virtual ~tst_QToolButton();
       
    63 
       
    64 private slots:
       
    65     void getSetCheck();
       
    66     void triggered();
       
    67     void collapseTextOnPriority();
       
    68     void task230994_iconSize();
       
    69     void task176137_autoRepeatOfAction();
       
    70 
       
    71 protected slots:
       
    72     void sendMouseClick();
       
    73 private:
       
    74     QWidget *w;
       
    75 };
       
    76 
       
    77 tst_QToolButton::tst_QToolButton()
       
    78 {
       
    79 }
       
    80 
       
    81 tst_QToolButton::~tst_QToolButton()
       
    82 {
       
    83 }
       
    84 
       
    85 // Testing get/set functions
       
    86 void tst_QToolButton::getSetCheck()
       
    87 {
       
    88     QToolButton obj1;
       
    89     // QMenu* QToolButton::menu()
       
    90     // void QToolButton::setMenu(QMenu*)
       
    91     QMenu *var1 = new QMenu;
       
    92     obj1.setMenu(var1);
       
    93     QCOMPARE(var1, obj1.menu());
       
    94     obj1.setMenu((QMenu *)0);
       
    95     QCOMPARE((QMenu *)0, obj1.menu());
       
    96     delete var1;
       
    97 
       
    98     // ToolButtonPopupMode QToolButton::popupMode()
       
    99     // void QToolButton::setPopupMode(ToolButtonPopupMode)
       
   100     obj1.setPopupMode(QToolButton::ToolButtonPopupMode(QToolButton::DelayedPopup));
       
   101     QCOMPARE(QToolButton::ToolButtonPopupMode(QToolButton::DelayedPopup), obj1.popupMode());
       
   102     obj1.setPopupMode(QToolButton::ToolButtonPopupMode(QToolButton::MenuButtonPopup));
       
   103     QCOMPARE(QToolButton::ToolButtonPopupMode(QToolButton::MenuButtonPopup), obj1.popupMode());
       
   104     obj1.setPopupMode(QToolButton::ToolButtonPopupMode(QToolButton::InstantPopup));
       
   105     QCOMPARE(QToolButton::ToolButtonPopupMode(QToolButton::InstantPopup), obj1.popupMode());
       
   106 
       
   107     // bool QToolButton::autoRaise()
       
   108     // void QToolButton::setAutoRaise(bool)
       
   109     obj1.setAutoRaise(false);
       
   110     QCOMPARE(false, obj1.autoRaise());
       
   111     obj1.setAutoRaise(true);
       
   112     QCOMPARE(true, obj1.autoRaise());
       
   113 
       
   114     // QAction * QToolButton::defaultAction()
       
   115     // void QToolButton::setDefaultAction(QAction *)
       
   116     QAction *var4 = new QAction(0);
       
   117     obj1.setDefaultAction(var4);
       
   118     QCOMPARE(var4, obj1.defaultAction());
       
   119     obj1.setDefaultAction((QAction *)0);
       
   120     QCOMPARE((QAction *)0, obj1.defaultAction());
       
   121     delete var4;
       
   122 
       
   123 #ifdef QT_HAS_QT3SUPPORT
       
   124     //ensure that popup delay is not reset on style change
       
   125     obj1.setPopupDelay(5);
       
   126     obj1.setStyle(new QWindowsStyle);
       
   127     QCOMPARE(obj1.popupDelay(), 5);
       
   128 #endif
       
   129 }
       
   130 
       
   131 Q_DECLARE_METATYPE(QAction*)
       
   132 
       
   133 void tst_QToolButton::triggered()
       
   134 {
       
   135     qRegisterMetaType<QAction *>("QAction *");
       
   136     QToolButton tb;
       
   137     tb.show();
       
   138     QSignalSpy spy(&tb,SIGNAL(triggered(QAction*)));
       
   139     QMenu *menu = new QMenu("Menu");
       
   140     QAction *one = menu->addAction("one");
       
   141     menu->addAction("two");
       
   142     QAction *def = new QAction("def", this);
       
   143 
       
   144     tb.setMenu(menu);
       
   145     tb.setDefaultAction(def);
       
   146 
       
   147 #ifdef Q_WS_QWS
       
   148     QApplication::processEvents(); //wait for the window system to show the tool button
       
   149 #endif
       
   150 
       
   151     def->trigger();
       
   152     QCOMPARE(spy.count(),1);
       
   153     QCOMPARE(qvariant_cast<QAction *>(spy.at(0).at(0)), def);
       
   154 
       
   155     w = menu;
       
   156     QTimer::singleShot(30, this, SLOT(sendMouseClick()));
       
   157     tb.showMenu();
       
   158     QTest::qWait(20);
       
   159     QCOMPARE(spy.count(),2);
       
   160     QCOMPARE(qvariant_cast<QAction *>(spy.at(1).at(0)), one);
       
   161     delete menu;
       
   162 }
       
   163 
       
   164 void tst_QToolButton::collapseTextOnPriority()
       
   165 {
       
   166     class MyToolButton : public QToolButton
       
   167     {
       
   168         friend class tst_QToolButton;
       
   169     public:
       
   170         void initStyleOption(QStyleOptionToolButton *option)
       
   171         {
       
   172             QToolButton::initStyleOption(option);
       
   173         }
       
   174     };
       
   175 
       
   176     MyToolButton button;
       
   177     button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
       
   178     QAction action(button.style()->standardIcon(QStyle::SP_ArrowBack), "test", 0);
       
   179     button.setDefaultAction(&action);
       
   180 
       
   181     QStyleOptionToolButton option;
       
   182     button.initStyleOption(&option);
       
   183     QVERIFY(option.toolButtonStyle == Qt::ToolButtonTextBesideIcon);
       
   184     action.setPriority(QAction::LowPriority);
       
   185     button.initStyleOption(&option);
       
   186     QVERIFY(option.toolButtonStyle == Qt::ToolButtonIconOnly);
       
   187 }
       
   188 
       
   189 
       
   190 void tst_QToolButton::task230994_iconSize()
       
   191 {
       
   192     //we check that the iconsize returned bu initStyleOption is valid
       
   193     //when the toolbutton has no parent
       
   194     class MyToolButton : public QToolButton
       
   195     {
       
   196         friend class tst_QToolButton;
       
   197     public:
       
   198         void initStyleOption(QStyleOptionToolButton *option)
       
   199         {
       
   200             QToolButton::initStyleOption(option);
       
   201         }
       
   202     };
       
   203 
       
   204     MyToolButton button;
       
   205     QStyleOptionToolButton option;
       
   206     button.initStyleOption(&option);
       
   207     QVERIFY(option.iconSize.isValid());
       
   208 }
       
   209 
       
   210 void tst_QToolButton::task176137_autoRepeatOfAction()
       
   211 {
       
   212     QAction action(0);
       
   213     QToolButton tb;
       
   214     tb.setDefaultAction (&action);
       
   215     tb.show();
       
   216     QSignalSpy spy(&action,SIGNAL(triggered()));
       
   217     QTest::mousePress ( &tb, Qt::LeftButton);
       
   218     QTest::mouseRelease ( &tb, Qt::LeftButton, 0, QPoint (), 2000);
       
   219     QCOMPARE(spy.count(),1);
       
   220 
       
   221     // try again with auto repeat
       
   222     tb.setAutoRepeat (true);
       
   223     QSignalSpy repeatSpy(&action,SIGNAL(triggered())); // new spy
       
   224     QTest::mousePress ( &tb, Qt::LeftButton);
       
   225     QTest::mouseRelease ( &tb, Qt::LeftButton, 0, QPoint (), 3000);
       
   226     qreal expected = (3000 - tb.autoRepeatDelay()) / tb.autoRepeatInterval() + 1;
       
   227     //we check that the difference is less than 10% (on some systems timers are not super accurate)
       
   228     QVERIFY ( qAbs( (expected - repeatSpy.count()) / expected) < 0.1);
       
   229 }
       
   230 
       
   231 
       
   232 void tst_QToolButton::sendMouseClick()
       
   233 {
       
   234     QTest::mouseClick(w, Qt::LeftButton, 0, QPoint(7,7));
       
   235 }
       
   236 
       
   237 QTEST_MAIN(tst_QToolButton)
       
   238 #include "tst_qtoolbutton.moc"