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 <qwebelement.h> 23#include <qwebframe.h> 24#include <qwebview.h> 25#include <qpainter.h> 26 27/** 28 * Starts an event loop that runs until the given signal is received. 29 Optionally the event loop 30 * can return earlier on a timeout. 31 * 32 * \return \p true if the requested signal was received 33 * \p false on timeout 34 */ 35static bool waitForSignal(QObject* obj, const char* signal, int timeout = 0) 36{ 37 QEventLoop loop; 38 QObject::connect(obj, signal, &loop, SLOT(quit())); 39 QTimer timer; 40 QSignalSpy timeoutSpy(&timer, SIGNAL(timeout())); 41 if (timeout > 0) { 42 QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); 43 timer.setSingleShot(true); 44 timer.start(timeout); 45 } 46 loop.exec(); 47 return timeoutSpy.isEmpty(); 48} 49 50class tst_Painting : public QObject 51{ 52 Q_OBJECT 53 54public: 55 56public Q_SLOTS: 57 void init(); 58 void cleanup(); 59 60private Q_SLOTS: 61 void paint_data(); 62 void paint(); 63 void textAreas(); 64 65private: 66 QWebView* m_view; 67 QWebPage* m_page; 68}; 69 70void tst_Painting::init() 71{ 72 m_view = new QWebView; 73 m_page = m_view->page(); 74 75 QSize viewportSize(1024, 768); 76 m_view->setFixedSize(viewportSize); 77 m_page->setViewportSize(viewportSize); 78} 79 80void tst_Painting::cleanup() 81{ 82 delete m_view; 83} 84 85void tst_Painting::paint_data() 86{ 87 QTest::addColumn<QUrl>("url"); 88 QTest::newRow("amazon") << QUrl("http://www.amazon.com"); 89} 90 91void tst_Painting::paint() 92{ 93 QFETCH(QUrl, url); 94 95 m_view->load(url); 96 ::waitForSignal(m_view, SIGNAL(loadFinished(bool))); 97 98 /* force a layout */ 99 QWebFrame* mainFrame = m_page->mainFrame(); 100 mainFrame->toPlainText(); 101 102 QPixmap pixmap(m_page->viewportSize()); 103 QBENCHMARK { 104 QPainter painter(&pixmap); 105 mainFrame->render(&painter, QRect(QPoint(0, 0), m_page->viewportSize())); 106 painter.end(); 107 } 108} 109 110void tst_Painting::textAreas() 111{ 112 m_view->load(QUrl("data:text/html;<html><body></body></html>")); 113 ::waitForSignal(m_view, SIGNAL(loadFinished(bool))); 114 115 QWebElement bodyElement = m_page->mainFrame()->findFirstElement("body"); 116 117 int count = 100; 118 while (count--) { 119 QString markup("<textarea cols='1' rows='1'></textarea>"); 120 bodyElement.appendInside(markup); 121 } 122 123 /* force a layout */ 124 QWebFrame* mainFrame = m_page->mainFrame(); 125 mainFrame->toPlainText(); 126 127 QPixmap pixmap(mainFrame->contentsSize()); 128 QBENCHMARK { 129 QPainter painter(&pixmap); 130 mainFrame->render(&painter, QRect(QPoint(0, 0), mainFrame->contentsSize())); 131 painter.end(); 132 } 133} 134 135QTEST_MAIN(tst_Painting) 136#include "tst_painting.moc" 137