1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/*
modelinspectortest.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "baseprobetest.h"
#include "testhelpers.h"
#include <plugins/modelinspector/modelinspectorinterface.h>
#include <plugins/modelinspector/modelcontentproxymodel.h>
#include <ui/clienttoolmanager.h>
#include <common/objectbroker.h>
#include <common/objectmodel.h>
#include <common/objectid.h>
#include <QAbstractItemModelTester>
#include <QAbstractItemView>
#include <QItemSelectionModel>
#include <QSignalSpy>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QStringListModel>
using namespace GammaRay;
using namespace TestHelpers;
class ModelInspectorTest : public BaseProbeTest
{
Q_OBJECT
private slots:
void testModelModel()
{
createProbe();
auto targetModel = new QStandardItemModel;
targetModel->setObjectName("targetModel");
QTest::qWait(1); // trigger model inspector plugin loading
auto modelModel = ObjectBroker::model("com.kdab.GammaRay.ModelModel");
QVERIFY(modelModel);
QAbstractItemModelTester modelModelTester(modelModel);
QVERIFY(modelModel->rowCount() >= 1); // can contain the QEmptyModel instance too
int topRowCount = modelModel->rowCount();
auto targetModelIdx = searchFixedIndex(modelModel, QLatin1String("targetModel"), Qt::MatchRecursive);
QVERIFY(targetModelIdx.isValid());
QCOMPARE(targetModelIdx.data(ObjectModel::ObjectIdRole).value<ObjectId>(), ObjectId(targetModel));
QCOMPARE(targetModelIdx.sibling(targetModelIdx.row(), 1).data().toString(), QLatin1String("QStandardItemModel"));
QVERIFY(!modelModel->hasChildren(targetModelIdx));
// proxy with source
auto targetProxy = new QSortFilterProxyModel;
targetProxy->setSourceModel(targetModel);
QTest::qWait(1);
targetModelIdx = searchFixedIndex(modelModel, QLatin1String("targetModel"), Qt::MatchRecursive); // re-lookup, due to model reset
QVERIFY(targetModelIdx.isValid());
QVERIFY(modelModel->hasChildren(targetModelIdx));
QCOMPARE(modelModel->rowCount(), topRowCount);
delete targetProxy;
QTest::qWait(1);
targetModelIdx = searchFixedIndex(modelModel, QLatin1String("targetModel"), Qt::MatchRecursive); // re-lookup, due to model reset
QVERIFY(targetModelIdx.isValid());
QVERIFY(!modelModel->hasChildren(targetModelIdx));
QCOMPARE(modelModel->rowCount(), topRowCount);
// proxy with source added/reset later
targetProxy = new QSortFilterProxyModel;
targetProxy->setObjectName("targetProxy");
QTest::qWait(1);
QCOMPARE(modelModel->rowCount(), topRowCount + 1);
auto proxyIdx = searchFixedIndex(modelModel, "targetProxy", Qt::MatchRecursive);
QVERIFY(proxyIdx.isValid());
QVERIFY(!proxyIdx.parent().isValid());
targetProxy->setSourceModel(targetModel);
QCOMPARE(modelModel->rowCount(), topRowCount);
proxyIdx = searchFixedIndex(modelModel, "targetProxy", Qt::MatchRecursive);
QVERIFY(proxyIdx.isValid());
QVERIFY(proxyIdx.parent().isValid());
targetProxy->setSourceModel(nullptr);
QCOMPARE(modelModel->rowCount(), topRowCount + 1);
proxyIdx = searchFixedIndex(modelModel, "targetProxy", Qt::MatchRecursive);
QVERIFY(proxyIdx.isValid());
QVERIFY(!proxyIdx.parent().isValid());
delete targetProxy;
QTest::qWait(1);
QCOMPARE(modelModel->rowCount(), topRowCount);
// 2 element proxy chain
targetProxy = new QSortFilterProxyModel;
targetProxy->setObjectName("targetProxy");
QTest::qWait(1);
auto targetProxy2 = new QSortFilterProxyModel(targetProxy);
targetProxy2->setObjectName("targetProxy2");
QTest::qWait(1);
targetProxy2->setSourceModel(targetProxy);
targetProxy->setSourceModel(targetModel);
QCOMPARE(modelModel->rowCount(), topRowCount);
proxyIdx = searchFixedIndex(modelModel, "targetProxy2", Qt::MatchRecursive);
QVERIFY(proxyIdx.isValid());
auto idx = proxyIdx.parent();
QVERIFY(idx.isValid());
QCOMPARE(modelModel->rowCount(idx), 1);
QCOMPARE(idx.data().toString(), QLatin1String("targetProxy"));
idx = idx.parent();
QVERIFY(idx.isValid());
QVERIFY(!idx.parent().isValid());
QCOMPARE(modelModel->rowCount(idx), 1);
QCOMPARE(idx.data().toString(), QLatin1String("targetModel"));
delete targetProxy;
QTest::qWait(1);
QCOMPARE(modelModel->rowCount(), topRowCount);
// QAIM removal
delete targetModel;
QTest::qWait(1);
targetModelIdx = searchFixedIndex(modelModel, QLatin1String("targetModel"), Qt::MatchRecursive);
QVERIFY(!targetModelIdx.isValid());
QCOMPARE(modelModel->rowCount(), topRowCount - 1);
}
void testSelectionModels()
{
createProbe();
auto targetModel = new QStringListModel;
targetModel->setObjectName("targetModel");
targetModel->setStringList(QStringList() << "item1"
<< "item2"
<< "item3");
QTest::qWait(1); // trigger model inspector plugin loading
auto modelModel = ObjectBroker::model("com.kdab.GammaRay.ModelModel");
QVERIFY(modelModel);
auto selectionModels = ObjectBroker::model("com.kdab.GammaRay.SelectionModels");
QVERIFY(selectionModels);
QAbstractItemModelTester selModelTester(selectionModels);
QCOMPARE(selectionModels->rowCount(), 0);
QSignalSpy resetSpy(selectionModels, &QAbstractItemModel::modelReset);
QVERIFY(resetSpy.isValid());
auto targetSelModel = new QItemSelectionModel(targetModel);
targetSelModel->setObjectName("targetSelModel");
QTest::qWait(1);
QCOMPARE(selectionModels->rowCount(), 0);
auto modelSelModel = ObjectBroker::selectionModel(modelModel);
QVERIFY(modelSelModel);
auto idx = searchFixedIndex(modelModel, "targetModel", Qt::MatchRecursive);
QVERIFY(idx.isValid());
modelSelModel->select(idx, QItemSelectionModel::ClearAndSelect);
QCOMPARE(selectionModels->rowCount(), 1);
QSignalSpy dataChangeSpy(selectionModels, &QAbstractItemModel::dataChanged);
QVERIFY(dataChangeSpy.isValid());
QCOMPARE(selectionModels->index(0, 1).data().toInt(), 0);
QCOMPARE(selectionModels->index(0, 2).data().toInt(), 0);
QCOMPARE(selectionModels->index(0, 3).data().toInt(), 0);
targetSelModel->select(targetModel->index(1, 0), QItemSelectionModel::ClearAndSelect);
QCOMPARE(dataChangeSpy.size(), 1);
QCOMPARE(selectionModels->index(0, 1).data().toInt(), 1);
QCOMPARE(selectionModels->index(0, 2).data().toInt(), 1); // rows
QCOMPARE(selectionModels->index(0, 3).data().toInt(), 0); // cols
targetSelModel->clear();
QCOMPARE(dataChangeSpy.size(), 2);
delete targetSelModel;
QTest::qWait(1);
QCOMPARE(selectionModels->rowCount(), 0);
delete targetModel;
QTest::qWait(1);
QVERIFY(resetSpy.isEmpty());
}
void testModelContent()
{
createProbe();
auto targetModel = new QStandardItemModel;
targetModel->setObjectName("targetModel");
QTest::qWait(1); // trigger model inspector plugin loading
auto modelModel = ObjectBroker::model("com.kdab.GammaRay.ModelModel");
QVERIFY(modelModel);
auto contentModel = ObjectBroker::model("com.kdab.GammaRay.ModelContent");
QVERIFY(contentModel);
QAbstractItemModelTester contentModelTester(contentModel);
QCOMPARE(contentModel->rowCount(), 0);
auto cellModel = ObjectBroker::model("com.kdab.GammaRay.ModelCellModel");
QVERIFY(cellModel);
QAbstractItemModelTester cellModelTester(cellModel);
QCOMPARE(cellModel->rowCount(), 0);
QSignalSpy cellContentResetSpy(cellModel, &QAbstractItemModel::modelReset);
QVERIFY(cellContentResetSpy.isValid());
auto targetModelIdx = searchFixedIndex(modelModel, QLatin1String("targetModel"), Qt::MatchRecursive);
QVERIFY(targetModelIdx.isValid());
auto modelSelModel = ObjectBroker::selectionModel(modelModel);
QVERIFY(modelSelModel);
QCOMPARE(contentModel->rowCount(), 0);
auto item = new QStandardItem("item0,0");
item->setFlags(Qt::NoItemFlags); // should nevertheless be selectable for inspection
targetModel->appendRow(item);
// TODO remove this when the above select does not assert in QAbstractItemModelTester
modelSelModel->select(targetModelIdx, QItemSelectionModel::ClearAndSelect);
QCOMPARE(contentModel->rowCount(), 1);
QCOMPARE(contentModel->columnCount(), 1);
auto idx = contentModel->index(0, 0);
QVERIFY(idx.isValid());
QCOMPARE(idx.data().toString(), QLatin1String("item0,0"));
QVERIFY(idx.flags() & Qt::ItemIsEnabled);
QVERIFY(idx.flags() & Qt::ItemIsSelectable);
QCOMPARE(idx.data(ModelContentProxyModel::DisabledRole).toBool(), true);
auto cellSelModel = ObjectBroker::selectionModel(contentModel);
QVERIFY(cellSelModel);
qDebug() << "selecting" << cellSelModel->model();
cellSelModel->select(idx, QItemSelectionModel::ClearAndSelect);
QVERIFY(cellModel->rowCount() > 0);
idx = searchFixedIndex(cellModel, QLatin1String("Qt::DisplayRole"), Qt::MatchRecursive);
QVERIFY(idx.isValid());
QCOMPARE(idx.sibling(idx.row(), 1).data().toString(), QLatin1String("item0,0"));
auto iface = ObjectBroker::object<ModelInspectorInterface *>();
QVERIFY(iface);
auto cellData = iface->currentCellData();
QCOMPARE(cellData.row, 0);
QCOMPARE(cellData.column, 0);
QCOMPARE(cellData.flags, Qt::NoItemFlags);
cellSelModel->clear();
QCOMPARE(cellModel->rowCount(), 0);
QVERIFY(cellContentResetSpy.isEmpty());
delete targetModel;
QTest::qWait(1);
}
void testSelectionModelSelection()
{
createProbe();
auto targetModel = new QStringListModel;
targetModel->setObjectName("targetModel");
targetModel->setStringList(QStringList() << "item1"
<< "item2"
<< "item3");
QTest::qWait(1); // trigger model inspector plugin loading
auto modelModel = ObjectBroker::model("com.kdab.GammaRay.ModelModel");
QVERIFY(modelModel);
auto targetSelModel = new QItemSelectionModel(targetModel);
targetSelModel->setObjectName("targetSelModel");
QTest::qWait(1);
auto selectionModels = ObjectBroker::model("com.kdab.GammaRay.SelectionModels");
QVERIFY(selectionModels);
auto modelSelModel = ObjectBroker::selectionModel(modelModel);
QVERIFY(modelSelModel);
auto idx = searchFixedIndex(modelModel, "targetModel", Qt::MatchRecursive);
QVERIFY(idx.isValid());
modelSelModel->select(idx, QItemSelectionModel::ClearAndSelect);
auto selSelModel = ObjectBroker::selectionModel(selectionModels);
QVERIFY(selSelModel);
idx = searchFixedIndex(selectionModels, "targetSelModel", Qt::MatchRecursive);
QVERIFY(idx.isValid());
selSelModel->select(idx, QItemSelectionModel::ClearAndSelect);
auto contentModel = ObjectBroker::model("com.kdab.GammaRay.ModelContent");
QVERIFY(contentModel);
QCOMPARE(contentModel->rowCount(), targetModel->rowCount());
for (int i = 0; i < targetModel->rowCount(); ++i)
QVERIFY(contentModel->index(i, 0).data(ModelContentProxyModel::SelectedRole).isNull());
QSignalSpy contentSpy(contentModel, &QAbstractItemModel::dataChanged);
QVERIFY(contentSpy.isValid());
targetSelModel->select(contentModel->index(1, 0), QItemSelectionModel::ClearAndSelect);
QVERIFY(contentModel->index(0, 0).data(ModelContentProxyModel::SelectedRole).isNull());
QVERIFY(contentModel->index(1, 0).data(ModelContentProxyModel::SelectedRole).toBool());
QVERIFY(contentModel->index(2, 0).data(ModelContentProxyModel::SelectedRole).isNull());
QVERIFY(!contentSpy.isEmpty());
delete targetSelModel;
delete targetModel;
}
void testWidget()
{
createProbe();
std::unique_ptr<QStringListModel> targetModel(new QStringListModel);
targetModel->setObjectName("targetModel");
targetModel->setStringList(QStringList() << "item1"
<< "item2"
<< "item3");
QTest::qWait(1); // trigger model inspector plugin loading
ClientToolManager mgr;
mgr.requestAvailableTools();
auto widget = mgr.widgetForId("gammaray_modelinspector");
QVERIFY(widget);
widget->show();
const auto views = widget->findChildren<QAbstractItemView *>();
for (auto view : views) {
QVERIFY(view->model());
}
}
};
QTEST_MAIN(ModelInspectorTest)
#include "modelinspectortest.moc"
|