1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
SPDX-FileCopyrightText: 2015 Alejandro Fiestas Olivares <afiestas@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kurlcomboboxtest.h"
#include "kurlcombobox.h"
#include <QtTestWidgets>
QTEST_MAIN(KUrlComboBoxTest)
void KUrlComboBoxTest::testTextForItem_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<QString>("expectedText");
QTest::newRow("with_host") << "ftp://foo.com/folder"
<< "ftp://foo.com/folder/";
QTest::newRow("with_no_host") << "smb://"
<< "smb://";
QTest::newRow("with_host_without_path") << "ftp://user@example.com"
<< "ftp://user@example.com";
}
void KUrlComboBoxTest::testTextForItem()
{
QFETCH(QString, url);
QFETCH(QString, expectedText);
KUrlComboBox combo(KUrlComboBox::Directories);
combo.setUrl(QUrl(url));
QCOMPARE(combo.itemText(0), expectedText);
}
void KUrlComboBoxTest::testSetUrlMultipleTimes()
{
KUrlComboBox combo(KUrlComboBox::Directories);
combo.setUrl(QUrl("http://kde.org"));
combo.setUrl(QUrl("http://www.kde.org"));
}
void KUrlComboBoxTest::testRemoveUrl()
{
KUrlComboBox combo(KUrlComboBox::Both);
combo.addDefaultUrl(QUrl("http://kde.org"));
combo.addDefaultUrl(QUrl("http://www.kde.org"));
QStringList urls{"http://foo.org", "http://bar.org"};
combo.setUrls(urls);
QCOMPARE(combo.urls(), urls);
QCOMPARE(combo.count(), 4);
QCOMPARE(combo.itemText(0), QString("http://kde.org"));
QCOMPARE(combo.itemText(1), QString("http://www.kde.org"));
QCOMPARE(combo.itemText(2), QString("http://foo.org"));
QCOMPARE(combo.itemText(3), QString("http://bar.org"));
// Remove a url
combo.removeUrl(QUrl("http://foo.org"));
QCOMPARE(combo.count(), 3);
QCOMPARE(combo.urls(), QStringList{"http://bar.org"});
QCOMPARE(combo.itemText(0), QString("http://kde.org"));
QCOMPARE(combo.itemText(1), QString("http://www.kde.org"));
QCOMPARE(combo.itemText(2), QString("http://bar.org"));
// Removing a default url with checkDefaultURLs=true removes the url
combo.removeUrl(QUrl("http://kde.org"));
QCOMPARE(combo.count(), 2);
QCOMPARE(combo.urls(), QStringList{"http://bar.org"});
QCOMPARE(combo.itemText(0), QString("http://www.kde.org"));
QCOMPARE(combo.itemText(1), QString("http://bar.org"));
// Removing a default url with checkDefaultURLs=false does not remove the url
combo.removeUrl(QUrl("http://www.kde.org"), false);
QCOMPARE(combo.count(), 2);
QCOMPARE(combo.urls(), QStringList{"http://bar.org"});
QCOMPARE(combo.itemText(0), QString("http://www.kde.org"));
QCOMPARE(combo.itemText(1), QString("http://bar.org"));
// Removing a non-existing url is a no-op
combo.removeUrl(QUrl("http://www.foo.org"));
QCOMPARE(combo.count(), 2);
QCOMPARE(combo.urls(), QStringList{"http://bar.org"});
QCOMPARE(combo.itemText(0), QString("http://www.kde.org"));
QCOMPARE(combo.itemText(1), QString("http://bar.org"));
// Remove the last user provided url
combo.removeUrl(QUrl("http://bar.org"));
QCOMPARE(combo.count(), 1);
QCOMPARE(combo.urls(), QStringList{});
QCOMPARE(combo.itemText(0), QString("http://www.kde.org"));
// Remove the last url
combo.removeUrl(QUrl("http://www.kde.org"));
QCOMPARE(combo.count(), 0);
QCOMPARE(combo.urls(), QStringList{});
QCOMPARE(combo.itemText(0), QString());
}
void KUrlComboBoxTest::testAddUrls()
{
// GIVEN
KUrlComboBox combo(KUrlComboBox::Both);
combo.addDefaultUrl(QUrl("http://kde.org"));
combo.addDefaultUrl(QUrl("http://www.kde.org"));
const QStringList urls{"http://foo.org", "http://bar.org"};
combo.setUrls(urls);
// WHEN
const QString url("http://foo.org/newUrl");
combo.setUrl(QUrl(url));
// THEN
QStringList expected = urls;
expected << url;
QCOMPARE(combo.urls(), expected);
}
void KUrlComboBoxTest::testSetMaxItems()
{
// GIVEN
KUrlComboBox combo(KUrlComboBox::Both);
combo.addDefaultUrl(QUrl("http://kde.org"));
combo.addDefaultUrl(QUrl("http://www.kde.org"));
const QStringList urls{"http://foo.org", "http://bar.org", "http://example.org", "http://example2.org"};
combo.setUrls(urls);
QStringList expected = urls;
QCOMPARE(combo.urls(), expected);
// WHEN
combo.setMaxItems(4); // includes the default URLs
// THEN
expected = expected.mid(2);
QCOMPARE(combo.urls(), expected);
// WHEN
combo.setMaxItems(1); // no room for additional URLs
// THEN
QCOMPARE(combo.urls(), QStringList());
}
#include "moc_kurlcomboboxtest.cpp"
|