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

0001 // ct_lvtqtc_tool_add_physical_dependency.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_lakosentity.h>
0026 #include <ct_lvtqtc_testing_utils.h>
0027 #include <ct_lvtqtc_tool_add_physical_dependency.h>
0028 #include <ct_lvttst_fixture_qt.h>
0029 #include <ct_lvttst_tmpdir.h>
0030 
0031 #include <QTest>
0032 #include <memory>
0033 
0034 using namespace Codethink::lvtqtc;
0035 using namespace Codethink::lvtldr;
0036 using namespace Codethink::lvtclr;
0037 using namespace Codethink::lvtshr;
0038 
0039 TEST_CASE_METHOD(QTApplicationFixture, "Add dependency")
0040 {
0041     auto tmpDir = TmpDir{"add_dependency_test"};
0042     auto dbPath = tmpDir.path() / "codedb.db";
0043     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0044 
0045     auto expectAddComponent = [&nodeStorage](const std::string& name, const std::string& qualifiedName, auto *parent) {
0046         auto result = nodeStorage.addComponent(name, qualifiedName, parent);
0047         REQUIRE_FALSE(result.has_error());
0048         return result.value();
0049     };
0050 
0051     auto *a = nodeStorage.addPackage("a", "a").value();
0052     auto *aa = nodeStorage.addPackage("aa", "aa", a).value();
0053     auto *aaa = expectAddComponent("aaa", "aaa", aa);
0054     auto *aab = expectAddComponent("aab", "aab", aa);
0055 
0056     GraphicsViewWrapperForTesting gv{nodeStorage};
0057     //: TODO: Update this call:
0058     auto *scene = dynamic_cast<GraphicsScene *>(gv.scene());
0059     scene->loadEntityByQualifiedName(QString::fromStdString(aab->qualifiedName()), QPoint(10, 10));
0060     scene->loadEntityByQualifiedName(QString::fromStdString(aaa->qualifiedName()), QPoint(100, 100));
0061 
0062     scene->enableLayoutUpdates();
0063     scene->reLayout();
0064 
0065     gv.show();
0066 
0067     auto tool = ToolAddPhysicalDependency{&gv, nodeStorage};
0068     std::string lastMessage;
0069     QObject::connect(&tool,
0070                      &ToolAddPhysicalDependency::sendMessage,
0071                      &tool,
0072                      [&lastMessage](const QString& s, KMessageWidget::MessageType) {
0073                          qDebug() << "Message Received" << s;
0074                          lastMessage = s.toStdString();
0075                      });
0076 
0077     // Using the tool ar arbitrary place won't do anything (including no crashing)
0078     REQUIRE_FALSE(mousePressAt(tool, {0, 0}));
0079     REQUIRE(lastMessage.empty());
0080     QTest::qWait(500);
0081 
0082     // Basic tool usage
0083     tool.activate();
0084     REQUIRE(lastMessage == "Select the source Element");
0085     REQUIRE_FALSE(aaa->hasProvider(aab));
0086     mousePressAt(tool, gv.getEntityPosition(aaa->uid()));
0087     mouseReleaseAt(tool, gv.getEntityPosition(aaa->uid()));
0088 
0089     REQUIRE(lastMessage == "Source element: <strong>aaa</strong>, Select the target Element");
0090 
0091     mousePressAt(tool, gv.getEntityPosition(aab->uid()));
0092     mouseReleaseAt(tool, gv.getEntityPosition(aab->uid()));
0093     tool.deactivate();
0094     REQUIRE(lastMessage.empty());
0095 
0096     tool.activate();
0097     mousePressAt(tool, gv.getEntityPosition(aaa->uid()));
0098     mouseReleaseAt(tool, gv.getEntityPosition(aaa->uid()));
0099     mousePressAt(tool, gv.getEntityPosition(aab->uid()));
0100     mouseReleaseAt(tool, gv.getEntityPosition(aab->uid()));
0101     tool.deactivate();
0102     REQUIRE(lastMessage == "You can't connect aaa to aab, they already have a connection.");
0103     REQUIRE(aaa->hasProvider(aab));
0104 
0105     tool.activate();
0106     mousePressAt(tool, gv.getEntityPosition(aaa->uid()));
0107     mouseReleaseAt(tool, gv.getEntityPosition(aaa->uid()));
0108     mousePressAt(tool, gv.getEntityPosition(aab->uid()));
0109     mouseReleaseAt(tool, gv.getEntityPosition(aab->uid()));
0110     tool.deactivate();
0111     REQUIRE(lastMessage == "You can't connect aaa to aab, they already have a connection.");
0112 
0113     tool.activate();
0114     mousePressAt(tool, gv.getEntityPosition(aaa->uid()));
0115     mouseReleaseAt(tool, gv.getEntityPosition(aaa->uid()));
0116     mousePressAt(tool, gv.getEntityPosition(aaa->uid()));
0117     mouseReleaseAt(tool, gv.getEntityPosition(aaa->uid()));
0118     REQUIRE(lastMessage == "Cannot create self-dependency");
0119     tool.deactivate();
0120 }