2 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3 Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
4 Copyright (C) 2010 Holger Hans Peter Freyther
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.
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.
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.
23 #include "../WebCoreSupport/DumpRenderTreeSupportQt.h"
26 #include <QGraphicsWidget>
28 #include <QMainWindow>
30 #include <QPushButton>
32 #include <QtTest/QtTest>
33 #include <QTextCharFormat>
34 #include <qgraphicsscene.h>
35 #include <qgraphicsview.h>
36 #include <qgraphicswebview.h>
37 #include <qnetworkcookiejar.h>
38 #include <qnetworkrequest.h>
39 #include <qwebdatabase.h>
40 #include <qwebelement.h>
41 #include <qwebframe.h>
42 #include <qwebhistory.h>
44 #include <qwebsecurityorigin.h>
46 #include <qimagewriter.h>
48 class EventSpy : public QObject, public QList<QEvent::Type>
52 EventSpy(QObject* objectToSpy)
54 objectToSpy->installEventFilter(this);
57 virtual bool eventFilter(QObject* receiver, QEvent* event)
59 append(event->type());
64 class tst_QWebPage : public QObject
70 virtual ~tst_QWebPage();
79 void cleanupTestCase();
81 void contextMenuCopy();
82 void acceptNavigationRequest();
83 void geolocationRequestJS();
85 void acceptNavigationRequestWithNewWindow();
86 void userStyleSheet();
87 void loadHtml5Video();
89 void contextMenuCrash();
90 void updatePositionDependentActionsCrash();
92 void createPluginWithPluginsEnabled();
93 void createPluginWithPluginsDisabled();
94 void destroyPlugin_data();
96 void createViewlessPlugin_data();
97 void createViewlessPlugin();
98 void graphicsWidgetPlugin();
99 void multiplePageGroupsAndLocalStorage();
100 void cursorMovements();
101 void textSelection();
103 void backActionUpdate();
106 void loadCachedPage();
107 void protectBindingsRuntimeObjectsFromCollector();
108 void localURLSchemes();
109 void testOptionalJSObjects();
110 void testEnablePersistentStorage();
111 void consoleOutput();
112 void inputMethods_data();
114 void inputMethodsTextFormat_data();
115 void inputMethodsTextFormat();
116 void defaultTextEncoding();
117 void errorPageExtension();
118 void errorPageExtensionInIFrames();
119 void errorPageExtensionInFrameset();
120 void userAgentApplicationName();
124 void crashTests_LazyInitializationOfMainFrame();
126 void screenshot_data();
129 #if defined(ENABLE_WEBGL) && ENABLE_WEBGL
130 void acceleratedWebGLScreenshotWithoutView();
131 void unacceleratedWebGLScreenshotWithoutView();
134 void originatingObjectInNetworkRequests();
136 void showModalDialog();
137 void testStopScheduledPageRefresh();
139 void supportedContentType();
140 void infiniteLoopJS();
141 void navigatorCookieEnabled();
142 void deleteQWebViewTwice();
143 void renderOnRepaintRequestedShouldNotRecurse();
146 void macCopyUnicodeToClipboard();
154 tst_QWebPage::tst_QWebPage()
158 tst_QWebPage::~tst_QWebPage()
162 void tst_QWebPage::init()
164 m_view = new QWebView();
165 m_page = m_view->page();
168 void tst_QWebPage::cleanup()
173 void tst_QWebPage::cleanupFiles()
175 QFile::remove("Databases.db");
176 QDir::current().rmdir("http_www.myexample.com_0");
177 QFile::remove("http_www.myexample.com_0.localstorage");
180 void tst_QWebPage::initTestCase()
182 cleanupFiles(); // In case there are old files from previous runs
185 void tst_QWebPage::cleanupTestCase()
187 cleanupFiles(); // Be nice
190 class NavigationRequestOverride : public QWebPage
193 NavigationRequestOverride(QWebView* parent, bool initialValue) : QWebPage(parent), m_acceptNavigationRequest(initialValue) {}
195 bool m_acceptNavigationRequest;
197 virtual bool acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, QWebPage::NavigationType type) {
202 return m_acceptNavigationRequest;
206 void tst_QWebPage::acceptNavigationRequest()
208 QSignalSpy loadSpy(m_view, SIGNAL(loadFinished(bool)));
210 NavigationRequestOverride* newPage = new NavigationRequestOverride(m_view, false);
211 m_view->setPage(newPage);
213 m_view->setHtml(QString("<html><body><form name='tstform' action='data:text/html,foo'method='get'>"
214 "<input type='text'><input type='submit'></form></body></html>"), QUrl());
215 QTRY_COMPARE(loadSpy.count(), 1);
217 m_view->page()->mainFrame()->evaluateJavaScript("tstform.submit();");
219 newPage->m_acceptNavigationRequest = true;
220 m_view->page()->mainFrame()->evaluateJavaScript("tstform.submit();");
221 QTRY_COMPARE(loadSpy.count(), 2);
223 QCOMPARE(m_view->page()->mainFrame()->toPlainText(), QString("foo?"));
225 // Restore default page
229 class JSTestPage : public QWebPage
233 JSTestPage(QObject* parent = 0)
234 : QWebPage(parent) {}
237 bool shouldInterruptJavaScript() {
240 void requestPermission(QWebFrame* frame, QWebPage::Feature feature)
242 if (m_allowGeolocation)
243 setFeaturePermission(frame, feature, PermissionGrantedByUser);
245 setFeaturePermission(frame, feature, PermissionDeniedByUser);
249 void setGeolocationPermission(bool allow)
251 m_allowGeolocation = allow;
255 bool m_allowGeolocation;
258 void tst_QWebPage::infiniteLoopJS()
260 JSTestPage* newPage = new JSTestPage(m_view);
261 m_view->setPage(newPage);
262 m_view->setHtml(QString("<html><body>test</body></html>"), QUrl());
263 m_view->page()->mainFrame()->evaluateJavaScript("var run = true;var a = 1;while(run){a++;}");
267 void tst_QWebPage::geolocationRequestJS()
269 JSTestPage* newPage = new JSTestPage(m_view);
271 if (newPage->mainFrame()->evaluateJavaScript(QLatin1String("!navigator.geolocation")).toBool()) {
273 QSKIP("Geolocation is not supported.", SkipSingle);
276 connect(newPage, SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)),
277 newPage, SLOT(requestPermission(QWebFrame*, QWebPage::Feature)));
279 newPage->setGeolocationPermission(false);
280 m_view->setPage(newPage);
281 m_view->setHtml(QString("<html><body>test</body></html>"), QUrl());
282 m_view->page()->mainFrame()->evaluateJavaScript("var errorCode = 0; function error(err) { errorCode = err.code; } function success(pos) { } navigator.geolocation.getCurrentPosition(success, error)");
284 QVariant empty = m_view->page()->mainFrame()->evaluateJavaScript("errorCode");
286 QVERIFY(empty.type() == QVariant::Double && empty.toInt() != 0);
288 newPage->setGeolocationPermission(true);
289 m_view->page()->mainFrame()->evaluateJavaScript("errorCode = 0; navigator.geolocation.getCurrentPosition(success, error);");
290 empty = m_view->page()->mainFrame()->evaluateJavaScript("errorCode");
292 //http://dev.w3.org/geo/api/spec-source.html#position
293 //PositionError: const unsigned short PERMISSION_DENIED = 1;
294 QVERIFY(empty.type() == QVariant::Double && empty.toInt() != 1);
298 void tst_QWebPage::loadFinished()
300 qRegisterMetaType<QWebFrame*>("QWebFrame*");
301 qRegisterMetaType<QNetworkRequest*>("QNetworkRequest*");
302 QSignalSpy spyLoadStarted(m_view, SIGNAL(loadStarted()));
303 QSignalSpy spyLoadFinished(m_view, SIGNAL(loadFinished(bool)));
305 m_view->page()->mainFrame()->load(QUrl("data:text/html,<frameset cols=\"25%,75%\"><frame src=\"data:text/html,"
306 "<head><meta http-equiv='refresh' content='1'></head>foo \">"
307 "<frame src=\"data:text/html,bar\"></frameset>"));
308 QTRY_COMPARE(spyLoadFinished.count(), 1);
310 QTRY_VERIFY(spyLoadStarted.count() > 1);
311 QTRY_VERIFY(spyLoadFinished.count() > 1);
313 spyLoadFinished.clear();
315 m_view->page()->mainFrame()->load(QUrl("data:text/html,<frameset cols=\"25%,75%\"><frame src=\"data:text/html,"
316 "foo \"><frame src=\"data:text/html,bar\"></frameset>"));
317 QTRY_COMPARE(spyLoadFinished.count(), 1);
318 QCOMPARE(spyLoadFinished.count(), 1);
321 class ConsolePage : public QWebPage
324 ConsolePage(QObject* parent = 0) : QWebPage(parent) {}
326 virtual void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID)
328 messages.append(message);
329 lineNumbers.append(lineNumber);
330 sourceIDs.append(sourceID);
333 QStringList messages;
334 QList<int> lineNumbers;
335 QStringList sourceIDs;
338 void tst_QWebPage::consoleOutput()
341 page.mainFrame()->evaluateJavaScript("this is not valid JavaScript");
342 QCOMPARE(page.messages.count(), 1);
343 QCOMPARE(page.lineNumbers.at(0), 1);
346 class TestPage : public QWebPage
349 TestPage(QObject* parent = 0) : QWebPage(parent) {}
352 QPointer<QWebFrame> frame;
353 QNetworkRequest request;
357 QList<Navigation> navigations;
358 QList<QWebPage*> createdWindows;
360 virtual bool acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, NavigationType type) {
365 navigations.append(n);
369 virtual QWebPage* createWindow(WebWindowType) {
370 QWebPage* page = new TestPage(this);
371 createdWindows.append(page);
376 void tst_QWebPage::acceptNavigationRequestWithNewWindow()
378 TestPage* page = new TestPage(m_view);
379 page->settings()->setAttribute(QWebSettings::LinksIncludedInFocusChain, true);
381 m_view->setPage(m_page);
383 m_view->setUrl(QString("data:text/html,<a href=\"data:text/html,Reached\" target=\"_blank\">Click me</a>"));
384 QVERIFY(::waitForSignal(m_view, SIGNAL(loadFinished(bool))));
386 QFocusEvent fe(QEvent::FocusIn);
389 QVERIFY(m_page->focusNextPrevChild(/*next*/ true));
391 QKeyEvent keyEnter(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
392 m_page->event(&keyEnter);
394 QCOMPARE(page->navigations.count(), 2);
396 TestPage::Navigation n = page->navigations.at(1);
397 QVERIFY(n.frame.isNull());
398 QCOMPARE(n.request.url().toString(), QString("data:text/html,Reached"));
399 QVERIFY(n.type == QWebPage::NavigationTypeLinkClicked);
401 QCOMPARE(page->createdWindows.count(), 1);
404 class TestNetworkManager : public QNetworkAccessManager
407 TestNetworkManager(QObject* parent) : QNetworkAccessManager(parent) {}
409 QList<QUrl> requestedUrls;
410 QList<QNetworkRequest> requests;
413 virtual QNetworkReply* createRequest(Operation op, const QNetworkRequest &request, QIODevice* outgoingData) {
414 requests.append(request);
415 requestedUrls.append(request.url());
416 return QNetworkAccessManager::createRequest(op, request, outgoingData);
420 void tst_QWebPage::userStyleSheet()
422 TestNetworkManager* networkManager = new TestNetworkManager(m_page);
423 m_page->setNetworkAccessManager(networkManager);
424 networkManager->requestedUrls.clear();
426 m_page->settings()->setUserStyleSheetUrl(QUrl("data:text/css;charset=utf-8;base64,"
427 + QByteArray("p { background-image: url('http://does.not/exist.png');}").toBase64()));
428 m_view->setHtml("<p>hello world</p>");
429 QVERIFY(::waitForSignal(m_view, SIGNAL(loadFinished(bool))));
431 QVERIFY(networkManager->requestedUrls.count() >= 1);
432 QCOMPARE(networkManager->requestedUrls.at(0), QUrl("http://does.not/exist.png"));
435 void tst_QWebPage::loadHtml5Video()
437 #if defined(WTF_USE_QT_MULTIMEDIA) && WTF_USE_QT_MULTIMEDIA
438 QByteArray url("http://does.not/exist?a=1%2Cb=2");
439 m_view->setHtml("<p><video id ='video' src='" + url + "' autoplay/></p>");
441 QUrl mUrl = DumpRenderTreeSupportQt::mediaContentUrlByElementId(m_page->mainFrame(), "video");
442 QCOMPARE(mUrl.toEncoded(), url);
444 QSKIP("This test requires Qt Multimedia", SkipAll);
448 void tst_QWebPage::viewModes()
450 m_view->setHtml("<body></body>");
451 m_page->setProperty("_q_viewMode", "minimized");
453 QVariant empty = m_page->mainFrame()->evaluateJavaScript("window.styleMedia.matchMedium(\"(-webkit-view-mode)\")");
454 QVERIFY(empty.type() == QVariant::Bool && empty.toBool());
456 QVariant minimized = m_page->mainFrame()->evaluateJavaScript("window.styleMedia.matchMedium(\"(-webkit-view-mode: minimized)\")");
457 QVERIFY(minimized.type() == QVariant::Bool && minimized.toBool());
459 QVariant maximized = m_page->mainFrame()->evaluateJavaScript("window.styleMedia.matchMedium(\"(-webkit-view-mode: maximized)\")");
460 QVERIFY(maximized.type() == QVariant::Bool && !maximized.toBool());
463 void tst_QWebPage::modified()
465 m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>blub"));
466 QVERIFY(::waitForSignal(m_view, SIGNAL(loadFinished(bool))));
468 m_page->mainFrame()->setUrl(QUrl("data:text/html,<body id=foo contenteditable>blah"));
469 QVERIFY(::waitForSignal(m_view, SIGNAL(loadFinished(bool))));
471 QVERIFY(!m_page->isModified());
473 // m_page->mainFrame()->evaluateJavaScript("alert(document.getElementById('foo'))");
474 m_page->mainFrame()->evaluateJavaScript("document.getElementById('foo').focus()");
475 m_page->mainFrame()->evaluateJavaScript("document.execCommand('InsertText', true, 'Test');");
477 QVERIFY(m_page->isModified());
479 m_page->mainFrame()->evaluateJavaScript("document.execCommand('Undo', true);");
481 QVERIFY(!m_page->isModified());
483 m_page->mainFrame()->evaluateJavaScript("document.execCommand('Redo', true);");
485 QVERIFY(m_page->isModified());
487 QVERIFY(m_page->history()->canGoBack());
488 QVERIFY(!m_page->history()->canGoForward());
489 QCOMPARE(m_page->history()->count(), 2);
490 QVERIFY(m_page->history()->backItem().isValid());
491 QVERIFY(!m_page->history()->forwardItem().isValid());
493 m_page->history()->back();
494 QVERIFY(::waitForSignal(m_view, SIGNAL(loadFinished(bool))));
496 QVERIFY(!m_page->history()->canGoBack());
497 QVERIFY(m_page->history()->canGoForward());
499 QVERIFY(!m_page->isModified());
501 QVERIFY(m_page->history()->currentItemIndex() == 0);
503 m_page->history()->setMaximumItemCount(3);
504 QVERIFY(m_page->history()->maximumItemCount() == 3);
506 QVariant variant("string test");
507 m_page->history()->currentItem().setUserData(variant);
508 QVERIFY(m_page->history()->currentItem().userData().toString() == "string test");
510 m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>This is second page"));
511 m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>This is third page"));
512 QVERIFY(m_page->history()->count() == 2);
513 m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>This is fourth page"));
514 QVERIFY(m_page->history()->count() == 2);
515 m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>This is fifth page"));
516 QVERIFY(::waitForSignal(m_page, SIGNAL(saveFrameStateRequested(QWebFrame*,QWebHistoryItem*))));
519 // https://bugs.webkit.org/show_bug.cgi?id=51331
520 void tst_QWebPage::updatePositionDependentActionsCrash()
523 view.setHtml("<p>test");
525 view.page()->updatePositionDependentActions(pos);
526 QMenu* contextMenu = 0;
527 foreach (QObject* child, view.children()) {
528 contextMenu = qobject_cast<QMenu*>(child);
532 QVERIFY(!contextMenu);
535 // https://bugs.webkit.org/show_bug.cgi?id=20357
536 void tst_QWebPage::contextMenuCrash()
539 view.setHtml("<p>test");
541 QContextMenuEvent event(QContextMenuEvent::Mouse, pos);
542 view.page()->swallowContextMenuEvent(&event);
543 view.page()->updatePositionDependentActions(pos);
544 QMenu* contextMenu = 0;
545 foreach (QObject* child, view.children()) {
546 contextMenu = qobject_cast<QMenu*>(child);
550 QVERIFY(contextMenu);
554 void tst_QWebPage::database()
556 QString path = QDir::currentPath();
557 m_page->settings()->setOfflineStoragePath(path);
558 QVERIFY(m_page->settings()->offlineStoragePath() == path);
560 QWebSettings::setOfflineStorageDefaultQuota(1024 * 1024);
561 QVERIFY(QWebSettings::offlineStorageDefaultQuota() == 1024 * 1024);
563 m_page->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
564 m_page->settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
566 QString dbFileName = path + "Databases.db";
568 if (QFile::exists(dbFileName))
569 QFile::remove(dbFileName);
571 qRegisterMetaType<QWebFrame*>("QWebFrame*");
572 QSignalSpy spy(m_page, SIGNAL(databaseQuotaExceeded(QWebFrame*,QString)));
573 m_view->setHtml(QString("<html><head><script>var db; db=openDatabase('testdb', '1.0', 'test database API', 50000); </script></head><body><div></div></body></html>"), QUrl("http://www.myexample.com"));
574 QTRY_COMPARE(spy.count(), 1);
575 m_page->mainFrame()->evaluateJavaScript("var db2; db2=openDatabase('testdb', '1.0', 'test database API', 50000);");
576 QTRY_COMPARE(spy.count(),1);
578 m_page->mainFrame()->evaluateJavaScript("localStorage.test='This is a test for local storage';");
579 m_view->setHtml(QString("<html><body id='b'>text</body></html>"), QUrl("http://www.myexample.com"));
581 QVariant s1 = m_page->mainFrame()->evaluateJavaScript("localStorage.test");
582 QCOMPARE(s1.toString(), QString("This is a test for local storage"));
584 m_page->mainFrame()->evaluateJavaScript("sessionStorage.test='This is a test for session storage';");
585 m_view->setHtml(QString("<html><body id='b'>text</body></html>"), QUrl("http://www.myexample.com"));
586 QVariant s2 = m_page->mainFrame()->evaluateJavaScript("sessionStorage.test");
587 QCOMPARE(s2.toString(), QString("This is a test for session storage"));
589 m_view->setHtml(QString("<html><head></head><body><div></div></body></html>"), QUrl("http://www.myexample.com"));
590 m_page->mainFrame()->evaluateJavaScript("var db3; db3=openDatabase('testdb', '1.0', 'test database API', 50000);db3.transaction(function(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS Test (text TEXT)', []); }, function(tx, result) { }, function(tx, error) { });");
593 // Remove all databases.
594 QWebSecurityOrigin origin = m_page->mainFrame()->securityOrigin();
595 QList<QWebDatabase> dbs = origin.databases();
596 for (int i = 0; i < dbs.count(); i++) {
597 QString fileName = dbs[i].fileName();
598 QVERIFY(QFile::exists(fileName));
599 QWebDatabase::removeDatabase(dbs[i]);
600 QVERIFY(!QFile::exists(fileName));
602 QVERIFY(!origin.databases().size());
603 // Remove removed test :-)
604 QWebDatabase::removeAllDatabases();
605 QVERIFY(!origin.databases().size());
608 class PluginPage : public QWebPage
611 PluginPage(QObject *parent = 0)
612 : QWebPage(parent) {}
616 CallInfo(const QString &c, const QUrl &u,
617 const QStringList &pn, const QStringList &pv,
619 : classid(c), url(u), paramNames(pn),
620 paramValues(pv), returnValue(r)
624 QStringList paramNames;
625 QStringList paramValues;
626 QObject *returnValue;
629 QList<CallInfo> calls;
632 virtual QObject *createPlugin(const QString &classid, const QUrl &url,
633 const QStringList ¶mNames,
634 const QStringList ¶mValues)
637 if (classid == "pushbutton")
638 result = new QPushButton();
639 #ifndef QT_NO_INPUTDIALOG
640 else if (classid == "lineedit")
641 result = new QLineEdit();
643 else if (classid == "graphicswidget")
644 result = new QGraphicsWidget();
646 result->setObjectName(classid);
647 calls.append(CallInfo(classid, url, paramNames, paramValues, result));
652 static void createPlugin(QWebView *view)
654 QSignalSpy loadSpy(view, SIGNAL(loadFinished(bool)));
656 PluginPage* newPage = new PluginPage(view);
657 view->setPage(newPage);
659 // type has to be application/x-qt-plugin
660 view->setHtml(QString("<html><body><object type='application/x-foobarbaz' classid='pushbutton' id='mybutton'/></body></html>"));
661 QTRY_COMPARE(loadSpy.count(), 1);
662 QCOMPARE(newPage->calls.count(), 0);
664 view->setHtml(QString("<html><body><object type='application/x-qt-plugin' classid='pushbutton' id='mybutton'/></body></html>"));
665 QTRY_COMPARE(loadSpy.count(), 2);
666 QCOMPARE(newPage->calls.count(), 1);
668 PluginPage::CallInfo ci = newPage->calls.takeFirst();
669 QCOMPARE(ci.classid, QString::fromLatin1("pushbutton"));
670 QCOMPARE(ci.url, QUrl());
671 QCOMPARE(ci.paramNames.count(), 3);
672 QCOMPARE(ci.paramValues.count(), 3);
673 QCOMPARE(ci.paramNames.at(0), QString::fromLatin1("type"));
674 QCOMPARE(ci.paramValues.at(0), QString::fromLatin1("application/x-qt-plugin"));
675 QCOMPARE(ci.paramNames.at(1), QString::fromLatin1("classid"));
676 QCOMPARE(ci.paramValues.at(1), QString::fromLatin1("pushbutton"));
677 QCOMPARE(ci.paramNames.at(2), QString::fromLatin1("id"));
678 QCOMPARE(ci.paramValues.at(2), QString::fromLatin1("mybutton"));
679 QVERIFY(ci.returnValue != 0);
680 QVERIFY(ci.returnValue->inherits("QPushButton"));
683 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("document.getElementById('mybutton').toString()").toString(),
684 QString::fromLatin1("[object HTMLObjectElement]"));
685 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mybutton.toString()").toString(),
686 QString::fromLatin1("[object HTMLObjectElement]"));
687 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("typeof mybutton.objectName").toString(),
688 QString::fromLatin1("string"));
689 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mybutton.objectName").toString(),
690 QString::fromLatin1("pushbutton"));
691 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("typeof mybutton.clicked").toString(),
692 QString::fromLatin1("function"));
693 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mybutton.clicked.toString()").toString(),
694 QString::fromLatin1("function clicked() {\n [native code]\n}"));
696 view->setHtml(QString("<html><body><table>"
697 "<tr><object type='application/x-qt-plugin' classid='lineedit' id='myedit'/></tr>"
698 "<tr><object type='application/x-qt-plugin' classid='pushbutton' id='mybutton'/></tr>"
699 "</table></body></html>"), QUrl("http://foo.bar.baz"));
700 QTRY_COMPARE(loadSpy.count(), 3);
701 QCOMPARE(newPage->calls.count(), 2);
703 PluginPage::CallInfo ci = newPage->calls.takeFirst();
704 QCOMPARE(ci.classid, QString::fromLatin1("lineedit"));
705 QCOMPARE(ci.url, QUrl());
706 QCOMPARE(ci.paramNames.count(), 3);
707 QCOMPARE(ci.paramValues.count(), 3);
708 QCOMPARE(ci.paramNames.at(0), QString::fromLatin1("type"));
709 QCOMPARE(ci.paramValues.at(0), QString::fromLatin1("application/x-qt-plugin"));
710 QCOMPARE(ci.paramNames.at(1), QString::fromLatin1("classid"));
711 QCOMPARE(ci.paramValues.at(1), QString::fromLatin1("lineedit"));
712 QCOMPARE(ci.paramNames.at(2), QString::fromLatin1("id"));
713 QCOMPARE(ci.paramValues.at(2), QString::fromLatin1("myedit"));
714 QVERIFY(ci.returnValue != 0);
715 QVERIFY(ci.returnValue->inherits("QLineEdit"));
718 PluginPage::CallInfo ci = newPage->calls.takeFirst();
719 QCOMPARE(ci.classid, QString::fromLatin1("pushbutton"));
720 QCOMPARE(ci.url, QUrl());
721 QCOMPARE(ci.paramNames.count(), 3);
722 QCOMPARE(ci.paramValues.count(), 3);
723 QCOMPARE(ci.paramNames.at(0), QString::fromLatin1("type"));
724 QCOMPARE(ci.paramValues.at(0), QString::fromLatin1("application/x-qt-plugin"));
725 QCOMPARE(ci.paramNames.at(1), QString::fromLatin1("classid"));
726 QCOMPARE(ci.paramValues.at(1), QString::fromLatin1("pushbutton"));
727 QCOMPARE(ci.paramNames.at(2), QString::fromLatin1("id"));
728 QCOMPARE(ci.paramValues.at(2), QString::fromLatin1("mybutton"));
729 QVERIFY(ci.returnValue != 0);
730 QVERIFY(ci.returnValue->inherits("QPushButton"));
734 void tst_QWebPage::graphicsWidgetPlugin()
736 m_view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
737 QGraphicsWebView webView;
739 QSignalSpy loadSpy(&webView, SIGNAL(loadFinished(bool)));
741 PluginPage* newPage = new PluginPage(&webView);
742 webView.setPage(newPage);
744 // type has to be application/x-qt-plugin
745 webView.setHtml(QString("<html><body><object type='application/x-foobarbaz' classid='graphicswidget' id='mygraphicswidget'/></body></html>"));
746 QTRY_COMPARE(loadSpy.count(), 1);
747 QCOMPARE(newPage->calls.count(), 0);
749 webView.setHtml(QString("<html><body><object type='application/x-qt-plugin' classid='graphicswidget' id='mygraphicswidget'/></body></html>"));
750 QTRY_COMPARE(loadSpy.count(), 2);
751 QCOMPARE(newPage->calls.count(), 1);
753 PluginPage::CallInfo ci = newPage->calls.takeFirst();
754 QCOMPARE(ci.classid, QString::fromLatin1("graphicswidget"));
755 QCOMPARE(ci.url, QUrl());
756 QCOMPARE(ci.paramNames.count(), 3);
757 QCOMPARE(ci.paramValues.count(), 3);
758 QCOMPARE(ci.paramNames.at(0), QString::fromLatin1("type"));
759 QCOMPARE(ci.paramValues.at(0), QString::fromLatin1("application/x-qt-plugin"));
760 QCOMPARE(ci.paramNames.at(1), QString::fromLatin1("classid"));
761 QCOMPARE(ci.paramValues.at(1), QString::fromLatin1("graphicswidget"));
762 QCOMPARE(ci.paramNames.at(2), QString::fromLatin1("id"));
763 QCOMPARE(ci.paramValues.at(2), QString::fromLatin1("mygraphicswidget"));
764 QVERIFY(ci.returnValue);
765 QVERIFY(ci.returnValue->inherits("QGraphicsWidget"));
768 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("document.getElementById('mygraphicswidget').toString()").toString(),
769 QString::fromLatin1("[object HTMLObjectElement]"));
770 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mygraphicswidget.toString()").toString(),
771 QString::fromLatin1("[object HTMLObjectElement]"));
772 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("typeof mygraphicswidget.objectName").toString(),
773 QString::fromLatin1("string"));
774 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mygraphicswidget.objectName").toString(),
775 QString::fromLatin1("graphicswidget"));
776 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("typeof mygraphicswidget.geometryChanged").toString(),
777 QString::fromLatin1("function"));
778 QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mygraphicswidget.geometryChanged.toString()").toString(),
779 QString::fromLatin1("function geometryChanged() {\n [native code]\n}"));
782 void tst_QWebPage::createPluginWithPluginsEnabled()
784 m_view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
785 createPlugin(m_view);
788 void tst_QWebPage::createPluginWithPluginsDisabled()
790 // Qt Plugins should be loaded by QtWebKit even when PluginsEnabled is
791 // false. The client decides whether a Qt plugin is enabled or not when
792 // it decides whether or not to instantiate it.
793 m_view->settings()->setAttribute(QWebSettings::PluginsEnabled, false);
794 createPlugin(m_view);
797 // Standard base class for template PluginTracerPage. In tests it is used as interface.
798 class PluginCounterPage : public QWebPage {
801 QPointer<QObject> m_widget;
802 QObject* m_pluginParent;
803 PluginCounterPage(QObject* parent = 0)
809 settings()->setAttribute(QWebSettings::PluginsEnabled, true);
814 m_pluginParent->deleteLater();
819 class PluginTracerPage : public PluginCounterPage {
821 PluginTracerPage(QObject* parent = 0)
822 : PluginCounterPage(parent)
824 // this is a dummy parent object for the created plugin
825 m_pluginParent = new T;
827 virtual QObject* createPlugin(const QString&, const QUrl&, const QStringList&, const QStringList&)
831 // need a cast to the specific type, as QObject::setParent cannot be called,
832 // because it is not virtual. Instead it is necesary to call QWidget::setParent,
833 // which also takes a QWidget* instead of a QObject*. Therefore we need to
834 // upcast to T*, which is a QWidget.
835 static_cast<T*>(m_widget.data())->setParent(static_cast<T*>(m_pluginParent));
840 class PluginFactory {
842 enum FactoredType {QWidgetType, QGraphicsWidgetType};
843 static PluginCounterPage* create(FactoredType type, QObject* parent = 0)
845 PluginCounterPage* result = 0;
848 result = new PluginTracerPage<QWidget>(parent);
850 case QGraphicsWidgetType:
851 result = new PluginTracerPage<QGraphicsWidget>(parent);
858 static void prepareTestData()
860 QTest::addColumn<int>("type");
861 QTest::newRow("QWidget") << (int)PluginFactory::QWidgetType;
862 QTest::newRow("QGraphicsWidget") << (int)PluginFactory::QGraphicsWidgetType;
866 void tst_QWebPage::destroyPlugin_data()
868 PluginFactory::prepareTestData();
871 void tst_QWebPage::destroyPlugin()
874 PluginCounterPage* page = PluginFactory::create((PluginFactory::FactoredType)type, m_view);
875 m_view->setPage(page);
877 // we create the plugin, so the widget should be constructed
878 QString content("<html><body><object type=\"application/x-qt-plugin\" classid=\"QProgressBar\"></object></body></html>");
879 m_view->setHtml(content);
880 QVERIFY(page->m_widget);
881 QCOMPARE(page->m_count, 1);
883 // navigate away, the plugin widget should be destructed
884 m_view->setHtml("<html><body>Hi</body></html>");
885 QTestEventLoop::instance().enterLoop(1);
886 QVERIFY(!page->m_widget);
889 void tst_QWebPage::createViewlessPlugin_data()
891 PluginFactory::prepareTestData();
894 void tst_QWebPage::createViewlessPlugin()
897 PluginCounterPage* page = PluginFactory::create((PluginFactory::FactoredType)type);
898 QString content("<html><body><object type=\"application/x-qt-plugin\" classid=\"QProgressBar\"></object></body></html>");
899 page->mainFrame()->setHtml(content);
900 QCOMPARE(page->m_count, 1);
901 QVERIFY(page->m_widget);
902 QVERIFY(page->m_pluginParent);
903 QVERIFY(page->m_widget->parent() == page->m_pluginParent);
908 void tst_QWebPage::multiplePageGroupsAndLocalStorage()
910 QDir dir(QDir::currentPath());
917 view1.page()->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
918 view1.page()->settings()->setLocalStoragePath(QDir::toNativeSeparators(QDir::currentPath() + "/path1"));
919 DumpRenderTreeSupportQt::webPageSetGroupName(view1.page(), "group1");
920 view2.page()->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
921 view2.page()->settings()->setLocalStoragePath(QDir::toNativeSeparators(QDir::currentPath() + "/path2"));
922 DumpRenderTreeSupportQt::webPageSetGroupName(view2.page(), "group2");
923 QCOMPARE(DumpRenderTreeSupportQt::webPageGroupName(view1.page()), QString("group1"));
924 QCOMPARE(DumpRenderTreeSupportQt::webPageGroupName(view2.page()), QString("group2"));
927 view1.setHtml(QString("<html><body> </body></html>"), QUrl("http://www.myexample.com"));
928 view2.setHtml(QString("<html><body> </body></html>"), QUrl("http://www.myexample.com"));
930 view1.page()->mainFrame()->evaluateJavaScript("localStorage.test='value1';");
931 view2.page()->mainFrame()->evaluateJavaScript("localStorage.test='value2';");
933 view1.setHtml(QString("<html><body> </body></html>"), QUrl("http://www.myexample.com"));
934 view2.setHtml(QString("<html><body> </body></html>"), QUrl("http://www.myexample.com"));
936 QVariant s1 = view1.page()->mainFrame()->evaluateJavaScript("localStorage.test");
937 QCOMPARE(s1.toString(), QString("value1"));
939 QVariant s2 = view2.page()->mainFrame()->evaluateJavaScript("localStorage.test");
940 QCOMPARE(s2.toString(), QString("value2"));
944 QFile::remove(QDir::toNativeSeparators(QDir::currentPath() + "/path1/http_www.myexample.com_0.localstorage"));
945 QFile::remove(QDir::toNativeSeparators(QDir::currentPath() + "/path2/http_www.myexample.com_0.localstorage"));
946 dir.rmdir(QDir::toNativeSeparators("./path1"));
947 dir.rmdir(QDir::toNativeSeparators("./path2"));
950 class CursorTrackedPage : public QWebPage
954 CursorTrackedPage(QWidget *parent = 0): QWebPage(parent) {
955 setViewportSize(QSize(1024, 768)); // big space
958 QString selectedText() {
959 return mainFrame()->evaluateJavaScript("window.getSelection().toString()").toString();
962 int selectionStartOffset() {
963 return mainFrame()->evaluateJavaScript("window.getSelection().getRangeAt(0).startOffset").toInt();
966 int selectionEndOffset() {
967 return mainFrame()->evaluateJavaScript("window.getSelection().getRangeAt(0).endOffset").toInt();
970 // true if start offset == end offset, i.e. no selected text
971 int isSelectionCollapsed() {
972 return mainFrame()->evaluateJavaScript("window.getSelection().getRangeAt(0).collapsed").toBool();
976 void tst_QWebPage::cursorMovements()
978 CursorTrackedPage* page = new CursorTrackedPage;
979 QString content("<html><body><p id=one>The quick brown fox</p><p id=two>jumps over the lazy dog</p><p>May the source<br/>be with you!</p></body></html>");
980 page->mainFrame()->setHtml(content);
982 // this will select the first paragraph
983 QString script = "var range = document.createRange(); " \
984 "var node = document.getElementById(\"one\"); " \
985 "range.selectNode(node); " \
986 "getSelection().addRange(range);";
987 page->mainFrame()->evaluateJavaScript(script);
988 QCOMPARE(page->selectedText().trimmed(), QString::fromLatin1("The quick brown fox"));
989 QCOMPARE(page->selectedHtml().trimmed(), QString::fromLatin1("<span class=\"Apple-style-span\" style=\"border-collapse: separate; color: rgb(0, 0, 0); font-family: Times; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; \"><p id=\"one\">The quick brown fox</p></span>"));
991 // these actions must exist
992 QVERIFY(page->action(QWebPage::MoveToNextChar) != 0);
993 QVERIFY(page->action(QWebPage::MoveToPreviousChar) != 0);
994 QVERIFY(page->action(QWebPage::MoveToNextWord) != 0);
995 QVERIFY(page->action(QWebPage::MoveToPreviousWord) != 0);
996 QVERIFY(page->action(QWebPage::MoveToNextLine) != 0);
997 QVERIFY(page->action(QWebPage::MoveToPreviousLine) != 0);
998 QVERIFY(page->action(QWebPage::MoveToStartOfLine) != 0);
999 QVERIFY(page->action(QWebPage::MoveToEndOfLine) != 0);
1000 QVERIFY(page->action(QWebPage::MoveToStartOfBlock) != 0);
1001 QVERIFY(page->action(QWebPage::MoveToEndOfBlock) != 0);
1002 QVERIFY(page->action(QWebPage::MoveToStartOfDocument) != 0);
1003 QVERIFY(page->action(QWebPage::MoveToEndOfDocument) != 0);
1005 // right now they are disabled because contentEditable is false
1006 QCOMPARE(page->action(QWebPage::MoveToNextChar)->isEnabled(), false);
1007 QCOMPARE(page->action(QWebPage::MoveToPreviousChar)->isEnabled(), false);
1008 QCOMPARE(page->action(QWebPage::MoveToNextWord)->isEnabled(), false);
1009 QCOMPARE(page->action(QWebPage::MoveToPreviousWord)->isEnabled(), false);
1010 QCOMPARE(page->action(QWebPage::MoveToNextLine)->isEnabled(), false);
1011 QCOMPARE(page->action(QWebPage::MoveToPreviousLine)->isEnabled(), false);
1012 QCOMPARE(page->action(QWebPage::MoveToStartOfLine)->isEnabled(), false);
1013 QCOMPARE(page->action(QWebPage::MoveToEndOfLine)->isEnabled(), false);
1014 QCOMPARE(page->action(QWebPage::MoveToStartOfBlock)->isEnabled(), false);
1015 QCOMPARE(page->action(QWebPage::MoveToEndOfBlock)->isEnabled(), false);
1016 QCOMPARE(page->action(QWebPage::MoveToStartOfDocument)->isEnabled(), false);
1017 QCOMPARE(page->action(QWebPage::MoveToEndOfDocument)->isEnabled(), false);
1019 // make it editable before navigating the cursor
1020 page->setContentEditable(true);
1022 // here the actions are enabled after contentEditable is true
1023 QCOMPARE(page->action(QWebPage::MoveToNextChar)->isEnabled(), true);
1024 QCOMPARE(page->action(QWebPage::MoveToPreviousChar)->isEnabled(), true);
1025 QCOMPARE(page->action(QWebPage::MoveToNextWord)->isEnabled(), true);
1026 QCOMPARE(page->action(QWebPage::MoveToPreviousWord)->isEnabled(), true);
1027 QCOMPARE(page->action(QWebPage::MoveToNextLine)->isEnabled(), true);
1028 QCOMPARE(page->action(QWebPage::MoveToPreviousLine)->isEnabled(), true);
1029 QCOMPARE(page->action(QWebPage::MoveToStartOfLine)->isEnabled(), true);
1030 QCOMPARE(page->action(QWebPage::MoveToEndOfLine)->isEnabled(), true);
1031 QCOMPARE(page->action(QWebPage::MoveToStartOfBlock)->isEnabled(), true);
1032 QCOMPARE(page->action(QWebPage::MoveToEndOfBlock)->isEnabled(), true);
1033 QCOMPARE(page->action(QWebPage::MoveToStartOfDocument)->isEnabled(), true);
1034 QCOMPARE(page->action(QWebPage::MoveToEndOfDocument)->isEnabled(), true);
1036 // cursor will be before the word "jump"
1037 page->triggerAction(QWebPage::MoveToNextChar);
1038 QVERIFY(page->isSelectionCollapsed());
1039 QCOMPARE(page->selectionStartOffset(), 0);
1041 // cursor will be between 'j' and 'u' in the word "jump"
1042 page->triggerAction(QWebPage::MoveToNextChar);
1043 QVERIFY(page->isSelectionCollapsed());
1044 QCOMPARE(page->selectionStartOffset(), 1);
1046 // cursor will be between 'u' and 'm' in the word "jump"
1047 page->triggerAction(QWebPage::MoveToNextChar);
1048 QVERIFY(page->isSelectionCollapsed());
1049 QCOMPARE(page->selectionStartOffset(), 2);
1051 // cursor will be after the word "jump"
1052 page->triggerAction(QWebPage::MoveToNextWord);
1053 QVERIFY(page->isSelectionCollapsed());
1054 QCOMPARE(page->selectionStartOffset(), 5);
1056 // cursor will be after the word "lazy"
1057 page->triggerAction(QWebPage::MoveToNextWord);
1058 page->triggerAction(QWebPage::MoveToNextWord);
1059 page->triggerAction(QWebPage::MoveToNextWord);
1060 QVERIFY(page->isSelectionCollapsed());
1061 QCOMPARE(page->selectionStartOffset(), 19);
1063 // cursor will be between 'z' and 'y' in "lazy"
1064 page->triggerAction(QWebPage::MoveToPreviousChar);
1065 QVERIFY(page->isSelectionCollapsed());
1066 QCOMPARE(page->selectionStartOffset(), 18);
1068 // cursor will be between 'a' and 'z' in "lazy"
1069 page->triggerAction(QWebPage::MoveToPreviousChar);
1070 QVERIFY(page->isSelectionCollapsed());
1071 QCOMPARE(page->selectionStartOffset(), 17);
1073 // cursor will be before the word "lazy"
1074 page->triggerAction(QWebPage::MoveToPreviousWord);
1075 QVERIFY(page->isSelectionCollapsed());
1076 QCOMPARE(page->selectionStartOffset(), 15);
1078 // cursor will be before the word "quick"
1079 page->triggerAction(QWebPage::MoveToPreviousWord);
1080 page->triggerAction(QWebPage::MoveToPreviousWord);
1081 page->triggerAction(QWebPage::MoveToPreviousWord);
1082 page->triggerAction(QWebPage::MoveToPreviousWord);
1083 page->triggerAction(QWebPage::MoveToPreviousWord);
1084 page->triggerAction(QWebPage::MoveToPreviousWord);
1085 QVERIFY(page->isSelectionCollapsed());
1086 QCOMPARE(page->selectionStartOffset(), 4);
1088 // cursor will be between 'p' and 's' in the word "jumps"
1089 page->triggerAction(QWebPage::MoveToNextWord);
1090 page->triggerAction(QWebPage::MoveToNextWord);
1091 page->triggerAction(QWebPage::MoveToNextWord);
1092 page->triggerAction(QWebPage::MoveToNextChar);
1093 page->triggerAction(QWebPage::MoveToNextChar);
1094 page->triggerAction(QWebPage::MoveToNextChar);
1095 page->triggerAction(QWebPage::MoveToNextChar);
1096 page->triggerAction(QWebPage::MoveToNextChar);
1097 QVERIFY(page->isSelectionCollapsed());
1098 QCOMPARE(page->selectionStartOffset(), 4);
1100 // cursor will be before the word "jumps"
1101 page->triggerAction(QWebPage::MoveToStartOfLine);
1102 QVERIFY(page->isSelectionCollapsed());
1103 QCOMPARE(page->selectionStartOffset(), 0);
1105 // cursor will be after the word "dog"
1106 page->triggerAction(QWebPage::MoveToEndOfLine);
1107 QVERIFY(page->isSelectionCollapsed());
1108 QCOMPARE(page->selectionStartOffset(), 23);
1110 // cursor will be between 'w' and 'n' in "brown"
1111 page->triggerAction(QWebPage::MoveToStartOfLine);
1112 page->triggerAction(QWebPage::MoveToPreviousWord);
1113 page->triggerAction(QWebPage::MoveToPreviousWord);
1114 page->triggerAction(QWebPage::MoveToNextChar);
1115 page->triggerAction(QWebPage::MoveToNextChar);
1116 page->triggerAction(QWebPage::MoveToNextChar);
1117 page->triggerAction(QWebPage::MoveToNextChar);
1118 QVERIFY(page->isSelectionCollapsed());
1119 QCOMPARE(page->selectionStartOffset(), 14);
1121 // cursor will be after the word "fox"
1122 page->triggerAction(QWebPage::MoveToEndOfLine);
1123 QVERIFY(page->isSelectionCollapsed());
1124 QCOMPARE(page->selectionStartOffset(), 19);
1126 // cursor will be before the word "The"
1127 page->triggerAction(QWebPage::MoveToStartOfDocument);
1128 QVERIFY(page->isSelectionCollapsed());
1129 QCOMPARE(page->selectionStartOffset(), 0);
1131 // cursor will be after the word "you!"
1132 page->triggerAction(QWebPage::MoveToEndOfDocument);
1133 QVERIFY(page->isSelectionCollapsed());
1134 QCOMPARE(page->selectionStartOffset(), 12);
1136 // cursor will be before the word "be"
1137 page->triggerAction(QWebPage::MoveToStartOfBlock);
1138 QVERIFY(page->isSelectionCollapsed());
1139 QCOMPARE(page->selectionStartOffset(), 0);
1141 // cursor will be after the word "you!"
1142 page->triggerAction(QWebPage::MoveToEndOfBlock);
1143 QVERIFY(page->isSelectionCollapsed());
1144 QCOMPARE(page->selectionStartOffset(), 12);
1146 // try to move before the document start
1147 page->triggerAction(QWebPage::MoveToStartOfDocument);
1148 page->triggerAction(QWebPage::MoveToPreviousChar);
1149 QVERIFY(page->isSelectionCollapsed());
1150 QCOMPARE(page->selectionStartOffset(), 0);
1151 page->triggerAction(QWebPage::MoveToStartOfDocument);
1152 page->triggerAction(QWebPage::MoveToPreviousWord);
1153 QVERIFY(page->isSelectionCollapsed());
1154 QCOMPARE(page->selectionStartOffset(), 0);
1156 // try to move past the document end
1157 page->triggerAction(QWebPage::MoveToEndOfDocument);
1158 page->triggerAction(QWebPage::MoveToNextChar);
1159 QVERIFY(page->isSelectionCollapsed());
1160 QCOMPARE(page->selectionStartOffset(), 12);
1161 page->triggerAction(QWebPage::MoveToEndOfDocument);
1162 page->triggerAction(QWebPage::MoveToNextWord);
1163 QVERIFY(page->isSelectionCollapsed());
1164 QCOMPARE(page->selectionStartOffset(), 12);
1169 void tst_QWebPage::textSelection()
1171 CursorTrackedPage* page = new CursorTrackedPage;
1172 QString content("<html><body><p id=one>The quick brown fox</p>" \
1173 "<p id=two>jumps over the lazy dog</p>" \
1174 "<p>May the source<br/>be with you!</p></body></html>");
1175 page->mainFrame()->setHtml(content);
1177 // these actions must exist
1178 QVERIFY(page->action(QWebPage::SelectAll) != 0);
1179 QVERIFY(page->action(QWebPage::SelectNextChar) != 0);
1180 QVERIFY(page->action(QWebPage::SelectPreviousChar) != 0);
1181 QVERIFY(page->action(QWebPage::SelectNextWord) != 0);
1182 QVERIFY(page->action(QWebPage::SelectPreviousWord) != 0);
1183 QVERIFY(page->action(QWebPage::SelectNextLine) != 0);
1184 QVERIFY(page->action(QWebPage::SelectPreviousLine) != 0);
1185 QVERIFY(page->action(QWebPage::SelectStartOfLine) != 0);
1186 QVERIFY(page->action(QWebPage::SelectEndOfLine) != 0);
1187 QVERIFY(page->action(QWebPage::SelectStartOfBlock) != 0);
1188 QVERIFY(page->action(QWebPage::SelectEndOfBlock) != 0);
1189 QVERIFY(page->action(QWebPage::SelectStartOfDocument) != 0);
1190 QVERIFY(page->action(QWebPage::SelectEndOfDocument) != 0);
1192 // right now they are disabled because contentEditable is false and
1193 // there isn't an existing selection to modify
1194 QCOMPARE(page->action(QWebPage::SelectNextChar)->isEnabled(), false);
1195 QCOMPARE(page->action(QWebPage::SelectPreviousChar)->isEnabled(), false);
1196 QCOMPARE(page->action(QWebPage::SelectNextWord)->isEnabled(), false);
1197 QCOMPARE(page->action(QWebPage::SelectPreviousWord)->isEnabled(), false);
1198 QCOMPARE(page->action(QWebPage::SelectNextLine)->isEnabled(), false);
1199 QCOMPARE(page->action(QWebPage::SelectPreviousLine)->isEnabled(), false);
1200 QCOMPARE(page->action(QWebPage::SelectStartOfLine)->isEnabled(), false);
1201 QCOMPARE(page->action(QWebPage::SelectEndOfLine)->isEnabled(), false);
1202 QCOMPARE(page->action(QWebPage::SelectStartOfBlock)->isEnabled(), false);
1203 QCOMPARE(page->action(QWebPage::SelectEndOfBlock)->isEnabled(), false);
1204 QCOMPARE(page->action(QWebPage::SelectStartOfDocument)->isEnabled(), false);
1205 QCOMPARE(page->action(QWebPage::SelectEndOfDocument)->isEnabled(), false);
1207 // ..but SelectAll is awalys enabled
1208 QCOMPARE(page->action(QWebPage::SelectAll)->isEnabled(), true);
1210 // Verify hasSelection returns false since there is no selection yet...
1211 QCOMPARE(page->hasSelection(), false);
1213 // this will select the first paragraph
1214 QString selectScript = "var range = document.createRange(); " \
1215 "var node = document.getElementById(\"one\"); " \
1216 "range.selectNode(node); " \
1217 "getSelection().addRange(range);";
1218 page->mainFrame()->evaluateJavaScript(selectScript);
1219 QCOMPARE(page->selectedText().trimmed(), QString::fromLatin1("The quick brown fox"));
1220 QCOMPARE(page->selectedHtml().trimmed(), QString::fromLatin1("<span class=\"Apple-style-span\" style=\"border-collapse: separate; color: rgb(0, 0, 0); font-family: Times; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; \"><p id=\"one\">The quick brown fox</p></span>"));
1222 // Make sure hasSelection returns true, since there is selected text now...
1223 QCOMPARE(page->hasSelection(), true);
1225 // here the actions are enabled after a selection has been created
1226 QCOMPARE(page->action(QWebPage::SelectNextChar)->isEnabled(), true);
1227 QCOMPARE(page->action(QWebPage::SelectPreviousChar)->isEnabled(), true);
1228 QCOMPARE(page->action(QWebPage::SelectNextWord)->isEnabled(), true);
1229 QCOMPARE(page->action(QWebPage::SelectPreviousWord)->isEnabled(), true);
1230 QCOMPARE(page->action(QWebPage::SelectNextLine)->isEnabled(), true);
1231 QCOMPARE(page->action(QWebPage::SelectPreviousLine)->isEnabled(), true);
1232 QCOMPARE(page->action(QWebPage::SelectStartOfLine)->isEnabled(), true);
1233 QCOMPARE(page->action(QWebPage::SelectEndOfLine)->isEnabled(), true);
1234 QCOMPARE(page->action(QWebPage::SelectStartOfBlock)->isEnabled(), true);
1235 QCOMPARE(page->action(QWebPage::SelectEndOfBlock)->isEnabled(), true);
1236 QCOMPARE(page->action(QWebPage::SelectStartOfDocument)->isEnabled(), true);
1237 QCOMPARE(page->action(QWebPage::SelectEndOfDocument)->isEnabled(), true);
1239 // make it editable before navigating the cursor
1240 page->setContentEditable(true);
1242 // cursor will be before the word "The", this makes sure there is a charet
1243 page->triggerAction(QWebPage::MoveToStartOfDocument);
1244 QVERIFY(page->isSelectionCollapsed());
1245 QCOMPARE(page->selectionStartOffset(), 0);
1247 // here the actions are enabled after contentEditable is true
1248 QCOMPARE(page->action(QWebPage::SelectNextChar)->isEnabled(), true);
1249 QCOMPARE(page->action(QWebPage::SelectPreviousChar)->isEnabled(), true);
1250 QCOMPARE(page->action(QWebPage::SelectNextWord)->isEnabled(), true);
1251 QCOMPARE(page->action(QWebPage::SelectPreviousWord)->isEnabled(), true);
1252 QCOMPARE(page->action(QWebPage::SelectNextLine)->isEnabled(), true);
1253 QCOMPARE(page->action(QWebPage::SelectPreviousLine)->isEnabled(), true);
1254 QCOMPARE(page->action(QWebPage::SelectStartOfLine)->isEnabled(), true);
1255 QCOMPARE(page->action(QWebPage::SelectEndOfLine)->isEnabled(), true);
1256 QCOMPARE(page->action(QWebPage::SelectStartOfBlock)->isEnabled(), true);
1257 QCOMPARE(page->action(QWebPage::SelectEndOfBlock)->isEnabled(), true);
1258 QCOMPARE(page->action(QWebPage::SelectStartOfDocument)->isEnabled(), true);
1259 QCOMPARE(page->action(QWebPage::SelectEndOfDocument)->isEnabled(), true);
1264 void tst_QWebPage::textEditing()
1266 CursorTrackedPage* page = new CursorTrackedPage;
1267 QString content("<html><body><p id=one>The quick brown fox</p>" \
1268 "<p id=two>jumps over the lazy dog</p>" \
1269 "<p>May the source<br/>be with you!</p></body></html>");
1270 page->mainFrame()->setHtml(content);
1272 // these actions must exist
1273 QVERIFY(page->action(QWebPage::Cut) != 0);
1274 QVERIFY(page->action(QWebPage::Copy) != 0);
1275 QVERIFY(page->action(QWebPage::Paste) != 0);
1276 QVERIFY(page->action(QWebPage::DeleteStartOfWord) != 0);
1277 QVERIFY(page->action(QWebPage::DeleteEndOfWord) != 0);
1278 QVERIFY(page->action(QWebPage::SetTextDirectionDefault) != 0);
1279 QVERIFY(page->action(QWebPage::SetTextDirectionLeftToRight) != 0);
1280 QVERIFY(page->action(QWebPage::SetTextDirectionRightToLeft) != 0);
1281 QVERIFY(page->action(QWebPage::ToggleBold) != 0);
1282 QVERIFY(page->action(QWebPage::ToggleItalic) != 0);
1283 QVERIFY(page->action(QWebPage::ToggleUnderline) != 0);
1284 QVERIFY(page->action(QWebPage::InsertParagraphSeparator) != 0);
1285 QVERIFY(page->action(QWebPage::InsertLineSeparator) != 0);
1286 QVERIFY(page->action(QWebPage::PasteAndMatchStyle) != 0);
1287 QVERIFY(page->action(QWebPage::RemoveFormat) != 0);
1288 QVERIFY(page->action(QWebPage::ToggleStrikethrough) != 0);
1289 QVERIFY(page->action(QWebPage::ToggleSubscript) != 0);
1290 QVERIFY(page->action(QWebPage::ToggleSuperscript) != 0);
1291 QVERIFY(page->action(QWebPage::InsertUnorderedList) != 0);
1292 QVERIFY(page->action(QWebPage::InsertOrderedList) != 0);
1293 QVERIFY(page->action(QWebPage::Indent) != 0);
1294 QVERIFY(page->action(QWebPage::Outdent) != 0);
1295 QVERIFY(page->action(QWebPage::AlignCenter) != 0);
1296 QVERIFY(page->action(QWebPage::AlignJustified) != 0);
1297 QVERIFY(page->action(QWebPage::AlignLeft) != 0);
1298 QVERIFY(page->action(QWebPage::AlignRight) != 0);
1300 // right now they are disabled because contentEditable is false
1301 QCOMPARE(page->action(QWebPage::Cut)->isEnabled(), false);
1302 QCOMPARE(page->action(QWebPage::Paste)->isEnabled(), false);
1303 QCOMPARE(page->action(QWebPage::DeleteStartOfWord)->isEnabled(), false);
1304 QCOMPARE(page->action(QWebPage::DeleteEndOfWord)->isEnabled(), false);
1305 QCOMPARE(page->action(QWebPage::SetTextDirectionDefault)->isEnabled(), false);
1306 QCOMPARE(page->action(QWebPage::SetTextDirectionLeftToRight)->isEnabled(), false);
1307 QCOMPARE(page->action(QWebPage::SetTextDirectionRightToLeft)->isEnabled(), false);
1308 QCOMPARE(page->action(QWebPage::ToggleBold)->isEnabled(), false);
1309 QCOMPARE(page->action(QWebPage::ToggleItalic)->isEnabled(), false);
1310 QCOMPARE(page->action(QWebPage::ToggleUnderline)->isEnabled(), false);
1311 QCOMPARE(page->action(QWebPage::InsertParagraphSeparator)->isEnabled(), false);
1312 QCOMPARE(page->action(QWebPage::InsertLineSeparator)->isEnabled(), false);
1313 QCOMPARE(page->action(QWebPage::PasteAndMatchStyle)->isEnabled(), false);
1314 QCOMPARE(page->action(QWebPage::RemoveFormat)->isEnabled(), false);
1315 QCOMPARE(page->action(QWebPage::ToggleStrikethrough)->isEnabled(), false);
1316 QCOMPARE(page->action(QWebPage::ToggleSubscript)->isEnabled(), false);
1317 QCOMPARE(page->action(QWebPage::ToggleSuperscript)->isEnabled(), false);
1318 QCOMPARE(page->action(QWebPage::InsertUnorderedList)->isEnabled(), false);
1319 QCOMPARE(page->action(QWebPage::InsertOrderedList)->isEnabled(), false);
1320 QCOMPARE(page->action(QWebPage::Indent)->isEnabled(), false);
1321 QCOMPARE(page->action(QWebPage::Outdent)->isEnabled(), false);
1322 QCOMPARE(page->action(QWebPage::AlignCenter)->isEnabled(), false);
1323 QCOMPARE(page->action(QWebPage::AlignJustified)->isEnabled(), false);
1324 QCOMPARE(page->action(QWebPage::AlignLeft)->isEnabled(), false);
1325 QCOMPARE(page->action(QWebPage::AlignRight)->isEnabled(), false);
1327 // Select everything
1328 page->triggerAction(QWebPage::SelectAll);
1330 // make sure it is enabled since there is a selection
1331 QCOMPARE(page->action(QWebPage::Copy)->isEnabled(), true);
1333 // make it editable before navigating the cursor
1334 page->setContentEditable(true);
1336 // clear the selection
1337 page->triggerAction(QWebPage::MoveToStartOfDocument);
1338 QVERIFY(page->isSelectionCollapsed());
1339 QCOMPARE(page->selectionStartOffset(), 0);
1341 // make sure it is disabled since there isn't a selection
1342 QCOMPARE(page->action(QWebPage::Copy)->isEnabled(), false);
1344 // here the actions are enabled after contentEditable is true
1345 QCOMPARE(page->action(QWebPage::Paste)->isEnabled(), true);
1346 QCOMPARE(page->action(QWebPage::DeleteStartOfWord)->isEnabled(), true);
1347 QCOMPARE(page->action(QWebPage::DeleteEndOfWord)->isEnabled(), true);
1348 QCOMPARE(page->action(QWebPage::SetTextDirectionDefault)->isEnabled(), true);
1349 QCOMPARE(page->action(QWebPage::SetTextDirectionLeftToRight)->isEnabled(), true);
1350 QCOMPARE(page->action(QWebPage::SetTextDirectionRightToLeft)->isEnabled(), true);
1351 QCOMPARE(page->action(QWebPage::ToggleBold)->isEnabled(), true);
1352 QCOMPARE(page->action(QWebPage::ToggleItalic)->isEnabled(), true);
1353 QCOMPARE(page->action(QWebPage::ToggleUnderline)->isEnabled(), true);
1354 QCOMPARE(page->action(QWebPage::InsertParagraphSeparator)->isEnabled(), true);
1355 QCOMPARE(page->action(QWebPage::InsertLineSeparator)->isEnabled(), true);
1356 QCOMPARE(page->action(QWebPage::PasteAndMatchStyle)->isEnabled(), true);
1357 QCOMPARE(page->action(QWebPage::ToggleStrikethrough)->isEnabled(), true);
1358 QCOMPARE(page->action(QWebPage::ToggleSubscript)->isEnabled(), true);
1359 QCOMPARE(page->action(QWebPage::ToggleSuperscript)->isEnabled(), true);
1360 QCOMPARE(page->action(QWebPage::InsertUnorderedList)->isEnabled(), true);
1361 QCOMPARE(page->action(QWebPage::InsertOrderedList)->isEnabled(), true);
1362 QCOMPARE(page->action(QWebPage::Indent)->isEnabled(), true);
1363 QCOMPARE(page->action(QWebPage::Outdent)->isEnabled(), true);
1364 QCOMPARE(page->action(QWebPage::AlignCenter)->isEnabled(), true);
1365 QCOMPARE(page->action(QWebPage::AlignJustified)->isEnabled(), true);
1366 QCOMPARE(page->action(QWebPage::AlignLeft)->isEnabled(), true);
1367 QCOMPARE(page->action(QWebPage::AlignRight)->isEnabled(), true);
1369 // make sure these are disabled since there isn't a selection
1370 QCOMPARE(page->action(QWebPage::Cut)->isEnabled(), false);
1371 QCOMPARE(page->action(QWebPage::RemoveFormat)->isEnabled(), false);
1373 // make sure everything is selected
1374 page->triggerAction(QWebPage::SelectAll);
1376 // this is only true if there is an editable selection
1377 QCOMPARE(page->action(QWebPage::Cut)->isEnabled(), true);
1378 QCOMPARE(page->action(QWebPage::RemoveFormat)->isEnabled(), true);
1383 void tst_QWebPage::requestCache()
1386 QSignalSpy loadSpy(&page, SIGNAL(loadFinished(bool)));
1388 page.mainFrame()->setUrl(QString("data:text/html,<a href=\"data:text/html,Reached\" target=\"_blank\">Click me</a>"));
1389 QTRY_COMPARE(loadSpy.count(), 1);
1390 QTRY_COMPARE(page.navigations.count(), 1);
1392 page.mainFrame()->setUrl(QString("data:text/html,<a href=\"data:text/html,Reached\" target=\"_blank\">Click me2</a>"));
1393 QTRY_COMPARE(loadSpy.count(), 2);
1394 QTRY_COMPARE(page.navigations.count(), 2);
1396 page.triggerAction(QWebPage::Stop);
1397 QVERIFY(page.history()->canGoBack());
1398 page.triggerAction(QWebPage::Back);
1400 QTRY_COMPARE(loadSpy.count(), 3);
1401 QTRY_COMPARE(page.navigations.count(), 3);
1402 QCOMPARE(page.navigations.at(0).request.attribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork).toInt(),
1403 (int)QNetworkRequest::PreferNetwork);
1404 QCOMPARE(page.navigations.at(1).request.attribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork).toInt(),
1405 (int)QNetworkRequest::PreferNetwork);
1406 QCOMPARE(page.navigations.at(2).request.attribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork).toInt(),
1407 (int)QNetworkRequest::PreferCache);
1410 void tst_QWebPage::loadCachedPage()
1413 QSignalSpy loadSpy(&page, SIGNAL(loadFinished(bool)));
1414 page.settings()->setMaximumPagesInCache(3);
1416 page.mainFrame()->load(QUrl("data:text/html,This is first page"));
1418 QTRY_COMPARE(loadSpy.count(), 1);
1419 QTRY_COMPARE(page.navigations.count(), 1);
1421 QUrl firstPageUrl = page.mainFrame()->url();
1422 page.mainFrame()->load(QUrl("data:text/html,This is second page"));
1424 QTRY_COMPARE(loadSpy.count(), 2);
1425 QTRY_COMPARE(page.navigations.count(), 2);
1427 page.triggerAction(QWebPage::Stop);
1428 QVERIFY(page.history()->canGoBack());
1430 QSignalSpy urlSpy(page.mainFrame(), SIGNAL(urlChanged(QUrl)));
1431 QVERIFY(urlSpy.isValid());
1433 page.triggerAction(QWebPage::Back);
1434 ::waitForSignal(page.mainFrame(), SIGNAL(urlChanged(QUrl)));
1435 QCOMPARE(urlSpy.size(), 1);
1437 QList<QVariant> arguments1 = urlSpy.takeFirst();
1438 QCOMPARE(arguments1.at(0).toUrl(), firstPageUrl);
1441 void tst_QWebPage::backActionUpdate()
1444 QWebPage *page = view.page();
1445 QAction *action = page->action(QWebPage::Back);
1446 QVERIFY(!action->isEnabled());
1447 QSignalSpy loadSpy(page, SIGNAL(loadFinished(bool)));
1448 QUrl url = QUrl("qrc:///resources/framedindex.html");
1449 page->mainFrame()->load(url);
1450 QTRY_COMPARE(loadSpy.count(), 1);
1451 QVERIFY(!action->isEnabled());
1452 QTest::mouseClick(&view, Qt::LeftButton, 0, QPoint(10, 10));
1453 QTRY_COMPARE(loadSpy.count(), 2);
1455 QVERIFY(action->isEnabled());
1458 void frameAtHelper(QWebPage* webPage, QWebFrame* webFrame, QPoint framePosition)
1463 framePosition += QPoint(webFrame->pos());
1464 QList<QWebFrame*> children = webFrame->childFrames();
1465 for (int i = 0; i < children.size(); ++i) {
1466 if (children.at(i)->childFrames().size() > 0)
1467 frameAtHelper(webPage, children.at(i), framePosition);
1469 QRect frameRect(children.at(i)->pos() + framePosition, children.at(i)->geometry().size());
1470 QVERIFY(children.at(i) == webPage->frameAt(frameRect.topLeft()));
1474 void tst_QWebPage::frameAt()
1477 QWebPage* webPage = webView.page();
1478 QSignalSpy loadSpy(webPage, SIGNAL(loadFinished(bool)));
1479 QUrl url = QUrl("qrc:///resources/iframe.html");
1480 webPage->mainFrame()->load(url);
1481 QTRY_COMPARE(loadSpy.count(), 1);
1482 frameAtHelper(webPage, webPage->mainFrame(), webPage->mainFrame()->pos());
1485 void tst_QWebPage::inputMethods_data()
1487 QTest::addColumn<QString>("viewType");
1488 QTest::newRow("QWebView") << "QWebView";
1489 QTest::newRow("QGraphicsWebView") << "QGraphicsWebView";
1492 static Qt::InputMethodHints inputMethodHints(QObject* object)
1494 if (QGraphicsObject* o = qobject_cast<QGraphicsObject*>(object))
1495 return o->inputMethodHints();
1496 if (QWidget* w = qobject_cast<QWidget*>(object))
1497 return w->inputMethodHints();
1498 return Qt::InputMethodHints();
1501 static bool inputMethodEnabled(QObject* object)
1503 if (QGraphicsObject* o = qobject_cast<QGraphicsObject*>(object))
1504 return o->flags() & QGraphicsItem::ItemAcceptsInputMethod;
1505 if (QWidget* w = qobject_cast<QWidget*>(object))
1506 return w->testAttribute(Qt::WA_InputMethodEnabled);
1510 static void clickOnPage(QWebPage* page, const QPoint& position)
1512 QMouseEvent evpres(QEvent::MouseButtonPress, position, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1513 page->event(&evpres);
1514 QMouseEvent evrel(QEvent::MouseButtonRelease, position, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1515 page->event(&evrel);
1518 void tst_QWebPage::inputMethods()
1520 QFETCH(QString, viewType);
1521 QWebPage* page = new QWebPage;
1523 QObject* container = 0;
1524 if (viewType == "QWebView") {
1525 QWebView* wv = new QWebView;
1529 } else if (viewType == "QGraphicsWebView") {
1530 QGraphicsWebView* wv = new QGraphicsWebView;
1534 QGraphicsView* gv = new QGraphicsView;
1535 QGraphicsScene* scene = new QGraphicsScene(gv);
1536 gv->setScene(scene);
1538 wv->setGeometry(QRect(0, 0, 500, 500));
1542 QVERIFY2(false, "Unknown view type");
1544 page->settings()->setFontFamily(QWebSettings::SerifFont, "FooSerifFont");
1545 page->mainFrame()->setHtml("<html><body>" \
1546 "<input type='text' id='input1' style='font-family: serif' value='' maxlength='20'/><br>" \
1547 "<input type='password'/>" \
1549 page->mainFrame()->setFocus();
1551 EventSpy viewEventSpy(container);
1553 QWebElementCollection inputs = page->mainFrame()->documentElement().findAll("input");
1554 QPoint textInputCenter = inputs.at(0).geometry().center();
1556 clickOnPage(page, textInputCenter);
1558 // This part of the test checks if the SIP (Software Input Panel) is triggered,
1559 // which normally happens on mobile platforms, when a user input form receives
1562 if (viewType == "QWebView") {
1563 if (QWebView* wv = qobject_cast<QWebView*>(view))
1564 inputPanel = wv->style()->styleHint(QStyle::SH_RequestSoftwareInputPanel);
1565 } else if (viewType == "QGraphicsWebView") {
1566 if (QGraphicsWebView* wv = qobject_cast<QGraphicsWebView*>(view))
1567 inputPanel = wv->style()->styleHint(QStyle::SH_RequestSoftwareInputPanel);
1570 // For non-mobile platforms RequestSoftwareInputPanel event is not called
1571 // because there is no SIP (Software Input Panel) triggered. In the case of a
1572 // mobile platform, an input panel, e.g. virtual keyboard, is usually invoked
1573 // and the RequestSoftwareInputPanel event is called. For these two situations
1574 // this part of the test can verified as the checks below.
1576 QVERIFY(viewEventSpy.contains(QEvent::RequestSoftwareInputPanel));
1578 QVERIFY(!viewEventSpy.contains(QEvent::RequestSoftwareInputPanel));
1579 viewEventSpy.clear();
1581 clickOnPage(page, textInputCenter);
1582 QVERIFY(viewEventSpy.contains(QEvent::RequestSoftwareInputPanel));
1585 QVariant variant = page->inputMethodQuery(Qt::ImMicroFocus);
1586 QRect focusRect = variant.toRect();
1587 QVERIFY(inputs.at(0).geometry().contains(variant.toRect().topLeft()));
1590 variant = page->inputMethodQuery(Qt::ImFont);
1591 QFont font = variant.value<QFont>();
1592 QCOMPARE(page->settings()->fontFamily(QWebSettings::SerifFont), font.family());
1594 QList<QInputMethodEvent::Attribute> inputAttributes;
1598 QInputMethodEvent eventText("QtWebKit", inputAttributes);
1599 QSignalSpy signalSpy(page, SIGNAL(microFocusChanged()));
1600 page->event(&eventText);
1601 QCOMPARE(signalSpy.count(), 0);
1605 QInputMethodEvent eventText("", inputAttributes);
1606 eventText.setCommitString(QString("QtWebKit"), 0, 0);
1607 page->event(&eventText);
1610 //ImMaximumTextLength
1611 variant = page->inputMethodQuery(Qt::ImMaximumTextLength);
1612 QCOMPARE(20, variant.toInt());
1615 inputAttributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, 3, 2, QVariant());
1616 QInputMethodEvent eventSelection("",inputAttributes);
1617 page->event(&eventSelection);
1620 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1621 int anchorPosition = variant.toInt();
1622 QCOMPARE(anchorPosition, 3);
1625 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1626 int cursorPosition = variant.toInt();
1627 QCOMPARE(cursorPosition, 5);
1629 //ImCurrentSelection
1630 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1631 QString selectionValue = variant.value<QString>();
1632 QCOMPARE(selectionValue, QString("eb"));
1634 //Set selection with negative length
1635 inputAttributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, 6, -5, QVariant());
1636 QInputMethodEvent eventSelection3("",inputAttributes);
1637 page->event(&eventSelection3);
1640 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1641 anchorPosition = variant.toInt();
1642 QCOMPARE(anchorPosition, 1);
1645 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1646 cursorPosition = variant.toInt();
1647 QCOMPARE(cursorPosition, 6);
1649 //ImCurrentSelection
1650 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1651 selectionValue = variant.value<QString>();
1652 QCOMPARE(selectionValue, QString("tWebK"));
1655 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1656 QString value = variant.value<QString>();
1657 QCOMPARE(value, QString("QtWebKit"));
1660 QList<QInputMethodEvent::Attribute> attributes;
1661 // Clear the selection, so the next test does not clear any contents.
1662 QInputMethodEvent::Attribute newSelection(QInputMethodEvent::Selection, 0, 0, QVariant());
1663 attributes.append(newSelection);
1664 QInputMethodEvent event("composition", attributes);
1665 page->event(&event);
1668 // A ongoing composition should not change the surrounding text before it is committed.
1669 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1670 value = variant.value<QString>();
1671 QCOMPARE(value, QString("QtWebKit"));
1673 // Cancel current composition first
1674 inputAttributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, 0, 0, QVariant());
1675 QInputMethodEvent eventSelection4("", inputAttributes);
1676 page->event(&eventSelection4);
1678 // START - Tests for Selection when the Editor is NOT in Composition mode
1680 // LEFT to RIGHT selection
1681 // Deselect the selection by sending MouseButtonPress events
1682 // This moves the current cursor to the end of the text
1683 clickOnPage(page, textInputCenter);
1686 QList<QInputMethodEvent::Attribute> attributes;
1687 QInputMethodEvent event(QString(), attributes);
1688 event.setCommitString("XXX", 0, 0);
1689 page->event(&event);
1690 event.setCommitString(QString(), -2, 2); // Erase two characters.
1691 page->event(&event);
1692 event.setCommitString(QString(), -1, 1); // Erase one character.
1693 page->event(&event);
1694 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1695 value = variant.value<QString>();
1696 QCOMPARE(value, QString("QtWebKit"));
1699 //Move to the start of the line
1700 page->triggerAction(QWebPage::MoveToStartOfLine);
1702 QKeyEvent keyRightEventPress(QEvent::KeyPress, Qt::Key_Right, Qt::NoModifier);
1703 QKeyEvent keyRightEventRelease(QEvent::KeyRelease, Qt::Key_Right, Qt::NoModifier);
1705 //Move 2 characters RIGHT
1706 for (int j = 0; j < 2; ++j) {
1707 page->event(&keyRightEventPress);
1708 page->event(&keyRightEventRelease);
1711 //Select to the end of the line
1712 page->triggerAction(QWebPage::SelectEndOfLine);
1714 //ImAnchorPosition QtWebKit
1715 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1716 anchorPosition = variant.toInt();
1717 QCOMPARE(anchorPosition, 2);
1720 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1721 cursorPosition = variant.toInt();
1722 QCOMPARE(cursorPosition, 8);
1724 //ImCurrentSelection
1725 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1726 selectionValue = variant.value<QString>();
1727 QCOMPARE(selectionValue, QString("WebKit"));
1729 //RIGHT to LEFT selection
1730 //Deselect the selection (this moves the current cursor to the end of the text)
1731 clickOnPage(page, textInputCenter);
1734 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1735 anchorPosition = variant.toInt();
1736 QCOMPARE(anchorPosition, 8);
1739 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1740 cursorPosition = variant.toInt();
1741 QCOMPARE(cursorPosition, 8);
1743 //ImCurrentSelection
1744 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1745 selectionValue = variant.value<QString>();
1746 QCOMPARE(selectionValue, QString(""));
1748 QKeyEvent keyLeftEventPress(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier);
1749 QKeyEvent keyLeftEventRelease(QEvent::KeyRelease, Qt::Key_Left, Qt::NoModifier);
1751 //Move 2 characters LEFT
1752 for (int i = 0; i < 2; ++i) {
1753 page->event(&keyLeftEventPress);
1754 page->event(&keyLeftEventRelease);
1757 //Select to the start of the line
1758 page->triggerAction(QWebPage::SelectStartOfLine);
1761 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1762 anchorPosition = variant.toInt();
1763 QCOMPARE(anchorPosition, 6);
1766 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1767 cursorPosition = variant.toInt();
1768 QCOMPARE(cursorPosition, 0);
1770 //ImCurrentSelection
1771 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1772 selectionValue = variant.value<QString>();
1773 QCOMPARE(selectionValue, QString("QtWebK"));
1775 //END - Tests for Selection when the Editor is not in Composition mode
1778 QPoint passwordInputCenter = inputs.at(1).geometry().center();
1779 clickOnPage(page, passwordInputCenter);
1781 QVERIFY(inputMethodEnabled(view));
1782 QVERIFY(inputMethodHints(view) & Qt::ImhHiddenText);
1784 clickOnPage(page, textInputCenter);
1785 QVERIFY(!(inputMethodHints(view) & Qt::ImhHiddenText));
1787 page->mainFrame()->setHtml("<html><body><p>nothing to input here");
1788 viewEventSpy.clear();
1790 QWebElement para = page->mainFrame()->findFirstElement("p");
1791 clickOnPage(page, para.geometry().center());
1793 QVERIFY(!viewEventSpy.contains(QEvent::RequestSoftwareInputPanel));
1795 //START - Test for sending empty QInputMethodEvent
1796 page->mainFrame()->setHtml("<html><body>" \
1797 "<input type='text' id='input3' value='QtWebKit2'/>" \
1799 page->mainFrame()->evaluateJavaScript("var inputEle = document.getElementById('input3'); inputEle.focus(); inputEle.select();");
1801 //Send empty QInputMethodEvent
1802 QInputMethodEvent emptyEvent;
1803 page->event(&emptyEvent);
1805 QString inputValue = page->mainFrame()->evaluateJavaScript("document.getElementById('input3').value").toString();
1806 QCOMPARE(inputValue, QString("QtWebKit2"));
1807 //END - Test for sending empty QInputMethodEvent
1809 page->mainFrame()->setHtml("<html><body>" \
1810 "<input type='text' id='input4' value='QtWebKit inputMethod'/>" \
1812 page->mainFrame()->evaluateJavaScript("var inputEle = document.getElementById('input4'); inputEle.focus(); inputEle.select();");
1814 // Clear the selection, also cancel the ongoing composition if there is one.
1816 QList<QInputMethodEvent::Attribute> attributes;
1817 QInputMethodEvent::Attribute newSelection(QInputMethodEvent::Selection, 0, 0, QVariant());
1818 attributes.append(newSelection);
1819 QInputMethodEvent event("", attributes);
1820 page->event(&event);
1823 // ImCurrentSelection
1824 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1825 selectionValue = variant.value<QString>();
1826 QCOMPARE(selectionValue, QString(""));
1828 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1829 QString surroundingValue = variant.value<QString>();
1830 QCOMPARE(surroundingValue, QString("QtWebKit inputMethod"));
1833 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1834 anchorPosition = variant.toInt();
1835 QCOMPARE(anchorPosition, 0);
1838 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1839 cursorPosition = variant.toInt();
1840 QCOMPARE(cursorPosition, 0);
1842 // 1. Insert a character to the begining of the line.
1843 // Send temporary text, which makes the editor has composition 'm'.
1845 QList<QInputMethodEvent::Attribute> attributes;
1846 QInputMethodEvent event("m", attributes);
1847 page->event(&event);
1850 // ImCurrentSelection
1851 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1852 selectionValue = variant.value<QString>();
1853 QCOMPARE(selectionValue, QString(""));
1855 // ImSurroundingText
1856 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1857 surroundingValue = variant.value<QString>();
1858 QCOMPARE(surroundingValue, QString("QtWebKit inputMethod"));
1861 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1862 cursorPosition = variant.toInt();
1863 QCOMPARE(cursorPosition, 0);
1866 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1867 anchorPosition = variant.toInt();
1868 QCOMPARE(anchorPosition, 0);
1870 // Send temporary text, which makes the editor has composition 'n'.
1872 QList<QInputMethodEvent::Attribute> attributes;
1873 QInputMethodEvent event("n", attributes);
1874 page->event(&event);
1877 // ImCurrentSelection
1878 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1879 selectionValue = variant.value<QString>();
1880 QCOMPARE(selectionValue, QString(""));
1882 // ImSurroundingText
1883 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1884 surroundingValue = variant.value<QString>();
1885 QCOMPARE(surroundingValue, QString("QtWebKit inputMethod"));
1888 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1889 cursorPosition = variant.toInt();
1890 QCOMPARE(cursorPosition, 0);
1893 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1894 anchorPosition = variant.toInt();
1895 QCOMPARE(anchorPosition, 0);
1897 // Send commit text, which makes the editor conforms composition.
1899 QList<QInputMethodEvent::Attribute> attributes;
1900 QInputMethodEvent event("", attributes);
1901 event.setCommitString("o");
1902 page->event(&event);
1905 // ImCurrentSelection
1906 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1907 selectionValue = variant.value<QString>();
1908 QCOMPARE(selectionValue, QString(""));
1910 // ImSurroundingText
1911 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1912 surroundingValue = variant.value<QString>();
1913 QCOMPARE(surroundingValue, QString("oQtWebKit inputMethod"));
1916 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1917 cursorPosition = variant.toInt();
1918 QCOMPARE(cursorPosition, 1);
1921 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1922 anchorPosition = variant.toInt();
1923 QCOMPARE(anchorPosition, 1);
1925 // 2. insert a character to the middle of the line.
1926 // Send temporary text, which makes the editor has composition 'd'.
1928 QList<QInputMethodEvent::Attribute> attributes;
1929 QInputMethodEvent event("d", attributes);
1930 page->event(&event);
1933 // ImCurrentSelection
1934 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1935 selectionValue = variant.value<QString>();
1936 QCOMPARE(selectionValue, QString(""));
1938 // ImSurroundingText
1939 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1940 surroundingValue = variant.value<QString>();
1941 QCOMPARE(surroundingValue, QString("oQtWebKit inputMethod"));
1944 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1945 cursorPosition = variant.toInt();
1946 QCOMPARE(cursorPosition, 1);
1949 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1950 anchorPosition = variant.toInt();
1951 QCOMPARE(anchorPosition, 1);
1953 // Send commit text, which makes the editor conforms composition.
1955 QList<QInputMethodEvent::Attribute> attributes;
1956 QInputMethodEvent event("", attributes);
1957 event.setCommitString("e");
1958 page->event(&event);
1961 // ImCurrentSelection
1962 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1963 selectionValue = variant.value<QString>();
1964 QCOMPARE(selectionValue, QString(""));
1966 // ImSurroundingText
1967 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1968 surroundingValue = variant.value<QString>();
1969 QCOMPARE(surroundingValue, QString("oeQtWebKit inputMethod"));
1972 variant = page->inputMethodQuery(Qt::ImCursorPosition);
1973 cursorPosition = variant.toInt();
1974 QCOMPARE(cursorPosition, 2);
1977 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
1978 anchorPosition = variant.toInt();
1979 QCOMPARE(anchorPosition, 2);
1981 // 3. Insert a character to the end of the line.
1982 page->triggerAction(QWebPage::MoveToEndOfLine);
1984 // Send temporary text, which makes the editor has composition 't'.
1986 QList<QInputMethodEvent::Attribute> attributes;
1987 QInputMethodEvent event("t", attributes);
1988 page->event(&event);
1991 // ImCurrentSelection
1992 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
1993 selectionValue = variant.value<QString>();
1994 QCOMPARE(selectionValue, QString(""));
1996 // ImSurroundingText
1997 variant = page->inputMethodQuery(Qt::ImSurroundingText);
1998 surroundingValue = variant.value<QString>();
1999 QCOMPARE(surroundingValue, QString("oeQtWebKit inputMethod"));
2002 variant = page->inputMethodQuery(Qt::ImCursorPosition);
2003 cursorPosition = variant.toInt();
2004 QCOMPARE(cursorPosition, 22);
2007 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
2008 anchorPosition = variant.toInt();
2009 QCOMPARE(anchorPosition, 22);
2011 // Send commit text, which makes the editor conforms composition.
2013 QList<QInputMethodEvent::Attribute> attributes;
2014 QInputMethodEvent event("", attributes);
2015 event.setCommitString("t");
2016 page->event(&event);
2019 // ImCurrentSelection
2020 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
2021 selectionValue = variant.value<QString>();
2022 QCOMPARE(selectionValue, QString(""));
2024 // ImSurroundingText
2025 variant = page->inputMethodQuery(Qt::ImSurroundingText);
2026 surroundingValue = variant.value<QString>();
2027 QCOMPARE(surroundingValue, QString("oeQtWebKit inputMethodt"));
2030 variant = page->inputMethodQuery(Qt::ImCursorPosition);
2031 cursorPosition = variant.toInt();
2032 QCOMPARE(cursorPosition, 23);
2035 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
2036 anchorPosition = variant.toInt();
2037 QCOMPARE(anchorPosition, 23);
2039 // 4. Replace the selection.
2040 page->triggerAction(QWebPage::SelectPreviousWord);
2042 // ImCurrentSelection
2043 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
2044 selectionValue = variant.value<QString>();
2045 QCOMPARE(selectionValue, QString("inputMethodt"));
2047 // ImSurroundingText
2048 variant = page->inputMethodQuery(Qt::ImSurroundingText);
2049 surroundingValue = variant.value<QString>();
2050 QCOMPARE(surroundingValue, QString("oeQtWebKit inputMethodt"));
2053 variant = page->inputMethodQuery(Qt::ImCursorPosition);
2054 cursorPosition = variant.toInt();
2055 QCOMPARE(cursorPosition, 11);
2058 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
2059 anchorPosition = variant.toInt();
2060 QCOMPARE(anchorPosition, 23);
2062 // Send temporary text, which makes the editor has composition 'w'.
2064 QList<QInputMethodEvent::Attribute> attributes;
2065 QInputMethodEvent event("w", attributes);
2066 page->event(&event);
2069 // ImCurrentSelection
2070 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
2071 selectionValue = variant.value<QString>();
2072 QCOMPARE(selectionValue, QString(""));
2074 // ImSurroundingText
2075 variant = page->inputMethodQuery(Qt::ImSurroundingText);
2076 surroundingValue = variant.value<QString>();
2077 QCOMPARE(surroundingValue, QString("oeQtWebKit "));
2080 variant = page->inputMethodQuery(Qt::ImCursorPosition);
2081 cursorPosition = variant.toInt();
2082 QCOMPARE(cursorPosition, 11);
2085 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
2086 anchorPosition = variant.toInt();
2087 QCOMPARE(anchorPosition, 11);
2089 // Send commit text, which makes the editor conforms composition.
2091 QList<QInputMethodEvent::Attribute> attributes;
2092 QInputMethodEvent event("", attributes);
2093 event.setCommitString("2");
2094 page->event(&event);
2097 // ImCurrentSelection
2098 variant = page->inputMethodQuery(Qt::ImCurrentSelection);
2099 selectionValue = variant.value<QString>();
2100 QCOMPARE(selectionValue, QString(""));
2102 // ImSurroundingText
2103 variant = page->inputMethodQuery(Qt::ImSurroundingText);
2104 surroundingValue = variant.value<QString>();
2105 QCOMPARE(surroundingValue, QString("oeQtWebKit 2"));
2108 variant = page->inputMethodQuery(Qt::ImCursorPosition);
2109 cursorPosition = variant.toInt();
2110 QCOMPARE(cursorPosition, 12);
2113 variant = page->inputMethodQuery(Qt::ImAnchorPosition);
2114 anchorPosition = variant.toInt();
2115 QCOMPARE(anchorPosition, 12);
2117 // Check sending RequestSoftwareInputPanel event
2118 page->mainFrame()->setHtml("<html><body>" \
2119 "<input type='text' id='input5' value='QtWebKit inputMethod'/>" \
2120 "<div id='btnDiv' onclick='i=document.getElementById("input5"); i.focus();'>abc</div>"\
2122 QWebElement inputElement = page->mainFrame()->findFirstElement("div");
2123 clickOnPage(page, inputElement.geometry().center());
2125 QVERIFY(!viewEventSpy.contains(QEvent::RequestSoftwareInputPanel));
2127 // START - Newline test for textarea
2128 qApp->processEvents();
2129 page->mainFrame()->setHtml("<html><body>" \
2130 "<textarea rows='5' cols='1' id='input5' value=''/>" \
2132 page->mainFrame()->evaluateJavaScript("var inputEle = document.getElementById('input5'); inputEle.focus(); inputEle.select();");
2133 QKeyEvent keyEnter(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
2134 page->event(&keyEnter);
2135 QList<QInputMethodEvent::Attribute> attribs;
2137 QInputMethodEvent eventText("\n", attribs);
2138 page->event(&eventText);
2140 QInputMethodEvent eventText2("third line", attribs);
2141 page->event(&eventText2);
2142 qApp->processEvents();
2144 QString inputValue2 = page->mainFrame()->evaluateJavaScript("document.getElementById('input5').value").toString();
2145 QCOMPARE(inputValue2, QString("\n\nthird line"));
2146 // END - Newline test for textarea
2151 void tst_QWebPage::inputMethodsTextFormat_data()
2153 QTest::addColumn<QString>("string");
2154 QTest::addColumn<int>("start");
2155 QTest::addColumn<int>("length");
2157 QTest::newRow("") << QString("") << 0 << 0;
2158 QTest::newRow("Q") << QString("Q") << 0 << 1;
2159 QTest::newRow("Qt") << QString("Qt") << 0 << 1;
2160 QTest::newRow("Qt") << QString("Qt") << 0 << 2;
2161 QTest::newRow("Qt") << QString("Qt") << 1 << 1;
2162 QTest::newRow("Qt ") << QString("Qt ") << 0 << 1;
2163 QTest::newRow("Qt ") << QString("Qt ") << 1 << 1;
2164 QTest::newRow("Qt ") << QString("Qt ") << 2 << 1;
2165 QTest::newRow("Qt ") << QString("Qt ") << 2 << -1;
2166 QTest::newRow("Qt ") << QString("Qt ") << -2 << 3;
2167 QTest::newRow("Qt ") << QString("Qt ") << 0 << 3;
2168 QTest::newRow("Qt by") << QString("Qt by") << 0 << 1;
2169 QTest::newRow("Qt by Nokia") << QString("Qt by Nokia") << 0 << 1;
2173 void tst_QWebPage::inputMethodsTextFormat()
2175 QWebPage* page = new QWebPage;
2176 QWebView* view = new QWebView;
2177 view->setPage(page);
2178 page->settings()->setFontFamily(QWebSettings::SerifFont, "FooSerifFont");
2179 page->mainFrame()->setHtml("<html><body>" \
2180 "<input type='text' id='input1' style='font-family: serif' value='' maxlength='20'/>");
2181 page->mainFrame()->evaluateJavaScript("document.getElementById('input1').focus()");
2182 page->mainFrame()->setFocus();
2185 QFETCH(QString, string);
2187 QFETCH(int, length);
2189 QList<QInputMethodEvent::Attribute> attrs;
2190 QTextCharFormat format;
2191 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
2192 format.setUnderlineColor(Qt::red);
2193 attrs.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, start, length, format));
2194 QInputMethodEvent im(string, attrs);
2202 void tst_QWebPage::protectBindingsRuntimeObjectsFromCollector()
2204 QSignalSpy loadSpy(m_view, SIGNAL(loadFinished(bool)));
2206 PluginPage* newPage = new PluginPage(m_view);
2207 m_view->setPage(newPage);
2209 m_view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
2211 m_view->setHtml(QString("<html><body><object type='application/x-qt-plugin' classid='lineedit' id='mylineedit'/></body></html>"));
2212 QTRY_COMPARE(loadSpy.count(), 1);
2214 newPage->mainFrame()->evaluateJavaScript("function testme(text) { var lineedit = document.getElementById('mylineedit'); lineedit.setText(text); lineedit.selectAll(); }");
2216 newPage->mainFrame()->evaluateJavaScript("testme('foo')");
2218 DumpRenderTreeSupportQt::garbageCollectorCollect();
2221 newPage->mainFrame()->evaluateJavaScript("testme('bar')");
2224 void tst_QWebPage::localURLSchemes()
2226 int i = QWebSecurityOrigin::localSchemes().size();
2228 QWebSecurityOrigin::removeLocalScheme("file");
2229 QTRY_COMPARE(QWebSecurityOrigin::localSchemes().size(), i);
2230 QWebSecurityOrigin::addLocalScheme("file");
2231 QTRY_COMPARE(QWebSecurityOrigin::localSchemes().size(), i);
2233 QWebSecurityOrigin::removeLocalScheme("qrc");
2234 QTRY_COMPARE(QWebSecurityOrigin::localSchemes().size(), i - 1);
2235 QWebSecurityOrigin::addLocalScheme("qrc");
2236 QTRY_COMPARE(QWebSecurityOrigin::localSchemes().size(), i);
2238 QString myscheme = "myscheme";
2239 QWebSecurityOrigin::addLocalScheme(myscheme);
2240 QTRY_COMPARE(QWebSecurityOrigin::localSchemes().size(), i + 1);
2241 QVERIFY(QWebSecurityOrigin::localSchemes().contains(myscheme));
2242 QWebSecurityOrigin::removeLocalScheme(myscheme);
2243 QTRY_COMPARE(QWebSecurityOrigin::localSchemes().size(), i);
2244 QWebSecurityOrigin::removeLocalScheme(myscheme);
2245 QTRY_COMPARE(QWebSecurityOrigin::localSchemes().size(), i);
2248 static inline bool testFlag(QWebPage& webPage, QWebSettings::WebAttribute settingAttribute, const QString& jsObjectName, bool settingValue)
2250 webPage.settings()->setAttribute(settingAttribute, settingValue);
2251 return webPage.mainFrame()->evaluateJavaScript(QString("(window.%1 != undefined)").arg(jsObjectName)).toBool();
2254 void tst_QWebPage::testOptionalJSObjects()
2256 // Once a feature is enabled and the JS object is accessed turning off the setting will not turn off
2257 // the visibility of the JS object any more. For this reason this test uses two QWebPage instances.
2258 // Part of the test is to make sure that the QWebPage instances do not interfere with each other so turning on
2259 // a feature for one instance will not turn it on for another.
2264 webPage1.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl());
2265 webPage2.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl());
2267 QEXPECT_FAIL("","Feature enabled/disabled checking problem. Look at bugs.webkit.org/show_bug.cgi?id=29867", Continue);
2268 QCOMPARE(testFlag(webPage1, QWebSettings::OfflineWebApplicationCacheEnabled, "applicationCache", false), false);
2269 QCOMPARE(testFlag(webPage2, QWebSettings::OfflineWebApplicationCacheEnabled, "applicationCache", true), true);
2270 QEXPECT_FAIL("","Feature enabled/disabled checking problem. Look at bugs.webkit.org/show_bug.cgi?id=29867", Continue);
2271 QCOMPARE(testFlag(webPage1, QWebSettings::OfflineWebApplicationCacheEnabled, "applicationCache", false), false);
2272 QCOMPARE(testFlag(webPage2, QWebSettings::OfflineWebApplicationCacheEnabled, "applicationCache", false), true);
2274 QCOMPARE(testFlag(webPage1, QWebSettings::LocalStorageEnabled, "localStorage", false), false);
2275 QCOMPARE(testFlag(webPage2, QWebSettings::LocalStorageEnabled, "localStorage", true), true);
2276 QCOMPARE(testFlag(webPage1, QWebSettings::LocalStorageEnabled, "localStorage", false), false);
2277 QCOMPARE(testFlag(webPage2, QWebSettings::LocalStorageEnabled, "localStorage", false), true);
2280 void tst_QWebPage::testEnablePersistentStorage()
2284 // By default all persistent options should be disabled
2285 QCOMPARE(webPage.settings()->testAttribute(QWebSettings::LocalStorageEnabled), false);
2286 QCOMPARE(webPage.settings()->testAttribute(QWebSettings::OfflineStorageDatabaseEnabled), false);
2287 QCOMPARE(webPage.settings()->testAttribute(QWebSettings::OfflineWebApplicationCacheEnabled), false);
2288 QVERIFY(webPage.settings()->iconDatabasePath().isEmpty());
2290 QWebSettings::enablePersistentStorage();
2293 QTRY_COMPARE(webPage.settings()->testAttribute(QWebSettings::LocalStorageEnabled), true);
2294 QTRY_COMPARE(webPage.settings()->testAttribute(QWebSettings::OfflineStorageDatabaseEnabled), true);
2295 QTRY_COMPARE(webPage.settings()->testAttribute(QWebSettings::OfflineWebApplicationCacheEnabled), true);
2297 QTRY_VERIFY(!webPage.settings()->offlineStoragePath().isEmpty());
2298 QTRY_VERIFY(!webPage.settings()->offlineWebApplicationCachePath().isEmpty());
2299 QTRY_VERIFY(!webPage.settings()->iconDatabasePath().isEmpty());
2302 void tst_QWebPage::defaultTextEncoding()
2304 QWebFrame* mainFrame = m_page->mainFrame();
2306 QString defaultCharset = mainFrame->evaluateJavaScript("document.defaultCharset").toString();
2307 QVERIFY(!defaultCharset.isEmpty());
2308 QCOMPARE(QWebSettings::globalSettings()->defaultTextEncoding(), defaultCharset);
2310 m_page->settings()->setDefaultTextEncoding(QString("utf-8"));
2311 QString charset = mainFrame->evaluateJavaScript("document.defaultCharset").toString();
2312 QCOMPARE(charset, QString("utf-8"));
2313 QCOMPARE(m_page->settings()->defaultTextEncoding(), charset);
2315 m_page->settings()->setDefaultTextEncoding(QString());
2316 charset = mainFrame->evaluateJavaScript("document.defaultCharset").toString();
2317 QVERIFY(!charset.isEmpty());
2318 QCOMPARE(charset, defaultCharset);
2320 QWebSettings::globalSettings()->setDefaultTextEncoding(QString("utf-8"));
2321 charset = mainFrame->evaluateJavaScript("document.defaultCharset").toString();
2322 QCOMPARE(charset, QString("utf-8"));
2323 QCOMPARE(QWebSettings::globalSettings()->defaultTextEncoding(), charset);
2326 class ErrorPage : public QWebPage
2330 ErrorPage(QWidget* parent = 0): QWebPage(parent)
2334 virtual bool supportsExtension(Extension extension) const
2336 return extension == ErrorPageExtension;
2339 virtual bool extension(Extension, const ExtensionOption* option, ExtensionReturn* output)
2341 ErrorPageExtensionReturn* errorPage = static_cast<ErrorPageExtensionReturn*>(output);
2343 errorPage->contentType = "text/html";
2344 errorPage->content = "error";
2349 void tst_QWebPage::errorPageExtension()
2351 ErrorPage* page = new ErrorPage;
2352 m_view->setPage(page);
2354 QSignalSpy spyLoadFinished(m_view, SIGNAL(loadFinished(bool)));
2356 m_view->setUrl(QUrl("data:text/html,foo"));
2357 QTRY_COMPARE(spyLoadFinished.count(), 1);
2359 page->mainFrame()->setUrl(QUrl("http://non.existent/url"));
2360 QTRY_COMPARE(spyLoadFinished.count(), 2);
2361 QCOMPARE(page->mainFrame()->toPlainText(), QString("error"));
2362 QCOMPARE(page->history()->count(), 2);
2363 QCOMPARE(page->history()->currentItem().url(), QUrl("http://non.existent/url"));
2364 QCOMPARE(page->history()->canGoBack(), true);
2365 QCOMPARE(page->history()->canGoForward(), false);
2367 page->triggerAction(QWebPage::Back);
2368 QTRY_COMPARE(page->history()->canGoBack(), false);
2369 QTRY_COMPARE(page->history()->canGoForward(), true);
2371 page->triggerAction(QWebPage::Forward);
2372 QTRY_COMPARE(page->history()->canGoBack(), true);
2373 QTRY_COMPARE(page->history()->canGoForward(), false);
2375 page->triggerAction(QWebPage::Back);
2376 QTRY_COMPARE(page->history()->canGoBack(), false);
2377 QTRY_COMPARE(page->history()->canGoForward(), true);
2378 QTRY_COMPARE(page->history()->currentItem().url(), QUrl("data:text/html,foo"));
2383 void tst_QWebPage::errorPageExtensionInIFrames()
2385 ErrorPage* page = new ErrorPage;
2386 m_view->setPage(page);
2388 m_view->page()->mainFrame()->load(QUrl(
2391 "<iframe src='data:text/html,<p/>p'></iframe>"
2392 "<iframe src='http://non.existent/url'></iframe>"));
2393 QSignalSpy spyLoadFinished(m_view, SIGNAL(loadFinished(bool)));
2394 QTRY_COMPARE(spyLoadFinished.count(), 1);
2396 QCOMPARE(page->mainFrame()->childFrames()[1]->toPlainText(), QString("error"));
2401 void tst_QWebPage::errorPageExtensionInFrameset()
2403 ErrorPage* page = new ErrorPage;
2404 m_view->setPage(page);
2406 m_view->load(QUrl("qrc:///resources/index.html"));
2408 QSignalSpy spyLoadFinished(m_view, SIGNAL(loadFinished(bool)));
2409 QTRY_COMPARE(spyLoadFinished.count(), 1);
2410 QCOMPARE(page->mainFrame()->childFrames()[1]->toPlainText(), QString("error"));
2415 class FriendlyWebPage : public QWebPage
2418 friend class tst_QWebPage;
2421 void tst_QWebPage::userAgentApplicationName()
2423 const QString oldApplicationName = QCoreApplication::applicationName();
2424 FriendlyWebPage page;
2426 const QString applicationNameMarker = QString::fromUtf8("StrangeName\342\210\236");
2427 QCoreApplication::setApplicationName(applicationNameMarker);
2428 QVERIFY(page.userAgentForUrl(QUrl()).contains(applicationNameMarker));
2430 QCoreApplication::setApplicationName(oldApplicationName);
2433 void tst_QWebPage::crashTests_LazyInitializationOfMainFrame()
2440 webPage.selectedText();
2444 webPage.selectedHtml();
2448 webPage.triggerAction(QWebPage::Back, true);
2453 webPage.updatePositionDependentActions(pos);
2457 static void takeScreenshot(QWebPage* page)
2459 QWebFrame* mainFrame = page->mainFrame();
2460 page->setViewportSize(mainFrame->contentsSize());
2461 QImage image(page->viewportSize(), QImage::Format_ARGB32);
2462 QPainter painter(&image);
2463 mainFrame->render(&painter);
2467 void tst_QWebPage::screenshot_data()
2469 QTest::addColumn<QString>("html");
2470 QTest::newRow("WithoutPlugin") << "<html><body id='b'>text</body></html>";
2471 QTest::newRow("WindowedPlugin") << QString("<html><body id='b'>text<embed src='resources/test.swf'></embed></body></html>");
2472 QTest::newRow("WindowlessPlugin") << QString("<html><body id='b'>text<embed src='resources/test.swf' wmode='transparent'></embed></body></html>");
2475 void tst_QWebPage::screenshot()
2477 if (!QDir(TESTS_SOURCE_DIR).exists())
2478 QSKIP(QString("This test requires access to resources found in '%1'").arg(TESTS_SOURCE_DIR).toLatin1().constData(), SkipAll);
2480 QDir::setCurrent(TESTS_SOURCE_DIR);
2482 QFETCH(QString, html);
2483 QWebPage* page = new QWebPage;
2484 page->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
2485 QWebFrame* mainFrame = page->mainFrame();
2486 mainFrame->setHtml(html, QUrl::fromLocalFile(TESTS_SOURCE_DIR));
2487 ::waitForSignal(mainFrame, SIGNAL(loadFinished(bool)), 2000);
2489 // take screenshot without a view
2490 takeScreenshot(page);
2492 QWebView* view = new QWebView;
2493 view->setPage(page);
2495 // take screenshot when attached to a view
2496 takeScreenshot(page);
2501 QDir::setCurrent(QApplication::applicationDirPath());
2504 #if defined(ENABLE_WEBGL) && ENABLE_WEBGL
2505 // https://bugs.webkit.org/show_bug.cgi?id=54138
2506 static void webGLScreenshotWithoutView(bool accelerated)
2509 page.settings()->setAttribute(QWebSettings::WebGLEnabled, true);
2510 page.settings()->setAttribute(QWebSettings::AcceleratedCompositingEnabled, accelerated);
2511 QWebFrame* mainFrame = page.mainFrame();
2512 mainFrame->setHtml("<html><body>"
2513 "<canvas id='webgl' width='300' height='300'></canvas>"
2514 "<script>document.getElementById('webgl').getContext('experimental-webgl')</script>"
2517 takeScreenshot(&page);
2520 void tst_QWebPage::acceleratedWebGLScreenshotWithoutView()
2522 webGLScreenshotWithoutView(true);
2525 void tst_QWebPage::unacceleratedWebGLScreenshotWithoutView()
2527 webGLScreenshotWithoutView(false);
2531 void tst_QWebPage::originatingObjectInNetworkRequests()
2533 TestNetworkManager* networkManager = new TestNetworkManager(m_page);
2534 m_page->setNetworkAccessManager(networkManager);
2535 networkManager->requests.clear();
2537 m_view->setHtml(QString("<frameset cols=\"25%,75%\"><frame src=\"data:text/html,"
2538 "<head><meta http-equiv='refresh' content='1'></head>foo \">"
2539 "<frame src=\"data:text/html,bar\"></frameset>"), QUrl());
2540 QVERIFY(::waitForSignal(m_view, SIGNAL(loadFinished(bool))));
2542 QCOMPARE(networkManager->requests.count(), 2);
2544 QList<QWebFrame*> childFrames = m_page->mainFrame()->childFrames();
2545 QCOMPARE(childFrames.count(), 2);
2547 for (int i = 0; i < 2; ++i)
2548 QVERIFY(qobject_cast<QWebFrame*>(networkManager->requests.at(i).originatingObject()) == childFrames.at(i));
2552 * Test fixups for https://bugs.webkit.org/show_bug.cgi?id=30914
2554 * From JS we test the following conditions.
2556 * OK + QString() => SUCCESS, empty string (but not null)
2557 * OK + "text" => SUCCESS, "text"
2558 * CANCEL + QString() => CANCEL, null string
2559 * CANCEL + "text" => CANCEL, null string
2561 class JSPromptPage : public QWebPage {
2567 bool javaScriptPrompt(QWebFrame* frame, const QString& msg, const QString& defaultValue, QString* result)
2569 if (msg == QLatin1String("test1")) {
2570 *result = QString();
2572 } else if (msg == QLatin1String("test2")) {
2573 *result = QLatin1String("text");
2575 } else if (msg == QLatin1String("test3")) {
2576 *result = QString();
2578 } else if (msg == QLatin1String("test4")) {
2579 *result = QLatin1String("text");
2583 qFatal("Unknown msg.");
2584 return QWebPage::javaScriptPrompt(frame, msg, defaultValue, result);
2588 void tst_QWebPage::testJSPrompt()
2594 res = page.mainFrame()->evaluateJavaScript(
2595 "var retval = prompt('test1');"
2596 "retval=='' && retval.length == 0;").toBool();
2600 res = page.mainFrame()->evaluateJavaScript(
2601 "var retval = prompt('test2');"
2602 "retval=='text' && retval.length == 4;").toBool();
2605 // Cancel + QString()
2606 res = page.mainFrame()->evaluateJavaScript(
2607 "var retval = prompt('test3');"
2608 "retval===null;").toBool();
2612 res = page.mainFrame()->evaluateJavaScript(
2613 "var retval = prompt('test4');"
2614 "retval===null;").toBool();
2618 class TestModalPage : public QWebPage
2622 TestModalPage(QObject* parent = 0) : QWebPage(parent) {
2624 virtual QWebPage* createWindow(WebWindowType) {
2625 QWebPage* page = new TestModalPage();
2626 connect(page, SIGNAL(windowCloseRequested()), page, SLOT(deleteLater()));
2631 void tst_QWebPage::showModalDialog()
2634 page.mainFrame()->setHtml(QString("<html></html>"));
2635 QString res = page.mainFrame()->evaluateJavaScript("window.showModalDialog('javascript:window.returnValue=dialogArguments; window.close();', 'This is a test');").toString();
2636 QCOMPARE(res, QString("This is a test"));
2639 void tst_QWebPage::testStopScheduledPageRefresh()
2641 // Without QWebPage::StopScheduledPageRefresh
2643 page1.setNetworkAccessManager(new TestNetworkManager(&page1));
2644 page1.mainFrame()->setHtml("<html><head>"
2645 "<meta http-equiv=\"refresh\"content=\"0;URL=qrc:///resources/index.html\">"
2646 "</head><body><h1>Page redirects immediately...</h1>"
2648 QVERIFY(::waitForSignal(&page1, SIGNAL(loadFinished(bool))));
2650 QCOMPARE(page1.mainFrame()->url(), QUrl(QLatin1String("qrc:///resources/index.html")));
2652 // With QWebPage::StopScheduledPageRefresh
2654 page2.setNetworkAccessManager(new TestNetworkManager(&page2));
2655 page2.mainFrame()->setHtml("<html><head>"
2656 "<meta http-equiv=\"refresh\"content=\"1;URL=qrc:///resources/index.html\">"
2657 "</head><body><h1>Page redirect test with 1 sec timeout...</h1>"
2659 page2.triggerAction(QWebPage::StopScheduledPageRefresh);
2661 QCOMPARE(page2.mainFrame()->url().toString(), QLatin1String("about:blank"));
2664 void tst_QWebPage::findText()
2666 m_view->setHtml(QString("<html><head></head><body><div>foo bar</div></body></html>"));
2667 m_page->triggerAction(QWebPage::SelectAll);
2668 QVERIFY(!m_page->selectedText().isEmpty());
2669 QVERIFY(!m_page->selectedHtml().isEmpty());
2670 m_page->findText("");
2671 QVERIFY(m_page->selectedText().isEmpty());
2672 QVERIFY(m_page->selectedHtml().isEmpty());
2673 QStringList words = (QStringList() << "foo" << "bar");
2674 foreach (QString subString, words) {
2675 m_page->findText(subString, QWebPage::FindWrapsAroundDocument);
2676 QCOMPARE(m_page->selectedText(), subString);
2677 QCOMPARE(m_page->selectedHtml(), QString("<span class=\"Apple-style-span\" style=\"border-collapse: separate; color: rgb(0, 0, 0); font-family: Times; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; \">%1</span>").arg(subString));
2678 m_page->findText("");
2679 QVERIFY(m_page->selectedText().isEmpty());
2680 QVERIFY(m_page->selectedHtml().isEmpty());
2684 struct ImageExtensionMap {
2685 const char* extension;
2686 const char* mimeType;
2689 static const ImageExtensionMap extensionMap[] = {
2690 { "bmp", "image/bmp" },
2691 { "css", "text/css" },
2692 { "gif", "image/gif" },
2693 { "html", "text/html" },
2694 { "htm", "text/html" },
2695 { "ico", "image/x-icon" },
2696 { "jpeg", "image/jpeg" },
2697 { "jpg", "image/jpeg" },
2698 { "js", "application/x-javascript" },
2699 { "mng", "video/x-mng" },
2700 { "pbm", "image/x-portable-bitmap" },
2701 { "pgm", "image/x-portable-graymap" },
2702 { "pdf", "application/pdf" },
2703 { "png", "image/png" },
2704 { "ppm", "image/x-portable-pixmap" },
2705 { "rss", "application/rss+xml" },
2706 { "svg", "image/svg+xml" },
2707 { "text", "text/plain" },
2708 { "tif", "image/tiff" },
2709 { "tiff", "image/tiff" },
2710 { "txt", "text/plain" },
2711 { "xbm", "image/x-xbitmap" },
2712 { "xml", "text/xml" },
2713 { "xpm", "image/x-xpm" },
2714 { "xsl", "text/xsl" },
2715 { "xhtml", "application/xhtml+xml" },
2716 { "wml", "text/vnd.wap.wml" },
2717 { "wmlc", "application/vnd.wap.wmlc" },
2721 static QString getMimeTypeForExtension(const QString &ext)
2723 const ImageExtensionMap *e = extensionMap;
2724 while (e->extension) {
2725 if (ext.compare(QLatin1String(e->extension), Qt::CaseInsensitive) == 0)
2726 return QLatin1String(e->mimeType);
2733 void tst_QWebPage::supportedContentType()
2735 QStringList contentTypes;
2737 // Add supported non image types...
2738 contentTypes << "text/html" << "text/xml" << "text/xsl" << "text/plain" << "text/"
2739 << "application/xml" << "application/xhtml+xml" << "application/vnd.wap.xhtml+xml"
2740 << "application/rss+xml" << "application/atom+xml" << "application/json";
2742 // Add supported image types...
2743 Q_FOREACH(const QByteArray& imageType, QImageWriter::supportedImageFormats()) {
2744 const QString mimeType = getMimeTypeForExtension(imageType);
2745 if (!mimeType.isEmpty())
2746 contentTypes << mimeType;
2749 // Get the mime types supported by webkit...
2750 const QStringList supportedContentTypes = m_page->supportedContentTypes();
2752 Q_FOREACH(const QString& mimeType, contentTypes)
2753 QVERIFY2(supportedContentTypes.contains(mimeType), QString("'%1' is not a supported content type!").arg(mimeType).toLatin1());
2755 Q_FOREACH(const QString& mimeType, contentTypes)
2756 QVERIFY2(m_page->supportsContentType(mimeType), QString("Cannot handle content types '%1'!").arg(mimeType).toLatin1());
2760 void tst_QWebPage::navigatorCookieEnabled()
2762 m_page->networkAccessManager()->setCookieJar(0);
2763 QVERIFY(!m_page->networkAccessManager()->cookieJar());
2764 QVERIFY(!m_page->mainFrame()->evaluateJavaScript("navigator.cookieEnabled").toBool());
2766 m_page->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
2767 QVERIFY(m_page->networkAccessManager()->cookieJar());
2768 QVERIFY(m_page->mainFrame()->evaluateJavaScript("navigator.cookieEnabled").toBool());
2772 void tst_QWebPage::macCopyUnicodeToClipboard()
2774 QString unicodeText = QString::fromUtf8("αβγδεζηθικλμπ");
2775 m_page->mainFrame()->setHtml(QString("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>%1</body></html>").arg(unicodeText));
2776 m_page->triggerAction(QWebPage::SelectAll);
2777 m_page->triggerAction(QWebPage::Copy);
2779 QString clipboardData = QString::fromUtf8(QApplication::clipboard()->mimeData()->data(QLatin1String("text/html")));
2781 QVERIFY(clipboardData.contains(QLatin1String("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />")));
2782 QVERIFY(clipboardData.contains(unicodeText));
2786 void tst_QWebPage::contextMenuCopy()
2790 view.setHtml("<a href=\"http://www.google.com\">You cant miss this</a>");
2792 view.page()->triggerAction(QWebPage::SelectAll);
2793 QVERIFY(!view.page()->selectedText().isEmpty());
2795 QWebElement link = view.page()->mainFrame()->findFirstElement("a");
2796 QPoint pos(link.geometry().center());
2797 QContextMenuEvent event(QContextMenuEvent::Mouse, pos);
2798 view.page()->swallowContextMenuEvent(&event);
2799 view.page()->updatePositionDependentActions(pos);
2801 QList<QMenu*> contextMenus = view.findChildren<QMenu*>();
2802 QVERIFY(!contextMenus.isEmpty());
2803 QMenu* contextMenu = contextMenus.first();
2804 QVERIFY(contextMenu);
2806 QList<QAction *> list = contextMenu->actions();
2807 int index = list.indexOf(view.page()->action(QWebPage::Copy));
2808 QVERIFY(index != -1);
2811 void tst_QWebPage::deleteQWebViewTwice()
2813 for (int i = 0; i < 2; ++i) {
2814 QMainWindow mainWindow;
2815 QWebView* webView = new QWebView(&mainWindow);
2816 mainWindow.setCentralWidget(webView);
2817 webView->load(QUrl("qrc:///resources/frame_a.html"));
2819 connect(webView, SIGNAL(loadFinished(bool)), &mainWindow, SLOT(close()));
2820 QApplication::instance()->exec();
2824 class RepaintRequestedRenderer : public QObject {
2827 RepaintRequestedRenderer(QWebPage* page, QPainter* painter)
2829 , m_painter(painter)
2830 , m_recursionCount(0)
2832 connect(m_page, SIGNAL(repaintRequested(QRect)), this, SLOT(onRepaintRequested(QRect)));
2839 void onRepaintRequested(const QRect& rect)
2841 QCOMPARE(m_recursionCount, 0);
2844 m_page->mainFrame()->render(m_painter, rect);
2847 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
2852 QPainter* m_painter;
2853 int m_recursionCount;
2856 void tst_QWebPage::renderOnRepaintRequestedShouldNotRecurse()
2858 QSize viewportSize(720, 576);
2861 QImage image(viewportSize, QImage::Format_ARGB32);
2862 QPainter painter(&image);
2864 page.setPreferredContentsSize(viewportSize);
2865 page.setViewportSize(viewportSize);
2866 RepaintRequestedRenderer r(&page, &painter);
2868 page.mainFrame()->setHtml("zalan loves trunk", QUrl());
2870 QVERIFY(::waitForSignal(&r, SIGNAL(finished())));
2873 QTEST_MAIN(tst_QWebPage)
2874 #include "tst_qwebpage.moc"