File indexing completed on 2024-04-21 14:55:17

0001 /*
0002 This file is part of the KDE libraries
0003 This file has been placed in the Public Domain.
0004 */
0005 
0006 #undef QT_NO_CAST_FROM_ASCII
0007 
0008 #include "ktemporaryfiletest.h"
0009 
0010 #include "qtest_kde.h"
0011 
0012 #include <QDir>
0013 #include <QCoreApplication>
0014 
0015 #include "ktemporaryfile.h"
0016 
0017 QTEST_MAIN(KTemporaryFileTest)
0018 
0019 /*
0020 Notes on these tests:
0021 
0022 - I'm not testing anything that QTemporaryFile should handle, like the
0023 working of setAutoRemove(). I suggest letting Trolltech handle making sure
0024 those things work. These should only test KDE specific functionality.
0025 
0026 */
0027 
0028 void KTemporaryFileTest::initTestCase()
0029 {
0030     kdeTempDir = QDir::tempPath() + '/';
0031     componentName = QCoreApplication::instance()->applicationName();
0032 
0033     QDir qdir(kdeTempDir);
0034     qdir.mkdir("ktempfiletest");
0035 }
0036 
0037 void KTemporaryFileTest::cleanupTestCase()
0038 {
0039     QDir qdir(kdeTempDir);
0040     qdir.rmdir("ktempfiletest");
0041 }
0042 
0043 // Test putting files in the default KDE temp directory
0044 void KTemporaryFileTest::testKTemporaryFile()
0045 {
0046 
0047     //Test basic placement
0048     QString first;
0049     {
0050         KTemporaryFile file;
0051         QVERIFY(file.open());
0052         QVERIFY(file.fileName().startsWith(kdeTempDir + componentName));
0053         QVERIFY(file.fileName().endsWith(QLatin1String(".tmp")));
0054         QVERIFY(QFile::exists(file.fileName()));
0055         first = file.fileName();
0056     }
0057     QVERIFY(!QFile::exists(first));
0058 
0059     // Test we don't get the same twice
0060     {
0061         KTemporaryFile file;
0062         QVERIFY(file.open());
0063         QVERIFY(first != file.fileName());
0064     }
0065 
0066     //Test relative subdirectory
0067     {
0068         KTemporaryFile file;
0069         file.setPrefix("ktempfiletest/");
0070         QVERIFY(file.open());
0071         QVERIFY(file.fileName().startsWith(kdeTempDir + "ktempfiletest/"));
0072         QVERIFY(file.fileName().endsWith(QLatin1String(".tmp")));
0073         QVERIFY(QFile::exists(file.fileName()));
0074     }
0075 
0076     //Test relative filename
0077     {
0078         KTemporaryFile file;
0079         file.setPrefix("spam");
0080         file.setSuffix("eggs");
0081         QVERIFY(file.open());
0082         QVERIFY(file.fileName().startsWith(kdeTempDir + "spam"));
0083         QVERIFY(file.fileName().endsWith(QLatin1String("eggs")));
0084         QVERIFY(QFile::exists(file.fileName()));
0085     }
0086 
0087     //Test suffix only
0088     {
0089         KTemporaryFile file;
0090         file.setSuffix("eggs");
0091         QVERIFY(file.open());
0092         QVERIFY(file.fileName().endsWith(QLatin1String("eggs")));
0093         QVERIFY(QFile::exists(file.fileName()));
0094     }
0095 
0096     //TODO: How to test outside of tmp when we can't be sure what
0097     //directories we have write access to?
0098 }
0099 
0100 #include "moc_ktemporaryfiletest.cpp"