Page overlay action context override should indicate the source of the request
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit2ObjC / ActionMenusBundle.mm
1 /*
2  * Copyright (C) 2014 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
28 #if WK_HAVE_C_SPI
29
30 #import "ActionMenusBundleSPI.h"
31 #import "InjectedBundleTest.h"
32 #import "PlatformUtilities.h"
33 #import "Test.h"
34 #import <Foundation/Foundation.h>
35 #import <WebKit/WKBundleFramePrivate.h>
36 #import <WebKit/WKBundleHitTestResult.h>
37 #import <WebKit/WKBundleNodeHandlePrivate.h>
38 #import <WebKit/WKBundlePage.h>
39 #import <WebKit/WKBundlePageOverlay.h>
40 #import <WebKit/WKBundleRangeHandlePrivate.h>
41 #import <WebKit/WKDictionary.h>
42 #import <WebKit/WKNumber.h>
43 #import <wtf/RetainPtr.h>
44
45 namespace TestWebKitAPI {
46
47 RetainPtr<DDActionContext> createActionContextForPhoneNumber()
48 {
49     RetainPtr<CFStringRef> plainText = CFSTR("(408) 996-1010");
50     RetainPtr<DDScannerRef> scanner = adoptCF(DDScannerCreate(DDScannerTypeStandard, 0, nullptr));
51     RetainPtr<DDScanQueryRef> scanQuery = adoptCF(DDScanQueryCreateFromString(kCFAllocatorDefault, plainText.get(), CFRangeMake(0, CFStringGetLength(plainText.get()))));
52
53     if (!DDScannerScanQuery(scanner.get(), scanQuery.get()))
54         return nullptr;
55
56     RetainPtr<CFArrayRef> results = adoptCF(DDScannerCopyResultsWithOptions(scanner.get(), DDScannerCopyResultsOptionsNoOverlap));
57
58     CFIndex resultCount = CFArrayGetCount(results.get());
59     if (resultCount != 1)
60         return nullptr;
61
62     DDResultRef mainResult = (DDResultRef)CFArrayGetValueAtIndex(results.get(), 0);
63
64     if (!mainResult)
65         return nullptr;
66
67     RetainPtr<DDActionContext> actionContext = adoptNS([[getDDActionContextClass() alloc] init]);
68     [actionContext setAllResults:@[ (id)mainResult ]];
69     [actionContext setMainResult:mainResult];
70
71     return actionContext;
72 }
73     
74 class ActionMenuTest : public InjectedBundleTest {
75 public:
76     ActionMenuTest(const std::string& identifier)
77         : InjectedBundleTest(identifier)
78     {
79     }
80
81     static void prepareForActionMenu(WKBundlePageRef page, WKBundleHitTestResultRef hitTestResult, WKTypeRef* userData, const void* clientInfo)
82     {
83         WKStringRef keys[1];
84         WKTypeRef values[1];
85
86         WKRetainPtr<WKStringRef> hasLinkKey = adoptWK(WKStringCreateWithUTF8CString("hasLinkURL"));
87         WKRetainPtr<WKURLRef> absoluteLinkURL = adoptWK(WKBundleHitTestResultCopyAbsoluteLinkURL(hitTestResult));
88         WKRetainPtr<WKBooleanRef> hasLinkValue = adoptWK(WKBooleanCreate(!!absoluteLinkURL));
89         keys[0] = hasLinkKey.get();
90         values[0] = hasLinkValue.get();
91
92         *userData = WKDictionaryCreate(keys, values, 1);
93     }
94
95     static void* actionContextForResultAtPoint(WKBundlePageOverlayRef pageOverlay, WKPoint position, WKBundleRangeHandleRef* rangeHandle, _WKBundlePageOverlayActionContextRequestType requestType, const void* clientInfo)
96     {
97         if (position.x > 700) {
98             RetainPtr<DDActionContext> actionContext = createActionContextForPhoneNumber();
99             *rangeHandle = (WKBundleRangeHandleRef)clientInfo;
100             return (void*)actionContext.autorelease();
101         }
102         return nullptr;
103     }
104
105     static void drawRect(WKBundlePageOverlayRef pageOverlay, void* graphicsContext, WKRect dirtyRect, const void* clientInfo)
106     {
107         CGContextRef ctx = static_cast<CGContextRef>(graphicsContext);
108         CGContextSetRGBFillColor(ctx, 0, 1, 0, 1);
109         CGContextFillRect(ctx, CGRectMake(700, 0, 100, 600));
110     }
111
112     virtual void didCreatePage(WKBundleRef bundle, WKBundlePageRef page)
113     {
114         WKBundlePageContextMenuClientV1 contextMenuClient;
115         memset(&contextMenuClient, 0, sizeof(contextMenuClient));
116         contextMenuClient.base.version = 1;
117         contextMenuClient.prepareForActionMenu = prepareForActionMenu;
118         WKBundlePageSetContextMenuClient(page, &contextMenuClient.base);
119
120         WKRetainPtr<WKBundleHitTestResultRef> hitTestResult = adoptWK(WKBundleFrameCreateHitTestResult(WKBundlePageGetMainFrame(page), WKPointMake(708, 8)));
121         WKRetainPtr<WKBundleNodeHandleRef> nodeHandle = adoptWK(WKBundleHitTestResultCopyNodeHandle(hitTestResult.get()));
122         _rangeHandle = adoptWK(WKBundleNodeHandleCopyVisibleRange(nodeHandle.get()));
123
124         WKBundlePageOverlayClientV1 pageOverlayClient;
125         memset(&pageOverlayClient, 0, sizeof(pageOverlayClient));
126         pageOverlayClient.base.version = 1;
127         pageOverlayClient.base.clientInfo = _rangeHandle.get();
128
129         pageOverlayClient.drawRect = drawRect;
130         pageOverlayClient.actionContextForResultAtPoint = actionContextForResultAtPoint;
131
132         _bundlePageOverlay = adoptWK(WKBundlePageOverlayCreate(&pageOverlayClient.base));
133         WKBundlePageInstallPageOverlay(page, _bundlePageOverlay.get());
134         WKBundlePageOverlaySetNeedsDisplay(_bundlePageOverlay.get(), WKRectMake(0, 0, 800, 600));
135     }
136
137     WKRetainPtr<WKBundlePageOverlayRef> _bundlePageOverlay;
138     WKRetainPtr<WKBundleRangeHandleRef> _rangeHandle;
139 };
140
141 static InjectedBundleTest::Register<ActionMenuTest> registrar("ActionMenusTest");
142
143 } // namespace TestWebKitAPI
144
145 #endif // PLATFORM(MAC)