tests/auto/qmacstyle/tst_qmacstyle.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 <QtGui>
       
    45 
       
    46 #include <qmacstyle_mac.h>
       
    47 
       
    48 #ifdef Q_OS_MAC
       
    49 
       
    50 const int N = 1;
       
    51 
       
    52 //TESTED_CLASS=
       
    53 //TESTED_FILES=gui/styles/qmacstyle_mac.h gui/styles/qmacstyle_mac.cpp
       
    54 
       
    55 enum Size { Normal, Small, Mini };
       
    56 
       
    57 Q_DECLARE_METATYPE(Size);
       
    58 
       
    59 #define CT(E) \
       
    60     static const ControlType E = QSizePolicy::E;
       
    61 
       
    62 typedef QSizePolicy::ControlType ControlType;
       
    63 
       
    64 CT(DefaultType)
       
    65 CT(ButtonBox)
       
    66 CT(CheckBox)
       
    67 CT(ComboBox)
       
    68 CT(Frame)
       
    69 CT(GroupBox)
       
    70 CT(Label)
       
    71 CT(Line)
       
    72 CT(LineEdit)
       
    73 CT(PushButton)
       
    74 CT(RadioButton)
       
    75 CT(Slider)
       
    76 CT(SpinBox)
       
    77 CT(TabWidget)
       
    78 CT(ToolButton)
       
    79 
       
    80 
       
    81 class tst_QMacStyle : public QObject
       
    82 {
       
    83     Q_OBJECT
       
    84 
       
    85 public:
       
    86     tst_QMacStyle() { qRegisterMetaType<Size>("Size"); }
       
    87 
       
    88 private slots:
       
    89     void sizeHints_data();
       
    90     void sizeHints();
       
    91     void layoutMargins_data();
       
    92     void layoutMargins();
       
    93     void layoutSpacings_data();
       
    94     void layoutSpacings();
       
    95     void smallMiniNormalExclusivity_data();
       
    96     void smallMiniNormalExclusivity();
       
    97 
       
    98 private:
       
    99     static QSize msh(QWidget *widget);
       
   100     static QSize sh(QWidget *widget);
       
   101     static QRect geo(QWidget *widget);
       
   102     static QPoint pos(QWidget *widget) { return geo(widget).topLeft(); }
       
   103     static QSize size(QWidget *widget) { return geo(widget).size(); }
       
   104     static QSize gap(QWidget *widget1, QWidget *widget2);
       
   105     static int hgap(QWidget *widget1, QWidget *widget2) { return gap(widget1, widget2).width(); }
       
   106     static int vgap(QWidget *widget1, QWidget *widget2) { return gap(widget1, widget2).height(); }
       
   107     static void setSize(QWidget *widget, Size size);
       
   108     static int spacing(ControlType control1, ControlType control2, Qt::Orientation orientation,
       
   109                        QStyleOption *option = 0, QWidget *widget = 0);
       
   110     static int hspacing(ControlType control1, ControlType control2, Size size = Normal);
       
   111     static int vspacing(ControlType control1, ControlType control2, Size size = Normal);
       
   112 };
       
   113 
       
   114 #define SIZE(x, y, z) \
       
   115     ((size == Normal) ? (x) : (size == Small) ? (y) : (z))
       
   116 
       
   117 void tst_QMacStyle::sizeHints_data()
       
   118 {
       
   119     QTest::addColumn<Size>("size");
       
   120     QTest::newRow("normal") << Normal;
       
   121 //    QTest::newRow("small") << Small;
       
   122 //    QTest::newRow("mini") << Mini;
       
   123 }
       
   124 
       
   125 void tst_QMacStyle::sizeHints()
       
   126 {
       
   127     QFETCH(Size, size);    
       
   128     QDialog w;
       
   129     setSize(&w, size);
       
   130 
       
   131     QLineEdit lineEdit1(&w);
       
   132     QCOMPARE(sh(&lineEdit1).height(), SIZE(22, 19, 16));    // 16 in Builder, 15 in AHIG
       
   133 
       
   134     QProgressBar progress1(&w);
       
   135     progress1.setOrientation(Qt::Horizontal);
       
   136     qDebug() << "sh" << progress1.sizeHint();
       
   137     QCOMPARE(sh(&progress1).height(), SIZE(16, 10, 10));   // Builder
       
   138 
       
   139     progress1.setOrientation(Qt::Vertical);
       
   140     QCOMPARE(sh(&progress1).width(), SIZE(16, 10, 10));   // Builder
       
   141 
       
   142     QRadioButton radio1("Radio", &w);
       
   143     QCOMPARE(sh(&radio1).height(), SIZE(15, 12, 10));   // Builder
       
   144 
       
   145     QCheckBox checkBox1("Switch", &w);
       
   146     QCOMPARE(sh(&checkBox1).height(), SIZE(14, 12, 10));   // Builder
       
   147 
       
   148     QComboBox comboBox1(&w);
       
   149     comboBox1.setEditable(false);
       
   150     comboBox1.addItem("Foo");
       
   151     QCOMPARE(sh(&comboBox1).height(), SIZE(20, 17, 15));
       
   152 
       
   153     QComboBox comboBox2(&w);
       
   154     comboBox2.setEditable(true);
       
   155     comboBox2.addItem("Foo");
       
   156     QCOMPARE(sh(&comboBox2).height(), SIZE(20, 17, 15));
       
   157 
       
   158     // Combos in toolbars use the actual widget rect to
       
   159     // avoid faulty clipping:
       
   160     QToolBar tb;
       
   161     setSize(&tb, size);
       
   162     QComboBox comboBox3(&tb);
       
   163     comboBox3.addItem("Foo");
       
   164     QCOMPARE(sh(&comboBox3).height(), SIZE(26, -1, -1));
       
   165 
       
   166     QSlider slider1(Qt::Horizontal, &w);
       
   167     QCOMPARE(sh(&slider1).height(), SIZE(15, 12, 10));
       
   168 
       
   169     slider1.setTickPosition(QSlider::TicksAbove);
       
   170     QCOMPARE(sh(&slider1).height(), SIZE(24, 17, 16));  // Builder
       
   171 
       
   172     slider1.setTickPosition(QSlider::TicksBelow);
       
   173     QCOMPARE(sh(&slider1).height(), SIZE(24, 17, 16));  // Builder
       
   174 
       
   175     slider1.setTickPosition(QSlider::TicksBothSides);
       
   176     QVERIFY(sh(&slider1).height() > SIZE(15, 12, 10));  // common sense
       
   177 
       
   178     QPushButton ok1("OK", &w);
       
   179     QPushButton cancel1("Cancel", &w);
       
   180 
       
   181     QSize s1 = sh(&ok1);
       
   182     if (size == Normal) {
       
   183         // AHIG says 68, Builder does 70, and Qt seems to do 69
       
   184         QVERIFY(s1.width() >= 68 && s1.width() <= 70);
       
   185     }
       
   186     QCOMPARE(s1.height(), SIZE(20, 17, 14));    // 14 in Builder, 15 in AHIG
       
   187 
       
   188     // Cancel should be identical to OK, no matter what
       
   189     QCOMPARE(s1, sh(&cancel1));
       
   190 
       
   191     // Play with auto-default and default
       
   192     cancel1.setAutoDefault(false);
       
   193     QCOMPARE(s1, sh(&cancel1));
       
   194     cancel1.setAutoDefault(true);
       
   195     QCOMPARE(s1, sh(&cancel1));
       
   196     cancel1.setDefault(true);
       
   197     QCOMPARE(s1, sh(&cancel1));
       
   198 
       
   199     QDialogButtonBox bbox(&w);
       
   200     bbox.setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
       
   201     QCOMPARE(s1, sh(bbox.button(QDialogButtonBox::Ok)));
       
   202     QCOMPARE(s1, sh(bbox.button(QDialogButtonBox::Cancel)));
       
   203 
       
   204     QMessageBox mbox(&w);
       
   205     mbox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
       
   206     QCOMPARE(s1, sh(mbox.button(QMessageBox::Ok)));
       
   207     QCOMPARE(s1, sh(mbox.button(QMessageBox::Cancel)));
       
   208 
       
   209     /*
       
   210     QSpinBox spinBox1(&w);
       
   211     int h1 = sh(&spinBox1).height();
       
   212     QCOMPARE(h1, SIZE(22, 19, 15));
       
   213 
       
   214     QDateEdit date1(&w);
       
   215     QCOMPARE(sh(&date1).height(), h1);
       
   216 
       
   217     QTimeEdit time1(&w);
       
   218     QCOMPARE(sh(&time1).height(), h1);
       
   219 
       
   220     QDateTimeEdit dateTime1(&w);
       
   221     QCOMPARE(sh(&dateTime1).height(), h1);
       
   222 
       
   223     ok1.setAttribute(Qt::WA_MacMetalStyle, true);
       
   224     QSize s2 = sh(&ok1);
       
   225     if (size == Normal) {
       
   226         QVERIFY(s2.height() >= 21 && s2.height() <= 32);
       
   227     } else {
       
   228         QVERIFY(s2.height() >= 18 && s2.height() <= 24);
       
   229     }
       
   230     */
       
   231 
       
   232     // QMacStyle bug: label doesn't react to Small and Mini
       
   233     QLabel label1("Blah", &w);
       
   234     QCOMPARE(sh(&label1).height(), SIZE(17, 14, 11));
       
   235 }
       
   236 
       
   237 void tst_QMacStyle::layoutMargins_data()
       
   238 {
       
   239     tst_QMacStyle::sizeHints_data();
       
   240 }
       
   241 
       
   242 void tst_QMacStyle::layoutMargins()
       
   243 {
       
   244     QFETCH(Size, size);    
       
   245     QWidget w;
       
   246     setSize(&w, size);
       
   247 
       
   248 }
       
   249 
       
   250 void tst_QMacStyle::layoutSpacings_data()
       
   251 {
       
   252     tst_QMacStyle::sizeHints_data();
       
   253 }
       
   254 
       
   255 void tst_QMacStyle::layoutSpacings()
       
   256 {
       
   257     QFETCH(Size, size);
       
   258 
       
   259     /*
       
   260         Constraints specified by AHIG.
       
   261     */
       
   262 
       
   263     for (int i = 0; i < 4; ++i) {
       
   264         ControlType c1 = (i % 2 == 0) ? PushButton : ButtonBox;
       
   265         ControlType c2 = (i / 2 == 0) ? PushButton : ButtonBox;
       
   266         QCOMPARE(hspacing(c1, c2, size), SIZE(14, 8, 8));
       
   267         QCOMPARE(vspacing(c1, c2, size), SIZE(14, 8, 8));
       
   268     }
       
   269 
       
   270     QCOMPARE(hspacing(Label, RadioButton, size), SIZE(8, 6, 5));
       
   271     QCOMPARE(vspacing(RadioButton, RadioButton, size), SIZE(5, 5, 5));  // Builder, guess, AHIG
       
   272 
       
   273     QCOMPARE(hspacing(Label, CheckBox, size), SIZE(8, 6, 5));
       
   274     QCOMPARE(vspacing(CheckBox, CheckBox, size), SIZE(8, 8, 7));
       
   275 
       
   276     QCOMPARE(hspacing(Label, ComboBox, size), SIZE(8, 6, 5));
       
   277 
       
   278     QCOMPARE(hspacing(LineEdit, LineEdit, size), SIZE(10, 8, 8));
       
   279 
       
   280     /*
       
   281         Common sense constraints, for when AHIG seems to make no sense (e.g., disagrees
       
   282         too much with Builder or looks improper).
       
   283     */
       
   284 
       
   285     // Comboboxes are a special pain, because AHIG and Builder can't agree,
       
   286     // and because they can be editable or not, with two totally different looks
       
   287     QVERIFY(vspacing(ComboBox, ComboBox, size) >= SIZE(8, 6, 5));
       
   288     QVERIFY(vspacing(ComboBox, ComboBox, size) <= SIZE(12, 10, 8));
       
   289 
       
   290     // Make sure button boxes get the respect they deserve, when they occur
       
   291     // in the bottom or right side of a dialog
       
   292     QCOMPARE(hspacing(ButtonBox, LineEdit), SIZE(20, 8, 8));
       
   293     QCOMPARE(vspacing(ButtonBox, LineEdit), SIZE(20, 7, 7));
       
   294 
       
   295     QCOMPARE(hspacing(LineEdit, ButtonBox), SIZE(8, 8, 8));
       
   296     QCOMPARE(vspacing(LineEdit, ButtonBox), SIZE(8, 8, 8));
       
   297 }
       
   298 
       
   299 // helper functions
       
   300 
       
   301 QSize tst_QMacStyle::msh(QWidget *widget)
       
   302 {
       
   303     QWidgetItem item(widget);
       
   304     return item.sizeHint();
       
   305 }
       
   306 
       
   307 QSize tst_QMacStyle::sh(QWidget *widget)
       
   308 {
       
   309     QWidgetItem item(widget);
       
   310     return item.sizeHint();
       
   311 }
       
   312 
       
   313 QRect tst_QMacStyle::geo(QWidget *widget)
       
   314 {
       
   315     QWidgetItem item(widget);
       
   316     return item.geometry();
       
   317 }
       
   318 
       
   319 QSize tst_QMacStyle::gap(QWidget *widget1, QWidget *widget2)
       
   320 {
       
   321     QPoint d = pos(widget2) - pos(widget1);
       
   322     QSize s = size(widget1);
       
   323     return s + QSize(d.x(), d.y());
       
   324 }
       
   325 
       
   326 void tst_QMacStyle::setSize(QWidget *widget, Size size)
       
   327 {
       
   328     switch (size) {
       
   329     case Normal:
       
   330         QMacStyle::setWidgetSizePolicy(widget, QMacStyle::SizeDefault);
       
   331         break;
       
   332     case Small:
       
   333         QMacStyle::setWidgetSizePolicy(widget, QMacStyle::SizeSmall);
       
   334         break;
       
   335     case Mini:
       
   336         QMacStyle::setWidgetSizePolicy(widget, QMacStyle::SizeMini);
       
   337     }
       
   338 }
       
   339 
       
   340 int tst_QMacStyle::spacing(ControlType control1, ControlType control2, Qt::Orientation orientation,
       
   341                            QStyleOption *option, QWidget *widget)
       
   342 {
       
   343     return QApplication::style()->layoutSpacing(control1, control2, orientation, option, widget);
       
   344 }
       
   345 
       
   346 int tst_QMacStyle::hspacing(ControlType control1, ControlType control2, Size size)
       
   347 {
       
   348     QWidget w;
       
   349     setSize(&w, size);
       
   350 
       
   351     QStyleOption opt;
       
   352     opt.initFrom(&w);
       
   353 
       
   354     return spacing(control1, control2, Qt::Horizontal, &opt);
       
   355 }
       
   356 
       
   357 int tst_QMacStyle::vspacing(ControlType control1, ControlType control2, Size size)
       
   358 {
       
   359     QWidget w;
       
   360     setSize(&w, size);
       
   361 
       
   362     QStyleOption opt;
       
   363     opt.initFrom(&w);
       
   364 
       
   365     return spacing(control1, control2, Qt::Vertical, &opt);
       
   366 }
       
   367 
       
   368 
       
   369 void tst_QMacStyle::smallMiniNormalExclusivity_data()
       
   370 {
       
   371 
       
   372     QTest::addColumn<int>("size1");
       
   373     QTest::addColumn<int>("size2");
       
   374     QTest::addColumn<int>("size3");
       
   375     QTest::addColumn<int>("expectedHeight1");
       
   376     QTest::addColumn<int>("expectedHeight2");
       
   377     QTest::addColumn<int>("expectedHeight3");
       
   378 
       
   379     QTest::newRow("normal small mini") << int(Qt::WA_MacNormalSize) << int(Qt::WA_MacSmallSize) << int(Qt::WA_MacMiniSize) << 32 << 16 << 24;
       
   380     QTest::newRow("normal mini small") << int(Qt::WA_MacNormalSize) <<int(Qt::WA_MacMiniSize) << int(Qt::WA_MacSmallSize) << 32 << 24 << 16;
       
   381     QTest::newRow("small normal mini") << int(Qt::WA_MacSmallSize) << int(Qt::WA_MacNormalSize) << int(Qt::WA_MacMiniSize) << 16 << 32 << 24;
       
   382     QTest::newRow("small mini normal") << int(Qt::WA_MacSmallSize) << int(Qt::WA_MacMiniSize) << int(Qt::WA_MacNormalSize) << 16 << 24 << 32;
       
   383     QTest::newRow("mini small normal") << int(Qt::WA_MacMiniSize) << int(Qt::WA_MacSmallSize) << int(Qt::WA_MacNormalSize) << 24 << 16 << 32;
       
   384     QTest::newRow("mini normal small") << int(Qt::WA_MacMiniSize) << int(Qt::WA_MacNormalSize) << int(Qt::WA_MacSmallSize) << 24 << 32 << 16;
       
   385 }
       
   386 
       
   387 void tst_QMacStyle::smallMiniNormalExclusivity()
       
   388 {
       
   389 
       
   390     QFETCH(int, size1);
       
   391     QFETCH(int, size2);
       
   392     QFETCH(int, size3);
       
   393     QFETCH(int, expectedHeight1);
       
   394     QFETCH(int, expectedHeight2);
       
   395     QFETCH(int, expectedHeight3);
       
   396 
       
   397     static const int TotalSizes = 3;
       
   398     int attrs[TotalSizes] = { size1, size2, size3 };
       
   399     int expected[TotalSizes] = { expectedHeight1, expectedHeight2, expectedHeight3 };
       
   400 
       
   401     QPushButton dummyWidget;
       
   402     QStyleOptionButton opt;
       
   403 
       
   404     for (int i = 0; i < TotalSizes; ++i) {
       
   405         dummyWidget.setAttribute(Qt::WidgetAttribute(attrs[i]));
       
   406         opt.initFrom(&dummyWidget);
       
   407         QSize size = dummyWidget.style()->sizeFromContents(QStyle::CT_PushButton, &opt,
       
   408                                                            QSize(0, 0), &dummyWidget);
       
   409         QCOMPARE(size.height(), expected[i]);
       
   410     }
       
   411 }
       
   412 
       
   413 QTEST_MAIN(tst_QMacStyle)
       
   414 
       
   415 #else
       
   416 
       
   417 QTEST_NOOP_MAIN
       
   418 
       
   419 #endif
       
   420 
       
   421 #include "tst_qmacstyle.moc"
       
   422