1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "web/ExternalPopupMenu.h"
#include "core/HTMLNames.h"
#include "core/frame/FrameHost.h"
#include "core/frame/PinchViewport.h"
#include "core/html/HTMLSelectElement.h"
#include "core/html/forms/PopupMenuClient.h"
#include "core/page/Page.h"
#include "core/rendering/RenderMenuList.h"
#include "core/testing/URLTestHelpers.h"
#include "platform/PopupMenu.h"
#include "public/platform/Platform.h"
#include "public/platform/WebUnitTestSupport.h"
#include "public/web/WebExternalPopupMenu.h"
#include "public/web/WebPopupMenuInfo.h"
#include "public/web/WebSettings.h"
#include "web/WebLocalFrameImpl.h"
#include "web/tests/FrameTestHelpers.h"
#include <gtest/gtest.h>
using namespace blink;
namespace {
const size_t kListSize = 7;
class TestPopupMenuClient : public PopupMenuClient {
public:
TestPopupMenuClient() : m_listSize(0) { }
virtual ~TestPopupMenuClient() { }
virtual void valueChanged(unsigned listIndex, bool fireEvents = true) override { }
virtual void selectionChanged(unsigned listIndex, bool fireEvents = true) override { }
virtual void selectionCleared() override { }
virtual String itemText(unsigned listIndex) const override { return emptyString(); }
virtual String itemToolTip(unsigned listIndex) const override { return emptyString(); }
virtual String itemAccessibilityText(unsigned listIndex) const override { return emptyString(); }
virtual bool itemIsEnabled(unsigned listIndex) const override { return true; }
virtual PopupMenuStyle itemStyle(unsigned listIndex) const override
{
FontDescription fontDescription;
fontDescription.setComputedSize(12.0);
Font font(fontDescription);
font.update(nullptr);
bool displayNone = m_displayNoneIndexSet.find(listIndex) != m_displayNoneIndexSet.end();
return PopupMenuStyle(Color::black, Color::white, font, true, displayNone, Length(), TextDirection(), false);
}
virtual PopupMenuStyle menuStyle() const override { return itemStyle(0); }
virtual LayoutUnit clientPaddingLeft() const override { return 0; }
virtual LayoutUnit clientPaddingRight() const override { return 0; }
virtual int listSize() const override { return m_listSize; }
virtual int selectedIndex() const override { return 0; }
virtual void popupDidHide() override { }
virtual bool itemIsSeparator(unsigned listIndex) const override { return false;}
virtual bool itemIsLabel(unsigned listIndex) const override { return false; }
virtual bool itemIsSelected(unsigned listIndex) const override { return listIndex == 0;}
virtual void setTextFromItem(unsigned listIndex) override { }
virtual bool multiple() const override { return false; }
void setListSize(size_t size) { m_listSize = size; }
void setDisplayNoneIndex(unsigned index) { m_displayNoneIndexSet.insert(index); }
private:
size_t m_listSize;
std::set<unsigned> m_displayNoneIndexSet;
};
class ExternalPopupMenuDisplayNoneItemsTest : public testing::Test {
public:
ExternalPopupMenuDisplayNoneItemsTest() { }
protected:
virtual void SetUp() override
{
m_popupMenuClient.setListSize(kListSize);
// Set the 4th an 5th items to have "display: none" property
m_popupMenuClient.setDisplayNoneIndex(3);
m_popupMenuClient.setDisplayNoneIndex(4);
}
TestPopupMenuClient m_popupMenuClient;
};
TEST_F(ExternalPopupMenuDisplayNoneItemsTest, PopupMenuInfoSizeTest)
{
WebPopupMenuInfo info;
ExternalPopupMenu::getPopupMenuInfo(info, m_popupMenuClient);
EXPECT_EQ(5U, info.items.size());
}
TEST_F(ExternalPopupMenuDisplayNoneItemsTest, IndexMappingTest)
{
// 6th indexed item in popupmenu would be the 4th item in ExternalPopupMenu,
// and vice-versa.
EXPECT_EQ(4, ExternalPopupMenu::toExternalPopupMenuItemIndex(6, m_popupMenuClient));
EXPECT_EQ(6, ExternalPopupMenu::toPopupMenuItemIndex(4, m_popupMenuClient));
// Invalid index, methods should return -1.
EXPECT_EQ(-1, ExternalPopupMenu::toExternalPopupMenuItemIndex(8, m_popupMenuClient));
EXPECT_EQ(-1, ExternalPopupMenu::toPopupMenuItemIndex(8, m_popupMenuClient));
}
class ExternalPopupMenuWebFrameClient : public FrameTestHelpers::TestWebFrameClient {
public:
virtual WebExternalPopupMenu* createExternalPopupMenu(const WebPopupMenuInfo&, WebExternalPopupMenuClient*) override
{
return &m_mockWebExternalPopupMenu;
}
WebRect shownBounds() const
{
return m_mockWebExternalPopupMenu.shownBounds();
}
private:
class MockWebExternalPopupMenu : public WebExternalPopupMenu {
virtual void show(const WebRect& bounds) override
{
m_shownBounds = bounds;
}
virtual void close() override { }
public:
WebRect shownBounds() const
{
return m_shownBounds;
}
private:
WebRect m_shownBounds;
};
WebRect m_shownBounds;
MockWebExternalPopupMenu m_mockWebExternalPopupMenu;
};
class ExternalPopupMenuTest : public testing::Test {
public:
ExternalPopupMenuTest() : m_baseURL("http://www.test.com") { }
protected:
virtual void SetUp() override
{
m_helper.initialize(false, &m_webFrameClient, &m_webViewClient, &configureSettings);
webView()->setUseExternalPopupMenus(true);
}
virtual void TearDown() override
{
Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
}
void registerMockedURLLoad(const std::string& fileName)
{
URLTestHelpers::registerMockedURLLoad(URLTestHelpers::toKURL(m_baseURL + fileName), WebString::fromUTF8(fileName.c_str()), WebString::fromUTF8("popup/"), WebString::fromUTF8("text/html"));
}
void loadFrame(const std::string& fileName)
{
FrameTestHelpers::loadFrame(mainFrame(), m_baseURL + fileName);
}
WebViewImpl* webView() const { return m_helper.webViewImpl(); }
const ExternalPopupMenuWebFrameClient& client() const { return m_webFrameClient; }
WebLocalFrameImpl* mainFrame() const { return m_helper.webViewImpl()->mainFrameImpl(); }
private:
static void configureSettings(WebSettings* settings)
{
settings->setPinchVirtualViewportEnabled(true);
}
std::string m_baseURL;
FrameTestHelpers::TestWebViewClient m_webViewClient;
ExternalPopupMenuWebFrameClient m_webFrameClient;
FrameTestHelpers::WebViewHelper m_helper;
};
TEST_F(ExternalPopupMenuTest, PopupAccountsForPinchViewportOffset)
{
registerMockedURLLoad("select_mid_screen.html");
loadFrame("select_mid_screen.html");
webView()->resize(WebSize(100, 100));
webView()->layout();
HTMLSelectElement* select = toHTMLSelectElement(mainFrame()->frame()->document()->getElementById("select"));
RenderMenuList* menuList = toRenderMenuList(select->renderer());
ASSERT_TRUE(menuList);
PinchViewport& pinchViewport = webView()->page()->frameHost().pinchViewport();
IntRect rectInDocument = menuList->absoluteBoundingBoxRect();
webView()->setPageScaleFactor(2);
IntPoint scrollDelta(20, 30);
pinchViewport.move(scrollDelta);
menuList->showPopup();
EXPECT_EQ(rectInDocument.x() - scrollDelta.x(), client().shownBounds().x);
EXPECT_EQ(rectInDocument.y() - scrollDelta.y(), client().shownBounds().y);
}
TEST_F(ExternalPopupMenuTest, DidAcceptIndex)
{
registerMockedURLLoad("select.html");
loadFrame("select.html");
HTMLSelectElement* select = toHTMLSelectElement(mainFrame()->frame()->document()->getElementById("select"));
RenderMenuList* menuList = toRenderMenuList(select->renderer());
ASSERT_TRUE(menuList);
menuList->showPopup();
ASSERT_TRUE(menuList->popupIsVisible());
WebExternalPopupMenuClient* client = static_cast<ExternalPopupMenu*>(menuList->popup());
client->didAcceptIndex(2);
EXPECT_FALSE(menuList->popupIsVisible());
ASSERT_STREQ("2", menuList->text().utf8().data());
EXPECT_EQ(2, select->selectedIndex());
}
TEST_F(ExternalPopupMenuTest, DidAcceptIndices)
{
registerMockedURLLoad("select.html");
loadFrame("select.html");
HTMLSelectElement* select = toHTMLSelectElement(mainFrame()->frame()->document()->getElementById("select"));
RenderMenuList* menuList = toRenderMenuList(select->renderer());
ASSERT_TRUE(menuList);
menuList->showPopup();
ASSERT_TRUE(menuList->popupIsVisible());
WebExternalPopupMenuClient* client = static_cast<ExternalPopupMenu*>(menuList->popup());
int indices[] = { 2 };
WebVector<int> indicesVector(indices, 1);
client->didAcceptIndices(indicesVector);
EXPECT_FALSE(menuList->popupIsVisible());
EXPECT_STREQ("2", menuList->text().utf8().data());
EXPECT_EQ(2, select->selectedIndex());
}
TEST_F(ExternalPopupMenuTest, DidAcceptIndicesClearSelect)
{
registerMockedURLLoad("select.html");
loadFrame("select.html");
HTMLSelectElement* select = toHTMLSelectElement(mainFrame()->frame()->document()->getElementById("select"));
RenderMenuList* menuList = toRenderMenuList(select->renderer());
ASSERT_TRUE(menuList);
menuList->showPopup();
ASSERT_TRUE(menuList->popupIsVisible());
WebExternalPopupMenuClient* client = static_cast<ExternalPopupMenu*>(menuList->popup());
WebVector<int> indices;
client->didAcceptIndices(indices);
EXPECT_FALSE(menuList->popupIsVisible());
EXPECT_EQ(-1, select->selectedIndex());
}
} // namespace
|