1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QFileIconProvider>
31#include <QFileInfo>
32#include <QTest>
33
34class tst_QFileIconProvider : public QObject
35{
36 Q_OBJECT
37
38private slots:
39 void qfileiconprovider();
40
41 void iconType_data();
42 void iconType();
43
44 void iconInfo_data();
45 void iconInfo();
46
47 void type_data();
48 void type();
49
50 void taskQTBUG_46755_QFileIconEngine_crash();
51};
52
53void tst_QFileIconProvider::qfileiconprovider()
54{
55 // don't crash
56 QFileIconProvider provider;
57}
58
59Q_DECLARE_METATYPE(QFileIconProvider::IconType)
60void tst_QFileIconProvider::iconType_data()
61{
62 QTest::addColumn<QFileIconProvider::IconType>(name: "type");
63
64 QTest::newRow(dataTag: "computer") << QFileIconProvider::Computer;
65 QTest::newRow(dataTag: "desktop") << QFileIconProvider::Desktop;
66 QTest::newRow(dataTag: "trashcan") << QFileIconProvider::Trashcan;
67 QTest::newRow(dataTag: "network") << QFileIconProvider::Network;
68 QTest::newRow(dataTag: "drive") << QFileIconProvider::Drive;
69 QTest::newRow(dataTag: "folder") << QFileIconProvider::Folder;
70 QTest::newRow(dataTag: "file") << QFileIconProvider::File;
71}
72
73// public QIcon icon(QFileIconProvider::IconType const& type) const
74void tst_QFileIconProvider::iconType()
75{
76 QFETCH(QFileIconProvider::IconType, type);
77 QFileIconProvider provider;
78 QVERIFY(!provider.icon(type).isNull());
79}
80
81void tst_QFileIconProvider::iconInfo_data()
82{
83 QTest::addColumn<QFileInfo>(name: "info");
84 QTest::addColumn<bool>(name: "setPath");
85
86 QTest::newRow(dataTag: "null") << QFileInfo() << false;
87 QTest::newRow(dataTag: "drive") << QFileInfo(QDir::rootPath()) << true;
88 QTest::newRow(dataTag: "home") << QFileInfo(QDir::homePath()) << true;
89 QTest::newRow(dataTag: "current") << QFileInfo(QDir::currentPath()) << true;
90}
91
92// public QIcon icon(QFileInfo const& info) const
93void tst_QFileIconProvider::iconInfo()
94{
95 QFETCH(QFileInfo, info);
96 QFETCH(bool, setPath);
97
98 if (setPath)
99 QVERIFY(info.exists());
100 QFileIconProvider provider;
101 // we should always get an icon
102 QVERIFY(!provider.icon(info).isNull());
103}
104
105void tst_QFileIconProvider::type_data()
106{
107 QTest::addColumn<QFileInfo>(name: "info");
108 // Return value is _very_ system dependent, hard to test
109 // QTest::addColumn<QString>("type");
110
111 QTest::newRow(dataTag: "null") << QFileInfo();
112 QTest::newRow(dataTag: "drive") << QFileInfo(QDir::rootPath());
113 QTest::newRow(dataTag: "home") << QFileInfo(QDir::homePath());
114 QTest::newRow(dataTag: "current") << QFileInfo(QDir::currentPath());
115 QTest::newRow(dataTag: "exe") << QFileInfo(QCoreApplication::applicationFilePath());
116}
117
118// public QString type(QFileInfo const& info) const
119void tst_QFileIconProvider::type()
120{
121 QFETCH(QFileInfo, info);
122 QFileIconProvider provider;
123 QVERIFY(!provider.type(info).isEmpty());
124}
125
126static QIcon getIcon()
127{
128 QFileIconProvider fip;
129 return fip.icon(info: QDir::currentPath());
130}
131
132void tst_QFileIconProvider::taskQTBUG_46755_QFileIconEngine_crash()
133{
134 const QIcon &icon = getIcon();
135 const auto sizes = icon.availableSizes();
136 for (const QSize &size : sizes)
137 icon.pixmap(size);
138
139 // No crash, all good.
140}
141
142QTEST_MAIN(tst_QFileIconProvider)
143#include "tst_qfileiconprovider.moc"
144
145

source code of qtbase/tests/auto/widgets/itemviews/qfileiconprovider/tst_qfileiconprovider.cpp