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