This source file includes following definitions.
- view
- SetUp
- GetSelectedIndex
- ShouldRemoveSelectOnChange
- TEST_F
- TEST_F
- TEST_F
- ShouldRemoveSelectOnChange
- TEST_F
#include "base/strings/utf_string_conversions.h"
#include "content/common/view_messages.h"
#include "content/public/test/render_view_test.h"
#include "content/renderer/render_view_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "third_party/WebKit/public/platform/WebSize.h"
namespace content {
namespace {
const char* const kSelectID = "mySelect";
const char* const kEmptySelectID = "myEmptySelect";
}
class ExternalPopupMenuTest : public RenderViewTest {
public:
ExternalPopupMenuTest() {}
RenderViewImpl* view() {
return static_cast<RenderViewImpl*>(view_);
}
virtual void SetUp() {
RenderViewTest::SetUp();
blink::WebView::setUseExternalPopupMenus(true);
std::string html = "<select id='mySelect' onchange='selectChanged(this)'>"
" <option>zero</option>"
" <option selected='1'>one</option>"
" <option>two</option>"
"</select>"
"<select id='myEmptySelect'>"
"</select>";
if (ShouldRemoveSelectOnChange()) {
html += "<script>"
" function selectChanged(select) {"
" select.parentNode.removeChild(select);"
" }"
"</script>";
}
LoadHTML(html.c_str());
view()->webwidget()->resize(blink::WebSize(500, 500));
view()->webwidget()->setFocus(true);
}
int GetSelectedIndex() {
base::string16 script(base::ASCIIToUTF16(kSelectID));
script.append(base::ASCIIToUTF16(".selectedIndex"));
int selected_index = -1;
ExecuteJavaScriptAndReturnIntValue(script, &selected_index);
return selected_index;
}
protected:
virtual bool ShouldRemoveSelectOnChange() const { return false; }
DISALLOW_COPY_AND_ASSIGN(ExternalPopupMenuTest);
};
TEST_F(ExternalPopupMenuTest, NormalCase) {
IPC::TestSink& sink = render_thread_->sink();
EXPECT_TRUE(SimulateElementClick(kSelectID));
const IPC::Message* message =
sink.GetUniqueMessageMatching(ViewHostMsg_ShowPopup::ID);
ASSERT_TRUE(message != NULL);
Tuple1<ViewHostMsg_ShowPopup_Params> param;
ViewHostMsg_ShowPopup::Read(message, ¶m);
ASSERT_EQ(3U, param.a.popup_items.size());
EXPECT_EQ(1, param.a.selected_item);
view()->OnSelectPopupMenuItem(-1);
EXPECT_EQ(1, GetSelectedIndex());
EXPECT_TRUE(SimulateElementClick(kSelectID));
view()->OnSelectPopupMenuItem(0);
EXPECT_EQ(0, GetSelectedIndex());
sink.ClearMessages();
EXPECT_TRUE(SimulateElementClick(kSelectID));
message = sink.GetUniqueMessageMatching(ViewHostMsg_ShowPopup::ID);
ASSERT_TRUE(message != NULL);
ViewHostMsg_ShowPopup::Read(message, ¶m);
ASSERT_EQ(3U, param.a.popup_items.size());
EXPECT_EQ(0, param.a.selected_item);
}
TEST_F(ExternalPopupMenuTest, ShowPopupThenNavigate) {
EXPECT_TRUE(SimulateElementClick(kSelectID));
LoadHTML("<blink>Awesome page!</blink>");
view()->OnSelectPopupMenuItem(-1);
}
TEST_F(ExternalPopupMenuTest, EmptySelect) {
EXPECT_TRUE(SimulateElementClick(kEmptySelectID));
}
class ExternalPopupMenuRemoveTest : public ExternalPopupMenuTest {
public:
ExternalPopupMenuRemoveTest() {}
protected:
virtual bool ShouldRemoveSelectOnChange() const OVERRIDE { return true; }
};
TEST_F(ExternalPopupMenuRemoveTest, RemoveOnChange) {
EXPECT_TRUE(SimulateElementClick(kSelectID));
view()->OnSelectPopupMenuItem(0);
EXPECT_FALSE(SimulateElementClick(kSelectID));
}
}