File: tst_QmlTests.cpp

package info (click to toggle)
morph-browser 1.1.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,424 kB
  • sloc: cpp: 12,201; javascript: 2,042; xml: 92; makefile: 43
file content (200 lines) | stat: -rw-r--r-- 7,143 bytes parent folder | download
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
/*
 * Copyright 2013-2017 Canonical Ltd.
 *
 * This file is part of webbrowser-app.
 *
 * webbrowser-app is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * webbrowser-app 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Qt
#include <QtCore/QFile>
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtCore/QTemporaryDir>
#include <QtQml/QQmlEngine>
#include <QtQml/QtQml>
#include <QtQuickTest/QtQuickTest>

// local
#include "bookmarks-model.h"
#include "bookmarks-folderlist-model.h"
#include "favicon-fetcher.h"
#include "file-operations.h"
#include "history-domain-model.h"
#include "history-domainlist-model.h"
#include "history-model.h"
#include "history-lastvisitdatelist-model.h"
#include "limit-proxy-model.h"
#include "reparenter.h"
#include "searchengine.h"
#include "tabs-model.h"
#include "text-search-filter-model.h"
#include "url-utils.h"

class TestContext : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString testDir1 READ testDir1 CONSTANT)
    Q_PROPERTY(QString testDir2 READ testDir2 CONSTANT)

public:
    explicit TestContext(QObject* parent=0)
        : QObject(parent)
    {}

    QString testDir1() const
    {
        return m_testDir1.path();
    }

    QString testDir2() const
    {
        return m_testDir2.path();
    }

    Q_INVOKABLE bool writeSearchEngineDescription(
        const QString& path, const QString& filename, const QString& name,
        const QString& description, const QString& urlTemplate)
    {
        QFile file(QDir(path).absoluteFilePath(QString("%1.xml").arg(filename)));
        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QTextStream out(&file);
            out << "<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\">";
            out << "<ShortName>" << name << "</ShortName>";
            out << "<Description>" << description << "</Description>";
            out << "<Url type=\"text/html\" template=\"" << urlTemplate << "\"/>";
            out << "</OpenSearchDescription>";
            file.close();
            return true;
        } else {
            return false;
        }
    }

    Q_INVOKABLE bool writeInvalidSearchEngineDescription(const QString& path, const QString& filename)
    {
        QFile file(QDir(path).absoluteFilePath(QString("%1.xml").arg(filename)));
        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QTextStream out(&file);
            out << "invalid";
            file.close();
            return true;
        } else {
            return false;
        }
    }

    Q_INVOKABLE bool deleteSearchEngineDescription(const QString& path, const QString& filename)
    {
        return QFile(QDir(path).absoluteFilePath(QString("%1.xml").arg(filename))).remove();
    }

    Q_INVOKABLE bool createFile(const QString& filePath) {
        // create all the directories necessary for the file to be created
        QFileInfo fileInfo(filePath);
        if (!QFileInfo::exists(fileInfo.path())) {
          QDir::root().mkpath(fileInfo.path());
        }

        QFile file(fileInfo.absoluteFilePath());
        return file.open(QIODevice::WriteOnly | QIODevice::Text);
    }

    Q_INVOKABLE bool removeDirectory(const QString& path) {
        QDir dir(path);
        return dir.removeRecursively();
    }

private:
    QTemporaryDir m_testDir1;
    QTemporaryDir m_testDir2;
};

class HistoryModelMock : public HistoryModel {
    Q_OBJECT

public:
    static bool compareHistoryEntries(const HistoryEntry& a, const HistoryEntry& b) {
        return a.lastVisit < b.lastVisit;
    }

    Q_INVOKABLE int addByDate(const QUrl& url, const QString& title, const QDateTime& date)
    {
        int index = getEntryIndex(url);
        int visitsToAdd = 1;
        if (index == -1) {
            add(url, title, QString());
            index = getEntryIndex(url);
            visitsToAdd = 0;
        }

        // Since this is useful only for testing and efficiency is not critical
        // we reorder the model and reset it every time we add a new item by date
        // to keep things simple.
        beginResetModel();
        HistoryEntry entry = m_entries.takeAt(index);
        entry.lastVisit = date;
        entry.visits = entry.visits + visitsToAdd;
        m_entries.append(entry);
        std::sort(m_entries.begin(), m_entries.end(), compareHistoryEntries);
        endResetModel();

        updateExistingEntryInDatabase(entry);

        return entry.visits;
    }
};

#define MAKE_SINGLETON_FACTORY(type) \
    static QObject* type##_singleton_factory(QQmlEngine* engine, QJSEngine* scriptEngine) { \
        Q_UNUSED(engine); \
        Q_UNUSED(scriptEngine); \
        return new type(); \
    }

MAKE_SINGLETON_FACTORY(FileOperations)
MAKE_SINGLETON_FACTORY(BookmarksModel)
MAKE_SINGLETON_FACTORY(HistoryModelMock)
MAKE_SINGLETON_FACTORY(TestContext)
MAKE_SINGLETON_FACTORY(Reparenter)
MAKE_SINGLETON_FACTORY(UrlUtils)

int main(int argc, char** argv)
{
    const char* commonUri = "webbrowsercommon.private";
    qmlRegisterType<FaviconFetcher>(commonUri, 0, 1, "FaviconFetcher");
    qmlRegisterSingletonType<FileOperations>(commonUri, 0, 1, "FileOperations", FileOperations_singleton_factory);
    qmlRegisterSingletonType<UrlUtils>(commonUri, 0, 1, "UrlUtils", UrlUtils_singleton_factory);

    const char* browserUri = "webbrowserapp.private";
    qmlRegisterType<SearchEngine>(browserUri, 0, 1, "SearchEngine");
    qmlRegisterType<TabsModel>(browserUri, 0, 1, "TabsModel");
    qmlRegisterSingletonType<BookmarksModel>(browserUri, 0, 1, "BookmarksModel", BookmarksModel_singleton_factory);
    qmlRegisterType<BookmarksFolderListModel>(browserUri, 0, 1, "BookmarksFolderListModel");
    qmlRegisterSingletonType<HistoryModel>(browserUri, 0, 1, "HistoryModel", HistoryModelMock_singleton_factory);
    qmlRegisterType<HistoryDomainModel>(browserUri, 0, 1, "HistoryDomainModel");
    qmlRegisterType<HistoryDomainListModel>(browserUri, 0, 1, "HistoryDomainListModel");
    qmlRegisterType<HistoryLastVisitDateListModel>(browserUri, 0, 1, "HistoryLastVisitDateListModel");
    qmlRegisterType<LimitProxyModel>(browserUri, 0, 1, "LimitProxyModel");
    qmlRegisterType<TextSearchFilterModel>(browserUri, 0, 1, "TextSearchFilterModel");
    qmlRegisterSingletonType<Reparenter>(browserUri, 0, 1, "Reparenter", Reparenter_singleton_factory);
    qmlRegisterSingletonType<UrlUtils>(browserUri, 0, 1, "UrlUtils", UrlUtils_singleton_factory);

    const char* testUri = "webbrowsertest.private";
    qmlRegisterSingletonType<TestContext>(testUri, 0, 1, "TestContext", TestContext_singleton_factory);

    return quick_test_main(argc, argv, "QmlTests", nullptr);
}

#include "tst_QmlTests.moc"