Warning, file /frameworks/khtml/tests/testkhtml.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // program to test the new khtml implementation
0002 
0003 #include "testkhtml.h"
0004 
0005 #include <stdlib.h>
0006 #include <QApplication>
0007 #include "khtmlview.h"
0008 #include "html_document.h"
0009 #include "htmltokenizer.h"
0010 // to be able to delete a static protected member pointer in kbrowser...
0011 // just for memory debugging
0012 #define protected public
0013 #include "khtml_part.h"
0014 #undef protected
0015 #include "misc/loader.h"
0016 #include <QAction>
0017 #include <QCursor>
0018 #include <QDomDocument>
0019 #include <QFileDialog>
0020 #include <dom_string.h>
0021 #include "dom/dom2_range.h"
0022 #include "dom/html_document.h"
0023 #include "dom/dom_exception.h"
0024 #include <stdio.h>
0025 #include "css/cssstyleselector.h"
0026 #include "html/html_imageimpl.h"
0027 #include "rendering/render_style.h"
0028 #include "khtml_global.h"
0029 #include <kxmlguiwindow.h>
0030 
0031 #include <ktoggleaction.h>
0032 #include <kactioncollection.h>
0033 #include "kxmlguifactory.h"
0034 #include <ksharedconfig.h>
0035 #include <kconfiggroup.h>
0036 
0037 int main(int argc, char *argv[])
0038 {
0039     QApplication a(argc, argv);
0040 
0041     if (a.arguments().count() <= 1) {
0042         qWarning() << "Argument expected: url to open";
0043         return 1;
0044     }
0045 
0046     new KHTMLGlobal;
0047 
0048     KXmlGuiWindow *toplevel = new KXmlGuiWindow();
0049     KHTMLPart *doc = new KHTMLPart(toplevel, toplevel, KHTMLPart::BrowserViewGUI);
0050 
0051     Dummy *dummy = new Dummy(doc);
0052     QObject::connect(doc->browserExtension(), SIGNAL(openUrlRequest(QUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)),
0053                      dummy, SLOT(slotOpenURL(QUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)));
0054 
0055     QObject::connect(doc, SIGNAL(completed()), dummy, SLOT(handleDone()));
0056 
0057     QUrl url = QUrl::fromUserInput(a.arguments().at(1)); // TODO support for relative paths
0058     if (url.path().right(4).toLower() == ".xml") {
0059         KParts::OpenUrlArguments args(doc->arguments());
0060         args.setMimeType("text/xml");
0061         doc->setArguments(args);
0062     }
0063 
0064     doc->openUrl(url);
0065 
0066     toplevel->setCentralWidget(doc->widget());
0067     toplevel->resize(800, 600);
0068 
0069     QDomDocument d = doc->domDocument();
0070     QDomElement viewMenu = d.documentElement().firstChild().childNodes().item(2).toElement();
0071     QDomElement e = d.createElement("action");
0072     e.setAttribute("name", "debugRenderTree");
0073     viewMenu.appendChild(e);
0074     e = d.createElement("action");
0075     e.setAttribute("name", "debugDOMTree");
0076     viewMenu.appendChild(e);
0077     e = d.createElement("action");
0078     e.setAttribute("name", "debugDoBenchmark");
0079     viewMenu.appendChild(e);
0080 
0081     QDomElement toolBar = d.documentElement().firstChild().nextSibling().toElement();
0082     e = d.createElement("action");
0083     e.setAttribute("name", "editable");
0084     toolBar.insertBefore(e, toolBar.firstChild());
0085     e = d.createElement("action");
0086     e.setAttribute("name", "navigable");
0087     toolBar.insertBefore(e, toolBar.firstChild());
0088     e = d.createElement("action");
0089     e.setAttribute("name", "reload");
0090     toolBar.insertBefore(e, toolBar.firstChild());
0091     e = d.createElement("action");
0092     e.setAttribute("name", "print");
0093     toolBar.insertBefore(e, toolBar.firstChild());
0094 
0095     QAction *action = new QAction(QIcon::fromTheme("view-refresh"),  "Reload", doc);
0096     doc->actionCollection()->addAction("reload", action);
0097     QObject::connect(action, SIGNAL(triggered(bool)), dummy, SLOT(reload()));
0098     doc->actionCollection()->setDefaultShortcut(action, Qt::Key_F5);
0099 
0100     QAction *bench = new QAction(QIcon(), "Benchmark...", doc);
0101     doc->actionCollection()->addAction("debugDoBenchmark", bench);
0102     QObject::connect(bench, SIGNAL(triggered(bool)), dummy, SLOT(doBenchmark()));
0103 
0104     QAction *kprint = new QAction(QIcon::fromTheme("document-print"),  "Print", doc);
0105     doc->actionCollection()->addAction("print", kprint);
0106     QObject::connect(kprint, SIGNAL(triggered(bool)), doc->browserExtension(), SLOT(print()));
0107     kprint->setEnabled(true);
0108     KToggleAction *ta = new KToggleAction(QIcon::fromTheme("edit-rename"), "Navigable", doc);
0109     doc->actionCollection()->addAction("navigable", ta);
0110     ta->setShortcuts(QList<QKeySequence>());
0111     ta->setChecked(doc->isCaretMode());
0112     QWidget::connect(ta, SIGNAL(toggled(bool)), dummy, SLOT(toggleNavigable(bool)));
0113     ta = new KToggleAction(QIcon::fromTheme("document-properties"), "Editable", doc);
0114     doc->actionCollection()->addAction("editable", ta);
0115     ta->setShortcuts(QList<QKeySequence>());
0116     ta->setChecked(doc->isEditable());
0117     QWidget::connect(ta, SIGNAL(toggled(bool)), dummy, SLOT(toggleEditable(bool)));
0118     toplevel->guiFactory()->addClient(doc);
0119 
0120     doc->setJScriptEnabled(true);
0121     doc->setJavaEnabled(true);
0122     doc->setPluginsEnabled(true);
0123     doc->setURLCursor(QCursor(Qt::PointingHandCursor));
0124     a.setActiveWindow(doc->widget());
0125     QWidget::connect(doc, SIGNAL(setWindowCaption(QString)),
0126                      doc->widget()->topLevelWidget(), SLOT(setCaption(QString)));
0127     doc->widget()->show();
0128     toplevel->show();
0129     doc->view()->viewport()->show();
0130     doc->view()->widget()->show();
0131 
0132     int ret = a.exec();
0133     return ret;
0134 }
0135 
0136 void Dummy::doBenchmark()
0137 {
0138     KConfigGroup settings(KSharedConfig::openConfig(), "bench");
0139     results.clear();
0140 
0141     const QString startDir = settings.readPathEntry("path", QString());
0142     QString directory = QFileDialog::getExistingDirectory(m_part->view(),
0143                         QString::fromLatin1("Please select directory with tests"),
0144                         startDir);
0145 
0146     if (!directory.isEmpty()) {
0147         settings.writePathEntry("path", directory);
0148         KSharedConfig::openConfig()->sync();
0149 
0150         QDir dirListing(directory, "*.html");
0151         for (unsigned i = 0; i < dirListing.count(); ++i) {
0152             filesToBenchmark.append(dirListing.absoluteFilePath(dirListing[i]));
0153         }
0154     }
0155 
0156     benchmarkRun = 0;
0157 
0158     if (!filesToBenchmark.isEmpty()) {
0159         nextRun();
0160     }
0161 }
0162 
0163 const int COLD_RUNS = 2;
0164 const int HOT_RUNS  = 6;
0165 
0166 void Dummy::nextRun()
0167 {
0168     if (benchmarkRun == (COLD_RUNS + HOT_RUNS)) {
0169         filesToBenchmark.removeFirst();
0170         benchmarkRun = 0;
0171     }
0172 
0173     if (!filesToBenchmark.isEmpty()) {
0174         loadTimer.start();
0175         m_part->openUrl(QUrl::fromLocalFile(filesToBenchmark[0]));
0176     } else {
0177         //Generate HTML for report.
0178         m_part->begin();
0179         m_part->write("<html><body><table border=1>");
0180 
0181         for (QMap<QString, QList<int> >::iterator i = results.begin(); i != results.end(); ++i) {
0182             m_part->write("<tr><td>" + i.key() + "</td>");
0183             QList<int> timings = i.value();
0184             int total = 0;
0185             for (int pos = 0; pos < timings.size(); ++pos) {
0186                 int t = timings[pos];
0187                 if (pos < COLD_RUNS) {
0188                     m_part->write(QString::fromLatin1("<td>(Cold):") + QString::number(t) + "</td>");
0189                 } else {
0190                     total += t;
0191                     m_part->write(QString::fromLatin1("<td><i>") + QString::number(t) + "</i></td>");
0192                 }
0193             }
0194 
0195             m_part->write(QString::fromLatin1("<td>Average:<b>") + QString::number(double(total) / HOT_RUNS) + "</b></td>");
0196 
0197             m_part->write("</tr>");
0198         }
0199 
0200         m_part->end();
0201     }
0202 }
0203 
0204 void Dummy::handleDone()
0205 {
0206     if (filesToBenchmark.isEmpty()) {
0207         return;
0208     }
0209 
0210     results[filesToBenchmark[0]].append(loadTimer.elapsed());
0211     ++benchmarkRun;
0212     QTimer::singleShot(100, this, SLOT(nextRun()));
0213 }
0214 
0215 #include "moc_testkhtml.cpp"