File indexing completed on 2024-05-12 05:09:55

0001 /***************************************************************************
0002     Copyright (C) 2021 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "commandtest.h"
0026 #include "../commands/collectioncommand.h"
0027 #include "../document.h"
0028 #include "../translators/tellicoimporter.h"
0029 #include "../collections/bookcollection.h"
0030 #include "../collections/bibtexcollection.h"
0031 #include "../collectionfactory.h"
0032 #include "../images/imagefactory.h"
0033 
0034 #include <KLocalizedString>
0035 
0036 #include <QTest>
0037 #include <QStandardPaths>
0038 #include <QFile>
0039 
0040 QTEST_GUILESS_MAIN( CommandTest )
0041 
0042 void CommandTest::initTestCase() {
0043   QStandardPaths::setTestModeEnabled(true);
0044   KLocalizedString::setApplicationDomain("tellico");
0045   Tellico::ImageFactory::init();
0046   Tellico::RegisterCollection<Tellico::Data::BookCollection> registerBook(Tellico::Data::Collection::Book, "book");
0047 
0048   QVERIFY(m_tempDir.isValid());
0049   m_tempDir.setAutoRemove(true);
0050   auto tempDirName = m_tempDir.path();
0051   m_fileName = tempDirName + QStringLiteral("/with-image.tc");
0052   // copy a collection file that includes an image into the temporary directory
0053   QFile::copy(QFINDTESTDATA("data/with-image.tc"), m_fileName);
0054 }
0055 
0056 void CommandTest::testCollectionReplace() {
0057   Tellico::Data::Document* doc = Tellico::Data::Document::self();
0058   QVERIFY(doc->openDocument(QUrl::fromLocalFile(m_fileName)));
0059   auto docUrl = doc->URL();
0060   QCOMPARE(QUrl::fromLocalFile(m_fileName), docUrl);
0061 
0062   Tellico::Data::CollPtr newColl(new Tellico::Data::BookCollection(true));
0063 
0064   {
0065     auto oldColl = doc->collection();
0066     Tellico::Command::CollectionCommand cmd(Tellico::Command::CollectionCommand::Replace,
0067                                             doc->collection(),
0068                                             newColl);
0069     cmd.redo();
0070     // doc url was erased
0071     QVERIFY(doc->URL() != docUrl);
0072     QCOMPARE(doc->collection(), newColl);
0073 
0074     // now undo it and check that everything returns to what it should be
0075     cmd.undo();
0076     QCOMPARE(Tellico::Data::Document::self()->URL(), docUrl);
0077     QCOMPARE(doc->collection(), oldColl);
0078   }
0079   //  the d'tor should clear the new collection (since the replace was undone)
0080   QVERIFY(newColl->fields().isEmpty());
0081 }
0082 
0083 void CommandTest::testCollectionAppend() {
0084   Tellico::Data::Document* doc = Tellico::Data::Document::self();
0085   QVERIFY(doc->openDocument(QUrl::fromLocalFile(m_fileName)));
0086   auto docUrl = doc->URL();
0087   QCOMPARE(QUrl::fromLocalFile(m_fileName), docUrl);
0088 
0089   auto test = QStringLiteral("test");
0090 
0091   Tellico::Data::CollPtr newColl(new Tellico::Data::BookCollection(true));
0092   Tellico::Data::FieldPtr field1(new Tellico::Data::Field(test, test));
0093   newColl->addField(field1);
0094   Tellico::Data::EntryPtr entry1(new Tellico::Data::Entry(newColl));
0095   newColl->addEntries(entry1);
0096 
0097   {
0098     auto oldColl = doc->collection();
0099     QCOMPARE(oldColl->entryCount(), 1);
0100     QVERIFY(!oldColl->hasField(test));
0101 
0102     Tellico::Command::CollectionCommand cmd(Tellico::Command::CollectionCommand::Append,
0103                                             doc->collection(),
0104                                             newColl);
0105     cmd.redo();
0106     // collection pointer did not change
0107     QCOMPARE(doc->collection(), oldColl);
0108     QCOMPARE(oldColl->entryCount(), 2);
0109     QVERIFY(oldColl->hasField(test));
0110 
0111     // now undo it and check that everything returns to what it should be
0112     cmd.undo();
0113     QCOMPARE(Tellico::Data::Document::self()->URL(), docUrl);
0114     QCOMPARE(doc->collection(), oldColl);
0115     QCOMPARE(oldColl->entryCount(), 1);
0116     QVERIFY(!oldColl->hasField(test));
0117 
0118     cmd.redo();
0119     QCOMPARE(doc->collection(), oldColl);
0120     QCOMPARE(oldColl->entryCount(), 2);
0121     QVERIFY(oldColl->hasField(test));
0122 
0123     cmd.undo();
0124     QCOMPARE(Tellico::Data::Document::self()->URL(), docUrl);
0125     QCOMPARE(doc->collection(), oldColl);
0126     QCOMPARE(oldColl->entryCount(), 1);
0127     QVERIFY(!oldColl->hasField(test));
0128   }
0129 }
0130 
0131 void CommandTest::testCollectionMerge() {
0132   Tellico::Data::Document* doc = Tellico::Data::Document::self();
0133   QVERIFY(doc->openDocument(QUrl::fromLocalFile(m_fileName)));
0134   auto docUrl = doc->URL();
0135   QCOMPARE(QUrl::fromLocalFile(m_fileName), docUrl);
0136 
0137   auto test = QStringLiteral("test");
0138 
0139   Tellico::Data::CollPtr newColl(new Tellico::Data::BookCollection(true));
0140   Tellico::Data::FieldPtr field1(new Tellico::Data::Field(test, test));
0141   newColl->addField(field1);
0142   Tellico::Data::EntryPtr entry1(new Tellico::Data::Entry(newColl));
0143   newColl->addEntries(entry1);
0144 
0145   {
0146     auto oldColl = doc->collection();
0147     QCOMPARE(oldColl->entryCount(), 1);
0148     QVERIFY(!oldColl->hasField(test));
0149 
0150     Tellico::Command::CollectionCommand cmd(Tellico::Command::CollectionCommand::Merge,
0151                                             doc->collection(),
0152                                             newColl);
0153     cmd.redo();
0154     // collection pointer did not change
0155     QCOMPARE(doc->collection(), oldColl);
0156     QCOMPARE(oldColl->entryCount(), 2);
0157     QVERIFY(oldColl->hasField(test));
0158 
0159     // now undo it and check that everything returns to what it should be
0160     cmd.undo();
0161     QCOMPARE(Tellico::Data::Document::self()->URL(), docUrl);
0162     QCOMPARE(doc->collection(), oldColl);
0163     QCOMPARE(oldColl->entryCount(), 1);
0164     QVERIFY(!oldColl->hasField(test));
0165 
0166     cmd.redo();
0167     QCOMPARE(doc->collection(), oldColl);
0168     QCOMPARE(oldColl->entryCount(), 2);
0169     QVERIFY(oldColl->hasField(test));
0170 
0171     cmd.undo();
0172     QCOMPARE(Tellico::Data::Document::self()->URL(), docUrl);
0173     QCOMPARE(doc->collection(), oldColl);
0174     QCOMPARE(oldColl->entryCount(), 1);
0175     QVERIFY(!oldColl->hasField(test));
0176   }
0177 }
0178 
0179 void CommandTest::testBibtexCollectionAppend() {
0180   auto bibtexColl1 = new Tellico::Data::BibtexCollection(true);
0181   // by default includes 12 months
0182   QCOMPARE(bibtexColl1->macroList().count(), 12);
0183   QVERIFY(bibtexColl1->preamble().isEmpty());
0184   Tellico::Data::CollPtr oldColl(bibtexColl1);
0185 
0186   Tellico::Data::Document* doc = Tellico::Data::Document::self();
0187   doc->replaceCollection(oldColl);
0188 
0189   auto test = QStringLiteral("test");
0190 
0191   auto bibtexColl2 = new Tellico::Data::BibtexCollection(true);
0192   bibtexColl2->addMacro(test, test);
0193   bibtexColl2->setPreamble(test);
0194   QCOMPARE(bibtexColl2->macroList().count(), 13);
0195   QCOMPARE(bibtexColl2->preamble(), test);
0196   Tellico::Data::CollPtr newColl(bibtexColl2);
0197   Tellico::Data::FieldPtr field1(new Tellico::Data::Field(test, test));
0198   newColl->addField(field1);
0199   Tellico::Data::EntryPtr entry1(new Tellico::Data::Entry(newColl));
0200   newColl->addEntries(entry1);
0201 
0202   {
0203     auto oldColl = doc->collection();
0204     QCOMPARE(oldColl->entryCount(), 0);
0205     QVERIFY(!oldColl->hasField(test));
0206     QVERIFY(bibtexColl1->preamble().isEmpty());
0207 
0208     Tellico::Command::CollectionCommand cmd(Tellico::Command::CollectionCommand::Append,
0209                                             doc->collection(),
0210                                             newColl);
0211     cmd.redo();
0212     // collection pointer did not change
0213     QCOMPARE(doc->collection(), oldColl);
0214     QCOMPARE(oldColl->entryCount(), 1);
0215     QVERIFY(oldColl->hasField(test));
0216     QCOMPARE(bibtexColl1->macroList().count(), 13);
0217     QCOMPARE(bibtexColl1->preamble(), test);
0218 
0219     // now undo it and check that everything returns to what it should be
0220     cmd.undo();
0221     QCOMPARE(doc->collection(), oldColl);
0222     QCOMPARE(oldColl->entryCount(), 0);
0223     QVERIFY(!oldColl->hasField(test));
0224     QCOMPARE(bibtexColl1->macroList().count(), 12);
0225     QVERIFY(bibtexColl1->preamble().isEmpty());
0226   }
0227 }