Home | History | Annotate | Download | only in painting
      1 /*
      2  * Copyright (C) 2009 Holger Hans Peter Freyther
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  */
     19 
     20 #include <QtTest/QtTest>
     21 
     22 #include <qwebframe.h>
     23 #include <qwebview.h>
     24 #include <qpainter.h>
     25 
     26 /**
     27  * Starts an event loop that runs until the given signal is received.
     28  Optionally the event loop
     29  * can return earlier on a timeout.
     30  *
     31  * \return \p true if the requested signal was received
     32  *         \p false on timeout
     33  */
     34 static bool waitForSignal(QObject* obj, const char* signal, int timeout = 0)
     35 {
     36     QEventLoop loop;
     37     QObject::connect(obj, signal, &loop, SLOT(quit()));
     38     QTimer timer;
     39     QSignalSpy timeoutSpy(&timer, SIGNAL(timeout()));
     40     if (timeout > 0) {
     41         QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
     42         timer.setSingleShot(true);
     43         timer.start(timeout);
     44     }
     45     loop.exec();
     46     return timeoutSpy.isEmpty();
     47 }
     48 
     49 class tst_Painting : public QObject
     50 {
     51     Q_OBJECT
     52 
     53 public:
     54 
     55 public Q_SLOTS:
     56     void init();
     57     void cleanup();
     58 
     59 private Q_SLOTS:
     60     void paint_data();
     61     void paint();
     62 
     63 private:
     64     QWebView* m_view;
     65     QWebPage* m_page;
     66 };
     67 
     68 void tst_Painting::init()
     69 {
     70     m_view = new QWebView;
     71     m_page = m_view->page();
     72 
     73     QSize viewportSize(1024, 768);
     74     m_view->setFixedSize(viewportSize);
     75     m_page->setViewportSize(viewportSize);
     76 }
     77 
     78 void tst_Painting::cleanup()
     79 {
     80     delete m_view;
     81 }
     82 
     83 void tst_Painting::paint_data()
     84 {
     85     QTest::addColumn<QUrl>("url");
     86     QTest::newRow("amazon") << QUrl("http://www.amazon.com");
     87 }
     88 
     89 void tst_Painting::paint()
     90 {
     91     QFETCH(QUrl, url);
     92 
     93     m_view->load(url);
     94     ::waitForSignal(m_view, SIGNAL(loadFinished(bool)));
     95 
     96     /* force a layout */
     97     QWebFrame* mainFrame = m_page->mainFrame();
     98     mainFrame->toPlainText();
     99 
    100     QPixmap pixmap(m_page->viewportSize());
    101     QBENCHMARK {
    102         QPainter painter(&pixmap);
    103         mainFrame->render(&painter, QRect(QPoint(0, 0), m_page->viewportSize()));
    104         painter.end();
    105     }
    106 }
    107 
    108 QTEST_MAIN(tst_Painting)
    109 #include "tst_painting.moc"
    110