File: offlinemsgengine_test.cpp

package info (click to toggle)
qtox 1.18.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,996 kB
  • sloc: cpp: 48,067; xml: 8,560; python: 704; sh: 232; makefile: 14
file content (139 lines) | stat: -rw-r--r-- 4,239 bytes parent folder | download
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
/* SPDX-License-Identifier: GPL-3.0-or-later
 * Copyright © 2019 by The qTox Project Contributors
 * Copyright © 2024-2025 The TokTok team.
 */

#include "src/persistence/offlinemsgengine.h"

#include <QtTest/QtTest>

class TestOfflineMsgEngine : public QObject
{
    Q_OBJECT

private slots:
    void testReceiptBeforeMessage();
    void testReceiptAfterMessage();
    void testResendWorkflow();
    void testTypeCoordination();
    void testCallback();
};

namespace {
void completionFn(bool success)
{
    std::ignore = success;
}
} // namespace

void TestOfflineMsgEngine::testReceiptBeforeMessage()
{
    OfflineMsgEngine offlineMsgEngine;

    Message msg{false, QString(), QDateTime(), {}};

    const auto receipt = ReceiptNum(0);
    offlineMsgEngine.onReceiptReceived(receipt);
    offlineMsgEngine.addSentCoreMessage(receipt, Message(), completionFn);

    const auto removedMessages = offlineMsgEngine.removeAllMessages();

    QVERIFY(removedMessages.empty());
}

void TestOfflineMsgEngine::testReceiptAfterMessage()
{
    OfflineMsgEngine offlineMsgEngine;

    const auto receipt = ReceiptNum(0);
    offlineMsgEngine.addSentCoreMessage(receipt, Message(), completionFn);
    offlineMsgEngine.onReceiptReceived(receipt);

    const auto removedMessages = offlineMsgEngine.removeAllMessages();

    QVERIFY(removedMessages.empty());
}

void TestOfflineMsgEngine::testResendWorkflow()
{
    OfflineMsgEngine offlineMsgEngine;

    const auto receipt = ReceiptNum(0);
    offlineMsgEngine.addSentCoreMessage(receipt, Message(), completionFn);
    auto messagesToResend = offlineMsgEngine.removeAllMessages();

    QCOMPARE(messagesToResend.size(), 1);

    offlineMsgEngine.addSentCoreMessage(receipt, Message(), completionFn);
    offlineMsgEngine.onReceiptReceived(receipt);

    messagesToResend = offlineMsgEngine.removeAllMessages();
    QVERIFY(messagesToResend.empty());

    const auto nullMsg = Message();
    auto msg2 = Message();
    auto msg3 = Message();
    msg2.content = "msg2";
    msg3.content = "msg3";
    offlineMsgEngine.addSentCoreMessage(ReceiptNum(0), nullMsg, completionFn);
    offlineMsgEngine.addSentCoreMessage(ReceiptNum(1), nullMsg, completionFn);
    offlineMsgEngine.addSentCoreMessage(ReceiptNum(2), msg2, completionFn);
    offlineMsgEngine.addSentCoreMessage(ReceiptNum(3), msg3, completionFn);

    offlineMsgEngine.onReceiptReceived(ReceiptNum(0));
    offlineMsgEngine.onReceiptReceived(ReceiptNum(1));
    offlineMsgEngine.onReceiptReceived(ReceiptNum(3));

    messagesToResend = offlineMsgEngine.removeAllMessages();
    QCOMPARE(messagesToResend.size(), 1);
    QCOMPARE(messagesToResend[0].message.content, "msg2");
}


void TestOfflineMsgEngine::testTypeCoordination()
{
    OfflineMsgEngine offlineMsgEngine;

    auto msg1 = Message();
    auto msg2 = Message();
    auto msg3 = Message();
    auto msg4 = Message();

    msg1.content = "msg1";
    msg2.content = "msg2";
    msg3.content = "msg3";
    msg4.content = "msg4";

    offlineMsgEngine.addSentCoreMessage(ReceiptNum(1), msg1, completionFn);
    offlineMsgEngine.addUnsentMessage(msg2, completionFn);
    offlineMsgEngine.addSentCoreMessage(ReceiptNum(2), msg3, completionFn);
    offlineMsgEngine.addSentCoreMessage(ReceiptNum(3), msg4, completionFn);

    const auto messagesToResend = offlineMsgEngine.removeAllMessages();

    QCOMPARE(messagesToResend.size(), 4);
    QCOMPARE(messagesToResend[0].message.content, "msg1");
    QCOMPARE(messagesToResend[1].message.content, "msg2");
    QCOMPARE(messagesToResend[2].message.content, "msg3");
    QCOMPARE(messagesToResend[3].message.content, "msg4");
}

void TestOfflineMsgEngine::testCallback()
{
    OfflineMsgEngine offlineMsgEngine;

    size_t numCallbacks = 0;
    auto callback = [&numCallbacks](bool) { numCallbacks++; };
    Message msg{false, QString(), QDateTime(), {}};

    offlineMsgEngine.addSentCoreMessage(ReceiptNum(1), Message(), callback);
    offlineMsgEngine.addSentCoreMessage(ReceiptNum(2), Message(), callback);

    offlineMsgEngine.onReceiptReceived(ReceiptNum(1));
    offlineMsgEngine.onReceiptReceived(ReceiptNum(2));

    QCOMPARE(numCallbacks, 2);
}

QTEST_GUILESS_MAIN(TestOfflineMsgEngine)
#include "offlinemsgengine_test.moc"