File indexing completed on 2024-05-12 05:46:35
0001 /*************************************************************************** 0002 * Copyright (C) 2015 by Alejandro Fiestas Olivares <afiestas@kde.org * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 0018 ***************************************************************************/ 0019 0020 #include "kurlcomboboxtest.h" 0021 #include "kurlcombobox.h" 0022 0023 #include <QtTestWidgets> 0024 0025 QTEST_MAIN(KUrlComboBoxTest) 0026 0027 void KUrlComboBoxTest::testTextForItem_data() 0028 { 0029 QTest::addColumn<QString>("url"); 0030 QTest::addColumn<QString>("expectedText"); 0031 0032 QTest::newRow("with_host") << "ftp://foo.com/folder" << "ftp://foo.com/folder/"; 0033 QTest::newRow("with_no_host") << "smb://" << "smb://"; 0034 QTest::newRow("with_host_without_path") << "ftp://user@example.com" << "ftp://user@example.com"; 0035 } 0036 0037 void KUrlComboBoxTest::testTextForItem() 0038 { 0039 QFETCH(QString, url); 0040 QFETCH(QString, expectedText); 0041 0042 KUrlComboBox combo(KUrlComboBox::Directories); 0043 combo.setUrl(QUrl(url)); 0044 0045 QCOMPARE(combo.itemText(0), expectedText); 0046 0047 } 0048 0049 void KUrlComboBoxTest::testSetUrlMultipleTimes() 0050 { 0051 KUrlComboBox combo(KUrlComboBox::Directories); 0052 combo.setUrl(QUrl("http://kde.org")); 0053 combo.setUrl(QUrl("http://www.kde.org")); 0054 } 0055 0056 void KUrlComboBoxTest::testRemoveUrl() 0057 { 0058 KUrlComboBox combo(KUrlComboBox::Both); 0059 combo.addDefaultUrl(QUrl("http://kde.org")); 0060 combo.addDefaultUrl(QUrl("http://www.kde.org")); 0061 0062 QStringList urls{"http://foo.org", "http://bar.org"}; 0063 combo.setUrls(urls); 0064 0065 QCOMPARE(combo.urls(), urls); 0066 QCOMPARE(combo.count(), 4); 0067 QCOMPARE(combo.itemText(0), QString("http://kde.org")); 0068 QCOMPARE(combo.itemText(1), QString("http://www.kde.org")); 0069 QCOMPARE(combo.itemText(2), QString("http://foo.org")); 0070 QCOMPARE(combo.itemText(3), QString("http://bar.org")); 0071 0072 // Remove a url 0073 combo.removeUrl(QUrl("http://foo.org")); 0074 QCOMPARE(combo.count(), 3); 0075 QCOMPARE(combo.urls(), QStringList{"http://bar.org"}); 0076 QCOMPARE(combo.itemText(0), QString("http://kde.org")); 0077 QCOMPARE(combo.itemText(1), QString("http://www.kde.org")); 0078 QCOMPARE(combo.itemText(2), QString("http://bar.org")); 0079 0080 // Removing a default url with checkDefaultURLs=true removes the url 0081 combo.removeUrl(QUrl("http://kde.org")); 0082 QCOMPARE(combo.count(), 2); 0083 QCOMPARE(combo.urls(), QStringList{"http://bar.org"}); 0084 QCOMPARE(combo.itemText(0), QString("http://www.kde.org")); 0085 QCOMPARE(combo.itemText(1), QString("http://bar.org")); 0086 0087 // Removing a default url with checkDefaultURLs=false does not remove the url 0088 combo.removeUrl(QUrl("http://www.kde.org"), false); 0089 QCOMPARE(combo.count(), 2); 0090 QCOMPARE(combo.urls(), QStringList{"http://bar.org"}); 0091 QCOMPARE(combo.itemText(0), QString("http://www.kde.org")); 0092 QCOMPARE(combo.itemText(1), QString("http://bar.org")); 0093 0094 // Removing a non-existing url is a no-op 0095 combo.removeUrl(QUrl("http://www.foo.org")); 0096 QCOMPARE(combo.count(), 2); 0097 QCOMPARE(combo.urls(), QStringList{"http://bar.org"}); 0098 QCOMPARE(combo.itemText(0), QString("http://www.kde.org")); 0099 QCOMPARE(combo.itemText(1), QString("http://bar.org")); 0100 0101 // Remove the last user provided url 0102 combo.removeUrl(QUrl("http://bar.org")); 0103 QCOMPARE(combo.count(), 1); 0104 QCOMPARE(combo.urls(), QStringList{}); 0105 QCOMPARE(combo.itemText(0), QString("http://www.kde.org")); 0106 0107 // Remove the last url 0108 combo.removeUrl(QUrl("http://www.kde.org")); 0109 QCOMPARE(combo.count(), 0); 0110 QCOMPARE(combo.urls(), QStringList{}); 0111 QCOMPARE(combo.itemText(0), QString()); 0112 }