#include <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QDBusReply>
#include "../../registeredSession/plugins/shortcut/shortcutinterface.h"
#include "../../registeredSession/plugins/shortcut/type.h"
#include "stubext.h"

using namespace stub_ext;

class ShortcutInterfaceTest : public testing::Test
{
protected:
    static void SetUpTestSuite()
    {
        m_pShortcutInterface = new ShortcutInterface();
	m_pShortcutDbus = new QDBusInterface("org.ukui.ukcc.session",
			"/Shortcut",
			"org.ukui.ukcc.session.Shortcut",
                        QDBusConnection::sessionBus());
    }

    static void TearDownTestSuite()
    {
	if (m_pShortcutInterface) {
            delete m_pShortcutInterface;
            m_pShortcutInterface = nullptr;
	}
	if (m_pShortcutInterface) {
	    delete m_pShortcutInterface;
	    m_pShortcutInterface = nullptr;
	}
    }

    static ShortcutInterface *m_pShortcutInterface;
    static QDBusInterface* m_pShortcutDbus;
};

ShortcutInterface *ShortcutInterfaceTest::m_pShortcutInterface = nullptr;
QDBusInterface *ShortcutInterfaceTest::m_pShortcutDbus = nullptr;

TEST_F(ShortcutInterfaceTest, getSystemShortcutSize)
{
    if (!m_pShortcutDbus || !m_pShortcutDbus->isValid()) {
       return;
    }
    QDBusReply<QStringPairList> ret = m_pShortcutDbus->call("getSystemShortcut");
    ASSERT_TRUE(ret.isValid());
    QStringPairList pairList = ret.value();
    ASSERT_GE(pairList.size(), 15);
}

TEST_F(ShortcutInterfaceTest, getCustomShortcutSize)
{
    if (!m_pShortcutDbus || !m_pShortcutDbus->isValid()) {
       return;
    }
    QDBusReply<QStringPairList> ret = m_pShortcutDbus->call("getSystemShortcut");
    ASSERT_TRUE(ret.isValid());
    QStringPairList pairList = ret.value();
    ASSERT_GE(pairList.size(), 0);
}

TEST_F(ShortcutInterfaceTest, setSystemShortcut)
{
    if (!m_pShortcutDbus || !m_pShortcutDbus->isValid()) {
       return;
    }
    QGSettings generalShort("org.ukui.SettingsDaemon.plugins.media-keys");
    std::string oldValue = generalShort.get("kylin-display-switch").toString().toStdString();

    m_pShortcutDbus->call("setSystemShortcut","kylin-display-switch","<Win>V");
    std::string newValue = generalShort.get("kylin-display-switch").toString().toStdString();

    ASSERT_STREQ(newValue.c_str(), "<Win>V");

    generalShort.set("kylin-display-switch", oldValue.c_str());
}
