PageRenderTime 47ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ProGamerCode/qtbase
C++ | 153 lines | 87 code | 21 blank | 45 comment | 2 complexity | 0fe43c2d8a15cc9ec1c78f53b7ef6b41 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, MIT
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the test suite of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  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 Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <QtTest/QtTest>
  42. #include <qdebug.h>
  43. #include <qdesktopservices.h>
  44. class tst_qdesktopservices : public QObject
  45. {
  46. Q_OBJECT
  47. public:
  48. tst_qdesktopservices();
  49. virtual ~tst_qdesktopservices();
  50. private slots:
  51. void init();
  52. void cleanup();
  53. void openUrl();
  54. void handlers();
  55. void testDataLocation();
  56. };
  57. tst_qdesktopservices::tst_qdesktopservices()
  58. {
  59. }
  60. tst_qdesktopservices::~tst_qdesktopservices()
  61. {
  62. }
  63. void tst_qdesktopservices::init()
  64. {
  65. }
  66. void tst_qdesktopservices::cleanup()
  67. {
  68. }
  69. void tst_qdesktopservices::openUrl()
  70. {
  71. // At the bare minimum check that they return false for invalid url's
  72. QCOMPARE(QDesktopServices::openUrl(QUrl()), false);
  73. #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
  74. // this test is only valid on windows on other systems it might mean open a new document in the application handling .file
  75. QTest::ignoreMessage(QtWarningMsg, "ShellExecute 'file://invalid.file' failed (error 3).");
  76. QCOMPARE(QDesktopServices::openUrl(QUrl("file://invalid.file")), false);
  77. #endif
  78. }
  79. class MyUrlHandler : public QObject
  80. {
  81. Q_OBJECT
  82. public:
  83. QUrl lastHandledUrl;
  84. public slots:
  85. inline void handle(const QUrl &url) {
  86. lastHandledUrl = url;
  87. }
  88. };
  89. void tst_qdesktopservices::handlers()
  90. {
  91. MyUrlHandler fooHandler;
  92. MyUrlHandler barHandler;
  93. QDesktopServices::setUrlHandler(QString("foo"), &fooHandler, "handle");
  94. QDesktopServices::setUrlHandler(QString("bar"), &barHandler, "handle");
  95. QUrl fooUrl("foo://blub/meh");
  96. QUrl barUrl("bar://hmm/hmmmm");
  97. QVERIFY(QDesktopServices::openUrl(fooUrl));
  98. QVERIFY(QDesktopServices::openUrl(barUrl));
  99. QCOMPARE(fooHandler.lastHandledUrl.toString(), fooUrl.toString());
  100. QCOMPARE(barHandler.lastHandledUrl.toString(), barUrl.toString());
  101. }
  102. #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
  103. #define Q_XDG_PLATFORM
  104. #endif
  105. void tst_qdesktopservices::testDataLocation()
  106. {
  107. // This is the one point where QDesktopServices and QStandardPaths differ.
  108. // QDesktopServices on unix returns "data"/orgname/appname for DataLocation, for Qt4 compat.
  109. // And the appname in qt4 defaulted to empty, not to argv[0].
  110. {
  111. const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
  112. const QString app = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
  113. #ifdef Q_XDG_PLATFORM
  114. QCOMPARE(app, base + "/data//"); // as ugly as in Qt4
  115. #else
  116. QCOMPARE(app, base);
  117. #endif
  118. }
  119. QCoreApplication::instance()->setOrganizationName("Qt");
  120. QCoreApplication::instance()->setApplicationName("QtTest");
  121. {
  122. const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
  123. const QString app = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
  124. #ifdef Q_XDG_PLATFORM
  125. QCOMPARE(app, base + "/data/Qt/QtTest");
  126. #else
  127. QCOMPARE(app, base + "/Qt/QtTest");
  128. #endif
  129. }
  130. }
  131. QTEST_MAIN(tst_qdesktopservices)
  132. #include "tst_qdesktopservices.moc"