author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <qcoreapplication.h> |
|
45 |
#include <qstring.h> |
|
46 |
#include <qtemporaryfile.h> |
|
47 |
#include <qfile.h> |
|
48 |
#include <qdir.h> |
|
49 |
#include <qset.h> |
|
50 |
||
51 |
#if defined(Q_OS_WIN) |
|
52 |
# include <windows.h> |
|
53 |
#endif |
|
54 |
#if defined(Q_OS_UNIX) |
|
55 |
# include <sys/types.h> |
|
56 |
# include <sys/stat.h> |
|
57 |
# include <errno.h> |
|
58 |
# include <fcntl.h> // open(2) |
|
59 |
# include <unistd.h> // close(2) |
|
60 |
#endif |
|
61 |
||
62 |
#ifdef Q_OS_SYMBIAN |
|
63 |
#define SRCDIR "" |
|
64 |
#endif |
|
65 |
||
66 |
//TESTED_CLASS= |
|
67 |
//TESTED_FILES= |
|
68 |
||
69 |
class tst_QTemporaryFile : public QObject |
|
70 |
{ |
|
71 |
Q_OBJECT |
|
72 |
public: |
|
73 |
tst_QTemporaryFile(); |
|
74 |
virtual ~tst_QTemporaryFile(); |
|
75 |
public slots: |
|
76 |
void init(); |
|
77 |
void cleanup(); |
|
78 |
private slots: |
|
79 |
void construction(); |
|
80 |
void fileTemplate(); |
|
81 |
void fileTemplate_data(); |
|
82 |
void getSetCheck(); |
|
83 |
void fileName(); |
|
84 |
void fileNameIsEmpty(); |
|
85 |
void autoRemove(); |
|
86 |
void write(); |
|
87 |
void openCloseOpenClose(); |
|
88 |
void size(); |
|
89 |
void resize(); |
|
90 |
void openOnRootDrives(); |
|
91 |
void stressTest(); |
|
92 |
void rename(); |
|
93 |
void renameFdLeak(); |
|
94 |
void reOpenThroughQFile(); |
|
95 |
void keepOpenMode(); |
|
96 |
void resetTemplateAfterError(); |
|
97 |
void setTemplateAfterOpen(); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
98 |
void autoRemoveAfterFailedRename(); |
0 | 99 |
|
100 |
public: |
|
101 |
}; |
|
102 |
||
103 |
void tst_QTemporaryFile::construction() |
|
104 |
{ |
|
105 |
QTemporaryFile file(0); |
|
106 |
QString tmp = QDir::tempPath(); |
|
107 |
QCOMPARE(file.fileTemplate().left(tmp.size()), tmp); |
|
108 |
QCOMPARE(file.fileTemplate().at(tmp.size()), QChar('/')); |
|
109 |
} |
|
110 |
||
111 |
// Testing get/set functions |
|
112 |
void tst_QTemporaryFile::getSetCheck() |
|
113 |
{ |
|
114 |
QTemporaryFile obj1; |
|
115 |
// bool QTemporaryFile::autoRemove() |
|
116 |
// void QTemporaryFile::setAutoRemove(bool) |
|
117 |
obj1.setAutoRemove(false); |
|
118 |
QCOMPARE(false, obj1.autoRemove()); |
|
119 |
obj1.setAutoRemove(true); |
|
120 |
QCOMPARE(true, obj1.autoRemove()); |
|
121 |
} |
|
122 |
||
123 |
tst_QTemporaryFile::tst_QTemporaryFile() |
|
124 |
{ |
|
125 |
} |
|
126 |
||
127 |
tst_QTemporaryFile::~tst_QTemporaryFile() |
|
128 |
{ |
|
129 |
||
130 |
} |
|
131 |
||
132 |
void tst_QTemporaryFile::init() |
|
133 |
{ |
|
134 |
// TODO: Add initialization code here. |
|
135 |
// This will be executed immediately before each test is run. |
|
136 |
} |
|
137 |
||
138 |
void tst_QTemporaryFile::cleanup() |
|
139 |
{ |
|
140 |
// TODO: Add cleanup code here. |
|
141 |
// This will be executed immediately after each test is run. |
|
142 |
} |
|
143 |
||
144 |
void tst_QTemporaryFile::fileTemplate_data() |
|
145 |
{ |
|
146 |
QTest::addColumn<QString>("constructorTemplate"); |
|
147 |
QTest::addColumn<QString>("suffix"); |
|
148 |
QTest::addColumn<QString>("fileTemplate"); |
|
149 |
||
150 |
QTest::newRow("constructor default") << "" << "" << ""; |
|
151 |
QTest::newRow("constructor with xxx sufix") << "qt_XXXXXXxxx" << "xxx" << ""; |
|
152 |
QTest::newRow("constructor with xXx sufix") << "qt_XXXXXXxXx" << "xXx" << ""; |
|
153 |
QTest::newRow("constructor with no sufix") << "qt_XXXXXX" << "" << ""; |
|
154 |
QTest::newRow("constructor with >6 X's and xxx suffix") << "qt_XXXXXXXXXXxxx" << "xxx" << ""; |
|
155 |
QTest::newRow("constructor with >6 X's, no suffix") << "qt_XXXXXXXXXX" << "" << ""; |
|
156 |
||
157 |
QTest::newRow("set template, no suffix") << "" << "" << "foo"; |
|
158 |
QTest::newRow("set template, with lowercase XXXXXX") << "" << "xxxxxx" << "qt_XXXXXXxxxxxx"; |
|
159 |
QTest::newRow("set template, with xxx") << "" << ".xxx" << "qt_XXXXXX.xxx"; |
|
160 |
QTest::newRow("set template, with >6 X's") << "" << ".xxx" << "qt_XXXXXXXXXXXXXX.xxx"; |
|
161 |
QTest::newRow("set template, with >6 X's, no suffix") << "" << "" << "qt_XXXXXXXXXXXXXX"; |
|
162 |
} |
|
163 |
||
164 |
void tst_QTemporaryFile::fileTemplate() |
|
165 |
{ |
|
166 |
QFETCH(QString, constructorTemplate); |
|
167 |
QFETCH(QString, suffix); |
|
168 |
QFETCH(QString, fileTemplate); |
|
169 |
||
170 |
QTemporaryFile file(constructorTemplate); |
|
171 |
if (!fileTemplate.isEmpty()) |
|
172 |
file.setFileTemplate(fileTemplate); |
|
173 |
||
174 |
QCOMPARE(file.open(), true); |
|
175 |
||
176 |
QCOMPARE(file.fileName().right(suffix.length()), suffix); |
|
177 |
file.close(); |
|
178 |
} |
|
179 |
||
180 |
||
181 |
/* |
|
182 |
This tests whether the temporary file really gets placed in QDir::tempPath |
|
183 |
*/ |
|
184 |
void tst_QTemporaryFile::fileName() |
|
185 |
{ |
|
186 |
// Get QDir::tempPath and make an absolute path. |
|
187 |
QString tempPath = QDir::tempPath(); |
|
188 |
QString absoluteTempPath = QDir(tempPath).absolutePath(); |
|
189 |
QTemporaryFile file; |
|
190 |
file.setAutoRemove(true); |
|
191 |
file.open(); |
|
192 |
QString fileName = file.fileName(); |
|
193 |
QVERIFY(QFile::exists(fileName)); |
|
194 |
// Get path to the temp file, whithout the file name. |
|
195 |
QString absoluteFilePath = QFileInfo(fileName).absolutePath(); |
|
196 |
#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) |
|
197 |
absoluteFilePath = absoluteFilePath.toLower(); |
|
198 |
absoluteTempPath = absoluteTempPath.toLower(); |
|
199 |
#endif |
|
200 |
QCOMPARE(absoluteFilePath, absoluteTempPath); |
|
201 |
} |
|
202 |
||
203 |
void tst_QTemporaryFile::fileNameIsEmpty() |
|
204 |
{ |
|
205 |
QString filename; |
|
206 |
{ |
|
207 |
QTemporaryFile file; |
|
208 |
QVERIFY(file.fileName().isEmpty()); |
|
209 |
||
210 |
QVERIFY(file.open()); |
|
211 |
QVERIFY(!file.fileName().isEmpty()); |
|
212 |
||
213 |
filename = file.fileName(); |
|
214 |
QVERIFY(QFile::exists(filename)); |
|
215 |
||
216 |
file.close(); |
|
217 |
QVERIFY(!file.isOpen()); |
|
218 |
QVERIFY(QFile::exists(filename)); |
|
219 |
QVERIFY(!file.fileName().isEmpty()); |
|
220 |
} |
|
221 |
QVERIFY(!QFile::exists(filename)); |
|
222 |
} |
|
223 |
||
224 |
void tst_QTemporaryFile::autoRemove() |
|
225 |
{ |
|
226 |
// Test auto remove |
|
227 |
QString fileName; |
|
228 |
{ |
|
229 |
QTemporaryFile file("tempXXXXXX"); |
|
230 |
file.setAutoRemove(true); |
|
231 |
QVERIFY(file.open()); |
|
232 |
fileName = file.fileName(); |
|
233 |
file.close(); |
|
234 |
} |
|
235 |
QVERIFY(!QFile::exists(fileName)); |
|
236 |
||
237 |
// Test if disabling auto remove works. |
|
238 |
{ |
|
239 |
QTemporaryFile file("tempXXXXXX"); |
|
240 |
file.setAutoRemove(false); |
|
241 |
QVERIFY(file.open()); |
|
242 |
fileName = file.fileName(); |
|
243 |
file.close(); |
|
244 |
} |
|
245 |
QVERIFY(QFile::exists(fileName)); |
|
246 |
QVERIFY(QFile::remove(fileName)); |
|
247 |
||
248 |
||
249 |
// Do not explicitly call setAutoRemove (tests if it really is the default as documented) |
|
250 |
{ |
|
251 |
QTemporaryFile file("tempXXXXXX"); |
|
252 |
QVERIFY(file.open()); |
|
253 |
fileName = file.fileName(); |
|
254 |
file.close(); |
|
255 |
} |
|
256 |
QVERIFY(!QFile::exists(fileName)); |
|
257 |
||
258 |
} |
|
259 |
||
260 |
void tst_QTemporaryFile::write() |
|
261 |
{ |
|
262 |
QByteArray data("OLE\nOLE\nOLE"); |
|
263 |
QTemporaryFile file; |
|
264 |
QVERIFY(file.open()); |
|
265 |
QCOMPARE((int)file.write(data), data.size()); |
|
266 |
file.reset(); |
|
267 |
QFile compare(file.fileName()); |
|
268 |
compare.open(QIODevice::ReadOnly); |
|
269 |
QCOMPARE(compare.readAll() , data); |
|
270 |
file.close(); |
|
271 |
} |
|
272 |
||
273 |
void tst_QTemporaryFile::openCloseOpenClose() |
|
274 |
{ |
|
275 |
QString fileName; |
|
276 |
{ |
|
277 |
// Create a temp file |
|
278 |
QTemporaryFile file("tempXXXXXX"); |
|
279 |
file.setAutoRemove(true); |
|
280 |
QVERIFY(file.open()); |
|
281 |
file.write("OLE"); |
|
282 |
fileName = file.fileName(); |
|
283 |
QVERIFY(QFile::exists(fileName)); |
|
284 |
file.close(); |
|
285 |
||
286 |
// Check that it still exists after being closed |
|
287 |
QVERIFY(QFile::exists(fileName)); |
|
288 |
QVERIFY(!file.isOpen()); |
|
289 |
QVERIFY(file.open()); |
|
290 |
QCOMPARE(file.readAll(), QByteArray("OLE")); |
|
291 |
// Check that it's still the same file after being opened again. |
|
292 |
QCOMPARE(file.fileName(), fileName); |
|
293 |
} |
|
294 |
QVERIFY(!QFile::exists(fileName)); |
|
295 |
} |
|
296 |
||
297 |
void tst_QTemporaryFile::size() |
|
298 |
{ |
|
299 |
QTemporaryFile file; |
|
300 |
QVERIFY(file.open()); |
|
301 |
QVERIFY(file.exists()); |
|
302 |
QVERIFY(!file.isSequential()); |
|
303 |
QByteArray str("foobar"); |
|
304 |
file.write(str); |
|
305 |
QVERIFY(QFile::exists(file.fileName())); |
|
306 |
// On CE it takes more time for the filesystem to update |
|
307 |
// the information. Usually you have to close it or seek |
|
308 |
// to get latest information. flush() does not help either. |
|
309 |
#if !defined(Q_OS_WINCE) |
|
310 |
QCOMPARE(file.size(), qint64(6)); |
|
311 |
#endif |
|
312 |
file.seek(0); |
|
313 |
QCOMPARE(file.size(), qint64(6)); |
|
314 |
} |
|
315 |
||
316 |
void tst_QTemporaryFile::resize() |
|
317 |
{ |
|
318 |
QTemporaryFile file; |
|
319 |
file.setAutoRemove(true); |
|
320 |
QVERIFY(file.open()); |
|
321 |
QVERIFY(file.resize(100)); |
|
322 |
||
323 |
QCOMPARE(QFileInfo(file.fileName()).size(), qint64(100)); |
|
324 |
||
325 |
file.close(); |
|
326 |
} |
|
327 |
||
328 |
void tst_QTemporaryFile::openOnRootDrives() |
|
329 |
{ |
|
330 |
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) |
|
331 |
unsigned int lastErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS); |
|
332 |
#endif |
|
333 |
// If it's possible to create a file in the root directory, it |
|
334 |
// must be possible to create a temp file there too. |
|
335 |
foreach (QFileInfo driveInfo, QDir::drives()) { |
|
336 |
QFile testFile(driveInfo.filePath() + "XXXXXX.txt"); |
|
337 |
if (testFile.open(QIODevice::ReadWrite)) { |
|
338 |
testFile.remove(); |
|
339 |
QTemporaryFile file(driveInfo.filePath() + "XXXXXX.txt"); |
|
340 |
file.setAutoRemove(true); |
|
341 |
QVERIFY(file.open()); |
|
342 |
} |
|
343 |
} |
|
344 |
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) |
|
345 |
SetErrorMode(lastErrorMode); |
|
346 |
#endif |
|
347 |
} |
|
348 |
||
349 |
void tst_QTemporaryFile::stressTest() |
|
350 |
{ |
|
351 |
#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
|
352 |
// 200 is still ok, first colision happens after ~30 |
|
353 |
const int iterations = 200; |
|
354 |
#else |
|
355 |
const int iterations = 1000; |
|
356 |
#endif |
|
357 |
||
358 |
QSet<QString> names; |
|
359 |
for (int i = 0; i < iterations; ++i) { |
|
360 |
QTemporaryFile file; |
|
361 |
file.setAutoRemove(false); |
|
362 |
if (!file.open()) { |
|
363 |
qDebug() << "Could not open File:" << file.fileName(); |
|
364 |
continue; |
|
365 |
} |
|
366 |
QVERIFY(!names.contains(file.fileName())); |
|
367 |
names.insert(file.fileName()); |
|
368 |
} |
|
369 |
for (QSet<QString>::const_iterator it = names.constBegin(); it != names.constEnd(); ++it) { |
|
370 |
QFile::remove(*it); |
|
371 |
} |
|
372 |
} |
|
373 |
||
374 |
void tst_QTemporaryFile::rename() |
|
375 |
{ |
|
376 |
// This test checks that the temporary file is deleted, even after a |
|
377 |
// rename. |
|
378 |
||
379 |
QDir dir; |
|
380 |
QVERIFY(!dir.exists("temporary-file.txt")); |
|
381 |
||
382 |
QString tempname; |
|
383 |
{ |
|
384 |
QTemporaryFile file(dir.filePath("temporary-file.XXXXXX")); |
|
385 |
||
386 |
QVERIFY(file.open()); |
|
387 |
tempname = file.fileName(); |
|
388 |
QVERIFY(dir.exists(tempname)); |
|
389 |
||
390 |
QVERIFY(file.rename("temporary-file.txt")); |
|
391 |
QVERIFY(!dir.exists(tempname)); |
|
392 |
QVERIFY(dir.exists("temporary-file.txt")); |
|
393 |
QCOMPARE(file.fileName(), QString("temporary-file.txt")); |
|
394 |
} |
|
395 |
||
396 |
QVERIFY(!dir.exists(tempname)); |
|
397 |
QVERIFY(!dir.exists("temporary-file.txt")); |
|
398 |
} |
|
399 |
||
400 |
void tst_QTemporaryFile::renameFdLeak() |
|
401 |
{ |
|
402 |
#ifdef Q_OS_UNIX |
|
403 |
// Test this on Unix only |
|
404 |
||
405 |
// Open a bunch of files to force the fd count to go up |
|
406 |
static const int count = 10; |
|
407 |
int bunch_of_files[count]; |
|
408 |
for (int i = 0; i < count; ++i) { |
|
409 |
bunch_of_files[i] = ::open(SRCDIR "tst_qtemporaryfile.cpp", O_RDONLY); |
|
410 |
QVERIFY(bunch_of_files[i] != -1); |
|
411 |
} |
|
412 |
||
413 |
int fd; |
|
414 |
{ |
|
415 |
QTemporaryFile file; |
|
416 |
file.setAutoRemove(false); |
|
417 |
QVERIFY(file.open()); |
|
418 |
||
419 |
// close the bunch of files |
|
420 |
for (int i = 0; i < count; ++i) |
|
421 |
::close(bunch_of_files[i]); |
|
422 |
||
423 |
// save the file descriptor for later |
|
424 |
fd = file.handle(); |
|
425 |
||
426 |
// rename the file to something |
|
427 |
QString newPath = QDir::tempPath() + "/tst_qtemporaryfile-renameFdLeak-" + QString::number(getpid()); |
|
428 |
file.rename(newPath); |
|
429 |
QFile::remove(newPath); |
|
430 |
} |
|
431 |
||
432 |
// check if QTemporaryFile closed the file |
|
433 |
QVERIFY(::close(fd) == -1 && errno == EBADF); |
|
434 |
#endif |
|
435 |
} |
|
436 |
||
437 |
void tst_QTemporaryFile::reOpenThroughQFile() |
|
438 |
{ |
|
439 |
QByteArray data("abcdefghij"); |
|
440 |
||
441 |
QTemporaryFile file; |
|
442 |
QVERIFY(((QFile &)file).open(QIODevice::WriteOnly)); |
|
443 |
QCOMPARE(file.write(data), (qint64)data.size()); |
|
444 |
||
445 |
file.close(); |
|
446 |
QVERIFY(file.open()); |
|
447 |
QCOMPARE(file.readAll(), data); |
|
448 |
} |
|
449 |
||
450 |
void tst_QTemporaryFile::keepOpenMode() |
|
451 |
{ |
|
452 |
QByteArray data("abcdefghij"); |
|
453 |
||
454 |
{ |
|
455 |
QTemporaryFile file; |
|
456 |
QVERIFY(((QFile &)file).open(QIODevice::WriteOnly)); |
|
457 |
QVERIFY(QIODevice::WriteOnly == file.openMode()); |
|
458 |
||
459 |
QCOMPARE(file.write(data), (qint64)data.size()); |
|
460 |
file.close(); |
|
461 |
||
462 |
QVERIFY(((QFile &)file).open(QIODevice::ReadOnly)); |
|
463 |
QVERIFY(QIODevice::ReadOnly == file.openMode()); |
|
464 |
QCOMPARE(file.readAll(), data); |
|
465 |
} |
|
466 |
||
467 |
{ |
|
468 |
QTemporaryFile file; |
|
469 |
QVERIFY(file.open()); |
|
470 |
QCOMPARE(file.write(data), (qint64)data.size()); |
|
471 |
QVERIFY(file.rename("temporary-file.txt")); |
|
472 |
||
473 |
QVERIFY(((QFile &)file).open(QIODevice::ReadOnly)); |
|
474 |
QVERIFY(QIODevice::ReadOnly == file.openMode()); |
|
475 |
QCOMPARE(file.readAll(), data); |
|
476 |
||
477 |
QVERIFY(((QFile &)file).open(QIODevice::WriteOnly)); |
|
478 |
QVERIFY(QIODevice::WriteOnly == file.openMode()); |
|
479 |
} |
|
480 |
} |
|
481 |
||
482 |
void tst_QTemporaryFile::resetTemplateAfterError() |
|
483 |
{ |
|
484 |
// calling setFileTemplate on a failed open |
|
485 |
||
486 |
QString tempPath = QDir::tempPath(); |
|
487 |
||
488 |
QString const fileTemplate("destination/qt_temp_file_test.XXXXXX"); |
|
489 |
QString const fileTemplate2(tempPath + "/qt_temp_file_test.XXXXXX"); |
|
490 |
||
491 |
QVERIFY2( QDir(tempPath).exists() || QDir().mkpath(tempPath), "Test precondition" ); |
|
492 |
QVERIFY2( !QFile::exists("destination"), "Test precondition" ); |
|
493 |
QVERIFY2( !QFile::exists(fileTemplate2) || QFile::remove(fileTemplate2), "Test precondition" ); |
|
494 |
||
495 |
QFile file(fileTemplate2); |
|
496 |
QByteArray fileContent("This file is intentionally NOT left empty."); |
|
497 |
||
498 |
QVERIFY( file.open(QIODevice::ReadWrite | QIODevice::Truncate) ); |
|
499 |
QCOMPARE( file.write(fileContent), (qint64)fileContent.size() ); |
|
500 |
QVERIFY( file.flush() ); |
|
501 |
||
502 |
QString fileName; |
|
503 |
{ |
|
504 |
QTemporaryFile temp; |
|
505 |
||
506 |
QVERIFY( temp.fileName().isEmpty() ); |
|
507 |
QVERIFY( !temp.fileTemplate().isEmpty() ); |
|
508 |
||
509 |
temp.setFileTemplate( fileTemplate ); |
|
510 |
||
511 |
QVERIFY( temp.fileName().isEmpty() ); |
|
512 |
QCOMPARE( temp.fileTemplate(), fileTemplate ); |
|
513 |
||
514 |
QVERIFY( !temp.open() ); |
|
515 |
||
516 |
QVERIFY( temp.fileName().isEmpty() ); |
|
517 |
QCOMPARE( temp.fileTemplate(), fileTemplate ); |
|
518 |
||
519 |
temp.setFileTemplate( fileTemplate2 ); |
|
520 |
QVERIFY( temp.open() ); |
|
521 |
||
522 |
fileName = temp.fileName(); |
|
523 |
QVERIFY( QFile::exists(fileName) ); |
|
524 |
QVERIFY( !fileName.isEmpty() ); |
|
525 |
QVERIFY2( fileName != fileTemplate2, |
|
526 |
("Generated name shouldn't be same as template: " + fileTemplate2).toLocal8Bit().constData() ); |
|
527 |
} |
|
528 |
||
529 |
QVERIFY( !QFile::exists(fileName) ); |
|
530 |
||
531 |
file.seek(0); |
|
532 |
QCOMPARE( QString(file.readAll()), QString(fileContent) ); |
|
533 |
QVERIFY( file.remove() ); |
|
534 |
} |
|
535 |
||
536 |
void tst_QTemporaryFile::setTemplateAfterOpen() |
|
537 |
{ |
|
538 |
QTemporaryFile temp; |
|
539 |
||
540 |
QVERIFY( temp.fileName().isEmpty() ); |
|
541 |
QVERIFY( !temp.fileTemplate().isEmpty() ); |
|
542 |
||
543 |
QVERIFY( temp.open() ); |
|
544 |
||
545 |
QString const fileName = temp.fileName(); |
|
546 |
QString const newTemplate("funny-path/funny-name-XXXXXX.tmp"); |
|
547 |
||
548 |
QVERIFY( !fileName.isEmpty() ); |
|
549 |
QVERIFY( QFile::exists(fileName) ); |
|
550 |
QVERIFY( !temp.fileTemplate().isEmpty() ); |
|
551 |
QVERIFY( temp.fileTemplate() != newTemplate ); |
|
552 |
||
553 |
temp.close(); // QTemporaryFile::setFileTemplate will assert on isOpen() up to 4.5.2 |
|
554 |
temp.setFileTemplate(newTemplate); |
|
555 |
QCOMPARE( temp.fileTemplate(), newTemplate ); |
|
556 |
||
557 |
QVERIFY( temp.open() ); |
|
558 |
QCOMPARE( temp.fileName(), fileName ); |
|
559 |
QCOMPARE( temp.fileTemplate(), newTemplate ); |
|
560 |
} |
|
561 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
562 |
void tst_QTemporaryFile::autoRemoveAfterFailedRename() |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
563 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
564 |
struct CleanOnReturn |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
565 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
566 |
~CleanOnReturn() |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
567 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
568 |
if (!tempName.isEmpty()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
569 |
QFile::remove(tempName); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
570 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
571 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
572 |
void reset() |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
573 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
574 |
tempName.clear(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
575 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
576 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
577 |
QString tempName; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
578 |
}; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
579 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
580 |
CleanOnReturn cleaner; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
581 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
582 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
583 |
QTemporaryFile file; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
584 |
QVERIFY( file.open() ); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
585 |
cleaner.tempName = file.fileName(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
586 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
587 |
QVERIFY( QFile::exists(cleaner.tempName) ); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
588 |
QVERIFY( !QFileInfo("i-do-not-exist").isDir() ); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
589 |
QVERIFY( !file.rename("i-do-not-exist/file.txt") ); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
590 |
QVERIFY( QFile::exists(cleaner.tempName) ); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
591 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
592 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
593 |
QVERIFY( !QFile::exists(cleaner.tempName) ); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
594 |
cleaner.reset(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
595 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
596 |
|
0 | 597 |
QTEST_MAIN(tst_QTemporaryFile) |
598 |
#include "tst_qtemporaryfile.moc" |