| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QtTest/QTest> |
| 30 | #include <Qt3DCore/private/qnode_p.h> |
| 31 | #include <Qt3DCore/private/qscene_p.h> |
| 32 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
| 33 | |
| 34 | #include <Qt3DAnimation/qchannelmapper.h> |
| 35 | #include <Qt3DAnimation/private/qchannelmapper_p.h> |
| 36 | #include <Qt3DAnimation/qchannelmapping.h> |
| 37 | |
| 38 | #include "testpostmanarbiter.h" |
| 39 | |
| 40 | class tst_QChannelmapper : public Qt3DAnimation::QChannelMapper |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | public: |
| 44 | tst_QChannelmapper() |
| 45 | { |
| 46 | qRegisterMetaType<Qt3DCore::QNode *>(); |
| 47 | } |
| 48 | |
| 49 | private Q_SLOTS: |
| 50 | void checkCreationChange_data() |
| 51 | { |
| 52 | QTest::addColumn<Qt3DAnimation::QChannelMapper *>(name: "mapper" ); |
| 53 | |
| 54 | Qt3DAnimation::QChannelMapper *defaultConstructed = new Qt3DAnimation::QChannelMapper; |
| 55 | QTest::newRow(dataTag: "defaultConstructed" ) << defaultConstructed; |
| 56 | |
| 57 | Qt3DAnimation::QChannelMapper *mapperWithMappings = new Qt3DAnimation::QChannelMapper; |
| 58 | mapperWithMappings->addMapping(mapping: new Qt3DAnimation::QChannelMapping); |
| 59 | mapperWithMappings->addMapping(mapping: new Qt3DAnimation::QChannelMapping); |
| 60 | mapperWithMappings->addMapping(mapping: new Qt3DAnimation::QChannelMapping); |
| 61 | QTest::newRow(dataTag: "mapperWithMappings" ) << mapperWithMappings; |
| 62 | } |
| 63 | |
| 64 | void checkCreationChange() |
| 65 | { |
| 66 | // GIVEN |
| 67 | QFETCH(Qt3DAnimation::QChannelMapper *, mapper); |
| 68 | |
| 69 | // WHEN |
| 70 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(mapper); |
| 71 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); |
| 72 | |
| 73 | const int mappingCount = mapper->mappings().count(); |
| 74 | |
| 75 | // THEN |
| 76 | QCOMPARE(creationChanges.size(), 1 + mappingCount); |
| 77 | |
| 78 | const Qt3DCore::QNodeCreatedChangePtr<Qt3DAnimation::QChannelMapperData> creationChange = |
| 79 | qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DAnimation::QChannelMapperData>>(src: creationChanges.first()); |
| 80 | const Qt3DAnimation::QChannelMapperData &creationData = creationChange->data; |
| 81 | |
| 82 | // THEN |
| 83 | QCOMPARE(mapper->id(), creationChange->subjectId()); |
| 84 | QCOMPARE(mapper->isEnabled(), creationChange->isNodeEnabled()); |
| 85 | QCOMPARE(mapper->metaObject(), creationChange->metaObject()); |
| 86 | QCOMPARE(mappingCount, creationData.mappingIds.count()); |
| 87 | |
| 88 | for (int i = 0; i < mappingCount; ++i) |
| 89 | QCOMPARE(mapper->mappings().at(i)->id(), creationData.mappingIds.at(i)); |
| 90 | } |
| 91 | |
| 92 | void checkPropertyUpdates() |
| 93 | { |
| 94 | // GIVEN |
| 95 | TestArbiter arbiter; |
| 96 | QScopedPointer<Qt3DAnimation::QChannelMapper> mapper(new Qt3DAnimation::QChannelMapper); |
| 97 | arbiter.setArbiterOnNode(mapper.data()); |
| 98 | |
| 99 | // WHEN |
| 100 | mapper->setEnabled(false); |
| 101 | |
| 102 | // THEN |
| 103 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 104 | QCOMPARE(arbiter.dirtyNodes.front(), mapper.data()); |
| 105 | |
| 106 | arbiter.dirtyNodes.clear(); |
| 107 | } |
| 108 | |
| 109 | void checkMappingBookkeeping() |
| 110 | { |
| 111 | // GIVEN |
| 112 | QScopedPointer<Qt3DAnimation::QChannelMapper> mapper(new Qt3DAnimation::QChannelMapper); |
| 113 | { |
| 114 | // WHEN |
| 115 | Qt3DAnimation::QChannelMapping mapping; |
| 116 | mapper->addMapping(mapping: &mapping); |
| 117 | |
| 118 | // THEN |
| 119 | QCOMPARE(mapping.parent(), mapper.data()); |
| 120 | QCOMPARE(mapper->mappings().size(), 1); |
| 121 | } |
| 122 | // THEN (Should not crash and output be unset) |
| 123 | QVERIFY(mapper->mappings().empty()); |
| 124 | |
| 125 | { |
| 126 | // WHEN |
| 127 | Qt3DAnimation::QChannelMapper someOtherMapper; |
| 128 | QScopedPointer<Qt3DAnimation::QChannelMapping> mapping(new Qt3DAnimation::QChannelMapping(&someOtherMapper)); |
| 129 | mapper->addMapping(mapping: mapping.data()); |
| 130 | |
| 131 | // THEN |
| 132 | QCOMPARE(mapping->parent(), &someOtherMapper); |
| 133 | QCOMPARE(mapper->mappings().size(), 1); |
| 134 | |
| 135 | // WHEN |
| 136 | mapper.reset(); |
| 137 | mapping.reset(); |
| 138 | |
| 139 | // THEN Should not crash when the output is destroyed (tests for failed removal of destruction helper) |
| 140 | } |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | QTEST_MAIN(tst_QChannelmapper) |
| 145 | |
| 146 | #include "tst_qchannelmapper.moc" |
| 147 | |