Chromium Code Reviews| Index: Source/web/tests/ContextMenuTest.cpp |
| diff --git a/Source/web/tests/ContextMenuTest.cpp b/Source/web/tests/ContextMenuTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..97fc550ae361bed845a7d16ba7c088b8af59d6f1 |
| --- /dev/null |
| +++ b/Source/web/tests/ContextMenuTest.cpp |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
tkent
2014/07/25 04:42:39
We should remove web/ dependency from the test, an
pals
2014/07/30 09:47:45
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| + |
| +#include "core/clipboard/DataTransfer.h" |
| +#include "core/events/MouseEvent.h" |
| +#include "core/page/ContextMenuController.h" |
| +#include "core/page/Page.h" |
| +#include "platform/ContextMenuItem.h" |
| +#include "platform/ContextMenu.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebUnitTestSupport.h" |
| +#include "public/web/WebSettings.h" |
| +#include "web/WebLocalFrameImpl.h" |
| +#include "web/WebViewImpl.h" |
| +#include "web/tests/FrameTestHelpers.h" |
| +#include "web/tests/URLTestHelpers.h" |
| +#include <gtest/gtest.h> |
| + |
| +using namespace WebCore; |
| +using namespace blink; |
| +using blink::URLTestHelpers::toKURL; |
| + |
| +namespace { |
| + |
| +class CustomContextMenuTest : public testing::Test { |
| +public: |
| + CustomContextMenuTest() |
| + : baseURL("http://www.test.com/") |
| + { |
| + } |
| + |
| + void registerMockedURLLoad(const std::string& fileName) |
| + { |
| + URLTestHelpers::registerMockedURLLoad(toKURL(baseURL + fileName), WebString::fromUTF8(fileName.c_str()), WebString::fromUTF8("context_menu/"), WebString::fromUTF8("text/html")); |
| + } |
| + |
| + void loadFrame(WebFrame* frame, const std::string& fileName) |
| + { |
| + FrameTestHelpers::loadFrame(frame, baseURL + fileName); |
| + } |
| + |
| + WebViewImpl* webView() const { return m_helper.webViewImpl(); } |
| + WebLocalFrameImpl* mainFrame() const { return m_helper.webViewImpl()->mainFrameImpl(); } |
| + |
| +protected: |
| + virtual void SetUp() |
| + { |
| + m_helper.initialize(false, 0, &m_webviewClient); |
| + } |
| + |
| + virtual void TearDown() |
| + { |
| + Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); |
| + } |
| + |
| +protected: |
| + FrameTestHelpers::TestWebViewClient m_webviewClient; |
| + std::string baseURL; |
| + |
| +private: |
| + FrameTestHelpers::WebViewHelper m_helper; |
| +}; |
| + |
| +TEST_F(CustomContextMenuTest, TestCustomMenu) |
| +{ |
| + // Load the the test page. |
| + registerMockedURLLoad("custom_context_menu.html"); |
| + webView()->settings()->setJavaScriptEnabled(true); |
| + loadFrame(mainFrame(), "custom_context_menu.html"); |
| + |
| + // Create right button click event and pass it to context menu controller. |
| + Document* document = mainFrame()->frame()->document(); |
| + RefPtrWillBeRawPtr<WebCore::Event> event = WebCore::MouseEvent::create(WebCore::EventTypeNames::click, false, false, |
| + document->domWindow(), 50, 50, 0, 0, 0, 0, 0, false, false, false, false, 1, nullptr, nullptr); |
| + event->setTarget(document->focusedElement()); |
| + document->page()->contextMenuController().handleContextMenuEvent(event.get()); |
| + |
| + // Item 1 |
| + // Item 2 |
| + // Item 3 |
| + // Submenu > Item 4 |
| + // Item 5 |
| + // Item 6 |
| + const Vector<ContextMenuItem>& items = document->page()->contextMenuController().contextMenu()->items(); |
| + EXPECT_EQ(4u, items.size()); |
| + EXPECT_EQ(ActionType, items[0].type()); |
| + EXPECT_EQ(SubmenuType, items[3].type()); |
| + EXPECT_STREQ("Submenu", items[3].title().utf8().data()); |
| + document->page()->contextMenuController().contextMenuItemSelected(&items[0]); |
| + EXPECT_STREQ("Title 1", document->title().utf8().data()); |
| + const Vector<ContextMenuItem>& subMenuItems = items[3].subMenuItems(); |
| + EXPECT_EQ(3u, subMenuItems.size()); |
| + document->page()->contextMenuController().contextMenuItemSelected(&subMenuItems[2]); |
| + EXPECT_STREQ("Title 6", document->title().utf8().data()); |
| +} |
| + |
| +} |