File indexing completed on 2024-12-08 06:39:42

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2014 Frank Reininghaus <frank78ac@googlemail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kfileitemactionstest.h"
0009 
0010 #include <kfileitemactions.h>
0011 #include <kfileitemlistproperties.h>
0012 
0013 #include <QMenu>
0014 #include <QStandardPaths>
0015 #include <QTest>
0016 
0017 /**
0018  * In KDE 4.x, calling KFileItemActions::setParentWidget(QWidget *widget) would
0019  * result in 'widget' not only being the parent of any dialogs created by,
0020  * KFileItemActions, but also of the actions. Nevertheless, the destructor of
0021  * KFileItemActions deleted all actions it created. This could lead to the deletion
0022  * of dangling pointers, and thus, a crash, if 'widget' was destroyed before the
0023  * KFileItemActions instance.
0024  */
0025 void KFileItemActionsTest::testSetParentWidget()
0026 {
0027     KFileItemActions fileItemActions;
0028 
0029     // Create a widget and make it the parent for any dialogs created by fileItemActions.
0030     QWidget *widget = new QWidget();
0031     fileItemActions.setParentWidget(widget);
0032 
0033     // Initialize fileItemActions with a KFileItemList that contains only the home URL.
0034     KFileItemList items;
0035     const QUrl homeUrl = QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).constFirst());
0036     const KFileItem item(homeUrl, QStringLiteral("inode/directory"));
0037     items << item;
0038     const KFileItemListProperties properties(items);
0039     fileItemActions.setItemListProperties(properties);
0040 
0041     // Create the "Open With" actions and add them to a menu.
0042     QMenu menu;
0043     fileItemActions.addActionsTo(&menu);
0044 
0045     // Delete the widget. In KDE 4.x, this would also delete the "Open With" actions
0046     // because they were children of the widget. We would then get a crash in the
0047     // destructor of fileItemActions because it tried to delete dangling pointers.
0048     delete widget;
0049 }
0050 
0051 void KFileItemActionsTest::testTopLevelServiceMenuActions()
0052 {
0053 #ifdef Q_OS_WIN
0054     QSKIP("Test skipped on Windows");
0055 #endif
0056     QStandardPaths::setTestModeEnabled(true);
0057 
0058     qputenv("XDG_DATA_DIRS", QFINDTESTDATA("servicemenu_protocol_mime_test_data").toLocal8Bit());
0059     KFileItemActions actions;
0060 
0061     {
0062         // only one menu should show up for the inode/directory mime type
0063         actions.setItemListProperties(KFileItemList({KFileItem(QUrl::fromLocalFile(QFINDTESTDATA("servicemenu_protocol_mime_test_data")))}));
0064         QMenu menu;
0065         actions.addActionsTo(&menu, KFileItemActions::MenuActionSource::Services);
0066         QCOMPARE(menu.actions().count(), 1);
0067         QCOMPARE(menu.actions().constFirst()->text(), "dir_service_menu");
0068     }
0069     {
0070         // The two actions should show up
0071         actions.setItemListProperties(KFileItemList({KFileItem(QUrl(QStringLiteral("smb://somefile.txt")))}));
0072         QMenu menu;
0073         actions.addActionsTo(&menu, KFileItemActions::MenuActionSource::Services);
0074         const auto resultingActions = menu.actions();
0075         QCOMPARE(resultingActions.count(), 2);
0076         QCOMPARE(resultingActions.at(0)->text(), "no_file");
0077         QCOMPARE(resultingActions.at(1)->text(), "smb");
0078     }
0079     {
0080         // Only the menu which handles URLs
0081         actions.setItemListProperties(KFileItemList({KFileItem(QUrl(QStringLiteral("someweirdscheme://somefile.txt")))}));
0082         QMenu menu;
0083         actions.addActionsTo(&menu, KFileItemActions::MenuActionSource::Services);
0084         const auto resultingActions = menu.actions();
0085         QCOMPARE(resultingActions.count(), 1);
0086         QCOMPARE(resultingActions.at(0)->text(), "no_file");
0087     }
0088 }
0089 
0090 QTEST_MAIN(KFileItemActionsTest)
0091 
0092 #include "moc_kfileitemactionstest.cpp"