2015-01-16 13:55:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-01-16 13:55:05 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:55:33 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-01-16 13:55:05 +01:00
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-01-16 13:55:05 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <utils/treemodel.h>
|
|
|
|
|
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
2016-06-24 09:36:42 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
2015-01-16 13:55:05 +01:00
|
|
|
//TESTED_COMPONENT=src/libs/utils/treemodel
|
|
|
|
|
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
|
|
class tst_TreeModel : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
private slots:
|
2016-06-24 09:36:42 +02:00
|
|
|
void testTypes();
|
2015-01-16 13:55:05 +01:00
|
|
|
void testIteration();
|
2016-05-27 11:12:03 +02:00
|
|
|
void testMixed();
|
2015-01-16 13:55:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int countLevelItems(TreeItem *base, int level)
|
|
|
|
|
{
|
|
|
|
|
int n = 0;
|
2016-05-27 11:12:03 +02:00
|
|
|
int bl = base->level();
|
2016-07-25 16:48:15 +02:00
|
|
|
base->forAllChildren([level, bl, &n](TreeItem *item) {
|
2016-05-27 11:12:03 +02:00
|
|
|
if (item->level() == bl + level)
|
|
|
|
|
++n;
|
|
|
|
|
});
|
2015-01-16 13:55:05 +01:00
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-10 10:37:49 +02:00
|
|
|
static TreeItem *createItem(const QString &name)
|
2015-01-16 13:55:05 +01:00
|
|
|
{
|
2016-06-17 08:24:47 +02:00
|
|
|
return new StaticTreeItem(name);
|
2015-01-16 13:55:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_TreeModel::testIteration()
|
|
|
|
|
{
|
2016-06-24 09:36:42 +02:00
|
|
|
TreeModel<> m;
|
2015-01-16 13:55:05 +01:00
|
|
|
TreeItem *r = m.rootItem();
|
|
|
|
|
TreeItem *group0 = createItem("group0");
|
|
|
|
|
TreeItem *group1 = createItem("group1");
|
|
|
|
|
TreeItem *item10 = createItem("item10");
|
|
|
|
|
TreeItem *item11 = createItem("item11");
|
|
|
|
|
TreeItem *item12 = createItem("item12");
|
|
|
|
|
group1->appendChild(item10);
|
|
|
|
|
group1->appendChild(item11);
|
|
|
|
|
TreeItem *group2 = createItem("group2");
|
|
|
|
|
TreeItem *item20 = createItem("item20");
|
|
|
|
|
TreeItem *item21 = createItem("item21");
|
|
|
|
|
TreeItem *item22 = createItem("item22");
|
|
|
|
|
r->appendChild(group0);
|
|
|
|
|
r->appendChild(group1);
|
|
|
|
|
r->appendChild(group2);
|
|
|
|
|
group1->appendChild(item12);
|
|
|
|
|
group2->appendChild(item20);
|
|
|
|
|
group2->appendChild(item21);
|
|
|
|
|
group2->appendChild(item22);
|
|
|
|
|
|
2016-07-06 13:38:00 +02:00
|
|
|
QCOMPARE(r->childCount(), 3);
|
2015-01-16 13:55:05 +01:00
|
|
|
QCOMPARE(countLevelItems(r, 1), 3);
|
|
|
|
|
QCOMPARE(countLevelItems(r, 2), 6);
|
|
|
|
|
QCOMPARE(countLevelItems(r, 3), 0);
|
|
|
|
|
QCOMPARE(countLevelItems(group0, 1), 0);
|
|
|
|
|
QCOMPARE(countLevelItems(group1, 1), 3);
|
|
|
|
|
QCOMPARE(countLevelItems(group1, 2), 0);
|
|
|
|
|
QCOMPARE(countLevelItems(group2, 1), 3);
|
|
|
|
|
QCOMPARE(countLevelItems(group2, 2), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-27 11:12:03 +02:00
|
|
|
struct ItemA : public TreeItem {};
|
|
|
|
|
struct ItemB : public TreeItem {};
|
|
|
|
|
|
|
|
|
|
void tst_TreeModel::testMixed()
|
|
|
|
|
{
|
2016-06-24 09:36:42 +02:00
|
|
|
TreeModel<TreeItem, ItemA, ItemB> m;
|
2016-05-27 11:12:03 +02:00
|
|
|
TreeItem *r = m.rootItem();
|
|
|
|
|
TreeItem *ra;
|
|
|
|
|
r->appendChild(new ItemA);
|
|
|
|
|
r->appendChild(ra = new ItemA);
|
|
|
|
|
ra->appendChild(new ItemB);
|
|
|
|
|
ra->appendChild(new ItemB);
|
|
|
|
|
|
|
|
|
|
int n = 0;
|
2016-06-24 09:36:42 +02:00
|
|
|
m.forItemsAtLevel<1>([&n](ItemA *) { ++n; });
|
2016-05-27 11:12:03 +02:00
|
|
|
QCOMPARE(n, 2);
|
|
|
|
|
|
|
|
|
|
n = 0;
|
2016-06-24 09:36:42 +02:00
|
|
|
m.forItemsAtLevel<2>([&n](ItemB *) { ++n; });
|
2016-05-27 11:12:03 +02:00
|
|
|
QCOMPARE(n, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 09:36:42 +02:00
|
|
|
void tst_TreeModel::testTypes()
|
|
|
|
|
{
|
|
|
|
|
struct A {};
|
|
|
|
|
struct B {};
|
|
|
|
|
struct C {};
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same<Internal::SelectType<0, A>::Type, A>::value, "");
|
|
|
|
|
static_assert(std::is_same<Internal::SelectType<0>::Type, TreeItem>::value, "");
|
|
|
|
|
static_assert(std::is_same<Internal::SelectType<1>::Type, TreeItem>::value, "");
|
|
|
|
|
static_assert(std::is_same<Internal::SelectType<0, A, B, C>::Type, A>::value, "");
|
|
|
|
|
static_assert(std::is_same<Internal::SelectType<1, A, B, C>::Type, B>::value, "");
|
|
|
|
|
static_assert(std::is_same<Internal::SelectType<2, A, B, C>::Type, C>::value, "");
|
|
|
|
|
static_assert(std::is_same<Internal::SelectType<3, A, B, C>::Type, TreeItem>::value, "");
|
|
|
|
|
}
|
2015-01-16 13:55:05 +01:00
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_TreeModel)
|
|
|
|
|
|
|
|
|
|
#include "tst_treemodel.moc"
|