diff --git a/src/widgets/room/autotests/messagedelegatehelperimagetest.cpp b/src/widgets/room/autotests/messagedelegatehelperimagetest.cpp index 0c3f6052..5a4cb4e2 100644 --- a/src/widgets/room/autotests/messagedelegatehelperimagetest.cpp +++ b/src/widgets/room/autotests/messagedelegatehelperimagetest.cpp @@ -1,61 +1,60 @@ /* Copyright (c) 2020 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messagedelegatehelperimagetest.h" #include "room/delegate/messagedelegatehelperimage.h" #include "managerdatapaths.h" #include "ruqola.h" #include "rocketchataccount.h" #include "messages/message.h" #include "messages/messageattachment.h" #include "testdata.h" #include #include #include QTEST_MAIN(MessageDelegateHelperImageTest) MessageDelegateHelperImageTest::MessageDelegateHelperImageTest(QObject *parent) : QObject(parent) { - QStandardPaths::setTestModeEnabled(true); - Ruqola::self()->rocketChatAccount()->setAccountName(QStringLiteral("accountName")); + initTestAccount(); } void MessageDelegateHelperImageTest::shouldExtractMessageData() { MessageDelegateHelperImage helper; QStyleOptionViewItem option; QWidget fakeWidget; option.widget = &fakeWidget; Message message; const MessageAttachment msgAttach = testAttachment(); message.setAttachements({msgAttach}); const MessageDelegateHelperImage::ImageLayout layout = helper.layoutImage(&message, option, 500, 500); QCOMPARE(layout.title, msgAttach.title()); QCOMPARE(layout.description, msgAttach.description()); QVERIFY(layout.isShown); QVERIFY(!layout.isAnimatedImage); QVERIFY(layout.imagePath.endsWith(QLatin1String("/testfile.png"))); QCOMPARE(layout.imageSize.height(), layout.imageSize.width() * 2); // aspect ratio is preserved QVERIFY(layout.imageSize.height() < 500); } diff --git a/src/widgets/room/autotests/messagelistdelegatetest.cpp b/src/widgets/room/autotests/messagelistdelegatetest.cpp index 23e8e0ac..b0c7af12 100644 --- a/src/widgets/room/autotests/messagelistdelegatetest.cpp +++ b/src/widgets/room/autotests/messagelistdelegatetest.cpp @@ -1,165 +1,163 @@ /* Copyright (c) 2020 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messagelistdelegatetest.h" #include "room/delegate/messagelistdelegate.h" #include "ruqola.h" #include "rocketchataccount.h" #include "messagedelegatehelperimagetest.h" #include "messages/message.h" #include "messages/messageattachment.h" #include "testdata.h" #include #include #include #include QTEST_MAIN(MessageListDelegateTest) MessageListDelegateTest::MessageListDelegateTest(QObject *parent) : QObject(parent) { - QStandardPaths::setTestModeEnabled(true); - auto *rcAccount = Ruqola::self()->rocketChatAccount(); - rcAccount->setAccountName(QStringLiteral("accountName")); + initTestAccount(); const QString userId = QStringLiteral("dfaureUserId"); - rcAccount->insertAvatarUrl(userId, avatarLink()); + Ruqola::self()->rocketChatAccount()->insertAvatarUrl(userId, avatarLink()); } void MessageListDelegateTest::layoutChecks_data() { QTest::addColumn("message"); QTest::addColumn("withDateHeader"); Message message; message.setMessageId(QStringLiteral("someNonEmptyId")); message.setUserId(QStringLiteral("dfaureUserId")); message.setUsername(QStringLiteral("dfaure")); message.setTimeStamp(QDateTime(QDate(2020, 2, 1), QTime(4, 7, 15)).toMSecsSinceEpoch()); message.setMessageType(Message::NormalText); QTest::newRow("text_no_date") << message << false; QTest::newRow("text_with_date") << message << true; message.setMessageType(Message::Image); const MessageAttachment msgAttach = testAttachment(); message.setAttachements({msgAttach}); QTest::newRow("attachment_no_text_no_date") << message << false; QTest::newRow("attachment_no_text_with_date") << message << true; message.setText(QStringLiteral("The text")); QTest::newRow("attachment_with_text_no_date") << message << false; QTest::newRow("attachment_with_text_with_date") << message << true; message.setEditedByUsername(message.username()); QTest::newRow("edited_with_attachment_with_text_with_date") << message << true; // TODO tests with reactions } void MessageListDelegateTest::layoutChecks() { QFETCH(Message, message); QFETCH(bool, withDateHeader); // GIVEN a delegate and an index pointing to a message MessageListDelegate delegate; delegate.setRocketChatAccount(Ruqola::self()->rocketChatAccount()); QStyleOptionViewItem option; QWidget fakeWidget; option.widget = &fakeWidget; option.rect = QRect(100, 100, 500, 500); QStandardItemModel model; auto *item = new QStandardItem; item->setData(message.username(), MessageModel::Username); item->setData(message.userId(), MessageModel::UserId); item->setData(withDateHeader, MessageModel::DateDiffersFromPrevious); item->setData(message.displayTime(), MessageModel::Timestamp); item->setData(QVariant::fromValue(&message), MessageModel::MessagePointer); item->setData(message.text(), MessageModel::OriginalMessage); item->setData(message.text(), MessageModel::MessageConvertedText); model.setItem(0, 0, item); // Ensure it's not last, that's a special case in sizeHint auto *item2 = new QStandardItem; model.setItem(1, 0, item2); const QModelIndex index = model.index(0, 0); // WHEN calculating sizehint const QSize sizeHint = delegate.sizeHint(option, index); QVERIFY(sizeHint.isValid()); option.rect.setSize(sizeHint); // ... and redoing layout while painting const MessageListDelegate::Layout layout = delegate.doLayout(option, index); // THEN QCOMPARE(layout.senderText, QStringLiteral("@dfaure")); QCOMPARE(layout.timeStampText, QStringLiteral("04:07")); QVERIFY(option.rect.contains(layout.usableRect)); QVERIFY(option.rect.contains(layout.senderRect.toRect())); if (message.attachements().isEmpty()) { QVERIFY(layout.attachmentsRect.isNull()); } else { QVERIFY(sizeHint.height() > layout.senderRect.height() + 1); QVERIFY(option.rect.contains(layout.attachmentsRect)); } // Text if (message.text().isEmpty()) { QVERIFY(!layout.textRect.isValid()); } else { QVERIFY(option.rect.contains(layout.textRect)); QCOMPARE(layout.usableRect.left(), layout.textRect.left()); QVERIFY(layout.textRect.top() >= layout.usableRect.top()); QVERIFY(!layout.senderRect.intersects(layout.textRect)); if (!message.attachements().isEmpty()) { QCOMPARE(layout.attachmentsRect.top(), layout.textRect.y() + layout.textRect.height()); } } const int bottom = layout.usableRect.y() + layout.usableRect.height(); // Avatar QCOMPARE(layout.avatarPixmap.height(), layout.senderRect.height()); //qDebug() << layout.avatarPos.y() << "+" << layout.avatarPixmap.height() << "must be <=" << bottom; QVERIFY(layout.avatarPos.y() + layout.avatarPixmap.height() <= bottom); // Reactions if (message.reactions().isEmpty()) { QCOMPARE(layout.reactionsHeight, 0); } else { QVERIFY(layout.reactionsHeight > 15); QVERIFY(layout.reactionsY + layout.reactionsHeight <= bottom); } // Edited if (message.wasEdited()) { QVERIFY(option.rect.contains(layout.editedIconRect)); QVERIFY(!layout.editedIconRect.intersects(layout.textRect)); QVERIFY(!layout.editedIconRect.intersects(layout.senderRect.toRect())); } } diff --git a/src/widgets/room/autotests/testdata.cpp b/src/widgets/room/autotests/testdata.cpp index 0983c9b6..91b00fef 100644 --- a/src/widgets/room/autotests/testdata.cpp +++ b/src/widgets/room/autotests/testdata.cpp @@ -1,66 +1,73 @@ /* Copyright (c) 2020 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "testdata.h" #include "managerdatapaths.h" #include "ruqola.h" #include "rocketchataccount.h" #include #include #include #include +void initTestAccount() +{ + QStandardPaths::setTestModeEnabled(true); + Ruqola::self()->rocketChatAccount()->setAccountName(QStringLiteral("accountName")); +} + MessageAttachment testAttachment() { MessageAttachment msgAttach; const QString title = QStringLiteral("This is the title"); msgAttach.setTitle(title); const QString description = QStringLiteral("A description"); msgAttach.setDescription(description); QPixmap pix(50, 100); pix.fill(Qt::white); // Save the pixmap directly into the cache so that no download hpapens const QString cachePath = ManagerDataPaths::self()->path(ManagerDataPaths::Cache, Ruqola::self()->rocketChatAccount()->accountName()); QDir().mkpath(cachePath); const QString link = QStringLiteral("/testfile.png"); const QString pixFileName = cachePath + link; pix.save(pixFileName, "png"); msgAttach.setLink(link); return msgAttach; } QUrl avatarLink() { // Save the pixmap directly into the cache so that no download hpapens const QString cachePath = ManagerDataPaths::self()->path(ManagerDataPaths::Cache, Ruqola::self()->rocketChatAccount()->accountName()); QDir().mkpath(cachePath); const QString link = QStringLiteral("/avatarpix.png"); const QString pixFileName = cachePath + link; if (!QFileInfo::exists(pixFileName)) { const QString srcPath = QFINDTESTDATA("../../../../src/icons/32-apps-ruqola.png"); Q_ASSERT(!srcPath.isEmpty()); if (!QFile::copy(srcPath, pixFileName)) { qWarning() << "Couldn't copy" << srcPath << "to" << pixFileName; } } return QUrl(QLatin1String("https://example.com") + link); } diff --git a/src/widgets/room/autotests/testdata.h b/src/widgets/room/autotests/testdata.h index a800835e..a64196d5 100644 --- a/src/widgets/room/autotests/testdata.h +++ b/src/widgets/room/autotests/testdata.h @@ -1,24 +1,25 @@ /* Copyright (c) 2020 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messages/messageattachment.h" +void initTestAccount(); MessageAttachment testAttachment(); QUrl avatarLink();