This source file includes following definitions.
- m_node
- valueChanged
- selectionChanged
- selectionCleared
- itemText
- itemLabel
- itemIcon
- itemToolTip
- itemAccessibilityText
- itemIsEnabled
- itemStyle
- menuStyle
- clientInsetLeft
- clientInsetRight
- clientPaddingLeft
- clientPaddingRight
- listSize
- selectedIndex
- popupDidHide
- itemIsSeparator
- itemIsLabel
- itemIsSelected
- valueShouldChangeOnHotTrack
- setTextFromItem
- fontSelector
- hostWindow
- createScrollbar
- setDisabledIndex
- setFocusedNode
- create
- createPopupMenu
- screenInfo
- SetUp
- TearDown
- popupOpen
- selectedIndex
- showPopup
- hidePopup
- simulateKeyDownEvent
- simulateKeyUpEvent
- simulateKeyEvent
- simulateLeftMouseDownEvent
- simulateLeftMouseUpEvent
- registerMockedURLLoad
- serveRequests
- loadFrame
- webView
- mainFrame
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "config.h"
#include "FrameTestHelpers.h"
#include "PopupContainer.h"
#include "PopupMenuChromium.h"
#include "RuntimeEnabledFeatures.h"
#include "URLTestHelpers.h"
#include "WebDocument.h"
#include "WebElement.h"
#include "WebFrame.h"
#include "WebFrameClient.h"
#include "WebFrameImpl.h"
#include "WebInputEvent.h"
#include "WebPopupMenuImpl.h"
#include "WebSettings.h"
#include "WebView.h"
#include "WebViewClient.h"
#include "WebViewImpl.h"
#include "core/dom/Element.h"
#include "core/frame/FrameView.h"
#include "core/html/HTMLSelectElement.h"
#include "core/page/EventHandler.h"
#include "platform/KeyboardCodes.h"
#include "platform/PlatformMouseEvent.h"
#include "platform/PopupMenuClient.h"
#include "platform/PopupMenu.h"
#include "platform/graphics/Color.h"
#include "public/platform/Platform.h"
#include "public/platform/WebScreenInfo.h"
#include "public/platform/WebString.h"
#include "public/platform/WebURL.h"
#include "public/platform/WebURLRequest.h"
#include "public/platform/WebURLResponse.h"
#include "public/platform/WebUnitTestSupport.h"
#include "v8.h"
#include <gtest/gtest.h>
using namespace WebCore;
using namespace blink;
using blink::URLTestHelpers::toKURL;
namespace {
class TestPopupMenuClient : public PopupMenuClient {
public:
    
    TestPopupMenuClient() : m_selectIndex(0), m_node(0) { }
    virtual ~TestPopupMenuClient() {}
    virtual void valueChanged(unsigned listIndex, bool fireEvents = true)
    {
        m_selectIndex = listIndex;
        if (m_node) {
            HTMLSelectElement* select = toHTMLSelectElement(m_node);
            select->optionSelectedByUser(select->listToOptionIndex(listIndex), fireEvents);
        }
    }
    virtual void selectionChanged(unsigned, bool) {}
    virtual void selectionCleared() {}
    virtual String itemText(unsigned listIndex) const
    {
        String str("Item ");
        str.append(String::number(listIndex));
        return str;
    }
    virtual String itemLabel(unsigned) const { return String(); }
    virtual String itemIcon(unsigned) const { return String(); }
    virtual String itemToolTip(unsigned listIndex) const { return itemText(listIndex); }
    virtual String itemAccessibilityText(unsigned listIndex) const { return itemText(listIndex); }
    virtual bool itemIsEnabled(unsigned listIndex) const { return m_disabledIndexSet.find(listIndex) == m_disabledIndexSet.end(); }
    virtual PopupMenuStyle itemStyle(unsigned listIndex) const
    {
        FontDescription fontDescription;
        fontDescription.setComputedSize(12.0);
        Font font(fontDescription);
        font.update(nullptr);
        return PopupMenuStyle(Color::black, Color::white, font, true, false, Length(), TextDirection(), false );
    }
    virtual PopupMenuStyle menuStyle() const { return itemStyle(0); }
    virtual int clientInsetLeft() const { return 0; }
    virtual int clientInsetRight() const { return 0; }
    virtual LayoutUnit clientPaddingLeft() const { return 0; }
    virtual LayoutUnit clientPaddingRight() const { return 0; }
    virtual int listSize() const { return 10; }
    virtual int selectedIndex() const { return m_selectIndex; }
    virtual void popupDidHide() { }
    virtual bool itemIsSeparator(unsigned listIndex) const { return false; }
    virtual bool itemIsLabel(unsigned listIndex) const { return false; }
    virtual bool itemIsSelected(unsigned listIndex) const { return listIndex == m_selectIndex; }
    virtual bool valueShouldChangeOnHotTrack() const { return false; }
    virtual void setTextFromItem(unsigned listIndex) { }
    virtual FontSelector* fontSelector() const { return 0; }
    virtual HostWindow* hostWindow() const { return 0; }
    virtual PassRefPtr<Scrollbar> createScrollbar(ScrollableArea*, ScrollbarOrientation, ScrollbarControlSize) { return nullptr; }
    void setDisabledIndex(unsigned index) { m_disabledIndexSet.insert(index); }
    void setFocusedNode(Node* node) { m_node = node; }
private:
    unsigned m_selectIndex;
    std::set<unsigned> m_disabledIndexSet;
    Node* m_node;
};
class TestWebWidgetClient : public WebWidgetClient {
public:
    ~TestWebWidgetClient() { }
};
class TestWebPopupMenuImpl : public WebPopupMenuImpl {
public:
    static PassRefPtr<TestWebPopupMenuImpl> create(WebWidgetClient* client)
    {
        return adoptRef(new TestWebPopupMenuImpl(client));
    }
    ~TestWebPopupMenuImpl() { }
private:
    TestWebPopupMenuImpl(WebWidgetClient* client) : WebPopupMenuImpl(client) { }
};
class TestWebViewClient : public WebViewClient {
public:
    TestWebViewClient() : m_webPopupMenu(TestWebPopupMenuImpl::create(&m_webWidgetClient)) { }
    ~TestWebViewClient() { }
    virtual WebWidget* createPopupMenu(WebPopupType) { return m_webPopupMenu.get(); }
    
    
    virtual WebScreenInfo screenInfo()
    {
        WebScreenInfo screenInfo;
        screenInfo.availableRect.height = 2000;
        screenInfo.availableRect.width = 2000;
        return screenInfo;
    }
private:
    TestWebWidgetClient m_webWidgetClient;
    RefPtr<TestWebPopupMenuImpl> m_webPopupMenu;
};
class SelectPopupMenuTest : public testing::Test {
public:
    SelectPopupMenuTest()
        : baseURL("http://www.test.com/")
    {
    }
protected:
    virtual void SetUp()
    {
        m_helper.initialize(false, 0, &m_webviewClient);
        m_popupMenu = adoptRef(new PopupMenuChromium(*mainFrame()->frame(), &m_popupMenuClient));
    }
    virtual void TearDown()
    {
        m_popupMenu = nullptr;
        Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
    }
    
    bool popupOpen() const { return webView()->selectPopup(); }
    int selectedIndex() const { return m_popupMenuClient.selectedIndex(); }
    void showPopup()
    {
        m_popupMenu->show(FloatQuad(FloatRect(0, 0, 100, 100)), IntSize(100, 100), 0);
        ASSERT_TRUE(popupOpen());
    }
    void hidePopup()
    {
        m_popupMenu->hide();
        EXPECT_FALSE(popupOpen());
    }
    void simulateKeyDownEvent(int keyCode)
    {
        simulateKeyEvent(WebInputEvent::RawKeyDown, keyCode);
    }
    void simulateKeyUpEvent(int keyCode)
    {
        simulateKeyEvent(WebInputEvent::KeyUp, keyCode);
    }
    
    
    void simulateKeyEvent(WebInputEvent::Type eventType, int keyCode)
    {
        WebKeyboardEvent keyEvent;
        keyEvent.windowsKeyCode = keyCode;
        keyEvent.type = eventType;
        webView()->handleInputEvent(keyEvent);
    }
    
    void simulateLeftMouseDownEvent(const IntPoint& point)
    {
        PlatformMouseEvent mouseEvent(point, point, LeftButton, PlatformEvent::MousePressed,
                                      1, false, false, false, false, 0);
        webView()->selectPopup()->handleMouseDownEvent(mouseEvent);
    }
    void simulateLeftMouseUpEvent(const IntPoint& point)
    {
        PlatformMouseEvent mouseEvent(point, point, LeftButton, PlatformEvent::MouseReleased,
                                      1, false, false, false, false, 0);
        webView()->selectPopup()->handleMouseReleaseEvent(mouseEvent);
    }
    void registerMockedURLLoad(const std::string& fileName)
    {
        URLTestHelpers::registerMockedURLLoad(toKURL(baseURL + fileName), WebString::fromUTF8(fileName.c_str()), WebString::fromUTF8("popup/"), WebString::fromUTF8("text/html"));
    }
    void serveRequests()
    {
        Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
    }
    void loadFrame(WebFrame* frame, const std::string& fileName)
    {
        WebURLRequest urlRequest;
        urlRequest.initialize();
        urlRequest.setURL(WebURL(toKURL(baseURL + fileName)));
        frame->loadRequest(urlRequest);
    }
    WebViewImpl* webView() const { return m_helper.webViewImpl(); }
    WebFrameImpl* mainFrame() const { return m_helper.webViewImpl()->mainFrameImpl(); }
protected:
    TestWebViewClient m_webviewClient;
    TestPopupMenuClient m_popupMenuClient;
    RefPtr<PopupMenu> m_popupMenu;
    std::string baseURL;
private:
    FrameTestHelpers::WebViewHelper m_helper;
};
TEST_F(SelectPopupMenuTest, ShowThenHide)
{
    for (int i = 0; i < 3; i++) {
        showPopup();
        hidePopup();
    }
}
TEST_F(SelectPopupMenuTest, ShowThenDelete)
{
    showPopup();
    
}
TEST_F(SelectPopupMenuTest, ShowThenLoseFocus)
{
    showPopup();
    
    webView()->setFocus(false);
    
    EXPECT_FALSE(popupOpen());
}
TEST_F(SelectPopupMenuTest, ShowThenPressESC)
{
    showPopup();
    simulateKeyDownEvent(VKEY_ESCAPE);
    
    EXPECT_FALSE(popupOpen());
}
TEST_F(SelectPopupMenuTest, SelectWithKeys)
{
    showPopup();
    
    simulateKeyDownEvent(VKEY_DOWN);
    simulateKeyDownEvent(VKEY_DOWN);
    simulateKeyDownEvent(VKEY_RETURN);
    
    EXPECT_TRUE(!popupOpen());
    EXPECT_EQ(2, selectedIndex());
    
    showPopup();
    simulateKeyDownEvent(VKEY_DOWN);
    simulateKeyDownEvent(VKEY_ESCAPE);
    EXPECT_FALSE(popupOpen());
    EXPECT_EQ(3, selectedIndex());
    
    showPopup();
    simulateKeyDownEvent(VKEY_DOWN);
    simulateKeyDownEvent(VKEY_TAB);
    EXPECT_FALSE(popupOpen());
    EXPECT_EQ(4, selectedIndex());
}
TEST_F(SelectPopupMenuTest, ClickItem)
{
    showPopup();
    int menuItemHeight = webView()->selectPopup()->menuItemHeight();
    
    IntPoint row1Point(2, menuItemHeight * 1.5);
    
    simulateLeftMouseDownEvent(row1Point);
    simulateLeftMouseUpEvent(row1Point);
    
    EXPECT_FALSE(popupOpen());
    EXPECT_EQ(1, selectedIndex());
}
TEST_F(SelectPopupMenuTest, MouseOverItemClickOutside)
{
    showPopup();
    int menuItemHeight = webView()->selectPopup()->menuItemHeight();
    
    IntPoint row1Point(2, menuItemHeight * 1.5);
    
    PlatformMouseEvent mouseEvent(row1Point, row1Point, NoButton, PlatformEvent::MouseMoved,
                                  1, false, false, false, false, 0);
    webView()->selectPopup()->handleMouseMoveEvent(mouseEvent);
    
    simulateLeftMouseDownEvent(IntPoint(1000, 1000));
    
    EXPECT_FALSE(popupOpen());
    EXPECT_EQ(0, selectedIndex());
}
TEST_F(SelectPopupMenuTest, SelectItemWithKeyboardItemClickOutside)
{
    showPopup();
    
    simulateKeyDownEvent(VKEY_DOWN);
    simulateKeyDownEvent(VKEY_DOWN);
    
    simulateLeftMouseDownEvent(IntPoint(1000, 1000));
    
    EXPECT_FALSE(popupOpen());
    EXPECT_EQ(2, selectedIndex());
}
TEST_F(SelectPopupMenuTest, DISABLED_SelectItemEventFire)
{
    registerMockedURLLoad("select_event.html");
    webView()->settings()->setJavaScriptEnabled(true);
    loadFrame(mainFrame(), "select_event.html");
    serveRequests();
    m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedElement());
    showPopup();
    int menuItemHeight = webView()->selectPopup()->menuItemHeight();
    
    IntPoint row1Point(2, menuItemHeight * 0.5);
    simulateLeftMouseDownEvent(row1Point);
    simulateLeftMouseUpEvent(row1Point);
    WebElement element = webView()->mainFrame()->document().getElementById("message");
    
    
    EXPECT_STREQ("upclick", element.innerText().utf8().data());
    
    m_popupMenuClient.setDisabledIndex(1);
    showPopup();
    
    row1Point.setY(menuItemHeight * 1.5);
    simulateLeftMouseDownEvent(row1Point);
    simulateLeftMouseUpEvent(row1Point);
    
    EXPECT_STREQ("upclick", element.innerText().utf8().data());
    showPopup();
    
    row1Point.setY(menuItemHeight * 2.5);
    simulateLeftMouseDownEvent(row1Point);
    simulateLeftMouseUpEvent(row1Point);
    
    EXPECT_STREQ("upclickchangeupclick", element.innerText().utf8().data());
}
TEST_F(SelectPopupMenuTest, FLAKY_SelectItemKeyEvent)
{
    registerMockedURLLoad("select_event.html");
    webView()->settings()->setJavaScriptEnabled(true);
    loadFrame(mainFrame(), "select_event.html");
    serveRequests();
    m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedElement());
    showPopup();
    
    simulateKeyDownEvent(VKEY_DOWN);
    simulateKeyDownEvent(VKEY_DOWN);
    simulateKeyDownEvent(VKEY_RETURN);
    WebElement element = webView()->mainFrame()->document().getElementById("message");
    
    EXPECT_STREQ("change", element.innerText().utf8().data());
}
TEST_F(SelectPopupMenuTest, SelectItemRemoveSelectOnChange)
{
    
    registerMockedURLLoad("select_event_remove_on_change.html");
    webView()->settings()->setJavaScriptEnabled(true);
    loadFrame(mainFrame(), "select_event_remove_on_change.html");
    serveRequests();
    m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedElement());
    showPopup();
    int menuItemHeight = webView()->selectPopup()->menuItemHeight();
    
    IntPoint row1Point(2, menuItemHeight * 1.5);
    simulateLeftMouseDownEvent(row1Point);
    simulateLeftMouseUpEvent(row1Point);
    WebElement element = webView()->mainFrame()->document().getElementById("message");
    EXPECT_STREQ("change", element.innerText().utf8().data());
}
TEST_F(SelectPopupMenuTest, SelectItemRemoveSelectOnClick)
{
    
    registerMockedURLLoad("select_event_remove_on_click.html");
    webView()->settings()->setJavaScriptEnabled(true);
    loadFrame(mainFrame(), "select_event_remove_on_click.html");
    serveRequests();
    m_popupMenuClient.setFocusedNode(mainFrame()->frame()->document()->focusedElement());
    showPopup();
    int menuItemHeight = webView()->selectPopup()->menuItemHeight();
    
    IntPoint row1Point(2, menuItemHeight * 1.5);
    simulateLeftMouseDownEvent(row1Point);
    simulateLeftMouseUpEvent(row1Point);
    WebElement element = webView()->mainFrame()->document().getElementById("message");
    EXPECT_STREQ("click", element.innerText().utf8().data());
}
}