PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/f3822/qtbase
C++ | 107 lines | 62 code | 16 blank | 29 comment | 1 complexity | 7d61bc7f8ce6aa263612614a53a1210c MD5 | raw file
  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. #include <QTest>
  29. #include <qdesktopservices.h>
  30. #include <qregularexpression.h>
  31. class tst_qdesktopservices : public QObject
  32. {
  33. Q_OBJECT
  34. private slots:
  35. void openUrl();
  36. void handlers();
  37. };
  38. void tst_qdesktopservices::openUrl()
  39. {
  40. // At the bare minimum check that they return false for invalid url's
  41. QCOMPARE(QDesktopServices::openUrl(QUrl()), false);
  42. #if defined(Q_OS_WIN)
  43. // this test is only valid on windows on other systems it might mean open a new document in the application handling .file
  44. const QRegularExpression messagePattern("ShellExecute 'file://invalid\\.file' failed \\(error \\d+\\)\\.");
  45. QVERIFY(messagePattern.isValid());
  46. QTest::ignoreMessage(QtWarningMsg, messagePattern);
  47. QCOMPARE(QDesktopServices::openUrl(QUrl("file://invalid.file")), false);
  48. #endif
  49. }
  50. class MyUrlHandler : public QObject
  51. {
  52. Q_OBJECT
  53. public:
  54. QUrl lastHandledUrl;
  55. public slots:
  56. inline void handle(const QUrl &url) {
  57. lastHandledUrl = url;
  58. }
  59. };
  60. #if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
  61. # define CAN_IMPLICITLY_UNSET
  62. #endif
  63. void tst_qdesktopservices::handlers()
  64. {
  65. MyUrlHandler fooHandler;
  66. MyUrlHandler barHandler;
  67. QDesktopServices::setUrlHandler(QString("foo"), &fooHandler, "handle");
  68. QDesktopServices::setUrlHandler(QString("bar"), &barHandler, "handle");
  69. #ifndef CAN_IMPLICITLY_UNSET
  70. const auto unsetHandlers = qScopeGuard([] {
  71. QDesktopServices::unsetUrlHandler(u"bar"_qs);
  72. QDesktopServices::unsetUrlHandler(u"foo"_qs);
  73. });
  74. #endif
  75. QUrl fooUrl("foo://blub/meh");
  76. QUrl barUrl("bar://hmm/hmmmm");
  77. QVERIFY(QDesktopServices::openUrl(fooUrl));
  78. QVERIFY(QDesktopServices::openUrl(barUrl));
  79. QCOMPARE(fooHandler.lastHandledUrl.toString(), fooUrl.toString());
  80. QCOMPARE(barHandler.lastHandledUrl.toString(), barUrl.toString());
  81. #ifdef CAN_IMPLICITLY_UNSET
  82. for (int i = 0; i < 2; ++i)
  83. QTest::ignoreMessage(QtWarningMsg,
  84. "Please call QDesktopServices::unsetUrlHandler() before destroying a "
  85. "registered URL handler object.\n"
  86. "Support for destroying a registered URL handler object is deprecated, "
  87. "and will be removed in Qt 6.6.");
  88. #endif
  89. }
  90. QTEST_MAIN(tst_qdesktopservices)
  91. #include "tst_qdesktopservices.moc"