#include <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QDBusReply>
#include <QDBusInterface>
#include <QGSettings>
#include "../../registeredSession/plugins/wallpaper/workerobject.h"
#include "../../registeredSession/plugins/wallpaper/xmlhandle.h"
#include "stubext.h"

using namespace stub_ext;

class WallpaperInterfaceTest : public testing::Test
{
protected:
    static void SetUpTestSuite()
    {
	m_pWallpaperDbus = new QDBusInterface("org.ukui.ukcc.session",
			"/Wallpaper",
			"org.ukui.ukcc.session.Wallpaper",
                        QDBusConnection::sessionBus());
    }

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

    static QDBusInterface* m_pWallpaperDbus;
};

QDBusInterface *WallpaperInterfaceTest::m_pWallpaperDbus = nullptr;

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

TEST_F(WallpaperInterfaceTest, setDisplayType)
{
    if (!m_pWallpaperDbus || !m_pWallpaperDbus->isValid()) {
       return;
    }
    std::string oldType = m_pWallpaperDbus->property("displayType").toString().toStdString();
    m_pWallpaperDbus->call("setDisplayType", "centered");
    std::string newType = m_pWallpaperDbus->property("displayType").toString().toStdString();

    ASSERT_STREQ(newType.c_str(), "centered");
    m_pWallpaperDbus->call("setDisplayType", oldType.c_str());
}

TEST_F(WallpaperInterfaceTest, resetDefault)
{
    if (!m_pWallpaperDbus || !m_pWallpaperDbus->isValid()) {
       return;
    }
    QGSettings gset("org.mate.background");
    std::string oldType = m_pWallpaperDbus->property("displayType").toString().toStdString();
    m_pWallpaperDbus->call("resetDefault");
    std::string newType = m_pWallpaperDbus->property("displayType").toString().toStdString();
    ASSERT_STREQ(newType.c_str(), "scaled");
    m_pWallpaperDbus->call("setDisplayType", oldType.c_str());
}
