File indexing completed on 2024-05-19 05:34:39

0001 // SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #include <QTest>
0005 #include <plasmaquick/sharedqmlengine.h>
0006 
0007 using namespace PlasmaQuick;
0008 
0009 class SharedQmlEngineTest : public QObject
0010 {
0011     Q_OBJECT
0012 
0013 private Q_SLOTS:
0014     void testSettingTranslationDomain()
0015     {
0016         std::unique_ptr<SharedQmlEngine> obj(new SharedQmlEngine());
0017 
0018         const QString testDomain = QStringLiteral("testme");
0019         QVERIFY(obj->translationDomain().isEmpty());
0020         obj->setTranslationDomain(testDomain);
0021         QCOMPARE(obj->translationDomain(), testDomain);
0022         obj.reset(new SharedQmlEngine());
0023         QVERIFY(obj->translationDomain().isEmpty());
0024     }
0025 
0026     void testUsingSameEngine()
0027     {
0028         std::unique_ptr<SharedQmlEngine> obj1(new SharedQmlEngine());
0029         std::unique_ptr<SharedQmlEngine> obj2(new SharedQmlEngine());
0030 
0031         QVERIFY(obj1->engine() == obj2->engine());
0032         QVERIFY(obj1->rootContext() != obj2->rootContext());
0033     }
0034 
0035     void testDeletingEngine()
0036     {
0037         std::unique_ptr<SharedQmlEngine> obj1(new SharedQmlEngine());
0038         std::weak_ptr<QQmlEngine> weakPtr(obj1->engine());
0039         QVERIFY(weakPtr.lock());
0040 
0041         // The one in obj1
0042         QCOMPARE(weakPtr.use_count(), 1);
0043 
0044         {
0045             std::unique_ptr<SharedQmlEngine> obj2(new SharedQmlEngine());
0046             // The one in obj1 and in obj2
0047             QCOMPARE(weakPtr.use_count(), 2);
0048         }
0049 
0050         obj1.reset(nullptr);
0051         // Our object is deleted and as the last strong ref it should ensure everything is deleted.
0052         QVERIFY(!weakPtr.lock());
0053         QCOMPARE(weakPtr.use_count(), 0);
0054     }
0055 };
0056 
0057 QTEST_MAIN(SharedQmlEngineTest)
0058 
0059 #include "sharedqmlenginetest.moc"