/**************************************************************************
 *  This file is part of QXmlEdit                                         *
 *  Copyright (C) 2011 by Luca Bellonda and individual contributors       *
 *    as indicated in the AUTHORS file                                    *
 *  lbellonda _at_ gmail.com                                              *
 *                                                                        *
 * This library is free software; you can redistribute it and/or          *
 * modify it under the terms of the GNU Library General Public            *
 * License as published by the Free Software Foundation; either           *
 * version 2 of the License, or (at your option) any later version.       *
 *                                                                        *
 * This library 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      *
 * Library General Public License for more details.                       *
 *                                                                        *
 * You should have received a copy of the GNU Library General Public      *
 * License along with this library; if not, write to the                  *
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,       *
 * Boston, MA  02110-1301  USA                                            *
 **************************************************************************/
#include "app.h"
#include "testpaste.h"
#include "comparexml.h"
#include "mainwindow.h"
#include "utils.h"
#include "config.h"

#define TEST_FILE_PASTE_INPUT "../test/data/paste_input.xml"
#define TEST_FILE_PASTE_RESULT "../test/data/paste_result.xml"

TestPaste::TestPaste()
{
}


bool TestPaste::testAPaste()
{
    App app;
    if(!app.initNoWindow() ) {
        return false;
    }
    MainWindow mainWindow(false, qApp, app.data());

    mainWindow.loadFile(QString(TEST_FILE_PASTE_INPUT));

    QList<int> selection;
    selection.append(1);
    selection.append(0);
    selection.append(1);
    selection.append(1);
    Element *selectedElement = mainWindow.getRegola()->findElementByArray(selection);
    if(NULL == selectedElement) {
        return false;
    }

    Element *pasteElement = new Element("test", "", NULL);
    if(NULL == pasteElement) {
        return false;
    }
    pasteElement->setText("test");
    pasteElement->addAttribute("attr1", "value");
    TextChunk *newTextChunk = new  TextChunk(false, "text node");
    if(NULL == newTextChunk) {
        delete pasteElement ;
        return false;
    }
    pasteElement->addTextNode(newTextChunk);
    mainWindow.getRegola()->pasteInternals(mainWindow.getMainTreeWidget(), selectedElement, pasteElement, -1);

    delete pasteElement ;
    QByteArray resultData = mainWindow.getRegola()->writeMemory();
    QBuffer outputData(&resultData);
    QApplication::clipboard()->setText(QString(resultData));

    QDomDocument document1;
    QDomDocument document2;
    CompareXML compare;
    if(!compare.loadFileIntoDocument(TEST_FILE_PASTE_RESULT, document1)) {
        return false;
    }
    if(!compare.loadFileIntoDocument(&outputData, document2)) {
        return false;
    }
    bool result = compare.compareDomDocuments(document1, document2);

    return result ;
}
