Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / lib / konq / tests / konqpopupmenutest.cpp
blob0f05bead4b84dbb5466826e7915b57ee79802593
1 /* This file is part of KDE
2 Copyright (c) 2007 David Faure <faure@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "konqpopupmenutest.h"
21 #include <kstandarddirs.h>
22 #include <kbookmarkmanager.h>
23 #include <assert.h>
24 #include "qtest_kde.h"
25 #include <QDir>
26 #include <kparts/browserextension.h>
27 #include <knewmenu.h>
28 #include <kdebug.h>
29 #include <konq_popupmenu.h>
31 QTEST_KDEMAIN(KonqPopupMenuTest, GUI)
33 KonqPopupMenuTest::KonqPopupMenuTest()
34 : m_actionCollection(this)
38 static QStringList extractActionNames(const QMenu& menu)
40 QStringList ret;
41 foreach (const QAction* action, menu.actions()) {
42 if (action->isSeparator()) {
43 ret.append("separator");
44 } else {
45 //qDebug() << action->objectName() << action->metaObject()->className();
46 const QString objectName = action->objectName();
47 if (objectName.isEmpty()) {
48 Q_ASSERT(action->menu());
49 ret.append("submenu");
50 } else {
51 if (objectName == "menuaction") // a single service-menu action: give same name as a submenu
52 ret.append("actions_submenu");
53 else
54 ret.append(objectName);
58 return ret;
62 void KonqPopupMenuTest::initTestCase()
64 m_rootItem = KFileItem(QDir::currentPath(), "inode/directory", S_IFDIR + 0777);
65 m_fileItem = KFileItem(QDir::currentPath() + "/Makefile", "text/x-makefile", S_IFREG + 0660);
66 m_linkItem = KFileItem(QDir::currentPath() + "/cmake_install.cmake", "text/html", S_IFREG + 0660);
67 m_subDirItem = KFileItem(QDir::currentPath() + "/CMakeFiles", "inode/directory", S_IFDIR + 0755);
68 m_cut = KStandardAction::cut(0, 0, this);
69 m_actionCollection.addAction("cut", m_cut);
70 m_copy = KStandardAction::copy(0, 0, this);
71 m_actionCollection.addAction("copy", m_copy);
72 m_paste = KStandardAction::paste(0, 0, this);
73 m_actionCollection.addAction("paste", m_paste);
74 m_pasteTo = KStandardAction::paste(0, 0, this);
75 m_actionCollection.addAction("pasteto", m_pasteTo);
76 m_back = new QAction(this);
77 m_actionCollection.addAction("go_back", m_back);
78 m_forward = new QAction(this);
79 m_actionCollection.addAction("go_forward", m_forward);
80 m_up = new QAction(this);
81 m_actionCollection.addAction("go_up", m_up);
82 m_reload = new QAction(this);
83 m_actionCollection.addAction("reload", m_reload);
84 m_properties = new QAction(this);
85 m_actionCollection.addAction("properties", m_properties);
87 m_tabHandlingActions = new QActionGroup(this);
88 m_newWindow = new QAction(m_tabHandlingActions);
89 m_actionCollection.addAction("openInNewWindow", m_newWindow);
90 m_newTab = new QAction(m_tabHandlingActions);
91 m_actionCollection.addAction("openInNewTab", m_newTab);
92 QAction* separator = new QAction(m_tabHandlingActions);
93 separator->setSeparator(true);
94 QCOMPARE(m_tabHandlingActions->actions().count(), 3);
96 m_previewActions = new QActionGroup(this);
97 m_preview1 = new QAction(m_previewActions);
98 m_actionCollection.addAction("preview1", m_preview1);
99 m_preview2 = new QAction(m_previewActions);
100 m_actionCollection.addAction("preview2", m_preview2);
102 m_fileEditActions = new QActionGroup(this);
103 m_rename = new QAction(m_fileEditActions);
104 m_actionCollection.addAction("rename", m_rename);
105 m_trash = new QAction(m_fileEditActions);
106 m_actionCollection.addAction("trash", m_trash);
108 m_htmlEditActions = new QActionGroup(this);
109 // TODO use m_htmlEditActions like in khtml (see khtml_popupmenu.rc)
111 m_linkActions = new QActionGroup(this);
112 QAction* saveLinkAs = new QAction(m_linkActions);
113 m_actionCollection.addAction("savelinkas", saveLinkAs);
114 QAction* copyLinkLocation = new QAction(m_linkActions);
115 m_actionCollection.addAction("copylinklocation", copyLinkLocation);
116 // TODO there's a whole bunch of things for frames, and for images, see khtml_popupmenu.rc
118 m_partActions = new QActionGroup(this);
119 separator = new QAction(m_partActions);
120 separator->setSeparator(true);
121 m_partActions->addAction(separator); // we better start with a separator
122 QAction* viewDocumentSource = new QAction(m_partActions);
123 m_actionCollection.addAction("viewDocumentSource", viewDocumentSource);
125 m_newMenu = new KNewMenu(&m_actionCollection, 0, "newmenu");
127 // Check if extractActionNames works
128 QMenu popup;
129 popup.addAction(m_back);
130 QMenu* subMenu = new QMenu(&popup);
131 popup.addMenu(subMenu);
132 subMenu->addAction(m_up);
133 QStringList actions = extractActionNames(popup);
134 kDebug() << actions;
135 QCOMPARE(actions, QStringList() << "go_back" << "submenu");
138 void KonqPopupMenuTest::testFile()
140 KFileItemList itemList;
141 itemList << m_fileItem;
142 KUrl viewUrl = QDir::currentPath();
143 KonqPopupMenu::Flags flags = 0;
144 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowProperties
145 | KParts::BrowserExtension::ShowReload
146 | KParts::BrowserExtension::ShowUrlOperations;
147 KParts::BrowserExtension::ActionGroupMap actionGroups;
148 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
149 actionGroups.insert("editactions", m_fileEditActions->actions());
150 actionGroups.insert("preview", QList<QAction *>() << m_preview1);
152 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
153 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
155 QStringList actions = extractActionNames(popup);
156 kDebug() << actions;
157 QStringList expectedActions;
158 expectedActions << "openInNewWindow" << "openInNewTab" << "separator"
159 << "cut" << "copy" << "rename" << "trash" << "separator"
160 << "openWith_submenu"
161 << "preview1";
162 if (!KStandardDirs::locate("services", "ServiceMenus/encryptfile.desktop").isEmpty())
163 expectedActions << "actions_submenu" << "separator";
165 // (came from arkplugin) << "compress"
166 // (came from kuick) << "copy_to" << "move_to"
167 expectedActions << "properties";
168 QCOMPARE(actions, expectedActions);
171 void KonqPopupMenuTest::testFilePreviewSubMenu()
173 KFileItemList itemList;
174 itemList << m_fileItem;
175 KUrl viewUrl = QDir::currentPath();
176 KonqPopupMenu::Flags flags = 0;
177 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowProperties
178 | KParts::BrowserExtension::ShowReload
179 | KParts::BrowserExtension::ShowUrlOperations;
180 KParts::BrowserExtension::ActionGroupMap actionGroups;
181 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
182 actionGroups.insert("editactions", m_fileEditActions->actions());
183 actionGroups.insert("preview", m_previewActions->actions());
185 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
186 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
188 QStringList actions = extractActionNames(popup);
189 kDebug() << actions;
190 QCOMPARE(actions, QStringList()
191 << "openInNewWindow" << "openInNewTab" << "separator"
192 << "cut" << "copy" << "rename" << "trash" << "separator"
193 << "openWith_submenu"
194 << "preview_submenu"
195 << "actions_submenu" << "separator"
196 // (came from arkplugin) << "compress"
197 // (came from kuick) << "copy_to" << "move_to"
198 << "properties");
201 void KonqPopupMenuTest::testSubDirectory()
203 KFileItemList itemList;
204 itemList << m_subDirItem;
205 KUrl viewUrl = QDir::currentPath();
206 KonqPopupMenu::Flags flags = 0;
207 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowProperties
208 | KParts::BrowserExtension::ShowUrlOperations;
209 KParts::BrowserExtension::ActionGroupMap actionGroups;
210 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
211 actionGroups.insert("editactions", m_fileEditActions->actions());
212 actionGroups.insert("preview", m_previewActions->actions());
214 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
215 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
216 QStringList actions = extractActionNames(popup);
217 kDebug() << actions;
218 // It's OK to have "openwith" instead of openWith_submenu, it depends on the number of offers
219 actions.replaceInStrings("openwith", "openWith_submenu");
220 QCOMPARE(actions, QStringList()
221 << "openInNewWindow" << "openInNewTab" << "separator"
222 << "cut" << "copy" << "pasteto" << "rename" << "trash" << "separator"
223 << "openWith_submenu"
224 << "preview_submenu"
225 << "actions_submenu" << "separator"
226 // (came from arkplugin) << "compress"
227 // (came from kuick) << "copy_to" << "move_to"
228 << "properties");
231 void KonqPopupMenuTest::testViewDirectory()
233 KFileItemList itemList;
234 itemList << m_rootItem;
235 KUrl viewUrl = QDir::currentPath();
236 KonqPopupMenu::Flags flags = 0;
237 KParts::BrowserExtension::PopupFlags beflags =
238 KParts::BrowserExtension::ShowNavigationItems |
239 KParts::BrowserExtension::ShowUp |
240 KParts::BrowserExtension::ShowCreateDirectory |
241 KParts::BrowserExtension::ShowUrlOperations |
242 KParts::BrowserExtension::ShowProperties;
243 // KonqMainWindow says: doTabHandling = !openedForViewURL && ... So we don't add tabhandling here
244 KParts::BrowserExtension::ActionGroupMap actionGroups;
245 actionGroups.insert("preview", m_previewActions->actions());
247 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
248 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
250 QStringList actions = extractActionNames(popup);
251 qDebug() << actions;
252 // It's OK to have "openwith" instead of openWith_submenu, it depends on the number of offers
253 actions.replaceInStrings("openwith", "openWith_submenu");
254 QCOMPARE(actions, QStringList()
255 << "newmenu" << "separator"
256 << "go_up" << "go_back" << "go_forward" << "separator"
257 << "paste" << "separator"
258 << "openWith_submenu"
259 << "preview_submenu"
260 << "actions_submenu" << "separator"
261 // (came from arkplugin) << "compress"
262 // (came from kuick) << "copy_to" << "move_to"
263 << "properties");
266 void KonqPopupMenuTest::testHtmlLink()
268 KFileItemList itemList;
269 itemList << m_linkItem;
270 KUrl viewUrl = m_fileItem.url();
271 KonqPopupMenu::Flags flags = 0;
272 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowBookmark
273 | KParts::BrowserExtension::ShowReload
274 | KParts::BrowserExtension::IsLink;
275 KParts::BrowserExtension::ActionGroupMap actionGroups;
276 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
277 actionGroups.insert("preview", m_previewActions->actions());
278 actionGroups.insert("editactions", m_htmlEditActions->actions());
279 actionGroups.insert("linkactions", m_linkActions->actions());
280 actionGroups.insert("partactions", m_partActions->actions());
282 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
283 0 /*parent*/, KBookmarkManager::userBookmarksManager(), actionGroups);
285 QStringList actions = extractActionNames(popup);
286 kDebug() << actions;
287 QCOMPARE(actions, QStringList()
288 << "openInNewWindow" << "openInNewTab" << "separator"
289 << "bookmark_add" << "savelinkas" << "copylinklocation"
290 << "separator"
291 << "openWith_submenu"
292 << "preview_submenu"
293 << "actions_submenu" << "separator"
294 // (came from kuick) << "copy_to" << "move_to"
295 << "viewDocumentSource");
298 void KonqPopupMenuTest::testHtmlPage()
300 KFileItemList itemList;
301 itemList << m_fileItem;
302 KUrl viewUrl = m_fileItem.url();
303 KonqPopupMenu::Flags flags = 0;
304 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowBookmark
305 | KParts::BrowserExtension::ShowReload
306 | KParts::BrowserExtension::ShowNavigationItems;
307 KParts::BrowserExtension::ActionGroupMap actionGroups;
308 // KonqMainWindow says: doTabHandling = !openedForViewURL && ... So we don't add tabhandling here
309 // TODO we could just move that logic to KonqPopupMenu...
310 //actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
311 actionGroups.insert("preview", m_previewActions->actions());
312 actionGroups.insert("editactions", m_htmlEditActions->actions());
313 //actionGroups.insert("linkactions", m_linkActions->actions());
314 QAction* security = new QAction(m_partActions);
315 m_actionCollection.addAction("security", security);
316 QAction* setEncoding = new QAction(m_partActions);
317 m_actionCollection.addAction("setEncoding", setEncoding);
318 actionGroups.insert("partactions", m_partActions->actions());
320 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
321 0 /*parent*/, KBookmarkManager::userBookmarksManager(), actionGroups);
323 QStringList actions = extractActionNames(popup);
324 kDebug() << actions;
325 QCOMPARE(actions, QStringList()
326 << "go_back" << "go_forward" << "reload" << "separator"
327 << "bookmark_add"
328 << "separator"
329 << "openWith_submenu"
330 << "preview_submenu"
331 << "actions_submenu" << "separator"
332 // << TODO "stopanimations"
333 // (came from kuick - but shouldn't be there IMHO) << "copy_to" << "move_to"
334 << "viewDocumentSource" << "security" << "setEncoding"
339 // TODO test ShowReload (khtml passes it, but not the file views. Maybe show it if "not a directory" or "not local")
341 // (because file viewers don't react on changes, and remote things don't notify) -- then get rid of ShowReload.
343 // TODO test ShowBookmark. Probably the same logic?
344 // TODO separate filemanager and webbrowser bookmark managers, too (share file bookmarks with file dialog)
346 // TODO click on background of html page; text selection in khtml
348 // TODO trash:/ tests
350 // TODO test NoDeletion part flag