diff --git a/tests/auto/corelib/io/qurl/CMakeLists.txt b/tests/auto/corelib/io/qurl/CMakeLists.txt index d105b323df0..4d135d2375c 100644 --- a/tests/auto/corelib/io/qurl/CMakeLists.txt +++ b/tests/auto/corelib/io/qurl/CMakeLists.txt @@ -25,4 +25,6 @@ qt_internal_add_test(tst_qurl qt_internal_extend_target(tst_qurl CONDITION APPLE SOURCES tst_qurl_mac.mm + LIBRARIES + Qt::CorePrivate ) diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index bbe5815cc48..e9d5aeeebb1 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -63,6 +63,7 @@ private slots: void fromLocalFileNormalize(); void fromLocalFileNormalizeNonRoundtrip_data(); void fromLocalFileNormalizeNonRoundtrip(); + void macTypes_data(); void macTypes(); void relative(); void compat_legacy(); @@ -1594,13 +1595,21 @@ void tst_QUrl::fromLocalFileNormalizeNonRoundtrip() QCOMPARE(url.toString(QUrl::NormalizePathSegments), urlWithNormalizedPath); } -void tst_QUrl::macTypes() +void tst_QUrl::macTypes_data() { #ifndef Q_OS_DARWIN QSKIP("This is a Mac-only test"); #else - extern void tst_QUrl_mactypes(); // in tst_qurl_mac.mm - void tst_QUrl_mactypes(); + extern void tst_QUrl_mactypes_data(); + tst_QUrl_mactypes_data(); +#endif +} + +void tst_QUrl::macTypes() +{ +#ifdef Q_OS_DARWIN + extern void tst_QUrl_mactypes(); + tst_QUrl_mactypes(); #endif } diff --git a/tests/auto/corelib/io/qurl/tst_qurl_mac.mm b/tests/auto/corelib/io/qurl/tst_qurl_mac.mm index a7cf3ebee52..8c6770016cc 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl_mac.mm +++ b/tests/auto/corelib/io/qurl/tst_qurl_mac.mm @@ -2,24 +2,75 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include +#include #include #include -void tst_QUrl_macTypes() +using namespace Qt::StringLiterals; + +void tst_QUrl_mactypes_data() { - { - QUrl qtUrl("example.com"); - const CFURLRef cfUrl = qtUrl.toCFURL(); - QCOMPARE(QUrl::fromCFURL(cfUrl), qtUrl); - qtUrl.setUrl("www.example.com"); - QVERIFY(QUrl::fromCFURL(cfUrl) != qtUrl); - } - { - QUrl qtUrl("example.com"); - const NSURL *nsUrl = qtUrl.toNSURL(); - QCOMPARE(QUrl::fromNSURL(nsUrl), qtUrl); - qtUrl.setUrl("www.example.com"); - QVERIFY(QUrl::fromNSURL(nsUrl) != qtUrl); - } + QTest::addColumn("input"); + QTest::addColumn("cfOutput"); + QTest::addColumn("nsOutput"); + + auto addSimple = [](const char *label, QLatin1StringView urll1) { + QString url = urll1; + QTest::addRow("%s", label) << url << url << url; + }; + addSimple("empty", {}); + + addSimple("https-empty-path", "https://example.com"_L1); + addSimple("https-nonempty-path", "https://example.com/"_L1); + addSimple("https-query", "https://example.com/?a=b"_L1); + addSimple("https-fragment", "https://example.com/#id"_L1); + addSimple("https-query-fragment", "https://example.com/?a=b#id"_L1); + addSimple("file-root", "file:///"_L1); + addSimple("file-path", "file:///etc/passwd"_L1); + + addSimple("file-relative", "file:README.txt"_L1); + addSimple("file-relative-dotdot", "file:../README.txt"_L1); + addSimple("uri-relative", "README.txt"_L1); + addSimple("uri-relative-dotdot", "../README.txt"_L1); + + // QUrl retains [] unencoded, unlike CFURL & NSURL + QTest::newRow("gen-delims") << "x://:@host/:@/[]?:/?@[]?#:/?@[]" + << "x://:@host/:@/%5B%5D?:/?@%5B%5D?#:/?@%5B%5D" + << "x://:@host/:@/%5B%5D?:/?@%5B%5D?#:/?@%5B%5D"; +} + +void tst_QUrl_mactypes() +{ + QFETCH(QString, input); + QFETCH(QString, cfOutput); + QFETCH(QString, nsOutput); + QUrl qtUrl(input); + QUrl otherUrl = qtUrl.isEmpty() ? QUrl("https://example.com") : QUrl(); + + // confirm the conversions result in what we expect it to result + CFURLRef cfUrl = qtUrl.toCFURL(); + QCFString cfStr = CFURLGetString(cfUrl); + QCOMPARE(QString(cfStr), cfOutput); + + const NSURL *nsUrl = qtUrl.toNSURL(); + QVERIFY(nsUrl); + const NSString *nsString = [nsUrl absoluteString]; + QVERIFY(nsString); + QCOMPARE(QString::fromNSString(nsString), nsOutput); + + // confirm that roundtripping works and the equality operator does too + QUrl qtCfUrl = QUrl::fromCFURL(cfUrl); + if (input == cfOutput) { + QCOMPARE(qtCfUrl, qtUrl); + QCOMPARE_NE(qtCfUrl, otherUrl); + } + QCOMPARE(qtCfUrl.isEmpty(), qtUrl.isEmpty()); + + QUrl qtNsUrl = QUrl::fromNSURL(nsUrl); + if (input == nsOutput) { + QCOMPARE(qtNsUrl, qtUrl); + QCOMPARE_NE(qtNsUrl, otherUrl); + } + QCOMPARE(qtNsUrl.isEmpty(), qtUrl.isEmpty()); }