tests/auto/qscrollarea/tst_qscrollarea.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 <qscrollarea.h>
       
    48 #include <qlayout.h>
       
    49 
       
    50 //TESTED_CLASS=
       
    51 //TESTED_FILES=
       
    52 
       
    53 class tst_QScrollArea : public QObject
       
    54 {
       
    55 Q_OBJECT
       
    56 
       
    57 public:
       
    58     tst_QScrollArea();
       
    59     virtual ~tst_QScrollArea();
       
    60 
       
    61 private slots:
       
    62     void getSetCheck();
       
    63     void ensureMicroFocusVisible_Task_167838();
       
    64     void checkHFW_Task_197736();
       
    65 };
       
    66 
       
    67 tst_QScrollArea::tst_QScrollArea()
       
    68 {
       
    69 }
       
    70 
       
    71 tst_QScrollArea::~tst_QScrollArea()
       
    72 {
       
    73 }
       
    74 
       
    75 // Testing get/set functions
       
    76 void tst_QScrollArea::getSetCheck()
       
    77 {
       
    78     QScrollArea obj1;
       
    79     // QWidget * QScrollArea::widget()
       
    80     // void QScrollArea::setWidget(QWidget *)
       
    81     QWidget *var1 = new QWidget();
       
    82     obj1.setWidget(var1);
       
    83     QCOMPARE(var1, obj1.widget());
       
    84     obj1.setWidget((QWidget *)0);
       
    85     QCOMPARE(var1, obj1.widget()); // Cannot set a 0-widget. Old widget returned
       
    86     // delete var1; // No delete, since QScrollArea takes ownership
       
    87 
       
    88     // bool QScrollArea::widgetResizable()
       
    89     // void QScrollArea::setWidgetResizable(bool)
       
    90     obj1.setWidgetResizable(false);
       
    91     QCOMPARE(false, obj1.widgetResizable());
       
    92     obj1.setWidgetResizable(true);
       
    93     QCOMPARE(true, obj1.widgetResizable());
       
    94 }
       
    95 
       
    96 class WidgetWithMicroFocus : public QWidget
       
    97 {
       
    98 public:
       
    99     WidgetWithMicroFocus(QWidget *parent = 0) : QWidget(parent)
       
   100     {
       
   101         setBackgroundRole(QPalette::Dark);
       
   102     }
       
   103 protected:
       
   104     QVariant inputMethodQuery(Qt::InputMethodQuery query) const
       
   105     {
       
   106         if (query == Qt::ImMicroFocus)
       
   107             return QRect(width() / 2, height() / 2, 5, 5);
       
   108         return QWidget::inputMethodQuery(query);
       
   109     }
       
   110 //     void paintEvent(QPaintEvent *event)
       
   111 //     {
       
   112 //         QPainter painter(this);
       
   113 //         painter.fillRect(rect(), QBrush(Qt::red));
       
   114 //     }
       
   115 };
       
   116 
       
   117 void tst_QScrollArea::ensureMicroFocusVisible_Task_167838()
       
   118 {
       
   119     QScrollArea scrollArea;
       
   120     scrollArea.resize(100, 100);
       
   121     scrollArea.show();
       
   122     QWidget *parent = new QWidget;
       
   123     parent->setLayout(new QVBoxLayout);
       
   124     QWidget *child = new WidgetWithMicroFocus;
       
   125     parent->layout()->addWidget(child);
       
   126     parent->resize(300, 300); 
       
   127     scrollArea.setWidget(parent);
       
   128     scrollArea.ensureWidgetVisible(child, 10, 10);
       
   129     QRect microFocus = child->inputMethodQuery(Qt::ImMicroFocus).toRect();
       
   130     QPoint p = child->mapTo(scrollArea.viewport(), microFocus.topLeft());
       
   131     microFocus.translate(p - microFocus.topLeft());
       
   132     QVERIFY(scrollArea.viewport()->rect().contains(microFocus));
       
   133 }
       
   134 
       
   135 class HFWWidget : public QWidget
       
   136 {
       
   137     public:
       
   138         HFWWidget();
       
   139         int heightForWidth(int w) const;
       
   140 };
       
   141 
       
   142 HFWWidget::HFWWidget()
       
   143     : QWidget()
       
   144 {
       
   145     setMinimumSize(QSize(100,50));
       
   146     QSizePolicy s = sizePolicy();
       
   147     s.setHeightForWidth(true);
       
   148     setSizePolicy(s);
       
   149 }
       
   150 
       
   151 int HFWWidget::heightForWidth(int w) const
       
   152 {
       
   153     // Mimic a label - the narrower we are, the taller we have to be
       
   154     if (w > 0)
       
   155         return 40000 / w;
       
   156     else
       
   157         return 40000;
       
   158 }
       
   159 
       
   160 void tst_QScrollArea::checkHFW_Task_197736()
       
   161 {
       
   162     QScrollArea scrollArea;
       
   163     HFWWidget *w = new HFWWidget;
       
   164     scrollArea.resize(200,100);
       
   165     scrollArea.show();
       
   166     scrollArea.setWidgetResizable(true);
       
   167     scrollArea.setWidget(w);
       
   168 
       
   169     // at 200x100px, we expect HFW to be 200px tall, not 100px
       
   170     QVERIFY(w->height() >= 200);
       
   171 
       
   172     // at 200x300px, we expect HFW to be 300px tall (the heightForWidth is a min, not prescribed)
       
   173     scrollArea.resize(QSize(200, 300));
       
   174     QVERIFY(w->height() >= 250); // 50px for a fudge factor (size of frame margins/scrollbars etc)
       
   175 
       
   176     // make sure this only happens with widget resizable
       
   177     scrollArea.setWidgetResizable(false);
       
   178     scrollArea.resize(QSize(100,100));
       
   179     w->resize(QSize(200,200));
       
   180     QVERIFY(w->width() == 200);
       
   181     QVERIFY(w->height() == 200);
       
   182 }
       
   183 
       
   184 QTEST_MAIN(tst_QScrollArea)
       
   185 #include "tst_qscrollarea.moc"