WebKit/qt/tests/qwebview/tst_qwebview.cpp
changeset 2 303757a437d3
parent 0 4f2f89ce4247
equal deleted inserted replaced
0:4f2f89ce4247 2:303757a437d3
     1 /*
       
     2     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
       
     3     Copyright (C) 2009 Torch Mobile Inc.
       
     4     Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
       
     5 
       
     6     This library is free software; you can redistribute it and/or
       
     7     modify it under the terms of the GNU Library General Public
       
     8     License as published by the Free Software Foundation; either
       
     9     version 2 of the License, or (at your option) any later version.
       
    10 
       
    11     This library is distributed in the hope that it will be useful,
       
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14     Library General Public License for more details.
       
    15 
       
    16     You should have received a copy of the GNU Library General Public License
       
    17     along with this library; see the file COPYING.LIB.  If not, write to
       
    18     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    19     Boston, MA 02110-1301, USA.
       
    20 */
       
    21 
       
    22 #include <qtest.h>
       
    23 #include "../util.h"
       
    24 
       
    25 #include <qpainter.h>
       
    26 #include <qwebview.h>
       
    27 #include <qwebpage.h>
       
    28 #include <qnetworkrequest.h>
       
    29 #include <qdiriterator.h>
       
    30 #include <qwebkitversion.h>
       
    31 #include <qwebframe.h>
       
    32 
       
    33 class tst_QWebView : public QObject
       
    34 {
       
    35     Q_OBJECT
       
    36 
       
    37 public slots:
       
    38     void initTestCase();
       
    39     void cleanupTestCase();
       
    40     void init();
       
    41     void cleanup();
       
    42 
       
    43 private slots:
       
    44     void renderHints();
       
    45     void getWebKitVersion();
       
    46 
       
    47     void reusePage_data();
       
    48     void reusePage();
       
    49     void microFocusCoordinates();
       
    50     void focusInputTypes();
       
    51 
       
    52     void crashTests();
       
    53 };
       
    54 
       
    55 class WebView : public QWebView
       
    56 {
       
    57     Q_OBJECT
       
    58 
       
    59 public:
       
    60     void fireMouseClick(QPoint point) {
       
    61         QMouseEvent presEv(QEvent::MouseButtonPress, point, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
       
    62         QMouseEvent relEv(QEvent::MouseButtonRelease, point, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
       
    63         QWebView::mousePressEvent(&presEv);
       
    64         QWebView::mousePressEvent(&relEv);
       
    65     }
       
    66 
       
    67 };
       
    68 
       
    69 // This will be called before the first test function is executed.
       
    70 // It is only called once.
       
    71 void tst_QWebView::initTestCase()
       
    72 {
       
    73 }
       
    74 
       
    75 // This will be called after the last test function is executed.
       
    76 // It is only called once.
       
    77 void tst_QWebView::cleanupTestCase()
       
    78 {
       
    79 }
       
    80 
       
    81 // This will be called before each test function is executed.
       
    82 void tst_QWebView::init()
       
    83 {
       
    84 }
       
    85 
       
    86 // This will be called after every test function.
       
    87 void tst_QWebView::cleanup()
       
    88 {
       
    89 }
       
    90 
       
    91 void tst_QWebView::renderHints()
       
    92 {
       
    93     QWebView webView;
       
    94 
       
    95     // default is only text antialiasing
       
    96     QVERIFY(!(webView.renderHints() & QPainter::Antialiasing));
       
    97     QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
       
    98     QVERIFY(!(webView.renderHints() & QPainter::SmoothPixmapTransform));
       
    99     QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
       
   100 
       
   101     webView.setRenderHint(QPainter::Antialiasing, true);
       
   102     QVERIFY(webView.renderHints() & QPainter::Antialiasing);
       
   103     QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
       
   104     QVERIFY(!(webView.renderHints() & QPainter::SmoothPixmapTransform));
       
   105     QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
       
   106 
       
   107     webView.setRenderHint(QPainter::Antialiasing, false);
       
   108     QVERIFY(!(webView.renderHints() & QPainter::Antialiasing));
       
   109     QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
       
   110     QVERIFY(!(webView.renderHints() & QPainter::SmoothPixmapTransform));
       
   111     QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
       
   112 
       
   113     webView.setRenderHint(QPainter::SmoothPixmapTransform, true);
       
   114     QVERIFY(!(webView.renderHints() & QPainter::Antialiasing));
       
   115     QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
       
   116     QVERIFY(webView.renderHints() & QPainter::SmoothPixmapTransform);
       
   117     QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
       
   118 
       
   119     webView.setRenderHint(QPainter::SmoothPixmapTransform, false);
       
   120     QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
       
   121     QVERIFY(!(webView.renderHints() & QPainter::SmoothPixmapTransform));
       
   122     QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
       
   123 }
       
   124 
       
   125 void tst_QWebView::getWebKitVersion()
       
   126 {
       
   127     QVERIFY(qWebKitVersion().toDouble() > 0);
       
   128 }
       
   129 
       
   130 void tst_QWebView::reusePage_data()
       
   131 {
       
   132     QTest::addColumn<QString>("html");
       
   133     QTest::newRow("WithoutPlugin") << "<html><body id='b'>text</body></html>";
       
   134     QTest::newRow("WindowedPlugin") << QString("<html><body id='b'>text<embed src='resources/test.swf'></embed></body></html>");
       
   135     QTest::newRow("WindowlessPlugin") << QString("<html><body id='b'>text<embed src='resources/test.swf' wmode=\"transparent\"></embed></body></html>");
       
   136 }
       
   137 
       
   138 void tst_QWebView::reusePage()
       
   139 {
       
   140     if (!QDir(TESTS_SOURCE_DIR).exists())
       
   141         QSKIP(QString("This test requires access to resources found in '%1'").arg(TESTS_SOURCE_DIR).toLatin1().constData(), SkipAll);
       
   142 
       
   143     QDir::setCurrent(TESTS_SOURCE_DIR);
       
   144 
       
   145     QFETCH(QString, html);
       
   146     QWebView* view1 = new QWebView;
       
   147     QPointer<QWebPage> page = new QWebPage;
       
   148     view1->setPage(page);
       
   149     page->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
       
   150     QWebFrame* mainFrame = page->mainFrame();
       
   151     mainFrame->setHtml(html, QUrl::fromLocalFile(TESTS_SOURCE_DIR));
       
   152     if (html.contains("</embed>")) {
       
   153         // some reasonable time for the PluginStream to feed test.swf to flash and start painting
       
   154         waitForSignal(view1, SIGNAL(loadFinished(bool)), 2000);
       
   155     }
       
   156 
       
   157     view1->show();
       
   158 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
       
   159     QTest::qWaitForWindowShown(view1);
       
   160 #else
       
   161     QTest::qWait(2000); 
       
   162 #endif
       
   163     delete view1;
       
   164     QVERIFY(page != 0); // deleting view must not have deleted the page, since it's not a child of view
       
   165 
       
   166     QWebView *view2 = new QWebView;
       
   167     view2->setPage(page);
       
   168     view2->show(); // in Windowless mode, you should still be able to see the plugin here
       
   169 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
       
   170     QTest::qWaitForWindowShown(view2);
       
   171 #else
       
   172     QTest::qWait(2000); 
       
   173 #endif
       
   174     delete view2;
       
   175 
       
   176     delete page; // must not crash
       
   177 
       
   178     QDir::setCurrent(QApplication::applicationDirPath());
       
   179 }
       
   180 
       
   181 // Class used in crashTests
       
   182 class WebViewCrashTest : public QObject {
       
   183     Q_OBJECT
       
   184     QWebView* m_view;
       
   185 public:
       
   186     bool m_executed;
       
   187 
       
   188 
       
   189     WebViewCrashTest(QWebView* view)
       
   190       : m_view(view)
       
   191       , m_executed(false)
       
   192     {
       
   193         view->connect(view, SIGNAL(loadProgress(int)), this, SLOT(loading(int)));
       
   194     }
       
   195 
       
   196 private slots:
       
   197     void loading(int progress)
       
   198     {
       
   199         if (progress >= 20 && progress < 90) {
       
   200             QVERIFY(!m_executed);
       
   201             m_view->stop();
       
   202             m_executed = true;
       
   203         }
       
   204     }
       
   205 };
       
   206 
       
   207 
       
   208 // Should not crash.
       
   209 void tst_QWebView::crashTests()
       
   210 {
       
   211     // Test if loading can be stopped in loadProgress handler without crash.
       
   212     // Test page should have frames.
       
   213     QWebView view;
       
   214     WebViewCrashTest tester(&view);
       
   215     QUrl url("qrc:///resources/index.html");
       
   216     view.load(url);
       
   217     QTRY_VERIFY(tester.m_executed); // If fail it means that the test wasn't executed.
       
   218 }
       
   219 
       
   220 void tst_QWebView::microFocusCoordinates()
       
   221 {
       
   222     QWebPage* page = new QWebPage;
       
   223     QWebView* webView = new QWebView;
       
   224     webView->setPage( page );
       
   225 
       
   226     page->mainFrame()->setHtml("<html><body>" \
       
   227         "<input type='text' id='input1' style='font--family: serif' value='' maxlength='20'/><br>" \
       
   228         "<canvas id='canvas1' width='500' height='500'/>" \
       
   229         "<input type='password'/><br>" \
       
   230         "<canvas id='canvas2' width='500' height='500'/>" \
       
   231         "</body></html>");
       
   232 
       
   233     page->mainFrame()->setFocus();
       
   234 
       
   235     QVariant initialMicroFocus = page->inputMethodQuery(Qt::ImMicroFocus);
       
   236     QVERIFY(initialMicroFocus.isValid());
       
   237 
       
   238     page->mainFrame()->scroll(0,50);
       
   239 
       
   240     QVariant currentMicroFocus = page->inputMethodQuery(Qt::ImMicroFocus);
       
   241     QVERIFY(currentMicroFocus.isValid());
       
   242 
       
   243     QCOMPARE(initialMicroFocus.toRect().translated(QPoint(0,-50)), currentMicroFocus.toRect());
       
   244 }
       
   245 
       
   246 void tst_QWebView::focusInputTypes()
       
   247 {
       
   248     QWebPage* page = new QWebPage;
       
   249     WebView* webView = new WebView;
       
   250     webView->setPage( page );
       
   251 
       
   252     QCoreApplication::processEvents();
       
   253     QUrl url("qrc:///resources/input_types.html");
       
   254     page->mainFrame()->load(url);
       
   255     page->mainFrame()->setFocus();
       
   256 
       
   257     QVERIFY(waitForSignal(page, SIGNAL(loadFinished(bool))));
       
   258 
       
   259     // 'text' type
       
   260     webView->fireMouseClick(QPoint(20, 10));
       
   261 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) || defined(Q_OS_SYMBIAN)
       
   262     QVERIFY(webView->inputMethodHints() & Qt::ImhNoAutoUppercase);
       
   263     QVERIFY(webView->inputMethodHints() & Qt::ImhNoPredictiveText);
       
   264 #else
       
   265     QVERIFY(webView->inputMethodHints() == Qt::ImhNone);
       
   266 #endif
       
   267     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   268 
       
   269     // 'password' field
       
   270     webView->fireMouseClick(QPoint(20, 60));
       
   271     QVERIFY(webView->inputMethodHints() == Qt::ImhHiddenText);
       
   272     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   273 
       
   274     // 'tel' field
       
   275     webView->fireMouseClick(QPoint(20, 110));
       
   276     QVERIFY(webView->inputMethodHints() == Qt::ImhDialableCharactersOnly);
       
   277     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   278 
       
   279     // 'number' field
       
   280     webView->fireMouseClick(QPoint(20, 160));
       
   281     QVERIFY(webView->inputMethodHints() == Qt::ImhDigitsOnly);
       
   282     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   283 
       
   284     // 'email' field
       
   285     webView->fireMouseClick(QPoint(20, 210));
       
   286     QVERIFY(webView->inputMethodHints() == Qt::ImhEmailCharactersOnly);
       
   287     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   288 
       
   289     // 'url' field
       
   290     webView->fireMouseClick(QPoint(20, 260));
       
   291     QVERIFY(webView->inputMethodHints() == Qt::ImhUrlCharactersOnly);
       
   292     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   293 
       
   294     // 'password' field
       
   295     webView->fireMouseClick(QPoint(20, 60));
       
   296     QVERIFY(webView->inputMethodHints() == Qt::ImhHiddenText);
       
   297     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   298 
       
   299     // 'text' type
       
   300     webView->fireMouseClick(QPoint(20, 10));
       
   301 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) || defined(Q_OS_SYMBIAN)
       
   302     QVERIFY(webView->inputMethodHints() & Qt::ImhNoAutoUppercase);
       
   303     QVERIFY(webView->inputMethodHints() & Qt::ImhNoPredictiveText);
       
   304 #else
       
   305     QVERIFY(webView->inputMethodHints() == Qt::ImhNone);
       
   306 #endif
       
   307     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   308 
       
   309     // 'password' field
       
   310     webView->fireMouseClick(QPoint(20, 60));
       
   311     QVERIFY(webView->inputMethodHints() == Qt::ImhHiddenText);
       
   312     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   313 
       
   314     qWarning("clicking on text area");
       
   315     // 'text area' field
       
   316     webView->fireMouseClick(QPoint(20, 320));
       
   317     QVERIFY(webView->inputMethodHints() == Qt::ImhNone);
       
   318     QVERIFY(webView->testAttribute(Qt::WA_InputMethodEnabled));
       
   319 
       
   320     delete webView;
       
   321 
       
   322 }
       
   323 
       
   324 QTEST_MAIN(tst_QWebView)
       
   325 #include "tst_qwebview.moc"
       
   326