From fc888e5d2df97c14cb759d1a7ce4dce3e87d24dd Mon Sep 17 00:00:00 2001 From: Fushan Wen Date: Tue, 18 Jan 2022 23:05:37 +0800 Subject: [PATCH] klipper: Add `testTrimmedText` Test if the text is correctly trimmed, and can still provide the full text. --- klipper/autotests/historymodeltest.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/klipper/autotests/historymodeltest.cpp b/klipper/autotests/historymodeltest.cpp index 2c802572d..b3805b8c6 100644 --- a/klipper/autotests/historymodeltest.cpp +++ b/klipper/autotests/historymodeltest.cpp @@ -22,6 +22,11 @@ private Q_SLOTS: void testIndexOf(); void testType_data(); void testType(); + + /** + * Test if the text is correctly trimmed, and can still provide the full text. + */ + void testTrimmedText(); }; void HistoryModelTest::testSetMaxSize() @@ -232,5 +237,26 @@ void HistoryModelTest::testType() QCOMPARE(history->index(0).data(HistoryModel::TypeRole).value(), expectedType); } +void HistoryModelTest::testTrimmedText() +{ + QScopedPointer history(new HistoryModel(nullptr)); + QScopedPointer modelTest(new QAbstractItemModelTester(history.data())); + history->setMaxSize(10); + QCOMPARE(history->rowCount(), 0); + + // insert one item with a very long text + QString veryLongText; + for (int i = 0; i < 500; i++) { + veryLongText.append(QStringLiteral("a")); // Text length is 500 + } + + history->insert(QSharedPointer(new HistoryStringItem(veryLongText))); + const QModelIndex index = history->index(0, 0); + // Test full text + QCOMPARE(index.data(HistoryModel::FullTextRole).value(), veryLongText); + // Test trimmed text, 200 is from HistoryStringItem::text() + QCOMPARE(index.data(Qt::DisplayRole).value(), veryLongText.left(200 - 1) + QStringLiteral("…")); +} + QTEST_MAIN(HistoryModelTest) #include "historymodeltest.moc"