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

0001 // ct_lvtqtc_undo_add_component.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 #include <catch2-local-includes.h>
0020 
0021 #include <ct_lvtclr_colormanagement.h>
0022 #include <ct_lvtldr_nodestoragetestutils.h>
0023 #include <ct_lvtqtc_graphicsscene.h>
0024 #include <ct_lvtqtc_testing_utils.h>
0025 #include <ct_lvtqtc_undo_add_component.h>
0026 #include <ct_lvttst_fixture_qt.h>
0027 #include <ct_lvttst_tmpdir.h>
0028 
0029 #include <memory>
0030 
0031 using namespace Codethink::lvtqtc;
0032 using namespace Codethink::lvtldr;
0033 using namespace Codethink::lvtclr;
0034 using namespace Codethink::lvtshr;
0035 
0036 TEST_CASE_METHOD(QTApplicationFixture, "Undo/Redo add component")
0037 {
0038     auto tmpDir = TmpDir{"undoredo_add_comp"};
0039     auto dbPath = tmpDir.path() / "codedb.db";
0040     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0041 
0042     auto *a = nodeStorage.addPackage("a", "a").value();
0043     auto *aa = nodeStorage.addPackage("aa", "aa", a).value();
0044     auto *aaa = nodeStorage.addComponent("aaa", "aaa", aa).value();
0045 
0046     // This GraphicsView is in the heap so that we can control it's lifetime
0047     auto *gv = new GraphicsViewWrapperForTesting{nodeStorage};
0048     auto *scene = dynamic_cast<GraphicsScene *>(gv->scene());
0049     scene->loadEntityByQualifiedName(QString::fromStdString(aaa->qualifiedName()), QPoint(100, 100));
0050 
0051     gv->show();
0052 
0053     auto undoRedo = UndoManager{};
0054     undoRedo.addUndoCommand(new UndoAddComponent(qobject_cast<GraphicsScene *>(gv->scene()),
0055                                                  {10, 10},
0056                                                  "aaa",
0057                                                  "aaa",
0058                                                  "aa",
0059                                                  QtcUtil::UndoActionType::e_Add,
0060                                                  nodeStorage));
0061 
0062     auto aaaUid = aaa->uid();
0063 
0064     aaa->setNotes("Some test notes");
0065     REQUIRE(nodeStorage.findById(aaaUid));
0066     REQUIRE(gv->hasEntityWithId(aaaUid));
0067     REQUIRE(aaa->notes() == "Some test notes");
0068 
0069     undoRedo.undo();
0070 
0071     // Component is removed from both nodeStorage and the GraphicsScene
0072     REQUIRE(nodeStorage.findById(aaaUid) == nullptr);
0073     REQUIRE_FALSE(gv->hasEntityWithId(aaaUid));
0074 
0075     undoRedo.redo();
0076 
0077     // Component is added back to nodeStorage and GraphicsScene
0078     aaa = nodeStorage.findByQualifiedName(DiagramType::ComponentType, "aaa");
0079     REQUIRE(aaa);
0080     aaaUid = aaa->uid();
0081     REQUIRE(nodeStorage.findById(aaaUid));
0082     REQUIRE(gv->hasEntityWithId(aaaUid));
0083     REQUIRE(aaa->notes() == "Some test notes");
0084 
0085     // Undo/Redo must work even if the GraphicsScene gets deleted
0086     delete gv;
0087 
0088     REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ComponentType, "aaa"));
0089     undoRedo.undo();
0090     REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ComponentType, "aaa") == nullptr);
0091     undoRedo.redo();
0092     REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ComponentType, "aaa"));
0093 }