Add a basic configuration object for WKView
[WebKit-https.git] / Tools / TestWebKitAPI / mac / PlatformWebViewMac.mm
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "PlatformWebView.h"
28
29 #import <Carbon/Carbon.h>
30 #import <WebKit/WKRetainPtr.h>
31 #import <WebKit/WKViewPrivate.h>
32
33 @interface ActiveOffscreenWindow : NSWindow
34 @end
35
36 @implementation ActiveOffscreenWindow
37 - (BOOL)isKeyWindow
38 {
39     return YES;
40 }
41 - (BOOL)isVisible
42 {
43     return YES;
44 }
45 @end
46
47 namespace TestWebKitAPI {
48
49 void PlatformWebView::initialize(WKPageConfigurationRef configuration, Class wkViewSubclass)
50 {
51     NSRect rect = NSMakeRect(0, 0, 800, 600);
52     m_view = [[wkViewSubclass alloc] initWithFrame:rect configurationRef:configuration];
53     [m_view setWindowOcclusionDetectionEnabled:NO];
54
55     NSRect windowRect = NSOffsetRect(rect, -10000, [(NSScreen *)[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
56     m_window = [[ActiveOffscreenWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
57     [m_window setColorSpace:[[NSScreen mainScreen] colorSpace]];
58     [[m_window contentView] addSubview:m_view];
59     [m_window orderBack:nil];
60     [m_window setAutodisplay:NO];
61     [m_window setReleasedWhenClosed:NO];
62 }
63
64 PlatformWebView::PlatformWebView(WKPageConfigurationRef configuration)
65 {
66     initialize(configuration, [WKView class]);
67 }
68
69 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
70 {
71     WKRetainPtr<WKPageConfigurationRef> configuration = adoptWK(WKPageConfigurationCreate());
72     
73     WKPageConfigurationSetContext(configuration.get(), contextRef);
74     WKPageConfigurationSetPageGroup(configuration.get(), pageGroupRef);
75     
76     initialize(configuration.get(), [WKView class]);
77 }
78
79 PlatformWebView::PlatformWebView(WKPageRef relatedPage)
80 {
81     WKRetainPtr<WKPageConfigurationRef> configuration = adoptWK(WKPageConfigurationCreate());
82     
83     WKPageConfigurationSetContext(configuration.get(), WKPageGetContext(relatedPage));
84     WKPageConfigurationSetPageGroup(configuration.get(), WKPageGetPageGroup(relatedPage));
85     WKPageConfigurationSetRelatedPage(configuration.get(), relatedPage);
86
87     initialize(configuration.get(), [WKView class]);
88 }
89
90 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef, Class wkViewSubclass)
91 {
92     WKRetainPtr<WKPageConfigurationRef> configuration = adoptWK(WKPageConfigurationCreate());
93     
94     WKPageConfigurationSetContext(configuration.get(), contextRef);
95     WKPageConfigurationSetPageGroup(configuration.get(), pageGroupRef);
96     
97     initialize(configuration.get(), wkViewSubclass);
98 }
99
100 PlatformWebView::~PlatformWebView()
101 {
102     [m_window close];
103     [m_window release];
104     [m_view release];
105 }
106
107 void PlatformWebView::resizeTo(unsigned width, unsigned height)
108 {
109     [m_view setFrame:NSMakeRect(0, 0, width, height)];
110 }
111
112 WKPageRef PlatformWebView::page() const
113 {
114     return [m_view pageRef];
115 }
116
117 void PlatformWebView::focus()
118 {
119     // Implement.
120 }
121
122 void PlatformWebView::simulateSpacebarKeyPress()
123 {
124     NSEvent *event = [NSEvent keyEventWithType:NSKeyDown
125                                       location:NSMakePoint(5, 5)
126                                  modifierFlags:0
127                                      timestamp:GetCurrentEventTime()
128                                   windowNumber:[m_window windowNumber]
129                                        context:[NSGraphicsContext currentContext]
130                                     characters:@" "
131                    charactersIgnoringModifiers:@" "
132                                      isARepeat:NO
133                                        keyCode:0x31];
134
135     [m_view keyDown:event];
136
137     event = [NSEvent keyEventWithType:NSKeyUp
138                              location:NSMakePoint(5, 5)
139                         modifierFlags:0
140                             timestamp:GetCurrentEventTime()
141                          windowNumber:[m_window windowNumber]
142                               context:[NSGraphicsContext currentContext]
143                            characters:@" "
144           charactersIgnoringModifiers:@" "
145                             isARepeat:NO
146                               keyCode:0x31];
147
148     [m_view keyUp:event];
149 }
150
151 void PlatformWebView::simulateRightClick(unsigned x, unsigned y)
152 {
153     NSEvent *event = [NSEvent mouseEventWithType:NSRightMouseDown
154                                         location:NSMakePoint(x, y)
155                                    modifierFlags:0
156                                        timestamp:GetCurrentEventTime()
157                                     windowNumber:[m_window windowNumber]
158                                          context:[NSGraphicsContext currentContext]
159                                      eventNumber:0
160                                       clickCount:0
161                                         pressure:0];
162
163
164     [m_view rightMouseDown:event];
165
166     event = [NSEvent mouseEventWithType:NSRightMouseUp
167                                location:NSMakePoint(x, y)
168                           modifierFlags:0
169                               timestamp:GetCurrentEventTime()
170                            windowNumber:[m_window windowNumber]
171                                 context:[NSGraphicsContext currentContext]
172                             eventNumber:0
173                              clickCount:0
174                                pressure:0];
175
176     [m_view rightMouseUp:event];
177
178 }
179     
180 void PlatformWebView::simulateMouseMove(unsigned x, unsigned y)
181 {   
182     NSEvent *event = [NSEvent mouseEventWithType:NSMouseMoved
183                                location:NSMakePoint(x, y)
184                           modifierFlags:0
185                               timestamp:GetCurrentEventTime()
186                            windowNumber:[m_window windowNumber]
187                                 context:[NSGraphicsContext currentContext]
188                             eventNumber:0
189                              clickCount:0
190                                pressure:0];
191     
192     [m_view mouseMoved:event];
193     
194 }
195
196 } // namespace TestWebKitAPI