blob: 296d39d374835c803b0a03343579a4179196557a [file] [log] [blame]
[email protected]c6d068ff2011-10-14 17:28:231// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]caf706f2010-10-26 17:54:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/utf_string_conversions.h"
[email protected]55722152011-03-22 01:33:536#include "content/common/view_messages.h"
[email protected]899617f2012-06-04 02:27:147#include "content/public/test/render_view_test.h"
[email protected]068970d2011-10-11 00:05:168#include "content/renderer/render_view_impl.h"
[email protected]caf706f2010-10-26 17:54:089#include "testing/gtest/include/gtest/gtest.h"
[email protected]e6e90dc2011-12-03 00:01:3710#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
[email protected]8bd0fe62011-01-17 06:44:3711#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]caf706f2010-10-26 17:54:0812
13// Tests for the external select popup menu (Mac specific).
14
15namespace {
16
17const char* const kSelectID = "mySelect";
[email protected]92ccbdd2010-12-01 19:30:4418const char* const kEmptySelectID = "myEmptySelect";
[email protected]caf706f2010-10-26 17:54:0819
20} // namespace
21
[email protected]c6d068ff2011-10-14 17:28:2322class ExternalPopupMenuTest : public content::RenderViewTest {
[email protected]caf706f2010-10-26 17:54:0823 public:
24 ExternalPopupMenuTest() {}
25
[email protected]068970d2011-10-11 00:05:1626 RenderViewImpl* view() {
27 return static_cast<RenderViewImpl*>(view_);
28 }
29
[email protected]caf706f2010-10-26 17:54:0830 virtual void SetUp() {
[email protected]c6d068ff2011-10-14 17:28:2331 content::RenderViewTest::SetUp();
[email protected]caf706f2010-10-26 17:54:0832 // We need to set this explictly as RenderMain is not run.
33 WebKit::WebView::setUseExternalPopupMenus(true);
34
[email protected]360bc0b12010-11-11 19:21:2635 std::string html = "<select id='mySelect' onchange='selectChanged(this)'>"
36 " <option>zero</option>"
37 " <option selected='1'>one</option>"
38 " <option>two</option>"
[email protected]92ccbdd2010-12-01 19:30:4439 "</select>"
40 "<select id='myEmptySelect'>"
[email protected]360bc0b12010-11-11 19:21:2641 "</select>";
42 if (ShouldRemoveSelectOnChange()) {
43 html += "<script>"
44 " function selectChanged(select) {"
45 " select.parentNode.removeChild(select);"
46 " }"
47 "</script>";
48 }
49
[email protected]caf706f2010-10-26 17:54:0850 // Load the test page.
[email protected]360bc0b12010-11-11 19:21:2651 LoadHTML(html.c_str());
[email protected]caf706f2010-10-26 17:54:0852
53 // Set a minimum size and give focus so simulated events work.
[email protected]068970d2011-10-11 00:05:1654 view()->webwidget()->resize(WebKit::WebSize(500, 500));
55 view()->webwidget()->setFocus(true);
[email protected]caf706f2010-10-26 17:54:0856 }
57
58 int GetSelectedIndex() {
59 string16 script(ASCIIToUTF16(kSelectID));
60 script.append(ASCIIToUTF16(".selectedIndex"));
61 int selected_index = -1;
62 ExecuteJavaScriptAndReturnIntValue(script, &selected_index);
63 return selected_index;
64 }
65
[email protected]360bc0b12010-11-11 19:21:2666 protected:
67 virtual bool ShouldRemoveSelectOnChange() const { return false; }
68
[email protected]caf706f2010-10-26 17:54:0869 DISALLOW_COPY_AND_ASSIGN(ExternalPopupMenuTest);
70};
71
72// Normal case: test showing a select popup, canceling/selecting an item.
73TEST_F(ExternalPopupMenuTest, NormalCase) {
[email protected]c6d068ff2011-10-14 17:28:2374 IPC::TestSink& sink = render_thread_->sink();
[email protected]caf706f2010-10-26 17:54:0875
76 // Click the text field once.
77 EXPECT_TRUE(SimulateElementClick(kSelectID));
78
79 // We should have sent a message to the browser to show the popup menu.
80 const IPC::Message* message =
81 sink.GetUniqueMessageMatching(ViewHostMsg_ShowPopup::ID);
82 ASSERT_TRUE(message != NULL);
83 Tuple1<ViewHostMsg_ShowPopup_Params> param;
84 ViewHostMsg_ShowPopup::Read(message, &param);
85 ASSERT_EQ(3U, param.a.popup_items.size());
86 EXPECT_EQ(1, param.a.selected_item);
87
88 // Simulate the user canceling the popup, the index should not have changed.
[email protected]068970d2011-10-11 00:05:1689 view()->OnSelectPopupMenuItem(-1);
[email protected]caf706f2010-10-26 17:54:0890 EXPECT_EQ(1, GetSelectedIndex());
91
92 // Show the pop-up again and this time make a selection.
93 EXPECT_TRUE(SimulateElementClick(kSelectID));
[email protected]068970d2011-10-11 00:05:1694 view()->OnSelectPopupMenuItem(0);
[email protected]caf706f2010-10-26 17:54:0895 EXPECT_EQ(0, GetSelectedIndex());
96
97 // Show the pop-up again and make another selection.
98 sink.ClearMessages();
99 EXPECT_TRUE(SimulateElementClick(kSelectID));
100 message = sink.GetUniqueMessageMatching(ViewHostMsg_ShowPopup::ID);
101 ASSERT_TRUE(message != NULL);
102 ViewHostMsg_ShowPopup::Read(message, &param);
103 ASSERT_EQ(3U, param.a.popup_items.size());
104 EXPECT_EQ(0, param.a.selected_item);
105}
106
107// Page shows popup, then navigates away while popup showing, then select.
108TEST_F(ExternalPopupMenuTest, ShowPopupThenNavigate) {
109 // Click the text field once.
110 EXPECT_TRUE(SimulateElementClick(kSelectID));
111
112 // Now we navigate to another pager.
113 LoadHTML("<blink>Awesome page!</blink>");
114
115 // Now the user selects something, we should not crash.
[email protected]068970d2011-10-11 00:05:16116 view()->OnSelectPopupMenuItem(-1);
[email protected]caf706f2010-10-26 17:54:08117}
[email protected]360bc0b12010-11-11 19:21:26118
[email protected]92ccbdd2010-12-01 19:30:44119// An empty select should not cause a crash when clicked.
120// http://crbug.com/63774
121TEST_F(ExternalPopupMenuTest, EmptySelect) {
122 EXPECT_TRUE(SimulateElementClick(kEmptySelectID));
123}
124
[email protected]360bc0b12010-11-11 19:21:26125class ExternalPopupMenuRemoveTest : public ExternalPopupMenuTest {
126 public:
127 ExternalPopupMenuRemoveTest() {}
128
129 protected:
130 virtual bool ShouldRemoveSelectOnChange() const { return true; }
131};
132
133// Tests that nothing bad happen when the page removes the select when it
134// changes. (http://crbug.com/61997)
135TEST_F(ExternalPopupMenuRemoveTest, RemoveOnChange) {
136 // Click the text field once to show the popup.
137 EXPECT_TRUE(SimulateElementClick(kSelectID));
138
139 // Select something, it causes the select to be removed from the page.
[email protected]068970d2011-10-11 00:05:16140 view()->OnSelectPopupMenuItem(0);
[email protected]360bc0b12010-11-11 19:21:26141
142 // Just to check the soundness of the test, call SimulateElementClick again.
143 // It should return false as the select has been removed.
144 EXPECT_FALSE(SimulateElementClick(kSelectID));
145}