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

0001 // ct_lvtqtc_tool_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 
0020 #include <ct_lvtqtc_tool_add_component.h>
0021 
0022 #include <ct_lvtclr_colormanagement.h>
0023 #include <ct_lvtldr_lakosiannode.h>
0024 #include <ct_lvtqtc_graphicsscene.h>
0025 #include <ct_lvtqtc_lakosentity.h>
0026 #include <ct_lvtqtc_testing_utils.h>
0027 #include <ct_lvttst_fixture_qt.h>
0028 
0029 #include <catch2-local-includes.h>
0030 
0031 #include <ct_lvtldr_nodestoragetestutils.h>
0032 #include <ct_lvttst_tmpdir.h>
0033 
0034 #include <QJsonObject>
0035 #include <QLineEdit>
0036 
0037 #include <QtTest/QTest>
0038 
0039 #include <memory>
0040 
0041 using namespace Codethink::lvtqtc;
0042 using namespace Codethink::lvtldr;
0043 using namespace Codethink::lvtclr;
0044 using namespace Codethink::lvtshr;
0045 
0046 TEST_CASE_METHOD(QTApplicationFixture, "Relayout Single Entity")
0047 {
0048     auto tmpDir = TmpDir{"relayout_single_entity"};
0049     auto dbPath = tmpDir.path() / "codedb.db";
0050     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0051 
0052     auto gv = GraphicsViewWrapperForTesting{nodeStorage};
0053     auto *gs = qobject_cast<GraphicsScene *>(gv.scene());
0054     gv.setColorManagement(std::make_shared<ColorManagement>(false));
0055     gv.show();
0056 
0057     auto *pkg = nodeStorage.addPackage("pkg", "pkg", nullptr, gv.scene()).value();
0058     auto comp1 = nodeStorage.addComponent("pkg_comp1", "pkg_comp1", pkg);
0059     REQUIRE_FALSE(comp1.has_error());
0060 
0061     auto comp2 = nodeStorage.addComponent("pkg_comp2", "pkg_comp2", pkg);
0062     REQUIRE_FALSE(comp2.has_error());
0063 
0064     auto *pkg2 = nodeStorage.addPackage("pkg2", "pkg2", nullptr, gv.scene()).value();
0065     REQUIRE(pkg2);
0066 
0067     auto comp3 = nodeStorage.addComponent("pkg_comp3", "pkg_comp3", pkg2);
0068     REQUIRE_FALSE(comp3.has_error());
0069 
0070     auto comp4 = nodeStorage.addComponent("pkg_comp4", "pkg_comp4", pkg2);
0071     REQUIRE_FALSE(comp4.has_error());
0072 
0073     LakosEntity *pkgEntity2 = gs->entityByQualifiedName("pkg2");
0074     LakosEntity *pkgComp1 = gs->entityByQualifiedName("pkg_comp1");
0075     pkgComp1->setPos(-1, -1);
0076     LakosEntity *pkgComp2 = gs->entityByQualifiedName("pkg_comp2");
0077     pkgComp2->setPos(-2, -3);
0078     LakosEntity *pkgComp3 = gs->entityByQualifiedName("pkg_comp3");
0079     pkgEntity2->setPos(200, 0);
0080 
0081     Q_EMIT pkgComp1->toggleSelection();
0082     REQUIRE(gs->selectedItems().count() == 1);
0083 
0084     // clazy:excludeall=lambda-in-connect
0085     bool valid = false;
0086     QObject::connect(gs, &GraphicsScene::selectedEntityChanged, gs, [&valid] {
0087         valid = true;
0088     });
0089 
0090     // This should trigger the selection via the scene, changing the selected entity,
0091     // triggering the above lambda.
0092     Q_EMIT pkgComp3->toggleSelection();
0093     REQUIRE(valid);
0094 
0095     const int original_size = gs->items().size();
0096     REQUIRE(original_size != 0);
0097     // Save the state in json
0098     const QJsonObject json = gs->toJson();
0099 
0100     gs->clearGraph();
0101     // Item "Drag to View"
0102     REQUIRE(gs->items().size() == 1);
0103 
0104     gs->fromJson(json);
0105 
0106     REQUIRE(gs->items().size() == original_size);
0107 }
0108 
0109 TEST_CASE_METHOD(QTApplicationFixture, "Add and remove edges")
0110 {
0111     auto tmpDir = TmpDir{"add_rm_edges"};
0112     auto dbPath = tmpDir.path() / "codedb.db";
0113     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0114 
0115     auto gv = GraphicsViewWrapperForTesting{nodeStorage};
0116     auto *gs = qobject_cast<GraphicsScene *>(gv.scene());
0117     gv.setColorManagement(std::make_shared<ColorManagement>(false));
0118     gv.show();
0119 
0120     auto *pkg1 = nodeStorage.addPackage("pkg", "pkg", nullptr, gv.scene()).value();
0121     REQUIRE(nodeStorage.addComponent("pkg1_comp1", "pkg1_comp1", pkg1).has_value());
0122     REQUIRE(nodeStorage.addComponent("pkg1_comp2", "pkg1_comp2", pkg1).has_value());
0123 
0124     auto *pkgComp1 = gs->entityByQualifiedName("pkg1_comp1");
0125     REQUIRE(pkgComp1);
0126     auto *pkgComp2 = gs->entityByQualifiedName("pkg1_comp2");
0127     REQUIRE(pkgComp2);
0128 
0129     REQUIRE_FALSE(pkgComp1->hasRelationshipWith(pkgComp2));
0130     (void) gs->addPackageDependencyRelation(pkgComp1, pkgComp2);
0131     REQUIRE(pkgComp1->hasRelationshipWith(pkgComp2));
0132     gs->removeEdge(*pkgComp1, *pkgComp2);
0133     REQUIRE_FALSE(pkgComp1->hasRelationshipWith(pkgComp2));
0134 }