qt5base-lts/tests/auto/widgets/dialogs/qsidebar/tst_qsidebar.cpp
Marc Mutz 1c6bf3e09e Port from container::count() and length() to size() - V5
This is a semantic patch using ClangTidyTransformator as in
qtbase/df9d882d41b741fef7c5beeddb0abe9d904443d8, but extended to
handle typedefs and accesses through pointers, too:

    const std::string o = "object";

    auto hasTypeIgnoringPointer = [](auto type) { return anyOf(hasType(type), hasType(pointsTo(type))); };

    auto derivedFromAnyOfClasses = [&](ArrayRef<StringRef> classes) {
        auto exprOfDeclaredType = [&](auto decl) {
            return expr(hasTypeIgnoringPointer(hasUnqualifiedDesugaredType(recordType(hasDeclaration(decl))))).bind(o);
        };
        return exprOfDeclaredType(cxxRecordDecl(isSameOrDerivedFrom(hasAnyName(classes))));
    };

    auto renameMethod = [&] (ArrayRef<StringRef> classes,
                            StringRef from, StringRef to) {
        return makeRule(cxxMemberCallExpr(on(derivedFromAnyOfClasses(classes)),
                            callee(cxxMethodDecl(hasName(from), parameterCountIs(0)))),
                        changeTo(cat(access(o, cat(to)), "()")),
                        cat("use '", to, "' instead of '", from, "'"));
    };

    renameMethod(<classes>, "count", "size");
    renameMethod(<classes>, "length", "size");

except that the on() matcher has been replaced by one that doesn't
ignoreParens().

a.k.a qt-port-to-std-compatible-api V5 with config Scope: 'Container'.

Added two NOLINTNEXTLINEs in tst_qbitarray and tst_qcontiguouscache,
to avoid porting calls that explicitly test count().

Change-Id: Icfb8808c2ff4a30187e9935a51cad26987451c22
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
2022-11-03 14:59:24 +01:00

181 lines
5.3 KiB
C++

// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QSignalSpy>
#include <QtWidgets/private/qsidebar_p.h>
#include <QtGui/private/qfilesystemmodel_p.h>
#include <QtWidgets/qfileiconprovider.h>
class tst_QSidebar : public QObject {
Q_OBJECT
private slots:
void setUrls();
void selectUrls();
void addUrls();
void goToUrl();
private:
QFileIconProvider defaultIconProvider;
};
void tst_QSidebar::setUrls()
{
QList<QUrl> urls;
QFileSystemModel fsmodel;
fsmodel.setIconProvider(&defaultIconProvider);
QSidebar qsidebar;
qsidebar.setModelAndUrls(&fsmodel, urls);
QAbstractItemModel *model = qsidebar.model();
urls << QUrl::fromLocalFile(QDir::rootPath())
<< QUrl::fromLocalFile(QDir::temp().absolutePath());
QCOMPARE(model->rowCount(), 0);
qsidebar.setUrls(urls);
QCOMPARE(qsidebar.urls(), urls);
QCOMPARE(model->rowCount(), urls.size());
qsidebar.setUrls(urls);
QCOMPARE(model->rowCount(), urls.size());
}
void tst_QSidebar::selectUrls()
{
QList<QUrl> urls;
urls << QUrl::fromLocalFile(QDir::rootPath())
<< QUrl::fromLocalFile(QDir::temp().absolutePath());
QFileSystemModel fsmodel;
fsmodel.setIconProvider(&defaultIconProvider);
QSidebar qsidebar;
qsidebar.setModelAndUrls(&fsmodel, urls);
QSignalSpy spy(&qsidebar, SIGNAL(goToUrl(QUrl)));
qsidebar.selectUrl(urls.at(0));
QCOMPARE(spy.size(), 0);
}
void tst_QSidebar::addUrls()
{
QList<QUrl> emptyUrls;
QFileSystemModel fsmodel;
fsmodel.setIconProvider(&defaultIconProvider);
QSidebar qsidebar;
qsidebar.setModelAndUrls(&fsmodel, emptyUrls);
QAbstractItemModel *model = qsidebar.model();
QDir testDir = QDir::home();
#ifdef Q_OS_ANDROID
// temp and home is the same directory on Android
testDir.mkdir(QStringLiteral("test"));
QVERIFY(testDir.cd(QStringLiteral("test")));
#endif
// default
QCOMPARE(model->rowCount(), 0);
QList<QUrl> urls;
urls << QUrl::fromLocalFile(QDir::rootPath())
<< QUrl::fromLocalFile(QDir::temp().absolutePath());
// test < 0
qsidebar.addUrls(urls, -1);
QCOMPARE(model->rowCount(), 2);
// test = 0
qsidebar.setUrls(emptyUrls);
qsidebar.addUrls(urls, 0);
QCOMPARE(model->rowCount(), 2);
// test > 0
qsidebar.setUrls(emptyUrls);
qsidebar.addUrls(urls, 100);
QCOMPARE(model->rowCount(), 2);
// test inserting with already existing rows
QList<QUrl> moreUrls;
moreUrls << QUrl::fromLocalFile(testDir.absolutePath());
qsidebar.addUrls(moreUrls, -1);
QCOMPARE(model->rowCount(), 3);
// make sure invalid urls are still added
QList<QUrl> badUrls;
badUrls << QUrl::fromLocalFile(testDir.absolutePath() + "/I used to exist");
qsidebar.addUrls(badUrls, 0);
QCOMPARE(model->rowCount(), 4);
// check that every item has text and an icon including the above invalid one
for (int i = 0; i < model->rowCount(); ++i) {
QVERIFY(!model->index(i, 0).data().toString().isEmpty());
QIcon icon = qvariant_cast<QIcon>(model->index(i, 0).data(Qt::DecorationRole));
QVERIFY(!icon.isNull());
}
// test moving up the list
qsidebar.setUrls(emptyUrls);
qsidebar.addUrls(urls, 100);
qsidebar.addUrls(moreUrls, 100);
QCOMPARE(model->rowCount(), 3);
qsidebar.addUrls(moreUrls, 1);
QCOMPARE(qsidebar.urls()[1], moreUrls[0]);
// test appending with -1
qsidebar.setUrls(emptyUrls);
qsidebar.addUrls(urls, -1);
qsidebar.addUrls(moreUrls, -1);
QCOMPARE(qsidebar.urls()[0], urls[0]);
QList<QUrl> doubleUrls;
//tow exact same paths, we have only one entry
doubleUrls << QUrl::fromLocalFile(testDir.absolutePath());
doubleUrls << QUrl::fromLocalFile(testDir.absolutePath());
qsidebar.setUrls(emptyUrls);
qsidebar.addUrls(doubleUrls, 1);
QCOMPARE(qsidebar.urls().size(), 1);
// Two paths that are effectively pointing to the same location
doubleUrls << QUrl::fromLocalFile(testDir.absolutePath());
doubleUrls << QUrl::fromLocalFile(testDir.absolutePath() + "/.");
qsidebar.setUrls(emptyUrls);
qsidebar.addUrls(doubleUrls, 1);
QCOMPARE(qsidebar.urls().size(), 1);
doubleUrls << QUrl::fromLocalFile(testDir.absolutePath());
doubleUrls << QUrl::fromLocalFile(testDir.absolutePath().toUpper());
qsidebar.setUrls(emptyUrls);
qsidebar.addUrls(doubleUrls, 1);
#ifdef Q_OS_WIN
//Windows is case insensitive so no duplicate entries in that case
QCOMPARE(qsidebar.urls().size(), 1);
#else
//Two different paths we should have two entries
QCOMPARE(qsidebar.urls().size(), 2);
#endif
}
void tst_QSidebar::goToUrl()
{
QList<QUrl> urls;
urls << QUrl::fromLocalFile(QDir::rootPath())
<< QUrl::fromLocalFile(QDir::temp().absolutePath());
QFileSystemModel fsmodel;
fsmodel.setIconProvider(&defaultIconProvider);
QSidebar qsidebar;
qsidebar.setModelAndUrls(&fsmodel, urls);
qsidebar.show();
QSignalSpy spy(&qsidebar, SIGNAL(goToUrl(QUrl)));
QTest::mousePress(qsidebar.viewport(), Qt::LeftButton, {},
qsidebar.visualRect(qsidebar.model()->index(0, 0)).center());
QCOMPARE(spy.size(), 1);
QCOMPARE((spy.value(0)).at(0).toUrl(), urls.first());
}
QTEST_MAIN(tst_QSidebar)
#include "tst_qsidebar.moc"