File: RosterItemWatcherTest.cpp

package info (click to toggle)
kaidan 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,376 kB
  • sloc: cpp: 30,143; xml: 1,623; sh: 522; python: 34; makefile: 5
file content (79 lines) | stat: -rw-r--r-- 2,549 bytes parent folder | download | duplicates (2)
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
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: GPL-3.0-or-later

// Qt
#include <QSignalSpy>
#include <QTest>
// Kaidan
#include "MainController.h"
#include "RosterItemWatcher.h"
#include "Test.h"

using namespace std;

template<typename T, typename Converter>
auto transform(vector<T> &input, Converter convert)
{
    using Output = decay_t<decltype(convert(input.front()))>;
    vector<Output> output;
    output.reserve(input.size());
    transform(input.begin(), input.end(), back_inserter(output), std::move(convert));
    return output;
}

class RosterItemWatcherTest : public Test
{
    Q_OBJECT

private:
    Q_SLOT void notifications();
};

struct SpyCountPair {
    std::unique_ptr<QSignalSpy> spy;
    int count;
};

void checkEmitted(vector<pair<RosterItemWatcher *, int>> watchers, function<void()> trigger)
{
    auto spies = transform(watchers, [](pair<RosterItemWatcher *, int> &input) {
        return SpyCountPair{std::make_unique<QSignalSpy>(input.first, &RosterItemWatcher::itemChanged), input.second};
    });
    trigger();
    for_each(spies.begin(), spies.end(), [](auto &pair) {
        QCOMPARE(pair.spy->size(), pair.count);
    });
}

void RosterItemWatcherTest::notifications()
{
    // This test requires a RosterModel instance, so we init kaidan
    MainController mainController(this);
    RosterItem item;

    const auto accountJid = QStringLiteral("alice@example.org");

    auto &notifier = RosterItemNotifier::instance();
    RosterItemWatcher watcher1;
    watcher1.setAccountJid(accountJid);
    watcher1.setJid(QStringLiteral("hello@kaidan.im"));
    RosterItemWatcher watcher2;
    watcher2.setAccountJid(accountJid);
    watcher2.setJid(QStringLiteral("hello@kaidan.im"));
    RosterItemWatcher watcher3;
    watcher3.setAccountJid(accountJid);
    watcher3.setJid(QStringLiteral("user@kaidan.im"));

    vector<pair<RosterItemWatcher *, int>> expected({{pair{&watcher1, 2}}, {pair{&watcher2, 2}}, {pair{&watcher3, 1}}});
    checkEmitted(expected, [&]() {
        notifier.notifyWatchers(accountJid, QStringLiteral("hello@kaidan.im"), item);
        notifier.notifyWatchers(accountJid, QStringLiteral("not-found"), item);
        notifier.notifyWatchers(accountJid, QStringLiteral("user@kaidan.im"), item);
        notifier.notifyWatchers(accountJid, QStringLiteral("hello@kaidan.im"), item);
        notifier.notifyWatchers(accountJid, QStringLiteral("not-found"), item);
    });
}

QTEST_GUILESS_MAIN(RosterItemWatcherTest)
#include "RosterItemWatcherTest.moc"