1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31#include <qdebug.h>
32#include <qdesktopservices.h>
33#include <qregularexpression.h>
34
35class tst_qdesktopservices : public QObject
36{
37 Q_OBJECT
38
39private slots:
40 void openUrl();
41 void handlers();
42#if QT_DEPRECATED_SINCE(5, 0)
43 void testDataLocation();
44#endif
45};
46
47void tst_qdesktopservices::openUrl()
48{
49 // At the bare minimum check that they return false for invalid url's
50 QCOMPARE(QDesktopServices::openUrl(QUrl()), false);
51#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
52 // this test is only valid on windows on other systems it might mean open a new document in the application handling .file
53 const QRegularExpression messagePattern("ShellExecute 'file://invalid\\.file' failed \\(error \\d+\\)\\.");
54 QVERIFY(messagePattern.isValid());
55 QTest::ignoreMessage(QtWarningMsg, messagePattern);
56 QCOMPARE(QDesktopServices::openUrl(QUrl("file://invalid.file")), false);
57#endif
58}
59
60class MyUrlHandler : public QObject
61{
62 Q_OBJECT
63public:
64 QUrl lastHandledUrl;
65
66public slots:
67 inline void handle(const QUrl &url) {
68 lastHandledUrl = url;
69 }
70};
71
72#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
73# define CAN_IMPLICITLY_UNSET
74#endif
75
76void tst_qdesktopservices::handlers()
77{
78 MyUrlHandler fooHandler;
79 MyUrlHandler barHandler;
80
81 QDesktopServices::setUrlHandler(scheme: QString("foo"), receiver: &fooHandler, method: "handle");
82 QDesktopServices::setUrlHandler(scheme: QString("bar"), receiver: &barHandler, method: "handle");
83#ifndef CAN_IMPLICITLY_UNSET
84 const auto unsetHandlers = qScopeGuard([] {
85 QDesktopServices::unsetUrlHandler(u"bar"_qs);
86 QDesktopServices::unsetUrlHandler(u"foo"_qs);
87 });
88#endif
89
90 QUrl fooUrl("foo://blub/meh");
91 QUrl barUrl("bar://hmm/hmmmm");
92
93 QVERIFY(QDesktopServices::openUrl(fooUrl));
94 QVERIFY(QDesktopServices::openUrl(barUrl));
95
96 QCOMPARE(fooHandler.lastHandledUrl.toString(), fooUrl.toString());
97 QCOMPARE(barHandler.lastHandledUrl.toString(), barUrl.toString());
98
99#ifdef CAN_IMPLICITLY_UNSET
100 for (int i = 0; i < 2; ++i)
101 QTest::ignoreMessage(type: QtWarningMsg,
102 message: "Please call QDesktopServices::unsetUrlHandler() before destroying a "
103 "registered URL handler object.\n"
104 "Support for destroying a registered URL handler object is deprecated, "
105 "and will be removed in Qt 6.6.");
106#endif
107}
108
109#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
110#define Q_XDG_PLATFORM
111#endif
112
113#if QT_DEPRECATED_SINCE(5, 0)
114void tst_qdesktopservices::testDataLocation()
115{
116 // This is the one point where QDesktopServices and QStandardPaths differ.
117 // QDesktopServices on unix returns "data"/orgname/appname for DataLocation, for Qt4 compat.
118 // And the appname in qt4 defaulted to empty, not to argv[0].
119 {
120 const QString base = QStandardPaths::writableLocation(type: QStandardPaths::GenericDataLocation);
121 const QString app = QDesktopServices::storageLocation(type: QDesktopServices::DataLocation);
122#ifdef Q_XDG_PLATFORM
123 QCOMPARE(app, base + "/data//"); // as ugly as in Qt4
124#else
125 QCOMPARE(app, base);
126#endif
127 }
128 QCoreApplication::instance()->setOrganizationName("Qt");
129 QCoreApplication::instance()->setApplicationName("QtTest");
130 {
131 const QString base = QStandardPaths::writableLocation(type: QStandardPaths::GenericDataLocation);
132 const QString app = QDesktopServices::storageLocation(type: QDesktopServices::DataLocation);
133#ifdef Q_XDG_PLATFORM
134 QCOMPARE(app, base + "/data/Qt/QtTest");
135#else
136 QCOMPARE(app, base + "/Qt/QtTest");
137#endif
138 }
139}
140#endif
141
142QTEST_MAIN(tst_qdesktopservices)
143
144#include "tst_qdesktopservices.moc"
145

source code of qtbase/tests/auto/gui/util/qdesktopservices/tst_qdesktopservices.cpp