File indexing completed on 2024-05-19 05:42:28
0001 // ct_lvtqtw_tabwidget.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_lvtprj_projectfile.h> 0021 #include <ct_lvtqtc_testing_utils.h> 0022 0023 #include <ct_lvtclr_colormanagement.h> 0024 #include <ct_lvtqtc_graphicsscene.h> 0025 #include <ct_lvtqtw_graphtabelement.h> 0026 #include <ct_lvttst_fixture_qt.h> 0027 0028 #include <ct_lvtldr_nodestoragetestutils.h> 0029 #include <ct_lvtqtw_tabwidget.h> 0030 #include <ct_lvttst_tmpdir.h> 0031 0032 #include <ct_lvtshr_graphenums.h> 0033 0034 #include <QJsonDocument> 0035 #include <catch2-local-includes.h> 0036 0037 using namespace Codethink::lvtqtc; 0038 using namespace Codethink::lvtqtw; 0039 using namespace Codethink::lvtclr; 0040 using namespace Codethink::lvtshr; 0041 using namespace Codethink::lvtmdl; 0042 using namespace Codethink::lvtprj; 0043 using namespace Codethink::lvtldr; 0044 0045 TEST_CASE_METHOD(QTApplicationFixture, "Basic tab widget workflow") 0046 { 0047 auto tmpDir = TmpDir{"basic_tab_test"}; 0048 auto dbPath = tmpDir.path() / "codedb.db"; 0049 auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath); 0050 0051 auto colorManagement = std::make_shared<ColorManagement>(false); 0052 auto projectFile = ProjectFileForTesting{}; 0053 auto tab = TabWidget{nodeStorage, projectFile, colorManagement}; 0054 0055 // By default, one tab is created 0056 REQUIRE(tab.count() == 1); 0057 0058 // If there's only one tab, and it is closed, a new tab will be created 0059 tab.closeTab(0); 0060 REQUIRE(tab.count() == 1); 0061 REQUIRE(tab.currentIndex() == 0); 0062 0063 // More tabs can be added and removed 0064 tab.openNewGraphTab(); 0065 REQUIRE(tab.count() == 2); 0066 REQUIRE(tab.currentIndex() == 1); 0067 tab.closeTab(0); 0068 REQUIRE(tab.count() == 1); 0069 REQUIRE(tab.currentIndex() == 0); 0070 REQUIRE(tab.tabText(tab.currentIndex()).toStdString() == "Unnamed 1"); 0071 0072 (void) nodeStorage.addPackage("aaa", "aaa", nullptr); 0073 (void) nodeStorage.addPackage("bbb", "bbb", nullptr); 0074 0075 // Now we need to load the graph, and send the data to the graphics scene. 0076 // it does not load directly anymore. 0077 tab.openNewGraphTab(QSet<QString>({QStringLiteral("aaa")})); 0078 REQUIRE(tab.count() == 2); 0079 REQUIRE(tab.currentIndex() == 1); 0080 0081 // Wrong tabs are accepted (won't crash), but the tab contents will be empty 0082 // TODO: Fix this test. 0083 tab.openNewGraphTab(QSet<QString>({QStringLiteral("zzz")})); 0084 REQUIRE(tab.count() == 3); 0085 REQUIRE(tab.currentIndex() == 2); 0086 0087 tab.replaceGraphAt(1, QStringLiteral("aaa")); 0088 REQUIRE(tab.count() == 3); 0089 REQUIRE(tab.currentIndex() == 2); 0090 } 0091 0092 TEST_CASE_METHOD(QTApplicationFixture, "Basic Bookmark Workflow") 0093 { 0094 auto tmpDir = TmpDir{"basic_tab_test"}; 0095 auto dbPath = tmpDir.path() / "codedb.db"; 0096 auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath); 0097 0098 (void) nodeStorage.addPackage("aaa", "aaa", nullptr); 0099 (void) nodeStorage.addPackage("bbb", "bbb", nullptr); 0100 0101 auto colorManagement = std::make_shared<ColorManagement>(false); 0102 auto projectFile = ProjectFileForTesting{}; 0103 auto tab = TabWidget{nodeStorage, projectFile, colorManagement}; 0104 0105 tab.show(); 0106 0107 // TODO: Fix this test. 0108 // tab.setCurrentGraphTab(TabWidget::GraphInfo{"aaa", NodeType::e_Package}); 0109 tab.saveBookmark("Bookmark1", 0, Codethink::lvtprj::ProjectFile::Bookmark); 0110 REQUIRE(tab.tabText(0) == "Bookmark1"); 0111 0112 tab.closeTab(0); 0113 REQUIRE(tab.tabText(0) == "Unnamed 0"); 0114 0115 tab.loadBookmark(projectFile.getBookmark("Bookmark1"), Codethink::lvtshr::HistoryType::NoHistory); 0116 REQUIRE(tab.tabText(0) == "Bookmark1"); 0117 }