File indexing completed on 2024-05-19 05:42:22
0001 // ct_lvtqtc_tool_add_logical_relation.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_lakosiannode.h> 0023 #include <ct_lvtldr_nodestoragetestutils.h> 0024 #include <ct_lvtqtc_graphicsscene.h> 0025 #include <ct_lvtqtc_testing_utils.h> 0026 #include <ct_lvtqtc_tool_add_logical_relation.h> 0027 #include <ct_lvttst_fixture_qt.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 template<typename TOOL_TYPE> 0038 void runTestOnTool(QTApplicationFixture *self) 0039 { 0040 auto tmpDir = TmpDir{"add_log_relation"}; 0041 auto dbPath = tmpDir.path() / "codedb.db"; 0042 auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath); 0043 0044 GraphicsViewWrapperForTesting gv{nodeStorage}; 0045 gv.setFixedWidth(800); 0046 gv.setFixedHeight(600); 0047 auto *pkg = nodeStorage.addPackage("pkg", "pkg").value(); 0048 auto *abc = nodeStorage.addComponent("abc", "pkg/abc", pkg).value(); 0049 auto *Polygon = nodeStorage.addLogicalEntity("Polygon", "abc::Polygon", abc, UDTKind::Class).value(); 0050 auto *Shape = nodeStorage.addLogicalEntity("Shape", "abc::Shape", abc, UDTKind::Class).value(); 0051 auto *def = nodeStorage.addComponent("def", "pkg/def", pkg).value(); 0052 auto *Square = nodeStorage.addLogicalEntity("Square", "def::Square", def, UDTKind::Class).value(); 0053 auto *utl = nodeStorage.addPackage("utl", "utl").value(); 0054 auto *utl_stuff = nodeStorage.addComponent("stuff", "utl/stuff", utl).value(); 0055 auto *Util = nodeStorage.addLogicalEntity("Util", "stuff::Util", utl_stuff, UDTKind::Class).value(); 0056 gv.show(); 0057 qobject_cast<GraphicsScene *>(gv.scene())->runLayoutAlgorithm(); 0058 0059 auto tool = TOOL_TYPE{&gv, nodeStorage}; 0060 tool.setProperty("debug", true); 0061 std::string lastErrorMsg; 0062 QObject::connect(&tool, &TOOL_TYPE::sendMessage, &tool, [&lastErrorMsg](const QString& s) { 0063 lastErrorMsg = s.toStdString(); 0064 qDebug() << QString::fromStdString(lastErrorMsg); 0065 }); 0066 0067 // Using the tool an arbitrary place won't do anything (including no crashing) 0068 tool.activate(); 0069 REQUIRE_FALSE(mousePressAt(tool, {500, 100})); 0070 mouseReleaseAt(tool, {500, 100}); 0071 tool.deactivate(); 0072 0073 gv.moveEntityTo(Polygon->uid(), {0, 0}); 0074 gv.moveEntityTo(Shape->uid(), {100, 100}); 0075 0076 // Basic tool usage 0077 tool.activate(); 0078 REQUIRE_FALSE(Polygon->hasProvider(Shape)); 0079 mousePressAt(tool, gv.getEntityPosition(Polygon->uid())); 0080 mouseReleaseAt(tool, gv.getEntityPosition(Polygon->uid())); 0081 0082 mousePressAt(tool, gv.getEntityPosition(Shape->uid())); 0083 mouseReleaseAt(tool, gv.getEntityPosition(Shape->uid())); 0084 REQUIRE(Polygon->hasProvider(Shape)); 0085 REQUIRE(lastErrorMsg.empty()); 0086 0087 // Add a relation between different packages 0088 nodeStorage.addPhysicalDependency(pkg, utl).expect("Unexpected error"); 0089 nodeStorage.addPhysicalDependency(abc, utl_stuff).expect("Unexpected error"); 0090 0091 REQUIRE_FALSE(Shape->hasProvider(Util)); 0092 REQUIRE_FALSE(gv.hasRelationWithId(Shape->uid(), Util->uid())); 0093 mousePressAt(tool, gv.getEntityPosition(Shape->uid())); 0094 mouseReleaseAt(tool, gv.getEntityPosition(Shape->uid())); 0095 mousePressAt(tool, gv.getEntityPosition(Util->uid())); 0096 mouseReleaseAt(tool, gv.getEntityPosition(Util->uid())); 0097 REQUIRE(Shape->hasProvider(Util)); 0098 REQUIRE(gv.hasRelationWithId(Shape->uid(), Util->uid())); 0099 0100 REQUIRE_FALSE(abc->hasProvider(def)); 0101 REQUIRE_FALSE(Shape->hasProvider(Square)); 0102 REQUIRE_FALSE(gv.hasRelationWithId(Shape->uid(), Square->uid())); 0103 mousePressAt(tool, gv.getEntityPosition(Shape->uid())); 0104 mouseReleaseAt(tool, gv.getEntityPosition(Shape->uid())); 0105 mousePressAt(tool, gv.getEntityPosition(Square->uid())); 0106 mouseReleaseAt(tool, gv.getEntityPosition(Square->uid())); 0107 REQUIRE(abc->hasProvider(def)); 0108 REQUIRE(Shape->hasProvider(Square)); 0109 REQUIRE(gv.hasRelationWithId(Shape->uid(), Square->uid())); 0110 } 0111 0112 TEST_CASE_METHOD(QTApplicationFixture, "Add logical relation") 0113 { 0114 runTestOnTool<ToolAddUsesInTheInterface>(this); 0115 runTestOnTool<ToolAddUsesInTheImplementation>(this); 0116 runTestOnTool<ToolAddIsARelation>(this); 0117 }