| 1 | // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <qbackendnodetester.h> |
| 6 | #include <Qt3DAnimation/private/handler_p.h> |
| 7 | #include <Qt3DAnimation/private/channelmapper_p.h> |
| 8 | #include <Qt3DAnimation/private/channelmapping_p.h> |
| 9 | #include <Qt3DAnimation/private/managers_p.h> |
| 10 | #include <Qt3DAnimation/qchannelmapper.h> |
| 11 | #include <Qt3DAnimation/qchannelmapping.h> |
| 12 | #include <Qt3DAnimation/private/qchannelmapper_p.h> |
| 13 | #include <Qt3DCore/private/qnode_p.h> |
| 14 | #include <Qt3DCore/private/qscene_p.h> |
| 15 | #include <Qt3DCore/private/qbackendnode_p.h> |
| 16 | #include "testarbiter.h" |
| 17 | |
| 18 | class tst_ChannelMapper : public Qt3DCore::QBackendNodeTester |
| 19 | { |
| 20 | Q_OBJECT |
| 21 | |
| 22 | private Q_SLOTS: |
| 23 | void checkPeerPropertyMirroring() |
| 24 | { |
| 25 | // GIVEN |
| 26 | Qt3DAnimation::Animation::Handler handler; |
| 27 | Qt3DAnimation::Animation::ChannelMapper backendMapper; |
| 28 | backendMapper.setHandler(&handler); |
| 29 | Qt3DAnimation::QChannelMapper mapper; |
| 30 | |
| 31 | mapper.addMapping(new Qt3DAnimation::QChannelMapping); |
| 32 | mapper.addMapping(new Qt3DAnimation::QChannelMapping); |
| 33 | |
| 34 | // WHEN |
| 35 | simulateInitializationSync(&mapper, &backendMapper); |
| 36 | |
| 37 | // THEN |
| 38 | QCOMPARE(backendMapper.peerId(), mapper.id()); |
| 39 | QCOMPARE(backendMapper.isEnabled(), mapper.isEnabled()); |
| 40 | |
| 41 | const int mappingCount = backendMapper.mappingIds().size(); |
| 42 | if (mappingCount > 0) { |
| 43 | for (int i = 0; i < mappingCount; ++i) |
| 44 | QCOMPARE(backendMapper.mappingIds().at(i), mapper.mappings().at(i)->id()); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void checkInitialAndCleanedUpState() |
| 49 | { |
| 50 | // GIVEN |
| 51 | Qt3DAnimation::Animation::Handler handler; |
| 52 | Qt3DAnimation::Animation::ChannelMapper backendMapper; |
| 53 | backendMapper.setHandler(&handler); |
| 54 | |
| 55 | // THEN |
| 56 | QVERIFY(backendMapper.peerId().isNull()); |
| 57 | QCOMPARE(backendMapper.isEnabled(), false); |
| 58 | QCOMPARE(backendMapper.mappingIds(), QList<Qt3DCore::QNodeId>()); |
| 59 | |
| 60 | // GIVEN |
| 61 | Qt3DAnimation::QChannelMapper mapper; |
| 62 | mapper.addMapping(new Qt3DAnimation::QChannelMapping()); |
| 63 | |
| 64 | // WHEN |
| 65 | simulateInitializationSync(&mapper, &backendMapper); |
| 66 | backendMapper.cleanup(); |
| 67 | |
| 68 | // THEN |
| 69 | QCOMPARE(backendMapper.isEnabled(), false); |
| 70 | QCOMPARE(backendMapper.mappingIds(), QList<Qt3DCore::QNodeId>()); |
| 71 | } |
| 72 | |
| 73 | void checkPropertyChanges() |
| 74 | { |
| 75 | // GIVEN |
| 76 | Qt3DAnimation::QChannelMapper mapper; |
| 77 | Qt3DAnimation::Animation::Handler handler; |
| 78 | Qt3DAnimation::Animation::ChannelMapper backendMapper; |
| 79 | backendMapper.setHandler(&handler); |
| 80 | simulateInitializationSync(&mapper, &backendMapper); |
| 81 | |
| 82 | // WHEN |
| 83 | mapper.setEnabled(false); |
| 84 | backendMapper.syncFromFrontEnd(&mapper, false); |
| 85 | |
| 86 | // THEN |
| 87 | QCOMPARE(backendMapper.isEnabled(), false); |
| 88 | |
| 89 | // WHEN |
| 90 | Qt3DAnimation::QChannelMapping mapping; |
| 91 | const Qt3DCore::QNodeId mappingId = mapping.id(); |
| 92 | Qt3DAnimation::Animation::ChannelMapping *backendMapping |
| 93 | = handler.channelMappingManager()->getOrCreateResource(mappingId); |
| 94 | backendMapping->setHandler(&handler); |
| 95 | simulateInitializationSync(&mapping, backendMapping); |
| 96 | |
| 97 | mapper.addMapping(&mapping); |
| 98 | backendMapper.syncFromFrontEnd(&mapper, false); |
| 99 | |
| 100 | // THEN |
| 101 | QCOMPARE(backendMapper.mappingIds().size(), 1); |
| 102 | QCOMPARE(backendMapper.mappingIds().first(), mappingId); |
| 103 | QCOMPARE(backendMapper.mappings().size(), 1); |
| 104 | QCOMPARE(backendMapper.mappings().first(), backendMapping); |
| 105 | |
| 106 | // WHEN |
| 107 | Qt3DAnimation::QChannelMapping mapping2; |
| 108 | const Qt3DCore::QNodeId mappingId2 = mapping2.id(); |
| 109 | Qt3DAnimation::Animation::ChannelMapping *backendMapping2 |
| 110 | = handler.channelMappingManager()->getOrCreateResource(mappingId2); |
| 111 | backendMapping2->setHandler(&handler); |
| 112 | simulateInitializationSync(&mapping2, backendMapping2); |
| 113 | |
| 114 | mapper.addMapping(&mapping2); |
| 115 | backendMapper.syncFromFrontEnd(&mapper, false); |
| 116 | |
| 117 | // THEN |
| 118 | QCOMPARE(backendMapper.mappingIds().size(), 2); |
| 119 | QCOMPARE(backendMapper.mappingIds().first(), mappingId); |
| 120 | QCOMPARE(backendMapper.mappingIds().last(), mappingId2); |
| 121 | QCOMPARE(backendMapper.mappings().size(), 2); |
| 122 | QCOMPARE(backendMapper.mappings().first(), backendMapping); |
| 123 | QCOMPARE(backendMapper.mappings().last(), backendMapping2); |
| 124 | |
| 125 | // WHEN |
| 126 | mapper.removeMapping(&mapping); |
| 127 | backendMapper.syncFromFrontEnd(&mapper, false); |
| 128 | |
| 129 | // THEN |
| 130 | QCOMPARE(backendMapper.mappingIds().size(), 1); |
| 131 | QCOMPARE(backendMapper.mappingIds().first(), mappingId2); |
| 132 | QCOMPARE(backendMapper.mappings().size(), 1); |
| 133 | QCOMPARE(backendMapper.mappings().first(), backendMapping2); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | QTEST_MAIN(tst_ChannelMapper) |
| 138 | |
| 139 | #include "tst_channelmapper.moc" |