|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0-only |
| 3 | + * MuseScore-Studio-CLA-applies |
| 4 | + * |
| 5 | + * MuseScore Studio |
| 6 | + * Music Composition & Notation |
| 7 | + * |
| 8 | + * Copyright (C) 2025 MuseScore Limited |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License version 3 as |
| 12 | + * published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +#include "engraving/compat/scoreaccess.h" |
| 26 | +#include "engraving/dom/factory.h" |
| 27 | +#include "engraving/dom/instrument.h" |
| 28 | +#include "engraving/dom/part.h" |
| 29 | +#include "engraving/dom/staff.h" |
| 30 | +#include "engraving/editing/editpart.h" |
| 31 | + |
| 32 | +using namespace mu::engraving; |
| 33 | + |
| 34 | +class Engraving_ApiScoreTests : public ::testing::Test |
| 35 | +{ |
| 36 | +public: |
| 37 | +}; |
| 38 | + |
| 39 | +//--------------------------------------------------------- |
| 40 | +// testReplaceInstrumentAtDomLevel |
| 41 | +// Test that ChangePart correctly replaces the instrument |
| 42 | +// This tests the underlying mechanism used by the Plugin API |
| 43 | +//--------------------------------------------------------- |
| 44 | + |
| 45 | +TEST_F(Engraving_ApiScoreTests, replaceInstrumentAtDomLevel) |
| 46 | +{ |
| 47 | + // [GIVEN] A score with a part |
| 48 | + MasterScore* score = compat::ScoreAccess::createMasterScore(nullptr); |
| 49 | + |
| 50 | + // Create a part with a default instrument |
| 51 | + Part* part = new Part(score); |
| 52 | + score->appendPart(part); |
| 53 | + score->appendStaff(Factory::createStaff(part)); |
| 54 | + |
| 55 | + ASSERT_EQ(score->parts().size(), 1); |
| 56 | + ASSERT_NE(part, nullptr); |
| 57 | + |
| 58 | + // Set initial instrument |
| 59 | + Instrument initialInstrument; |
| 60 | + initialInstrument.setId(u"test.initial"); |
| 61 | + initialInstrument.setTrackName(u"Initial Instrument"); |
| 62 | + part->setInstrument(initialInstrument); |
| 63 | + |
| 64 | + // Verify initial instrument |
| 65 | + EXPECT_EQ(part->instrumentId(), QString("test.initial")); |
| 66 | + |
| 67 | + // [WHEN] We replace the instrument using ChangePart |
| 68 | + Instrument newInstrument; |
| 69 | + newInstrument.setId(u"test.replaced"); |
| 70 | + newInstrument.setTrackName(u"Replaced Instrument"); |
| 71 | + |
| 72 | + score->startCmd(TranslatableString::untranslatable("Replace instrument test")); |
| 73 | + score->undo(new ChangePart(part, new Instrument(newInstrument), u"Replaced Part")); |
| 74 | + score->endCmd(); |
| 75 | + |
| 76 | + // [THEN] The part's instrument should be changed |
| 77 | + EXPECT_EQ(part->instrumentId(), QString("test.replaced")); |
| 78 | + EXPECT_EQ(part->instrument()->trackName(), muse::String(u"Replaced Instrument")); |
| 79 | + |
| 80 | + delete score; |
| 81 | +} |
| 82 | + |
| 83 | +//--------------------------------------------------------- |
| 84 | +// testReplaceInstrumentUndo |
| 85 | +// Test that instrument replacement can be undone |
| 86 | +//--------------------------------------------------------- |
| 87 | + |
| 88 | +TEST_F(Engraving_ApiScoreTests, replaceInstrumentUndo) |
| 89 | +{ |
| 90 | + // [GIVEN] A score with a part |
| 91 | + MasterScore* score = compat::ScoreAccess::createMasterScore(nullptr); |
| 92 | + |
| 93 | + Part* part = new Part(score); |
| 94 | + score->appendPart(part); |
| 95 | + score->appendStaff(Factory::createStaff(part)); |
| 96 | + |
| 97 | + // Set initial instrument |
| 98 | + Instrument initialInstrument; |
| 99 | + initialInstrument.setId(u"test.original"); |
| 100 | + initialInstrument.setTrackName(u"Original Instrument"); |
| 101 | + part->setInstrument(initialInstrument); |
| 102 | + |
| 103 | + EXPECT_EQ(part->instrumentId(), QString("test.original")); |
| 104 | + |
| 105 | + // [WHEN] We replace the instrument |
| 106 | + Instrument newInstrument; |
| 107 | + newInstrument.setId(u"test.new"); |
| 108 | + newInstrument.setTrackName(u"New Instrument"); |
| 109 | + |
| 110 | + score->startCmd(TranslatableString::untranslatable("Replace instrument test")); |
| 111 | + score->undo(new ChangePart(part, new Instrument(newInstrument), u"New Part")); |
| 112 | + score->endCmd(); |
| 113 | + |
| 114 | + // Verify it changed |
| 115 | + EXPECT_EQ(part->instrumentId(), QString("test.new")); |
| 116 | + |
| 117 | + // [WHEN] We undo |
| 118 | + score->undoRedo(true, nullptr); |
| 119 | + |
| 120 | + // [THEN] The instrument should be back to original |
| 121 | + EXPECT_EQ(part->instrumentId(), QString("test.original")); |
| 122 | + |
| 123 | + delete score; |
| 124 | +} |
| 125 | + |
| 126 | +//--------------------------------------------------------- |
| 127 | +// testReplaceInstrumentRedo |
| 128 | +// Test that instrument replacement can be redone after undo |
| 129 | +//--------------------------------------------------------- |
| 130 | + |
| 131 | +TEST_F(Engraving_ApiScoreTests, replaceInstrumentRedo) |
| 132 | +{ |
| 133 | + // [GIVEN] A score with a part |
| 134 | + MasterScore* score = compat::ScoreAccess::createMasterScore(nullptr); |
| 135 | + |
| 136 | + Part* part = new Part(score); |
| 137 | + score->appendPart(part); |
| 138 | + score->appendStaff(Factory::createStaff(part)); |
| 139 | + |
| 140 | + // Set initial instrument |
| 141 | + Instrument initialInstrument; |
| 142 | + initialInstrument.setId(u"test.initial"); |
| 143 | + part->setInstrument(initialInstrument); |
| 144 | + |
| 145 | + // Replace the instrument |
| 146 | + Instrument newInstrument; |
| 147 | + newInstrument.setId(u"test.replaced"); |
| 148 | + |
| 149 | + score->startCmd(TranslatableString::untranslatable("Replace instrument test")); |
| 150 | + score->undo(new ChangePart(part, new Instrument(newInstrument), u"Replaced")); |
| 151 | + score->endCmd(); |
| 152 | + |
| 153 | + EXPECT_EQ(part->instrumentId(), QString("test.replaced")); |
| 154 | + |
| 155 | + // Undo |
| 156 | + score->undoRedo(true, nullptr); |
| 157 | + EXPECT_EQ(part->instrumentId(), QString("test.initial")); |
| 158 | + |
| 159 | + // [WHEN] We redo |
| 160 | + score->undoRedo(false, nullptr); |
| 161 | + |
| 162 | + // [THEN] The instrument should be replaced again |
| 163 | + EXPECT_EQ(part->instrumentId(), QString("test.replaced")); |
| 164 | + |
| 165 | + delete score; |
| 166 | +} |
0 commit comments