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