#include <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QDBusReply>
#include "../../registeredSession/plugins/screenlock/screenlockinterface.h"
#include "stubext.h"

using namespace stub_ext;

class ScreenlockInterfaceTest : public testing::Test
{
protected:
    static void SetUpTestSuite()
    {
        m_pScreenlockInterface = new ScreenlockInterface();
	m_pScreenlockDbus = new QDBusInterface("org.ukui.ukcc.session",
			"/Screenlock",
			"org.ukui.ukcc.session.Screenlock",
                        QDBusConnection::sessionBus());
    }

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

    static ScreenlockInterface *m_pScreenlockInterface;
    static QDBusInterface* m_pScreenlockDbus;
};

ScreenlockInterface *ScreenlockInterfaceTest::m_pScreenlockInterface = nullptr;
QDBusInterface *ScreenlockInterfaceTest::m_pScreenlockDbus = nullptr;

TEST_F(ScreenlockInterfaceTest, setShowOnLogin)
{
    if (!m_pScreenlockDbus || !m_pScreenlockDbus->isValid()) {
       return;
    }
    bool oldValue = m_pScreenlockDbus->property("showOnLogin").toBool();
    m_pScreenlockDbus->call("setShowOnLogin", false);
    bool newValue = m_pScreenlockDbus->property("showOnLogin").toBool();
    ASSERT_EQ(newValue, false);
    m_pScreenlockDbus->call("setShowOnLogin", oldValue);
}

TEST_F(ScreenlockInterfaceTest, setWallpaper)
{
    if (!m_pScreenlockDbus || !m_pScreenlockDbus->isValid()) {
       return;
    }
    QGSettings gset("org.ukui.screensaver");
    std::string oldValue = gset.get("background").toString().toStdString(); 
    m_pScreenlockDbus->call("setWallpaper", "/usr/share/backgrounds/city.jpg");
    std::string newValue = gset.get("background").toString().toStdString(); 
    ASSERT_STREQ(newValue.c_str(), "/usr/share/backgrounds/city.jpg");
    m_pScreenlockDbus->call("setWallpaper", oldValue.c_str());
}

TEST_F(ScreenlockInterfaceTest, resetDefault)
{
    if (!m_pScreenlockDbus || !m_pScreenlockDbus->isValid()) {
       return;
    }
    QGSettings gset("org.ukui.screensaver");
    std::string oldValue = gset.get("background").toString().toStdString(); 
    m_pScreenlockDbus->call("resetDefault");
    std::string newValue = gset.get("background").toString().toStdString(); 
    ASSERT_STREQ(newValue.c_str(), "/usr/share/backgrounds/1-warty-final-ubuntukylin.jpg");
    m_pScreenlockDbus->call("setWallpaper", oldValue.c_str());
}
