[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 5 | #include <tuple> |
| 6 | |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 7 | #include "base/macros.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 9 | #include "content/common/frame_messages.h" |
[email protected] | 899617f | 2012-06-04 02:27:14 | [diff] [blame] | 10 | #include "content/public/test/render_view_test.h" |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 11 | #include "content/renderer/render_frame_impl.h" |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 12 | #include "content/renderer/render_view_impl.h" |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 14 | #include "third_party/blink/public/platform/web_size.h" |
Kent Tamura | 5405341d5 | 2018-12-20 00:22:07 | [diff] [blame] | 15 | #include "third_party/blink/public/web/web_local_frame.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 16 | #include "third_party/blink/public/web/web_view.h" |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 17 | |
| 18 | // Tests for the external select popup menu (Mac specific). |
| 19 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 20 | namespace content { |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | const char* const kSelectID = "mySelect"; |
[email protected] | 92ccbdd | 2010-12-01 19:30:44 | [diff] [blame] | 24 | const char* const kEmptySelectID = "myEmptySelect"; |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 25 | |
| 26 | } // namespace |
| 27 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 28 | class ExternalPopupMenuTest : public RenderViewTest { |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 29 | public: |
| 30 | ExternalPopupMenuTest() {} |
| 31 | |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 32 | RenderViewImpl* view() { |
| 33 | return static_cast<RenderViewImpl*>(view_); |
| 34 | } |
| 35 | |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 36 | RenderFrameImpl* frame() { |
mostynb | c3335323 | 2014-09-12 09:38:31 | [diff] [blame] | 37 | return view()->GetMainRenderFrame(); |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 38 | } |
| 39 | |
dcheng | 6013533 | 2015-01-09 02:05:34 | [diff] [blame] | 40 | void SetUp() override { |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 41 | RenderViewTest::SetUp(); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 42 | // We need to set this explictly as RenderMain is not run. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 43 | blink::WebView::SetUseExternalPopupMenus(true); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 44 | |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 45 | std::string html = "<select id='mySelect' onchange='selectChanged(this)'>" |
| 46 | " <option>zero</option>" |
| 47 | " <option selected='1'>one</option>" |
| 48 | " <option>two</option>" |
[email protected] | 92ccbdd | 2010-12-01 19:30:44 | [diff] [blame] | 49 | "</select>" |
| 50 | "<select id='myEmptySelect'>" |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 51 | "</select>"; |
| 52 | if (ShouldRemoveSelectOnChange()) { |
| 53 | html += "<script>" |
| 54 | " function selectChanged(select) {" |
| 55 | " select.parentNode.removeChild(select);" |
| 56 | " }" |
| 57 | "</script>"; |
| 58 | } |
| 59 | |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 60 | // Load the test page. |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 61 | LoadHTML(html.c_str()); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 62 | |
| 63 | // Set a minimum size and give focus so simulated events work. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 64 | view()->GetWidget()->GetWebWidget()->Resize(blink::WebSize(500, 500)); |
| 65 | view()->GetWidget()->GetWebWidget()->SetFocus(true); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int GetSelectedIndex() { |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 69 | base::string16 script(base::ASCIIToUTF16(kSelectID)); |
| 70 | script.append(base::ASCIIToUTF16(".selectedIndex")); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 71 | int selected_index = -1; |
| 72 | ExecuteJavaScriptAndReturnIntValue(script, &selected_index); |
| 73 | return selected_index; |
| 74 | } |
| 75 | |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 76 | protected: |
| 77 | virtual bool ShouldRemoveSelectOnChange() const { return false; } |
| 78 | |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(ExternalPopupMenuTest); |
| 80 | }; |
| 81 | |
| 82 | // Normal case: test showing a select popup, canceling/selecting an item. |
| 83 | TEST_F(ExternalPopupMenuTest, NormalCase) { |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 84 | IPC::TestSink& sink = render_thread_->sink(); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 85 | |
| 86 | // Click the text field once. |
| 87 | EXPECT_TRUE(SimulateElementClick(kSelectID)); |
| 88 | |
| 89 | // We should have sent a message to the browser to show the popup menu. |
| 90 | const IPC::Message* message = |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 91 | sink.GetUniqueMessageMatching(FrameHostMsg_ShowPopup::ID); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 92 | ASSERT_TRUE(message != NULL); |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 93 | std::tuple<FrameHostMsg_ShowPopup_Params> param; |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 94 | FrameHostMsg_ShowPopup::Read(message, ¶m); |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 95 | ASSERT_EQ(3U, std::get<0>(param).popup_items.size()); |
| 96 | EXPECT_EQ(1, std::get<0>(param).selected_item); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 97 | |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 98 | // Simulate the user canceling the popup; the index should not have changed. |
| 99 | frame()->OnSelectPopupMenuItem(-1); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 100 | EXPECT_EQ(1, GetSelectedIndex()); |
| 101 | |
| 102 | // Show the pop-up again and this time make a selection. |
| 103 | EXPECT_TRUE(SimulateElementClick(kSelectID)); |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 104 | frame()->OnSelectPopupMenuItem(0); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 105 | EXPECT_EQ(0, GetSelectedIndex()); |
| 106 | |
| 107 | // Show the pop-up again and make another selection. |
| 108 | sink.ClearMessages(); |
| 109 | EXPECT_TRUE(SimulateElementClick(kSelectID)); |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 110 | message = sink.GetUniqueMessageMatching(FrameHostMsg_ShowPopup::ID); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 111 | ASSERT_TRUE(message != NULL); |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 112 | FrameHostMsg_ShowPopup::Read(message, ¶m); |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 113 | ASSERT_EQ(3U, std::get<0>(param).popup_items.size()); |
| 114 | EXPECT_EQ(0, std::get<0>(param).selected_item); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Page shows popup, then navigates away while popup showing, then select. |
| 118 | TEST_F(ExternalPopupMenuTest, ShowPopupThenNavigate) { |
| 119 | // Click the text field once. |
| 120 | EXPECT_TRUE(SimulateElementClick(kSelectID)); |
| 121 | |
| 122 | // Now we navigate to another pager. |
| 123 | LoadHTML("<blink>Awesome page!</blink>"); |
| 124 | |
| 125 | // Now the user selects something, we should not crash. |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 126 | frame()->OnSelectPopupMenuItem(-1); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 127 | } |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 128 | |
[email protected] | 92ccbdd | 2010-12-01 19:30:44 | [diff] [blame] | 129 | // An empty select should not cause a crash when clicked. |
| 130 | // http://crbug.com/63774 |
| 131 | TEST_F(ExternalPopupMenuTest, EmptySelect) { |
| 132 | EXPECT_TRUE(SimulateElementClick(kEmptySelectID)); |
| 133 | } |
| 134 | |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 135 | class ExternalPopupMenuRemoveTest : public ExternalPopupMenuTest { |
| 136 | public: |
| 137 | ExternalPopupMenuRemoveTest() {} |
| 138 | |
| 139 | protected: |
dcheng | 6d18e40 | 2014-10-21 12:32:52 | [diff] [blame] | 140 | bool ShouldRemoveSelectOnChange() const override { return true; } |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | // Tests that nothing bad happen when the page removes the select when it |
| 144 | // changes. (http://crbug.com/61997) |
tkent | 4e91ab8 | 2015-08-05 22:36:46 | [diff] [blame] | 145 | TEST_F(ExternalPopupMenuRemoveTest, RemoveOnChange) { |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 146 | // Click the text field once to show the popup. |
| 147 | EXPECT_TRUE(SimulateElementClick(kSelectID)); |
| 148 | |
| 149 | // Select something, it causes the select to be removed from the page. |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 150 | frame()->OnSelectPopupMenuItem(0); |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 151 | |
| 152 | // Just to check the soundness of the test, call SimulateElementClick again. |
| 153 | // It should return false as the select has been removed. |
| 154 | EXPECT_FALSE(SimulateElementClick(kSelectID)); |
| 155 | } |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 156 | |
Kent Tamura | 5405341d5 | 2018-12-20 00:22:07 | [diff] [blame] | 157 | // crbug.com/912211 |
| 158 | TEST_F(ExternalPopupMenuRemoveTest, RemoveFrameOnChange) { |
| 159 | LoadHTML( |
| 160 | "<style>* { margin: 0; } iframe { border: 0; }</style>" |
| 161 | "<body><iframe srcdoc=\"" |
| 162 | "<style>* { margin: 0; }</style><select><option>opt1<option>opt2" |
| 163 | "\"></iframe>" |
| 164 | "<script>" |
| 165 | "onload = function() {" |
| 166 | " const frame = document.querySelector('iframe');" |
| 167 | " frame.contentDocument.querySelector('select').onchange = " |
| 168 | " () => { frame.remove(); };" |
| 169 | "};" |
| 170 | "</script>"); |
| 171 | // Open a popup. |
| 172 | SimulatePointClick(gfx::Point(8, 8)); |
| 173 | // Select something on the sub-frame, it causes the frame to be removed from |
| 174 | // the page. |
| 175 | auto* child_web_frame = |
| 176 | static_cast<blink::WebLocalFrame*>(frame()->GetWebFrame()->FirstChild()); |
| 177 | static_cast<RenderFrameImpl*>(RenderFrame::FromWebFrame(child_web_frame)) |
| 178 | ->OnSelectPopupMenuItem(1); |
| 179 | // The test passes if the test didn't crash and ASAN didn't complain. |
| 180 | } |
| 181 | |
[email protected] | d0cd6748 | 2014-08-12 19:17:12 | [diff] [blame] | 182 | class ExternalPopupMenuDisplayNoneTest : public ExternalPopupMenuTest { |
| 183 | public: |
| 184 | ExternalPopupMenuDisplayNoneTest() {} |
| 185 | |
dcheng | 6013533 | 2015-01-09 02:05:34 | [diff] [blame] | 186 | void SetUp() override { |
[email protected] | d0cd6748 | 2014-08-12 19:17:12 | [diff] [blame] | 187 | RenderViewTest::SetUp(); |
| 188 | // We need to set this explictly as RenderMain is not run. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 189 | blink::WebView::SetUseExternalPopupMenus(true); |
[email protected] | d0cd6748 | 2014-08-12 19:17:12 | [diff] [blame] | 190 | |
| 191 | std::string html = "<select id='mySelect'>" |
| 192 | " <option value='zero'>zero</option>" |
| 193 | " <optgroup label='hide' style='display: none'>" |
| 194 | " <option value='one'>one</option>" |
| 195 | " </optgroup>" |
| 196 | " <option value='two'>two</option>" |
| 197 | " <option value='three'>three</option>" |
| 198 | " <option value='four'>four</option>" |
| 199 | " <option value='five'>five</option>" |
| 200 | "</select>"; |
| 201 | // Load the test page. |
| 202 | LoadHTML(html.c_str()); |
| 203 | |
| 204 | // Set a minimum size and give focus so simulated events work. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 205 | view()->GetWidget()->GetWebWidget()->Resize(blink::WebSize(500, 500)); |
| 206 | view()->GetWidget()->GetWebWidget()->SetFocus(true); |
[email protected] | d0cd6748 | 2014-08-12 19:17:12 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | }; |
| 210 | |
| 211 | TEST_F(ExternalPopupMenuDisplayNoneTest, SelectItem) { |
sudarshan.p | 9ff8ce3 | 2014-09-03 11:20:37 | [diff] [blame] | 212 | IPC::TestSink& sink = render_thread_->sink(); |
| 213 | |
[email protected] | d0cd6748 | 2014-08-12 19:17:12 | [diff] [blame] | 214 | // Click the text field once to show the popup. |
| 215 | EXPECT_TRUE(SimulateElementClick(kSelectID)); |
| 216 | |
sudarshan.p | 9ff8ce3 | 2014-09-03 11:20:37 | [diff] [blame] | 217 | // Read the message sent to browser to show the popup menu. |
| 218 | const IPC::Message* message = |
| 219 | sink.GetUniqueMessageMatching(FrameHostMsg_ShowPopup::ID); |
| 220 | ASSERT_TRUE(message != NULL); |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 221 | std::tuple<FrameHostMsg_ShowPopup_Params> param; |
sudarshan.p | 9ff8ce3 | 2014-09-03 11:20:37 | [diff] [blame] | 222 | FrameHostMsg_ShowPopup::Read(message, ¶m); |
| 223 | // Number of items should match item count minus the number |
| 224 | // of "display: none" items. |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 225 | ASSERT_EQ(5U, std::get<0>(param).popup_items.size()); |
sudarshan.p | 9ff8ce3 | 2014-09-03 11:20:37 | [diff] [blame] | 226 | |
[email protected] | d0cd6748 | 2014-08-12 19:17:12 | [diff] [blame] | 227 | // Select index 1 item. This should select item with index 2, |
| 228 | // skipping the item with 'display: none' |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 229 | frame()->OnSelectPopupMenuItem(1); |
[email protected] | d0cd6748 | 2014-08-12 19:17:12 | [diff] [blame] | 230 | |
avi | 485e5fd6 | 2014-08-25 23:26:14 | [diff] [blame] | 231 | EXPECT_EQ(2, GetSelectedIndex()); |
[email protected] | d0cd6748 | 2014-08-12 19:17:12 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 234 | } // namespace content |