PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/auto/qdesktopservices/tst_qdesktopservices.cpp

https://bitbucket.org/jjgod/qt-vtl
C++ | 388 lines | 264 code | 57 blank | 67 comment | 4 complexity | 6518c1ea9a6d1fce3f797909d185b108 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CC0-1.0, CC-BY-SA-4.0, LGPL-2.1, GPL-3.0, Apache-2.0, LGPL-2.0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the test suite of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <QtTest/QtTest>
  42. #include <QStandardItemModel>
  43. #include <qdebug.h>
  44. #include <qdesktopservices.h>
  45. //#define RUN_MANUAL_TESTS
  46. //TESTED_CLASS=
  47. //TESTED_FILES=
  48. class tst_qdesktopservices : public QObject {
  49. Q_OBJECT
  50. public:
  51. tst_qdesktopservices();
  52. virtual ~tst_qdesktopservices();
  53. private slots:
  54. void init();
  55. void cleanup();
  56. void openUrl();
  57. #ifdef Q_OS_SYMBIAN
  58. // These test are manual ones, you need to check from device that
  59. // correct system application is started with correct content
  60. // When you want to run these test, uncomment //#define RUN_MANUAL_TESTS
  61. void openHttpUrl_data();
  62. void openHttpUrl();
  63. void openMailtoUrl_data();
  64. void openMailtoUrl();
  65. void openFileUrl_data();
  66. void openFileUrl();
  67. void openMultipleFileUrls();
  68. #endif
  69. void handlers();
  70. void storageLocation_data();
  71. void storageLocation();
  72. void storageLocationDoesNotEndWithSlash_data();
  73. void storageLocationDoesNotEndWithSlash();
  74. protected:
  75. };
  76. tst_qdesktopservices::tst_qdesktopservices()
  77. {
  78. QCoreApplication::setOrganizationName("Nokia");
  79. QCoreApplication::setApplicationName("tst_qdesktopservices");
  80. }
  81. tst_qdesktopservices::~tst_qdesktopservices()
  82. {
  83. }
  84. void tst_qdesktopservices::init()
  85. {
  86. }
  87. void tst_qdesktopservices::cleanup()
  88. {
  89. }
  90. void tst_qdesktopservices::openUrl()
  91. {
  92. // At the bare minimum check that they return false for invalid url's
  93. QCOMPARE(QDesktopServices::openUrl(QUrl()), false);
  94. #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
  95. // this test is only valid on windows on other systems it might mean open a new document in the application handling .file
  96. QCOMPARE(QDesktopServices::openUrl(QUrl("file://invalid.file")), false);
  97. #endif
  98. }
  99. #ifdef Q_OS_SYMBIAN
  100. void tst_qdesktopservices::openHttpUrl_data()
  101. {
  102. QTest::addColumn<QUrl>("url");
  103. QTest::addColumn<bool>("result");
  104. QTest::newRow("BasicWithHttp") << QUrl("http://www.google.fi") << true;
  105. QTest::newRow("BasicWithoutHttp") << QUrl("www.nokia.fi") << true;
  106. QTest::newRow("BasicWithUserAndPw") << QUrl("http://s60prereleases:oslofjord@pepper.troll.no/s60prereleases/patches/") << true;
  107. QTest::newRow("URL with space") << QUrl("http://www.manataka.org/Contents Page.html") << true;
  108. }
  109. void tst_qdesktopservices::openHttpUrl()
  110. {
  111. #ifndef RUN_MANUAL_TESTS
  112. QSKIP("Test disabled -- only for manual purposes", SkipAll);
  113. #endif
  114. QFETCH(QUrl, url);
  115. QFETCH(bool, result);
  116. QCOMPARE(QDesktopServices::openUrl(url), result);
  117. QTest::qWait(30000);
  118. }
  119. void tst_qdesktopservices::openMailtoUrl_data()
  120. {
  121. QTest::addColumn<QUrl>("url");
  122. QTest::addColumn<bool>("result");
  123. // http://en.wikipedia.org/wiki/E-mail_address
  124. // RFC Valid e-mail addresses
  125. QTest::newRow("Wiki valid email 1") << QUrl("mailto:abc@example.com") << true;
  126. QTest::newRow("Wiki valid email 2") << QUrl("mailto:Abc@example.com") << true;
  127. QTest::newRow("Wiki valid email 3") << QUrl("mailto:aBC@example.com") << true;
  128. QTest::newRow("Wiki valid email 4") << QUrl("mailto:abc.123@example.com") << true;
  129. QTest::newRow("Wiki valid email 5") << QUrl("mailto:1234567890@example.com") << true;
  130. QTest::newRow("Wiki valid email 6") << QUrl("mailto:_______@example.com") << true;
  131. QTest::newRow("Wiki valid email 7") << QUrl("mailto:abc+mailbox/department=shipping@example.com") << true;
  132. // S60 email client considers the next URL invalid, even ity should be valid
  133. QTest::newRow("Wiki valid email 8") << QUrl("mailto:!#$%&'*+-/=?^_`.{|}~@example.com") << true; // all of these characters are allowed
  134. QTest::newRow("Wiki valid email 9") << QUrl("mailto:\"abc@def\"@example.com") << true; // anything goes inside quotation marks
  135. QTest::newRow("Wiki valid email 10") << QUrl("mailto:\"Fred \\\"quota\\\" Bloggs\"@example.com") << true; // however, quotes need escaping
  136. // RFC invalid e-mail addresses
  137. // These return true even though they are invalid, but check that user is notified about invalid URL in mail application
  138. QTest::newRow("Wiki invalid email 1") << QUrl("mailto:Abc.example.com") << true; // character @ is missing
  139. QTest::newRow("Wiki invalid email 2") << QUrl("mailto:Abc.@example.com") << true; // character dot(.) is last in local part
  140. QTest::newRow("Wiki invalid email 3") << QUrl("mailto:Abc..123@example.com") << true; // character dot(.) is double
  141. QTest::newRow("Wiki invalid email 4") << QUrl("mailto:A@b@c@example.com") << true; // only one @ is allowed outside quotations marks
  142. QTest::newRow("Wiki invalid email 5") << QUrl("mailto:()[]\\;:,<>@example.com") << true; // none of the characters before the @ is allowed outside quotation marks
  143. QTest::newRow("Basic") << QUrl("mailto:test@nokia.com") << true;
  144. QTest::newRow("BasicSeveralAddr") << QUrl("mailto:test@nokia.com,test2@nokia.com,test3@nokia.com") << true;
  145. QTest::newRow("BasicAndSubject") << QUrl("mailto:test@nokia.com?subject=hello nokia") << true;
  146. QTest::newRow("BasicAndTo") << QUrl("mailto:test@nokia.com?to=test2@nokia.com") << true;
  147. QTest::newRow("BasicAndCc") << QUrl("mailto:test@nokia.com?cc=mycc@address.com") << true;
  148. QTest::newRow("BasicAndBcc") << QUrl("mailto:test@nokia.com?bcc=mybcc@address.com") << true;
  149. QTest::newRow("BasicAndBody") << QUrl("mailto:test@nokia.com?body=Test email message body") << true;
  150. // RFC examples, these are actually invalid because there is not host defined
  151. // Check that user is notified about invalid URL in mail application
  152. QTest::newRow("RFC2368 Example 1") << QUrl::fromEncoded("mailto:addr1%2C%20addr2") << true;
  153. QTest::newRow("RFC2368 Example 2") << QUrl::fromEncoded("mailto:?to=addr1%2C%20addr2") << true;
  154. QTest::newRow("RFC2368 Example 3") << QUrl("mailto:addr1?to=addr2") << true;
  155. QTest::newRow("RFC2368 Example 4") << QUrl("mailto:joe@example.com?cc=bob@example.com&body=hello") << true;
  156. QTest::newRow("RFC2368 Example 5") << QUrl("mailto:?to=joe@example.com&cc=bob@example.com&body=hello") << true;
  157. QTest::newRow("RFC2368 Example 6") << QUrl("mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com") << true; // OpaqueData
  158. QTest::newRow("RFC2368 Example 7") << QUrl::fromEncoded("mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index") << true;
  159. QTest::newRow("RFC2368 Example 8") << QUrl::fromEncoded("mailto:infobot@example.com?body=send%20current-issue") << true;
  160. QTest::newRow("RFC2368 Example 9") << QUrl("mailto:infobot@example.com?subject=current-issue") << true;
  161. QTest::newRow("RFC2368 Example 10") << QUrl("mailto:chris@example.com") << true;
  162. //QTest::newRow("RFC2368 Example 11 - illegal chars") << QUrl("mailto:joe@example.com?cc=bob@example.com?body=hello") << false;
  163. QTest::newRow("RFC2368 Example 12") << QUrl::fromEncoded("mailto:gorby%25kremvax@example.com") << true; // encoded reserved chars '%'
  164. QTest::newRow("RFC2368 Example 13") << QUrl::fromEncoded("mailto:unlikely%3Faddress@example.com?blat=foop") << true; // encoded reserved chars `?'
  165. }
  166. void tst_qdesktopservices::openMailtoUrl()
  167. {
  168. #ifndef RUN_MANUAL_TESTS
  169. QSKIP("Test disabled -- only for manual purposes", SkipAll);
  170. #endif
  171. QFETCH(QUrl, url);
  172. QFETCH(bool, result);
  173. QCOMPARE(QDesktopServices::openUrl(url), result);
  174. QTest::qWait(5000);
  175. }
  176. void tst_qdesktopservices::openFileUrl_data()
  177. {
  178. QTest::addColumn<QUrl>("url");
  179. QTest::addColumn<bool>("result");
  180. // Text files
  181. QTest::newRow("DOS text file") << QUrl("file:///c:/data/others/dosfile.txt") << true;
  182. QTest::newRow("No EOF text file") << QUrl("file:///c:/data/others/noendofline.txt") << true;
  183. QTest::newRow("text file") << QUrl("file:///c:/data/others/testfile.txt") << true;
  184. QTest::newRow("text file with space") << QUrl("file:///c:/data/others/test file.txt") << true;
  185. // Images
  186. QTest::newRow("BMP image") << QUrl("file:///c:/data/images/image.bmp") << true;
  187. QTest::newRow("GIF image") << QUrl("file:///c:/data/images/image.gif") << true;
  188. QTest::newRow("JPG image") << QUrl("file:///c:/data/images/image.jpg") << true;
  189. QTest::newRow("PNG image") << QUrl("file:///c:/data/images/image.png") << true;
  190. // Audio
  191. QTest::newRow("MP4 audio") << QUrl("file:///c:/data/sounds/aac-only.mp4") << true;
  192. QTest::newRow("3GP audio") << QUrl("file:///c:/data/sounds/audio_3gpp.3gp") << true;
  193. // Video
  194. QTest::newRow("MP4 video") << QUrl("file:///c:/data/videos/vid-mpeg4-22k.mp4") << true;
  195. // Installs
  196. QTest::newRow("SISX") << QUrl("file:///c:/data/installs/ErrRd.sisx") << true;
  197. // Errors
  198. QTest::newRow("File does not exist") << QUrl("file:///c:/thisfileneverexists.txt") << false;
  199. }
  200. void tst_qdesktopservices::openFileUrl()
  201. {
  202. #ifndef RUN_MANUAL_TESTS
  203. QSKIP("Test disabled -- only for manual purposes", SkipAll);
  204. #endif
  205. QFETCH(QUrl, url);
  206. QFETCH(bool, result);
  207. QCOMPARE(QDesktopServices::openUrl(url), result);
  208. QTest::qWait(5000);
  209. }
  210. void tst_qdesktopservices::openMultipleFileUrls()
  211. {
  212. #ifndef RUN_MANUAL_TESTS
  213. QSKIP("Test disabled -- only for manual purposes", SkipAll);
  214. #endif
  215. QCOMPARE(QDesktopServices::openUrl(QUrl("file:///c:/data/images/image.bmp")), true);
  216. QCOMPARE(QDesktopServices::openUrl(QUrl("file:///c:/data/images/image.png")), true);
  217. QCOMPARE(QDesktopServices::openUrl(QUrl("file:///c:/data/others/noendofline.txt")), true);
  218. QCOMPARE(QDesktopServices::openUrl(QUrl("file:///c:/data/installs/ErrRd.sisx")), true);
  219. }
  220. #endif
  221. class MyUrlHandler : public QObject
  222. {
  223. Q_OBJECT
  224. public:
  225. QUrl lastHandledUrl;
  226. public slots:
  227. inline void handle(const QUrl &url) {
  228. lastHandledUrl = url;
  229. }
  230. };
  231. void tst_qdesktopservices::handlers()
  232. {
  233. MyUrlHandler fooHandler;
  234. MyUrlHandler barHandler;
  235. QDesktopServices::setUrlHandler(QString("foo"), &fooHandler, "handle");
  236. QDesktopServices::setUrlHandler(QString("bar"), &barHandler, "handle");
  237. QUrl fooUrl("foo://blub/meh");
  238. QUrl barUrl("bar://hmm/hmmmm");
  239. QVERIFY(QDesktopServices::openUrl(fooUrl));
  240. QVERIFY(QDesktopServices::openUrl(barUrl));
  241. QCOMPARE(fooHandler.lastHandledUrl.toString(), fooUrl.toString());
  242. QCOMPARE(barHandler.lastHandledUrl.toString(), barUrl.toString());
  243. }
  244. Q_DECLARE_METATYPE(QDesktopServices::StandardLocation)
  245. void tst_qdesktopservices::storageLocation_data()
  246. {
  247. QTest::addColumn<QDesktopServices::StandardLocation>("location");
  248. QTest::newRow("DesktopLocation") << QDesktopServices::DesktopLocation;
  249. QTest::newRow("DocumentsLocation") << QDesktopServices::DocumentsLocation;
  250. QTest::newRow("FontsLocation") << QDesktopServices::FontsLocation;
  251. QTest::newRow("ApplicationsLocation") << QDesktopServices::ApplicationsLocation;
  252. QTest::newRow("MusicLocation") << QDesktopServices::MusicLocation;
  253. QTest::newRow("MoviesLocation") << QDesktopServices::MoviesLocation;
  254. QTest::newRow("PicturesLocation") << QDesktopServices::PicturesLocation;
  255. QTest::newRow("TempLocation") << QDesktopServices::TempLocation;
  256. QTest::newRow("HomeLocation") << QDesktopServices::HomeLocation;
  257. QTest::newRow("DataLocation") << QDesktopServices::DataLocation;
  258. }
  259. void tst_qdesktopservices::storageLocation()
  260. {
  261. QFETCH(QDesktopServices::StandardLocation, location);
  262. #ifdef Q_OS_SYMBIAN
  263. QString storageLocation = QDesktopServices::storageLocation(location);
  264. QString displayName = QDesktopServices::displayName(location);
  265. //qDebug( "displayName: %s", displayName );
  266. storageLocation = storageLocation.toLower();
  267. displayName = displayName.toLower();
  268. QString drive = QDir::currentPath().left(2).toLower();
  269. if( drive == "z:" )
  270. drive = "c:";
  271. switch(location) {
  272. case QDesktopServices::DesktopLocation:
  273. QCOMPARE( storageLocation, drive + QString("/data") );
  274. break;
  275. case QDesktopServices::DocumentsLocation:
  276. QCOMPARE( storageLocation, drive + QString("/data") );
  277. break;
  278. case QDesktopServices::FontsLocation:
  279. // Currently point always to ROM
  280. QCOMPARE( storageLocation, QString("z:/resource/fonts") );
  281. break;
  282. case QDesktopServices::ApplicationsLocation:
  283. #ifdef Q_CC_NOKIAX86
  284. QCOMPARE( storageLocation, QString("z:/sys/bin") );
  285. #else
  286. QCOMPARE( storageLocation, drive + QString("/sys/bin") );
  287. #endif
  288. break;
  289. case QDesktopServices::MusicLocation:
  290. QCOMPARE( storageLocation, drive + QString("/data/sounds") );
  291. break;
  292. case QDesktopServices::MoviesLocation:
  293. QCOMPARE( storageLocation, drive + QString("/data/videos") );
  294. break;
  295. case QDesktopServices::PicturesLocation:
  296. QCOMPARE( storageLocation, drive + QString("/data/images") );
  297. break;
  298. case QDesktopServices::TempLocation:
  299. QCOMPARE( storageLocation, QDir::tempPath().toLower());
  300. break;
  301. case QDesktopServices::HomeLocation:
  302. QCOMPARE( storageLocation, QDir::homePath().toLower());
  303. break;
  304. case QDesktopServices::DataLocation:
  305. // Just check the folder not the drive
  306. QCOMPARE( storageLocation.mid(2), QDir::currentPath().mid(2).toLower());
  307. break;
  308. default:
  309. QCOMPARE( storageLocation, QString() );
  310. break;
  311. }
  312. #else
  313. QDesktopServices::storageLocation(location);
  314. QDesktopServices::displayName(location);
  315. #endif
  316. }
  317. void tst_qdesktopservices::storageLocationDoesNotEndWithSlash_data()
  318. {
  319. storageLocation_data();
  320. }
  321. void tst_qdesktopservices::storageLocationDoesNotEndWithSlash()
  322. {
  323. // Currently all desktop locations return their storage location
  324. // with "Unix-style" paths (i.e. they use a slash, not backslash).
  325. QFETCH(QDesktopServices::StandardLocation, location);
  326. QString loc = QDesktopServices::storageLocation(location);
  327. if (loc.size() > 1) // workaround for unlikely case of locations that return '/'
  328. QCOMPARE(loc.endsWith(QLatin1Char('/')), false);
  329. }
  330. QTEST_MAIN(tst_qdesktopservices)
  331. #include "tst_qdesktopservices.moc"