| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DQuickScene2D/qscene2d.h> |
| 6 | #include <Qt3DRender/qrendertargetoutput.h> |
| 7 | #include <private/qscene2d_p.h> |
| 8 | #include <QObject> |
| 9 | #include <QSignalSpy> |
| 10 | #include "testarbiter.h" |
| 11 | |
| 12 | using namespace Qt3DRender::Quick; |
| 13 | |
| 14 | class tst_QScene2D : public QObject |
| 15 | { |
| 16 | Q_OBJECT |
| 17 | |
| 18 | private Q_SLOTS: |
| 19 | |
| 20 | void initTestCase() |
| 21 | { |
| 22 | qRegisterMetaType<Qt3DRender::Quick::QScene2D::RenderPolicy>( |
| 23 | "QScene2D::RenderPolicy"); |
| 24 | } |
| 25 | |
| 26 | void checkDefaultConstruction() |
| 27 | { |
| 28 | // GIVEN |
| 29 | Qt3DRender::Quick::QScene2D scene2d; |
| 30 | |
| 31 | // THEN |
| 32 | QCOMPARE(scene2d.output(), nullptr); |
| 33 | QCOMPARE(scene2d.renderPolicy(), QScene2D::Continuous); |
| 34 | QCOMPARE(scene2d.item(), nullptr); |
| 35 | QCOMPARE(scene2d.isMouseEnabled(), true); |
| 36 | } |
| 37 | |
| 38 | void checkPropertyChanges() |
| 39 | { |
| 40 | // GIVEN |
| 41 | Qt3DRender::Quick::QScene2D scene2d; |
| 42 | QScopedPointer<Qt3DRender::QRenderTargetOutput> output(new Qt3DRender::QRenderTargetOutput()); |
| 43 | QScopedPointer<QQuickItem> item(new QQuickItem()); |
| 44 | |
| 45 | { |
| 46 | // WHEN |
| 47 | QSignalSpy spy(&scene2d, SIGNAL(outputChanged(Qt3DRender::QRenderTargetOutput*))); |
| 48 | Qt3DRender::QRenderTargetOutput *newValue = output.data(); |
| 49 | scene2d.setOutput(newValue); |
| 50 | |
| 51 | // THEN |
| 52 | QVERIFY(spy.isValid()); |
| 53 | QCOMPARE(scene2d.output(), newValue); |
| 54 | QCOMPARE(spy.size(), 1); |
| 55 | |
| 56 | // WHEN |
| 57 | spy.clear(); |
| 58 | scene2d.setOutput(newValue); |
| 59 | |
| 60 | // THEN |
| 61 | QCOMPARE(scene2d.output(), newValue); |
| 62 | QCOMPARE(spy.size(), 0); |
| 63 | } |
| 64 | { |
| 65 | // WHEN |
| 66 | QSignalSpy spy(&scene2d, SIGNAL(renderPolicyChanged(QScene2D::RenderPolicy))); |
| 67 | const QScene2D::RenderPolicy newValue = QScene2D::SingleShot; |
| 68 | scene2d.setRenderPolicy(newValue); |
| 69 | |
| 70 | // THEN |
| 71 | QVERIFY(spy.isValid()); |
| 72 | QCOMPARE(scene2d.renderPolicy(), newValue); |
| 73 | QCOMPARE(spy.size(), 1); |
| 74 | |
| 75 | // WHEN |
| 76 | spy.clear(); |
| 77 | scene2d.setRenderPolicy(newValue); |
| 78 | |
| 79 | // THEN |
| 80 | QCOMPARE(scene2d.renderPolicy(), newValue); |
| 81 | QCOMPARE(spy.size(), 0); |
| 82 | } |
| 83 | { |
| 84 | // WHEN |
| 85 | QSignalSpy spy(&scene2d, SIGNAL(itemChanged(QQuickItem*))); |
| 86 | QQuickItem *newValue = item.data(); |
| 87 | scene2d.setItem(newValue); |
| 88 | |
| 89 | // THEN |
| 90 | QVERIFY(spy.isValid()); |
| 91 | QCOMPARE(scene2d.item(), newValue); |
| 92 | QCOMPARE(spy.size(), 1); |
| 93 | |
| 94 | // WHEN |
| 95 | spy.clear(); |
| 96 | scene2d.setItem(newValue); |
| 97 | |
| 98 | // THEN |
| 99 | QCOMPARE(scene2d.item(), newValue); |
| 100 | QCOMPARE(spy.size(), 0); |
| 101 | } |
| 102 | |
| 103 | { |
| 104 | // WHEN |
| 105 | QSignalSpy spy(&scene2d, SIGNAL(mouseEnabledChanged(bool))); |
| 106 | bool newValue = false; |
| 107 | scene2d.setMouseEnabled(newValue); |
| 108 | |
| 109 | // THEN |
| 110 | QVERIFY(spy.isValid()); |
| 111 | QCOMPARE(scene2d.isMouseEnabled(), newValue); |
| 112 | QCOMPARE(spy.size(), 1); |
| 113 | |
| 114 | // WHEN |
| 115 | spy.clear(); |
| 116 | scene2d.setMouseEnabled(newValue); |
| 117 | |
| 118 | // THEN |
| 119 | QCOMPARE(scene2d.isMouseEnabled(), newValue); |
| 120 | QCOMPARE(spy.size(), 0); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void checkOutputUpdate() |
| 125 | { |
| 126 | // GIVEN |
| 127 | TestArbiter arbiter; |
| 128 | Qt3DRender::Quick::QScene2D scene2d; |
| 129 | arbiter.setArbiterOnNode(&scene2d); |
| 130 | QScopedPointer<Qt3DRender::QRenderTargetOutput> output(new Qt3DRender::QRenderTargetOutput()); |
| 131 | |
| 132 | { |
| 133 | // WHEN |
| 134 | scene2d.setOutput(output.data()); |
| 135 | QCoreApplication::processEvents(); |
| 136 | |
| 137 | // THEN |
| 138 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 139 | QCOMPARE(arbiter.dirtyNodes().front(), &scene2d); |
| 140 | |
| 141 | arbiter.clear(); |
| 142 | } |
| 143 | |
| 144 | { |
| 145 | // WHEN |
| 146 | scene2d.setOutput(output.data()); |
| 147 | QCoreApplication::processEvents(); |
| 148 | |
| 149 | // THEN |
| 150 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | void checkRenderPolicyUpdate() |
| 156 | { |
| 157 | // GIVEN |
| 158 | TestArbiter arbiter; |
| 159 | Qt3DRender::Quick::QScene2D scene2d; |
| 160 | arbiter.setArbiterOnNode(&scene2d); |
| 161 | |
| 162 | { |
| 163 | // WHEN |
| 164 | scene2d.setRenderPolicy(QScene2D::SingleShot); |
| 165 | QCoreApplication::processEvents(); |
| 166 | |
| 167 | // THEN |
| 168 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 169 | QCOMPARE(arbiter.dirtyNodes().front(), &scene2d); |
| 170 | |
| 171 | arbiter.clear(); |
| 172 | } |
| 173 | |
| 174 | { |
| 175 | // WHEN |
| 176 | scene2d.setRenderPolicy(QScene2D::SingleShot); |
| 177 | QCoreApplication::processEvents(); |
| 178 | |
| 179 | // THEN |
| 180 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 181 | } |
| 182 | |
| 183 | } |
| 184 | |
| 185 | void checkMouseEnabledUpdate() |
| 186 | { |
| 187 | // GIVEN |
| 188 | TestArbiter arbiter; |
| 189 | Qt3DRender::Quick::QScene2D scene2d; |
| 190 | arbiter.setArbiterOnNode(&scene2d); |
| 191 | |
| 192 | { |
| 193 | // WHEN |
| 194 | scene2d.setMouseEnabled(false); |
| 195 | QCoreApplication::processEvents(); |
| 196 | |
| 197 | // THEN |
| 198 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 199 | QCOMPARE(arbiter.dirtyNodes().front(), &scene2d); |
| 200 | |
| 201 | arbiter.clear(); |
| 202 | } |
| 203 | |
| 204 | { |
| 205 | // WHEN |
| 206 | scene2d.setMouseEnabled(false); |
| 207 | QCoreApplication::processEvents(); |
| 208 | |
| 209 | // THEN |
| 210 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | }; |
| 216 | |
| 217 | QTEST_MAIN(tst_QScene2D) |
| 218 | |
| 219 | #include "tst_qscene2d.moc" |