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