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

0001 // ct_lvtqtc_undo_load_entity.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_lvtclr_colormanagement.h>
0023 #include <ct_lvtldr_lakosiannode.h>
0024 #include <ct_lvtldr_nodestoragetestutils.h>
0025 #include <ct_lvtqtc_graphicsscene.h>
0026 #include <ct_lvtqtc_testing_utils.h>
0027 #include <ct_lvtqtc_undo_load_entity.h>
0028 #include <ct_lvttst_fixture_qt.h>
0029 #include <ct_lvttst_tmpdir.h>
0030 
0031 #include <memory>
0032 
0033 using namespace Codethink::lvtqtc;
0034 using namespace Codethink::lvtldr;
0035 using namespace Codethink::lvtclr;
0036 using namespace Codethink::lvtshr;
0037 
0038 TEST_CASE_METHOD(QTApplicationFixture, "Undo/Redo Load Components")
0039 {
0040     auto tmpDir = TmpDir{"undo_redo_load_comps"};
0041     auto dbPath = tmpDir.path() / "codedb.db";
0042     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0043 
0044     auto *a = nodeStorage.addPackage("a", "a").value();
0045     auto *aa = nodeStorage.addPackage("aa", "aa", a).value();
0046     auto *aaa = nodeStorage.addComponent("aaa", "aaa", aa).value();
0047 
0048     GraphicsViewWrapperForTesting gv{nodeStorage};
0049     auto *scene = dynamic_cast<GraphicsScene *>(gv.scene());
0050     scene->loadEntityByQualifiedName(QString::fromStdString(aaa->qualifiedName()), QPoint(10, 10));
0051     gv.show();
0052 
0053     auto *aa_node = scene->entityByQualifiedName(aa->qualifiedName());
0054     REQUIRE(aa_node);
0055 
0056     auto undoRedo = UndoManager{};
0057     undoRedo.addUndoCommand(new UndoLoadEntity(qobject_cast<GraphicsScene *>(gv.scene()),
0058                                                aa->uid(),
0059                                                GraphicsScene::UnloadDepth::Entity,
0060                                                QtcUtil::UndoActionType::e_Remove));
0061 
0062     aa_node = scene->entityByQualifiedName(aa->qualifiedName());
0063     REQUIRE_FALSE(aa_node);
0064 
0065     undoRedo.undo();
0066 
0067     aa_node = scene->entityByQualifiedName(aa->qualifiedName());
0068     REQUIRE(aa_node);
0069 
0070     undoRedo.redo();
0071 
0072     aa_node = scene->entityByQualifiedName(aa->qualifiedName());
0073     REQUIRE_FALSE(aa_node);
0074 
0075     undoRedo.undo();
0076     aa_node = scene->entityByQualifiedName(aa->qualifiedName());
0077     REQUIRE(aa_node);
0078 
0079     auto *aaa_node = scene->entityByQualifiedName(aaa->qualifiedName());
0080     REQUIRE_FALSE(aaa_node);
0081 
0082     undoRedo.addUndoCommand(new UndoLoadEntity(qobject_cast<GraphicsScene *>(gv.scene()),
0083                                                aa->uid(),
0084                                                GraphicsScene::UnloadDepth::Children,
0085                                                QtcUtil::UndoActionType::e_Add));
0086 
0087     aaa_node = scene->entityByQualifiedName(aaa->qualifiedName());
0088     REQUIRE(aaa_node);
0089 
0090     undoRedo.undo();
0091 
0092     aaa_node = scene->entityByQualifiedName(aaa->qualifiedName());
0093     REQUIRE_FALSE(aaa_node);
0094 
0095     undoRedo.redo();
0096 
0097     aaa_node = scene->entityByQualifiedName(aaa->qualifiedName());
0098     REQUIRE(aaa_node);
0099 }