⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tst_qwebpage.cpp

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    QVERIFY(m_page->history()->backItem().isValid());    QVERIFY(!m_page->history()->forwardItem().isValid());    m_page->history()->back();    QVERIFY(::waitForSignal(m_view, SIGNAL(loadFinished(bool))));    QVERIFY(!m_page->history()->canGoBack());    QVERIFY(m_page->history()->canGoForward());    QVERIFY(!m_page->isModified());    QVERIFY(m_page->history()->currentItemIndex() == 0);    m_page->history()->setMaximumItemCount(3);    QVERIFY(m_page->history()->maximumItemCount() == 3);    QVariant variant("string test");    m_page->history()->currentItem().setUserData(variant);    QVERIFY(m_page->history()->currentItem().userData().toString() == "string test");    m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>This is second page"));    m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>This is third page"));    QVERIFY(m_page->history()->count() == 2);    m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>This is fourth page"));    QVERIFY(m_page->history()->count() == 2);    m_page->mainFrame()->setUrl(QUrl("data:text/html,<body>This is fifth page"));    QVERIFY(::waitForSignal(m_page, SIGNAL(saveFrameStateRequested(QWebFrame*, QWebHistoryItem*))));}void tst_QWebPage::contextMenuCrash(){    QWebView view;    view.setHtml("<p>test");    view.page()->updatePositionDependentActions(QPoint(0, 0));    QMenu* contextMenu = 0;    foreach (QObject* child, view.children()) {        contextMenu = qobject_cast<QMenu*>(child);        if (contextMenu)            break;    }    QVERIFY(contextMenu);    delete contextMenu;}void tst_QWebPage::database(){    QString path = QDir::currentPath();    m_page->settings()->setOfflineStoragePath(path);    QVERIFY(m_page->settings()->offlineStoragePath() == path);    QWebSettings::setOfflineStorageDefaultQuota(1024 * 1024);    QVERIFY(QWebSettings::offlineStorageDefaultQuota() == 1024 * 1024);    QString dbFileName = path + "Databases.db";    if (QFile::exists(dbFileName))        QFile::remove(dbFileName);    qRegisterMetaType<QWebFrame*>("QWebFrame*");    QSignalSpy spy(m_page, SIGNAL(databaseQuotaExceeded(QWebFrame *, QString)));    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"));    QTRY_COMPARE(spy.count(), 1);    m_page->mainFrame()->evaluateJavaScript("var db2; db2=openDatabase('testdb', '1.0', 'test database API', 50000);");    QTRY_COMPARE(spy.count(),1);    m_page->mainFrame()->evaluateJavaScript("localStorage.test='This is a test for local storage';");    m_view->setHtml(QString("<html><body id='b'>text</body></html>"), QUrl("http://www.myexample.com"));    QVariant s1 = m_page->mainFrame()->evaluateJavaScript("localStorage.test");    QCOMPARE(s1.toString(), QString("This is a test for local storage"));    m_page->mainFrame()->evaluateJavaScript("sessionStorage.test='This is a test for session storage';");    m_view->setHtml(QString("<html><body id='b'>text</body></html>"), QUrl("http://www.myexample.com"));    QVariant s2 = m_page->mainFrame()->evaluateJavaScript("sessionStorage.test");    QCOMPARE(s2.toString(), QString("This is a test for session storage"));    m_view->setHtml(QString("<html><head></head><body><div></div></body></html>"), QUrl("http://www.myexample.com"));    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) { });");    QTest::qWait(200);    QWebSecurityOrigin origin = m_page->mainFrame()->securityOrigin();    QList<QWebDatabase> dbs = origin.databases();    if (dbs.count() > 0) {        QString fileName = dbs[0].fileName();        QVERIFY(QFile::exists(fileName));        QWebDatabase::removeDatabase(dbs[0]);        QVERIFY(!QFile::exists(fileName));    }    QTest::qWait(1000);}class PluginPage : public QWebPage{public:    PluginPage(QObject *parent = 0)        : QWebPage(parent) {}    struct CallInfo    {        CallInfo(const QString &c, const QUrl &u,                 const QStringList &pn, const QStringList &pv,                 QObject *r)            : classid(c), url(u), paramNames(pn),              paramValues(pv), returnValue(r)            {}        QString classid;        QUrl url;        QStringList paramNames;        QStringList paramValues;        QObject *returnValue;    };    QList<CallInfo> calls;protected:    virtual QObject *createPlugin(const QString &classid, const QUrl &url,                                  const QStringList &paramNames,                                  const QStringList &paramValues)    {        QObject *result = 0;        if (classid == "pushbutton")            result = new QPushButton();        else if (classid == "lineedit")            result = new QLineEdit();        if (result)            result->setObjectName(classid);        calls.append(CallInfo(classid, url, paramNames, paramValues, result));        return result;    }};void tst_QWebPage::createPlugin(){    QSignalSpy loadSpy(m_view, SIGNAL(loadFinished(bool)));    PluginPage* newPage = new PluginPage(m_view);    m_view->setPage(newPage);    // plugins not enabled by default, so the plugin shouldn't be loaded    m_view->setHtml(QString("<html><body><object type='application/x-qt-plugin' classid='pushbutton' id='mybutton'/></body></html>"));    QTRY_COMPARE(loadSpy.count(), 1);    QCOMPARE(newPage->calls.count(), 0);    m_view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);    // type has to be application/x-qt-plugin    m_view->setHtml(QString("<html><body><object type='application/x-foobarbaz' classid='pushbutton' id='mybutton'/></body></html>"));    QTRY_COMPARE(loadSpy.count(), 2);    QCOMPARE(newPage->calls.count(), 0);    m_view->setHtml(QString("<html><body><object type='application/x-qt-plugin' classid='pushbutton' id='mybutton'/></body></html>"));    QTRY_COMPARE(loadSpy.count(), 3);    QCOMPARE(newPage->calls.count(), 1);    {        PluginPage::CallInfo ci = newPage->calls.takeFirst();        QCOMPARE(ci.classid, QString::fromLatin1("pushbutton"));        QCOMPARE(ci.url, QUrl());        QCOMPARE(ci.paramNames.count(), 3);        QCOMPARE(ci.paramValues.count(), 3);        QCOMPARE(ci.paramNames.at(0), QString::fromLatin1("type"));        QCOMPARE(ci.paramValues.at(0), QString::fromLatin1("application/x-qt-plugin"));        QCOMPARE(ci.paramNames.at(1), QString::fromLatin1("classid"));        QCOMPARE(ci.paramValues.at(1), QString::fromLatin1("pushbutton"));        QCOMPARE(ci.paramNames.at(2), QString::fromLatin1("id"));        QCOMPARE(ci.paramValues.at(2), QString::fromLatin1("mybutton"));        QVERIFY(ci.returnValue != 0);        QVERIFY(ci.returnValue->inherits("QPushButton"));    }    // test JS bindings    QCOMPARE(newPage->mainFrame()->evaluateJavaScript("document.getElementById('mybutton').toString()").toString(),             QString::fromLatin1("[object HTMLObjectElement]"));    QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mybutton.toString()").toString(),             QString::fromLatin1("[object HTMLObjectElement]"));    QCOMPARE(newPage->mainFrame()->evaluateJavaScript("typeof mybutton.objectName").toString(),             QString::fromLatin1("string"));    QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mybutton.objectName").toString(),             QString::fromLatin1("pushbutton"));    QCOMPARE(newPage->mainFrame()->evaluateJavaScript("typeof mybutton.clicked").toString(),             QString::fromLatin1("function"));    QCOMPARE(newPage->mainFrame()->evaluateJavaScript("mybutton.clicked.toString()").toString(),             QString::fromLatin1("function clicked() {\n    [native code]\n}"));    m_view->setHtml(QString("<html><body><table>"                            "<tr><object type='application/x-qt-plugin' classid='lineedit' id='myedit'/></tr>"                            "<tr><object type='application/x-qt-plugin' classid='pushbutton' id='mybutton'/></tr>"                            "</table></body></html>"), QUrl("http://foo.bar.baz"));    QTRY_COMPARE(loadSpy.count(), 4);    QCOMPARE(newPage->calls.count(), 2);    {        PluginPage::CallInfo ci = newPage->calls.takeFirst();        QCOMPARE(ci.classid, QString::fromLatin1("lineedit"));        QCOMPARE(ci.url, QUrl());        QCOMPARE(ci.paramNames.count(), 3);        QCOMPARE(ci.paramValues.count(), 3);        QCOMPARE(ci.paramNames.at(0), QString::fromLatin1("type"));        QCOMPARE(ci.paramValues.at(0), QString::fromLatin1("application/x-qt-plugin"));        QCOMPARE(ci.paramNames.at(1), QString::fromLatin1("classid"));        QCOMPARE(ci.paramValues.at(1), QString::fromLatin1("lineedit"));        QCOMPARE(ci.paramNames.at(2), QString::fromLatin1("id"));        QCOMPARE(ci.paramValues.at(2), QString::fromLatin1("myedit"));        QVERIFY(ci.returnValue != 0);        QVERIFY(ci.returnValue->inherits("QLineEdit"));    }    {        PluginPage::CallInfo ci = newPage->calls.takeFirst();        QCOMPARE(ci.classid, QString::fromLatin1("pushbutton"));        QCOMPARE(ci.url, QUrl());        QCOMPARE(ci.paramNames.count(), 3);        QCOMPARE(ci.paramValues.count(), 3);        QCOMPARE(ci.paramNames.at(0), QString::fromLatin1("type"));        QCOMPARE(ci.paramValues.at(0), QString::fromLatin1("application/x-qt-plugin"));        QCOMPARE(ci.paramNames.at(1), QString::fromLatin1("classid"));        QCOMPARE(ci.paramValues.at(1), QString::fromLatin1("pushbutton"));        QCOMPARE(ci.paramNames.at(2), QString::fromLatin1("id"));        QCOMPARE(ci.paramValues.at(2), QString::fromLatin1("mybutton"));        QVERIFY(ci.returnValue != 0);        QVERIFY(ci.returnValue->inherits("QPushButton"));    }    m_view->settings()->setAttribute(QWebSettings::PluginsEnabled, false);    m_view->setHtml(QString("<html><body><object type='application/x-qt-plugin' classid='pushbutton' id='mybutton'/></body></html>"));    QTRY_COMPARE(loadSpy.count(), 5);    QCOMPARE(newPage->calls.count(), 0);}class PluginTrackedPage : public QWebPage{public:    int count;    QPointer<QWidget> widget;    PluginTrackedPage(QWidget *parent = 0) : QWebPage(parent), count(0) {       settings()->setAttribute(QWebSettings::PluginsEnabled, true);    }    virtual QObject* createPlugin(const QString&, const QUrl&, const QStringList&, const QStringList&) {       count++;       QWidget *w = new QWidget;       widget = w;       return w;    }};void tst_QWebPage::destroyPlugin(){    PluginTrackedPage* page = new PluginTrackedPage(m_view);    m_view->setPage(page);    // we create the plugin, so the widget should be constructed    QString content("<html><body><object type=\"application/x-qt-plugin\" classid=\"QProgressBar\"></object></body></html>");    m_view->setHtml(content);    QVERIFY(page->widget != 0);    QCOMPARE(page->count, 1);    // navigate away, the plugin widget should be destructed    m_view->setHtml("<html><body>Hi</body></html>");    QTestEventLoop::instance().enterLoop(1);    QVERIFY(page->widget == 0);}void tst_QWebPage::createViewlessPlugin(){    PluginTrackedPage* page = new PluginTrackedPage;    QString content("<html><body><object type=\"application/x-qt-plugin\" classid=\"QProgressBar\"></object></body></html>");    page->mainFrame()->setHtml(content);    QCOMPARE(page->count, 1);    QVERIFY(page->widget != 0);    delete page;}// import private APIvoid QWEBKIT_EXPORT qt_webpage_setGroupName(QWebPage* page, const QString& groupName);QString QWEBKIT_EXPORT qt_webpage_groupName(QWebPage* page);void QWEBKIT_EXPORT qt_websettings_setLocalStorageDatabasePath(QWebSettings* settings, const QString& path);void tst_QWebPage::multiplePageGroupsAndLocalStorage(){    QDir dir(QDir::currentPath());    dir.mkdir("path1");    dir.mkdir("path2");    QWebView view1;    QWebView view2;    qt_websettings_setLocalStorageDatabasePath(view1.page()->settings(), QDir::toNativeSeparators(QDir::currentPath() + "/path1"));    qt_webpage_setGroupName(view1.page(), "group1");    qt_websettings_setLocalStorageDatabasePath(view2.page()->settings(), QDir::toNativeSeparators(QDir::currentPath() + "/path2"));    qt_webpage_setGroupName(view2.page(), "group2");    QCOMPARE(qt_webpage_groupName(view1.page()), QString("group1"));    QCOMPARE(qt_webpage_groupName(view2.page()), QString("group2"));    view1.setHtml(QString("<html><body> </body></html>"), QUrl("http://www.myexample.com"));    view2.setHtml(QString("<html><body> </body></html>"), QUrl("http://www.myexample.com"));    view1.page()->mainFrame()->evaluateJavaScript("localStorage.test='value1';");    view2.page()->mainFrame()->evaluateJavaScript("localStorage.test='value2';");    view1.setHtml(QString("<html><body> </body></html>"), QUrl("http://www.myexample.com"));    view2.setHtml(QString("<html><body> </body></html>"), QUrl("http://www.myexample.com"));    QVariant s1 = view1.page()->mainFrame()->evaluateJavaScript("localStorage.test");    QCOMPARE(s1.toString(), QString("value1"));    QVariant s2 = view2.page()->mainFrame()->evaluateJavaScript("localStorage.test");    QCOMPARE(s2.toString(), QString("value2"));    QTest::qWait(1000);    QFile::remove(QDir::toNativeSeparators(QDir::currentPath() + "/path1/http_www.myexample.com_0.localstorage"));    QFile::remove(QDir::toNativeSeparators(QDir::currentPath() + "/path2/http_www.myexample.com_0.localstorage"));    dir.rmdir(QDir::toNativeSeparators("./path1"));    dir.rmdir(QDir::toNativeSeparators("./path2"));}class CursorTrackedPage : public QWebPage{public:    CursorTrackedPage(QWidget *parent = 0): QWebPage(parent) {        setViewportSize(QSize(1024, 768)); // big space    }    QString selectedText() {        return mainFrame()->evaluateJavaScript("window.getSelection().toString()").toString();    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -