OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ui/views/widget/root_view.h" | 5 #include "ui/views/widget/root_view.h" |
6 | 6 |
7 #include "ui/views/context_menu_controller.h" | 7 #include "ui/views/context_menu_controller.h" |
8 #include "ui/views/test/views_test_base.h" | 8 #include "ui/views/test/views_test_base.h" |
9 #include "ui/views/view_targeter.h" | 9 #include "ui/views/view_targeter.h" |
10 #include "ui/views/widget/root_view.h" | 10 #include "ui/views/widget/root_view.h" |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 }; | 172 }; |
173 | 173 |
174 // Tests that context menus are shown for long press by the post-target handler | 174 // Tests that context menus are shown for long press by the post-target handler |
175 // installed on the RootView only if the event is targetted at a view which can | 175 // installed on the RootView only if the event is targetted at a view which can |
176 // show a context menu. | 176 // show a context menu. |
177 TEST_F(RootViewTest, ContextMenuFromLongPress) { | 177 TEST_F(RootViewTest, ContextMenuFromLongPress) { |
178 Widget widget; | 178 Widget widget; |
179 Widget::InitParams init_params = | 179 Widget::InitParams init_params = |
180 CreateParams(Widget::InitParams::TYPE_POPUP); | 180 CreateParams(Widget::InitParams::TYPE_POPUP); |
181 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 181 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
182 init_params.bounds = gfx::Rect(100,100); | 182 init_params.bounds = gfx::Rect(100, 100); |
183 widget.Init(init_params); | 183 widget.Init(init_params); |
184 internal::RootView* root_view = | 184 internal::RootView* root_view = |
185 static_cast<internal::RootView*>(widget.GetRootView()); | 185 static_cast<internal::RootView*>(widget.GetRootView()); |
186 | 186 |
187 // Create a view capable of showing the context menu with two children one of | 187 // Create a view capable of showing the context menu with two children one of |
188 // which handles all gesture events (e.g. a button). | 188 // which handles all gesture events (e.g. a button). |
189 TestContextMenuController controller; | 189 TestContextMenuController controller; |
190 View* parent_view = new View; | 190 View* parent_view = new View; |
191 parent_view->set_context_menu_controller(&controller); | 191 parent_view->set_context_menu_controller(&controller); |
192 widget.SetContentsView(parent_view); | 192 widget.SetContentsView(parent_view); |
193 | 193 |
194 View* gesture_handling_child_view = new GestureHandlingView; | 194 View* gesture_handling_child_view = new GestureHandlingView; |
195 gesture_handling_child_view->SetBoundsRect(gfx::Rect(10,10)); | 195 gesture_handling_child_view->SetBoundsRect(gfx::Rect(10, 10)); |
196 parent_view->AddChildView(gesture_handling_child_view); | 196 parent_view->AddChildView(gesture_handling_child_view); |
197 | 197 |
198 View* other_child_view = new View; | 198 View* other_child_view = new View; |
199 other_child_view->SetBoundsRect(gfx::Rect(20, 0, 10,10)); | 199 other_child_view->SetBoundsRect(gfx::Rect(20, 0, 10, 10)); |
200 parent_view->AddChildView(other_child_view); | 200 parent_view->AddChildView(other_child_view); |
201 | 201 |
202 // |parent_view| should not show a context menu as a result of a long press on | 202 // |parent_view| should not show a context menu as a result of a long press on |
203 // |gesture_handling_child_view|. | 203 // |gesture_handling_child_view|. |
204 ui::GestureEvent long_press1( | 204 ui::GestureEvent long_press1( |
205 5, | 205 5, |
206 5, | 206 5, |
207 0, | 207 0, |
208 base::TimeDelta(), | 208 base::TimeDelta(), |
209 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0)); | 209 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0)); |
210 ui::EventDispatchDetails details = root_view->OnEventFromSource(&long_press1); | 210 ui::EventDispatchDetails details = root_view->OnEventFromSource(&long_press1); |
211 | 211 |
212 ui::GestureEvent end1(5, | 212 ui::GestureEvent end1(5, |
213 5, | 213 5, |
214 0, | 214 0, |
215 base::TimeDelta(), | 215 base::TimeDelta(), |
216 ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0)); | 216 ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0)); |
217 details = root_view->OnEventFromSource(&end1); | 217 details = root_view->OnEventFromSource(&end1); |
218 | 218 |
219 EXPECT_FALSE(details.target_destroyed); | 219 EXPECT_FALSE(details.target_destroyed); |
220 EXPECT_FALSE(details.dispatcher_destroyed); | 220 EXPECT_FALSE(details.dispatcher_destroyed); |
221 EXPECT_EQ(0, controller.show_context_menu_calls()); | 221 EXPECT_EQ(0, controller.show_context_menu_calls()); |
| 222 controller.Reset(); |
222 | 223 |
223 // |parent_view| should show a context menu as a result of a long press on | 224 // |parent_view| should show a context menu as a result of a long press on |
224 // |other_child_view|. | 225 // |other_child_view|. |
225 ui::GestureEvent long_press2( | 226 ui::GestureEvent long_press2( |
226 25, | 227 25, |
227 5, | 228 5, |
228 0, | 229 0, |
229 base::TimeDelta(), | 230 base::TimeDelta(), |
230 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0)); | 231 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0)); |
231 details = root_view->OnEventFromSource(&long_press2); | 232 details = root_view->OnEventFromSource(&long_press2); |
232 | 233 |
233 ui::GestureEvent end2(25, | 234 ui::GestureEvent end2(25, |
234 5, | 235 5, |
235 0, | 236 0, |
236 base::TimeDelta(), | 237 base::TimeDelta(), |
237 ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0)); | 238 ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0)); |
238 details = root_view->OnEventFromSource(&end2); | 239 details = root_view->OnEventFromSource(&end2); |
239 | 240 |
240 EXPECT_FALSE(details.target_destroyed); | 241 EXPECT_FALSE(details.target_destroyed); |
241 EXPECT_FALSE(details.dispatcher_destroyed); | 242 EXPECT_FALSE(details.dispatcher_destroyed); |
242 EXPECT_EQ(1, controller.show_context_menu_calls()); | 243 EXPECT_EQ(1, controller.show_context_menu_calls()); |
| 244 controller.Reset(); |
| 245 |
| 246 // |parent_view| should show a context menu as a result of a long press on |
| 247 // itself. |
| 248 ui::GestureEvent long_press3( |
| 249 50, |
| 250 50, |
| 251 0, |
| 252 base::TimeDelta(), |
| 253 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0)); |
| 254 details = root_view->OnEventFromSource(&long_press3); |
| 255 |
| 256 ui::GestureEvent end3(25, |
| 257 5, |
| 258 0, |
| 259 base::TimeDelta(), |
| 260 ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0)); |
| 261 details = root_view->OnEventFromSource(&end3); |
| 262 |
| 263 EXPECT_FALSE(details.target_destroyed); |
| 264 EXPECT_FALSE(details.dispatcher_destroyed); |
| 265 EXPECT_EQ(1, controller.show_context_menu_calls()); |
243 } | 266 } |
244 | 267 |
| 268 // Tests that context menus are not shown for disabled views on a long press. |
| 269 TEST_F(RootViewTest, ContextMenuFromLongPressOnDisabledView) { |
| 270 Widget widget; |
| 271 Widget::InitParams init_params = |
| 272 CreateParams(Widget::InitParams::TYPE_POPUP); |
| 273 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 274 init_params.bounds = gfx::Rect(100, 100); |
| 275 widget.Init(init_params); |
| 276 internal::RootView* root_view = |
| 277 static_cast<internal::RootView*>(widget.GetRootView()); |
| 278 |
| 279 // Create a view capable of showing the context menu with two children one of |
| 280 // which handles all gesture events (e.g. a button). Also mark this view |
| 281 // as disabled. |
| 282 TestContextMenuController controller; |
| 283 View* parent_view = new View; |
| 284 parent_view->set_context_menu_controller(&controller); |
| 285 parent_view->SetEnabled(false); |
| 286 widget.SetContentsView(parent_view); |
| 287 |
| 288 View* gesture_handling_child_view = new GestureHandlingView; |
| 289 gesture_handling_child_view->SetBoundsRect(gfx::Rect(10, 10)); |
| 290 parent_view->AddChildView(gesture_handling_child_view); |
| 291 |
| 292 View* other_child_view = new View; |
| 293 other_child_view->SetBoundsRect(gfx::Rect(20, 0, 10, 10)); |
| 294 parent_view->AddChildView(other_child_view); |
| 295 |
| 296 // |parent_view| should not show a context menu as a result of a long press on |
| 297 // |gesture_handling_child_view|. |
| 298 ui::GestureEvent long_press1( |
| 299 5, |
| 300 5, |
| 301 0, |
| 302 base::TimeDelta(), |
| 303 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0)); |
| 304 ui::EventDispatchDetails details = root_view->OnEventFromSource(&long_press1); |
| 305 |
| 306 ui::GestureEvent end1(5, |
| 307 5, |
| 308 0, |
| 309 base::TimeDelta(), |
| 310 ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0)); |
| 311 details = root_view->OnEventFromSource(&end1); |
| 312 |
| 313 EXPECT_FALSE(details.target_destroyed); |
| 314 EXPECT_FALSE(details.dispatcher_destroyed); |
| 315 EXPECT_EQ(0, controller.show_context_menu_calls()); |
| 316 controller.Reset(); |
| 317 |
| 318 // |parent_view| should not show a context menu as a result of a long press on |
| 319 // |other_child_view|. |
| 320 ui::GestureEvent long_press2( |
| 321 25, |
| 322 5, |
| 323 0, |
| 324 base::TimeDelta(), |
| 325 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0)); |
| 326 details = root_view->OnEventFromSource(&long_press2); |
| 327 |
| 328 ui::GestureEvent end2(25, |
| 329 5, |
| 330 0, |
| 331 base::TimeDelta(), |
| 332 ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0)); |
| 333 details = root_view->OnEventFromSource(&end2); |
| 334 |
| 335 EXPECT_FALSE(details.target_destroyed); |
| 336 EXPECT_FALSE(details.dispatcher_destroyed); |
| 337 EXPECT_EQ(0, controller.show_context_menu_calls()); |
| 338 controller.Reset(); |
| 339 |
| 340 // |parent_view| should not show a context menu as a result of a long press on |
| 341 // itself. |
| 342 ui::GestureEvent long_press3( |
| 343 50, |
| 344 50, |
| 345 0, |
| 346 base::TimeDelta(), |
| 347 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0)); |
| 348 details = root_view->OnEventFromSource(&long_press3); |
| 349 |
| 350 ui::GestureEvent end3(25, |
| 351 5, |
| 352 0, |
| 353 base::TimeDelta(), |
| 354 ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0)); |
| 355 details = root_view->OnEventFromSource(&end3); |
| 356 |
| 357 EXPECT_FALSE(details.target_destroyed); |
| 358 EXPECT_FALSE(details.dispatcher_destroyed); |
| 359 EXPECT_EQ(0, controller.show_context_menu_calls()); |
| 360 } |
| 361 |
245 } // namespace test | 362 } // namespace test |
246 } // namespace views | 363 } // namespace views |
OLD | NEW |