OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
6 #include "content/common/view_messages.h" | 6 #include "content/common/frame_messages.h" |
7 #include "content/public/test/render_view_test.h" | 7 #include "content/public/test/render_view_test.h" |
| 8 #include "content/renderer/render_frame_impl.h" |
8 #include "content/renderer/render_view_impl.h" | 9 #include "content/renderer/render_view_impl.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "third_party/WebKit/public/web/WebView.h" | 11 #include "third_party/WebKit/public/web/WebView.h" |
11 #include "third_party/WebKit/public/platform/WebSize.h" | 12 #include "third_party/WebKit/public/platform/WebSize.h" |
12 | 13 |
13 // Tests for the external select popup menu (Mac specific). | 14 // Tests for the external select popup menu (Mac specific). |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 namespace { | 17 namespace { |
17 | 18 |
18 const char* const kSelectID = "mySelect"; | 19 const char* const kSelectID = "mySelect"; |
19 const char* const kEmptySelectID = "myEmptySelect"; | 20 const char* const kEmptySelectID = "myEmptySelect"; |
20 | 21 |
21 } // namespace | 22 } // namespace |
22 | 23 |
23 class ExternalPopupMenuTest : public RenderViewTest { | 24 class ExternalPopupMenuTest : public RenderViewTest { |
24 public: | 25 public: |
25 ExternalPopupMenuTest() {} | 26 ExternalPopupMenuTest() {} |
26 | 27 |
27 RenderViewImpl* view() { | 28 RenderViewImpl* view() { |
28 return static_cast<RenderViewImpl*>(view_); | 29 return static_cast<RenderViewImpl*>(view_); |
29 } | 30 } |
30 | 31 |
| 32 RenderFrameImpl* frame() { |
| 33 return view()->main_render_frame(); |
| 34 } |
| 35 |
31 virtual void SetUp() { | 36 virtual void SetUp() { |
32 RenderViewTest::SetUp(); | 37 RenderViewTest::SetUp(); |
33 // We need to set this explictly as RenderMain is not run. | 38 // We need to set this explictly as RenderMain is not run. |
34 blink::WebView::setUseExternalPopupMenus(true); | 39 blink::WebView::setUseExternalPopupMenus(true); |
35 | 40 |
36 std::string html = "<select id='mySelect' onchange='selectChanged(this)'>" | 41 std::string html = "<select id='mySelect' onchange='selectChanged(this)'>" |
37 " <option>zero</option>" | 42 " <option>zero</option>" |
38 " <option selected='1'>one</option>" | 43 " <option selected='1'>one</option>" |
39 " <option>two</option>" | 44 " <option>two</option>" |
40 "</select>" | 45 "</select>" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 77 |
73 // Normal case: test showing a select popup, canceling/selecting an item. | 78 // Normal case: test showing a select popup, canceling/selecting an item. |
74 TEST_F(ExternalPopupMenuTest, NormalCase) { | 79 TEST_F(ExternalPopupMenuTest, NormalCase) { |
75 IPC::TestSink& sink = render_thread_->sink(); | 80 IPC::TestSink& sink = render_thread_->sink(); |
76 | 81 |
77 // Click the text field once. | 82 // Click the text field once. |
78 EXPECT_TRUE(SimulateElementClick(kSelectID)); | 83 EXPECT_TRUE(SimulateElementClick(kSelectID)); |
79 | 84 |
80 // We should have sent a message to the browser to show the popup menu. | 85 // We should have sent a message to the browser to show the popup menu. |
81 const IPC::Message* message = | 86 const IPC::Message* message = |
82 sink.GetUniqueMessageMatching(ViewHostMsg_ShowPopup::ID); | 87 sink.GetUniqueMessageMatching(FrameHostMsg_ShowPopup::ID); |
83 ASSERT_TRUE(message != NULL); | 88 ASSERT_TRUE(message != NULL); |
84 Tuple1<ViewHostMsg_ShowPopup_Params> param; | 89 Tuple1<FrameHostMsg_ShowPopup_Params> param; |
85 ViewHostMsg_ShowPopup::Read(message, ¶m); | 90 FrameHostMsg_ShowPopup::Read(message, ¶m); |
86 ASSERT_EQ(3U, param.a.popup_items.size()); | 91 ASSERT_EQ(3U, param.a.popup_items.size()); |
87 EXPECT_EQ(1, param.a.selected_item); | 92 EXPECT_EQ(1, param.a.selected_item); |
88 | 93 |
89 // Simulate the user canceling the popup, the index should not have changed. | 94 // Simulate the user canceling the popup; the index should not have changed. |
90 view()->OnSelectPopupMenuItem(-1); | 95 frame()->OnSelectPopupMenuItem(-1); |
91 EXPECT_EQ(1, GetSelectedIndex()); | 96 EXPECT_EQ(1, GetSelectedIndex()); |
92 | 97 |
93 // Show the pop-up again and this time make a selection. | 98 // Show the pop-up again and this time make a selection. |
94 EXPECT_TRUE(SimulateElementClick(kSelectID)); | 99 EXPECT_TRUE(SimulateElementClick(kSelectID)); |
95 view()->OnSelectPopupMenuItem(0); | 100 frame()->OnSelectPopupMenuItem(0); |
96 EXPECT_EQ(0, GetSelectedIndex()); | 101 EXPECT_EQ(0, GetSelectedIndex()); |
97 | 102 |
98 // Show the pop-up again and make another selection. | 103 // Show the pop-up again and make another selection. |
99 sink.ClearMessages(); | 104 sink.ClearMessages(); |
100 EXPECT_TRUE(SimulateElementClick(kSelectID)); | 105 EXPECT_TRUE(SimulateElementClick(kSelectID)); |
101 message = sink.GetUniqueMessageMatching(ViewHostMsg_ShowPopup::ID); | 106 message = sink.GetUniqueMessageMatching(FrameHostMsg_ShowPopup::ID); |
102 ASSERT_TRUE(message != NULL); | 107 ASSERT_TRUE(message != NULL); |
103 ViewHostMsg_ShowPopup::Read(message, ¶m); | 108 FrameHostMsg_ShowPopup::Read(message, ¶m); |
104 ASSERT_EQ(3U, param.a.popup_items.size()); | 109 ASSERT_EQ(3U, param.a.popup_items.size()); |
105 EXPECT_EQ(0, param.a.selected_item); | 110 EXPECT_EQ(0, param.a.selected_item); |
106 } | 111 } |
107 | 112 |
108 // Page shows popup, then navigates away while popup showing, then select. | 113 // Page shows popup, then navigates away while popup showing, then select. |
109 TEST_F(ExternalPopupMenuTest, ShowPopupThenNavigate) { | 114 TEST_F(ExternalPopupMenuTest, ShowPopupThenNavigate) { |
110 // Click the text field once. | 115 // Click the text field once. |
111 EXPECT_TRUE(SimulateElementClick(kSelectID)); | 116 EXPECT_TRUE(SimulateElementClick(kSelectID)); |
112 | 117 |
113 // Now we navigate to another pager. | 118 // Now we navigate to another pager. |
114 LoadHTML("<blink>Awesome page!</blink>"); | 119 LoadHTML("<blink>Awesome page!</blink>"); |
115 | 120 |
116 // Now the user selects something, we should not crash. | 121 // Now the user selects something, we should not crash. |
117 view()->OnSelectPopupMenuItem(-1); | 122 frame()->OnSelectPopupMenuItem(-1); |
118 } | 123 } |
119 | 124 |
120 // An empty select should not cause a crash when clicked. | 125 // An empty select should not cause a crash when clicked. |
121 // http://crbug.com/63774 | 126 // http://crbug.com/63774 |
122 TEST_F(ExternalPopupMenuTest, EmptySelect) { | 127 TEST_F(ExternalPopupMenuTest, EmptySelect) { |
123 EXPECT_TRUE(SimulateElementClick(kEmptySelectID)); | 128 EXPECT_TRUE(SimulateElementClick(kEmptySelectID)); |
124 } | 129 } |
125 | 130 |
126 class ExternalPopupMenuRemoveTest : public ExternalPopupMenuTest { | 131 class ExternalPopupMenuRemoveTest : public ExternalPopupMenuTest { |
127 public: | 132 public: |
128 ExternalPopupMenuRemoveTest() {} | 133 ExternalPopupMenuRemoveTest() {} |
129 | 134 |
130 protected: | 135 protected: |
131 virtual bool ShouldRemoveSelectOnChange() const OVERRIDE { return true; } | 136 virtual bool ShouldRemoveSelectOnChange() const OVERRIDE { return true; } |
132 }; | 137 }; |
133 | 138 |
134 // Tests that nothing bad happen when the page removes the select when it | 139 // Tests that nothing bad happen when the page removes the select when it |
135 // changes. (http://crbug.com/61997) | 140 // changes. (http://crbug.com/61997) |
136 TEST_F(ExternalPopupMenuRemoveTest, RemoveOnChange) { | 141 TEST_F(ExternalPopupMenuRemoveTest, RemoveOnChange) { |
137 // Click the text field once to show the popup. | 142 // Click the text field once to show the popup. |
138 EXPECT_TRUE(SimulateElementClick(kSelectID)); | 143 EXPECT_TRUE(SimulateElementClick(kSelectID)); |
139 | 144 |
140 // Select something, it causes the select to be removed from the page. | 145 // Select something, it causes the select to be removed from the page. |
141 view()->OnSelectPopupMenuItem(0); | 146 frame()->OnSelectPopupMenuItem(0); |
142 | 147 |
143 // Just to check the soundness of the test, call SimulateElementClick again. | 148 // Just to check the soundness of the test, call SimulateElementClick again. |
144 // It should return false as the select has been removed. | 149 // It should return false as the select has been removed. |
145 EXPECT_FALSE(SimulateElementClick(kSelectID)); | 150 EXPECT_FALSE(SimulateElementClick(kSelectID)); |
146 } | 151 } |
147 | 152 |
148 class ExternalPopupMenuDisplayNoneTest : public ExternalPopupMenuTest { | 153 class ExternalPopupMenuDisplayNoneTest : public ExternalPopupMenuTest { |
149 public: | 154 public: |
150 ExternalPopupMenuDisplayNoneTest() {} | 155 ExternalPopupMenuDisplayNoneTest() {} |
151 | 156 |
(...skipping 21 matching lines...) Expand all Loading... |
173 } | 178 } |
174 | 179 |
175 }; | 180 }; |
176 | 181 |
177 TEST_F(ExternalPopupMenuDisplayNoneTest, SelectItem) { | 182 TEST_F(ExternalPopupMenuDisplayNoneTest, SelectItem) { |
178 // Click the text field once to show the popup. | 183 // Click the text field once to show the popup. |
179 EXPECT_TRUE(SimulateElementClick(kSelectID)); | 184 EXPECT_TRUE(SimulateElementClick(kSelectID)); |
180 | 185 |
181 // Select index 1 item. This should select item with index 2, | 186 // Select index 1 item. This should select item with index 2, |
182 // skipping the item with 'display: none' | 187 // skipping the item with 'display: none' |
183 view()->OnSelectPopupMenuItem(1); | 188 frame()->OnSelectPopupMenuItem(1); |
184 | 189 |
185 EXPECT_EQ(2,GetSelectedIndex()); | 190 EXPECT_EQ(2, GetSelectedIndex()); |
186 } | 191 } |
187 | 192 |
188 } // namespace content | 193 } // namespace content |
OLD | NEW |