fix logic
[personal-kdelibs.git] / khtml / testkhtml.cpp
blob811315dbbfc1e17e2f5c575c8df0edd6ae8f4441
1 // program to test the new khtml implementation
3 #include "testkhtml.h"
4 #include "testkhtml.moc"
6 #include <stdlib.h>
7 #include "kapplication.h"
8 #include "khtmlview.h"
9 #include "html_document.h"
10 #include "htmltokenizer.h"
11 // to be able to delete a static protected member pointer in kbrowser...
12 // just for memory debugging
13 #define protected public
14 #include "khtml_part.h"
15 #include "khtmlview.h"
16 #undef protected
17 #include "misc/loader.h"
18 #include <QtGui/QCursor>
19 #include <QDomDocument>
20 #include <dom_string.h>
21 #include "dom/dom2_range.h"
22 #include "dom/html_document.h"
23 #include "dom/dom_exception.h"
24 #include <stdio.h>
25 #include "css/cssstyleselector.h"
26 #include "html/html_imageimpl.h"
27 #include "rendering/render_style.h"
28 #include "khtml_global.h"
29 #include <kxmlguiwindow.h>
30 #include <kcmdlineargs.h>
31 #include <ktoggleaction.h>
32 #include <kactioncollection.h>
33 #include <kicon.h>
34 #include "kxmlguifactory.h"
35 #include <kfiledialog.h>
37 int main(int argc, char *argv[])
39 KCmdLineOptions options;
40 options.add("+file", ki18n("URL to open"));
42 KCmdLineArgs::init(argc, argv, "testkhtml", 0, ki18n("Testkhtml"),
43 "1.0", ki18n("a basic web browser using the KHTML library"));
44 KCmdLineArgs::addCmdLineOptions(options);
46 KApplication a;
47 KCmdLineArgs *args = KCmdLineArgs::parsedArgs( );
48 if ( args->count() == 0 ) {
49 KCmdLineArgs::usage();
50 ::exit( 1 );
53 KHTMLGlobal *fac = new KHTMLGlobal;
55 KXmlGuiWindow *toplevel = new KXmlGuiWindow();
56 KHTMLPart *doc = new KHTMLPart( toplevel, toplevel, KHTMLPart::BrowserViewGUI );
58 Dummy *dummy = new Dummy( doc );
59 QObject::connect( doc->browserExtension(), SIGNAL( openUrlRequest(const KUrl&, const KParts::OpenUrlArguments&, const KParts::BrowserArguments &) ),
60 dummy, SLOT( slotOpenURL( const KUrl&, const KParts::OpenUrlArguments&, const KParts::BrowserArguments& ) ) );
62 QObject::connect( doc, SIGNAL(completed()), dummy, SLOT(handleDone()) );
64 if (args->url(0).url().right(4).toLower() == ".xml") {
65 KParts::OpenUrlArguments args(doc->arguments());
66 args.setMimeType("text/xml");
67 doc->setArguments(args);
70 doc->openUrl( args->url(0) );
72 toplevel->setCentralWidget( doc->widget() );
73 toplevel->resize( 800, 600);
75 QDomDocument d = doc->domDocument();
76 QDomElement viewMenu = d.documentElement().firstChild().childNodes().item( 2 ).toElement();
77 QDomElement e = d.createElement( "action" );
78 e.setAttribute( "name", "debugRenderTree" );
79 viewMenu.appendChild( e );
80 e = d.createElement( "action" );
81 e.setAttribute( "name", "debugDOMTree" );
82 viewMenu.appendChild( e );
83 e = d.createElement( "action" );
84 e.setAttribute( "name", "debugDoBenchmark" );
85 viewMenu.appendChild( e );
87 QDomElement toolBar = d.documentElement().firstChild().nextSibling().toElement();
88 e = d.createElement( "action" );
89 e.setAttribute( "name", "editable" );
90 toolBar.insertBefore( e, toolBar.firstChild() );
91 e = d.createElement( "action" );
92 e.setAttribute( "name", "navigable" );
93 toolBar.insertBefore( e, toolBar.firstChild() );
94 e = d.createElement( "action" );
95 e.setAttribute( "name", "reload" );
96 toolBar.insertBefore( e, toolBar.firstChild() );
97 e = d.createElement( "action" );
98 e.setAttribute( "name", "print" );
99 toolBar.insertBefore( e, toolBar.firstChild() );
101 KAction *action = new KAction(KIcon("view-refresh"), "Reload", doc );
102 doc->actionCollection()->addAction( "reload", action );
103 QObject::connect(action, SIGNAL(triggered(bool)), dummy, SLOT( reload() ));
104 action->setShortcut(Qt::Key_F5);
106 KAction *bench = new KAction( KIcon(), "Benchmark...", doc );
107 doc->actionCollection()->addAction( "debugDoBenchmark", bench );
108 QObject::connect(bench, SIGNAL(triggered(bool)), dummy, SLOT( doBenchmark() ));
110 KAction *kprint = new KAction(KIcon("document-print"), "Print", doc );
111 doc->actionCollection()->addAction( "print", kprint );
112 QObject::connect(kprint, SIGNAL(triggered(bool)), doc->browserExtension(), SLOT( print() ));
113 kprint->setEnabled(true);
114 KToggleAction *ta = new KToggleAction( KIcon("edit-rename"), "Navigable", doc );
115 doc->actionCollection()->addAction( "navigable", ta );
116 ta->setShortcuts( KShortcut() );
117 ta->setChecked(doc->isCaretMode());
118 QWidget::connect(ta, SIGNAL(toggled(bool)), dummy, SLOT( toggleNavigable(bool) ));
119 ta = new KToggleAction( KIcon("document-properties"), "Editable", doc );
120 doc->actionCollection()->addAction( "editable", ta );
121 ta->setShortcuts( KShortcut() );
122 ta->setChecked(doc->isEditable());
123 QWidget::connect(ta, SIGNAL(toggled(bool)), dummy, SLOT( toggleEditable(bool) ));
124 toplevel->guiFactory()->addClient( doc );
126 doc->setJScriptEnabled(true);
127 doc->setJavaEnabled(true);
128 doc->setPluginsEnabled( true );
129 doc->setURLCursor(QCursor(Qt::PointingHandCursor));
130 a.setTopWidget(doc->widget());
131 QWidget::connect(doc, SIGNAL(setWindowCaption(const QString &)),
132 doc->widget()->topLevelWidget(), SLOT(setCaption(const QString &)));
133 doc->widget()->show();
134 toplevel->show();
135 doc->view()->viewport()->show();
136 doc->view()->widget()->show();
139 int ret = a.exec();
140 return ret;
143 void Dummy::doBenchmark()
145 KConfigGroup settings(KGlobal::config(), "bench");
146 results.clear();
148 QString directory = KFileDialog::getExistingDirectory(settings.readPathEntry("path", QString()), m_part->view(),
149 QString::fromLatin1("Please select directory with tests"));
151 if (!directory.isEmpty()) {
152 settings.writePathEntry("path", directory);
153 KGlobal::config()->sync();
155 QDir dirListing(directory, "*.html");
156 for (unsigned i = 0; i < dirListing.count(); ++i) {
157 filesToBenchmark.append(dirListing.absoluteFilePath(dirListing[i]));
161 benchmarkRun = 0;
163 if (!filesToBenchmark.isEmpty())
164 nextRun();
167 const int COLD_RUNS = 2;
168 const int HOT_RUNS = 6;
170 void Dummy::nextRun()
172 if (benchmarkRun == (COLD_RUNS + HOT_RUNS)) {
173 filesToBenchmark.removeFirst();
174 benchmarkRun = 0;
177 if (!filesToBenchmark.isEmpty()) {
178 loadTimer.start();
179 m_part->openUrl(filesToBenchmark[0]);
180 } else {
181 //Generate HTML for report.
182 m_part->begin();
183 m_part->write("<html><body><table border=1>");
185 for (QMap<QString, QList<int> >::iterator i = results.begin(); i != results.end(); ++i) {
186 m_part->write("<tr><td>" + i.key() + "</td>");
187 QList<int> timings = i.value();
188 int total = 0;
189 for (int pos = 0; pos < timings.size(); ++pos) {
190 int t = timings[pos];
191 if (pos < COLD_RUNS)
192 m_part->write(QString::fromLatin1("<td>(Cold):") + QString::number(t) + "</td>");
193 else {
194 total += t;
195 m_part->write(QString::fromLatin1("<td><i>") + QString::number(t) + "</i></td>");
199 m_part->write(QString::fromLatin1("<td>Average:<b>") + QString::number(double(total) / HOT_RUNS) + "</b></td>");
201 m_part->write("</tr>");
204 m_part->end();
208 void Dummy::handleDone()
210 if (filesToBenchmark.isEmpty()) return;
212 results[filesToBenchmark[0]].append(loadTimer.elapsed());
213 ++benchmarkRun;
214 QTimer::singleShot(100, this, SLOT(nextRun()));