OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
6 #include <set> | 6 #include <set> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
32 #include "ui/views/window/dialog_delegate.h" | 32 #include "ui/views/window/dialog_delegate.h" |
33 #include "ui/views/window/native_frame_view.h" | 33 #include "ui/views/window/native_frame_view.h" |
34 | 34 |
35 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
36 #include "ui/views/win/hwnd_util.h" | 36 #include "ui/views/win/hwnd_util.h" |
37 #endif | 37 #endif |
38 | 38 |
39 namespace views { | 39 namespace views { |
40 namespace test { | 40 namespace test { |
41 | 41 |
42 namespace { | |
43 | |
44 // TODO(tdanderson): This utility function is used in different unittest | |
45 // files. Move to a common location to avoid | |
46 // repeated code. | |
47 gfx::Point ConvertPointToView(View* view, const gfx::Point& p) { | |
sadrul
2014/09/07 05:33:47
FromWidgetToView?
tdanderson
2014/09/08 17:21:08
Done.
| |
48 gfx::Point tmp(p); | |
49 View::ConvertPointToTarget(view->GetWidget()->GetRootView(), view, &tmp); | |
50 return tmp; | |
51 } | |
52 | |
53 } // namespace | |
54 | |
42 // A view that keeps track of the events it receives, optionally consuming them. | 55 // A view that keeps track of the events it receives, optionally consuming them. |
43 class EventCountView : public View { | 56 class EventCountView : public View { |
44 public: | 57 public: |
45 // Whether to call SetHandled() on events as they are received. For some event | 58 // Whether to call SetHandled() on events as they are received. For some event |
46 // types, this will allow EventCountView to receives future events in the | 59 // types, this will allow EventCountView to receives future events in the |
47 // event sequence, such as a drag. | 60 // event sequence, such as a drag. |
48 enum HandleMode { | 61 enum HandleMode { |
49 PROPAGATE_EVENTS, | 62 PROPAGATE_EVENTS, |
50 CONSUME_EVENTS | 63 CONSUME_EVENTS |
51 }; | 64 }; |
(...skipping 2290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2342 EXPECT_EQ(1, v1->GetEventCount(ui::ET_GESTURE_END)); | 2355 EXPECT_EQ(1, v1->GetEventCount(ui::ET_GESTURE_END)); |
2343 EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_END)); | 2356 EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_END)); |
2344 EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_END)); | 2357 EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_END)); |
2345 EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_END)); | 2358 EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_END)); |
2346 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | 2359 EXPECT_EQ(NULL, GetGestureHandler(root_view)); |
2347 EXPECT_TRUE(end.handled()); | 2360 EXPECT_TRUE(end.handled()); |
2348 | 2361 |
2349 widget->Close(); | 2362 widget->Close(); |
2350 } | 2363 } |
2351 | 2364 |
2365 // A class used in WidgetTest.GestureEventLocationWhileBubbling to verify | |
2366 // that when a gesture event bubbles up a View hierarchy, the location | |
2367 // of a gesture event seen by each View is in the local coordinate space | |
2368 // of that View. | |
2369 class GestureLocationView : public EventCountView { | |
2370 public: | |
2371 GestureLocationView() {} | |
2372 virtual ~GestureLocationView() {} | |
2373 | |
2374 void set_expected_location(gfx::Point expected_location) { | |
2375 expected_location_ = expected_location; | |
2376 } | |
2377 | |
2378 // EventCountView: | |
2379 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE { | |
2380 EventCountView::OnGestureEvent(event); | |
2381 | |
2382 // Verify that the location of |event| is in the local coordinate | |
2383 // space of |this|. | |
2384 EXPECT_EQ(expected_location_, event->location()); | |
2385 } | |
2386 | |
2387 private: | |
2388 // The expected location of a gesture event dispatched to |this|. | |
2389 gfx::Point expected_location_; | |
2390 | |
2391 DISALLOW_COPY_AND_ASSIGN(GestureLocationView); | |
2392 }; | |
2393 | |
2394 // Verifies that the location of a gesture event is always in the local | |
2395 // coordinate space of the View receiving the event while bubbling. | |
2396 TEST_F(WidgetTest, GestureEventLocationWhileBubbling) { | |
2397 Widget* widget = CreateTopLevelNativeWidget(); | |
2398 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); | |
2399 | |
2400 // Define a hierarchy of three views (coordinates shown below are in the | |
2401 // coordinate space of the root view, but the coordinates used for | |
2402 // SetBounds() are in their parent coordinate space). | |
2403 // v1 (50, 50, 150, 150) | |
2404 // v2 (100, 70, 50, 80) | |
2405 // v3 (120, 100, 10, 10) | |
2406 GestureLocationView* v1 = new GestureLocationView(); | |
2407 v1->SetBounds(50, 50, 150, 150); | |
2408 GestureLocationView* v2 = new GestureLocationView(); | |
2409 v2->SetBounds(50, 20, 50, 80); | |
2410 GestureLocationView* v3 = new GestureLocationView(); | |
2411 v3->SetBounds(20, 30, 10, 10); | |
2412 internal::RootView* root_view = | |
2413 static_cast<internal::RootView*>(widget->GetRootView()); | |
2414 root_view->AddChildView(v1); | |
2415 v1->AddChildView(v2); | |
2416 v2->AddChildView(v3); | |
2417 | |
2418 widget->Show(); | |
2419 | |
2420 // Define a GESTURE_TAP event located at (125, 105) in root view coordinates. | |
2421 // This event is contained within all of |v1|, |v2|, and |v3|. | |
2422 gfx::Point location_in_root(125, 105); | |
2423 GestureEventForTest tap( | |
2424 ui::ET_GESTURE_TAP, location_in_root.x(), location_in_root.y()); | |
2425 | |
2426 // Calculate the location of the event in the local coordinate spaces | |
2427 // of each of the views. | |
2428 gfx::Point location_in_v1(ConvertPointToView(v1, location_in_root)); | |
2429 EXPECT_EQ(gfx::Point(75, 55), location_in_v1); | |
2430 gfx::Point location_in_v2(ConvertPointToView(v2, location_in_root)); | |
2431 EXPECT_EQ(gfx::Point(25, 35), location_in_v2); | |
2432 gfx::Point location_in_v3(ConvertPointToView(v3, location_in_root)); | |
2433 EXPECT_EQ(gfx::Point(5, 5), location_in_v3); | |
2434 | |
2435 // Dispatch the event. When each view receives the event, its location should | |
2436 // be in the local coordinate space of that view (see the check made by | |
2437 // GestureLocationView). After dispatch is complete the event's location | |
2438 // should be in the root coordinate space. | |
2439 v1->set_expected_location(location_in_v1); | |
2440 v2->set_expected_location(location_in_v2); | |
2441 v3->set_expected_location(location_in_v3); | |
2442 widget->OnGestureEvent(&tap); | |
2443 EXPECT_EQ(location_in_root, tap.location()); | |
2444 | |
2445 // Verify that each view did in fact see the event. | |
2446 EventCountView* view1 = v1; | |
2447 EventCountView* view2 = v2; | |
2448 EventCountView* view3 = v3; | |
2449 EXPECT_EQ(1, view1->GetEventCount(ui::ET_GESTURE_TAP)); | |
2450 EXPECT_EQ(1, view2->GetEventCount(ui::ET_GESTURE_TAP)); | |
2451 EXPECT_EQ(1, view3->GetEventCount(ui::ET_GESTURE_TAP)); | |
2452 | |
2453 widget->Close(); | |
2454 } | |
2455 | |
2352 // Verifies that disabled views are permitted to be set as the default gesture | 2456 // Verifies that disabled views are permitted to be set as the default gesture |
2353 // handler in RootView. Also verifies that gesture events targeted to a disabled | 2457 // handler in RootView. Also verifies that gesture events targeted to a disabled |
2354 // view are not actually dispatched to the view, but are still marked as | 2458 // view are not actually dispatched to the view, but are still marked as |
2355 // handled. | 2459 // handled. |
2356 TEST_F(WidgetTest, DisabledGestureEventTarget) { | 2460 TEST_F(WidgetTest, DisabledGestureEventTarget) { |
2357 Widget* widget = CreateTopLevelNativeWidget(); | 2461 Widget* widget = CreateTopLevelNativeWidget(); |
2358 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); | 2462 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); |
2359 | 2463 |
2360 // Define a hierarchy of four views (coordinates are in | 2464 // Define a hierarchy of four views (coordinates are in |
2361 // their parent coordinate space). | 2465 // their parent coordinate space). |
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3089 EXPECT_EQ(3, view->GetEventCount(ui::ET_MOUSE_PRESSED)); | 3193 EXPECT_EQ(3, view->GetEventCount(ui::ET_MOUSE_PRESSED)); |
3090 EXPECT_EQ(3, view->GetEventCount(ui::ET_MOUSE_RELEASED)); | 3194 EXPECT_EQ(3, view->GetEventCount(ui::ET_MOUSE_RELEASED)); |
3091 EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_DRAGGED)); | 3195 EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_DRAGGED)); |
3092 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, view->last_flags()); | 3196 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, view->last_flags()); |
3093 | 3197 |
3094 widget->CloseNow(); | 3198 widget->CloseNow(); |
3095 } | 3199 } |
3096 | 3200 |
3097 } // namespace test | 3201 } // namespace test |
3098 } // namespace views | 3202 } // namespace views |
OLD | NEW |