1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include <aggregate.h>
36 #include <QtTest/QtTest>
38 //TESTED_COMPONENT=src/libs/aggregation
40 class tst_Aggregate : public QObject
45 void deleteAggregation();
46 void queryAggregation();
48 void parentAggregate();
51 class Interface1 : public QObject
56 class Interface11 : public Interface1
61 class Interface2 : public QObject
66 class Interface3 : public QObject
71 void tst_Aggregate::deleteAggregation()
73 QPointer<Aggregation::Aggregate> aggregation;
74 QPointer<QObject> component1;
75 QPointer<QObject> component2;
77 aggregation = new Aggregation::Aggregate;
78 component1 = new Interface1;
79 component2 = new Interface2;
80 aggregation->add(component1);
81 aggregation->add(component2);
83 QVERIFY(aggregation == 0);
84 QVERIFY(component1 == 0);
85 QVERIFY(component2 == 0);
87 aggregation = new Aggregation::Aggregate;
88 component1 = new Interface1;
89 component2 = new Interface2;
90 aggregation->add(component1);
91 aggregation->add(component2);
93 QVERIFY(aggregation == 0);
94 QVERIFY(component1 == 0);
95 QVERIFY(component2 == 0);
97 aggregation = new Aggregation::Aggregate;
98 component1 = new Interface1;
99 component2 = new Interface2;
100 aggregation->add(component1);
101 aggregation->add(component2);
103 QVERIFY(aggregation == 0);
104 QVERIFY(component1 == 0);
105 QVERIFY(component2 == 0);
107 // if a component doesn't belong to an aggregation, it should simply delete itself
108 component1 = new Interface1;
110 QVERIFY(component1 == 0);
113 void tst_Aggregate::queryAggregation()
115 Aggregation::Aggregate aggregation;
116 QObject *aggObject = &aggregation;
117 QObject *component1 = new Interface11;
118 QObject *component2 = new Interface2;
119 aggregation.add(component1);
120 aggregation.add(component2);
121 QCOMPARE(Aggregation::query<Interface1>(&aggregation), component1);
122 QCOMPARE(Aggregation::query<Interface2>(&aggregation), component2);
123 QCOMPARE(Aggregation::query<Interface11>(&aggregation), component1);
124 QCOMPARE(Aggregation::query<Interface3>(&aggregation), (Interface3 *)0);
126 QCOMPARE(Aggregation::query<Interface1>(aggObject), component1);
127 QCOMPARE(Aggregation::query<Interface2>(aggObject), component2);
128 QCOMPARE(Aggregation::query<Interface11>(aggObject), component1);
129 QCOMPARE(Aggregation::query<Interface3>(aggObject), (Interface3 *)0);
131 QCOMPARE(Aggregation::query<Interface1>(component1), component1);
132 QCOMPARE(Aggregation::query<Interface2>(component1), component2);
133 QCOMPARE(Aggregation::query<Interface11>(component1), component1);
134 QCOMPARE(Aggregation::query<Interface3>(component1), (Interface3 *)0);
136 QCOMPARE(Aggregation::query<Interface1>(component2), component1);
137 QCOMPARE(Aggregation::query<Interface2>(component2), component2);
138 QCOMPARE(Aggregation::query<Interface11>(component2), component1);
139 QCOMPARE(Aggregation::query<Interface3>(component2), (Interface3 *)0);
141 // components that don't belong to an aggregation should be query-able to itself only
142 QObject *component3 = new Interface3;
143 QCOMPARE(Aggregation::query<Interface1>(component3), (Interface1 *)0);
144 QCOMPARE(Aggregation::query<Interface2>(component3), (Interface2 *)0);
145 QCOMPARE(Aggregation::query<Interface11>(component3), (Interface11 *)0);
146 QCOMPARE(Aggregation::query<Interface3>(component3), component3);
150 void tst_Aggregate::queryAll()
152 Aggregation::Aggregate aggregation;
153 QObject *aggObject = &aggregation;
154 Interface1 *component1 = new Interface1;
155 Interface11 *component11 = new Interface11;
156 Interface2 *component2 = new Interface2;
157 aggregation.add(component1);
158 aggregation.add(component11);
159 aggregation.add(component2);
160 QCOMPARE(Aggregation::query_all<Interface1>(&aggregation), QList<Interface1 *>() << component1 << component11);
161 QCOMPARE(Aggregation::query_all<Interface11>(&aggregation), QList<Interface11 *>() << component11);
162 QCOMPARE(Aggregation::query_all<Interface2>(&aggregation), QList<Interface2 *>() << component2);
163 QCOMPARE(Aggregation::query_all<Interface3>(&aggregation), QList<Interface3 *>());
165 QCOMPARE(Aggregation::query_all<Interface1>(aggObject), QList<Interface1 *>() << component1 << component11);
166 QCOMPARE(Aggregation::query_all<Interface11>(aggObject), QList<Interface11 *>() << component11);
167 QCOMPARE(Aggregation::query_all<Interface2>(aggObject), QList<Interface2 *>() << component2);
168 QCOMPARE(Aggregation::query_all<Interface3>(aggObject), QList<Interface3 *>());
170 QCOMPARE(Aggregation::query_all<Interface1>(component1), QList<Interface1 *>() << component1 << component11);
171 QCOMPARE(Aggregation::query_all<Interface11>(component1), QList<Interface11 *>() << component11);
172 QCOMPARE(Aggregation::query_all<Interface2>(component1), QList<Interface2 *>() << component2);
173 QCOMPARE(Aggregation::query_all<Interface3>(component1), QList<Interface3 *>());
175 QCOMPARE(Aggregation::query_all<Interface1>(component11), QList<Interface1 *>() << component1 << component11);
176 QCOMPARE(Aggregation::query_all<Interface11>(component11), QList<Interface11 *>() << component11);
177 QCOMPARE(Aggregation::query_all<Interface2>(component11), QList<Interface2 *>() << component2);
178 QCOMPARE(Aggregation::query_all<Interface3>(component11), QList<Interface3 *>());
180 QCOMPARE(Aggregation::query_all<Interface1>(component2), QList<Interface1 *>() << component1 << component11);
181 QCOMPARE(Aggregation::query_all<Interface11>(component2), QList<Interface11 *>() << component11);
182 QCOMPARE(Aggregation::query_all<Interface2>(component2), QList<Interface2 *>() << component2);
183 QCOMPARE(Aggregation::query_all<Interface3>(component2), QList<Interface3 *>());
186 void tst_Aggregate::parentAggregate()
188 Aggregation::Aggregate aggregation;
189 Interface1 *component1 = new Interface1;
190 Interface11 *component11 = new Interface11;
191 QObject *component2 = new QObject;
192 aggregation.add(component1);
193 aggregation.add(component11);
194 QCOMPARE(Aggregation::Aggregate::parentAggregate(&aggregation), &aggregation);
195 QCOMPARE(Aggregation::Aggregate::parentAggregate(component1), &aggregation);
196 QCOMPARE(Aggregation::Aggregate::parentAggregate(component11), &aggregation);
197 QCOMPARE(Aggregation::Aggregate::parentAggregate(component2), (Aggregation::Aggregate *)0);
200 QTEST_MAIN(tst_Aggregate)
202 #include "tst_aggregate.moc"