File indexing completed on 2024-10-27 06:46:48

0001 /*
0002     SPDX-FileCopyrightText: 2015 Alejandro Fiestas Olivares <afiestas@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kurlcomboboxtest.h"
0008 #include "kurlcombobox.h"
0009 
0010 #include <QtTestWidgets>
0011 
0012 QTEST_MAIN(KUrlComboBoxTest)
0013 
0014 void KUrlComboBoxTest::testTextForItem_data()
0015 {
0016     QTest::addColumn<QString>("url");
0017     QTest::addColumn<QString>("expectedText");
0018 
0019     QTest::newRow("with_host") << "ftp://foo.com/folder"
0020                                << "ftp://foo.com/folder/";
0021     QTest::newRow("with_no_host") << "smb://"
0022                                   << "smb://";
0023     QTest::newRow("with_host_without_path") << "ftp://user@example.com"
0024                                             << "ftp://user@example.com";
0025 }
0026 
0027 void KUrlComboBoxTest::testTextForItem()
0028 {
0029     QFETCH(QString, url);
0030     QFETCH(QString, expectedText);
0031 
0032     KUrlComboBox combo(KUrlComboBox::Directories);
0033     combo.setUrl(QUrl(url));
0034 
0035     QCOMPARE(combo.itemText(0), expectedText);
0036 }
0037 
0038 void KUrlComboBoxTest::testSetUrlMultipleTimes()
0039 {
0040     KUrlComboBox combo(KUrlComboBox::Directories);
0041     combo.setUrl(QUrl("http://kde.org"));
0042     combo.setUrl(QUrl("http://www.kde.org"));
0043 }
0044 
0045 void KUrlComboBoxTest::testRemoveUrl()
0046 {
0047     KUrlComboBox combo(KUrlComboBox::Both);
0048     combo.addDefaultUrl(QUrl("http://kde.org"));
0049     combo.addDefaultUrl(QUrl("http://www.kde.org"));
0050 
0051     QStringList urls{"http://foo.org", "http://bar.org"};
0052     combo.setUrls(urls);
0053 
0054     QCOMPARE(combo.urls(), urls);
0055     QCOMPARE(combo.count(), 4);
0056     QCOMPARE(combo.itemText(0), QString("http://kde.org"));
0057     QCOMPARE(combo.itemText(1), QString("http://www.kde.org"));
0058     QCOMPARE(combo.itemText(2), QString("http://foo.org"));
0059     QCOMPARE(combo.itemText(3), QString("http://bar.org"));
0060 
0061     // Remove a url
0062     combo.removeUrl(QUrl("http://foo.org"));
0063     QCOMPARE(combo.count(), 3);
0064     QCOMPARE(combo.urls(), QStringList{"http://bar.org"});
0065     QCOMPARE(combo.itemText(0), QString("http://kde.org"));
0066     QCOMPARE(combo.itemText(1), QString("http://www.kde.org"));
0067     QCOMPARE(combo.itemText(2), QString("http://bar.org"));
0068 
0069     // Removing a default url with checkDefaultURLs=true removes the url
0070     combo.removeUrl(QUrl("http://kde.org"));
0071     QCOMPARE(combo.count(), 2);
0072     QCOMPARE(combo.urls(), QStringList{"http://bar.org"});
0073     QCOMPARE(combo.itemText(0), QString("http://www.kde.org"));
0074     QCOMPARE(combo.itemText(1), QString("http://bar.org"));
0075 
0076     // Removing a default url with checkDefaultURLs=false does not remove the url
0077     combo.removeUrl(QUrl("http://www.kde.org"), false);
0078     QCOMPARE(combo.count(), 2);
0079     QCOMPARE(combo.urls(), QStringList{"http://bar.org"});
0080     QCOMPARE(combo.itemText(0), QString("http://www.kde.org"));
0081     QCOMPARE(combo.itemText(1), QString("http://bar.org"));
0082 
0083     // Removing a non-existing url is a no-op
0084     combo.removeUrl(QUrl("http://www.foo.org"));
0085     QCOMPARE(combo.count(), 2);
0086     QCOMPARE(combo.urls(), QStringList{"http://bar.org"});
0087     QCOMPARE(combo.itemText(0), QString("http://www.kde.org"));
0088     QCOMPARE(combo.itemText(1), QString("http://bar.org"));
0089 
0090     // Remove the last user provided url
0091     combo.removeUrl(QUrl("http://bar.org"));
0092     QCOMPARE(combo.count(), 1);
0093     QCOMPARE(combo.urls(), QStringList{});
0094     QCOMPARE(combo.itemText(0), QString("http://www.kde.org"));
0095 
0096     // Remove the last url
0097     combo.removeUrl(QUrl("http://www.kde.org"));
0098     QCOMPARE(combo.count(), 0);
0099     QCOMPARE(combo.urls(), QStringList{});
0100     QCOMPARE(combo.itemText(0), QString());
0101 }
0102 
0103 void KUrlComboBoxTest::testAddUrls()
0104 {
0105     // GIVEN
0106     KUrlComboBox combo(KUrlComboBox::Both);
0107     combo.addDefaultUrl(QUrl("http://kde.org"));
0108     combo.addDefaultUrl(QUrl("http://www.kde.org"));
0109 
0110     const QStringList urls{"http://foo.org", "http://bar.org"};
0111     combo.setUrls(urls);
0112 
0113     // WHEN
0114     const QString url("http://foo.org/newUrl");
0115     combo.setUrl(QUrl(url));
0116 
0117     // THEN
0118     QStringList expected = urls;
0119     expected << url;
0120     QCOMPARE(combo.urls(), expected);
0121 }
0122 
0123 void KUrlComboBoxTest::testSetMaxItems()
0124 {
0125     // GIVEN
0126     KUrlComboBox combo(KUrlComboBox::Both);
0127     combo.addDefaultUrl(QUrl("http://kde.org"));
0128     combo.addDefaultUrl(QUrl("http://www.kde.org"));
0129 
0130     const QStringList urls{"http://foo.org", "http://bar.org", "http://example.org", "http://example2.org"};
0131     combo.setUrls(urls);
0132 
0133     QStringList expected = urls;
0134     QCOMPARE(combo.urls(), expected);
0135 
0136     // WHEN
0137     combo.setMaxItems(4); // includes the default URLs
0138 
0139     // THEN
0140     expected = expected.mid(2);
0141     QCOMPARE(combo.urls(), expected);
0142 
0143     // WHEN
0144     combo.setMaxItems(1); // no room for additional URLs
0145 
0146     // THEN
0147     QCOMPARE(combo.urls(), QStringList());
0148 }
0149 
0150 #include "moc_kurlcomboboxtest.cpp"