File: tst_mainwindow.cpp

package info (click to toggle)
lomiri-ui-toolkit 1.3.5010%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,900 kB
  • sloc: cpp: 85,772; python: 5,528; sh: 1,364; javascript: 919; ansic: 573; makefile: 204
file content (175 lines) | stat: -rw-r--r-- 6,515 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
/*
 * Copyright 2012-2016 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Christian Dywan <christian.dywan@canonical.com>
 */

#include <QtCore/QString>
#include <QtCore/QTextCodec>
#include <QtCore/QStandardPaths>
#include <QtCore/QProcessEnvironment>
#include <QtCore/QDebug>
#include <QtTest/QTest>
#include <QtTest/QSignalSpy>
#include <QtCore/QCoreApplication>
#include <QtQml/QQmlEngine>
#include <QtQuick/QQuickView>
#include <QtQuick/QQuickItem>
#include <QtCore/QThread>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QCryptographicHash>
#include <QtCore/QSettings>

#include <QtGui/QGuiApplication>
#include <QtQml/QQmlContext>
#include <QtQml/QQmlComponent>

#include <LomiriToolkit/lomiritoolkitmodule.h>
#include <LomiriToolkit/private/quickutils_p.h>
#include <LomiriToolkit/private/ucapplication_p.h>
#include <LomiriToolkit/private/ucmainwindow_p.h>
#include <LomiriToolkit/private/ucunits_p.h>

UT_USE_NAMESPACE

class tst_MainWindow : public QObject
{
    Q_OBJECT

public:
    tst_MainWindow()
    {
    }

    QQuickWindow *loadTest(const QString &document)
    {
        // Can't use LomiriTestCase: We need a Window root item
        QPointer<QQmlEngine> engine(new QQmlEngine());
        QString modulePath(LOMIRI_QML_IMPORT_PATH);
        if (!QDir(modulePath).exists()) {
            qWarning("'%s' doesn't exist", qPrintable(modulePath));
            return 0;
        }
        engine->addImportPath(modulePath);
        LomiriToolkitModule::initializeContextProperties(engine);
        QPointer<QQmlComponent> component(new QQmlComponent(engine));
        component->loadUrl(QUrl::fromLocalFile(document), QQmlComponent::Asynchronous);
        while (component->isLoading())
            QCoreApplication::processEvents();
        QObject *toplevel(component->create());
        if (component->errorString() != "") {
            qWarning("%s", qPrintable(component->errorString()));
            return 0;
        }
        QQuickWindow* window(qobject_cast<QQuickWindow *>(toplevel));
        if (window)
            engine->setIncubationController(window->incubationController());
        else {
            QQuickItem *rootItem = qobject_cast<QQuickItem *>(toplevel);
            if (rootItem) {
                QQuickView *view(new QQuickView(engine, 0));
                window = view;
                view->setResizeMode(QQuickView::SizeRootObjectToView);
                view->setContent(document, component, rootItem);
            }
        }
        return window;
    }

    QQuickItem *testItem(QObject *that, const QString &identifier)
    {
        if (that->property(identifier.toLocal8Bit()).isValid())
            return that->property(identifier.toLocal8Bit()).value<QQuickItem*>();

        QList<QQuickItem*> children = that->findChildren<QQuickItem*>(identifier);
        return (children.count() > 0) ? children[0] : 0;
    }

private Q_SLOTS:

    void cleanup()
    {
        // Delete engine, and thereby also the UCApplication instance
        QObject* engine(UCApplication::instance()->parent());
        delete engine;
        QCoreApplication::setApplicationName(QStringLiteral(""));
        QCoreApplication::setOrganizationName(QStringLiteral(""));
    }

    // Note: tests/unit/mainview13 contains the UCApplication bits

    void testCase_AppName()
    {
        // Sanity check: no name set yet
        QCOMPARE(QStringLiteral("mainwindow"), QCoreApplication::applicationName());

        QString applicationName("org.gnu.wildebeest");
        QQuickWindow *mainWindow(loadTest("AppName.qml"));
        QVERIFY(mainWindow);
        QCOMPARE(applicationName, mainWindow->property("applicationName").toString());
        QCOMPARE(applicationName, QCoreApplication::applicationName());
        QCOMPARE(QString(""), QCoreApplication::organizationName());
    }

    void testCase_OrganizationName()
    {
        // Sanity check: no organization set yet
        QCOMPARE(QStringLiteral(""), QCoreApplication::organizationName());

        QString applicationName("tv.island.pacific");
        QString organizationName("pacifist");
        QQuickWindow *mainWindow(loadTest("OrganizationName.qml"));
        QVERIFY(mainWindow);
        QCOMPARE(applicationName, mainWindow->property("applicationName").toString());
        QCOMPARE(applicationName, QCoreApplication::applicationName());
        QCOMPARE(organizationName, mainWindow->property("organizationName").toString());
        QCOMPARE(organizationName, QCoreApplication::organizationName());
    }

    void testCase_NoOrganizationName()
    {
        // Sanity check: no organization set yet
        QCOMPARE(QStringLiteral(""), QCoreApplication::organizationName());

        // Custom values set outside of QML
        QString applicationName("com.example.random");
        QString organizationName(QStringLiteral("Example"));
        QCoreApplication::setApplicationName(applicationName);
        QCoreApplication::setOrganizationName(organizationName);

        QQuickWindow *mainWindow(loadTest("VisualRoot.qml"));
        QVERIFY(mainWindow);
        // MainView shouldn't interfere as applicationName wasn't set in QML
        QCOMPARE(QStringLiteral(""), mainWindow->property("applicationName").toString());
        QCOMPARE(applicationName, QCoreApplication::applicationName());
        QCOMPARE(QStringLiteral(""), mainWindow->property("organizationName").toString());
        QCOMPARE(organizationName, QCoreApplication::organizationName());
    }

    void testCase_VisualRoot()
    {
        QQuickWindow *mainWindow(loadTest("VisualRoot.qml"));
        QVERIFY(mainWindow);
        QQuickItem* myLabel(testItem(mainWindow, "myLabel"));
        QQuickItem* visualRoot(QuickUtils::instance()->rootItem(myLabel));
        QQuickItem* myRoot(testItem(mainWindow, "myRoot"));
        QCOMPARE(visualRoot, myRoot);
    }
};

QTEST_MAIN(tst_MainWindow)

#include "tst_mainwindow.moc"