#include <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QDBusReply>
#include <QStringList>
#include <QDBusInterface>
#include "../../registeredSession/plugins/autoboot/autostartinterface.h"
#include "stubext.h"

using namespace stub_ext;

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

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

    static QDBusInterface* m_pAutobootDbus;
};

QDBusInterface *AutobootInterfaceTest::m_pAutobootDbus = nullptr;

TEST_F(AutobootInterfaceTest, addAutobootApp)
{
    if (!m_pAutobootDbus || !m_pAutobootDbus->isValid()) {
       return;
    }
    QDBusReply<bool> ret = m_pAutobootDbus->call("addAutobootApp", "/usr/share/applications/kylin-video.desktop");
    ASSERT_EQ(ret.isValid(), true);
}

TEST_F(AutobootInterfaceTest, setAppHiddenInfo)
{
    if (!m_pAutobootDbus || !m_pAutobootDbus->isValid()) {
       return;
    }
    QDBusReply<bool> ret = m_pAutobootDbus->call("setAppHiddenInfo", "kylin-video.desktop", true);
    ASSERT_EQ(ret.isValid(), true);
}

TEST_F(AutobootInterfaceTest, saveAppStatus)
{
    if (!m_pAutobootDbus || !m_pAutobootDbus->isValid()) {
       return;
    }
    QDBusReply<void> ret = m_pAutobootDbus->call("saveAppStatus", "/usr/share/applications/kylin-video.desktop", false);
    ASSERT_EQ(ret.isValid(), true);
}

TEST_F(AutobootInterfaceTest, setApplist)
{
    if (!m_pAutobootDbus || !m_pAutobootDbus->isValid()) {
       return;
    }
    QStringList oldValue = m_pAutobootDbus->property("appList").toStringList();
    QStringList strList = {"kylin-weather.desktop", "sogouImeService.desktop", "kylin-video.desktop"};
    QDBusReply<void> ret = m_pAutobootDbus->call("setApplist", strList);
    ASSERT_EQ(ret.isValid(), true);
    QStringList newValue = m_pAutobootDbus->property("appList").toStringList();
    ASSERT_STREQ(newValue.at(0).toStdString().c_str(), "kylin-weather.desktop");
    m_pAutobootDbus->call("setApplist", oldValue);
}

TEST_F(AutobootInterfaceTest, getDisplayStatus)
{
    if (!m_pAutobootDbus || !m_pAutobootDbus->isValid()) {
       return;
    }
    QDBusReply<bool> ret = m_pAutobootDbus->call("getDisplayStatus", "/usr/share/applications/kylin-video.desktop");
    ASSERT_EQ(ret.value(), false);
}

TEST_F(AutobootInterfaceTest, deleteLocalFile)
{
    if (!m_pAutobootDbus || !m_pAutobootDbus->isValid()) {
       return;
    }
    QDBusReply<bool> ret = m_pAutobootDbus->call("deleteLocalFile", "kylin-video.desktop");
    ASSERT_EQ(ret.value(), true);
}
