/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2024 KylinSoft  Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QDBusReply>
#include <QDBusInterface>
#include <QGSettings>
#include <QMetaType>
#include <QDBusMetaType>
#include "stubext.h"
#include "../../registeredSession/plugins/font/libfun.h"

using namespace stub_ext;

class FontInterfaceTest : public testing::Test
{
protected:
    static void SetUpTestSuite()
    {
        qRegisterMetaType<ViewData>("ViewData");
        qDBusRegisterMetaType<ViewData>();
        qRegisterMetaType<ViewDataList>("ViewDataList");
        qDBusRegisterMetaType<ViewDataList>();

        qRegisterMetaType<FontInformation>("FontInformation");
        qDBusRegisterMetaType<FontInformation>();
        m_pFontDbus = new QDBusInterface("org.ukui.ukcc.session",
                                         "/Font",
                                         "org.ukui.ukcc.session.Font",
                                         QDBusConnection::sessionBus());
    }

    static void TearDownTestSuite()
    {
        if (m_pFontDbus) {
            delete m_pFontDbus;
            m_pFontDbus = nullptr;
        }
    }
    static QDBusInterface* m_pFontDbus;
};

QDBusInterface *FontInterfaceTest::m_pFontDbus = nullptr;

TEST_F(FontInterfaceTest, getViewData)
{
    QDBusReply<ViewDataList> res = m_pFontDbus->call("getViewData");
    ASSERT_GE(res.value().size(), 100);
}

TEST_F(FontInterfaceTest, getInstallViewData)
{
    QDBusReply<ViewDataList> res = m_pFontDbus->call("getInstallViewData");
    ASSERT_GE(res.value().size(), 0);
}

TEST_F(FontInterfaceTest, getFontDetails)
{
    QString fontPath = "/usr/share/fonts/truetype/ubuntu/UbuntuMono-Italic[wght].ttf";
    QDBusReply<FontInformation> res =  m_pFontDbus->call("getFontDetails", fontPath);
    ASSERT_STREQ(res.value().path.toStdString().c_str(), fontPath.toStdString().c_str());
}

TEST_F(FontInterfaceTest, resetDefault)
{
    QDBusReply<bool> res =  m_pFontDbus->call("resetDefault");
    EXPECT_TRUE(res.value());
}

TEST_F(FontInterfaceTest, installFontList)
{
    QVariantList strList;
    QDBusReply<bool> res =  m_pFontDbus->call("installFontList", strList);
    EXPECT_TRUE(res.value());
}

TEST_F(FontInterfaceTest, uninstallFont)
{
    QDBusReply<bool> res =  m_pFontDbus->call("uninstallFont", "", "");
    EXPECT_TRUE(res.value());
}

TEST_F(FontInterfaceTest, applyFont)
{
    QDBusReply<bool> res =  m_pFontDbus->call("applyFont", "Ubuntu Mono", "/usr/share/fonts/truetype/ubuntu/UbuntuMono-Italic[wght].ttf", "Medium Italic");
    EXPECT_TRUE(res.value());
}

TEST_F(FontInterfaceTest, getFontSize)
{
    QDBusReply<double> res = m_pFontDbus->call("getFontSize");
    double oldValue = res.value();
    m_pFontDbus->call("setFontSize", 11);
    res = m_pFontDbus->call("getFontSize");
    ASSERT_EQ(res.value(), 11);
    m_pFontDbus->call("setFontSize", oldValue);
}

