/*
* Copyright (C) 2019 ~ 2020 Deepin Technology Co., Ltd.
*
* Author:     zhangyunfeng <zhangyunfeng@uniontech.com>
* Maintainer: zhangyunfeng <zhangyunfeng@uniontech.com>
*
* 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 3 of the License, or
* 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, see <http://www.gnu.org/licenses/>.
*/

#include "ut_mainwindow.h"

#include <QCloseEvent>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QTest>

ut_mainwindow_test::ut_mainwindow_test()
{

}

void ut_mainwindow_test::SetUp()
{
    m_mainWindow = new MainWindow();
    m_mainWindow->show();
}

void ut_mainwindow_test::TearDown()
{
    if (m_mainWindow) {
        m_mainWindow->deleteLater();
        m_mainWindow = nullptr;
    }
}

/*******************************************************************************
 1. @函数:    keyPressEvent
 2. @说明:    测试keyPressEvent，和MainWindow的构造和析构函数
*******************************************************************************/
TEST_F(ut_mainwindow_test, keyPressEvent)
{
    setlocale(LC_NUMERIC, "C");
    QKeyEvent* event = new QKeyEvent(QEvent::Type::KeyPress, Qt::Key_Escape, Qt::KeyboardModifier::NoModifier);
    m_mainWindow->m_index = 1;
    m_mainWindow->keyPressEvent(event);

    m_mainWindow->m_index = 5;
    m_mainWindow->keyPressEvent(event);

    delete event;
    event = nullptr;
}

TEST_F(ut_mainwindow_test, closeEvent)
{
    QCloseEvent event;
    m_mainWindow->closeEvent(&event);
}

/*******************************************************************************
 1. @函数:    mousePressEvent
 2. @说明:    测试鼠标按下事件
*******************************************************************************/
TEST_F(ut_mainwindow_test, mousePressEvent)
{
    QMouseEvent* event = new QMouseEvent(QEvent::Type::MouseButtonPress, QPointF(100, 100),
                                         Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton,
                                         Qt::KeyboardModifier::NoModifier);
    m_mainWindow->mousePressEvent(event);
    delete event;
}

/*******************************************************************************
 1. @函数:    mouseReleaseEvent
 2. @说明:    测试鼠标释放事件
*******************************************************************************/
TEST_F(ut_mainwindow_test, mouseReleaseEvent)
{
    QMouseEvent* event = new QMouseEvent(QEvent::Type::MouseButtonRelease, QPointF(100, 100),
                                         Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton,
                                         Qt::KeyboardModifier::NoModifier);
    m_mainWindow->mouseReleaseEvent(event);
    delete event;
}

/*******************************************************************************
 1. @函数:    mouseMoveEvent
 2. @说明:    测试鼠标移动事件
*******************************************************************************/
TEST_F(ut_mainwindow_test, mouseMoveEvent)
{
    QMouseEvent* event = new QMouseEvent(QEvent::Type::MouseMove, QPointF(200, 200),
                                         Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton,
                                         Qt::KeyboardModifier::NoModifier);
    m_mainWindow->mouseMoveEvent(event);
    delete event;
}

/*******************************************************************************
 1. @函数:    resizeEvent
 2. @说明:    测试窗口大小变化事件
*******************************************************************************/
TEST_F(ut_mainwindow_test, resizeEventSmall)
{
    QResizeEvent* event = new QResizeEvent(QSize(800, 600), QSize(1000, 700));
    m_mainWindow->resizeEvent(event);
    delete event;
}

/*******************************************************************************
 1. @函数:    slotTheme
 2. @说明:    主题变化响应
*******************************************************************************/
TEST_F(ut_mainwindow_test, slotTheme)
{
    m_mainWindow->slotTheme();
}

/*******************************************************************************
 1. @函数:    initUI
 2. @说明:    测试initUI
*******************************************************************************/
TEST_F(ut_mainwindow_test, initUI)
{
    m_mainWindow->initUI();
    ASSERT_TRUE(m_mainWindow->m_fakerWidget != nullptr);
    ASSERT_TRUE(m_mainWindow->m_pCloseBtn != nullptr);
    ASSERT_TRUE(m_mainWindow->m_nextBtn != nullptr);
    ASSERT_TRUE(m_mainWindow->m_previousBtn != nullptr);
    ASSERT_TRUE(m_mainWindow->m_doneBtn != nullptr);
}

/*******************************************************************************
 1. @函数:    initConnect
 2. @说明:    测试initConnect
*******************************************************************************/
TEST_F(ut_mainwindow_test, initConnect)
{
    m_mainWindow->initConnect();
}

/*******************************************************************************
 1. @函数:    bindAnimation
 2. @说明:    测试bindAnimation
*******************************************************************************/
TEST_F(ut_mainwindow_test, bindAnimation)
{
    if (!m_mainWindow->m_current)
        m_mainWindow->m_current = new DWidget(m_mainWindow);
    if (!m_mainWindow->m_last)
        m_mainWindow->m_last = new DWidget(m_mainWindow);
    m_mainWindow->bindAnimation();
}

/*******************************************************************************
 1. @函数:    updateModule
 2. @说明:    测试updateModule
*******************************************************************************/
TEST_F(ut_mainwindow_test, updateModule)
{
    m_mainWindow->m_isFirst = true;
    m_mainWindow->updateModule(1);

    m_mainWindow->updateModule(2);
    ASSERT_TRUE(m_mainWindow->m_current != nullptr);

    m_mainWindow->updateModule(3);
    ASSERT_TRUE(m_mainWindow->m_current != nullptr);

    m_mainWindow->updateModule(4);
    ASSERT_TRUE(m_mainWindow->m_current != nullptr);
}

/*******************************************************************************
 1. @函数:    animationHandle
 2. @说明:    测试animationHandle
*******************************************************************************/
TEST_F(ut_mainwindow_test, animationHandle)
{
    m_mainWindow->m_last = new DWidget(m_mainWindow);
    m_mainWindow->animationHandle();
}

TEST_F(ut_mainwindow_test, delayInitVideo)
{
    m_mainWindow->delayInitVideo();
    EXPECT_TRUE(m_mainWindow->m_delayInit);
}

TEST_F(ut_mainwindow_test, resizeEventTiny)
{
    QResizeEvent event(QSize(600, 400), QSize(800, 600));
    m_mainWindow->resizeEvent(&event);
}

TEST_F(ut_mainwindow_test, resizeEventExact)
{
    QResizeEvent event(QSize(1280, 800), QSize(1280, 800));
    m_mainWindow->resizeEvent(&event);
}

TEST_F(ut_mainwindow_test, previous)
{
    // 设置动画目标并启动，触发动画运行分支的提前返回
    m_mainWindow->m_currentAni->setTargetObject(m_mainWindow);
    m_mainWindow->m_currentAni->setPropertyName("pos");
    m_mainWindow->m_currentAni->setDuration(1000);
    m_mainWindow->m_currentAni->setStartValue(QPoint(0, 0));
    m_mainWindow->m_currentAni->setEndValue(QPoint(100, 0));
    m_mainWindow->m_currentAni->start();
    ASSERT_EQ(m_mainWindow->m_currentAni->state(), QPropertyAnimation::Running);
    m_mainWindow->m_index = 1;
    m_mainWindow->previous();
}

TEST_F(ut_mainwindow_test, next)
{
    m_mainWindow->m_currentAni->setTargetObject(m_mainWindow);
    m_mainWindow->m_currentAni->setPropertyName("pos");
    m_mainWindow->m_currentAni->setDuration(1000);
    m_mainWindow->m_currentAni->setStartValue(QPoint(0, 0));
    m_mainWindow->m_currentAni->setEndValue(QPoint(100, 0));
    m_mainWindow->m_currentAni->start();
    ASSERT_EQ(m_mainWindow->m_currentAni->state(), QPropertyAnimation::Running);
    m_mainWindow->m_index = 0;
    m_mainWindow->next();
}

TEST_F(ut_mainwindow_test, finishedAndClose)
{
    m_mainWindow->m_isFirst = false;
    m_mainWindow->finishedAndClose();
}
