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

0001 // ct_lvtqtw_tool_reparent_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_lvtldr_lakosiannode.h>
0023 #include <ct_lvtqtc_testing_utils.h>
0024 #include <ct_lvtqtc_tool_reparent_entity.h>
0025 #include <ct_lvttst_fixture_qt.h>
0026 
0027 #include <ct_lvtldr_nodestoragetestutils.h>
0028 #include <ct_lvttst_tmpdir.h>
0029 
0030 #include <memory>
0031 
0032 using namespace Codethink::lvtqtc;
0033 using namespace Codethink::lvtldr;
0034 using namespace Codethink::lvtclr;
0035 using namespace Codethink::lvtshr;
0036 
0037 TEST_CASE_METHOD(QTApplicationFixture, "Reparent entity")
0038 {
0039     auto tmpDir = TmpDir{"reparent_test"};
0040     auto dbPath = tmpDir.path() / "codedb.db";
0041     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0042 
0043     GraphicsViewWrapperForTesting gv{nodeStorage};
0044     gv.show();
0045 
0046     auto *a = nodeStorage.addPackage("a", "a").value();
0047     auto *b = nodeStorage.addPackage("b", "b").value();
0048     auto *aa = nodeStorage.addComponent("aa", "aa", a).value();
0049     auto *bb = nodeStorage.addComponent("bb", "bb", b).value();
0050 
0051     gv.moveEntityTo(a->uid(), {0, 0});
0052     gv.moveEntityTo(b->uid(), {100, 0});
0053 
0054     auto tool = ToolReparentEntity{&gv, nodeStorage};
0055     gv.setCurrentTool(&tool);
0056     tool.activate();
0057 
0058     // Basic tool usage
0059     REQUIRE(gv.countEntityChildren(a->uid()) == 1);
0060     REQUIRE(gv.countEntityChildren(b->uid()) == 1);
0061     mouseMoveTo(tool, gv.getEntityPosition(aa->uid()));
0062     mousePressAt(tool, gv.getEntityPosition(aa->uid()));
0063     mouseMoveTo(tool, gv.getEntityPosition(b->uid()));
0064     mouseReleaseAt(tool, gv.getEntityPosition(b->uid()));
0065     REQUIRE(gv.countEntityChildren(a->uid()) == 0);
0066     REQUIRE(gv.countEntityChildren(b->uid()) == 2);
0067     mouseMoveTo(tool, gv.getEntityPosition(bb->uid()));
0068     mousePressAt(tool, gv.getEntityPosition(bb->uid()));
0069     mouseMoveTo(tool, gv.getEntityPosition(a->uid()));
0070     mouseReleaseAt(tool, gv.getEntityPosition(a->uid()));
0071     REQUIRE(gv.countEntityChildren(a->uid()) == 1);
0072     REQUIRE(gv.countEntityChildren(b->uid()) == 1);
0073 }
0074 
0075 TEST_CASE_METHOD(QTApplicationFixture, "Reparent entity multiple GraphicsScenes")
0076 {
0077     auto tmpDir = TmpDir{"reparent_multiple_gs"};
0078     auto dbPath = tmpDir.path() / "codedb.db";
0079     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0080 
0081     GraphicsViewWrapperForTesting gv{nodeStorage};
0082     gv.show();
0083 
0084     auto *b = nodeStorage.addPackage("b", "b").value();
0085     auto *bb = nodeStorage.addComponent("bb", "bb", b).value();
0086 
0087     // gv2 will only have visibility of 'a' and 'aa'
0088     GraphicsViewWrapperForTesting gv2{nodeStorage};
0089     gv2.show();
0090 
0091     auto *a = nodeStorage.addPackage("a", "a").value();
0092     auto *aa = nodeStorage.addComponent("aa", "aa", a).value();
0093 
0094     gv.moveEntityTo(a->uid(), {0, 0});
0095     gv.moveEntityTo(b->uid(), {100, 0});
0096 
0097     auto tool = ToolReparentEntity{&gv, nodeStorage};
0098     gv.setCurrentTool(&tool);
0099     tool.activate();
0100 
0101     // Basic tool usage
0102     REQUIRE(gv.countEntityChildren(a->uid()) == 1);
0103     REQUIRE(gv.countEntityChildren(b->uid()) == 1);
0104     REQUIRE(gv2.countEntityChildren(a->uid()) == 1);
0105     mouseMoveTo(tool, gv.getEntityPosition(aa->uid()));
0106     mousePressAt(tool, gv.getEntityPosition(aa->uid()));
0107     mouseMoveTo(tool, gv.getEntityPosition(b->uid()));
0108     mouseReleaseAt(tool, gv.getEntityPosition(b->uid()));
0109     REQUIRE(gv2.countEntityChildren(a->uid()) == 0);
0110     REQUIRE(gv.countEntityChildren(a->uid()) == 0);
0111     REQUIRE(gv.countEntityChildren(b->uid()) == 2);
0112     mouseMoveTo(tool, gv.getEntityPosition(bb->uid()));
0113     mousePressAt(tool, gv.getEntityPosition(bb->uid()));
0114     mouseMoveTo(tool, gv.getEntityPosition(a->uid()));
0115     mouseReleaseAt(tool, gv.getEntityPosition(a->uid()));
0116     REQUIRE(gv.countEntityChildren(a->uid()) == 1);
0117     REQUIRE(gv.countEntityChildren(b->uid()) == 1);
0118 
0119     // Because `bb` was not in the gv2 scene in the first place, he won't be shown after being moved to the `a` package.
0120     // The user would have to drag `bb` manually. This is the expected behavior.
0121     REQUIRE(gv2.countEntityChildren(a->uid()) == 0);
0122 }