File indexing completed on 2024-02-25 04:00:10

0001 /*
0002     SPDX-FileCopyrightText: 2016 Gregor Mi <codestruct@posteo.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include "../src/gui/urlutil_p.h"
0010 
0011 class UrlUtilTest : public QObject
0012 {
0013     Q_OBJECT
0014 
0015 private Q_SLOTS:
0016     void testFirstChildUrl();
0017 };
0018 
0019 static inline QUrl lUrl(const QString &path)
0020 {
0021     return QUrl::fromLocalFile(path);
0022 }
0023 
0024 void UrlUtilTest::testFirstChildUrl()
0025 {
0026     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/test/data/documents/muh/"), lUrl("/home/test/")), lUrl("/home/test/data"));
0027     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/test/data/documents/muh"), lUrl("/home/test")), lUrl("/home/test/data"));
0028     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/test/data/documents/muh/"), lUrl("/home/test")), lUrl("/home/test/data"));
0029     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/test/data/documents/muh"), lUrl("/home/test/")), lUrl("/home/test/data"));
0030     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/a/"), lUrl("/home")), lUrl("/home/a"));
0031     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/te"), lUrl("/")), lUrl("/te"));
0032     // One letter under root is also a valid child
0033     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/d"), lUrl("/")), lUrl("/d"));
0034     // Same urls should return QUrl()
0035     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/test/data"), lUrl("/home/test/data/")), QUrl());
0036     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/test/data/"), lUrl("/home/test/data")), QUrl());
0037     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/test/"), lUrl("/home/test/")), QUrl());
0038     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/"), lUrl("/")), QUrl());
0039     // Not related urls should return QUrl()
0040     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/aaa/"), lUrl("/home/bbb/")), QUrl());
0041     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/aaa/"), lUrl("/home/bbb/ccc")), QUrl());
0042     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home"), lUrl("/test")), QUrl());
0043     // Child urls in reverse order should return QUrl()
0044     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home"), lUrl("/home/test")), QUrl());
0045     // # is %23 in an URL, so this test could reveal path/URL confusion in the code:
0046     QCOMPARE(KIO::UrlUtil::firstChildUrl(lUrl("/home/a#/b#"), lUrl("/home/a#")), lUrl("/home/a#/b#"));
0047 }
0048 
0049 QTEST_MAIN(UrlUtilTest)
0050 
0051 #include "urlutiltest.moc"