// Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include #include //TESTED_COMPONENT=src/libs/aggregation 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; QPointer component1; QPointer 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(&aggregation), component1); QCOMPARE(Aggregation::query(&aggregation), component2); QCOMPARE(Aggregation::query(&aggregation), component1); QCOMPARE(Aggregation::query(&aggregation), (Interface3 *)0); QCOMPARE(Aggregation::query(aggObject), component1); QCOMPARE(Aggregation::query(aggObject), component2); QCOMPARE(Aggregation::query(aggObject), component1); QCOMPARE(Aggregation::query(aggObject), (Interface3 *)0); QCOMPARE(Aggregation::query(component1), component1); QCOMPARE(Aggregation::query(component1), component2); QCOMPARE(Aggregation::query(component1), component1); QCOMPARE(Aggregation::query(component1), (Interface3 *)0); QCOMPARE(Aggregation::query(component2), component1); QCOMPARE(Aggregation::query(component2), component2); QCOMPARE(Aggregation::query(component2), component1); QCOMPARE(Aggregation::query(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(component3), (Interface1 *)0); QCOMPARE(Aggregation::query(component3), (Interface2 *)0); QCOMPARE(Aggregation::query(component3), (Interface11 *)0); QCOMPARE(Aggregation::query(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(&aggregation), QList() << component1 << component11); QCOMPARE(Aggregation::query_all(&aggregation), QList() << component11); QCOMPARE(Aggregation::query_all(&aggregation), QList() << component2); QCOMPARE(Aggregation::query_all(&aggregation), QList()); QCOMPARE(Aggregation::query_all(aggObject), QList() << component1 << component11); QCOMPARE(Aggregation::query_all(aggObject), QList() << component11); QCOMPARE(Aggregation::query_all(aggObject), QList() << component2); QCOMPARE(Aggregation::query_all(aggObject), QList()); QCOMPARE(Aggregation::query_all(component1), QList() << component1 << component11); QCOMPARE(Aggregation::query_all(component1), QList() << component11); QCOMPARE(Aggregation::query_all(component1), QList() << component2); QCOMPARE(Aggregation::query_all(component1), QList()); QCOMPARE(Aggregation::query_all(component11), QList() << component1 << component11); QCOMPARE(Aggregation::query_all(component11), QList() << component11); QCOMPARE(Aggregation::query_all(component11), QList() << component2); QCOMPARE(Aggregation::query_all(component11), QList()); QCOMPARE(Aggregation::query_all(component2), QList() << component1 << component11); QCOMPARE(Aggregation::query_all(component2), QList() << component11); QCOMPARE(Aggregation::query_all(component2), QList() << component2); QCOMPARE(Aggregation::query_all(component2), QList()); } void tst_Aggregate::parentAggregate() { Aggregation::Aggregate aggregation; Aggregation::Aggregate aggregation2; 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); // 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); } QTEST_GUILESS_MAIN(tst_Aggregate) #include "tst_aggregate.moc"