summaryrefslogtreecommitdiff
path: root/Source/WebKit2/UIProcess/API/qt/tests/qrawwebview/tst_qrawwebview.cpp
blob: 208288d1e6d0ece5be7ffed3a1efe12edc5c02d8 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
    Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
#include "../util.h"

#include <QImage>
#include <QMatrix4x4>
#include <QOpenGLContext>
#include <QSize>
#include <QWindow>
#include <QtTest/QtTest>

#include <WebKit2/WKContext.h>
#include <WebKit2/WKPageGroup.h>
#include <WebKit2/WKPageLoadTypes.h>
#include <WebKit2/WKPreferences.h>
#include <WebKit2/WKPreferencesPrivate.h>
#include <WebKit2/WKStringQt.h>
#include <WebKit2/WKURL.h>
#include <WebKit2/qrawwebview_p.h>

static WKContextRef webContext()
{
    static WKContextRef result = WKContextCreate();
    return result;
}

static WKPageGroupRef createWebPageGroup(const QString& name)
{
    WKPageGroupRef pageGroup =WKPageGroupCreateWithIdentifier(WKStringCreateWithQString(name));
    WKPreferencesRef preferences = WKPageGroupGetPreferences(pageGroup);
    WKPreferencesSetAcceleratedCompositingEnabled(preferences, true);
    WKPreferencesSetFrameFlatteningEnabled(preferences, true);

    return pageGroup;
}

static WKPageGroupRef webPageGroup(const QString& name)
{
    static WKPageGroupRef result = createWebPageGroup(name);
    return result;
}

class WebView : public QObject, public QRawWebViewClient {
    Q_OBJECT
public:
    WebView(const QSize& size, bool transparent = false)
    {
        m_webView = new QRawWebView(webContext(), webPageGroup(QString()), this);
        m_webView->setTransparentBackground(transparent);
        m_webView->create();

        WKPageLoaderClient loaderClient;
        memset(&loaderClient, 0, sizeof(WKPageLoaderClient));
        loaderClient.version = kWKPageLoaderClientCurrentVersion;
        loaderClient.clientInfo = this;
        loaderClient.didLayout = WebView::didLayout;

        WKPageSetPageLoaderClient(m_webView->pageRef(), &loaderClient);
        WKPageListenForLayoutMilestones(m_webView->pageRef(), kWKDidFirstVisuallyNonEmptyLayout);
        WKPageSetUseFixedLayout(m_webView->pageRef(), true);

        m_webView->setSize(size);
        m_webView->setFocused(true);
        m_webView->setVisible(true);
        m_webView->setActive(true);
    }

    ~WebView() { delete m_webView; }

    void load(const QString& html)
    {
        m_frameLoaded = false;
        WKPageLoadURL(m_webView->pageRef(), WKURLCreateWithUTF8CString(html.toLocal8Bit().data()));
        QVERIFY(::waitForSignal(this, SIGNAL(loaded()), 5000));
    }

    void setDrawBackground(bool value) { m_webView->setDrawBackground(value); }
    void setTransparentBackground(bool value) { m_webView->setTransparentBackground(value); }

    virtual void viewNeedsDisplay(const QRect&)
    {
        m_webView->paint(QMatrix4x4(), 1, 0);
    }

    virtual void viewRequestedScroll(const QPoint&) { }
    virtual void viewProcessCrashed() { }
    virtual void viewProcessRelaunched() { }
    virtual void viewContentSizeChanged(const QSize&) { }
    virtual void viewRequestedCursorOverride(const QCursor&) { }
    virtual void doneWithKeyEvent(const QKeyEvent*, bool wasHandled) { }
    virtual void doneWithTouchEvent(const QTouchEvent*, bool wasHandled) { }

    void frameLoaded()
    {
        m_frameLoaded = true;
        WKPageForceRepaint(m_webView->pageRef(), this, finishForceRepaint);
    }

    void onRepaintDone()
    {
        emit loaded();
    }

    static void finishForceRepaint(WKErrorRef, void* context)
    {
        static_cast<WebView*>(context)->onRepaintDone();
    }

    static void didLayout(WKPageRef page, WKLayoutMilestones milestones, WKTypeRef userData, const void *clientInfo)
    {
        static_cast<WebView*>(const_cast<void*>(clientInfo))->frameLoaded();
    }

Q_SIGNALS:
    void loaded();

private:
    QRawWebView* m_webView;
    bool m_frameLoaded;
};

static bool compareImages(const QImage& i1, const QImage& i2, int count)
{
    if (i1.size() != i2.size())
        return false;
    for (int x = 0; x < count; ++x) {
        for (int y = 0; y < count; ++y) {
            QPoint point(x * i1.width() / count, y * i1.height() / count);
            if (i1.pixel(point) != i2.pixel(point))
                return false;
        }
    }

    return true;
}

class tst_qrawwebview : public QObject {
    Q_OBJECT
public:
    tst_qrawwebview()
        : m_resourceDir(QString::fromLatin1(TESTS_SOURCE_DIR "/html/resources"))
        , m_baseUrl(QUrl::fromLocalFile(TESTS_SOURCE_DIR "/html").toString())
    {
        addQtWebProcessToPath();
    }

private Q_SLOTS:
    void paint() { run(&tst_qrawwebview::doPaint, m_resourceDir + "/qwkview_paint.png"); }
    void noBackground1() { run(&tst_qrawwebview::doNoBackground1, m_resourceDir + "/qwkview_noBackground1.png"); }
    void noBackground2() { run(&tst_qrawwebview::doNoBackground2, m_resourceDir + "/qwkview_noBackground1.png"); }
    void noBackground3() { run(&tst_qrawwebview::doNoBackground3, m_resourceDir + "/qwkview_noBackground3.png"); }

private:
    const QString m_resourceDir;
    const QString m_baseUrl;

    void doPaint(const QSize& size);
    void doNoBackground1(const QSize& size);
    void doNoBackground2(const QSize& size);
    void doNoBackground3(const QSize& size);

    typedef void (tst_qrawwebview::*PaintMethod)(const QSize& size);
    void run(PaintMethod, const QString& expectation);
};

void tst_qrawwebview::doPaint(const QSize& size)
{
    WebView view(size);
    view.load(m_baseUrl + "/redsquare.html");
}

void tst_qrawwebview::doNoBackground1(const QSize& size)
{
    WebView view(size, true);
    view.load(m_baseUrl + "/redsquare.html");
    view.load(m_baseUrl + "/bluesquare.html");
}

void tst_qrawwebview::doNoBackground2(const QSize& size)
{
    WebView view1(size, true);
    view1.load(m_baseUrl + "/redsquare.html");

    WebView view2(size, true);
    view2.load(m_baseUrl + "/bluesquare.html");
}

void tst_qrawwebview::doNoBackground3(const QSize& size)
{
    WebView view1(size, false);
    view1.load(m_baseUrl + "/redsquare.html");

    WebView view2(size, true);
    view2.load(m_baseUrl + "/bluesquare.html");
}

void tst_qrawwebview::run(PaintMethod method, const QString& expectation)
{
    QWindow window;
    window.setSurfaceType(QSurface::OpenGLSurface);
    window.setGeometry(0, 0, 200, 200);
    window.create();

    QOpenGLContext context;
    context.create();
    context.makeCurrent(&window);

    glViewport(0, 0, window.size().width(), window.size().height());
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    (this->*method)(window.size());

    QImage image(window.size(), QImage::Format_ARGB32_Premultiplied);
    glReadPixels(0, 0, window.size().width(), window.size().height(), GL_RGBA, GL_UNSIGNED_BYTE, image.bits());

    QVERIFY(compareImages(QImage(expectation), image.rgbSwapped(), 5));
}

QTEST_MAIN(tst_qrawwebview)

#include "tst_qrawwebview.moc"