Debian Codegraph @

tests/unit/mainview11/tst_mainview.cpp

raw
1 /*
2 * Copyright 2012-2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Christian Dywan <[email protected]>
17 */
18
19 #include <QtCore/QCoreApplication>
20 #include <QtCore/QCryptographicHash>
21 #include <QtCore/QDebug>
22 #include <QtCore/QDir>
23 #include <QtCore/QFileInfo>
24 #include <QtCore/QProcessEnvironment>
25 #include <QtCore/QSettings>
26 #include <QtCore/QStandardPaths>
27 #include <QtCore/QString>
28 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
29 #include <QTextCodec>
30 #else
31 #include <QtCore/QTextCodec>
32 #endif
33 #include <QtCore/QThread>
34 #include <QtQml/QQmlEngine>
35 #include <QtQuick/QQuickItem>
36 #include <QtQuick/QQuickView>
37 #include <QtTest/QSignalSpy>
38 #include <QtTest/QTest>
39 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
40 #include <LomiriToolkit-Qt6/lomiritoolkitmodule.h>
41 #else
42 #include <LomiriToolkit/lomiritoolkitmodule.h>
43 #endif
44 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
45 #include <LomiriToolkit-Qt6/private/ucapplication_p.h>
46 #else
47 #include <LomiriToolkit/private/ucapplication_p.h>
48 #endif
49 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
50 #include <LomiriToolkit-Qt6/private/ucunits_p.h>
51 #else
52 #include <LomiriToolkit/private/ucunits_p.h>
53 #endif
54
55 #include "uctestcase.h"
56
57 UT_USE_NAMESPACE
58
59 class tst_MainView : public QObject
60 {
61 Q_OBJECT
62
63 private:
64 QQuickView *view;
65
66 public:
67 tst_MainView() :
68 view(0)
69 {
70 }
71
72 QQuickItem *loadTest(const QString &document)
73 {
74 // load the document
75 view->setSource(QUrl::fromLocalFile(document));
76 QCoreApplication::processEvents();
77
78 return view->rootObject();
79 }
80
81 QQuickItem *testItem(QQuickItem *that, const QString &identifier)
82 {
83 if (that->property(identifier.toLocal8Bit()).isValid())
84 return that->property(identifier.toLocal8Bit()).value<QQuickItem*>();
85
86 QList<QQuickItem*> children = that->findChildren<QQuickItem*>(identifier);
87 return (children.count() > 0) ? children[0] : 0;
88 }
89
90 private Q_SLOTS:
91
92 void initTestCase()
93 {
94 QString modules(LOMIRI_QML_IMPORT_PATH);
95 QVERIFY(QDir(modules).exists());
96
97 view = new QQuickView;
98 QQmlEngine *quickEngine = view->engine();
99 LomiriToolkitModule::initializeContextProperties(quickEngine);
100
101 view->setGeometry(0,0, UCUnits::instance()->gu(40), UCUnits::instance()->gu(30));
102 //add modules folder so we have access to the plugin from QML
103 QStringList imports = quickEngine->importPathList();
104 imports.prepend(QDir(modules).absolutePath());
105 quickEngine->setImportPathList(imports);
106 }
107
108 void cleanupTestCase()
109 {
110 delete view;
111 }
112
113 void testCase_AppName()
114 {
115 QQuickItem *root = loadTest("AppName.qml");
116 QVERIFY(root);
117 QQuickItem *mainView = root;
118 QString applicationName(mainView->property("applicationName").toString());
119 QCOMPARE(applicationName, QString("org.gnu.wildebeest"));
120 QCOMPARE(applicationName, QCoreApplication::applicationName());
121 QCOMPARE(QString(""), QCoreApplication::organizationName());
122 }
123
124 void testSetApplicationName() {
125 QString appName("com.lomiri.foo");
126 UCApplication::instance()->setApplicationName(appName);
127 QCOMPARE(UCApplication::instance()->applicationName(), appName);
128 QCOMPARE(QCoreApplication::applicationName(), appName);
129 QCOMPARE(QString(""), QCoreApplication::organizationName());
130 }
131
132 void testExpectedDataFolder() {
133 QString appName("net.weight.gain");
134 UCApplication::instance()->setApplicationName(appName);
135 QString dataFolder(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
136 QString xdgDataHome(QProcessEnvironment::systemEnvironment().value("XDG_DATA_HOME",
137 QProcessEnvironment::systemEnvironment().value("HOME") + "/.local/share"));
138 QString expectedDataFolder(xdgDataHome + "/" + appName);
139 QCOMPARE(dataFolder, expectedDataFolder);
140 }
141
142 void testExpectedCacheFolder() {
143 QString appName("cat.long.very");
144 UCApplication::instance()->setApplicationName(appName);
145 QString cacheFolder(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
146 QString xdgCacheHome(QProcessEnvironment::systemEnvironment().value("XDG_CACHE_HOME",
147 QProcessEnvironment::systemEnvironment().value("HOME") + "/.cache"));
148 QString expectedCacheFolder(xdgCacheHome + "/" + appName);
149 QCOMPARE(cacheFolder, expectedCacheFolder);
150 }
151
152 void testLocalStorage() {
153 QQuickItem *root = loadTest("LocalStorage.qml");
154 QVERIFY(root);
155 QQuickItem *mainView = root;
156 QString applicationName(mainView->property("applicationName").toString());
157 QCOMPARE(applicationName, QString("tv.island.pacific"));
158 QCOMPARE(applicationName, QCoreApplication::applicationName());
159 QCOMPARE(QString(""), QCoreApplication::organizationName());
160 QString dataFolder(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
161 QString databaseFolder(dataFolder + "/Databases");
162 QVERIFY(QFile::exists(databaseFolder));
163 QString hash(QCryptographicHash::hash("pacific.island.tv", QCryptographicHash::Md5).toHex());
164 QString database(databaseFolder + "/" + hash + ".sqlite");
165 QVERIFY(QFile::exists(database));
166 }
167
168 QString getConfFile(QString applicationName) {
169 QString configFolder(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
170 QString subFolder(configFolder + "/" + applicationName);
171 QString filename(subFolder + "/" + applicationName + ".conf");
172 return filename;
173 }
174
175 void testQSettings() {
176 QString applicationName("i.prefer.pi");
177 // Delete file if it exists to avoid false positives
178 QString filename(getConfFile(applicationName));
179 QFile::remove(filename);
180
181 UCApplication::instance()->setApplicationName(applicationName);
182 // QSettings with defaults
183 QSettings mySettings;
184 mySettings.setValue("spam", "eggs");
185 // Force writing to disk
186 mySettings.sync();
187 QVERIFY(QFile::exists(filename));
188 }
189
190 void testNoWarnings_bug186065() {
191 // An empty MainView would suffice
192 QScopedPointer<LomiriTestCase>testCase (new LomiriTestCase("AppName.qml"));
193
194 // No warnings from QML
195 QCOMPARE(testCase->warnings(), 0);
196 }
197
198 void testWindowTitleFromPage() {
199 QScopedPointer<LomiriTestCase> testCase(new LomiriTestCase("PageTitle.qml"));
200 QQuickItem *page = testCase->findItem<QQuickItem*>("page");
201 QCOMPARE(QString("Once upon a time"), page->property("title").toString());
202 QCOMPARE(testCase->title(), page->property("title").toString());
203 }
204
205 void testWindowTitleFromStack() {
206 QScopedPointer<LomiriTestCase> testCase(new LomiriTestCase("PageStack.qml"));
207 QQuickItem *page = testCase->findItem<QQuickItem*>("page");
208 QCOMPARE(QString("Far far away"), page->property("title").toString());
209 QCOMPARE(testCase->title(), page->property("title").toString());
210 }
211
212 void testWindowTitleFromTabs() {
213 QScopedPointer<LomiriTestCase> testCase(new LomiriTestCase("TabsTitle.qml"));
214 QQuickItem *page = testCase->findItem<QQuickItem*>("page");
215 QCOMPARE(QString("Long long ago"), page->property("title").toString());
216 QCOMPARE(testCase->title(), page->property("title").toString());
217 }
218 };
219
220 QTEST_MAIN(tst_MainView)
221
222 #include "tst_mainview.moc"