tst_aggregate.cpp 8.34 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
29
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
con's avatar
con committed
32
33
34

#include <aggregate.h>

hjk's avatar
hjk committed
35
36
#include <QtTest/QtTest>

37
38
//TESTED_COMPONENT=src/libs/aggregation

con's avatar
con committed
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
class tst_Aggregate : public QObject
{
    Q_OBJECT

private slots:
    void deleteAggregation();
    void queryAggregation();
    void queryAll();
    void parentAggregate();
};

class Interface1 : public QObject
{
    Q_OBJECT
};

class Interface11 : public Interface1
{
    Q_OBJECT
};

class Interface2 : public QObject
{
    Q_OBJECT
};

class Interface3 : public QObject
{
    Q_OBJECT
};

void tst_Aggregate::deleteAggregation()
{
    QPointer<Aggregation::Aggregate> aggregation;
    QPointer<QObject> component1;
    QPointer<QObject> component2;

    aggregation = new Aggregation::Aggregate;
    component1 = new Interface1;
    component2 = new Interface2;
    aggregation->add(component1);
    aggregation->add(component2);
    delete aggregation;
    QVERIFY(aggregation == 0);
    QVERIFY(component1 == 0);
    QVERIFY(component2 == 0);

    aggregation = new Aggregation::Aggregate;
    component1 = new Interface1;
    component2 = new Interface2;
    aggregation->add(component1);
    aggregation->add(component2);
    delete component1;
    QVERIFY(aggregation == 0);
    QVERIFY(component1 == 0);
    QVERIFY(component2 == 0);

    aggregation = new Aggregation::Aggregate;
    component1 = new Interface1;
    component2 = new Interface2;
    aggregation->add(component1);
    aggregation->add(component2);
    delete component2;
    QVERIFY(aggregation == 0);
    QVERIFY(component1 == 0);
    QVERIFY(component2 == 0);

    // if a component doesn't belong to an aggregation, it should simply delete itself
    component1 = new Interface1;
    delete component1;
    QVERIFY(component1 == 0);
}

void tst_Aggregate::queryAggregation()
{
    Aggregation::Aggregate aggregation;
    QObject *aggObject = &aggregation;
    QObject *component1 = new Interface11;
    QObject *component2 = new Interface2;
    aggregation.add(component1);
    aggregation.add(component2);
    QCOMPARE(Aggregation::query<Interface1>(&aggregation), component1);
    QCOMPARE(Aggregation::query<Interface2>(&aggregation), component2);
    QCOMPARE(Aggregation::query<Interface11>(&aggregation), component1);
    QCOMPARE(Aggregation::query<Interface3>(&aggregation), (Interface3 *)0);

    QCOMPARE(Aggregation::query<Interface1>(aggObject), component1);
    QCOMPARE(Aggregation::query<Interface2>(aggObject), component2);
    QCOMPARE(Aggregation::query<Interface11>(aggObject), component1);
    QCOMPARE(Aggregation::query<Interface3>(aggObject), (Interface3 *)0);

    QCOMPARE(Aggregation::query<Interface1>(component1), component1);
    QCOMPARE(Aggregation::query<Interface2>(component1), component2);
    QCOMPARE(Aggregation::query<Interface11>(component1), component1);
    QCOMPARE(Aggregation::query<Interface3>(component1), (Interface3 *)0);

    QCOMPARE(Aggregation::query<Interface1>(component2), component1);
    QCOMPARE(Aggregation::query<Interface2>(component2), component2);
    QCOMPARE(Aggregation::query<Interface11>(component2), component1);
    QCOMPARE(Aggregation::query<Interface3>(component2), (Interface3 *)0);

    // components that don't belong to an aggregation should be query-able to itself only
    QObject *component3 = new Interface3;
    QCOMPARE(Aggregation::query<Interface1>(component3), (Interface1 *)0);
    QCOMPARE(Aggregation::query<Interface2>(component3), (Interface2 *)0);
    QCOMPARE(Aggregation::query<Interface11>(component3), (Interface11 *)0);
    QCOMPARE(Aggregation::query<Interface3>(component3), component3);
    delete component3;
}

void tst_Aggregate::queryAll()
{
    Aggregation::Aggregate aggregation;
    QObject *aggObject = &aggregation;
    Interface1 *component1 = new Interface1;
    Interface11 *component11 = new Interface11;
    Interface2 *component2 = new Interface2;
    aggregation.add(component1);
    aggregation.add(component11);
    aggregation.add(component2);
    QCOMPARE(Aggregation::query_all<Interface1>(&aggregation), QList<Interface1 *>() << component1 << component11);
    QCOMPARE(Aggregation::query_all<Interface11>(&aggregation), QList<Interface11 *>() << component11);
    QCOMPARE(Aggregation::query_all<Interface2>(&aggregation), QList<Interface2 *>() << component2);
    QCOMPARE(Aggregation::query_all<Interface3>(&aggregation), QList<Interface3 *>());

    QCOMPARE(Aggregation::query_all<Interface1>(aggObject), QList<Interface1 *>() << component1 << component11);
    QCOMPARE(Aggregation::query_all<Interface11>(aggObject), QList<Interface11 *>() << component11);
    QCOMPARE(Aggregation::query_all<Interface2>(aggObject), QList<Interface2 *>() << component2);
    QCOMPARE(Aggregation::query_all<Interface3>(aggObject), QList<Interface3 *>());

    QCOMPARE(Aggregation::query_all<Interface1>(component1), QList<Interface1 *>() << component1 << component11);
    QCOMPARE(Aggregation::query_all<Interface11>(component1), QList<Interface11 *>() << component11);
    QCOMPARE(Aggregation::query_all<Interface2>(component1), QList<Interface2 *>() << component2);
    QCOMPARE(Aggregation::query_all<Interface3>(component1), QList<Interface3 *>());

    QCOMPARE(Aggregation::query_all<Interface1>(component11), QList<Interface1 *>() << component1 << component11);
    QCOMPARE(Aggregation::query_all<Interface11>(component11), QList<Interface11 *>() << component11);
    QCOMPARE(Aggregation::query_all<Interface2>(component11), QList<Interface2 *>() << component2);
    QCOMPARE(Aggregation::query_all<Interface3>(component11), QList<Interface3 *>());

    QCOMPARE(Aggregation::query_all<Interface1>(component2), QList<Interface1 *>() << component1 << component11);
    QCOMPARE(Aggregation::query_all<Interface11>(component2), QList<Interface11 *>() << component11);
    QCOMPARE(Aggregation::query_all<Interface2>(component2), QList<Interface2 *>() << component2);
    QCOMPARE(Aggregation::query_all<Interface3>(component2), QList<Interface3 *>());
}

void tst_Aggregate::parentAggregate()
{
    Aggregation::Aggregate aggregation;
con's avatar
con committed
188
    Aggregation::Aggregate aggregation2;
con's avatar
con committed
189
190
191
192
193
194
195
196
197
    Interface1 *component1 = new Interface1;
    Interface11 *component11 = new Interface11;
    QObject *component2 = new QObject;
    aggregation.add(component1);
    aggregation.add(component11);
    QCOMPARE(Aggregation::Aggregate::parentAggregate(&aggregation), &aggregation);
    QCOMPARE(Aggregation::Aggregate::parentAggregate(component1), &aggregation);
    QCOMPARE(Aggregation::Aggregate::parentAggregate(component11), &aggregation);
    QCOMPARE(Aggregation::Aggregate::parentAggregate(component2), (Aggregation::Aggregate *)0);
con's avatar
con committed
198
199
200
201
202
203
204
205
206
    // test reparenting a component to another aggregate (should warn but not work)
    aggregation2.add(component11);
    QCOMPARE(Aggregation::Aggregate::parentAggregate(component11), &aggregation);
    // test adding an aggregate to an aggregate (should warn but not work)
    aggregation.add(&aggregation2);
    QCOMPARE(Aggregation::Aggregate::parentAggregate(&aggregation2), &aggregation2);
    // test removing an object from an aggregation.
    aggregation.remove(component11);
    QCOMPARE(Aggregation::Aggregate::parentAggregate(component11), (Aggregation::Aggregate *)0);
con's avatar
con committed
207
208
209
}

QTEST_MAIN(tst_Aggregate)
hjk's avatar
hjk committed
210

con's avatar
con committed
211
#include "tst_aggregate.moc"