[iOS WK2] WTR fails to set the key window in the UIKit sense
[WebKit-https.git] / Tools / WebKitTestRunner / gtk / PlatformWebViewGtk.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 University of Szeged. All rights reserved.
4  * Copyright (C) 2010 Igalia S.L.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "config.h"
29 #include "PlatformWebView.h"
30
31 #include <WebKit/WKImageCairo.h>
32 #include <WebKit/WKPageConfigurationRef.h>
33 #include <WebKit/WKViewPrivate.h>
34 #include <gtk/gtk.h>
35 #include <wtf/Assertions.h>
36
37 namespace WTR {
38
39 PlatformWebView::PlatformWebView(WKPageConfigurationRef configuration, const TestOptions& options)
40     : m_view(WKViewCreate(configuration))
41     , m_window(gtk_window_new(GTK_WINDOW_POPUP))
42     , m_windowIsKey(true)
43     , m_options(options)
44 {
45     gtk_container_add(GTK_CONTAINER(m_window), GTK_WIDGET(m_view));
46
47     GtkAllocation size = { 0, 0, 800, 600 };
48     gtk_widget_size_allocate(GTK_WIDGET(m_view), &size);
49     gtk_window_resize(GTK_WINDOW(m_window), 800, 600);
50     gtk_widget_show_all(m_window);
51
52     while (gtk_events_pending())
53         gtk_main_iteration();
54 }
55
56 PlatformWebView::~PlatformWebView()
57 {
58     gtk_widget_destroy(m_window);
59 }
60
61 void PlatformWebView::setWindowIsKey(bool isKey)
62 {
63     m_windowIsKey = isKey;
64 }
65
66 void PlatformWebView::resizeTo(unsigned width, unsigned height)
67 {
68     WKRect frame = windowFrame();
69     frame.size.width = width;
70     frame.size.height = height;
71     setWindowFrame(frame);
72 }
73
74 WKPageRef PlatformWebView::page()
75 {
76     return WKViewGetPage(m_view);
77 }
78
79 void PlatformWebView::focus()
80 {
81     WKViewSetFocus(m_view, true);
82     setWindowIsKey(true);
83 }
84
85 WKRect PlatformWebView::windowFrame()
86 {
87     GtkAllocation geometry;
88     gdk_window_get_geometry(gtk_widget_get_window(GTK_WIDGET(m_window)),
89                             &geometry.x, &geometry.y, &geometry.width, &geometry.height);
90
91     WKRect frame;
92     frame.origin.x = geometry.x;
93     frame.origin.y = geometry.y;
94     frame.size.width = geometry.width;
95     frame.size.height = geometry.height;
96     return frame;
97 }
98
99 void PlatformWebView::setWindowFrame(WKRect frame)
100 {
101     gdk_window_move_resize(gtk_widget_get_window(GTK_WIDGET(m_window)),
102         frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
103     GtkAllocation size = { 0, 0, static_cast<int>(frame.size.width), static_cast<int>(frame.size.height) };
104     gtk_widget_size_allocate(GTK_WIDGET(m_view), &size);
105
106     while (gtk_events_pending())
107         gtk_main_iteration();
108 }
109
110 void PlatformWebView::addChromeInputField()
111 {
112 }
113
114 void PlatformWebView::removeChromeInputField()
115 {
116 }
117
118 void PlatformWebView::makeWebViewFirstResponder()
119 {
120 }
121
122 void PlatformWebView::changeWindowScaleIfNeeded(float)
123 {
124 }
125
126 WKRetainPtr<WKImageRef> PlatformWebView::windowSnapshotImage()
127 {
128     int width = gtk_widget_get_allocated_width(GTK_WIDGET(m_view));
129     int height = gtk_widget_get_allocated_height(GTK_WIDGET(m_view));
130
131     while (gtk_events_pending())
132         gtk_main_iteration();
133
134     cairo_surface_t* imageSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
135
136     cairo_t* context = cairo_create(imageSurface);
137     gtk_widget_draw(GTK_WIDGET(m_view), context);
138     cairo_destroy(context);
139
140     WKRetainPtr<WKImageRef> wkImage = adoptWK(WKImageCreateFromCairoSurface(imageSurface, 0 /* options */));
141
142     cairo_surface_destroy(imageSurface);
143     return wkImage;
144 }
145
146 void PlatformWebView::didInitializeClients()
147 {
148 }
149
150 bool PlatformWebView::viewSupportsOptions(const TestOptions&) const
151 {
152     return true;
153 }
154
155 void PlatformWebView::dismissAllPopupMenus()
156 {
157     // gtk_menu_popdown doesn't modify the GList of attached menus, so it should
158     // be safe to walk this list while calling it.
159     GList* attachedMenusList = gtk_menu_get_for_attach_widget(GTK_WIDGET(m_view));
160     g_list_foreach(attachedMenusList, [] (void* data, void*) {
161         ASSERT(data);
162         gtk_menu_popdown(GTK_MENU(data));
163     }, nullptr);
164 }
165
166 void PlatformWebView::setNavigationGesturesEnabled(bool)
167 {
168 }
169
170 } // namespace WTR
171