OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / tests / auto / aggregation / tst_aggregate.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include <aggregate.h>
35
36 #include <QtTest/QtTest>
37
38 //TESTED_COMPONENT=src/libs/aggregation
39
40 class tst_Aggregate : public QObject
41 {
42     Q_OBJECT
43
44 private slots:
45     void deleteAggregation();
46     void queryAggregation();
47     void queryAll();
48     void parentAggregate();
49 };
50
51 class Interface1 : public QObject
52 {
53     Q_OBJECT
54 };
55
56 class Interface11 : public Interface1
57 {
58     Q_OBJECT
59 };
60
61 class Interface2 : public QObject
62 {
63     Q_OBJECT
64 };
65
66 class Interface3 : public QObject
67 {
68     Q_OBJECT
69 };
70
71 void tst_Aggregate::deleteAggregation()
72 {
73     QPointer<Aggregation::Aggregate> aggregation;
74     QPointer<QObject> component1;
75     QPointer<QObject> component2;
76
77     aggregation = new Aggregation::Aggregate;
78     component1 = new Interface1;
79     component2 = new Interface2;
80     aggregation->add(component1);
81     aggregation->add(component2);
82     delete aggregation;
83     QVERIFY(aggregation == 0);
84     QVERIFY(component1 == 0);
85     QVERIFY(component2 == 0);
86
87     aggregation = new Aggregation::Aggregate;
88     component1 = new Interface1;
89     component2 = new Interface2;
90     aggregation->add(component1);
91     aggregation->add(component2);
92     delete component1;
93     QVERIFY(aggregation == 0);
94     QVERIFY(component1 == 0);
95     QVERIFY(component2 == 0);
96
97     aggregation = new Aggregation::Aggregate;
98     component1 = new Interface1;
99     component2 = new Interface2;
100     aggregation->add(component1);
101     aggregation->add(component2);
102     delete component2;
103     QVERIFY(aggregation == 0);
104     QVERIFY(component1 == 0);
105     QVERIFY(component2 == 0);
106
107     // if a component doesn't belong to an aggregation, it should simply delete itself
108     component1 = new Interface1;
109     delete component1;
110     QVERIFY(component1 == 0);
111 }
112
113 void tst_Aggregate::queryAggregation()
114 {
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);
125
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);
130
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);
135
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);
140
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);
147     delete component3;
148 }
149
150 void tst_Aggregate::queryAll()
151 {
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 *>());
164
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 *>());
169
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 *>());
174
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 *>());
179
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 *>());
184 }
185
186 void tst_Aggregate::parentAggregate()
187 {
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);
198 }
199
200 QTEST_MAIN(tst_Aggregate)
201
202 #include "tst_aggregate.moc"