File indexing completed on 2024-05-19 05:42:24
0001 // ct_lvtqtc_undo_add_logicalentity.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_logicalentity.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 logical entity") 0037 { 0038 auto tmpDir = TmpDir{"undo_redo_log_entities"}; 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 auto result = nodeStorage.addLogicalEntity("SomeClass", "SomeClass", aaa, Codethink::lvtshr::UDTKind::Class); 0047 REQUIRE(result.has_value()); 0048 auto *logical_entity = result.value(); 0049 0050 // This GraphicsView is in the heap so that we can control it's lifetime 0051 auto *gv = new GraphicsViewWrapperForTesting{nodeStorage}; 0052 auto *scene = dynamic_cast<GraphicsScene *>(gv->scene()); 0053 scene->loadEntityByQualifiedName(QString::fromStdString(logical_entity->qualifiedName()), QPoint(10, 10)); 0054 0055 gv->show(); 0056 0057 auto undoRedo = UndoManager{}; 0058 undoRedo.addUndoCommand(new UndoAddLogicalEntity(qobject_cast<GraphicsScene *>(gv->scene()), 0059 {10, 10}, 0060 "SomeClass", 0061 "SomeClass", 0062 "aaa", 0063 QtcUtil::UndoActionType::e_Add, 0064 nodeStorage)); 0065 0066 auto logicalEntityUUID = logical_entity->uid(); 0067 0068 logical_entity->setNotes("Some test notes"); 0069 REQUIRE(nodeStorage.findById(logicalEntityUUID)); 0070 REQUIRE(gv->hasEntityWithId(logicalEntityUUID)); 0071 0072 undoRedo.undo(); 0073 // Component is removed from both nodeStorage and the GraphicsScene 0074 REQUIRE(nodeStorage.findById(logicalEntityUUID) == nullptr); 0075 REQUIRE_FALSE(gv->hasEntityWithId(logicalEntityUUID)); 0076 0077 undoRedo.redo(); 0078 // Component is added back to nodeStorage and GraphicsScene 0079 logical_entity = nodeStorage.findByQualifiedName(DiagramType::ClassType, "SomeClass"); 0080 REQUIRE(logical_entity); 0081 logicalEntityUUID = aaa->uid(); 0082 REQUIRE(nodeStorage.findById(logicalEntityUUID)); 0083 REQUIRE(gv->hasEntityWithId(logicalEntityUUID)); 0084 REQUIRE(logical_entity->notes() == "Some test notes"); 0085 0086 // Undo/Redo must work even if the GraphicsScene gets deleted 0087 delete gv; 0088 REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ClassType, "SomeClass")); 0089 undoRedo.undo(); 0090 REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ClassType, "SomeClass") == nullptr); 0091 undoRedo.redo(); 0092 REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ClassType, "SomeClass")); 0093 }