@@ -1,6 +1,7 @@
/* ***************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2017 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
Expand Down
Expand Up
@@ -32,6 +33,7 @@
#include < qtemporarydir.h>
#include < qtemporaryfile.h>
#include < qfile.h>
#include < qdatetime.h>
#include < qdir.h>
#include < qset.h>
#include < qtextcodec.h>
Expand Down
Expand Up
@@ -63,7 +65,7 @@ private slots:
void fileNameIsEmpty ();
void autoRemove ();
void nonWritableCurrentDir ();
void write ();
void io ();
void openCloseOpenClose ();
void removeAndReOpen ();
void size ();
Expand Down
Expand Up
@@ -286,6 +288,18 @@ void tst_QTemporaryFile::autoRemove()
fileName = file.fileName ();
file.close ();
}
QVERIFY (!fileName.isEmpty ());
QVERIFY (!QFile::exists (fileName));
// same, but gets the file name after closing
{
QTemporaryFile file (" tempXXXXXX" );
file.setAutoRemove (true );
QVERIFY (file.open ());
file.close ();
fileName = file.fileName ();
}
QVERIFY (!fileName.isEmpty ());
QVERIFY (!QFile::exists (fileName));
// Test if disabling auto remove works.
Expand All
@@ -296,6 +310,19 @@ void tst_QTemporaryFile::autoRemove()
fileName = file.fileName ();
file.close ();
}
QVERIFY (!fileName.isEmpty ());
QVERIFY (QFile::exists (fileName));
QVERIFY (QFile::remove (fileName));
// same, but gets the file name after closing
{
QTemporaryFile file (" tempXXXXXX" );
file.setAutoRemove (false );
QVERIFY (file.open ());
file.close ();
fileName = file.fileName ();
}
QVERIFY (!fileName.isEmpty ());
QVERIFY (QFile::exists (fileName));
QVERIFY (QFile::remove (fileName));
Expand Down
Expand Up
@@ -346,17 +373,51 @@ void tst_QTemporaryFile::nonWritableCurrentDir()
#endif
}
void tst_QTemporaryFile::write ()
void tst_QTemporaryFile::io ()
{
QByteArray data (" OLE\n OLE\n OLE" );
QTemporaryFile file;
QDateTime before = QDateTime::currentDateTimeUtc ().addMSecs (-250 );
// discard msec component (round down) - not all FSs and OSs support them
before.setSecsSinceEpoch (before.toSecsSinceEpoch ());
QVERIFY (file.open ());
QVERIFY (file.readLink ().isEmpty ()); // it's not a link!
QFile::Permissions perm = file.permissions ();
QVERIFY (perm & QFile::ReadOwner);
QVERIFY (file.setPermissions (perm));
QCOMPARE (int (file.size ()), 0 );
QVERIFY (file.resize (data.size ()));
QCOMPARE (int (file.size ()), data.size ());
QCOMPARE ((int )file.write (data), data.size ());
QCOMPARE (int (file.size ()), data.size ());
QDateTime mtime = file.fileTime (QFile::FileModificationTime).toUTC ();
QDateTime btime = file.fileTime (QFile::FileBirthTime).toUTC ();
QDateTime ctime = file.fileTime (QFile::FileMetadataChangeTime).toUTC ();
QDateTime atime = file.fileTime (QFile::FileAccessTime).toUTC ();
QDateTime after = QDateTime::currentDateTimeUtc ().toUTC ().addMSecs (250 );
// round msecs up
after.setSecsSinceEpoch (after.toSecsSinceEpoch () + 1 );
// mtime must be valid, the rest could fail
QVERIFY (mtime <= after && mtime >= before);
QVERIFY (!btime.isValid () || (btime <= after && btime >= before));
QVERIFY (!ctime .isValid () || (ctime <= after && ctime >= before));
QVERIFY (!btime.isValid () || (btime <= after && btime >= before));
QVERIFY (file.setFileTime (before.addSecs (-10 ), QFile::FileModificationTime));
mtime = file.fileTime (QFile::FileModificationTime).toUTC ();
QCOMPARE (mtime, before.addSecs (-10 ));
file.reset ();
QFile compare (file.fileName ());
compare.open (QIODevice::ReadOnly);
QCOMPARE (compare.readAll () , data);
file. close ( );
QCOMPARE (compare. fileTime (QFile::FileModificationTime), mtime );
}
void tst_QTemporaryFile::openCloseOpenClose ()
Expand Down
Expand Up
@@ -407,17 +468,19 @@ void tst_QTemporaryFile::size()
{
QTemporaryFile file;
QVERIFY (file.open ());
QVERIFY (file.exists ());
QVERIFY (!file.isSequential ());
QByteArray str (" foobar" );
file.write (str);
QVERIFY ( QFile::exists (file. fileName ()));
// On CE it takes more time for the filesystem to update
// the information. Usually you have to close it or seek
// to get latest information. flush() does not help either.
QCOMPARE (file.size (), qint64 (6 ));
file.seek (0 );
QCOMPARE (file.size (), qint64 (6 ));
QVERIFY (QFile::exists (file.fileName ()));
QVERIFY (file.exists ());
}
void tst_QTemporaryFile::resize ()
Expand Down
Expand Up
@@ -814,6 +877,14 @@ void tst_QTemporaryFile::QTBUG_4796()
QCOMPARE (file5.open (), openResult);
QCOMPARE (file6.open (), openResult);
// force the files to exist, if they are supposed to
QCOMPARE (!file1.fileName ().isEmpty (), openResult);
QCOMPARE (!file2.fileName ().isEmpty (), openResult);
QCOMPARE (!file3.fileName ().isEmpty (), openResult);
QCOMPARE (!file4.fileName ().isEmpty (), openResult);
QCOMPARE (!file5.fileName ().isEmpty (), openResult);
QCOMPARE (!file6.fileName ().isEmpty (), openResult);
QCOMPARE (file1.exists (), openResult);
QCOMPARE (file2.exists (), openResult);
QCOMPARE (file3.exists (), openResult);
Expand Down