tests/benchmarks/qtableview/tst_qtableview.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 #include <qtest.h>
       
    43 #include <QDebug>
       
    44 #include <QTableView>
       
    45 #include <QImage>
       
    46 #include <QPainter>
       
    47 
       
    48 //TESTED_FILES=
       
    49 
       
    50 class QtTestTableModel: public QAbstractTableModel
       
    51 {
       
    52     Q_OBJECT
       
    53 
       
    54 
       
    55 public:
       
    56     QtTestTableModel(int rows = 0, int columns = 0, QObject *parent = 0)
       
    57         : QAbstractTableModel(parent),
       
    58           row_count(rows),
       
    59           column_count(columns) {}
       
    60 
       
    61     int rowCount(const QModelIndex& = QModelIndex()) const { return row_count; }
       
    62     int columnCount(const QModelIndex& = QModelIndex()) const { return column_count; }
       
    63     bool isEditable(const QModelIndex &) const { return true; }
       
    64 
       
    65     QVariant data(const QModelIndex &idx, int role) const
       
    66     {
       
    67         if (!idx.isValid() || idx.row() >= row_count || idx.column() >= column_count) {
       
    68             qWarning() << "Invalid modelIndex [%d,%d,%p]" << idx;
       
    69             return QVariant();
       
    70         }
       
    71 
       
    72         if (role == Qt::DisplayRole || role == Qt::EditRole)
       
    73             return QString("[%1,%2,%3]").arg(idx.row()).arg(idx.column()).arg(0);
       
    74 
       
    75         return QVariant();
       
    76     }
       
    77 
       
    78     bool insertRows(int start, int count, const QModelIndex &parent = QModelIndex())
       
    79     {
       
    80         if (start < 0 || start > row_count)
       
    81             return false;
       
    82 
       
    83         beginInsertRows(parent, start, start + count - 1);
       
    84         row_count += count;
       
    85         endInsertRows();
       
    86         return true;
       
    87     }
       
    88 
       
    89     bool removeRows(int start, int count, const QModelIndex &parent = QModelIndex())
       
    90     {
       
    91         if (start < 0 || start >= row_count || row_count < count)
       
    92             return false;
       
    93 
       
    94         beginRemoveRows(parent, start, start + count - 1);
       
    95         row_count -= count;
       
    96         endRemoveRows();
       
    97         return true;
       
    98     }
       
    99 
       
   100     bool insertColumns(int start, int count, const QModelIndex &parent = QModelIndex())
       
   101     {
       
   102         if (start < 0 || start > column_count)
       
   103             return false;
       
   104 
       
   105         beginInsertColumns(parent, start, start + count - 1);
       
   106         column_count += count;
       
   107         endInsertColumns();
       
   108         return true;
       
   109     }
       
   110 
       
   111     bool removeColumns(int start, int count, const QModelIndex &parent = QModelIndex())
       
   112     {
       
   113         if (start < 0 || start >= column_count || column_count < count)
       
   114             return false;
       
   115 
       
   116         beginRemoveColumns(parent, start, start + count - 1);
       
   117         column_count -= count;
       
   118         endRemoveColumns();
       
   119         return true;
       
   120     }
       
   121 
       
   122     int row_count;
       
   123     int column_count;
       
   124 };
       
   125 
       
   126 
       
   127 
       
   128 
       
   129 class tst_QTableView : public QObject
       
   130 {
       
   131     Q_OBJECT
       
   132 
       
   133 public:
       
   134     tst_QTableView();
       
   135     virtual ~tst_QTableView();
       
   136 
       
   137 public slots:
       
   138     void init();
       
   139     void cleanup();
       
   140 
       
   141 private slots:
       
   142     void spanInit();
       
   143     void spanDraw();
       
   144     void spanSelectColumn();
       
   145     void spanSelectAll();
       
   146     void rowInsertion_data();
       
   147     void rowInsertion();
       
   148     void rowRemoval_data();
       
   149     void rowRemoval();
       
   150     void columnInsertion_data();
       
   151     void columnInsertion();
       
   152     void columnRemoval_data();
       
   153     void columnRemoval();
       
   154 private:
       
   155     static inline void spanInit_helper(QTableView *);
       
   156 };
       
   157 
       
   158 tst_QTableView::tst_QTableView()
       
   159 {
       
   160 }
       
   161 
       
   162 tst_QTableView::~tst_QTableView()
       
   163 {
       
   164 }
       
   165 
       
   166 void tst_QTableView::init()
       
   167 {
       
   168 }
       
   169 
       
   170 void tst_QTableView::cleanup()
       
   171 {
       
   172 }
       
   173 
       
   174 void tst_QTableView::spanInit_helper(QTableView *view)
       
   175 {
       
   176     for (int i=0; i < 40; i++) {
       
   177         view->setSpan(1+i%2, 1+4*i, 1+i%3, 2);
       
   178     }
       
   179     
       
   180     for (int i=1; i < 40; i++) {
       
   181         view->setSpan(6 + i*7, 4, 4, 50);
       
   182     }
       
   183 }
       
   184 
       
   185 void tst_QTableView::spanInit()
       
   186 {
       
   187     QtTestTableModel model(500, 500);
       
   188     QTableView v;
       
   189     v.setModel(&model);
       
   190     
       
   191     QBENCHMARK {
       
   192         spanInit_helper(&v);
       
   193     }
       
   194 }
       
   195 
       
   196 void tst_QTableView::spanDraw()
       
   197 {
       
   198     QtTestTableModel model(500, 500);
       
   199     QTableView v;
       
   200     v.setModel(&model);
       
   201     
       
   202     spanInit_helper(&v);
       
   203     v.show();
       
   204     v.resize(500,500);
       
   205     QTest::qWait(30);
       
   206 
       
   207     QImage image(500, 500, QImage::Format_ARGB32_Premultiplied);
       
   208     QPainter painter(&image);
       
   209     QBENCHMARK {
       
   210         v.render(&painter);
       
   211     }
       
   212 }
       
   213 
       
   214 void tst_QTableView::spanSelectAll()
       
   215 {
       
   216     QtTestTableModel model(500, 500);
       
   217     QTableView v;
       
   218     v.setModel(&model);
       
   219     
       
   220     spanInit_helper(&v);
       
   221     v.show();
       
   222     QTest::qWait(30);
       
   223     
       
   224     QBENCHMARK {
       
   225         v.selectAll();
       
   226     }
       
   227 }
       
   228 
       
   229 void tst_QTableView::spanSelectColumn()
       
   230 {
       
   231     QtTestTableModel model(500, 500);
       
   232     QTableView v;
       
   233     v.setModel(&model);
       
   234     
       
   235     spanInit_helper(&v);
       
   236     v.show();
       
   237     QTest::qWait(30);
       
   238     
       
   239     QBENCHMARK {
       
   240         v.selectColumn(22);
       
   241     }
       
   242 }
       
   243 
       
   244 typedef QVector<QRect> SpanList;
       
   245 Q_DECLARE_METATYPE(SpanList)
       
   246 
       
   247 void spansData()
       
   248 {
       
   249     QTest::addColumn<SpanList>("spans");
       
   250 
       
   251     QTest::newRow("Without spans")
       
   252         << SpanList();
       
   253 
       
   254     QTest::newRow("With spans")
       
   255             << (SpanList()
       
   256                   << QRect(0, 1, 1, 2)
       
   257                   << QRect(1, 2, 1, 2)
       
   258                   << QRect(2, 2, 1, 5)
       
   259                   << QRect(2, 8, 1, 2)
       
   260                   << QRect(3, 4, 1, 2)
       
   261                   << QRect(4, 4, 1, 4)
       
   262                   << QRect(5, 6, 1, 3)
       
   263                   << QRect(6, 7, 1, 3));
       
   264 }
       
   265 
       
   266 void tst_QTableView::rowInsertion_data()
       
   267 {
       
   268     spansData();
       
   269 }
       
   270 
       
   271 void tst_QTableView::rowInsertion()
       
   272 {
       
   273     QFETCH(SpanList, spans);
       
   274 
       
   275     QtTestTableModel model(10, 10);
       
   276     QTableView view;
       
   277     view.setModel(&model);
       
   278 
       
   279     foreach (QRect span, spans)
       
   280         view.setSpan(span.top(), span.left(), span.height(), span.width());
       
   281     view.show();
       
   282     QTest::qWait(50);
       
   283 
       
   284     QBENCHMARK_ONCE {
       
   285         view.model()->insertRows(0, 2);
       
   286         view.model()->insertRows(5, 2);
       
   287         view.model()->insertRows(8, 2);
       
   288         view.model()->insertRows(12, 2);
       
   289     }
       
   290 }
       
   291 
       
   292 void tst_QTableView::rowRemoval_data()
       
   293 {
       
   294     spansData();
       
   295 }
       
   296 
       
   297 void tst_QTableView::rowRemoval()
       
   298 {
       
   299     QFETCH(SpanList, spans);
       
   300 
       
   301     QtTestTableModel model(10, 10);
       
   302     QTableView view;
       
   303     view.setModel(&model);
       
   304 
       
   305     foreach (QRect span, spans)
       
   306         view.setSpan(span.top(), span.left(), span.height(), span.width());
       
   307     view.show();
       
   308     QTest::qWait(50);
       
   309 
       
   310     QBENCHMARK_ONCE {
       
   311         view.model()->removeRows(3, 3);
       
   312     }
       
   313 }
       
   314 
       
   315 void tst_QTableView::columnInsertion_data()
       
   316 {
       
   317     spansData();
       
   318 }
       
   319 
       
   320 void tst_QTableView::columnInsertion()
       
   321 {
       
   322     QFETCH(SpanList, spans);
       
   323 
       
   324     QtTestTableModel model(10, 10);
       
   325     QTableView view;
       
   326     view.setModel(&model);
       
   327 
       
   328     // Same set as for rowInsertion, just swapping columns and rows.
       
   329     foreach (QRect span, spans)
       
   330         view.setSpan(span.left(), span.top(), span.width(), span.height());
       
   331     view.show();
       
   332     QTest::qWait(50);
       
   333 
       
   334     QBENCHMARK_ONCE {
       
   335         view.model()->insertColumns(0, 2);
       
   336         view.model()->insertColumns(5, 2);
       
   337         view.model()->insertColumns(8, 2);
       
   338         view.model()->insertColumns(12, 2);
       
   339     }
       
   340 }
       
   341 
       
   342 void tst_QTableView::columnRemoval_data()
       
   343 {
       
   344     spansData();
       
   345 }
       
   346 
       
   347 void tst_QTableView::columnRemoval()
       
   348 {
       
   349     QFETCH(SpanList, spans);
       
   350 
       
   351     QtTestTableModel model(10, 10);
       
   352     QTableView view;
       
   353     view.setModel(&model);
       
   354 
       
   355     // Same set as for rowRemoval, just swapping columns and rows.
       
   356     foreach (QRect span, spans)
       
   357         view.setSpan(span.left(), span.top(), span.width(), span.height());
       
   358     view.show();
       
   359     QTest::qWait(50);
       
   360 
       
   361     QBENCHMARK_ONCE {
       
   362         view.model()->removeColumns(3, 3);
       
   363     }
       
   364 }
       
   365 
       
   366 QTEST_MAIN(tst_QTableView)
       
   367 #include "tst_qtableview.moc"