File: WebViewTestGtk.cpp

package info (click to toggle)
webkit2gtk 2.50.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 445,828 kB
  • sloc: cpp: 3,799,604; javascript: 197,921; ansic: 161,360; python: 49,141; asm: 21,987; ruby: 18,540; perl: 16,722; xml: 4,623; yacc: 2,360; sh: 2,246; java: 2,019; lex: 1,327; pascal: 366; makefile: 306
file content (138 lines) | stat: -rw-r--r-- 4,848 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2011 Igalia S.L.
 * Portions Copyright (c) 2011 Motorola Mobility, Inc.  All rights reserved.
 *
 * 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 "config.h"
#include "WebViewTest.h"

#include <WebCore/GUniquePtrGtk.h>
#include <WebCore/GtkVersioning.h>
#include <gtk/gtk.h>
#include <webkit/WebKitWebViewBaseInternal.h>

void WebViewTest::platformDestroy()
{
#if USE(GTK4)
    if (m_parentWindow)
        gtk_window_destroy(GTK_WINDOW(m_parentWindow));
#else
    if (m_parentWindow)
        gtk_widget_destroy(m_parentWindow);
#endif
}

void WebViewTest::quitMainLoopAfterProcessingPendingEvents()
{
    while (g_main_context_pending(nullptr))
        g_main_context_iteration(nullptr, TRUE);
    quitMainLoop();
}

void WebViewTest::resizeView(int width, int height)
{
    GtkAllocation allocation;
    gtk_widget_get_allocation(GTK_WIDGET(m_webView.get()), &allocation);
    if (width != -1)
        allocation.width = width;
    if (height != -1)
        allocation.height = height;
    gtk_widget_size_allocate(GTK_WIDGET(m_webView.get()), &allocation);
}

void WebViewTest::hideView()
{
    gtk_widget_hide(GTK_WIDGET(m_webView.get()));
}

void WebViewTest::showInWindow(int width, int height)
{
    g_assert_null(m_parentWindow);
#if USE(GTK4)
    m_parentWindow = gtk_window_new();
    gtk_window_set_child(GTK_WINDOW(m_parentWindow), GTK_WIDGET(m_webView.get()));
#else
    m_parentWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_add(GTK_CONTAINER(m_parentWindow), GTK_WIDGET(m_webView.get()));
    gtk_widget_show(GTK_WIDGET(m_webView.get()));
#endif

    if (width && height)
        gtk_window_set_default_size(GTK_WINDOW(m_parentWindow), width, height);

    gtk_widget_show(m_parentWindow);

    while (g_main_context_pending(nullptr))
        g_main_context_iteration(nullptr, TRUE);
}

static unsigned testModifiersToGDK(const OptionSet<WebViewTest::Modifiers> modifiers)
{
    unsigned gdkModifiers = 0;
    if (modifiers.contains(WebViewTest::Modifiers::Control))
        gdkModifiers |= GDK_CONTROL_MASK;
    if (modifiers.contains(WebViewTest::Modifiers::Shift))
        gdkModifiers |= GDK_SHIFT_MASK;
    if (modifiers.contains(WebViewTest::Modifiers::Alt))
        gdkModifiers |= GDK_MOD1_MASK;
    if (modifiers.contains(WebViewTest::Modifiers::Meta))
        gdkModifiers |= GDK_META_MASK;
    return gdkModifiers;
}

static unsigned testMouseButtonToGDK(WebViewTest::MouseButton button)
{
    switch (button) {
    case WebViewTest::MouseButton::Primary:
        return GDK_BUTTON_PRIMARY;
    case WebViewTest::MouseButton::Middle:
        return GDK_BUTTON_MIDDLE;
    case WebViewTest::MouseButton::Secondary:
        return GDK_BUTTON_SECONDARY;
    }
    RELEASE_ASSERT_NOT_REACHED();
}

void WebViewTest::mouseMoveTo(int x, int y, OptionSet<Modifiers> mouseModifiers)
{
    g_assert_nonnull(m_parentWindow);
    webkitWebViewBaseSynthesizeMouseEvent(WEBKIT_WEB_VIEW_BASE(m_webView.get()), MouseEventType::Motion, 0, 0, x, y, testModifiersToGDK(mouseModifiers), 0);
}

void WebViewTest::clickMouseButton(int x, int y, MouseButton button, OptionSet<Modifiers> mouseModifiers)
{
    auto gdkModifiers = testModifiersToGDK(mouseModifiers);
    auto gdkButton = testMouseButtonToGDK(button);
    webkitWebViewBaseSynthesizeMouseEvent(WEBKIT_WEB_VIEW_BASE(m_webView.get()), MouseEventType::Press, gdkButton, 1 << (8 + gdkButton - 1), x, y, gdkModifiers, 1);
    webkitWebViewBaseSynthesizeMouseEvent(WEBKIT_WEB_VIEW_BASE(m_webView.get()), MouseEventType::Release, gdkButton, 0, x, y, gdkModifiers, 0);
}

void WebViewTest::emitPopupMenuSignal()
{
    GtkWidget* viewWidget = GTK_WIDGET(m_webView.get());
    g_assert_true(gtk_widget_get_realized(viewWidget));

    gboolean handled;
    g_signal_emit_by_name(viewWidget, "popup-menu", &handled);
}

void WebViewTest::keyStroke(unsigned keyVal, OptionSet<Modifiers> keyModifiers)
{
    g_assert_nonnull(m_parentWindow);
    webkitWebViewBaseSynthesizeKeyEvent(WEBKIT_WEB_VIEW_BASE(m_webView.get()), KeyEventType::Insert, keyVal, testModifiersToGDK(keyModifiers), ShouldTranslateKeyboardState::No);
}