|
a/Tools/TestWebKitAPI/Tests/WebKit2/ContextMenu.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2013 Samsung Electronics. 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 |
#include "config.h" |
| 27 |
#include "PlatformUtilities.h" |
| 28 |
#include "PlatformWebView.h" |
| 29 |
#include "Test.h" |
| 30 |
#include <WebKit2/WKContextMenuItem.h> |
| 31 |
|
| 32 |
namespace TestWebKitAPI { |
| 33 |
|
| 34 |
static bool didFinishLoad; |
| 35 |
static bool didSelectCustomItem; |
| 36 |
static bool isMenuDismissed; |
| 37 |
static bool isMenuHidden; |
| 38 |
|
| 39 |
static const uint32_t customItemTag = kWKContextMenuItemBaseApplicationTag + 1; |
| 40 |
static const char* customItemTitle = "TEST"; |
| 41 |
static const int defaultMenuItemsNumber = 4; |
| 42 |
|
| 43 |
static void didFinishLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef, const void*) |
| 44 |
{ |
| 45 |
didFinishLoad = true; |
| 46 |
} |
| 47 |
|
| 48 |
static void customContextMenuItemSelected(WKPageRef, WKContextMenuItemRef contextMenuItem, const void*) |
| 49 |
{ |
| 50 |
EXPECT_EQ(WKContextMenuItemGetTag(contextMenuItem), customItemTag); |
| 51 |
EXPECT_TRUE(WKStringIsEqualToUTF8CString(WKContextMenuItemCopyTitle(contextMenuItem), customItemTitle)); |
| 52 |
didSelectCustomItem = true; |
| 53 |
} |
| 54 |
|
| 55 |
static void contextMenuDismissed(WKPageRef, const void*) |
| 56 |
{ |
| 57 |
isMenuDismissed = true; |
| 58 |
} |
| 59 |
|
| 60 |
static void getContextMenuFromProposedMenu(WKPageRef, WKArrayRef proposedMenu, WKArrayRef* newMenu, WKHitTestResultRef, WKTypeRef, const void*) |
| 61 |
{ |
| 62 |
size_t itemsNumber = WKArrayGetSize(proposedMenu); |
| 63 |
ASSERT_EQ(defaultMenuItemsNumber, itemsNumber); |
| 64 |
|
| 65 |
WKTypeRef newList[defaultMenuItemsNumber + 1]; |
| 66 |
newList[0] = reinterpret_cast<WKTypeRef>(WKContextMenuItemCreateAsAction(customItemTag, WKStringCreateWithUTF8CString(customItemTitle), true)); |
| 67 |
|
| 68 |
for (size_t i = 0; i < itemsNumber; ++i) { |
| 69 |
WKTypeRef item = WKArrayGetItemAtIndex(proposedMenu, i); |
| 70 |
ASSERT_EQ(WKGetTypeID(item), WKContextMenuItemGetTypeID()); |
| 71 |
|
| 72 |
newList[i + 1] = item; |
| 73 |
} |
| 74 |
|
| 75 |
*newMenu = WKArrayCreate(newList, defaultMenuItemsNumber + 1); |
| 76 |
} |
| 77 |
|
| 78 |
static void showContextMenu(WKPageRef page, WKPoint, WKArrayRef menuItems, const void*) |
| 79 |
{ |
| 80 |
size_t itemsNumber = WKArrayGetSize(menuItems); |
| 81 |
ASSERT_EQ(defaultMenuItemsNumber + 1, itemsNumber); |
| 82 |
|
| 83 |
for (size_t i = 0; i < itemsNumber; ++i) { |
| 84 |
WKTypeRef item = WKArrayGetItemAtIndex(menuItems, i); |
| 85 |
ASSERT_EQ(WKGetTypeID(item), WKContextMenuItemGetTypeID()); |
| 86 |
|
| 87 |
WKContextMenuItemRef contextMenuItem = reinterpret_cast<WKContextMenuItemRef>(item); |
| 88 |
WKContextMenuItemTag contextMenuTag = WKContextMenuItemGetTag(contextMenuItem); |
| 89 |
|
| 90 |
if (contextMenuTag == customItemTag || contextMenuTag == kWKContextMenuItemTagReload) |
| 91 |
WKPageSelectContextMenuItem(page, contextMenuItem); |
| 92 |
} |
| 93 |
EXPECT_TRUE(didSelectCustomItem); |
| 94 |
} |
| 95 |
|
| 96 |
static void hideContextMenu(WKPageRef, const void*) |
| 97 |
{ |
| 98 |
isMenuHidden = true; |
| 99 |
} |
| 100 |
|
| 101 |
static void setPageLoaderClient(WKPageRef page) |
| 102 |
{ |
| 103 |
WKPageLoaderClient loaderClient; |
| 104 |
memset(&loaderClient, 0, sizeof(loaderClient)); |
| 105 |
loaderClient.version = kWKPageLoaderClientCurrentVersion; |
| 106 |
loaderClient.didFinishLoadForFrame = didFinishLoadForFrame; |
| 107 |
|
| 108 |
WKPageSetPageLoaderClient(page, &loaderClient); |
| 109 |
} |
| 110 |
|
| 111 |
static void setContextMenuClient(WKPageRef page) |
| 112 |
{ |
| 113 |
WKPageContextMenuClient contextMenuClient; |
| 114 |
memset(&contextMenuClient, 0, sizeof(contextMenuClient)); |
| 115 |
|
| 116 |
contextMenuClient.version = kWKPageContextMenuClientCurrentVersion; |
| 117 |
contextMenuClient.customContextMenuItemSelected = customContextMenuItemSelected; |
| 118 |
contextMenuClient.contextMenuDismissed = contextMenuDismissed; |
| 119 |
contextMenuClient.getContextMenuFromProposedMenu = getContextMenuFromProposedMenu; |
| 120 |
contextMenuClient.showContextMenu = showContextMenu; |
| 121 |
contextMenuClient.hideContextMenu = hideContextMenu; |
| 122 |
|
| 123 |
WKPageSetPageContextMenuClient(page, &contextMenuClient); |
| 124 |
} |
| 125 |
|
| 126 |
TEST(WebKit2, ContextMenu) |
| 127 |
{ |
| 128 |
WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate()); |
| 129 |
|
| 130 |
PlatformWebView webView(context.get()); |
| 131 |
setPageLoaderClient(webView.page()); |
| 132 |
setContextMenuClient(webView.page()); |
| 133 |
|
| 134 |
WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple", "html")).get()); |
| 135 |
Util::run(&didFinishLoad); |
| 136 |
didFinishLoad = false; |
| 137 |
|
| 138 |
webView.simulateRightClick(10, 10); |
| 139 |
Util::run(&didFinishLoad); |
| 140 |
|
| 141 |
EXPECT_TRUE(isMenuHidden); |
| 142 |
EXPECT_TRUE(isMenuDismissed); |
| 143 |
EXPECT_TRUE(didFinishLoad); |
| 144 |
} |
| 145 |
|
| 146 |
} // namespace TestWebKitAPI |