#include <QtTest>

#include "sidebar-main.h"
#define private public
#include "../../src/sidebar-dbus-service.h"
#undef private

#include "sidebaradaptor.h"

class sidebarDbusService_test : public QObject
{
    Q_OBJECT

private slots:
    void init();
    void test_parentAndAdaptor();
    void test_activeDispatchesKnownModules();
    void test_shortcutWidgetActive_ignoresEmptyId();
    void test_shortcutWidgetActive_dispatchesValidId();
    void test_stateSignalForwardedToAdaptor();
};

void sidebarDbusService_test::init()
{
    SidebarMain::resetRecords();
}

void sidebarDbusService_test::test_parentAndAdaptor()
{
    SidebarMain main;
    Sidebar::SidebarDbusService service(&main);

    QCOMPARE(service.parent(), &main);
    QVERIFY(service.m_adaptor != nullptr);
}

void sidebarDbusService_test::test_activeDispatchesKnownModules()
{
    SidebarMain main;
    Sidebar::SidebarDbusService service(&main);

    service.sidebarActive();
    service.shortcutsActive();
    service.active("notice");
    service.active("unknown");

    QStringList calls = SidebarMain::calls();
    QVERIFY(calls.contains("sidebar:0"));
    QVERIFY(calls.contains("shortcut:0"));
    QVERIFY(calls.contains("notice:0"));
    QCOMPARE(calls.filter("unknown").count(), 0);
}

void sidebarDbusService_test::test_shortcutWidgetActive_ignoresEmptyId()
{
    SidebarMain main;
    Sidebar::SidebarDbusService service(&main);

    service.shortcutWidgetActive("", true);

    QVERIFY(!SidebarMain::calls().contains("widget"));
}

void sidebarDbusService_test::test_shortcutWidgetActive_dispatchesValidId()
{
    SidebarMain main;
    Sidebar::SidebarDbusService service(&main);

    service.shortcutWidgetActive("network", true);

    QVERIFY(SidebarMain::calls().contains("widget"));
    QCOMPARE(SidebarMain::lastWidgetId(), QString("network"));
    QCOMPARE(SidebarMain::lastShowReturnButton(), true);
}

void sidebarDbusService_test::test_stateSignalForwardedToAdaptor()
{
    SidebarMain main;
    Sidebar::SidebarDbusService service(&main);
    QVariantMap data;
    data.insert("unreadMsg", 3);

    Q_EMIT main.stateChange("sidebar", data);

    auto *adaptor = qobject_cast<SidebarAdaptor *>(service.m_adaptor);
    QVERIFY(adaptor != nullptr);
    QCOMPARE(adaptor->lastModule(), QString("sidebar"));
    QCOMPARE(adaptor->lastData().value("unreadMsg").toInt(), 3);
}

QTEST_MAIN(sidebarDbusService_test)

#include "sidebar-dbus-service-test.moc"
