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

0001 // ct_lvtqtc_undo_notes.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_lvtclr_colormanagement.h>
0023 #include <ct_lvtldr_lakosiannode.h>
0024 #include <ct_lvtldr_nodestoragetestutils.h>
0025 #include <ct_lvtqtc_graphicsscene.h>
0026 #include <ct_lvtqtc_testing_utils.h>
0027 #include <ct_lvtqtc_undo_notes.h>
0028 #include <ct_lvttst_fixture_qt.h>
0029 #include <ct_lvttst_tmpdir.h>
0030 
0031 #include <memory>
0032 
0033 using namespace Codethink::lvtqtc;
0034 using namespace Codethink::lvtldr;
0035 using namespace Codethink::lvtclr;
0036 using namespace Codethink::lvtshr;
0037 
0038 TEST_CASE_METHOD(QTApplicationFixture, "Undo/Redo Set Notes")
0039 {
0040     auto tmpDir = TmpDir{"undo_redo_set_notes"};
0041     auto dbPath = tmpDir.path() / "codedb.db";
0042     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0043 
0044     auto *a = nodeStorage.addPackage("a", "a").value();
0045 
0046     // This GraphicsView is in the heap so that we can control it's lifetime
0047     auto *gv = new GraphicsViewWrapperForTesting{nodeStorage};
0048     auto *scene = dynamic_cast<GraphicsScene *>(gv->scene());
0049     scene->loadEntityByQualifiedName(QString::fromStdString(a->qualifiedName()), QPoint(10, 10));
0050     gv->show();
0051 
0052     auto undoRedo = UndoManager{};
0053 
0054     // this undo command does not trigger a change on the first execution, so we need to update the node too.
0055     REQUIRE(a->notes().empty());
0056     a->setNotes("def");
0057     REQUIRE(a->notes() == "def");
0058 
0059     undoRedo.addUndoCommand(
0060         new UndoNotes(a->qualifiedName(), a->type(), "abc", "def", dynamic_cast<GraphicsScene *>(gv->scene())));
0061 
0062     undoRedo.undo();
0063     REQUIRE(a->notes() == "abc");
0064     undoRedo.redo();
0065     REQUIRE(a->notes() == "def");
0066 }