diff options
Diffstat (limited to 'Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestContextMenu.cpp')
| -rw-r--r-- | Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestContextMenu.cpp | 208 |
1 files changed, 192 insertions, 16 deletions
diff --git a/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestContextMenu.cpp b/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestContextMenu.cpp index a91e78741..e08ec799d 100644 --- a/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestContextMenu.cpp +++ b/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestContextMenu.cpp @@ -19,7 +19,8 @@ #include "config.h" #include "WebViewTest.h" -#include <wtf/gobject/GRefPtr.h> +#include <wtf/Vector.h> +#include <wtf/glib/GRefPtr.h> class ContextMenuTest: public WebViewTest { public: @@ -92,17 +93,6 @@ public: return 0; } - bool shouldShowInputMethodsMenu() - { - GtkSettings* settings = gtk_widget_get_settings(GTK_WIDGET(m_webView)); - if (!settings) - return true; - - gboolean showInputMethodMenu; - g_object_get(settings, "gtk-show-input-method-menu", &showInputMethodMenu, NULL); - return showInputMethodMenu; - } - void checkActionState(GtkAction* action, unsigned state) { if (state & Visible) @@ -239,7 +229,8 @@ public: LinkImage, Video, Audio, - Editable + Editable, + Selection }; ContextMenuDefaultTest() @@ -257,6 +248,7 @@ public: g_assert(!webkit_hit_test_result_context_is_image(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_media(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_editable(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_selection(hitTestResult)); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_GO_BACK, Visible); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_GO_FORWARD, Visible); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_STOP, Visible); @@ -267,6 +259,7 @@ public: g_assert(!webkit_hit_test_result_context_is_image(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_media(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_editable(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_selection(hitTestResult)); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK_IN_NEW_WINDOW, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_LINK_TO_DISK, Visible | Enabled); @@ -277,6 +270,7 @@ public: g_assert(webkit_hit_test_result_context_is_image(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_media(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_editable(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_selection(hitTestResult)); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_OPEN_IMAGE_IN_NEW_WINDOW, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_IMAGE_TO_DISK, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_TO_CLIPBOARD, Visible | Enabled); @@ -287,6 +281,7 @@ public: g_assert(webkit_hit_test_result_context_is_image(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_media(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_editable(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_selection(hitTestResult)); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK_IN_NEW_WINDOW, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_LINK_TO_DISK, Visible | Enabled); @@ -302,6 +297,7 @@ public: g_assert(!webkit_hit_test_result_context_is_image(hitTestResult)); g_assert(webkit_hit_test_result_context_is_media(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_editable(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_selection(hitTestResult)); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PLAY, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_MEDIA_MUTE, Visible); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_CONTROLS, Visible | Enabled | Checked); @@ -317,6 +313,7 @@ public: g_assert(!webkit_hit_test_result_context_is_image(hitTestResult)); g_assert(webkit_hit_test_result_context_is_media(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_editable(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_selection(hitTestResult)); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PLAY, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_MEDIA_MUTE, Visible); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_CONTROLS, Visible | Enabled | Checked); @@ -332,6 +329,7 @@ public: g_assert(!webkit_hit_test_result_context_is_image(hitTestResult)); g_assert(!webkit_hit_test_result_context_is_media(hitTestResult)); g_assert(webkit_hit_test_result_context_is_editable(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_selection(hitTestResult)); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_CUT, Visible); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_COPY, Visible); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_PASTE, Visible | Enabled); @@ -339,10 +337,16 @@ public: iter = checkCurrentItemIsSeparatorAndGetNext(iter); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_SELECT_ALL, Visible | Enabled); iter = checkCurrentItemIsSeparatorAndGetNext(iter); - if (shouldShowInputMethodsMenu()) - iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_INPUT_METHODS, Visible | Enabled); iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_UNICODE, Visible | Enabled); break; + case Selection: + g_assert(!webkit_hit_test_result_context_is_link(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_image(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_media(hitTestResult)); + g_assert(!webkit_hit_test_result_context_is_editable(hitTestResult)); + g_assert(webkit_hit_test_result_context_is_selection(hitTestResult)); + iter = checkCurrentItemIsStockActionAndGetNext(iter, WEBKIT_CONTEXT_MENU_ACTION_COPY, Visible | Enabled); + break; default: g_assert_not_reached(); } @@ -366,17 +370,32 @@ static void testContextMenuDefaultMenu(ContextMenuDefaultTest* test, gconstpoint test->showInWindowAndWaitUntilMapped(); const char* linksHTML = - "<html><body>" + "<html><head>" + " <script>" + " window.onload = function () {" + " window.getSelection().removeAllRanges();" + " var select_range = document.createRange();" + " select_range.selectNodeContents(document.getElementById('text_to_select'));" + " window.getSelection().addRange(select_range);" + " }" + " </script>" + "</head><body>" " <a style='position:absolute; left:1; top:1' href='http://www.webkitgtk.org' title='WebKitGTK+ Title'>WebKitGTK+ Website</a>" " <img style='position:absolute; left:1; top:10' src='0xdeadbeef' width=5 height=5></img>" " <a style='position:absolute; left:1; top:20' href='http://www.webkitgtk.org/logo' title='WebKitGTK+ Logo'><img src='0xdeadbeef' width=5 height=5></img></a>" " <input style='position:absolute; left:1; top:30' size='10'></input>" " <video style='position:absolute; left:1; top:50' width='300' height='300' controls='controls' preload='none'><source src='movie.ogg' type='video/ogg' /></video>" " <audio style='position:absolute; left:1; top:60' width='50' height='20' controls='controls' preload='none'><source src='track.mp3' type='audio/mp3' /></audio>" + " <p style='position:absolute; left:1; top:90' id='text_to_select'>Lorem ipsum.</p>" "</body></html>"; test->loadHtml(linksHTML, "file:///"); test->waitUntilLoadFinished(); + // Context menu for selection. + // This test should always be the first because any other click removes the selection. + test->m_expectedMenuType = ContextMenuDefaultTest::Selection; + test->showContextMenuAtPositionAndWaitUntilFinished(2, 115); + // Context menu for document. test->m_expectedMenuType = ContextMenuDefaultTest::Navigation; test->showContextMenuAtPositionAndWaitUntilFinished(0, 0); @@ -851,6 +870,161 @@ static void testContextMenuSmartSeparators(ContextMenuSmartSeparatorsTest* test, g_assert(!GTK_IS_SEPARATOR_MENU_ITEM(item) && !gtk_widget_get_visible(item)); } +class ContextMenuWebExtensionTest: public ContextMenuTest { +public: + MAKE_GLIB_TEST_FIXTURE(ContextMenuWebExtensionTest); + + void deserializeContextMenuFromUserData(GVariant* userData) + { + m_actions.clear(); + if (!userData) + return; + + GVariantIter iter; + g_variant_iter_init(&iter, userData); + m_actions.reserveInitialCapacity(g_variant_iter_n_children(&iter)); + + uint32_t item; + while (g_variant_iter_next(&iter, "u", &item)) + m_actions.uncheckedAppend(static_cast<WebKitContextMenuAction>(item)); + } + + bool contextMenu(WebKitContextMenu* menu, GdkEvent*, WebKitHitTestResult*) + { + deserializeContextMenuFromUserData(webkit_context_menu_get_user_data(menu)); + GList* items = webkit_context_menu_get_items(menu); + g_assert_cmpuint(g_list_length(items), ==, m_actions.size()); + + unsigned actionIndex = 0; + for (GList* it = items; it; it = g_list_next(it)) { + WebKitContextMenuItem* item = WEBKIT_CONTEXT_MENU_ITEM(it->data); + g_assert_cmpuint(webkit_context_menu_item_get_stock_action(item), ==, m_actions[actionIndex++]); + } + + quitMainLoop(); + + return true; + } + + Vector<WebKitContextMenuAction> m_actions; +}; + +static void testContextMenuWebExtensionMenu(ContextMenuWebExtensionTest* test, gconstpointer) +{ + test->showInWindowAndWaitUntilMapped(); + test->loadHtml("<html><body>WebKitGTK+ Context menu tests<br>" + "<a style='position:absolute; left:1; top:10' href='http://www.webkitgtk.org'>WebKitGTK+ Website</a></body></html>", + "ContextMenuTestDefault"); + test->waitUntilLoadFinished(); + + // Default context menu. + test->showContextMenuAtPositionAndWaitUntilFinished(1, 1); + g_assert_cmpuint(test->m_actions.size(), ==, 4); + g_assert_cmpuint(test->m_actions[0], ==, WEBKIT_CONTEXT_MENU_ACTION_GO_BACK); + g_assert_cmpuint(test->m_actions[1], ==, WEBKIT_CONTEXT_MENU_ACTION_GO_FORWARD); + g_assert_cmpuint(test->m_actions[2], ==, WEBKIT_CONTEXT_MENU_ACTION_STOP); + g_assert_cmpuint(test->m_actions[3], ==, WEBKIT_CONTEXT_MENU_ACTION_RELOAD); + + // Link menu. + test->showContextMenuAtPositionAndWaitUntilFinished(1, 11); + g_assert_cmpuint(test->m_actions.size(), ==, 4); + g_assert_cmpuint(test->m_actions[0], ==, WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK); + g_assert_cmpuint(test->m_actions[1], ==, WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK_IN_NEW_WINDOW); + g_assert_cmpuint(test->m_actions[2], ==, WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_LINK_TO_DISK); + g_assert_cmpuint(test->m_actions[3], ==, WEBKIT_CONTEXT_MENU_ACTION_COPY_LINK_TO_CLIPBOARD); + + // Custom menu. + test->loadHtml("<html><body></body></html>", "ContextMenuTestCustom"); + test->showContextMenuAndWaitUntilFinished(); + g_assert_cmpuint(test->m_actions.size(), ==, 4); + g_assert_cmpuint(test->m_actions[0], ==, WEBKIT_CONTEXT_MENU_ACTION_STOP); + g_assert_cmpuint(test->m_actions[1], ==, WEBKIT_CONTEXT_MENU_ACTION_RELOAD); + g_assert_cmpuint(test->m_actions[2], ==, WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION); + g_assert_cmpuint(test->m_actions[3], ==, WEBKIT_CONTEXT_MENU_ACTION_INSPECT_ELEMENT); + + // Menu cleared by the web process. + test->loadHtml("<html><body></body></html>", "ContextMenuTestClear"); + test->showContextMenuAndWaitUntilFinished(); + g_assert_cmpuint(test->m_actions.size(), ==, 0); +} + +class ContextMenuWebExtensionNodeTest: public ContextMenuTest { +public: + MAKE_GLIB_TEST_FIXTURE(ContextMenuWebExtensionNodeTest); + + struct Node { + enum { + NodeUnknown = 0, + NodeElement = 1, + NodeText = 3 + }; + typedef unsigned Type; + + CString name; + Type type; + CString contents; + CString parentName; + }; + + void deserializeNodeFromUserData(GVariant* userData) + { + GVariantIter iter; + g_variant_iter_init(&iter, userData); + + const char* key; + GVariant* value; + while (g_variant_iter_next(&iter, "{&sv}", &key, &value)) { + if (!strcmp(key, "Name") && g_variant_classify(value) == G_VARIANT_CLASS_STRING) + m_node.name = g_variant_get_string(value, nullptr); + else if (!strcmp(key, "Type") && g_variant_classify(value) == G_VARIANT_CLASS_UINT32) + m_node.type = g_variant_get_uint32(value); + else if (!strcmp(key, "Contents") && g_variant_classify(value) == G_VARIANT_CLASS_STRING) + m_node.contents = g_variant_get_string(value, nullptr); + else if (!strcmp(key, "Parent") && g_variant_classify(value) == G_VARIANT_CLASS_STRING) + m_node.parentName = g_variant_get_string(value, nullptr); + g_variant_unref(value); + } + } + + bool contextMenu(WebKitContextMenu* menu, GdkEvent*, WebKitHitTestResult*) + { + deserializeNodeFromUserData(webkit_context_menu_get_user_data(menu)); + quitMainLoop(); + + return true; + } + + Node m_node; +}; + +static void testContextMenuWebExtensionNode(ContextMenuWebExtensionNodeTest* test, gconstpointer) +{ + test->showInWindowAndWaitUntilMapped(); + test->loadHtml("<html><body><p style='position:absolute; left:1; top:1'>WebKitGTK+ Context menu tests</p><br>" + "<a style='position:absolute; left:1; top:100' href='http://www.webkitgtk.org'>WebKitGTK+ Website</a></body></html>", + "ContextMenuTestNode"); + test->waitUntilLoadFinished(); + + test->showContextMenuAtPositionAndWaitUntilFinished(0, 0); + g_assert_cmpstr(test->m_node.name.data(), ==, "HTML"); + g_assert_cmpuint(test->m_node.type, ==, ContextMenuWebExtensionNodeTest::Node::NodeElement); + g_assert_cmpstr(test->m_node.contents.data(), ==, "WebKitGTK+ Context menu testsWebKitGTK+ Website"); + g_assert_cmpstr(test->m_node.parentName.data(), ==, "#document"); + + test->showContextMenuAtPositionAndWaitUntilFinished(1, 20); + g_assert_cmpstr(test->m_node.name.data(), ==, "#text"); + g_assert_cmpuint(test->m_node.type, ==, ContextMenuWebExtensionNodeTest::Node::NodeText); + g_assert_cmpstr(test->m_node.contents.data(), ==, "WebKitGTK+ Context menu tests"); + g_assert_cmpstr(test->m_node.parentName.data(), ==, "P"); + + // Link menu. + test->showContextMenuAtPositionAndWaitUntilFinished(1, 101); + g_assert_cmpstr(test->m_node.name.data(), ==, "#text"); + g_assert_cmpuint(test->m_node.type, ==, ContextMenuWebExtensionNodeTest::Node::NodeText); + g_assert_cmpstr(test->m_node.contents.data(), ==, "WebKitGTK+ Website"); + g_assert_cmpstr(test->m_node.parentName.data(), ==, "A"); +} + void beforeAll() { ContextMenuDefaultTest::add("WebKitWebView", "default-menu", testContextMenuDefaultMenu); @@ -860,6 +1034,8 @@ void beforeAll() ContextMenuSubmenuTest::add("WebKitWebView", "submenu", testContextMenuSubMenu); ContextMenuDismissedTest::add("WebKitWebView", "menu-dismissed", testContextMenuDismissed); ContextMenuSmartSeparatorsTest::add("WebKitWebView", "smart-separators", testContextMenuSmartSeparators); + ContextMenuWebExtensionTest::add("WebKitWebPage", "context-menu", testContextMenuWebExtensionMenu); + ContextMenuWebExtensionNodeTest::add("WebKitWebPage", "context-menu-node", testContextMenuWebExtensionNode); } void afterAll() |
