Source/WebKit/mac: Add SPI to WebKit1 WebFrame for hit testing
authoralice.liu@apple.com <alice.liu@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 19 Dec 2012 03:00:02 +0000 (03:00 +0000)
committeralice.liu@apple.com <alice.liu@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 19 Dec 2012 03:00:02 +0000 (03:00 +0000)
https://bugs.webkit.org/show_bug.cgi?id=105106

Reviewed by Dan Bernstein.

* WebView/WebFrame.mm:
(-[WebFrame elementAtPoint:]): Added. Takes an NSPoint to feed to the WebCore::Frame for hit-testing.
Creates a WebElementDictionary from that WebCore::HitTestResult, and returns the element dictionary.
* WebView/WebFramePrivate.h:

Tools: Test for https://bugs.webkit.org/show_bug.cgi?id=105106
Add SPI to WebKit1 WebFrame for hit testing

Reviewed by Dan Bernstein.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: Added file Tests/mac/ElementAtPointInWebFrame.mm
* TestWebKitAPI/Tests/mac/ElementAtPointInWebFrame.mm: Added.
(TestWebKitAPI::TEST): Loads html with two divs positioned in the 2nd and 4th quadrants of the webview.
Then hit-tests at three points, expecting to hit the two divs and body element.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@138104 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebView/WebFrame.mm
Source/WebKit/mac/WebView/WebFramePrivate.h
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/mac/ElementAtPointInWebFrame.mm [new file with mode: 0644]

index 3665146..993c128 100644 (file)
@@ -1,5 +1,17 @@
 2012-12-18  Alice Liu  <alice.liu@apple.com>
 
+        Add SPI to WebKit1 WebFrame for hit testing
+        https://bugs.webkit.org/show_bug.cgi?id=105106
+
+        Reviewed by Dan Bernstein.
+
+        * WebView/WebFrame.mm:
+        (-[WebFrame elementAtPoint:]): Added. Takes an NSPoint to feed to the WebCore::Frame for hit-testing.
+        Creates a WebElementDictionary from that WebCore::HitTestResult, and returns the element dictionary. 
+        * WebView/WebFramePrivate.h:
+
+2012-12-18  Alice Liu  <alice.liu@apple.com>
+
         Add SPI to WebKit1 WebFrame for node conversion to JSValueRef
         https://bugs.webkit.org/show_bug.cgi?id=105262
 
index d2cb3c6..c66d446 100644 (file)
@@ -40,6 +40,7 @@
 #import "WebDataSourceInternal.h"
 #import "WebDocumentLoaderMac.h"
 #import "WebDynamicScrollBarsView.h"
+#import "WebElementDictionary.h"
 #import "WebFrameLoaderClient.h"
 #import "WebFrameViewInternal.h"
 #import "WebHTMLView.h"
@@ -1257,6 +1258,14 @@ static inline WebDataSource *dataSource(DocumentLoader* loader)
     return toRef(exec, toJS(exec, globalObject, core(node)));
 }
 
+- (NSDictionary *)elementAtPoint:(NSPoint)point
+{
+    Frame* coreFrame = _private->coreFrame;
+    if (!coreFrame)
+        return nil;
+    return [[[WebElementDictionary alloc] initWithHitTestResult:coreFrame->eventHandler()->hitTestResultAtPoint(IntPoint(point), false, true)] autorelease];
+}
+
 @end
 
 @implementation WebFrame
index 4f786c7..adc89bb 100644 (file)
@@ -140,4 +140,6 @@ typedef enum {
 
 - (JSValueRef)jsWrapperForNode:(DOMNode *)node inScriptWorld:(WebScriptWorld *)world;
 
+- (NSDictionary *)elementAtPoint:(NSPoint)point;
+
 @end
index 6207cf8..63afdbc 100644 (file)
@@ -1,5 +1,17 @@
 2012-12-18  Alice Liu  <alice.liu@apple.com>
 
+        Test for https://bugs.webkit.org/show_bug.cgi?id=105106
+        Add SPI to WebKit1 WebFrame for hit testing
+
+        Reviewed by Dan Bernstein.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: Added file Tests/mac/ElementAtPointInWebFrame.mm
+        * TestWebKitAPI/Tests/mac/ElementAtPointInWebFrame.mm: Added.
+        (TestWebKitAPI::TEST): Loads html with two divs positioned in the 2nd and 4th quadrants of the webview.
+        Then hit-tests at three points, expecting to hit the two divs and body element. 
+
+2012-12-18  Alice Liu  <alice.liu@apple.com>
+
         Test for https://bugs.webkit.org/show_bug.cgi?id=105262
         Add SPI to WebKit1 WebFrame for node conversion to JSValueRef
 
index f0d10f7..d991d47 100644 (file)
@@ -57,6 +57,7 @@
                37DC6791140D7D7600ABCCDB /* DOMRangeOfString.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 37DC678F140D7D3A00ABCCDB /* DOMRangeOfString.html */; };
                440A1D3914A0103A008A66F2 /* KURL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 440A1D3814A0103A008A66F2 /* KURL.cpp */; };
                4BB4160216815B2600824238 /* JSWrapperForNodeInWebFrame.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4BB4160116815B2600824238 /* JSWrapperForNodeInWebFrame.mm */; };
+               4BB4160416815F9100824238 /* ElementAtPointInWebFrame.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4BB4160316815F9100824238 /* ElementAtPointInWebFrame.mm */; };
                4BFDFFA71314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BFDFFA61314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp */; };
                4BFDFFA9131477770061F24B /* HitTestResultNodeHandle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BFDFFA8131477770061F24B /* HitTestResultNodeHandle.cpp */; };
                4F4D2C0E1626FE2700320FE1 /* MemoryInstrumentationTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F4D2C0D1626FE2700320FE1 /* MemoryInstrumentationTest.cpp */; };
                440A1D3814A0103A008A66F2 /* KURL.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KURL.cpp; sourceTree = "<group>"; };
                44A622C114A0E2B60048515B /* WTFStringUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTFStringUtilities.h; sourceTree = "<group>"; };
                4BB4160116815B2600824238 /* JSWrapperForNodeInWebFrame.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = JSWrapperForNodeInWebFrame.mm; sourceTree = "<group>"; };
+               4BB4160316815F9100824238 /* ElementAtPointInWebFrame.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ElementAtPointInWebFrame.mm; sourceTree = "<group>"; };
                4BFDFFA61314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HitTestResultNodeHandle_Bundle.cpp; sourceTree = "<group>"; };
                4BFDFFA8131477770061F24B /* HitTestResultNodeHandle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HitTestResultNodeHandle.cpp; sourceTree = "<group>"; };
                4F4D2C0D1626FE2700320FE1 /* MemoryInstrumentationTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MemoryInstrumentationTest.cpp; path = WTF/MemoryInstrumentationTest.cpp; sourceTree = "<group>"; };
                                C07E6CAE13FD67650038B22B /* DynamicDeviceScaleFactor.mm */,
                                3776BC62150946BC0043A66D /* DeviceScaleFactorInDashboardRegions.mm */,
                                939BA91614103412001A01BD /* DeviceScaleFactorOnBack.mm */,
+                               4BB4160316815F9100824238 /* ElementAtPointInWebFrame.mm */,
                                9B4F8FA3159D52B1002D9F94 /* HTMLCollectionNamedItem.mm */,
                                9B26FC6B159D061000CC3765 /* HTMLFormCollectionNamedItem.mm */,
                                C507E8A614C6545B005D6B3B /* InspectorBar.mm */,
                                29AB8AA4164C7A9300D49BEC /* TestBrowsingContextLoadDelegate.mm in Sources */,
                                14464013167A8305000BD218 /* LayoutUnit.cpp in Sources */,
                                4BB4160216815B2600824238 /* JSWrapperForNodeInWebFrame.mm in Sources */,
+                               4BB4160416815F9100824238 /* ElementAtPointInWebFrame.mm in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
diff --git a/Tools/TestWebKitAPI/Tests/mac/ElementAtPointInWebFrame.mm b/Tools/TestWebKitAPI/Tests/mac/ElementAtPointInWebFrame.mm
new file mode 100644 (file)
index 0000000..10fc581
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2012 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "PlatformUtilities.h"
+#import <WebKit/WebFramePrivate.h>
+#import <wtf/RetainPtr.h>
+
+@interface ElementAtPointFrameLoadDelegate : NSObject
+@end
+
+static bool didFinishLoad;
+
+@implementation ElementAtPointFrameLoadDelegate
+
+- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
+{
+    didFinishLoad = true;
+}
+
+@end
+
+namespace TestWebKitAPI {
+
+TEST(WebKit1, ElementAtPoint)
+{
+    RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]);
+    RetainPtr<ElementAtPointFrameLoadDelegate> frameLoadDelegate = adoptNS([[ElementAtPointFrameLoadDelegate alloc] init]);
+
+    webView.get().frameLoadDelegate = frameLoadDelegate.get();
+    WebFrame *mainFrame = webView.get().mainFrame;
+
+    [mainFrame loadHTMLString:@"<style> div { position:absolute; width:60px; height:100px; } </style> <div name='first'></div> <div name='second' style='left:60px; top:100px;'></div>"
+                      baseURL:[NSURL URLWithString:@"about:blank"]];
+
+    Util::run(&didFinishLoad);
+    
+    NSDictionary *elementDictionary = [mainFrame elementAtPoint:NSMakePoint(30, 50)];
+    DOMElement *domElement = [elementDictionary objectForKey:WebElementDOMNodeKey];
+    EXPECT_WK_STREQ(@"first", [domElement getAttribute:@"name"]);
+
+    elementDictionary = [mainFrame elementAtPoint:NSMakePoint(90, 150)];
+    domElement = [elementDictionary objectForKey:WebElementDOMNodeKey];
+    EXPECT_WK_STREQ(@"second", [domElement getAttribute:@"name"]);
+
+    elementDictionary = [mainFrame elementAtPoint:NSMakePoint(30, 150)];
+    domElement = [elementDictionary objectForKey:WebElementDOMNodeKey];
+    EXPECT_WK_STREQ(@"BODY", [domElement tagName]);
+}
+
+} // namespace TestWebKitAPI