1 /* This file is part of the KDE project
2 Copyright (C) 2008 David Faure <faure@kde.org>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <khtml_part.h>
20 #include <khtmlview.h>
21 #include <ktemporaryfile.h>
22 #include <kstandarddirs.h>
25 #include <QScrollArea>
26 #include <qtest_kde.h>
27 #include <qtest_gui.h>
29 #include <konqmainwindow.h>
30 #include <konqviewmanager.h>
35 class KonqHtmlTest
: public QObject
41 //qRegisterMetaType<KonqView *>("KonqView*");
43 void cleanupTestCase()
45 // in case some test broke, don't assert in khtmlglobal...
46 deleteAllMainWindows();
50 KonqMainWindow mainWindow
;
51 // we specify the mimetype so that we don't have to wait for a KonqRun
52 mainWindow
.openUrl(0, KUrl("data:text/html, <p>Hello World</p>"), "text/html");
53 KonqView
* view
= mainWindow
.currentView();
55 QVERIFY(view
->part());
56 QVERIFY(QTest::kWaitForSignal(view
, SIGNAL(viewCompleted(KonqView
*)), 20000));
57 QCOMPARE(view
->serviceType(), QString("text/html"));
58 //KHTMLPart* part = qobject_cast<KHTMLPart *>(view->part());
62 void loadDirectory() // #164495
64 KonqMainWindow mainWindow
;
65 mainWindow
.openUrl(0, KUrl(QDir::homePath()), "text/html");
66 KonqView
* view
= mainWindow
.currentView();
67 kDebug() << "Waiting for first completed signal";
68 QVERIFY(QTest::kWaitForSignal(view
, SIGNAL(viewCompleted(KonqView
*)), 20000)); // error calls openUrlRequest
69 kDebug() << "Waiting for first second signal";
70 QVERIFY(QTest::kWaitForSignal(view
, SIGNAL(viewCompleted(KonqView
*)), 20000)); // which then opens the right part
71 QCOMPARE(view
->serviceType(), QString("inode/directory"));
74 void rightClickClose() // #149736
76 QPointer
<KonqMainWindow
> mainWindow
= new KonqMainWindow
;
77 // we specify the mimetype so that we don't have to wait for a KonqRun
78 mainWindow
->openUrl(0, KUrl(
79 "data:text/html, <script type=\"text/javascript\">"
80 "function closeMe() { window.close(); } "
81 "document.onmousedown = closeMe; "
82 "</script>"), QString("text/html"));
83 QPointer
<KonqView
> view
= mainWindow
->currentView();
85 QVERIFY(QTest::kWaitForSignal(view
, SIGNAL(viewCompleted(KonqView
*)), 10000));
86 QWidget
* widget
= partWidget(view
);
87 qDebug() << "Clicking on" << widget
;
88 QTest::mousePress(widget
, Qt::RightButton
);
89 qApp
->processEvents();
90 QVERIFY(!view
); // deleted
91 QVERIFY(!mainWindow
); // the whole window gets deleted, in fact
96 // Simple test for window.open in a onmousedown handler.
98 // We have to use the same protocol for both the orig and dest urls.
99 // KAuthorized would forbid a data: URL to redirect to a file: URL for instance.
100 KTemporaryFile tempFile
;
101 QVERIFY(tempFile
.open());
102 tempFile
.write("<script>document.write(\"Opener=\" + window.opener);</script>");
104 KTemporaryFile origTempFile
;
105 QVERIFY(origTempFile
.open());
108 "function openWindow() { window.open('" + KUrl(tempFile
.fileName()).url().toUtf8() + "'); } "
109 "document.onmousedown = openWindow; "
113 const QString origFile
= origTempFile
.fileName();
114 origTempFile
.close();
116 KonqMainWindow
* mainWindow
= new KonqMainWindow
;
117 const QString profile
= KStandardDirs::locate("data", "konqueror/profiles/webbrowsing");
118 mainWindow
->viewManager()->loadViewProfileFromFile(profile
, "webbrowsing", KUrl(origFile
));
119 QCOMPARE(KMainWindow::memberList().count(), 1);
120 KonqView
* view
= mainWindow
->currentView();
122 QVERIFY(QTest::kWaitForSignal(view
, SIGNAL(viewCompleted(KonqView
*)), 10000));
123 qApp
->processEvents();
124 QWidget
* widget
= partWidget(view
);
125 kDebug() << "Clicking on the khtmlview";
126 QTest::mousePress(widget
, Qt::LeftButton
);
127 qApp
->processEvents(); // openurlrequestdelayed
128 qApp
->processEvents(); // browserrun
129 hideAllMainWindows(); // TODO: why does it appear nonetheless? hiding too early? hiding too late?
130 QTest::qWait(10); // just in case there's more delayed calls :)
131 // Did it open a window?
132 QCOMPARE(KMainWindow::memberList().count(), 2);
133 KMainWindow
* newWindow
= KMainWindow::memberList().last();
134 QVERIFY(newWindow
!= mainWindow
);
135 compareToolbarSettings(mainWindow
, newWindow
);
136 // Does the window contain exactly one tab?
137 QTabWidget
* tab
= newWindow
->findChild
<QTabWidget
*>();
139 QCOMPARE(tab
->count(), 1);
140 KonqFrame
* frame
= qobject_cast
<KonqFrame
*>(tab
->widget(0));
142 KHTMLPart
* part
= qobject_cast
<KHTMLPart
*>(frame
->part());
145 const QString text
= part
->selectedText();
146 QCOMPARE(text
, QString("Opener=[object Window]"));
147 deleteAllMainWindows();
151 // Return the main widget for the given KonqView; used for clicking onto it
152 static QWidget
* partWidget(KonqView
* view
)
154 QWidget
* widget
= view
->part()->widget();
155 KHTMLPart
* htmlPart
= qobject_cast
<KHTMLPart
*>(view
->part());
157 widget
= htmlPart
->view(); // khtmlview != widget() nowadays, due to find bar
158 if (QScrollArea
* scrollArea
= qobject_cast
<QScrollArea
*>(widget
))
159 widget
= scrollArea
->widget();
163 // Delete all KonqMainWindows
164 static void deleteAllMainWindows()
166 const QList
<KMainWindow
*> windows
= KMainWindow::memberList();
170 void compareToolbarSettings(KMainWindow
* mainWindow
, KMainWindow
* newWindow
)
172 QVERIFY(mainWindow
!= newWindow
);
173 KToolBar
* firstToolBar
= mainWindow
->toolBars().first();
174 QVERIFY(firstToolBar
);
175 KToolBar
* newFirstToolBar
= newWindow
->toolBars().first();
176 QVERIFY(newFirstToolBar
);
177 QCOMPARE(firstToolBar
->toolButtonStyle(), newFirstToolBar
->toolButtonStyle());
180 static void hideAllMainWindows()
182 const QList
<KMainWindow
*> windows
= KMainWindow::memberList();
183 kDebug() << "hiding" << windows
.count() << "windows";
184 Q_FOREACH(KMainWindow
* window
, windows
)
190 QTEST_KDEMAIN_WITH_COMPONENTNAME( KonqHtmlTest
, GUI
, "konqueror" )
192 #include "konqhtmltest.moc"