b6586dd42fd4044431b6ea41419a6b420aa7b90e
[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 <WebKit/WKViewPrivate.h>
30 #import <Carbon/Carbon.h>
31
32 @interface ActiveOffscreenWindow : NSWindow
33 @end
34
35 @implementation ActiveOffscreenWindow
36 - (BOOL)isKeyWindow
37 {
38     return YES;
39 }
40 - (BOOL)isVisible
41 {
42     return YES;
43 }
44 @end
45
46 namespace TestWebKitAPI {
47
48 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
49     : PlatformWebView(contextRef, pageGroupRef, [WKView class])
50 {
51 }
52
53 PlatformWebView::PlatformWebView(WKPageRef relatedPage)
54 {
55     NSRect rect = NSMakeRect(0, 0, 800, 600);
56     m_view = [[WKView alloc] initWithFrame:rect contextRef:WKPageGetContext(relatedPage) pageGroupRef:WKPageGetPageGroup(relatedPage) relatedToPage:relatedPage];
57     [m_view setWindowOcclusionDetectionEnabled:NO];
58
59     NSRect windowRect = NSOffsetRect(rect, -10000, [(NSScreen *)[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
60     m_window = [[ActiveOffscreenWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
61     [m_window setColorSpace:[[NSScreen mainScreen] colorSpace]];
62     [[m_window contentView] addSubview:m_view];
63     [m_window orderBack:nil];
64     [m_window setAutodisplay:NO];
65     [m_window setReleasedWhenClosed:NO];
66 }
67
68 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef, Class wkViewSubclass)
69 {
70     NSRect rect = NSMakeRect(0, 0, 800, 600);
71     m_view = [[wkViewSubclass alloc] initWithFrame:rect contextRef:contextRef pageGroupRef:pageGroupRef];
72     [m_view setWindowOcclusionDetectionEnabled:NO];
73
74     NSRect windowRect = NSOffsetRect(rect, -10000, [(NSScreen *)[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
75     m_window = [[ActiveOffscreenWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
76     [m_window setColorSpace:[[NSScreen mainScreen] colorSpace]];
77     [[m_window contentView] addSubview:m_view];
78     [m_window orderBack:nil];
79     [m_window setAutodisplay:NO];
80     [m_window setReleasedWhenClosed:NO];
81 }
82
83 PlatformWebView::~PlatformWebView()
84 {
85     [m_window close];
86     [m_window release];
87     [m_view release];
88 }
89
90 void PlatformWebView::resizeTo(unsigned width, unsigned height)
91 {
92     [m_view setFrame:NSMakeRect(0, 0, width, height)];
93 }
94
95 WKPageRef PlatformWebView::page() const
96 {
97     return [m_view pageRef];
98 }
99
100 void PlatformWebView::focus()
101 {
102     // Implement.
103 }
104
105 void PlatformWebView::simulateSpacebarKeyPress()
106 {
107     NSEvent *event = [NSEvent keyEventWithType:NSKeyDown
108                                       location:NSMakePoint(5, 5)
109                                  modifierFlags:0
110                                      timestamp:GetCurrentEventTime()
111                                   windowNumber:[m_window windowNumber]
112                                        context:[NSGraphicsContext currentContext]
113                                     characters:@" "
114                    charactersIgnoringModifiers:@" "
115                                      isARepeat:NO
116                                        keyCode:0x31];
117
118     [m_view keyDown:event];
119
120     event = [NSEvent keyEventWithType:NSKeyUp
121                              location:NSMakePoint(5, 5)
122                         modifierFlags:0
123                             timestamp:GetCurrentEventTime()
124                          windowNumber:[m_window windowNumber]
125                               context:[NSGraphicsContext currentContext]
126                            characters:@" "
127           charactersIgnoringModifiers:@" "
128                             isARepeat:NO
129                               keyCode:0x31];
130
131     [m_view keyUp:event];
132 }
133
134 void PlatformWebView::simulateRightClick(unsigned x, unsigned y)
135 {
136     NSEvent *event = [NSEvent mouseEventWithType:NSRightMouseDown
137                                         location:NSMakePoint(x, y)
138                                    modifierFlags:0
139                                        timestamp:GetCurrentEventTime()
140                                     windowNumber:[m_window windowNumber]
141                                          context:[NSGraphicsContext currentContext]
142                                      eventNumber:0
143                                       clickCount:0
144                                         pressure:0];
145
146
147     [m_view rightMouseDown:event];
148
149     event = [NSEvent mouseEventWithType:NSRightMouseUp
150                                location:NSMakePoint(x, y)
151                           modifierFlags:0
152                               timestamp:GetCurrentEventTime()
153                            windowNumber:[m_window windowNumber]
154                                 context:[NSGraphicsContext currentContext]
155                             eventNumber:0
156                              clickCount:0
157                                pressure:0];
158
159     [m_view rightMouseUp:event];
160
161 }
162     
163 void PlatformWebView::simulateMouseMove(unsigned x, unsigned y)
164 {   
165     NSEvent *event = [NSEvent mouseEventWithType:NSMouseMoved
166                                location:NSMakePoint(x, y)
167                           modifierFlags:0
168                               timestamp:GetCurrentEventTime()
169                            windowNumber:[m_window windowNumber]
170                                 context:[NSGraphicsContext currentContext]
171                             eventNumber:0
172                              clickCount:0
173                                pressure:0];
174     
175     [m_view mouseMoved:event];
176     
177 }
178
179 } // namespace TestWebKitAPI