File indexing completed on 2024-05-19 05:42:24

0001 // ct_lvtqtc_undo_move.t.cpp                              -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <catch2-local-includes.h>
0021 
0022 #include <ct_lvtldr_lakosiannode.h>
0023 #include <ct_lvtqtc_graphicsscene.h>
0024 #include <ct_lvtqtc_testing_utils.h>
0025 #include <ct_lvtqtc_undo_move.h>
0026 #include <ct_lvttst_fixture_qt.h>
0027 
0028 #include <ct_lvtldr_nodestoragetestutils.h>
0029 #include <ct_lvttst_tmpdir.h>
0030 
0031 #include <QtTest/QTest>
0032 #include <memory>
0033 
0034 using namespace Codethink::lvtqtc;
0035 using namespace Codethink::lvtldr;
0036 using namespace Codethink::lvtclr;
0037 using namespace Codethink::lvtshr;
0038 
0039 TEST_CASE_METHOD(QTApplicationFixture, "Undo/Redo move")
0040 {
0041     auto tmpDir = TmpDir{"undo_redo_move"};
0042     auto dbPath = tmpDir.path() / "codedb.db";
0043     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0044 
0045     auto *a = nodeStorage.addPackage("a", "a").value();
0046     auto *aa = nodeStorage.addPackage("aa", "aa", a).value();
0047     auto *ab = nodeStorage.addPackage("ab", "ab", a).value();
0048 
0049     // This GraphicsView is in the heap so that we can control it's lifetime
0050     auto *gv = new GraphicsViewWrapperForTesting{nodeStorage};
0051     auto *scene = dynamic_cast<GraphicsScene *>(gv->scene());
0052     scene->loadEntityByQualifiedName(QString::fromStdString(ab->qualifiedName()), QPoint(10, 10));
0053     scene->loadEntityByQualifiedName(QString::fromStdString(aa->qualifiedName()), QPoint(100, 100));
0054 
0055     scene->enableLayoutUpdates();
0056     scene->reLayout();
0057     gv->show();
0058 
0059     LakosEntity *entityAA = scene->entityByQualifiedName("aa");
0060     auto undoRedo = UndoManager{};
0061     undoRedo.addUndoCommand(new UndoMove(scene, aa->qualifiedName(), QPoint(10, 10), QPoint(20, 20)));
0062 
0063     undoRedo.undo();
0064     QTest::qWait(1000);
0065     REQUIRE(entityAA->pos() == QPoint(10, 10));
0066     undoRedo.redo();
0067     QTest::qWait(1000);
0068 
0069     REQUIRE(entityAA->pos() == QPoint(20, 20));
0070 
0071     // Calling undo/redo after gv dies won't crash
0072     delete gv;
0073     undoRedo.undo();
0074     undoRedo.redo();
0075 }