#include <QtTest/QTest>
#include <QStringList>
#include <QSignalSpy>
#include <QThread>
#include <memory>
#include <QBuffer>
#include <QPrintDialog>
#include <QWidget>
#include <kysdk/kysdk-security/libkyfilesec.h>
#include "../src/controller/core/coreinteraction.h"
#include "../src/controller/core/core.h"
#include "../src/controller/interaction.h"
#include "../src/model/dbus.h"
using namespace kdk;
class MyTest : public QObject
{
    Q_OBJECT
public:
    MyTest();
    ~MyTest();

private Q_SLOTS:
    static void initTestCase()
    {
        int argc = 0;
        char *argv[] = {};
        m_app = std::make_unique<QApplication>(argc, argv);
        if (m_coreInteraction == nullptr) {
            m_coreInteraction = new CoreInteraction;
        }
        if (m_core == nullptr) {
            m_core = new Core;
        }
        if (m_interaction == nullptr) {
            m_interaction = new CoreInteraction;
        }
        if (m_dbus == nullptr) {
            m_dbus = new Dbus;
        }
    }
    static void cleanupTestCase()
    {
        m_app.reset();
        if (m_coreInteraction) {
            delete m_coreInteraction;
            m_coreInteraction = nullptr;
        }
    }

    //coreinteraction
    void tst_creatCore();
    void tst_initUiFinish();
    void tst_openImage();
    void tst_getInstance();
    void tst_coreOperateTooOften();
    void tst_nextImage();
    void tst_backImage();
    void tst_changeWidgetSize();
    void tst_getDefaultProportion();
    void tst_watchOriginalImage();
    void tst_watchAutoImage();
    void tst_rotate();
    void tst_flipH();
    void tst_flipV();
    void tst_deleteImage();
    void tst_setAsBackground();
    void tst_openImageInfile();
    void tst_changeOpenIcon();
    void tst_close();
    void tst_reName();
    void tst_printImage();
    void tst_cutImage();
    void tst_getText();
    void tst_cutRegion();
    void tst_startChange();
    void tst_releasePos();
    void tst_releaseCutChange();
    void tst_exitCut();
    void tst_needSaveCutImage();
    void tst_needSaveAs();
    void tst_qmlStartCutDisplay();
    void tst_qmlStartNaviDisplay();
    void tst_qmlClickNaviDisplay();
    void tst_qmlTiffPageNavi();
    void tst_qmlTiffImagePage();
    void tst_clickCutFullViableImage();
    void tst_startMark();
    void tst_toMarkImage();
    void tst_markUndo();
    void tst_markSave();
    void tst_markClear();
    void tst_replaceBackground();
    void tst_picMatting();
    void tst_needSaveReplaceImage();
    void tst_mattingRotate();
    void tst_mattingFlipH();
    void tst_mattingFlipV();
    void tst_saveCutImage();
    void tst_scanImage();
    void tst_needSaveScanImage();
    void tst_printScanImage();
    void tst_scannerSavePDF();
    void tst_scannerAddWatermark();
    void tst_scannerRotate();
    void tst_scannerFlipH();
    void tst_scannerFlipV();
    void tst_scannerSaveCutImage();

    //dbus
    void tst_fullScreen();

    //core
    void tst_initDbusFileAddress();
    void tst_initDbusApi();
    void tst_findAllImageFromDir();
    void tst_tiffOperate();
    void tst_processingFullscreen();
    void tst_changeImageShowSize();
    void tst_tiffOperateFinish();
    void tst_flipImage();
    void tst_getAlbumModel();
    void tst_changeImage();
    void tst_coreGetDefaultProportion();
    void tst_coreReplaceBackground();
    void tst_coreScanImage();
    void tst_coreMarkImage();
    void tst_qmlCutImageDisplay();
    void tst_apiFunction();
    void tst_toPrintImage();
    void tst_processingCommand();

private:
    static std::unique_ptr<QApplication> m_app;
    static CoreInteraction *m_coreInteraction;
    static Core *m_core;
    static Interaction *m_interaction;
    static Dbus *m_dbus;
};

std::unique_ptr<QApplication> MyTest::m_app;
CoreInteraction *MyTest::m_coreInteraction = nullptr;
Core *MyTest::m_core = nullptr;
Interaction *MyTest::m_interaction = nullptr;
Dbus *MyTest::m_dbus = nullptr;

MyTest::MyTest()
{
}

MyTest::~MyTest()
{
}

void MyTest::tst_getInstance()
{
    Interaction *testInteraction = m_interaction->getInstance();
    QVERIFY(testInteraction!= nullptr);
}
void MyTest::tst_creatCore()
{
    QStringList list= {"kylin-photo-viewer"};
    m_coreInteraction->creatCore(list);
    QVERIFY(m_coreInteraction->m_isCoreInit);
    QThread* firstThread = m_coreInteraction->m_thread;
    m_coreInteraction->creatCore(list);
    QCOMPARE(m_coreInteraction->m_thread, firstThread);
    QVERIFY(m_coreInteraction->m_isCoreInit);
    m_coreInteraction->m_thread->quit();
    m_coreInteraction->m_thread->wait();
}

void MyTest::tst_initUiFinish()
{
    m_coreInteraction->m_needStartWithOpenImagePath = true;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::startWithOpenImage);
    m_coreInteraction->initUiFinish();
    QCOMPARE(spy.count(), 1);
}

void MyTest::tst_openImage()
{
    QString path = "/home/sufang/test.bmp";
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreOpenImage);
    m_coreInteraction->openImage(path);
    QCOMPARE(spy.count(), 1);
}

void MyTest::tst_coreOperateTooOften()
{
    QVERIFY(!m_coreInteraction->coreOperateTooOften());
}

void MyTest::tst_nextImage()
{
    auto originalAllowChangeImage = Variable::g_allowChangeImage;
    Variable::g_allowChangeImage = false;
    QSignalSpy spy1(m_coreInteraction, &CoreInteraction::coreChangeImage);
    m_coreInteraction->nextImage();
    QCOMPARE(spy1.count(), 0);
    Variable::g_allowChangeImage = originalAllowChangeImage;

    Variable::g_allowChangeImage = true;
    QSignalSpy spy2(m_coreInteraction, &CoreInteraction::coreChangeImage);
    m_coreInteraction->nextImage();
    QCOMPARE(spy2.count(), 1);
    Variable::g_allowChangeImage = originalAllowChangeImage;

    m_core->changeImage(Enums::NEXT_IMAGE);
}

void MyTest::tst_backImage()
{
    auto originalAllowChangeImage = Variable::g_allowChangeImage;
    Variable::g_allowChangeImage = false;
    QSignalSpy spy1(m_coreInteraction, &CoreInteraction::coreChangeImage);
    m_coreInteraction->backImage();
    QCOMPARE(spy1.count(), 0);
    Variable::g_allowChangeImage = originalAllowChangeImage;

    Variable::g_allowChangeImage = true;
    QSignalSpy spy2(m_coreInteraction, &CoreInteraction::coreChangeImage);
    m_coreInteraction->backImage();
    QCOMPARE(spy2.count(), 1);
    Variable::g_allowChangeImage = originalAllowChangeImage;

    m_core->changeImage(Enums::BACK_IMAGE);
}

void MyTest::tst_changeWidgetSize()
{
    QSize size(1080, 720);
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreChangeWidgetSize);
    m_coreInteraction->changeWidgetSize(size);
    QCOMPARE(spy.count(), 1);
    m_core->changeWidgetSize(size);
}

void MyTest::tst_getDefaultProportion()
{
    QSize size(1080, 720);
    QSignalSpy spy1(m_coreInteraction, &CoreInteraction::coreGetDefaultProportion);
    m_coreInteraction->getDefaultProportion(size);
    QCOMPARE(spy1.count(), 1);
}

void MyTest::tst_watchOriginalImage()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreChangeImageShowSize);
    m_coreInteraction->watchOriginalImage();
    QCOMPARE(spy.count(), 1);
    m_core->changeImageShowSize(ImageShowStatus::ORIGIN, QPoint());
}
void MyTest::tst_watchAutoImage()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreChangeImageShowSize);
    m_coreInteraction->watchAutoImage();
    QCOMPARE(spy.count(), 1);
    m_core->changeImageShowSize(ImageShowStatus::AUTO, QPoint());
}

void MyTest::tst_rotate()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreFlip);
    m_coreInteraction->rotate(true);
    m_core->flipImage(Processing::clockwise);
    QCOMPARE(spy.count(), 1);
    m_coreInteraction->rotate(false);
    m_core->flipImage(Processing::counterclockwise);
    QCOMPARE(spy.count(), 2);
}
void MyTest::tst_flipH()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreFlip);
    m_coreInteraction->flipH();
    m_core->flipImage(Processing::horizontal);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_flipV()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreFlip);
    m_coreInteraction->flipV();
    m_core->flipImage(Processing::vertical);
    QCOMPARE(spy.count(), 1);
}

void MyTest::tst_deleteImage()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreDeleteImage);
    m_coreInteraction->deleteImage();
    m_core->deleteImage();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_setAsBackground()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreSetAsBackground);
    m_coreInteraction->setAsBackground();
    m_core->setAsBackground();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_openImageInfile()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreOpenInfile);
    m_coreInteraction->openImageInfile();
    m_core->openInfile();
    QCOMPARE(spy.count(), 1);
}

void MyTest::tst_changeOpenIcon()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreChangeOpenIcon);
    m_coreInteraction->changeOpenIcon("ukui-dark");
    m_core->changeOpenIcon("ukui-dark");
    QCOMPARE(spy.count(), 1);
    m_coreInteraction->changeOpenIcon("ukui-light");
    m_core->changeOpenIcon("ukui-light");
    QCOMPARE(spy.count(), 2);
}
void MyTest::tst_close()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreClose);
    m_coreInteraction->close();
    m_core->close();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_reName()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreRename);
    m_coreInteraction->reName("/home/sufang/test.bmp", "/home/sufang/test2.bmp");
    m_core->toCoreChangeName("/home/sufang/test.bmp", "/home/sufang/test2.bmp");
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_printImage()
{
    QPrinter *printer;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::corePrint);
    m_coreInteraction->printImage(printer);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_cutImage()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreCut);
    m_coreInteraction->cutImage();
    m_core->cutImage();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_getText()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreGetText);
    m_coreInteraction->getText();
    m_core->getTextFromImage();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_cutRegion()
{
    QPoint point(10, 20);
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreCutRegion);
    m_coreInteraction->cutRegion(point);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_startChange()
{
    QPoint point(10, 20);
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreStartChange);
    m_coreInteraction->startChange(point);
    m_core->startChange(point);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_releasePos()
{
    QPoint point(100, 200);
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreRealsePos);
    m_coreInteraction->releasePos(point);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_releaseCutChange()
{
    QPoint point(10, 20);
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreReleaseCutChange);
    m_coreInteraction->releaseCutChange(0, point);
    m_core->releaseCutChange(0, point);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_exitCut()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreExitCut);
    m_coreInteraction->exitCut();
    m_core->exitCut();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_needSaveCutImage()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreNeedSaveCutImage);
    m_coreInteraction->needSaveCutImage(20);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_needSaveAs()
{
    QString path = "/home/sufang/test.bmp";
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreNeedSaveas);
    m_coreInteraction->needSaveAs(path);
    m_core->findAllImageFromDir(path);
    m_core->needSaveAsImage(path);
    QCOMPARE(spy.count(), 1);
}

void MyTest::tst_qmlStartCutDisplay()
{
    QPointF startPos(10.0, 20.0);
    QSizeF displaySize(30.0, 40.0);
    double imageScale = 1.5;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreQmlStartCutDisplay);
    m_coreInteraction->qmlStartCutDisplay(startPos, displaySize, imageScale);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_qmlStartNaviDisplay()
{
    QPointF startPos(10.0, 20.0);
    QSizeF displaySize(30.0, 40.0);
    double imageScale = 1.5;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreQmlStartNaviDisplay);
    m_coreInteraction->qmlStartNaviDisplay(startPos, displaySize, imageScale);
    m_core->qmlNavigation(startPos, displaySize, imageScale);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_qmlClickNaviDisplay()
{
    QPointF curMousePos(30.0, 40.0);
    bool needChangeDisplayImage = false;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreQmlClickNavigation);
    m_coreInteraction->qmlClickNaviDisplay(curMousePos, needChangeDisplayImage);
    m_core->qmlClickNavigation(curMousePos, needChangeDisplayImage);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_qmlTiffPageNavi()
{
    int pageNum = 1;
    QPointF startPos(10.0, 20.0);
    QSizeF displaySize(30.0, 40.0);
    double imageScale = 1.5;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreQmlTiffPageNavi);
    m_coreInteraction->qmlTiffPageNavi(pageNum, startPos, displaySize, imageScale);
    m_core->qmlTiffPage(pageNum, startPos, displaySize, imageScale);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_qmlTiffImagePage()
{
    int pageNum = 1;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreQmlTiffImagePage);
    m_coreInteraction->qmlTiffImagePage(pageNum);
    m_core->qmlTiffImagePage(pageNum);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_clickCutFullViableImage()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreCutFullViableImage);
    m_coreInteraction->clickCutFullViableImage();
    m_core->cutFullViableImage();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_startMark()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreStartMark);
    m_coreInteraction->startMark();
    m_core->startMark();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_toMarkImage()
{
    QVariant var;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreMarkImage);
    m_coreInteraction->toMarkImage(var);
    m_core->markImage(var);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_markUndo()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreMarkUndo);
    m_coreInteraction->markUndo();
    m_core->markUndo();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_markSave()
{
    QString path = "/home/sufang/test.bmp";
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreMarkSave);
    m_coreInteraction->markSave(path);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_markClear()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreMarkClear);
    m_coreInteraction->markClear();
    m_core->markClear();
    QCOMPARE(spy.count(), 1);
}

void MyTest::tst_replaceBackground()
{
    uint b = 0; uint g = 0; uint r = 0; uint a = 0; bool isFirstEnter = true;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreReplaceBackground);
    m_coreInteraction->replaceBackground(b, g, r, a, isFirstEnter);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_picMatting()
{
    QImage pic("/home/sufang/test.bmp");
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::corePicMatting);
    m_coreInteraction->picMatting(pic);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_needSaveReplaceImage()
{
    QString path = "/home/sufang/test.bmp";
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreNeedSaveReplaceImage);
    m_coreInteraction->needSaveReplaceImage(path);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_mattingRotate()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreMattingFlip);
    m_coreInteraction->mattingRotate(true);
    QCOMPARE(spy.count(), 1);
    m_coreInteraction->mattingRotate(false);
    QCOMPARE(spy.count(), 2);
}
void MyTest::tst_mattingFlipH()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreMattingFlip);
    m_coreInteraction->mattingFlipH();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_mattingFlipV()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreMattingFlip);
    m_coreInteraction->mattingFlipV();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_saveCutImage()
{
    int proportion = 50;
    QPoint beginPoint(10, 20);
    QPoint endPoint(30, 40);
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreSaveCutImage);
    m_coreInteraction->saveCutImage(proportion, beginPoint, endPoint);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_scanImage()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreScanImage);
    m_coreInteraction->scanImage();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_needSaveScanImage()
{
    QString path = "/home/sufang/test.bmp";
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreNeedSaveScanImage);
    m_coreInteraction->needSaveScanImage(path);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_printScanImage()
{
    QPrinter *printer;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::corePrintScanImage);
    m_coreInteraction->printScanImage(printer);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_scannerSavePDF()
{
    QPrinter *printer;
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreScannerSavePDF);
    m_coreInteraction->scannerSavePDF(printer);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_scannerAddWatermark()
{
    bool isWatermark = true;
    QString vmContent = "test";
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreScannerAddWatermark);
    m_coreInteraction->scannerAddWatermark(isWatermark, vmContent);
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_scannerRotate()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreScannerFlip);
    m_coreInteraction->scannerRotate(true);
    //m_core->scannerFlip(Processing::clockwise);
    QCOMPARE(spy.count(), 1);
    m_coreInteraction->scannerRotate(false);
    //m_core->scannerFlip(Processing::counterclockwise);
    QCOMPARE(spy.count(), 2);
}
void MyTest::tst_scannerFlipH()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreScannerFlip);
    m_coreInteraction->scannerFlipH();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_scannerFlipV()
{
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreScannerFlip);
    m_coreInteraction->scannerFlipV();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_scannerSaveCutImage()
{
    int proportion = 50;
    QPoint beginPoint(10, 20);
    QPoint endPoint(30, 40);
    QSignalSpy spy(m_coreInteraction, &CoreInteraction::coreScannerSaveCutImage);
    m_coreInteraction->scannerSaveCutImage(proportion, beginPoint, endPoint);
    QCOMPARE(spy.count(), 1);
}

void MyTest::tst_fullScreen()
{
    QSignalSpy spy(m_dbus, &Dbus::processingFullScreen);
    m_dbus->fullScreen();
    QCOMPARE(spy.count(), 1);
}

void MyTest::tst_initDbusFileAddress()
{
    QStringList arguments = {"kylin-photo-viewer", "file:///home/sufang/test.bmp"};
    QString result = m_core->initDbus(arguments);
    QCOMPARE(result, "/home/sufang/test.bmp");
}
void MyTest::tst_initDbusApi()
{
    QStringList arguments = {"kylin-photo-viewer", "-api", "-big"};
    QString result = m_core->initDbus(arguments);
    QCOMPARE(result, "");
}

void MyTest::tst_findAllImageFromDir()
{
    QStringList list = { "/home/sufang/testimage/1.apng",
                         "/home/sufang/testimage/2.tiff",
                         "/home/sufang/testimage/3.tiff",
                         "/home/sufang/testimage/4.svg",
                         "/home/sufang/testimage/5.tga",
                         "/home/sufang/testimage/6.ico",
                         "/home/sufang/testimage/7.bmp",
                         "/home/sufang/testimage/8.jpg",
                         "/home/sufang/testimage/9.gif",
                         "/home/sufang/testimage/10.png",
                         "/home/sufang/testimage/11.xbm",
                         "/home/sufang/testimage/12.xpm",
                         "/home/sufang/testimage/13-异常.tga",
                         "/home/sufang/testimage/14",
                         "/home/sufang/testimage/15.png"};
    for (const QString& str: list) {
        QString path = str;
        m_core->findAllImageFromDir(path);
    }
    QVERIFY(!m_core->m_allImageList.isEmpty());
}

void MyTest::tst_tiffOperate()
{
    QString path = "/home/sufang/testimage/2.tiff";
    m_core->m_nowpath = path;
    m_core->m_tiffProcess.append(Processing::horizontal);
    m_core->tiffOperate();
    QVERIFY(!m_core->m_tiffProcess.isEmpty());
}

void MyTest::tst_processingFullscreen()
{
    QSignalSpy spy(m_core, &Core::fullScreen);
    m_core->processingFullscreen();
    QCOMPARE(spy.count(), 1);
}
void MyTest::tst_changeImageShowSize()
{
    m_core->changeImageShowSize(ImageShowStatus::BIG, QPoint());
    m_core->changeImageShowSize(ImageShowStatus::SMALL, QPoint());
}
void MyTest::tst_tiffOperateFinish()
{
    QString path = "/home/sufang/testimage/2.tiff";
    m_core->tiffOperateFinish(path);
    QCOMPARE(m_core->m_nowpath, path);
}
void MyTest::tst_flipImage()
{
    QStringList list = { "/home/sufang/testimage/1.apng",
                         "/home/sufang/testimage/2.tiff",
                         "/home/sufang/testimage/3.tiff",
                         "/home/sufang/testimage/4.svg",
                         "/home/sufang/testimage/5.tga",
                         "/home/sufang/testimage/6.ico",
                         "/home/sufang/testimage/7.bmp",
                         "/home/sufang/testimage/8.jpg",
                         "/home/sufang/testimage/9.gif",
                         "/home/sufang/testimage/10.png",
                         "/home/sufang/testimage/11.xbm",
                         "/home/sufang/testimage/12.xpm",
                         "/home/sufang/testimage/13-异常.tga",
                         "/home/sufang/testimage/14",
                         "/home/sufang/testimage/15.png",
                         "/home/sufang/testimage/16.ras"};
    for (const QString& str: list) {
        QString path = str;
        m_core->findAllImageFromDir(path);
        m_core->flipImage(Processing::clockwise);
        m_core->flipImage(Processing::counterclockwise);
        m_core->flipImage(Processing::horizontal);
        m_core->flipImage(Processing::vertical);
    }
    m_core->fileChangedNeedUpdated();
    m_core->processLog();
    QVERIFY(m_core->m_imageNeedUpdate);
}
void MyTest::tst_getAlbumModel()
{
    QStandardItemModel *albumModel = m_core->getAlbumModel();
    QCOMPARE(m_core->m_albumModel, albumModel);
}
void MyTest::tst_changeImage()
{
    QString path = "/home/sufang/testimage/10.png";
    m_core->findAllImageFromDir(path);
    QSignalSpy spy(m_core, &Core::updateSideNum);
    m_core->changeImage(Enums::ChamgeImageType::ERROR_IMAGE);
    QCOMPARE(spy.count(), 1);

    m_core->changeImage(Enums::ChamgeImageType::NEXT_IMAGE);

    m_core->changeImage(Enums::ChamgeImageType::BACK_IMAGE);

    m_core->changeImage(0);
}
void MyTest::tst_coreGetDefaultProportion()
{
    QSize size(1080, 720);
    QSignalSpy spy(m_core, &Core::updateDefaultProportion);
    QString path = "/home/sufang/testimage/2.tiff";
    m_core->findAllImageFromDir(path);
    m_core->getDefaultProportion(size);
    QCOMPARE(spy.count(), 1);
    QPrinter printer(QPrinter::HighResolution);
    m_core->toPrintImage(&printer);
}
void MyTest::tst_coreReplaceBackground()
{
    QString path = "/home/sufang/testimage/7.bmp";
    m_core->findAllImageFromDir(path);
    QSignalSpy spy(m_core, &Core::returnReplaceAndOrigImage);
    uint b = 0; uint g = 0; uint r = 0; uint a = 0; bool isFirstEnter = true;
    m_core->replaceBackground(b, g, r, a, isFirstEnter);
    QCOMPARE(spy.count(), 1);
    QImage image("/home/sufang/testimage/10.png");
    m_core->picMatting(image);
    QCOMPARE(spy.count(), 2);
    QSignalSpy spy2(m_core, &Core::returnFlipAndOrigImage);
    m_core->mattingFlip(Processing::horizontal);
    QCOMPARE(spy2.count(), 1);
    m_core->saveCutImage(43, QPoint(181, 0), QPoint(644, 463));
    m_core->mattingFlip(Processing::horizontal);
    QCOMPARE(spy2.count(), 2);
}
void MyTest::tst_coreScanImage()
{
    QString path = "/home/sufang/testimage/7.bmp";
    m_core->findAllImageFromDir(path);
    QSignalSpy spy(m_core, &Core::returnScanAndOrigImage);
    m_core->scanImage();
    QCOMPARE(spy.count(), 1);
    m_core->scannerAddWatermark(false, "");
    QCOMPARE(spy.count(), 2);
    m_core->scannerSaveCutImage(52, QPoint(204, 74), QPoint(720, 446));
    m_core->scannerFlip(Processing::horizontal);
    QPrinter printer(QPrinter::HighResolution);
    m_core->printScanImage(&printer);
    m_core->scannerSavePDF(&printer);

    m_core->scannerAddWatermark(true, "test");
    QCOMPARE(spy.count(), 4);
    m_core->scannerSaveCutImage(52, QPoint(204, 74), QPoint(720, 446));
    m_core->scannerFlip(Processing::horizontal);
    m_core->printScanImage(&printer);
    m_core->scannerSavePDF(&printer);
}

void MyTest::tst_coreMarkImage()
{
    MarkPainterSet recordDate;
    for (int i = 0; i <= 7; i++) {
        recordDate.type = i;
        QVariant var;
        var.setValue<MarkPainterSet>(recordDate);
        m_core->markImage(var);
        QString path = "/home/sufang/testimage/mark.png";
        m_core->markSave(path);
    }
}
void MyTest::tst_qmlCutImageDisplay()
{
    QString path = "/home/sufang/testimage/10.png";
    m_core->findAllImageFromDir(path);
    QPointF startPos(0, 0);
    QSizeF displaySize(1920, 1200);
    double imageScale = 0.56;
    m_core->qmlCutImageDisplay(startPos, displaySize, imageScale);
    m_core->startChange(QPoint(298, 160));
    m_core->cutRegion(QPoint(681, 434));
    m_core->realsePos(QPoint(681, 434));
    m_core->needSaveCutImage(56);
    QVERIFY(!m_core->m_isCutting);
}
void MyTest::tst_apiFunction()
{
    m_core->m_isApi = true;
    m_core->m_apiCmd << "-flip" << "/home/sufang/testimage/14" << "h";
    m_core->apiFunction();
    m_core->m_apiCmd.clear();
    m_core->m_apiCmd << "-flip" << "/home/sufang/testimage/14" << "v";
    m_core->apiFunction();
    m_core->m_apiCmd.clear();
    m_core->m_apiCmd << "-flip" << "/home/sufang/testimage/14" << "c";
    m_core->apiFunction();
    m_core->m_apiCmd.clear();
    m_core->m_isApi = false;
}

void MyTest::tst_toPrintImage()
{
    QString path = "/home/sufang/testimage/2.tiff";
    m_core->findAllImageFromDir(path);
    m_core->getDefaultProportion(QSize(1080, 720));
    QPrinter printer(QPrinter::HighResolution);
    m_core->toPrintImage(&printer);
}

void MyTest::tst_processingCommand()
{
    QStringList cmd;
    cmd << "-next";
    m_core->processingCommand(cmd);
    cmd.clear();
    cmd << "-back";
    m_core->processingCommand(cmd);
    cmd.clear();
    cmd << "-big";
    m_core->processingCommand(cmd);
    cmd.clear();
    cmd << "-small";
    m_core->processingCommand(cmd);
    cmd.clear();
    cmd << "-origin";
    m_core->processingCommand(cmd);
    cmd.clear();
    cmd << "-auto";
    m_core->processingCommand(cmd);
    cmd.clear();
    cmd << "-rotate";
    m_core->processingCommand(cmd);
    cmd.clear();
}

QTEST_APPLESS_MAIN(MyTest)
#include "tst_mytest.moc"
