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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/*
* Copyright 2012-2013 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/QCoreApplication>
#include <QtCore/QCryptographicHash>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QProcessEnvironment>
#include <QtCore/QSettings>
#include <QtCore/QStandardPaths>
#include <QtCore/QString>
#include <QtCore/QTextCodec>
#include <QtCore/QThread>
#include <QtQml/QQmlEngine>
#include <QtQuick/QQuickItem>
#include <QtQuick/QQuickView>
#include <QtTest/QSignalSpy>
#include <QtTest/QTest>
#include <LomiriToolkit/lomiritoolkitmodule.h>
#include <LomiriToolkit/private/ucapplication_p.h>
#include <LomiriToolkit/private/ucunits_p.h>
#include "uctestcase.h"
UT_USE_NAMESPACE
class tst_MainView : public QObject
{
Q_OBJECT
private:
QQuickView *view;
public:
tst_MainView() :
view(0)
{
}
QQuickItem *loadTest(const QString &document)
{
// load the document
view->setSource(QUrl::fromLocalFile(document));
QCoreApplication::processEvents();
return view->rootObject();
}
QQuickItem *testItem(QQuickItem *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 initTestCase()
{
QString modules(LOMIRI_QML_IMPORT_PATH);
QVERIFY(QDir(modules).exists());
view = new QQuickView;
QQmlEngine *quickEngine = view->engine();
LomiriToolkitModule::initializeContextProperties(quickEngine);
view->setGeometry(0,0, UCUnits::instance()->gu(40), UCUnits::instance()->gu(30));
//add modules folder so we have access to the plugin from QML
QStringList imports = quickEngine->importPathList();
imports.prepend(QDir(modules).absolutePath());
quickEngine->setImportPathList(imports);
}
void cleanupTestCase()
{
delete view;
}
void testCase_AppName()
{
QQuickItem *root = loadTest("AppName.qml");
QVERIFY(root);
QQuickItem *mainView = root;
QString applicationName(mainView->property("applicationName").toString());
QCOMPARE(applicationName, QString("org.gnu.wildebeest"));
QCOMPARE(applicationName, QCoreApplication::applicationName());
QCOMPARE(QString(""), QCoreApplication::organizationName());
}
void testSetApplicationName() {
QString appName("com.lomiri.foo");
UCApplication::instance()->setApplicationName(appName);
QCOMPARE(UCApplication::instance()->applicationName(), appName);
QCOMPARE(QCoreApplication::applicationName(), appName);
QCOMPARE(QString(""), QCoreApplication::organizationName());
}
void testExpectedDataFolder() {
QString appName("net.weight.gain");
UCApplication::instance()->setApplicationName(appName);
QString dataFolder(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
QString xdgDataHome(QProcessEnvironment::systemEnvironment().value("XDG_DATA_HOME",
QProcessEnvironment::systemEnvironment().value("HOME") + "/.local/share"));
QString expectedDataFolder(xdgDataHome + "/" + appName);
QCOMPARE(dataFolder, expectedDataFolder);
}
void testExpectedCacheFolder() {
QString appName("cat.long.very");
UCApplication::instance()->setApplicationName(appName);
QString cacheFolder(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
QString xdgCacheHome(QProcessEnvironment::systemEnvironment().value("XDG_CACHE_HOME",
QProcessEnvironment::systemEnvironment().value("HOME") + "/.cache"));
QString expectedCacheFolder(xdgCacheHome + "/" + appName);
QCOMPARE(cacheFolder, expectedCacheFolder);
}
void testLocalStorage() {
QQuickItem *root = loadTest("LocalStorage.qml");
QVERIFY(root);
QQuickItem *mainView = root;
QString applicationName(mainView->property("applicationName").toString());
QCOMPARE(applicationName, QString("tv.island.pacific"));
QCOMPARE(applicationName, QCoreApplication::applicationName());
QCOMPARE(QString(""), QCoreApplication::organizationName());
QString dataFolder(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
QString databaseFolder(dataFolder + "/Databases");
QVERIFY(QFile::exists(databaseFolder));
QString hash(QCryptographicHash::hash("pacific.island.tv", QCryptographicHash::Md5).toHex());
QString database(databaseFolder + "/" + hash + ".sqlite");
QVERIFY(QFile::exists(database));
}
QString getConfFile(QString applicationName) {
QString configFolder(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
QString subFolder(configFolder + "/" + applicationName);
QString filename(subFolder + "/" + applicationName + ".conf");
return filename;
}
void testQSettings() {
QString applicationName("i.prefer.pi");
// Delete file if it exists to avoid false positives
QString filename(getConfFile(applicationName));
QFile::remove(filename);
UCApplication::instance()->setApplicationName(applicationName);
// QSettings with defaults
QSettings mySettings;
mySettings.setValue("spam", "eggs");
// Force writing to disk
mySettings.sync();
QVERIFY(QFile::exists(filename));
}
void testNoWarnings_bug186065() {
// An empty MainView would suffice
QScopedPointer<LomiriTestCase>testCase (new LomiriTestCase("AppName.qml"));
// No warnings from QML
QCOMPARE(testCase->warnings(), 0);
}
void testWindowTitleFromPage() {
QScopedPointer<LomiriTestCase> testCase(new LomiriTestCase("PageTitle.qml"));
QQuickItem *page = testCase->findItem<QQuickItem*>("page");
QCOMPARE(QString("Once upon a time"), page->property("title").toString());
QCOMPARE(testCase->title(), page->property("title").toString());
}
void testWindowTitleFromStack() {
QScopedPointer<LomiriTestCase> testCase(new LomiriTestCase("PageStack.qml"));
QQuickItem *page = testCase->findItem<QQuickItem*>("page");
QCOMPARE(QString("Far far away"), page->property("title").toString());
QCOMPARE(testCase->title(), page->property("title").toString());
}
void testWindowTitleFromTabs() {
QScopedPointer<LomiriTestCase> testCase(new LomiriTestCase("TabsTitle.qml"));
QQuickItem *page = testCase->findItem<QQuickItem*>("page");
QCOMPARE(QString("Long long ago"), page->property("title").toString());
QCOMPARE(testCase->title(), page->property("title").toString());
}
};
QTEST_MAIN(tst_MainView)
#include "tst_mainview.moc"
|