From 6c3c1a4cb8e4973b8e603edfbe608e1b1587f3f2 Mon Sep 17 00:00:00 2001 From: Ivan Komissarov Date: Tue, 6 May 2014 16:10:10 +0000 Subject: Add the QStorageInfo class Allows to retrieve information about mounted volumes such as label, total/available size, filesystem type and so on. Possible use cases are: - allows to do checks about filesystem before performing actual operation (such as available/maximum volume size) - allows to retrive information about volume that can be shown in file dialogs - allows to retrieve volume for specific path and check if two or more paths belong to the same volume or not [ChangeLog][QtCore] Added QStorageInfo class to retrive information about mounted volumes and drives Change-Id: Ibf9c2e6b53ef39c5605894a4422acdbbca4030c4 Reviewed-by: Shawn Rutledge --- tests/auto/corelib/io/io.pro | 1 + .../auto/corelib/io/qstorageinfo/qstorageinfo.pro | 4 + .../corelib/io/qstorageinfo/tst_qstorageinfo.cpp | 198 +++++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 tests/auto/corelib/io/qstorageinfo/qstorageinfo.pro create mode 100644 tests/auto/corelib/io/qstorageinfo/tst_qstorageinfo.cpp (limited to 'tests/auto/corelib/io') diff --git a/tests/auto/corelib/io/io.pro b/tests/auto/corelib/io/io.pro index ba055a4f3f..24592238bd 100644 --- a/tests/auto/corelib/io/io.pro +++ b/tests/auto/corelib/io/io.pro @@ -26,6 +26,7 @@ SUBDIRS=\ qsettings \ qsavefile \ qstandardpaths \ + qstorageinfo \ qtemporarydir \ qtemporaryfile \ qtextstream \ diff --git a/tests/auto/corelib/io/qstorageinfo/qstorageinfo.pro b/tests/auto/corelib/io/qstorageinfo/qstorageinfo.pro new file mode 100644 index 0000000000..de27504a9b --- /dev/null +++ b/tests/auto/corelib/io/qstorageinfo/qstorageinfo.pro @@ -0,0 +1,4 @@ +CONFIG += testcase +TARGET = tst_qstorageinfo +QT = core-private testlib +SOURCES = tst_qstorageinfo.cpp diff --git a/tests/auto/corelib/io/qstorageinfo/tst_qstorageinfo.cpp b/tests/auto/corelib/io/qstorageinfo/tst_qstorageinfo.cpp new file mode 100644 index 0000000000..cfcabed127 --- /dev/null +++ b/tests/auto/corelib/io/qstorageinfo/tst_qstorageinfo.cpp @@ -0,0 +1,198 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Ivan Komissarov +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include + +class tst_QStorageInfo : public QObject +{ + Q_OBJECT +private slots: + void defaultValues(); + void operatorEqual(); +#ifndef Q_OS_WINRT + void operatorNotEqual(); + void root(); + void currentStorage(); + void storageList(); + void tempFile(); + void caching(); +#endif +}; + +void tst_QStorageInfo::defaultValues() +{ + QStorageInfo storage; + + QVERIFY(!storage.isValid()); + QVERIFY(!storage.isReady()); + QVERIFY(storage.rootPath().isEmpty()); + QVERIFY(!storage.isRoot()); + QVERIFY(storage.device().isEmpty()); + QVERIFY(storage.fileSystemType().isEmpty()); + QVERIFY(storage.bytesTotal() == 0); + QVERIFY(storage.bytesFree() == 0); + QVERIFY(storage.bytesAvailable() == 0); +} + +void tst_QStorageInfo::operatorEqual() +{ + { + QStorageInfo storage1 = QStorageInfo::root(); + QStorageInfo storage2(QDir::rootPath()); + QVERIFY(storage1 == storage2); + } + + { + QStorageInfo storage1(QCoreApplication::applicationDirPath()); + QStorageInfo storage2(QCoreApplication::applicationFilePath()); + QVERIFY(storage1 == storage2); + } + + { + QStorageInfo storage1; + QStorageInfo storage2; + QVERIFY(storage1 == storage2); + } +} + +#ifndef Q_OS_WINRT +void tst_QStorageInfo::operatorNotEqual() +{ + QStorageInfo storage1 = QStorageInfo::root(); + QStorageInfo storage2; + QVERIFY(storage1 != storage2); +} + +void tst_QStorageInfo::root() +{ + QStorageInfo storage = QStorageInfo::root(); + + QVERIFY(storage.isValid()); + QVERIFY(storage.isReady()); + QCOMPARE(storage.rootPath(), QDir::rootPath()); + QVERIFY(storage.isRoot()); + QVERIFY(!storage.device().isEmpty()); + QVERIFY(!storage.fileSystemType().isEmpty()); + QVERIFY(storage.bytesTotal() > 0); + QVERIFY(storage.bytesFree() > 0); + QVERIFY(storage.bytesAvailable() > 0); +} + +void tst_QStorageInfo::currentStorage() +{ + QString appPath = QCoreApplication::applicationFilePath(); + QStorageInfo storage(appPath); + QVERIFY(storage.isValid()); + QVERIFY(storage.isReady()); + QVERIFY(appPath.startsWith(storage.rootPath(), Qt::CaseInsensitive)); + QVERIFY(!storage.device().isEmpty()); + QVERIFY(!storage.fileSystemType().isEmpty()); + QVERIFY(storage.bytesTotal() > 0); + QVERIFY(storage.bytesFree() > 0); + QVERIFY(storage.bytesAvailable() > 0); +} + +void tst_QStorageInfo::storageList() +{ + QStorageInfo root = QStorageInfo::root(); + + QList volumes = QStorageInfo::mountedVolumes(); + + // at least, root storage should be present + QVERIFY(volumes.contains(root)); + volumes.removeOne(root); + QVERIFY(!volumes.contains(root)); + + foreach (const QStorageInfo &storage, volumes) { + if (!storage.isReady()) + continue; + + QVERIFY(storage.isValid()); + QVERIFY(!storage.isRoot()); +#ifndef Q_OS_WIN + QVERIFY(!storage.device().isEmpty()); + QVERIFY(!storage.fileSystemType().isEmpty()); +#endif + } +} + +void tst_QStorageInfo::tempFile() +{ + QTemporaryFile file; + QVERIFY(file.open()); + + QStorageInfo storage1(file.fileName()); + qint64 free = storage1.bytesFree(); + + file.write(QByteArray(1024*1024, '1')); + file.flush(); + file.close(); + + QStorageInfo storage2(file.fileName()); + QVERIFY(free != storage2.bytesFree()); +} + +void tst_QStorageInfo::caching() +{ + QTemporaryFile file; + QVERIFY(file.open()); + + QStorageInfo storage1(file.fileName()); + qint64 free = storage1.bytesFree(); + QStorageInfo storage2(storage1); + QVERIFY(free == storage2.bytesFree()); + + file.write(QByteArray(1024*1024, '\0')); + file.flush(); + + QVERIFY(free == storage1.bytesFree()); + QVERIFY(free == storage2.bytesFree()); + storage2.refresh(); + QVERIFY(storage1 == storage2); + QVERIFY(free != storage2.bytesFree()); +} +#endif + +QTEST_MAIN(tst_QStorageInfo) + +#include "tst_qstorageinfo.moc" -- cgit v1.2.3