tests/auto/qstyleoption/tst_qstyleoption.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 <QStyleOption>
       
    45 
       
    46 
       
    47 class tst_QStyleOption: public QObject 
       
    48 {
       
    49     Q_OBJECT
       
    50 
       
    51 private slots:
       
    52     void qstyleoptioncast_data();
       
    53     void qstyleoptioncast();
       
    54     void copyconstructors();
       
    55 };
       
    56 
       
    57 // Just a simple container for QStyleOption-pointer 
       
    58 struct StyleOptionPointerBase
       
    59 {
       
    60     QStyleOption *pointer;
       
    61 
       
    62     StyleOptionPointerBase(QStyleOption *p = 0) : pointer(p) { }
       
    63 
       
    64     virtual ~StyleOptionPointerBase() { pointer = 0; }
       
    65 };
       
    66 
       
    67 template <typename T>
       
    68 struct StyleOptionPointer: public StyleOptionPointerBase
       
    69 {
       
    70     StyleOptionPointer(T *p = 0): StyleOptionPointerBase(p) {}
       
    71     ~StyleOptionPointer() { delete static_cast<T *>(pointer); pointer = 0; }
       
    72 };
       
    73 
       
    74 Q_DECLARE_METATYPE(StyleOptionPointerBase*)
       
    75 
       
    76 template <typename T>
       
    77 inline StyleOptionPointerBase *stylePtr(T *ptr) { return new StyleOptionPointer<T>(ptr); }
       
    78 
       
    79 void tst_QStyleOption::qstyleoptioncast_data()
       
    80 {
       
    81     QTest::addColumn<StyleOptionPointerBase *>("testOption");
       
    82     QTest::addColumn<bool>("canCastToComplex");
       
    83     QTest::addColumn<int>("type");    
       
    84 
       
    85     QTest::newRow("optionDefault") << stylePtr(new QStyleOption) << false << int(QStyleOption::SO_Default);
       
    86     QTest::newRow("optionButton") << stylePtr(new QStyleOptionButton) << false << int(QStyleOption::SO_Button);
       
    87     QTest::newRow("optionComboBox") << stylePtr(new QStyleOptionComboBox) << true << int(QStyleOption::SO_ComboBox);
       
    88     QTest::newRow("optionComplex") << stylePtr(new QStyleOptionComplex) << true << int(QStyleOption::SO_Complex);
       
    89     QTest::newRow("optionDockWidget") << stylePtr(new QStyleOptionDockWidget) << false << int(QStyleOption::SO_DockWidget);
       
    90     QTest::newRow("optionFocusRect") << stylePtr(new QStyleOptionFocusRect) << false << int(QStyleOption::SO_FocusRect);
       
    91     QTest::newRow("optionFrame") << stylePtr(new QStyleOptionFrame) << false << int(QStyleOption::SO_Frame);
       
    92     QTest::newRow("optionHeader") << stylePtr(new QStyleOptionHeader) << false << int(QStyleOption::SO_Header);
       
    93     QTest::newRow("optionMenuItem") << stylePtr(new QStyleOptionMenuItem) << false << int(QStyleOption::SO_MenuItem);
       
    94     QTest::newRow("optionProgressBar") << stylePtr(new QStyleOptionProgressBar) << false << int(QStyleOption::SO_ProgressBar);
       
    95     QTest::newRow("optionQ3DockWindow") << stylePtr(new QStyleOptionQ3DockWindow) << false << int(QStyleOption::SO_Q3DockWindow);
       
    96     QTest::newRow("optionQ3ListView") << stylePtr(new QStyleOptionQ3ListView) << true << int(QStyleOption::SO_Q3ListView);
       
    97     QTest::newRow("optionQ3ListViewItem") << stylePtr(new QStyleOptionQ3ListViewItem) << false << int(QStyleOption::SO_Q3ListViewItem);
       
    98     QTest::newRow("optionSlider") << stylePtr(new QStyleOptionSlider) << true << int(QStyleOption::SO_Slider);
       
    99     QTest::newRow("optionSpinBox") << stylePtr(new QStyleOptionSpinBox) << true << int(QStyleOption::SO_SpinBox);
       
   100     QTest::newRow("optionTab") << stylePtr(new QStyleOptionTab) << false << int(QStyleOption::SO_Tab);
       
   101     QTest::newRow("optionTitleBar") << stylePtr(new QStyleOptionTitleBar) << true << int(QStyleOption::SO_TitleBar);
       
   102     QTest::newRow("optionToolBox") << stylePtr(new QStyleOptionToolBox) << false << int(QStyleOption::SO_ToolBox);
       
   103     QTest::newRow("optionToolButton") << stylePtr(new QStyleOptionToolButton) << true << int(QStyleOption::SO_ToolButton);
       
   104     QTest::newRow("optionViewItem") << stylePtr(new QStyleOptionViewItem) << false << int(QStyleOption::SO_ViewItem);
       
   105     QTest::newRow("optionGraphicsItem") << stylePtr(new QStyleOptionGraphicsItem) << false << int(QStyleOption::SO_GraphicsItem);
       
   106 }
       
   107 
       
   108 void tst_QStyleOption::qstyleoptioncast()
       
   109 {
       
   110     QFETCH(StyleOptionPointerBase *, testOption);
       
   111     QFETCH(bool, canCastToComplex);
       
   112     QFETCH(int, type);
       
   113 
       
   114     QVERIFY(testOption->pointer != 0);
       
   115 
       
   116     QCOMPARE(testOption->pointer->type, type);
       
   117 
       
   118     // Cast to common base class
       
   119     QStyleOption *castOption = qstyleoption_cast<QStyleOption*>(testOption->pointer);
       
   120     QVERIFY(castOption != 0);
       
   121 
       
   122     // Cast to complex base class
       
   123     castOption = qstyleoption_cast<QStyleOptionComplex*>(testOption->pointer);
       
   124     QCOMPARE(canCastToComplex, (castOption != 0));
       
   125 
       
   126     // Cast to combo box
       
   127     castOption = qstyleoption_cast<QStyleOptionComboBox*>(testOption->pointer);
       
   128     QCOMPARE((castOption != 0),(testOption->pointer->type == QStyleOption::SO_ComboBox));
       
   129 
       
   130     // Cast to button
       
   131     castOption = qstyleoption_cast<QStyleOptionButton*>(testOption->pointer);
       
   132     QCOMPARE((castOption != 0),(testOption->pointer->type == QStyleOption::SO_Button));
       
   133 
       
   134     // Cast to lower version
       
   135     testOption->pointer->version += 1;
       
   136     castOption = qstyleoption_cast<QStyleOption*>(testOption->pointer);
       
   137     QVERIFY(castOption);
       
   138 
       
   139     // Cast a null pointer
       
   140     castOption = qstyleoption_cast<QStyleOption*>((QStyleOption*)0);
       
   141     QCOMPARE(castOption,(QStyleOption*)0);
       
   142 
       
   143     // Deallocate
       
   144     delete testOption;
       
   145 }
       
   146 
       
   147 void tst_QStyleOption::copyconstructors()
       
   148 {
       
   149     QStyleOptionFrame frame;
       
   150     QStyleOptionFrameV2 frame2(frame);
       
   151     QCOMPARE(frame2.version, int(QStyleOptionFrameV2::Version));
       
   152     frame2 = frame;
       
   153     QCOMPARE(frame2.version, int(QStyleOptionFrameV2::Version));
       
   154 
       
   155     QStyleOptionProgressBar bar;
       
   156     QStyleOptionProgressBarV2 bar2(bar);
       
   157     QCOMPARE(bar2.version, int(QStyleOptionProgressBarV2::Version));
       
   158     bar2 = bar;
       
   159     QCOMPARE(bar2.version, int(QStyleOptionProgressBarV2::Version));
       
   160 }
       
   161 
       
   162 QTEST_MAIN(tst_QStyleOption)
       
   163 #include "tst_qstyleoption.moc"
       
   164