#include "contentpresenttest.h"
#include <QtTest/QTest>

using namespace TestCase;


ContentPresentTest::ContentPresentTest(MakeTools::FilePresent *view)
    : inst(view)
{

}

void ContentPresentTest::test_Suffixes()
{
    QVERIFY(inst->suffixes().count());
}

void ContentPresentTest::test_NewInst_Widget()
{
    auto new_inst = inst->newInst();
    QVERIFY(new_inst != nullptr);
    QVERIFY(typeid (new_inst) == typeid(inst));
    QVERIFY(new_inst->widget() != nullptr);
    delete new_inst;
}

void ContentPresentTest::test_ApplySetting()
{
    auto ninst = inst->newInst();
    ninst->applySetting("新命名");
    QVERIFY(ninst->name() == QString("新命名"));
    delete ninst;
}

void ContentPresentTest::test_Create_Load_SaveAs()
{
    QDir curr("./");
    for(auto &s : inst->suffixes()){
        if(s == "*")
            continue;

        auto files = curr.entryInfoList(QStringList() << s, QDir::Files);
        for(auto &x:files)
            curr.remove(x.fileName());

        auto mark = inst->create(curr, "新建测试文档", s);
        QVERIFY(std::get<0>(mark) || QFileInfo::exists(std::get<1>(mark)));

        mark = inst->create(curr, "新建测试文档", s);
        QVERIFY(!std::get<0>(mark));

        auto nins = inst->newInst();
        QVERIFY(nins->relativeTargetPath(curr) == "");
        QVERIFY(nins->absoluteTargetPath() == "");
        nins->load(QFileInfo(curr.absoluteFilePath(std::get<1>(mark))));
        QVERIFY(nins->relativeTargetPath(curr) != "");
        QVERIFY(nins->absoluteTargetPath() != "");
        auto a = std::get<1>(mark);
        auto b = nins->relativeTargetPath(curr);
        QVERIFY(a == b || a=="./"+b || "./"+a == b);

        nins->saveAs("./另存测试文档." + s);
        QVERIFY(QFileInfo::exists("./另存测试文档." + s));

        nins->load(QFileInfo("./另存测试文档." + s));
        delete nins;
    }
}
