Changeset View
Changeset View
Standalone View
Standalone View
kioslave/desktop/tests/kio_desktop_test.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2009 David Faure <faure@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include <kdirlister.h> | ||||
21 | #include <QDesktopServices> | ||||
22 | #include <QStandardPaths> | ||||
23 | #include <QTemporaryFile> | ||||
24 | #include <QTest> | ||||
25 | #include <kio/job.h> | ||||
26 | #include <kio/copyjob.h> | ||||
27 | | ||||
28 | class TestDesktop : public QObject | ||||
29 | { | ||||
30 | Q_OBJECT | ||||
31 | | ||||
32 | public: | ||||
33 | TestDesktop() {} | ||||
34 | | ||||
35 | private Q_SLOTS: | ||||
36 | void initTestCase() | ||||
37 | { | ||||
38 | setenv( "KDE_FORK_SLAVES", "yes", true ); | ||||
39 | | ||||
40 | //make KIOs use test mode too | ||||
41 | setenv("KIOSLAVE_ENABLE_TESTMODE", "1", 1); | ||||
42 | QStandardPaths::setTestModeEnabled(true); | ||||
43 | | ||||
44 | m_desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); | ||||
45 | m_testFileName = QLatin1String("kio_desktop_test_file"); | ||||
46 | } | ||||
47 | void cleanupTestCase() | ||||
48 | { | ||||
49 | QFile::remove(m_desktopPath + '/' + m_testFileName); | ||||
50 | QFile::remove(m_desktopPath + '/' + m_testFileName + ".part"); | ||||
51 | } | ||||
52 | | ||||
53 | void testCopyToDesktop() | ||||
54 | { | ||||
55 | QTemporaryFile tempFile; | ||||
56 | QVERIFY(tempFile.open()); | ||||
57 | tempFile.write( "Hello world\n", 12 ); | ||||
58 | QString fileName = tempFile.fileName(); | ||||
59 | tempFile.close(); | ||||
60 | KIO::Job* job = KIO::file_copy(QUrl::fromLocalFile(fileName), QUrl("desktop:/" + m_testFileName), -1, KIO::HideProgressInfo); | ||||
61 | job->setUiDelegate(0); | ||||
62 | QVERIFY(job->exec()); | ||||
63 | QVERIFY(QFile::exists(m_desktopPath + '/' + m_testFileName)); | ||||
64 | } | ||||
65 | | ||||
66 | void testMostLocalUrl() // relies on testCopyToDesktop being run before | ||||
67 | { | ||||
68 | const QUrl desktopUrl("desktop:/" + m_testFileName); | ||||
69 | const QString filePath(m_desktopPath + '/' + m_testFileName); | ||||
70 | KIO::StatJob* job = KIO::mostLocalUrl(desktopUrl, KIO::HideProgressInfo); | ||||
71 | QVERIFY(job); | ||||
72 | bool ok = job->exec(); | ||||
73 | QVERIFY(ok); | ||||
74 | QCOMPARE(job->mostLocalUrl().toLocalFile(), filePath); | ||||
75 | } | ||||
76 | | ||||
77 | void testRename_data() | ||||
78 | { | ||||
79 | QTest::addColumn<bool>("withDirListerCache"); | ||||
80 | QTest::addColumn<QString>("srcFile"); | ||||
81 | QTest::addColumn<QString>("destFile"); | ||||
82 | | ||||
83 | const QString orig = "desktop:/" + m_testFileName; | ||||
84 | const QString part = orig + ".part"; | ||||
85 | QTest::newRow("from orig to .part") << false << orig << part; | ||||
86 | QTest::newRow("from .part to orig") << false << part << orig; | ||||
87 | // Warnings: all tests without dirlister cache should above this line | ||||
88 | // and all tests with it should be below - the cache stays forever once it exists. | ||||
89 | QTest::newRow("from orig to .part, with cache") << true << orig << part; | ||||
90 | QTest::newRow("from .part to orig, with cache (#218719)") << true << part << orig; | ||||
91 | } | ||||
92 | | ||||
93 | void testRename() // relies on testCopyToDesktop being run before | ||||
94 | { | ||||
95 | QFETCH(bool, withDirListerCache); | ||||
96 | QFETCH(QString, srcFile); | ||||
97 | QFETCH(QString, destFile); | ||||
98 | | ||||
99 | if (withDirListerCache) { | ||||
100 | KDirLister lister; | ||||
101 | lister.openUrl(QUrl(QStringLiteral("desktop:/"))); | ||||
102 | QEventLoop eventLoop; | ||||
103 | connect(&lister, static_cast<void (KDirLister::*)()>(&KDirLister::completed), &eventLoop, &QEventLoop::quit); | ||||
104 | eventLoop.exec(QEventLoop::ExcludeUserInputEvents); | ||||
105 | } | ||||
106 | | ||||
107 | const QUrl srcUrl = QUrl(srcFile); | ||||
108 | const QUrl destUrl = QUrl(destFile); | ||||
109 | | ||||
110 | const QString srcFilePath(m_desktopPath + srcUrl.path()); | ||||
111 | QVERIFY(QFile::exists(srcFilePath)); | ||||
112 | const QString destFilePath(m_desktopPath + destUrl.path()); | ||||
113 | QVERIFY(!QFile::exists(destFilePath)); | ||||
114 | | ||||
115 | KIO::CopyJob* job = KIO::move(srcUrl, destUrl, KIO::HideProgressInfo); | ||||
116 | job->setUiDelegate(0); | ||||
117 | QVERIFY(job); | ||||
118 | bool ok = job->exec(); | ||||
119 | QVERIFY(ok); | ||||
120 | QVERIFY(!QFile::exists(srcFilePath)); | ||||
121 | QVERIFY(QFile::exists(destFilePath)); | ||||
122 | } | ||||
123 | | ||||
124 | private: | ||||
125 | QString m_desktopPath; | ||||
126 | QString m_testFileName; | ||||
127 | }; | ||||
128 | | ||||
129 | QTEST_GUILESS_MAIN(TestDesktop) | ||||
130 | | ||||
131 | #include "kio_desktop_test.moc" |