[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 | |
| 5 | #include "base/utf_string_conversions.h" |
[email protected] | 5572215 | 2011-03-22 01:33:53 | [diff] [blame] | 6 | #include "content/common/view_messages.h" |
[email protected] | 899617f | 2012-06-04 02:27:14 | [diff] [blame^] | 7 | #include "content/public/test/render_view_test.h" |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 8 | #include "content/renderer/render_view_impl.h" |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e6e90dc | 2011-12-03 00:01:37 | [diff] [blame] | 10 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 11 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 12 | |
| 13 | // Tests for the external select popup menu (Mac specific). |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | const char* const kSelectID = "mySelect"; |
[email protected] | 92ccbdd | 2010-12-01 19:30:44 | [diff] [blame] | 18 | const char* const kEmptySelectID = "myEmptySelect"; |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 19 | |
| 20 | } // namespace |
| 21 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 22 | class ExternalPopupMenuTest : public content::RenderViewTest { |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 23 | public: |
| 24 | ExternalPopupMenuTest() {} |
| 25 | |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 26 | RenderViewImpl* view() { |
| 27 | return static_cast<RenderViewImpl*>(view_); |
| 28 | } |
| 29 | |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 30 | virtual void SetUp() { |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 31 | content::RenderViewTest::SetUp(); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 32 | // We need to set this explictly as RenderMain is not run. |
| 33 | WebKit::WebView::setUseExternalPopupMenus(true); |
| 34 | |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 35 | 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] | 92ccbdd | 2010-12-01 19:30:44 | [diff] [blame] | 39 | "</select>" |
| 40 | "<select id='myEmptySelect'>" |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 41 | "</select>"; |
| 42 | if (ShouldRemoveSelectOnChange()) { |
| 43 | html += "<script>" |
| 44 | " function selectChanged(select) {" |
| 45 | " select.parentNode.removeChild(select);" |
| 46 | " }" |
| 47 | "</script>"; |
| 48 | } |
| 49 | |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 50 | // Load the test page. |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 51 | LoadHTML(html.c_str()); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 52 | |
| 53 | // Set a minimum size and give focus so simulated events work. |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 54 | view()->webwidget()->resize(WebKit::WebSize(500, 500)); |
| 55 | view()->webwidget()->setFocus(true); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 56 | } |
| 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] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 66 | protected: |
| 67 | virtual bool ShouldRemoveSelectOnChange() const { return false; } |
| 68 | |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 69 | DISALLOW_COPY_AND_ASSIGN(ExternalPopupMenuTest); |
| 70 | }; |
| 71 | |
| 72 | // Normal case: test showing a select popup, canceling/selecting an item. |
| 73 | TEST_F(ExternalPopupMenuTest, NormalCase) { |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 74 | IPC::TestSink& sink = render_thread_->sink(); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 75 | |
| 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, ¶m); |
| 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] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 89 | view()->OnSelectPopupMenuItem(-1); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 90 | EXPECT_EQ(1, GetSelectedIndex()); |
| 91 | |
| 92 | // Show the pop-up again and this time make a selection. |
| 93 | EXPECT_TRUE(SimulateElementClick(kSelectID)); |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 94 | view()->OnSelectPopupMenuItem(0); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 95 | 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, ¶m); |
| 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. |
| 108 | TEST_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] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 116 | view()->OnSelectPopupMenuItem(-1); |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 117 | } |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 118 | |
[email protected] | 92ccbdd | 2010-12-01 19:30:44 | [diff] [blame] | 119 | // An empty select should not cause a crash when clicked. |
| 120 | // http://crbug.com/63774 |
| 121 | TEST_F(ExternalPopupMenuTest, EmptySelect) { |
| 122 | EXPECT_TRUE(SimulateElementClick(kEmptySelectID)); |
| 123 | } |
| 124 | |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 125 | class 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) |
| 135 | TEST_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] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 140 | view()->OnSelectPopupMenuItem(0); |
[email protected] | 360bc0b1 | 2010-11-11 19:21:26 | [diff] [blame] | 141 | |
| 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 | } |