File indexing completed on 2024-06-23 10:37:24

0001 /*
0002     SPDX-FileCopyrightText: 2015 Montel Laurent <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kurifiltersearchprovideractionstest.h"
0008 #include "kurifiltersearchprovideractions.h"
0009 #include <KUriFilter>
0010 #include <QMenu>
0011 #include <QTest>
0012 
0013 void KUriFilterSearchProviderActionsTest::initTestCase()
0014 {
0015     QString searchProvidersDir = QFINDTESTDATA("../src/urifilters/ikws/searchproviders/google.desktop").section('/', 0, -2);
0016     QVERIFY(!searchProvidersDir.isEmpty());
0017     qputenv("KIO_SEARCHPROVIDERS_DIR", QFile::encodeName(searchProvidersDir));
0018 }
0019 
0020 void KUriFilterSearchProviderActionsTest::shouldHaveDefaultValue()
0021 {
0022     KIO::KUriFilterSearchProviderActions shortcutManager;
0023     QVERIFY(shortcutManager.selectedText().isEmpty());
0024 }
0025 
0026 void KUriFilterSearchProviderActionsTest::shouldAssignSelectedText()
0027 {
0028     KIO::KUriFilterSearchProviderActions shortcutManager;
0029     const QString selectText = QStringLiteral("foo");
0030     shortcutManager.setSelectedText(selectText);
0031     QCOMPARE(shortcutManager.selectedText(), selectText);
0032 }
0033 
0034 void KUriFilterSearchProviderActionsTest::shouldAddActionToMenu()
0035 {
0036     KIO::KUriFilterSearchProviderActions shortcutManager;
0037     QMenu *menu = new QMenu(nullptr);
0038     shortcutManager.addWebShortcutsToMenu(menu);
0039     // Empty when we don't have selected text
0040     QVERIFY(menu->actions().isEmpty());
0041 
0042     const QString selectText = QStringLiteral("foo");
0043 
0044     KUriFilterData filterData(selectText);
0045 
0046     filterData.setSearchFilteringOptions(KUriFilterData::RetrievePreferredSearchProvidersOnly);
0047 
0048     QStringList searchProviders;
0049     if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::NormalTextFilter)) {
0050         searchProviders = filterData.preferredSearchProviders();
0051     }
0052 
0053     shortcutManager.setSelectedText(selectText);
0054     shortcutManager.addWebShortcutsToMenu(menu);
0055     QVERIFY(!menu->actions().isEmpty());
0056 
0057     // Verify that there is a submenu
0058     QVERIFY(menu->actions().at(0)->menu());
0059     QVERIFY(!menu->actions().at(0)->menu()->actions().isEmpty());
0060 
0061     QStringList actionData;
0062     for (const QString &str : std::as_const(searchProviders)) {
0063         actionData.append(filterData.queryForPreferredSearchProvider(str));
0064     }
0065 
0066     int count = 0;
0067     const QList<QAction *> actionsList = menu->actions().at(0)->menu()->actions();
0068     for (const QAction *act : actionsList) {
0069         if (!act->data().isNull()) {
0070             QVERIFY(actionData.contains(act->data().toString()));
0071             count++;
0072         }
0073     }
0074     QCOMPARE(count, actionData.count());
0075 
0076     delete menu;
0077 }
0078 
0079 QTEST_MAIN(KUriFilterSearchProviderActionsTest)
0080 
0081 #include "moc_kurifiltersearchprovideractionstest.cpp"