Debian Codegraph @

tests/auto/gui/util/qdesktopservices/tst_qdesktopservices.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4
5 #include <QTest>
6 #include <qdesktopservices.h>
7 #include <qregularexpression.h>
8
9 using namespace Qt::StringLiterals;
10
11 class tst_qdesktopservices : public QObject
12 {
13 Q_OBJECT
14
15 private slots:
16 void openUrl();
17 void handlers();
18 };
19
20 void tst_qdesktopservices::openUrl()
21 {
22 // At the bare minimum check that they return false for invalid url's
23 QCOMPARE(QDesktopServices::openUrl(QUrl()), false);
24 #if defined(Q_OS_WIN)
25 // this test is only valid on windows on other systems it might mean open a new document in the application handling .file
26 const QRegularExpression messagePattern("ShellExecute 'file://invalid\\.file' failed \\(error \\d+\\)\\.");
27 QVERIFY(messagePattern.isValid());
28 QTest::ignoreMessage(QtWarningMsg, messagePattern);
29 QCOMPARE(QDesktopServices::openUrl(QUrl("file://invalid.file")), false);
30 #endif
31 }
32
33 class MyUrlHandler : public QObject
34 {
35 Q_OBJECT
36 public:
37 QUrl lastHandledUrl;
38
39 public slots:
40 inline void handle(const QUrl &url) {
41 lastHandledUrl = url;
42 }
43 };
44
45 void tst_qdesktopservices::handlers()
46 {
47 MyUrlHandler fooHandler;
48 MyUrlHandler barHandler;
49
50 QDesktopServices::setUrlHandler(QString("foo"), &fooHandler, "handle");
51 QDesktopServices::setUrlHandler(QString("bar"), &barHandler, "handle");
52 const auto unsetHandlers = qScopeGuard([] {
53 QDesktopServices::unsetUrlHandler(u"bar"_s);
54 QDesktopServices::unsetUrlHandler(u"foo"_s);
55 });
56
57 QUrl fooUrl("foo://blub/meh");
58 QUrl barUrl("bar://hmm/hmmmm");
59
60 QVERIFY(QDesktopServices::openUrl(fooUrl));
61 QVERIFY(QDesktopServices::openUrl(barUrl));
62
63 QCOMPARE(fooHandler.lastHandledUrl.toString(), fooUrl.toString());
64 QCOMPARE(barHandler.lastHandledUrl.toString(), barUrl.toString());
65 }
66
67 QTEST_MAIN(tst_qdesktopservices)
68
69 #include "tst_qdesktopservices.moc"