/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QTest>
#include <Qt3DCore/private/qnode_p.h>
#include <Qt3DCore/private/qscene_p.h>

#include <Qt3DRender/QBlitFramebuffer>
#include <Qt3DRender/private/qblitframebuffer_p.h>

#include "testarbiter.h"

class tst_QBlitFrameBuffer: public QObject
{
    Q_OBJECT

public:
    tst_QBlitFrameBuffer() : QObject()
    {
        qRegisterMetaType<Qt3DRender::QRenderTarget*>();
    }

private Q_SLOTS:

    void checkCreationData_data()
    {
        QTest::addColumn<Qt3DRender::QBlitFramebuffer *>("blitFramebuffer");

        Qt3DRender::QBlitFramebuffer *defaultConstructed = new Qt3DRender::QBlitFramebuffer();
        Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget();
        Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget();
        defaultConstructed->setSource(sourceRenderTarget);
        defaultConstructed->setDestination(destinationRenderTarget);
        QTest::newRow("defaultConstructed") << defaultConstructed;
    }

    void checkInitialState()
    {
        // GIVEN
        Qt3DRender::QBlitFramebuffer blitFramebuffer;

        // THEN
        QCOMPARE(blitFramebuffer.source(), nullptr);
        QCOMPARE(blitFramebuffer.destination(), nullptr);
        QCOMPARE(blitFramebuffer.sourceRect(), QRect());
        QCOMPARE(blitFramebuffer.destinationRect(), QRect());
        QCOMPARE(blitFramebuffer.sourceAttachmentPoint(), Qt3DRender::QRenderTargetOutput::AttachmentPoint::Color0);
        QCOMPARE(blitFramebuffer.destinationAttachmentPoint(), Qt3DRender::QRenderTargetOutput::AttachmentPoint::Color0);
        QCOMPARE(blitFramebuffer.interpolationMethod(), Qt3DRender::QBlitFramebuffer::Linear);
    }

    void checkPropertyUpdate()
    {
        // GIVEN
        TestArbiter arbiter;
        QScopedPointer<Qt3DRender::QBlitFramebuffer> blitFramebuffer(new Qt3DRender::QBlitFramebuffer());
        arbiter.setArbiterOnNode(blitFramebuffer.data());

        Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget;
        Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget;

        // sourceRenderTarget
        // WHEN
        blitFramebuffer->setSource(sourceRenderTarget);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // WHEN
        blitFramebuffer->setSource(sourceRenderTarget);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 0);

        // WHEN
        blitFramebuffer->setSource(nullptr);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // destinationRenderTarget
        // WHEN
        blitFramebuffer->setDestination(destinationRenderTarget);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // WHEN
        blitFramebuffer->setDestination(destinationRenderTarget);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 0);

        // WHEN
        blitFramebuffer->setDestination(nullptr);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // sourceRect
        // WHEN
        blitFramebuffer->setSourceRect(QRect(0,0,1,1));

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // WHEN
        blitFramebuffer->setSourceRect(QRect(0,0,1,1));

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 0);

        // WHEN
        blitFramebuffer->setSourceRect(QRect());

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // destinationRect
        blitFramebuffer->setDestinationRect(QRect(0,0,1,1));

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // WHEN
        blitFramebuffer->setDestinationRect(QRect(0,0,1,1));

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 0);

        // WHEN
        blitFramebuffer->setDestinationRect(QRect());

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // sourceAttachmentPoint
        // WHEN
        blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // WHEN
        blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 0);

        // WHEN
        blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color0);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // destinationAttachmentPoint
        // WHEN
        blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();

        // WHEN
        blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 0);

        // WHEN
        blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color0);

        // THEN
        QCOMPARE(arbiter.dirtyNodes().size(), 1);
        QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data());

        arbiter.clear();
    }

    void checkSourceDestReset()
    {
        // GIVEN
        TestArbiter arbiter;
        QScopedPointer<Qt3DRender::QBlitFramebuffer> blitFramebuffer(new Qt3DRender::QBlitFramebuffer());
        arbiter.setArbiterOnNode(blitFramebuffer.data());

        Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget; // no parent
        Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget(sourceRenderTarget); // have a parent

        // WHEN
        blitFramebuffer->setSource(sourceRenderTarget);
        blitFramebuffer->setDestination(destinationRenderTarget);
        QCoreApplication::processEvents();

        // THEN
        QCOMPARE(sourceRenderTarget->parent(), blitFramebuffer.data());
        QCOMPARE(destinationRenderTarget->parent(), sourceRenderTarget); // already set parent must not change

        // WHEN
        delete sourceRenderTarget;

        // THEN
        QVERIFY(!blitFramebuffer->source());
        QVERIFY(!blitFramebuffer->destination()); // gone too since destinationRenderTarget was parented to sourceRenderTarget
    }
};

QTEST_MAIN(tst_QBlitFrameBuffer)

#include "tst_qblitframebuffer.moc"
