1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/test/model/sessionchatlog_test.cpp

138 lines
4.2 KiB
C++
Raw Normal View History

/*
Copyright © 2019 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "src/model/ichatlog.h"
#include "src/model/imessagedispatcher.h"
#include "src/model/sessionchatlog.h"
#include <QtTest/QtTest>
namespace {
static const QString TEST_USERNAME = "qTox Tester #1";
Message createMessage(const QString& content)
{
Message message;
message.content = content;
message.isAction = false;
message.timestamp = QDateTime::currentDateTime();
return message;
}
class MockCoreIdHandler : public ICoreIdHandler
{
public:
ToxId getSelfId() const override
{
std::terminate();
return ToxId();
}
ToxPk getSelfPublicKey() const override
{
static uint8_t id[TOX_PUBLIC_KEY_SIZE] = {5};
return ToxPk(id);
}
QString getUsername() const override
{
return TEST_USERNAME;
}
};
} // namespace
class TestSessionChatLog : public QObject
{
Q_OBJECT
public:
chore: Various code cleanups. * Reorder class data members and/or constructor initialisers to match, reducing confusion about when members will be initialised. * Remove (most) unused variables. Not removed: some global variables with `TODO(sudden6)` on them for using them in the future. I don't know how far into the future sudden6 wants to use them, so I left them there for now. * Distinguish different bootstrap nodes in the logs by index in the bootstrap node list. Originally, we used to log the address/port of the node we're bootstrapping to. This was removed out of privacy concerns (even though the bootstrap nodes are public). This made the logs much less useful when debugging why the client isn't connecting. Having indices makes it easier to see that different nodes are being selected, and makes it possible to determine which node was selected. * Explicitly cast unused results of Tox API functions to `void` when all we want is to know whether the function succeeds or not. * Don't try to `#include <unistd.h>` on Windows. It does not exist on MSVC. * Remove extra `;` after function definitions. * Remove reference indirection of QJsonValueRef, since a copy of that ref (small pointer-like object) has to be made anyway when iterating over QJsonArrays. * Make some file-scope global state `static`. * Use `nullptr` instead of `NULL`. * Add `#if DESKTOP_NOTIFICATIONS` around the code that implements desktop notifications, so it becomes a bit easier to compile everything with a single compiler command - useful for manually running static analysers. * Fix an error on MSVC where `disconnect` is looked up to be a non-static member function and the `this` capture is missing. * Consistently use `struct` and `class` tags for types. * Use references in ranged-for where it reduces copies. * Move private static data members out of the Style class and into file-local scope. There is no need for them to be in the class. Also marked them `const` where possible. * Removed unused lambda capture. * Ensure qTox can compile under NDEBUG with `-Wunused-variable` by inlining the unused variable into the `assert` that was its only target. * Minor reformatting in core_test.cpp.
2020-04-16 23:59:29 +08:00
TestSessionChatLog(){}
private slots:
void init();
void testSanity();
private:
MockCoreIdHandler idHandler;
std::unique_ptr<SessionChatLog> chatLog;
};
/**
* @brief Test initialiation, resets the chatlog
*/
void TestSessionChatLog::init()
{
chatLog = std::unique_ptr<SessionChatLog>(new SessionChatLog(idHandler));
}
/**
* @brief Quick sanity test that the chatlog is working as expected. Tests basic insertion, retrieval, and searching of messages
*/
void TestSessionChatLog::testSanity()
{
/* ChatLogIdx(0) */ chatLog->onMessageSent(DispatchedMessageId(0), createMessage("test"));
/* ChatLogIdx(1) */ chatLog->onMessageSent(DispatchedMessageId(1), createMessage("test test"));
/* ChatLogIdx(2) */ chatLog->onMessageReceived(ToxPk(), createMessage("test2"));
/* ChatLogIdx(3) */ chatLog->onFileUpdated(ToxPk(), ToxFile());
/* ChatLogIdx(4) */ chatLog->onMessageSent(DispatchedMessageId(2), createMessage("test3"));
/* ChatLogIdx(5) */ chatLog->onMessageSent(DispatchedMessageId(3), createMessage("test4"));
/* ChatLogIdx(6) */ chatLog->onMessageSent(DispatchedMessageId(4), createMessage("test"));
/* ChatLogIdx(7) */ chatLog->onMessageReceived(ToxPk(), createMessage("test5"));
QVERIFY(chatLog->getNextIdx() == ChatLogIdx(8));
QVERIFY(chatLog->at(ChatLogIdx(3)).getContentType() == ChatLogItem::ContentType::fileTransfer);
QVERIFY(chatLog->at(ChatLogIdx(7)).getContentType() == ChatLogItem::ContentType::message);
auto searchPos = SearchPos{ChatLogIdx(1), 0};
auto searchResult = chatLog->searchForward(searchPos, "test", ParameterSearch());
QVERIFY(searchResult.found);
QVERIFY(searchResult.len == 4);
QVERIFY(searchResult.pos.logIdx == ChatLogIdx(1));
QVERIFY(searchResult.start == 0);
searchPos = searchResult.pos;
searchResult = chatLog->searchForward(searchPos, "test", ParameterSearch());
QVERIFY(searchResult.found);
QVERIFY(searchResult.len == 4);
QVERIFY(searchResult.pos.logIdx == ChatLogIdx(1));
QVERIFY(searchResult.start == 5);
searchPos = searchResult.pos;
searchResult = chatLog->searchForward(searchPos, "test", ParameterSearch());
QVERIFY(searchResult.found);
QVERIFY(searchResult.len == 4);
QVERIFY(searchResult.pos.logIdx == ChatLogIdx(2));
QVERIFY(searchResult.start == 0);
searchPos = searchResult.pos;
searchResult = chatLog->searchBackward(searchPos, "test", ParameterSearch());
QVERIFY(searchResult.found);
QVERIFY(searchResult.len == 4);
QVERIFY(searchResult.pos.logIdx == ChatLogIdx(1));
QVERIFY(searchResult.start == 5);
}
QTEST_GUILESS_MAIN(TestSessionChatLog)
#include "sessionchatlog_test.moc"