| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/page/ContextMenuController.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include "core/clipboard/DataTransfer.h" | |
| 9 #include "core/events/MouseEvent.h" | |
| 10 #include "core/frame/LocalFrameView.h" | |
| 11 #include "core/frame/Settings.h" | |
| 12 #include "core/html/HTMLElement.h" | |
| 13 #include "core/testing/DummyPageHolder.h" | |
| 14 #include "platform/ContextMenu.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace blink { | |
| 18 | |
| 19 class ContextMenuControllerTest : public testing::Test { | |
| 20 protected: | |
| 21 virtual void SetUp() { | |
| 22 page_holder_ = DummyPageHolder::Create(IntSize(800, 600)); | |
| 23 } | |
| 24 | |
| 25 Document& GetDocument() const { return page_holder_->GetDocument(); } | |
| 26 | |
| 27 void SetBodyInnerHTML(const String& html_content) { | |
| 28 GetDocument().body()->setInnerHTML(html_content); | |
| 29 GetDocument().View()->UpdateAllLifecyclePhases(); | |
| 30 } | |
| 31 | |
| 32 private: | |
| 33 std::unique_ptr<DummyPageHolder> page_holder_; | |
| 34 }; | |
| 35 | |
| 36 TEST_F(ContextMenuControllerTest, TestCustomMenu) { | |
| 37 GetDocument().GetSettings()->SetScriptEnabled(true); | |
| 38 // Load the the test page. | |
| 39 SetBodyInnerHTML( | |
| 40 "<button id=\"button_id\" contextmenu=\"menu_id\" style=\"height: 100px; " | |
| 41 "width: 100px;\">" | |
| 42 "<menu type=\"context\" id=\"menu_id\">" | |
| 43 "<menuitem label=\"Item1\" onclick='document.title = \"Title 1\";'>" | |
| 44 "</menuitem>" | |
| 45 "<menuitem label=\"Item2\" onclick='document.title = \"Title 2\";'>" | |
| 46 "</menuitem>" | |
| 47 "<menuitem label=\"Item3\" onclick='document.title = \"Title 3\";'>" | |
| 48 "</menuitem>" | |
| 49 "<menu label='Submenu'>" | |
| 50 "<menuitem label=\"Item4\" onclick='document.title = \"Title 4\";'>" | |
| 51 "</menuitem>" | |
| 52 "<menuitem label=\"Item5\" onclick='document.title = \"Title 5\";'>" | |
| 53 "</menuitem>" | |
| 54 "<menuitem label=\"Item6\" onclick='document.title = \"Title 6\";'>" | |
| 55 "</menuitem>" | |
| 56 "</menu>" | |
| 57 "<menuitem id=\"item7\" type=\"checkbox\" checked label=\"Item7\"" | |
| 58 "onclick='if " | |
| 59 "(document.getElementById(\"item7\").hasAttribute(\"checked\"))" | |
| 60 "document.title = \"Title 7 checked\"; else document.title = \"Title 7 " | |
| 61 "not checked\";'>" | |
| 62 "</menuitem>" | |
| 63 "<menuitem id=\"item8\" type=\"radio\" radiogroup=\"group\" " | |
| 64 "label=\"Item8\"" | |
| 65 "onclick='if " | |
| 66 "(document.getElementById(\"item8\").hasAttribute(\"checked\"))" | |
| 67 "document.title = \"Title 8 checked\"; else if " | |
| 68 "(document.getElementById(\"item9\").hasAttribute(\"checked\"))" | |
| 69 "document.title = \"Title 8 not checked and Title 9 checked\";'>" | |
| 70 "</menuitem>" | |
| 71 "<menuitem id=\"item9\" type=\"radio\" radiogroup=\"group\" checked " | |
| 72 "label=\"Item9\"" | |
| 73 "onclick='if " | |
| 74 "(document.getElementById(\"item9\").hasAttribute(\"checked\"))" | |
| 75 "document.title = \"Title 9 checked\"; else document.title = \"Title 9 " | |
| 76 "not checked\";'>" | |
| 77 "</menuitem>" | |
| 78 "<menuitem id=\"item10\" type=\"radio\" radiogroup=\"group\" " | |
| 79 "label=\"Item10\"" | |
| 80 "onclick='if " | |
| 81 "(document.getElementById(\"item10\").hasAttribute(\"checked\"))" | |
| 82 "document.title = \"Title 10 checked\"; else if " | |
| 83 "(document.getElementById(\"item8\").hasAttribute(\"checked\"))" | |
| 84 "document.title = \"Title 10 not checked and Title 8 checked\";'>" | |
| 85 "</menuitem>" | |
| 86 "</menu>" | |
| 87 "</button>"); | |
| 88 | |
| 89 MouseEventInit mouse_initializer; | |
| 90 mouse_initializer.setView(GetDocument().domWindow()); | |
| 91 mouse_initializer.setScreenX(50); | |
| 92 mouse_initializer.setScreenY(50); | |
| 93 mouse_initializer.setButton(1); | |
| 94 | |
| 95 // Create right button click event and pass it to context menu controller. | |
| 96 Event* event = | |
| 97 MouseEvent::Create(nullptr, EventTypeNames::click, mouse_initializer); | |
| 98 GetDocument().getElementById("button_id")->focus(); | |
| 99 event->SetTarget(GetDocument().getElementById("button_id")); | |
| 100 GetDocument().GetPage()->GetContextMenuController().HandleContextMenuEvent( | |
| 101 event); | |
| 102 | |
| 103 // Item 1 | |
| 104 // Item 2 | |
| 105 // Item 3 | |
| 106 // Submenu > Item 4 | |
| 107 // Item 5 | |
| 108 // Item 6 | |
| 109 // *Item 7 | |
| 110 // Item 8 | |
| 111 // *Item 9 | |
| 112 // Item 10 | |
| 113 const Vector<ContextMenuItem>& items = GetDocument() | |
| 114 .GetPage() | |
| 115 ->GetContextMenuController() | |
| 116 .GetContextMenu() | |
| 117 ->Items(); | |
| 118 EXPECT_EQ(8u, items.size()); | |
| 119 EXPECT_EQ(kActionType, items[0].GetType()); | |
| 120 EXPECT_STREQ("Item1", items[0].Title().Utf8().data()); | |
| 121 GetDocument().GetPage()->GetContextMenuController().ContextMenuItemSelected( | |
| 122 &items[0]); | |
| 123 EXPECT_STREQ("Title 1", GetDocument().title().Utf8().data()); | |
| 124 EXPECT_EQ(kSubmenuType, items[3].GetType()); | |
| 125 EXPECT_STREQ("Submenu", items[3].Title().Utf8().data()); | |
| 126 const Vector<ContextMenuItem>& sub_menu_items = items[3].SubMenuItems(); | |
| 127 EXPECT_EQ(3u, sub_menu_items.size()); | |
| 128 EXPECT_STREQ("Item6", sub_menu_items[2].Title().Utf8().data()); | |
| 129 GetDocument().GetPage()->GetContextMenuController().ContextMenuItemSelected( | |
| 130 &sub_menu_items[2]); | |
| 131 EXPECT_STREQ("Title 6", GetDocument().title().Utf8().data()); | |
| 132 GetDocument().GetPage()->GetContextMenuController().ContextMenuItemSelected( | |
| 133 &items[4]); | |
| 134 EXPECT_STREQ("Title 7 checked", GetDocument().title().Utf8().data()); | |
| 135 GetDocument().GetPage()->GetContextMenuController().ContextMenuItemSelected( | |
| 136 &items[4]); | |
| 137 EXPECT_STREQ("Title 7 not checked", GetDocument().title().Utf8().data()); | |
| 138 GetDocument().GetPage()->GetContextMenuController().ContextMenuItemSelected( | |
| 139 &items[5]); | |
| 140 EXPECT_STREQ("Title 8 not checked and Title 9 checked", | |
| 141 GetDocument().title().Utf8().data()); | |
| 142 GetDocument().GetPage()->GetContextMenuController().ContextMenuItemSelected( | |
| 143 &items[7]); | |
| 144 EXPECT_STREQ("Title 10 not checked and Title 8 checked", | |
| 145 GetDocument().title().Utf8().data()); | |
| 146 } | |
| 147 | |
| 148 } // namespace blink | |
| OLD | NEW |