summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/util/qshadergraph/tst_qshadergraph.cpp
blob: 5ffdc58b51d386d76b7c4767a22ff463791773e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/****************************************************************************
**
** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#include <QtGui/private/qshadergraph_p.h>

namespace
{
    QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName)
    {
        auto port = QShaderNodePort();
        port.direction = portDirection;
        port.name = portName;
        return port;
    }

    QShaderNode createNode(const QVector<QShaderNodePort> &ports)
    {
        auto node = QShaderNode();
        node.setUuid(QUuid::createUuid());
        for (const auto &port : ports)
            node.addPort(port);
        return node;
    }

    QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName,
                                  const QUuid &targetUuid, const QString &targetName)
    {
        auto edge = QShaderGraph::Edge();
        edge.sourceNodeUuid = sourceUuid;
        edge.sourcePortName = sourceName;
        edge.targetNodeUuid = targetUuid;
        edge.targetPortName = targetName;
        return edge;
    }
}

class tst_QShaderGraph : public QObject
{
    Q_OBJECT
private slots:
    void shouldHaveEdgeDefaultState();
    void shouldTestEdgesEquality_data();
    void shouldTestEdgesEquality();
    void shouldManageNodeList();
    void shouldManageEdgeList();
};

void tst_QShaderGraph::shouldHaveEdgeDefaultState()
{
    // GIVEN
    auto edge = QShaderGraph::Edge();

    // THEN
    QVERIFY(edge.sourceNodeUuid.isNull());
    QVERIFY(edge.sourcePortName.isEmpty());
    QVERIFY(edge.targetNodeUuid.isNull());
    QVERIFY(edge.targetPortName.isEmpty());
}

void tst_QShaderGraph::shouldTestEdgesEquality_data()
{
    QTest::addColumn<QShaderGraph::Edge>("left");
    QTest::addColumn<QShaderGraph::Edge>("right");
    QTest::addColumn<bool>("expected");

    const auto sourceUuid1 = QUuid::createUuid();
    const auto sourceUuid2 = QUuid::createUuid();
    const auto targetUuid1 = QUuid::createUuid();
    const auto targetUuid2 = QUuid::createUuid();

    QTest::newRow("Equals") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
                            << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
                            << true;
    QTest::newRow("SourceUuid") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
                                << createEdge(sourceUuid2, "foo", targetUuid1, "bar")
                                << false;
    QTest::newRow("SourceName") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
                                << createEdge(sourceUuid1, "bleh", targetUuid1, "bar")
                                << false;
    QTest::newRow("TargetUuid") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
                                << createEdge(sourceUuid1, "foo", targetUuid2, "bar")
                                << false;
    QTest::newRow("TargetName") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
                                << createEdge(sourceUuid1, "foo", targetUuid1, "bleh")
                                << false;
}

void tst_QShaderGraph::shouldTestEdgesEquality()
{
    // GIVEN
    QFETCH(QShaderGraph::Edge, left);
    QFETCH(QShaderGraph::Edge, right);

    // WHEN
    const auto equal = (left == right);
    const auto notEqual = (left != right);

    // THEN
    QFETCH(bool, expected);
    QCOMPARE(equal, expected);
    QCOMPARE(notEqual, !expected);
}

void tst_QShaderGraph::shouldManageNodeList()
{
    // GIVEN
    const auto node1 = createNode({createPort(QShaderNodePort::Output, "node1")});
    const auto node2 = createNode({createPort(QShaderNodePort::Output, "node2")});

    auto graph = QShaderGraph();

    // THEN (default state)
    QVERIFY(graph.nodes().isEmpty());

    // WHEN
    graph.addNode(node1);

    // THEN
    QCOMPARE(graph.nodes().size(), 1);
    QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid());
    QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name);

    // WHEN
    graph.addNode(node2);

    // THEN
    QCOMPARE(graph.nodes().size(), 2);
    QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid());
    QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name);
    QCOMPARE(graph.nodes().at(1).uuid(), node2.uuid());
    QCOMPARE(graph.nodes().at(1).ports().at(0).name, node2.ports().at(0).name);


    // WHEN
    graph.removeNode(node2);

    // THEN
    QCOMPARE(graph.nodes().size(), 1);
    QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid());
    QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name);

    // WHEN
    graph.addNode(node2);

    // THEN
    QCOMPARE(graph.nodes().size(), 2);
    QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid());
    QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name);
    QCOMPARE(graph.nodes().at(1).uuid(), node2.uuid());
    QCOMPARE(graph.nodes().at(1).ports().at(0).name, node2.ports().at(0).name);

    // WHEN
    const auto node1bis = [node1] {
        auto res = node1;
        auto port = res.ports().at(0);
        port.name = QStringLiteral("node1bis");
        res.addPort(port);
        return res;
    }();
    graph.addNode(node1bis);

    // THEN
    QCOMPARE(graph.nodes().size(), 2);
    QCOMPARE(graph.nodes().at(0).uuid(), node2.uuid());
    QCOMPARE(graph.nodes().at(0).ports().at(0).name, node2.ports().at(0).name);
    QCOMPARE(graph.nodes().at(1).uuid(), node1bis.uuid());
    QCOMPARE(graph.nodes().at(1).ports().at(0).name, node1bis.ports().at(0).name);
}

void tst_QShaderGraph::shouldManageEdgeList()
{
    // GIVEN
    const auto edge1 = createEdge(QUuid::createUuid(), "foo", QUuid::createUuid(), "bar");
    const auto edge2 = createEdge(QUuid::createUuid(), "baz", QUuid::createUuid(), "boo");

    auto graph = QShaderGraph();

    // THEN (default state)
    QVERIFY(graph.edges().isEmpty());

    // WHEN
    graph.addEdge(edge1);

    // THEN
    QCOMPARE(graph.edges().size(), 1);
    QCOMPARE(graph.edges().at(0), edge1);

    // WHEN
    graph.addEdge(edge2);

    // THEN
    QCOMPARE(graph.edges().size(), 2);
    QCOMPARE(graph.edges().at(0), edge1);
    QCOMPARE(graph.edges().at(1), edge2);


    // WHEN
    graph.removeEdge(edge2);

    // THEN
    QCOMPARE(graph.edges().size(), 1);
    QCOMPARE(graph.edges().at(0), edge1);

    // WHEN
    graph.addEdge(edge2);

    // THEN
    QCOMPARE(graph.edges().size(), 2);
    QCOMPARE(graph.edges().at(0), edge1);
    QCOMPARE(graph.edges().at(1), edge2);

    // WHEN
    graph.addEdge(edge1);

    // THEN
    QCOMPARE(graph.edges().size(), 2);
    QCOMPARE(graph.edges().at(0), edge1);
    QCOMPARE(graph.edges().at(1), edge2);
}

QTEST_MAIN(tst_QShaderGraph)

#include "tst_qshadergraph.moc"