Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / lib / konq / tests / konqpopupmenutest.cpp
blobf185ca21853e7fdec966fc0c3f7c0f5113e484ab
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>
30 #include <konq_fileitemcapabilities.h>
32 QTEST_KDEMAIN(KonqPopupMenuTest, GUI)
34 KonqPopupMenuTest::KonqPopupMenuTest()
35 : m_actionCollection(this)
39 static QStringList extractActionNames(const QMenu& menu)
41 QString lastObjectName;
42 QStringList ret;
43 foreach (const QAction* action, menu.actions()) {
44 if (action->isSeparator()) {
45 ret.append("separator");
46 } else {
47 //qDebug() << action->objectName() << action->metaObject()->className() << action->text();
48 const QString objectName = action->objectName();
49 if (objectName.isEmpty()) {
50 if (action->menu()) // if this fails, then we have an unnamed action somewhere...
51 ret.append("submenu");
52 else {
53 ret.append("UNNAMED " + action->text());
55 } else {
56 if (objectName == "menuaction") // a single service-menu action: give same name as a submenu
57 ret.append("actions_submenu");
58 else if (objectName == "openWith_submenu") {
59 ret.append("openwith");
60 } else if (objectName == "openwith_browse" && lastObjectName == "openwith") {
61 // We had "open with foo" followed by openwith_browse, all is well.
62 // The expected lists only say "openwith" so that they work in both cases
63 // -> skip the browse action.
64 } else {
65 ret.append(objectName);
69 lastObjectName = action->objectName();
71 return ret;
75 void KonqPopupMenuTest::initTestCase()
77 m_thisDirectoryItem = KFileItem(QDir::currentPath(), "inode/directory", S_IFDIR + 0777);
78 m_fileItem = KFileItem(QDir::currentPath() + "/Makefile", "text/x-makefile", S_IFREG + 0660);
79 m_linkItem = KFileItem(KUrl("http://www.kde.org/foo"), "text/html", S_IFREG + 0660);
80 m_subDirItem = KFileItem(QDir::currentPath() + "/CMakeFiles", "inode/directory", S_IFDIR + 0755);
81 m_cut = KStandardAction::cut(0, 0, this);
82 m_actionCollection.addAction("cut", m_cut);
83 m_copy = KStandardAction::copy(0, 0, this);
84 m_actionCollection.addAction("copy", m_copy);
85 m_paste = KStandardAction::paste(0, 0, this);
86 m_actionCollection.addAction("paste", m_paste);
87 m_pasteTo = KStandardAction::paste(0, 0, this);
88 m_actionCollection.addAction("pasteto", m_pasteTo);
89 m_back = new QAction(this);
90 m_actionCollection.addAction("go_back", m_back);
91 m_forward = new QAction(this);
92 m_actionCollection.addAction("go_forward", m_forward);
93 m_up = new QAction(this);
94 m_actionCollection.addAction("go_up", m_up);
95 m_reload = new QAction(this);
96 m_actionCollection.addAction("reload", m_reload);
97 m_properties = new QAction(this);
98 m_actionCollection.addAction("properties", m_properties);
100 m_tabHandlingActions = new QActionGroup(this);
101 m_newWindow = new QAction(m_tabHandlingActions);
102 m_actionCollection.addAction("openInNewWindow", m_newWindow);
103 m_newTab = new QAction(m_tabHandlingActions);
104 m_actionCollection.addAction("openInNewTab", m_newTab);
105 QAction* separator = new QAction(m_tabHandlingActions);
106 separator->setSeparator(true);
107 QCOMPARE(m_tabHandlingActions->actions().count(), 3);
109 m_previewActions = new QActionGroup(this);
110 m_preview1 = new QAction(m_previewActions);
111 m_actionCollection.addAction("preview1", m_preview1);
112 m_preview2 = new QAction(m_previewActions);
113 m_actionCollection.addAction("preview2", m_preview2);
115 m_fileEditActions = new QActionGroup(this);
116 m_rename = new QAction(m_fileEditActions);
117 m_actionCollection.addAction("rename", m_rename);
118 m_trash = new QAction(m_fileEditActions);
119 m_actionCollection.addAction("trash", m_trash);
121 m_htmlEditActions = new QActionGroup(this);
122 // TODO use m_htmlEditActions like in khtml (see khtml_popupmenu.rc)
124 m_linkActions = new QActionGroup(this);
125 QAction* saveLinkAs = new QAction(m_linkActions);
126 m_actionCollection.addAction("savelinkas", saveLinkAs);
127 QAction* copyLinkLocation = new QAction(m_linkActions);
128 m_actionCollection.addAction("copylinklocation", copyLinkLocation);
129 // TODO there's a whole bunch of things for frames, and for images, see khtml_popupmenu.rc
131 m_partActions = new QActionGroup(this);
132 separator = new QAction(m_partActions);
133 separator->setSeparator(true);
134 m_partActions->addAction(separator); // we better start with a separator
135 QAction* viewDocumentSource = new QAction(m_partActions);
136 m_actionCollection.addAction("viewDocumentSource", viewDocumentSource);
138 m_newMenu = new KNewMenu(&m_actionCollection, 0, "newmenu");
140 // Check if extractActionNames works
141 QMenu popup;
142 popup.addAction(m_back);
143 QMenu* subMenu = new QMenu(&popup);
144 popup.addMenu(subMenu);
145 subMenu->addAction(m_up);
146 QStringList actions = extractActionNames(popup);
147 kDebug() << actions;
148 QCOMPARE(actions, QStringList() << "go_back" << "submenu");
151 void KonqPopupMenuTest::testFile()
153 KFileItemList itemList;
154 itemList << m_fileItem;
155 KUrl viewUrl = QDir::currentPath();
156 KonqPopupMenu::Flags flags = 0;
157 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowProperties
158 | KParts::BrowserExtension::ShowReload
159 | KParts::BrowserExtension::ShowUrlOperations;
160 KParts::BrowserExtension::ActionGroupMap actionGroups;
161 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
162 actionGroups.insert("editactions", m_fileEditActions->actions());
163 actionGroups.insert("preview", QList<QAction *>() << m_preview1);
165 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
166 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
168 QStringList actions = extractActionNames(popup);
169 kDebug() << actions;
170 QStringList expectedActions;
171 expectedActions << "openInNewWindow" << "openInNewTab" << "separator"
172 << "cut" << "copy" << "rename" << "trash" << "separator"
173 << "openwith"
174 << "preview1";
175 if (!KStandardDirs::locate("services", "ServiceMenus/encryptfile.desktop").isEmpty())
176 expectedActions << "actions_submenu";
177 if (!KStandardDirs::locate("services", "ServiceMenus/ark_addtoservicemenu.desktop").isEmpty())
178 expectedActions << "services_submenu";
179 expectedActions << "separator";
180 expectedActions << "copyTo_submenu" << "moveTo_submenu" << "separator";
181 // (came from arkplugin) << "compress"
182 expectedActions << "properties";
183 kDebug() << "Expected:" << expectedActions;
184 QCOMPARE(actions, expectedActions);
187 void KonqPopupMenuTest::testFileInReadOnlyDirectory()
189 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl("/etc/passwd"));
190 KFileItemList itemList;
191 itemList << item;
193 KonqFileItemCapabilities capabilities(itemList);
194 QVERIFY(!capabilities.supportsMoving());
196 KUrl viewUrl("/etc");
197 KonqPopupMenu::Flags flags = 0;
198 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowProperties
199 | KParts::BrowserExtension::ShowReload
200 | KParts::BrowserExtension::ShowUrlOperations;
201 KParts::BrowserExtension::ActionGroupMap actionGroups;
202 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
203 // DolphinPart doesn't add rename/trash when supportsMoving is false
204 // Maybe we should test dolphinpart directly :)
205 //actionGroups.insert("editactions", m_fileEditActions->actions());
206 actionGroups.insert("preview", QList<QAction *>() << m_preview1);
208 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
209 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
211 QStringList actions = extractActionNames(popup);
212 actions.removeAll("services_submenu");
213 kDebug() << actions;
214 QStringList expectedActions;
215 expectedActions << "openInNewWindow" << "openInNewTab" << "separator"
216 << "copy" << "separator"
217 << "openwith"
218 << "preview1";
219 if (!KStandardDirs::locate("services", "ServiceMenus/konsolehere.desktop").isEmpty())
220 expectedActions << "actions_submenu";
221 expectedActions << "separator";
222 expectedActions << "copyTo_submenu" << "separator";
223 expectedActions << "properties";
224 kDebug() << "Expected:" << expectedActions;
225 QCOMPARE(actions, expectedActions);
228 void KonqPopupMenuTest::testFilePreviewSubMenu()
230 // Same as testFile, but this time the "preview" action group has more than one action
231 KFileItemList itemList;
232 itemList << m_fileItem;
233 KUrl viewUrl = QDir::currentPath();
234 KonqPopupMenu::Flags flags = 0;
235 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowProperties
236 | KParts::BrowserExtension::ShowReload
237 | KParts::BrowserExtension::ShowUrlOperations;
238 KParts::BrowserExtension::ActionGroupMap actionGroups;
239 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
240 actionGroups.insert("editactions", m_fileEditActions->actions());
241 actionGroups.insert("preview", m_previewActions->actions());
243 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
244 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
246 QStringList actions = extractActionNames(popup);
247 kDebug() << actions;
248 QStringList expectedActions;
249 expectedActions << "openInNewWindow" << "openInNewTab" << "separator"
250 << "cut" << "copy" << "rename" << "trash" << "separator"
251 << "openwith"
252 << "preview_submenu";
253 if (!KStandardDirs::locate("services", "ServiceMenus/encryptfile.desktop").isEmpty())
254 expectedActions << "actions_submenu";
255 if (!KStandardDirs::locate("services", "ServiceMenus/ark_addtoservicemenu.desktop").isEmpty())
256 expectedActions << "services_submenu";
257 expectedActions << "separator";
258 expectedActions << "copyTo_submenu" << "moveTo_submenu" << "separator";
259 expectedActions << "properties";
260 kDebug() << "Expected:" << expectedActions;
261 QCOMPARE(actions, expectedActions);
264 void KonqPopupMenuTest::testSubDirectory()
266 KFileItemList itemList;
267 itemList << m_subDirItem;
268 KUrl viewUrl = QDir::currentPath();
269 KonqPopupMenu::Flags flags = 0;
270 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowProperties
271 | KParts::BrowserExtension::ShowUrlOperations;
272 KParts::BrowserExtension::ActionGroupMap actionGroups;
273 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
274 actionGroups.insert("editactions", m_fileEditActions->actions());
275 actionGroups.insert("preview", m_previewActions->actions());
277 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
278 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
279 QStringList actions = extractActionNames(popup);
280 actions.removeAll("services_submenu");
281 kDebug() << actions;
282 QStringList expectedActions;
283 expectedActions << "openInNewWindow" << "openInNewTab" << "separator"
284 << "cut" << "copy" << "pasteto" << "rename" << "trash" << "separator"
285 << "openwith"
286 << "preview_submenu";
287 if (!KStandardDirs::locate("services", "ServiceMenus/konsolehere.desktop").isEmpty())
288 expectedActions << "actions_submenu";
289 expectedActions << "separator";
290 expectedActions << "copyTo_submenu" << "moveTo_submenu" << "separator";
291 expectedActions << "properties";
292 kDebug() << "Expected:" << expectedActions;
293 QCOMPARE(actions, expectedActions);
296 void KonqPopupMenuTest::testViewDirectory()
298 KFileItemList itemList;
299 itemList << m_thisDirectoryItem;
300 KUrl viewUrl = m_thisDirectoryItem.url();
301 KonqPopupMenu::Flags flags = 0;
302 KParts::BrowserExtension::PopupFlags beflags =
303 KParts::BrowserExtension::ShowNavigationItems |
304 KParts::BrowserExtension::ShowUp |
305 KParts::BrowserExtension::ShowCreateDirectory |
306 KParts::BrowserExtension::ShowUrlOperations |
307 KParts::BrowserExtension::ShowProperties;
308 // KonqMainWindow says: doTabHandling = !openedForViewURL && ... So we don't add tabhandling here
309 KParts::BrowserExtension::ActionGroupMap actionGroups;
310 actionGroups.insert("preview", m_previewActions->actions());
312 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
313 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
315 QStringList actions = extractActionNames(popup);
316 actions.removeAll("services_submenu");
317 qDebug() << actions;
318 QStringList expectedActions;
319 expectedActions << "newmenu" << "separator"
320 << "go_up" << "go_back" << "go_forward" << "separator"
321 << "paste" << "separator"
322 << "openwith"
323 << "preview_submenu";
324 if (!KStandardDirs::locate("services", "ServiceMenus/konsolehere.desktop").isEmpty())
325 expectedActions << "actions_submenu";
326 expectedActions << "separator";
327 expectedActions << "copyTo_submenu" << "moveTo_submenu" << "separator";
328 expectedActions << "properties";
329 kDebug() << "Expected:" << expectedActions;
330 QCOMPARE(actions, expectedActions);
333 void KonqPopupMenuTest::testViewReadOnlyDirectory()
335 KFileItem rootItem(QDir::rootPath(), "inode/directory", KFileItem::Unknown);
336 KFileItemList itemList;
337 itemList << rootItem;
338 KUrl viewUrl = rootItem.url();
339 KonqPopupMenu::Flags flags = 0;
340 KParts::BrowserExtension::PopupFlags beflags =
341 KParts::BrowserExtension::ShowNavigationItems |
342 KParts::BrowserExtension::ShowUp |
343 KParts::BrowserExtension::ShowCreateDirectory |
344 KParts::BrowserExtension::ShowUrlOperations |
345 KParts::BrowserExtension::ShowProperties;
346 // KonqMainWindow says: doTabHandling = !openedForViewURL && ... So we don't add tabhandling here
347 KParts::BrowserExtension::ActionGroupMap actionGroups;
348 actionGroups.insert("preview", m_previewActions->actions());
350 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
351 0 /*parent*/, 0 /*bookmark manager*/, actionGroups);
353 QStringList actions = extractActionNames(popup);
354 qDebug() << actions;
355 actions.removeAll("services_submenu");
356 QStringList expectedActions;
357 expectedActions << "go_up" << "go_back" << "go_forward" << "separator"
358 // << "paste" // no paste since readonly
359 << "separator" // the doubled separator doesn't hurt, QMenu removes them
360 << "openwith"
361 << "preview_submenu";
362 if (!KStandardDirs::locate("services", "ServiceMenus/konsolehere.desktop").isEmpty())
363 expectedActions << "actions_submenu";
364 expectedActions << "separator";
365 expectedActions << "copyTo_submenu" << "separator"; // no moveTo_submenu, since readonly
366 expectedActions << "properties";
367 kDebug() << "Expected:" << expectedActions;
368 QCOMPARE(actions, expectedActions);
371 void KonqPopupMenuTest::testHtmlLink()
373 KFileItemList itemList;
374 itemList << m_linkItem;
375 //KUrl viewUrl = m_fileItem.url();
376 KUrl viewUrl("http://www.kde.org");
377 KonqPopupMenu::Flags flags = 0;
378 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowBookmark
379 | KParts::BrowserExtension::ShowReload
380 | KParts::BrowserExtension::IsLink;
381 KParts::BrowserExtension::ActionGroupMap actionGroups;
382 actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
383 actionGroups.insert("preview", m_previewActions->actions());
384 actionGroups.insert("editactions", m_htmlEditActions->actions());
385 actionGroups.insert("linkactions", m_linkActions->actions());
386 actionGroups.insert("partactions", m_partActions->actions());
388 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
389 0 /*parent*/, KBookmarkManager::userBookmarksManager(), actionGroups);
391 QStringList actions = extractActionNames(popup);
392 kDebug() << actions;
393 const int actionsIndex = actions.indexOf("actions_submenu");
394 if (actionsIndex > -1) {
395 actions.removeAt(actionsIndex);
397 QStringList expectedActions;
398 expectedActions << "openInNewWindow" << "openInNewTab" << "separator"
399 << "bookmark_add" << "savelinkas" << "copylinklocation"
400 << "separator"
401 << "openwith"
402 << "preview_submenu"
403 << "separator"
404 << "viewDocumentSource";
405 kDebug() << "Expected:" << expectedActions;
406 QCOMPARE(actions, expectedActions);
409 void KonqPopupMenuTest::testHtmlPage()
411 KFileItemList itemList;
412 itemList << m_linkItem;
413 KUrl viewUrl = m_linkItem.url();
414 KonqPopupMenu::Flags flags = 0;
415 KParts::BrowserExtension::PopupFlags beflags = KParts::BrowserExtension::ShowBookmark
416 | KParts::BrowserExtension::ShowReload
417 | KParts::BrowserExtension::ShowNavigationItems;
418 KParts::BrowserExtension::ActionGroupMap actionGroups;
419 // KonqMainWindow says: doTabHandling = !openedForViewURL && ... So we don't add tabhandling here
420 // TODO we could just move that logic to KonqPopupMenu...
421 //actionGroups.insert("tabhandling", m_tabHandlingActions->actions());
422 actionGroups.insert("preview", m_previewActions->actions());
423 actionGroups.insert("editactions", m_htmlEditActions->actions());
424 //actionGroups.insert("linkactions", m_linkActions->actions());
425 QAction* security = new QAction(m_partActions);
426 m_actionCollection.addAction("security", security);
427 QAction* setEncoding = new QAction(m_partActions);
428 m_actionCollection.addAction("setEncoding", setEncoding);
429 actionGroups.insert("partactions", m_partActions->actions());
431 KonqPopupMenu popup(itemList, viewUrl, m_actionCollection, m_newMenu, flags, beflags,
432 0 /*parent*/, KBookmarkManager::userBookmarksManager(), actionGroups);
434 QStringList actions = extractActionNames(popup);
435 kDebug() << actions;
436 const int actionsIndex = actions.indexOf("actions_submenu");
437 if (actionsIndex > -1) {
438 actions.removeAt(actionsIndex);
440 QStringList expectedActions;
441 expectedActions << "go_back" << "go_forward" << "reload" << "separator"
442 << "bookmark_add"
443 << "separator"
444 << "openwith"
445 << "preview_submenu"
446 << "separator"
447 // << TODO "stopanimations"
448 << "viewDocumentSource" << "security" << "setEncoding";
449 kDebug() << "Expected:" << expectedActions;
450 QCOMPARE(actions, expectedActions);
454 // TODO test ShowReload (khtml passes it, but not the file views. Maybe show it if "not a directory" or "not local")
456 // (because file viewers don't react on changes, and remote things don't notify) -- then get rid of ShowReload.
458 // TODO test ShowBookmark. Probably the same logic?
459 // TODO separate filemanager and webbrowser bookmark managers, too (share file bookmarks with file dialog)
461 // TODO test text selection actions in khtml
463 // TODO trash:/ tests
465 // TODO test NoDeletion part flag