/***************************************************************************
 *   This file is part of awesome-widgets                                  *
 *                                                                         *
 *   awesome-widgets 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 (at your option) any later version.                       *
 *                                                                         *
 *   awesome-widgets 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 awesome-widgets. If not, see http://www.gnu.org/licenses/  *
 ***************************************************************************/

#include "testextupgrade.h"

#include <QtTest>

#include "awtestlibrary.h"
#include "extupgrade.h"


void TestExtUpgrade::initTestCase()
{
    AWTestLibrary::init();
    randomStrings = AWTestLibrary::randomStringList();
    cmd = QString("echo -e '%1'").arg(randomStrings.join("\n"));

    extUpgrade = new ExtUpgrade(this);
    extUpgrade->setInterval(1);
    extUpgrade->setExecutable(cmd);
    extUpgrade->setNumber(0);

    extUpgrade->run();
}


void TestExtUpgrade::cleanupTestCase() {}


void TestExtUpgrade::test_values()
{
    QCOMPARE(extUpgrade->interval(), 1);
    QCOMPARE(extUpgrade->number(), 0);
    QCOMPARE(extUpgrade->executable(), cmd);
}


void TestExtUpgrade::test_run()
{
    // init spy
    QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &)));
    auto firstValue = extUpgrade->run();

    // check values
    QVERIFY(spy.wait(5000));
    auto arguments = spy.takeFirst();

    QCOMPARE(firstValue[extUpgrade->tag("pkgcount")].toInt(), 0);
    QCOMPARE(arguments.at(0).toHash()[extUpgrade->tag("pkgcount")].toInt(), randomStrings.count());
}


void TestExtUpgrade::test_null()
{
    auto null = AWTestLibrary::randomInt(randomStrings.count());
    extUpgrade->setNull(null);
    QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &)));
    extUpgrade->run();

    // check values
    QVERIFY(spy.wait(5000));
    auto arguments = spy.takeFirst();
    QCOMPARE(arguments.at(0).toHash()[extUpgrade->tag("pkgcount")].toInt(), randomStrings.count() - null);
}


void TestExtUpgrade::test_filter()
{
    auto filters = AWTestLibrary::randomSelect(randomStrings);
    extUpgrade->setFilter(QString("(^%1$)").arg(filters.join("$|^")));
    // init spy
    QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &)));
    extUpgrade->run();

    // check values
    QVERIFY(spy.wait(5000));
    auto arguments = spy.takeFirst();
    QCOMPARE(arguments.at(0).toHash()[extUpgrade->tag("pkgcount")].toInt(), filters.count());
}


void TestExtUpgrade::test_copy()
{
    auto newExtUpgrade = extUpgrade->copy("/dev/null", 1);

    QCOMPARE(newExtUpgrade->interval(), extUpgrade->interval());
    QCOMPARE(newExtUpgrade->executable(), extUpgrade->executable());
    QCOMPARE(newExtUpgrade->filter(), extUpgrade->filter());
    QCOMPARE(newExtUpgrade->null(), extUpgrade->null());
    QCOMPARE(newExtUpgrade->number(), 1);

    newExtUpgrade->deleteLater();
}


QTEST_MAIN(TestExtUpgrade);
