| 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 <QtTest/QtTest> |
| 31 | #include <QtWidgets/private/qsidebar_p.h> |
| 32 | #include <QtWidgets/private/qfilesystemmodel_p.h> |
| 33 | |
| 34 | class : public QObject { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | private slots: |
| 38 | void setUrls(); |
| 39 | void selectUrls(); |
| 40 | void addUrls(); |
| 41 | |
| 42 | void goToUrl(); |
| 43 | }; |
| 44 | |
| 45 | void tst_QSidebar::() |
| 46 | { |
| 47 | QList<QUrl> urls; |
| 48 | QFileSystemModel fsmodel; |
| 49 | QSidebar ; |
| 50 | qsidebar.setModelAndUrls(model: &fsmodel, newUrls: urls); |
| 51 | QAbstractItemModel *model = qsidebar.model(); |
| 52 | |
| 53 | urls << QUrl::fromLocalFile(localfile: QDir::rootPath()) |
| 54 | << QUrl::fromLocalFile(localfile: QDir::temp().absolutePath()); |
| 55 | |
| 56 | QCOMPARE(model->rowCount(), 0); |
| 57 | qsidebar.setUrls(urls); |
| 58 | #ifdef Q_OS_WINRT |
| 59 | QEXPECT_FAIL("" , "One of the URLs is not seen as valid on WinRT - QTBUG-68297" , Abort); |
| 60 | #endif |
| 61 | QCOMPARE(qsidebar.urls(), urls); |
| 62 | QCOMPARE(model->rowCount(), urls.count()); |
| 63 | qsidebar.setUrls(urls); |
| 64 | QCOMPARE(model->rowCount(), urls.count()); |
| 65 | } |
| 66 | |
| 67 | void tst_QSidebar::() |
| 68 | { |
| 69 | QList<QUrl> urls; |
| 70 | urls << QUrl::fromLocalFile(localfile: QDir::rootPath()) |
| 71 | << QUrl::fromLocalFile(localfile: QDir::temp().absolutePath()); |
| 72 | QFileSystemModel fsmodel; |
| 73 | QSidebar ; |
| 74 | qsidebar.setModelAndUrls(model: &fsmodel, newUrls: urls); |
| 75 | |
| 76 | QSignalSpy spy(&qsidebar, SIGNAL(goToUrl(QUrl))); |
| 77 | qsidebar.selectUrl(url: urls.at(i: 0)); |
| 78 | QCOMPARE(spy.count(), 0); |
| 79 | } |
| 80 | |
| 81 | void tst_QSidebar::() |
| 82 | { |
| 83 | QList<QUrl> emptyUrls; |
| 84 | QFileSystemModel fsmodel; |
| 85 | QSidebar ; |
| 86 | qsidebar.setModelAndUrls(model: &fsmodel, newUrls: emptyUrls); |
| 87 | QAbstractItemModel *model = qsidebar.model(); |
| 88 | QDir testDir = QDir::home(); |
| 89 | |
| 90 | #if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED) |
| 91 | // temp and home is the same directory on Android |
| 92 | testDir.mkdir(QStringLiteral("test" )); |
| 93 | QVERIFY(testDir.cd(QStringLiteral("test" ))); |
| 94 | #endif |
| 95 | |
| 96 | // default |
| 97 | QCOMPARE(model->rowCount(), 0); |
| 98 | |
| 99 | QList<QUrl> urls; |
| 100 | urls << QUrl::fromLocalFile(localfile: QDir::rootPath()) |
| 101 | << QUrl::fromLocalFile(localfile: QDir::temp().absolutePath()); |
| 102 | |
| 103 | // test < 0 |
| 104 | qsidebar.addUrls(list: urls, row: -1); |
| 105 | #ifdef Q_OS_WINRT |
| 106 | QEXPECT_FAIL("" , "One of the URLs is not seen as valid on WinRT - QTBUG-68297" , Abort); |
| 107 | #endif |
| 108 | QCOMPARE(model->rowCount(), 2); |
| 109 | |
| 110 | // test = 0 |
| 111 | qsidebar.setUrls(emptyUrls); |
| 112 | qsidebar.addUrls(list: urls, row: 0); |
| 113 | QCOMPARE(model->rowCount(), 2); |
| 114 | |
| 115 | // test > 0 |
| 116 | qsidebar.setUrls(emptyUrls); |
| 117 | qsidebar.addUrls(list: urls, row: 100); |
| 118 | QCOMPARE(model->rowCount(), 2); |
| 119 | |
| 120 | // test inserting with already existing rows |
| 121 | QList<QUrl> moreUrls; |
| 122 | moreUrls << QUrl::fromLocalFile(localfile: testDir.absolutePath()); |
| 123 | qsidebar.addUrls(list: moreUrls, row: -1); |
| 124 | QCOMPARE(model->rowCount(), 3); |
| 125 | |
| 126 | // make sure invalid urls are still added |
| 127 | QList<QUrl> badUrls; |
| 128 | badUrls << QUrl::fromLocalFile(localfile: testDir.absolutePath() + "/I used to exist" ); |
| 129 | qsidebar.addUrls(list: badUrls, row: 0); |
| 130 | QCOMPARE(model->rowCount(), 4); |
| 131 | |
| 132 | // check that every item has text and an icon including the above invalid one |
| 133 | for (int i = 0; i < model->rowCount(); ++i) { |
| 134 | QVERIFY(!model->index(i, 0).data().toString().isEmpty()); |
| 135 | QIcon icon = qvariant_cast<QIcon>(v: model->index(row: i, column: 0).data(arole: Qt::DecorationRole)); |
| 136 | QVERIFY(!icon.isNull()); |
| 137 | } |
| 138 | |
| 139 | // test moving up the list |
| 140 | qsidebar.setUrls(emptyUrls); |
| 141 | qsidebar.addUrls(list: urls, row: 100); |
| 142 | qsidebar.addUrls(list: moreUrls, row: 100); |
| 143 | QCOMPARE(model->rowCount(), 3); |
| 144 | qsidebar.addUrls(list: moreUrls, row: 1); |
| 145 | QCOMPARE(qsidebar.urls()[1], moreUrls[0]); |
| 146 | |
| 147 | // test appending with -1 |
| 148 | qsidebar.setUrls(emptyUrls); |
| 149 | qsidebar.addUrls(list: urls, row: -1); |
| 150 | qsidebar.addUrls(list: moreUrls, row: -1); |
| 151 | QCOMPARE(qsidebar.urls()[0], urls[0]); |
| 152 | |
| 153 | QList<QUrl> doubleUrls; |
| 154 | //tow exact same paths, we have only one entry |
| 155 | doubleUrls << QUrl::fromLocalFile(localfile: testDir.absolutePath()); |
| 156 | doubleUrls << QUrl::fromLocalFile(localfile: testDir.absolutePath()); |
| 157 | qsidebar.setUrls(emptyUrls); |
| 158 | qsidebar.addUrls(list: doubleUrls, row: 1); |
| 159 | QCOMPARE(qsidebar.urls().size(), 1); |
| 160 | |
| 161 | // Two paths that are effectively pointing to the same location |
| 162 | doubleUrls << QUrl::fromLocalFile(localfile: testDir.absolutePath()); |
| 163 | doubleUrls << QUrl::fromLocalFile(localfile: testDir.absolutePath() + "/." ); |
| 164 | qsidebar.setUrls(emptyUrls); |
| 165 | qsidebar.addUrls(list: doubleUrls, row: 1); |
| 166 | QCOMPARE(qsidebar.urls().size(), 1); |
| 167 | |
| 168 | doubleUrls << QUrl::fromLocalFile(localfile: testDir.absolutePath()); |
| 169 | doubleUrls << QUrl::fromLocalFile(localfile: testDir.absolutePath().toUpper()); |
| 170 | qsidebar.setUrls(emptyUrls); |
| 171 | qsidebar.addUrls(list: doubleUrls, row: 1); |
| 172 | |
| 173 | #ifdef Q_OS_WIN |
| 174 | //Windows is case insensitive so no duplicate entries in that case |
| 175 | QCOMPARE(qsidebar.urls().size(), 1); |
| 176 | #else |
| 177 | //Two different paths we should have two entries |
| 178 | QCOMPARE(qsidebar.urls().size(), 2); |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | void tst_QSidebar::() |
| 183 | { |
| 184 | QList<QUrl> urls; |
| 185 | urls << QUrl::fromLocalFile(localfile: QDir::rootPath()) |
| 186 | << QUrl::fromLocalFile(localfile: QDir::temp().absolutePath()); |
| 187 | QFileSystemModel fsmodel; |
| 188 | QSidebar ; |
| 189 | qsidebar.setModelAndUrls(model: &fsmodel, newUrls: urls); |
| 190 | qsidebar.show(); |
| 191 | |
| 192 | QSignalSpy spy(&qsidebar, SIGNAL(goToUrl(QUrl))); |
| 193 | QTest::mousePress(widget: qsidebar.viewport(), button: Qt::LeftButton, stateKey: {}, |
| 194 | pos: qsidebar.visualRect(index: qsidebar.model()->index(row: 0, column: 0)).center()); |
| 195 | #ifdef Q_OS_WINRT |
| 196 | QEXPECT_FAIL("" , "Fails on WinRT - QTBUG-68297" , Abort); |
| 197 | #endif |
| 198 | QCOMPARE(spy.count(), 1); |
| 199 | QCOMPARE((spy.value(0)).at(0).toUrl(), urls.first()); |
| 200 | } |
| 201 | |
| 202 | QTEST_MAIN(tst_QSidebar) |
| 203 | #include "tst_qsidebar.moc" |
| 204 | |
| 205 | |