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