| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2010 Google 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 are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "platform/PopupMenu.h" | |
| 33 | |
| 34 #include "core/dom/Element.h" | |
| 35 #include "core/frame/FrameView.h" | |
| 36 #include "core/frame/Settings.h" | |
| 37 #include "core/html/HTMLSelectElement.h" | |
| 38 #include "core/html/forms/PopupMenuClient.h" | |
| 39 #include "core/page/EventHandler.h" | |
| 40 #include "core/rendering/RenderMenuList.h" | |
| 41 #include "core/testing/URLTestHelpers.h" | |
| 42 #include "platform/KeyboardCodes.h" | |
| 43 #include "platform/PlatformMouseEvent.h" | |
| 44 #include "platform/RuntimeEnabledFeatures.h" | |
| 45 #include "platform/graphics/Color.h" | |
| 46 #include "platform/scroll/ScrollbarTheme.h" | |
| 47 #include "public/platform/Platform.h" | |
| 48 #include "public/platform/WebScreenInfo.h" | |
| 49 #include "public/platform/WebString.h" | |
| 50 #include "public/platform/WebURL.h" | |
| 51 #include "public/platform/WebURLRequest.h" | |
| 52 #include "public/platform/WebURLResponse.h" | |
| 53 #include "public/platform/WebUnitTestSupport.h" | |
| 54 #include "public/web/WebDocument.h" | |
| 55 #include "public/web/WebElement.h" | |
| 56 #include "public/web/WebFrame.h" | |
| 57 #include "public/web/WebFrameClient.h" | |
| 58 #include "public/web/WebInputEvent.h" | |
| 59 #include "public/web/WebSettings.h" | |
| 60 #include "public/web/WebView.h" | |
| 61 #include "public/web/WebViewClient.h" | |
| 62 #include "v8.h" | |
| 63 #include "web/PopupContainer.h" | |
| 64 #include "web/PopupListBox.h" | |
| 65 #include "web/PopupMenuChromium.h" | |
| 66 #include "web/WebLocalFrameImpl.h" | |
| 67 #include "web/WebPopupMenuImpl.h" | |
| 68 #include "web/WebViewImpl.h" | |
| 69 #include "web/tests/FrameTestHelpers.h" | |
| 70 #include <gtest/gtest.h> | |
| 71 | |
| 72 using namespace blink; | |
| 73 using blink::URLTestHelpers::toKURL; | |
| 74 | |
| 75 namespace { | |
| 76 | |
| 77 class TestPopupMenuClient : public PopupMenuClient { | |
| 78 public: | |
| 79 // Item at index 0 is selected by default. | |
| 80 TestPopupMenuClient() : m_selectIndex(0), m_node(0), m_listSize(10) { } | |
| 81 virtual ~TestPopupMenuClient() { } | |
| 82 virtual void valueChanged(unsigned listIndex, bool fireEvents = true) | |
| 83 { | |
| 84 m_selectIndex = listIndex; | |
| 85 if (m_node) { | |
| 86 HTMLSelectElement* select = toHTMLSelectElement(m_node); | |
| 87 select->optionSelectedByUser(select->listToOptionIndex(listIndex), f
ireEvents); | |
| 88 } | |
| 89 } | |
| 90 virtual void selectionChanged(unsigned, bool) { } | |
| 91 virtual void selectionCleared() { } | |
| 92 | |
| 93 virtual String itemText(unsigned listIndex) const | |
| 94 { | |
| 95 String str("Item "); | |
| 96 str.append(String::number(listIndex)); | |
| 97 return str; | |
| 98 } | |
| 99 virtual String itemLabel(unsigned) const { return String(); } | |
| 100 virtual String itemIcon(unsigned) const { return String(); } | |
| 101 virtual String itemToolTip(unsigned listIndex) const { return itemText(listI
ndex); } | |
| 102 virtual String itemAccessibilityText(unsigned listIndex) const { return item
Text(listIndex); } | |
| 103 virtual bool itemIsEnabled(unsigned listIndex) const { return m_disabledInde
xSet.find(listIndex) == m_disabledIndexSet.end(); } | |
| 104 virtual PopupMenuStyle itemStyle(unsigned listIndex) const | |
| 105 { | |
| 106 FontDescription fontDescription; | |
| 107 fontDescription.setComputedSize(12.0); | |
| 108 Font font(fontDescription); | |
| 109 font.update(nullptr); | |
| 110 return PopupMenuStyle(Color::black, Color::white, font, true, false, Len
gth(), TextDirection(), false /* has text direction override */); | |
| 111 } | |
| 112 virtual PopupMenuStyle menuStyle() const { return itemStyle(0); } | |
| 113 virtual int clientInsetLeft() const { return 0; } | |
| 114 virtual int clientInsetRight() const { return 0; } | |
| 115 virtual LayoutUnit clientPaddingLeft() const { return 0; } | |
| 116 virtual LayoutUnit clientPaddingRight() const { return 0; } | |
| 117 virtual int listSize() const { return m_listSize; } | |
| 118 virtual int selectedIndex() const { return m_selectIndex; } | |
| 119 virtual void popupDidHide() { } | |
| 120 virtual bool itemIsSeparator(unsigned listIndex) const { return false; } | |
| 121 virtual bool itemIsLabel(unsigned listIndex) const { return false; } | |
| 122 virtual bool itemIsSelected(unsigned listIndex) const { return listIndex ==
m_selectIndex; } | |
| 123 virtual bool valueShouldChangeOnHotTrack() const { return false; } | |
| 124 virtual void setTextFromItem(unsigned listIndex) { } | |
| 125 virtual IntRect elementRectRelativeToRootView() const override { return IntR
ect(); } | |
| 126 virtual Element& ownerElement() const override { return *toElement(m_node);
} | |
| 127 virtual RenderStyle* renderStyleForItem(Element& element) const override { r
eturn nullptr; } | |
| 128 | |
| 129 virtual FontSelector* fontSelector() const { return 0; } | |
| 130 virtual HostWindow* hostWindow() const { return 0; } | |
| 131 | |
| 132 virtual PassRefPtrWillBeRawPtr<Scrollbar> createScrollbar(ScrollableArea*, S
crollbarOrientation, ScrollbarControlSize) { return nullptr; } | |
| 133 | |
| 134 void setDisabledIndex(unsigned index) { m_disabledIndexSet.insert(index); } | |
| 135 void setFocusedNode(Node* node) { m_node = node; } | |
| 136 void setListSize(int listSize) { m_listSize = listSize; } | |
| 137 | |
| 138 private: | |
| 139 unsigned m_selectIndex; | |
| 140 std::set<unsigned> m_disabledIndexSet; | |
| 141 Node* m_node; | |
| 142 int m_listSize; | |
| 143 }; | |
| 144 | |
| 145 class TestWebWidgetClient : public WebWidgetClient { | |
| 146 public: | |
| 147 ~TestWebWidgetClient() { } | |
| 148 }; | |
| 149 | |
| 150 class TestWebPopupMenuImpl : public WebPopupMenuImpl { | |
| 151 public: | |
| 152 static PassRefPtr<TestWebPopupMenuImpl> create(WebWidgetClient* client) | |
| 153 { | |
| 154 return adoptRef(new TestWebPopupMenuImpl(client)); | |
| 155 } | |
| 156 | |
| 157 ~TestWebPopupMenuImpl() { } | |
| 158 | |
| 159 private: | |
| 160 TestWebPopupMenuImpl(WebWidgetClient* client) : WebPopupMenuImpl(client) { } | |
| 161 }; | |
| 162 | |
| 163 class PopupTestWebViewClient : public FrameTestHelpers::TestWebViewClient { | |
| 164 public: | |
| 165 PopupTestWebViewClient() : m_webPopupMenu(TestWebPopupMenuImpl::create(&m_we
bWidgetClient)) { } | |
| 166 ~PopupTestWebViewClient() { } | |
| 167 | |
| 168 virtual WebWidget* createPopupMenu(WebPopupType) { return m_webPopupMenu.get
(); } | |
| 169 | |
| 170 // We need to override this so that the popup menu size is not 0 | |
| 171 // (the layout code checks to see if the popup fits on the screen). | |
| 172 virtual WebScreenInfo screenInfo() | |
| 173 { | |
| 174 WebScreenInfo screenInfo; | |
| 175 screenInfo.availableRect.height = 2000; | |
| 176 screenInfo.availableRect.width = 2000; | |
| 177 return screenInfo; | |
| 178 } | |
| 179 | |
| 180 private: | |
| 181 TestWebWidgetClient m_webWidgetClient; | |
| 182 RefPtr<TestWebPopupMenuImpl> m_webPopupMenu; | |
| 183 }; | |
| 184 | |
| 185 class SelectPopupMenuTest : public testing::Test { | |
| 186 public: | |
| 187 SelectPopupMenuTest() | |
| 188 : baseURL("http://www.test.com/") | |
| 189 { | |
| 190 } | |
| 191 | |
| 192 protected: | |
| 193 virtual void SetUp() | |
| 194 { | |
| 195 m_helper.initialize(false, 0, &m_webviewClient); | |
| 196 m_popupMenu = adoptRefWillBeNoop(new PopupMenuChromium(*mainFrame()->fra
me(), &m_popupMenuClient)); | |
| 197 } | |
| 198 | |
| 199 virtual void TearDown() | |
| 200 { | |
| 201 m_popupMenu->disconnectClient(); | |
| 202 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | |
| 203 } | |
| 204 | |
| 205 // Returns true if there currently is a select popup in the WebView. | |
| 206 bool popupOpen() const { return webView()->selectPopup(); } | |
| 207 | |
| 208 int selectedIndex() const { return m_popupMenuClient.selectedIndex(); } | |
| 209 | |
| 210 void showPopup() | |
| 211 { | |
| 212 m_popupMenu->show(FloatQuad(FloatRect(0, 0, 100, 100)), IntSize(100, 100
), 0); | |
| 213 ASSERT_TRUE(popupOpen()); | |
| 214 } | |
| 215 | |
| 216 void hidePopup() | |
| 217 { | |
| 218 m_popupMenu->hide(); | |
| 219 EXPECT_FALSE(popupOpen()); | |
| 220 } | |
| 221 | |
| 222 void simulateKeyDownEvent(int keyCode) | |
| 223 { | |
| 224 simulateKeyEvent(WebInputEvent::RawKeyDown, keyCode); | |
| 225 } | |
| 226 | |
| 227 void simulateKeyUpEvent(int keyCode) | |
| 228 { | |
| 229 simulateKeyEvent(WebInputEvent::KeyUp, keyCode); | |
| 230 } | |
| 231 | |
| 232 // Simulates a key event on the WebView. | |
| 233 // The WebView forwards the event to the select popup if one is open. | |
| 234 void simulateKeyEvent(WebInputEvent::Type eventType, int keyCode) | |
| 235 { | |
| 236 WebKeyboardEvent keyEvent; | |
| 237 keyEvent.windowsKeyCode = keyCode; | |
| 238 keyEvent.type = eventType; | |
| 239 webView()->handleInputEvent(keyEvent); | |
| 240 } | |
| 241 | |
| 242 // Simulates a mouse event on the select popup. | |
| 243 void simulateLeftMouseDownEvent(const IntPoint& point) | |
| 244 { | |
| 245 PlatformMouseEvent mouseEvent(point, point, LeftButton, PlatformEvent::M
ousePressed, | |
| 246 1, false, false, false, false, PlatformMouseEvent::RealOrIndistingui
shable, 0); | |
| 247 webView()->selectPopup()->handleMouseDownEvent(mouseEvent); | |
| 248 } | |
| 249 void simulateLeftMouseUpEvent(const IntPoint& point) | |
| 250 { | |
| 251 PlatformMouseEvent mouseEvent(point, point, LeftButton, PlatformEvent::M
ouseReleased, | |
| 252 1, false, false, false, false, PlatformMouseEvent::RealOrIndistingui
shable, 0); | |
| 253 webView()->selectPopup()->handleMouseReleaseEvent(mouseEvent); | |
| 254 } | |
| 255 | |
| 256 void registerMockedURLLoad(const std::string& fileName) | |
| 257 { | |
| 258 URLTestHelpers::registerMockedURLLoad(toKURL(baseURL + fileName), WebStr
ing::fromUTF8(fileName.c_str()), WebString::fromUTF8("popup/"), WebString::fromU
TF8("text/html")); | |
| 259 } | |
| 260 | |
| 261 void loadFrame(WebFrame* frame, const std::string& fileName) | |
| 262 { | |
| 263 FrameTestHelpers::loadFrame(frame, baseURL + fileName); | |
| 264 } | |
| 265 | |
| 266 WebViewImpl* webView() const { return m_helper.webViewImpl(); } | |
| 267 WebLocalFrameImpl* mainFrame() const { return m_helper.webViewImpl()->mainFr
ameImpl(); } | |
| 268 | |
| 269 protected: | |
| 270 PopupTestWebViewClient m_webviewClient; | |
| 271 TestPopupMenuClient m_popupMenuClient; | |
| 272 RefPtrWillBePersistent<PopupMenu> m_popupMenu; | |
| 273 std::string baseURL; | |
| 274 | |
| 275 private: | |
| 276 FrameTestHelpers::WebViewHelper m_helper; | |
| 277 }; | |
| 278 | |
| 279 // Tests that show/hide and repeats. Select popups are reused in web pages when | |
| 280 // they are reopened, that what this is testing. | |
| 281 TEST_F(SelectPopupMenuTest, ShowThenHide) | |
| 282 { | |
| 283 for (int i = 0; i < 3; i++) { | |
| 284 showPopup(); | |
| 285 hidePopup(); | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 // Tests that showing a select popup and deleting it does not cause problem. | |
| 290 // This happens in real-life if a page navigates while a select popup is showing
. | |
| 291 TEST_F(SelectPopupMenuTest, ShowThenDelete) | |
| 292 { | |
| 293 showPopup(); | |
| 294 // Nothing else to do, TearDown() deletes the popup. | |
| 295 } | |
| 296 | |
| 297 // Tests that losing focus closes the select popup. | |
| 298 TEST_F(SelectPopupMenuTest, ShowThenLoseFocus) | |
| 299 { | |
| 300 showPopup(); | |
| 301 // Simulate losing focus. | |
| 302 webView()->setFocus(false); | |
| 303 | |
| 304 // Popup should have closed. | |
| 305 EXPECT_FALSE(popupOpen()); | |
| 306 } | |
| 307 | |
| 308 // Tests that pressing ESC closes the popup. | |
| 309 TEST_F(SelectPopupMenuTest, ShowThenPressESC) | |
| 310 { | |
| 311 showPopup(); | |
| 312 simulateKeyDownEvent(VKEY_ESCAPE); | |
| 313 // Popup should have closed. | |
| 314 EXPECT_FALSE(popupOpen()); | |
| 315 } | |
| 316 | |
| 317 // Tests selecting an item with the arrows and enter/esc/tab. | |
| 318 TEST_F(SelectPopupMenuTest, SelectWithKeys) | |
| 319 { | |
| 320 showPopup(); | |
| 321 // Simulate selecting the 2nd item by pressing Down, Down, enter. | |
| 322 simulateKeyDownEvent(VKEY_DOWN); | |
| 323 simulateKeyDownEvent(VKEY_DOWN); | |
| 324 simulateKeyDownEvent(VKEY_RETURN); | |
| 325 | |
| 326 // Popup should have closed. | |
| 327 EXPECT_TRUE(!popupOpen()); | |
| 328 EXPECT_EQ(2, selectedIndex()); | |
| 329 | |
| 330 // It should work as well with ESC. | |
| 331 showPopup(); | |
| 332 simulateKeyDownEvent(VKEY_DOWN); | |
| 333 simulateKeyDownEvent(VKEY_ESCAPE); | |
| 334 EXPECT_FALSE(popupOpen()); | |
| 335 EXPECT_EQ(3, selectedIndex()); | |
| 336 | |
| 337 // It should work as well with TAB. | |
| 338 showPopup(); | |
| 339 simulateKeyDownEvent(VKEY_DOWN); | |
| 340 simulateKeyDownEvent(VKEY_TAB); | |
| 341 EXPECT_FALSE(popupOpen()); | |
| 342 EXPECT_EQ(4, selectedIndex()); | |
| 343 } | |
| 344 | |
| 345 // Tests that selecting an item with the mouse does select the item and close | |
| 346 // the popup. | |
| 347 TEST_F(SelectPopupMenuTest, ClickItem) | |
| 348 { | |
| 349 showPopup(); | |
| 350 | |
| 351 int menuItemHeight = webView()->selectPopup()->menuItemHeight(); | |
| 352 // menuItemHeight * 1.5 means the Y position on the item at index 1. | |
| 353 IntPoint row1Point(2, menuItemHeight * 1.5); | |
| 354 // Simulate a click down/up on the first item. | |
| 355 simulateLeftMouseDownEvent(row1Point); | |
| 356 simulateLeftMouseUpEvent(row1Point); | |
| 357 | |
| 358 // Popup should have closed and the item at index 1 selected. | |
| 359 EXPECT_FALSE(popupOpen()); | |
| 360 EXPECT_EQ(1, selectedIndex()); | |
| 361 } | |
| 362 | |
| 363 // Tests that moving the mouse over an item and then clicking outside the select
popup | |
| 364 // leaves the seleted item unchanged. | |
| 365 TEST_F(SelectPopupMenuTest, MouseOverItemClickOutside) | |
| 366 { | |
| 367 showPopup(); | |
| 368 | |
| 369 int menuItemHeight = webView()->selectPopup()->menuItemHeight(); | |
| 370 // menuItemHeight * 1.5 means the Y position on the item at index 1. | |
| 371 IntPoint row1Point(2, menuItemHeight * 1.5); | |
| 372 // Simulate the mouse moving over the first item. | |
| 373 PlatformMouseEvent mouseEvent(row1Point, row1Point, NoButton, PlatformEvent:
:MouseMoved, | |
| 374 1, false, false, false, false, PlatformMouseEvent::RealOrIndistinguishab
le, 0); | |
| 375 webView()->selectPopup()->handleMouseMoveEvent(mouseEvent); | |
| 376 | |
| 377 // Click outside the popup. | |
| 378 simulateLeftMouseDownEvent(IntPoint(1000, 1000)); | |
| 379 | |
| 380 // Popup should have closed and item 0 should still be selected. | |
| 381 EXPECT_FALSE(popupOpen()); | |
| 382 EXPECT_EQ(0, selectedIndex()); | |
| 383 } | |
| 384 | |
| 385 // Tests that selecting an item with the keyboard and then clicking outside the
select | |
| 386 // popup does select that item. | |
| 387 TEST_F(SelectPopupMenuTest, SelectItemWithKeyboardItemClickOutside) | |
| 388 { | |
| 389 showPopup(); | |
| 390 | |
| 391 // Simulate selecting the 2nd item by pressing Down, Down. | |
| 392 simulateKeyDownEvent(VKEY_DOWN); | |
| 393 simulateKeyDownEvent(VKEY_DOWN); | |
| 394 | |
| 395 // Click outside the popup. | |
| 396 simulateLeftMouseDownEvent(IntPoint(1000, 1000)); | |
| 397 | |
| 398 // Popup should have closed and the item should have been selected. | |
| 399 EXPECT_FALSE(popupOpen()); | |
| 400 EXPECT_EQ(2, selectedIndex()); | |
| 401 } | |
| 402 | |
| 403 TEST_F(SelectPopupMenuTest, DISABLED_SelectItemEventFire) | |
| 404 { | |
| 405 registerMockedURLLoad("select_event.html"); | |
| 406 webView()->settings()->setJavaScriptEnabled(true); | |
| 407 loadFrame(mainFrame(), "select_event.html"); | |
| 408 | |
| 409 m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedEl
ement()); | |
| 410 | |
| 411 showPopup(); | |
| 412 | |
| 413 int menuItemHeight = webView()->selectPopup()->menuItemHeight(); | |
| 414 // menuItemHeight * 0.5 means the Y position on the item at index 0. | |
| 415 IntPoint row1Point(2, menuItemHeight * 0.5); | |
| 416 simulateLeftMouseDownEvent(row1Point); | |
| 417 simulateLeftMouseUpEvent(row1Point); | |
| 418 | |
| 419 WebElement element = webView()->mainFrame()->document().getElementById("mess
age"); | |
| 420 | |
| 421 // mousedown event is held by select node, and we don't simulate the event f
or the node. | |
| 422 // So we can only see mouseup and click event. | |
| 423 EXPECT_STREQ("upclick", element.innerText().utf8().data()); | |
| 424 | |
| 425 // Disable the item at index 1. | |
| 426 m_popupMenuClient.setDisabledIndex(1); | |
| 427 | |
| 428 showPopup(); | |
| 429 // menuItemHeight * 1.5 means the Y position on the item at index 1. | |
| 430 row1Point.setY(menuItemHeight * 1.5); | |
| 431 simulateLeftMouseDownEvent(row1Point); | |
| 432 simulateLeftMouseUpEvent(row1Point); | |
| 433 | |
| 434 // The item at index 1 is disabled, so the text should not be changed. | |
| 435 EXPECT_STREQ("upclick", element.innerText().utf8().data()); | |
| 436 | |
| 437 showPopup(); | |
| 438 // menuItemHeight * 2.5 means the Y position on the item at index 2. | |
| 439 row1Point.setY(menuItemHeight * 2.5); | |
| 440 simulateLeftMouseDownEvent(row1Point); | |
| 441 simulateLeftMouseUpEvent(row1Point); | |
| 442 | |
| 443 // The item is changed to the item at index 2, from index 0, so change event
is fired. | |
| 444 EXPECT_STREQ("upclickchangeupclick", element.innerText().utf8().data()); | |
| 445 } | |
| 446 | |
| 447 TEST_F(SelectPopupMenuTest, FLAKY_SelectItemKeyEvent) | |
| 448 { | |
| 449 registerMockedURLLoad("select_event.html"); | |
| 450 webView()->settings()->setJavaScriptEnabled(true); | |
| 451 loadFrame(mainFrame(), "select_event.html"); | |
| 452 | |
| 453 m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedEl
ement()); | |
| 454 | |
| 455 showPopup(); | |
| 456 | |
| 457 // Siumulate to choose the item at index 1 with keyboard. | |
| 458 simulateKeyDownEvent(VKEY_DOWN); | |
| 459 simulateKeyDownEvent(VKEY_DOWN); | |
| 460 simulateKeyDownEvent(VKEY_RETURN); | |
| 461 | |
| 462 WebElement element = webView()->mainFrame()->document().getElementById("mess
age"); | |
| 463 // We only can see change event but no other mouse related events. | |
| 464 EXPECT_STREQ("change", element.innerText().utf8().data()); | |
| 465 } | |
| 466 | |
| 467 TEST_F(SelectPopupMenuTest, SelectItemRemoveSelectOnChange) | |
| 468 { | |
| 469 // Make sure no crash, even if select node is removed on 'change' event hand
ler. | |
| 470 registerMockedURLLoad("select_event_remove_on_change.html"); | |
| 471 webView()->settings()->setJavaScriptEnabled(true); | |
| 472 loadFrame(mainFrame(), "select_event_remove_on_change.html"); | |
| 473 | |
| 474 m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedEl
ement()); | |
| 475 | |
| 476 showPopup(); | |
| 477 | |
| 478 int menuItemHeight = webView()->selectPopup()->menuItemHeight(); | |
| 479 // menuItemHeight * 1.5 means the Y position on the item at index 1. | |
| 480 IntPoint row1Point(2, menuItemHeight * 1.5); | |
| 481 simulateLeftMouseDownEvent(row1Point); | |
| 482 simulateLeftMouseUpEvent(row1Point); | |
| 483 | |
| 484 WebElement element = webView()->mainFrame()->document().getElementById("mess
age"); | |
| 485 EXPECT_STREQ("change", element.innerText().utf8().data()); | |
| 486 } | |
| 487 | |
| 488 TEST_F(SelectPopupMenuTest, SelectItemRemoveSelectOnClick) | |
| 489 { | |
| 490 // Make sure no crash, even if select node is removed on 'click' event handl
er. | |
| 491 registerMockedURLLoad("select_event_remove_on_click.html"); | |
| 492 webView()->settings()->setJavaScriptEnabled(true); | |
| 493 loadFrame(mainFrame(), "select_event_remove_on_click.html"); | |
| 494 | |
| 495 m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedEl
ement()); | |
| 496 | |
| 497 showPopup(); | |
| 498 | |
| 499 int menuItemHeight = webView()->selectPopup()->menuItemHeight(); | |
| 500 // menuItemHeight * 1.5 means the Y position on the item at index 1. | |
| 501 IntPoint row1Point(2, menuItemHeight * 1.5); | |
| 502 simulateLeftMouseDownEvent(row1Point); | |
| 503 simulateLeftMouseUpEvent(row1Point); | |
| 504 | |
| 505 WebElement element = webView()->mainFrame()->document().getElementById("mess
age"); | |
| 506 EXPECT_STREQ("click", element.innerText().utf8().data()); | |
| 507 } | |
| 508 | |
| 509 #if OS(ANDROID) | |
| 510 TEST_F(SelectPopupMenuTest, DISABLED_PopupListBoxWithOverlayScrollbarEnabled) | |
| 511 #else | |
| 512 TEST_F(SelectPopupMenuTest, PopupListBoxWithOverlayScrollbarEnabled) | |
| 513 #endif | |
| 514 { | |
| 515 Settings::setMockScrollbarsEnabled(true); | |
| 516 RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(true); | |
| 517 EXPECT_TRUE(ScrollbarTheme::theme()->usesOverlayScrollbars()); | |
| 518 registerMockedURLLoad("select_rtl.html"); | |
| 519 loadFrame(mainFrame(), "select_rtl.html"); | |
| 520 | |
| 521 m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedEl
ement()); | |
| 522 m_popupMenuClient.setListSize(30); | |
| 523 | |
| 524 showPopup(); | |
| 525 PopupContainer* container = webView()->selectPopup(); | |
| 526 PopupListBox* listBox = container->listBox(); | |
| 527 | |
| 528 EXPECT_EQ(container->width(), listBox->contentsSize().width() + 2); | |
| 529 Settings::setMockScrollbarsEnabled(false); | |
| 530 RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(false); | |
| 531 EXPECT_FALSE(ScrollbarTheme::theme()->usesOverlayScrollbars()); | |
| 532 } | |
| 533 | |
| 534 #if OS(ANDROID) | |
| 535 TEST_F(SelectPopupMenuTest, DISABLED_PopupListBoxWithOverlayScrollbarDisabled) | |
| 536 #else | |
| 537 TEST_F(SelectPopupMenuTest, PopupListBoxWithOverlayScrollbarDisabled) | |
| 538 #endif | |
| 539 { | |
| 540 EXPECT_FALSE(ScrollbarTheme::theme()->usesOverlayScrollbars()); | |
| 541 registerMockedURLLoad("select_rtl.html"); | |
| 542 loadFrame(mainFrame(), "select_rtl.html"); | |
| 543 | |
| 544 m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedEl
ement()); | |
| 545 m_popupMenuClient.setListSize(30); | |
| 546 | |
| 547 showPopup(); | |
| 548 PopupContainer* container = webView()->selectPopup(); | |
| 549 PopupListBox* listBox = container->listBox(); | |
| 550 | |
| 551 EXPECT_EQ(container->width(), listBox->contentsSize().width() + ScrollbarThe
me::theme()->scrollbarThickness() + 2); | |
| 552 } | |
| 553 | |
| 554 class SelectPopupMenuStyleTest : public testing::Test { | |
| 555 public: | |
| 556 SelectPopupMenuStyleTest() | |
| 557 : baseURL("http://www.test.com/") | |
| 558 { | |
| 559 } | |
| 560 | |
| 561 protected: | |
| 562 virtual void SetUp() override | |
| 563 { | |
| 564 m_helper.initialize(false, 0, &m_webviewClient); | |
| 565 } | |
| 566 | |
| 567 virtual void TearDown() override | |
| 568 { | |
| 569 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | |
| 570 } | |
| 571 | |
| 572 // Returns true if there currently is a select popup in the WebView. | |
| 573 bool popupOpen() const { return webView()->selectPopup(); } | |
| 574 | |
| 575 void registerMockedURLLoad(const std::string& fileName) | |
| 576 { | |
| 577 URLTestHelpers::registerMockedURLLoad(toKURL(baseURL + fileName), WebStr
ing::fromUTF8(fileName.c_str()), WebString::fromUTF8("popup/"), WebString::fromU
TF8("text/html")); | |
| 578 } | |
| 579 | |
| 580 void loadFrame(WebFrame* frame, const std::string& fileName) | |
| 581 { | |
| 582 FrameTestHelpers::loadFrame(frame, baseURL + fileName); | |
| 583 } | |
| 584 | |
| 585 WebViewImpl* webView() const { return m_helper.webViewImpl(); } | |
| 586 WebLocalFrameImpl* mainFrame() const { return m_helper.webViewImpl()->mainFr
ameImpl(); } | |
| 587 | |
| 588 protected: | |
| 589 PopupTestWebViewClient m_webviewClient; | |
| 590 std::string baseURL; | |
| 591 | |
| 592 private: | |
| 593 FrameTestHelpers::WebViewHelper m_helper; | |
| 594 }; | |
| 595 | |
| 596 #if OS(MACOSX) || OS(ANDROID) | |
| 597 TEST_F(SelectPopupMenuStyleTest, DISABLED_PopupListBoxRTLRowWidth) | |
| 598 #else | |
| 599 TEST_F(SelectPopupMenuStyleTest, PopupListBoxRTLRowWidth) | |
| 600 #endif | |
| 601 { | |
| 602 registerMockedURLLoad("select_rtl_width.html"); | |
| 603 loadFrame(mainFrame(), "select_rtl_width.html"); | |
| 604 HTMLSelectElement* select = toHTMLSelectElement(mainFrame()->frame()->docume
nt()->focusedElement()); | |
| 605 RenderMenuList* menuList = toRenderMenuList(select->renderer()); | |
| 606 ASSERT(menuList); | |
| 607 menuList->showPopup(); | |
| 608 ASSERT(popupOpen()); | |
| 609 PopupListBox* listBox = webView()->selectPopup()->listBox(); | |
| 610 int ltrWidth = listBox->getRowBaseWidth(0); | |
| 611 int rtlWidth = listBox->getRowBaseWidth(1); | |
| 612 EXPECT_LT(rtlWidth, ltrWidth); | |
| 613 } | |
| 614 | |
| 615 } // namespace | |
| OLD | NEW |