PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/manctl/qtbase
C++ | 152 lines | 86 code | 21 blank | 45 comment | 2 complexity | 51026ed78674467243b15e083fd05cb4 MD5 | raw file
  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. QCOMPARE(QDesktopServices::openUrl(QUrl("file://invalid.file")), false);
  76. #endif
  77. }
  78. class MyUrlHandler : public QObject
  79. {
  80. Q_OBJECT
  81. public:
  82. QUrl lastHandledUrl;
  83. public slots:
  84. inline void handle(const QUrl &url) {
  85. lastHandledUrl = url;
  86. }
  87. };
  88. void tst_qdesktopservices::handlers()
  89. {
  90. MyUrlHandler fooHandler;
  91. MyUrlHandler barHandler;
  92. QDesktopServices::setUrlHandler(QString("foo"), &fooHandler, "handle");
  93. QDesktopServices::setUrlHandler(QString("bar"), &barHandler, "handle");
  94. QUrl fooUrl("foo://blub/meh");
  95. QUrl barUrl("bar://hmm/hmmmm");
  96. QVERIFY(QDesktopServices::openUrl(fooUrl));
  97. QVERIFY(QDesktopServices::openUrl(barUrl));
  98. QCOMPARE(fooHandler.lastHandledUrl.toString(), fooUrl.toString());
  99. QCOMPARE(barHandler.lastHandledUrl.toString(), barUrl.toString());
  100. }
  101. #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
  102. #define Q_XDG_PLATFORM
  103. #endif
  104. void tst_qdesktopservices::testDataLocation()
  105. {
  106. // This is the one point where QDesktopServices and QStandardPaths differ.
  107. // QDesktopServices on unix returns "data"/orgname/appname for DataLocation, for Qt4 compat.
  108. // And the appname in qt4 defaulted to empty, not to argv[0].
  109. {
  110. const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
  111. const QString app = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
  112. #ifdef Q_XDG_PLATFORM
  113. QCOMPARE(app, base + "/data//"); // as ugly as in Qt4
  114. #else
  115. QCOMPARE(app, base);
  116. #endif
  117. }
  118. QCoreApplication::instance()->setOrganizationName("Qt");
  119. QCoreApplication::instance()->setApplicationName("QtTest");
  120. {
  121. const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
  122. const QString app = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
  123. #ifdef Q_XDG_PLATFORM
  124. QCOMPARE(app, base + "/data/Qt/QtTest");
  125. #else
  126. QCOMPARE(app, base + "/Qt/QtTest");
  127. #endif
  128. }
  129. }
  130. QTEST_MAIN(tst_qdesktopservices)
  131. #include "tst_qdesktopservices.moc"