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 1960 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1971 0, | 1971 0, |
1972 base::TimeDelta(), | 1972 base::TimeDelta(), |
1973 ui::GestureEventDetails(type)) {} | 1973 ui::GestureEventDetails(type)) {} |
1974 | 1974 |
1975 GestureEventForTest(ui::GestureEventDetails details, int x, int y) | 1975 GestureEventForTest(ui::GestureEventDetails details, int x, int y) |
1976 : GestureEvent(x, y, 0, base::TimeDelta(), details) {} | 1976 : GestureEvent(x, y, 0, base::TimeDelta(), details) {} |
1977 }; | 1977 }; |
1978 | 1978 |
1979 // Tests that the |gesture_handler_| member in RootView is always NULL | 1979 // Tests that the |gesture_handler_| member in RootView is always NULL |
1980 // after the dispatch of a ui::ET_GESTURE_END event corresponding to | 1980 // after the dispatch of a ui::ET_GESTURE_END event corresponding to |
1981 // the release of the final touch point on the screen and that | 1981 // the release of the final touch point on the screen, but that |
1982 // ui::ET_GESTURE_END events corresponding to the removal of any other touch | 1982 // ui::ET_GESTURE_END events corresponding to the removal of any other touch |
1983 // point are never dispatched to a view. Also verifies that | 1983 // point do not modify |gesture_handler_|. |
1984 // ui::ET_GESTURE_BEGIN is never dispatched to a view and does not change the | 1984 TEST_F(WidgetTest, GestureEndEvents) { |
tdanderson
2014/09/23 21:30:27
Reduced the scope and size of this test since some
| |
1985 // value of |gesture_handler_|. | |
1986 TEST_F(WidgetTest, GestureBeginAndEndEvents) { | |
1987 Widget* widget = CreateTopLevelNativeWidget(); | 1985 Widget* widget = CreateTopLevelNativeWidget(); |
1988 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); | 1986 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); |
1989 EventCountView* view = new EventCountView(); | 1987 EventCountView* view = new EventCountView(); |
1990 view->SetBounds(0, 0, 300, 300); | 1988 view->SetBounds(0, 0, 300, 300); |
1991 internal::RootView* root_view = | 1989 internal::RootView* root_view = |
1992 static_cast<internal::RootView*>(widget->GetRootView()); | 1990 static_cast<internal::RootView*>(widget->GetRootView()); |
1993 root_view->AddChildView(view); | 1991 root_view->AddChildView(view); |
1994 widget->Show(); | 1992 widget->Show(); |
1995 | 1993 |
1996 // If no gesture handler is set, dispatching a ui::ET_GESTURE_END or | 1994 // If no gesture handler is set, a ui::ET_GESTURE_END event should not set |
1997 // ui::ET_GESTURE_BEGIN event should not set the gesture handler and | 1995 // the gesture handler and the event should remain unhandled because the |
1998 // the events should remain unhandled because the handle mode of |view| | 1996 // handle mode of |view| indicates that events should not be consumed. |
1999 // indicates that events should not be consumed. | |
2000 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | 1997 EXPECT_EQ(NULL, GetGestureHandler(root_view)); |
2001 GestureEventForTest end(ui::ET_GESTURE_END, 15, 15); | 1998 GestureEventForTest end(ui::ET_GESTURE_END, 15, 15); |
2002 widget->OnGestureEvent(&end); | 1999 widget->OnGestureEvent(&end); |
2003 EXPECT_FALSE(end.handled()); | 2000 EXPECT_FALSE(end.handled()); |
2004 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | 2001 EXPECT_EQ(NULL, GetGestureHandler(root_view)); |
2005 | 2002 |
2006 GestureEventForTest begin(ui::ET_GESTURE_BEGIN, 15, 15); | |
2007 widget->OnGestureEvent(&begin); | |
2008 EXPECT_FALSE(begin.handled()); | |
2009 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2010 | |
2011 // Change the handle mode of |view| to indicate that it would like | 2003 // Change the handle mode of |view| to indicate that it would like |
2012 // to handle all events. | 2004 // to handle all events, then send a GESTURE_TAP to set the gesture handler. |
2013 view->set_handle_mode(EventCountView::CONSUME_EVENTS); | 2005 view->set_handle_mode(EventCountView::CONSUME_EVENTS); |
2014 | |
2015 // If no gesture handler is set, dispatching only a ui::ET_GESTURE_BEGIN | |
2016 // should not set the gesture handler and should not be marked as handled | |
2017 // because it is never dispatched. | |
2018 begin = GestureEventForTest(ui::ET_GESTURE_BEGIN, 15, 15); | |
2019 widget->OnGestureEvent(&begin); | |
2020 EXPECT_FALSE(begin.handled()); | |
2021 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2022 | |
2023 // If no gesture handler is set, dispatching only a ui::ET_GESTURE_BEGIN | |
2024 // corresponding to a second touch point should not set the gesture handler | |
2025 // and should not be marked as handled because it is never dispatched. | |
2026 ui::GestureEventDetails details(ui::ET_GESTURE_END); | |
2027 details.set_touch_points(2); | |
2028 GestureEventForTest end_second_touch_point(details, 15, 15); | |
2029 widget->OnGestureEvent(&end_second_touch_point); | |
2030 EXPECT_FALSE(end_second_touch_point.handled()); | |
2031 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2032 | |
2033 // If no gesture handler is set, dispatching only a ui::ET_GESTURE_END | |
2034 // event corresponding to the final touch point should not set the gesture | |
2035 // handler. Furthermore, it should not be marked as handled because it was | |
2036 // not dispatched (GESTURE_END events are only dispatched in cases where | |
2037 // a gesture handler is already set). | |
2038 end = GestureEventForTest(ui::ET_GESTURE_END, 15, 15); | |
2039 widget->OnGestureEvent(&end); | |
2040 EXPECT_FALSE(end.handled()); | |
2041 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2042 | |
2043 // If the gesture handler has been set by a previous gesture, then it should | |
2044 // remain unchanged on a ui::ET_GESTURE_BEGIN or a ui::ET_GESTURE_END | |
2045 // corresponding to a second touch point. It should be reset to NULL by a | |
2046 // ui::ET_GESTURE_END corresponding to the final touch point. | |
2047 GestureEventForTest tap(ui::ET_GESTURE_TAP, 15, 15); | 2006 GestureEventForTest tap(ui::ET_GESTURE_TAP, 15, 15); |
2048 widget->OnGestureEvent(&tap); | 2007 widget->OnGestureEvent(&tap); |
2049 EXPECT_TRUE(tap.handled()); | 2008 EXPECT_TRUE(tap.handled()); |
2050 EXPECT_EQ(view, GetGestureHandler(root_view)); | 2009 EXPECT_EQ(view, GetGestureHandler(root_view)); |
2051 | 2010 |
2052 begin = GestureEventForTest(ui::ET_GESTURE_BEGIN, 15, 15); | 2011 // The gesture handler should remain unchanged on a ui::ET_GESTURE_END |
2053 widget->OnGestureEvent(&begin); | 2012 // corresponding to a second touch point, but should be reset to NULL by a |
2054 EXPECT_FALSE(begin.handled()); | 2013 // ui::ET_GESTURE_END corresponding to the final touch point. |
2055 EXPECT_EQ(view, GetGestureHandler(root_view)); | 2014 ui::GestureEventDetails details(ui::ET_GESTURE_END); |
2056 | 2015 details.set_touch_points(2); |
2057 end_second_touch_point = GestureEventForTest(details, 15, 15); | 2016 GestureEventForTest end_second_touch_point(details, 15, 15); |
2058 widget->OnGestureEvent(&end_second_touch_point); | 2017 widget->OnGestureEvent(&end_second_touch_point); |
2059 EXPECT_FALSE(end_second_touch_point.handled()); | |
2060 EXPECT_EQ(view, GetGestureHandler(root_view)); | 2018 EXPECT_EQ(view, GetGestureHandler(root_view)); |
2061 | 2019 |
2062 end = GestureEventForTest(ui::ET_GESTURE_END, 15, 15); | 2020 end = GestureEventForTest(ui::ET_GESTURE_END, 15, 15); |
2063 widget->OnGestureEvent(&end); | 2021 widget->OnGestureEvent(&end); |
2064 EXPECT_TRUE(end.handled()); | 2022 EXPECT_TRUE(end.handled()); |
2065 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | 2023 EXPECT_EQ(NULL, GetGestureHandler(root_view)); |
2066 | 2024 |
2067 // If the gesture handler has been set by a previous gesture, then | 2025 // Send a GESTURE_TAP to set the gesture handler, then change the handle |
2068 // it should remain unchanged on a ui::ET_GESTURE_BEGIN or a | 2026 // mode of |view| to indicate that it does not want to handle any |
2069 // ui::ET_GESTURE_END corresponding to a second touch point and be reset | 2027 // further events. |
2070 // to NULL by a ui::ET_GESTURE_END corresponding to the final touch point, | |
2071 // even when the gesture handler has indicated that it would not like to | |
2072 // handle any further events. | |
2073 tap = GestureEventForTest(ui::ET_GESTURE_TAP, 15, 15); | 2028 tap = GestureEventForTest(ui::ET_GESTURE_TAP, 15, 15); |
2074 widget->OnGestureEvent(&tap); | 2029 widget->OnGestureEvent(&tap); |
2075 EXPECT_TRUE(tap.handled()); | 2030 EXPECT_TRUE(tap.handled()); |
2076 EXPECT_EQ(view, GetGestureHandler(root_view)); | 2031 EXPECT_EQ(view, GetGestureHandler(root_view)); |
2077 | |
2078 // Change the handle mode of |view| to indicate that it does not want | |
2079 // to handle any further events. | |
2080 view->set_handle_mode(EventCountView::PROPAGATE_EVENTS); | 2032 view->set_handle_mode(EventCountView::PROPAGATE_EVENTS); |
2081 | 2033 |
2082 begin = GestureEventForTest(ui::ET_GESTURE_BEGIN, 15, 15); | 2034 // The gesture handler should remain unchanged on a ui::ET_GESTURE_END |
2083 widget->OnGestureEvent(&begin); | 2035 // corresponding to a second touch point, but should be reset to NULL by a |
2084 EXPECT_FALSE(begin.handled()); | 2036 // ui::ET_GESTURE_END corresponding to the final touch point. |
2085 EXPECT_EQ(view, GetGestureHandler(root_view)); | |
2086 | |
2087 end_second_touch_point = GestureEventForTest(details, 15, 15); | 2037 end_second_touch_point = GestureEventForTest(details, 15, 15); |
2088 widget->OnGestureEvent(&end_second_touch_point); | 2038 widget->OnGestureEvent(&end_second_touch_point); |
2089 EXPECT_FALSE(end_second_touch_point.handled()); | |
2090 EXPECT_EQ(view, GetGestureHandler(root_view)); | 2039 EXPECT_EQ(view, GetGestureHandler(root_view)); |
2091 | 2040 |
2092 end = GestureEventForTest(ui::ET_GESTURE_END, 15, 15); | 2041 end = GestureEventForTest(ui::ET_GESTURE_END, 15, 15); |
2093 widget->OnGestureEvent(&end); | 2042 widget->OnGestureEvent(&end); |
2094 EXPECT_FALSE(end.handled()); | 2043 EXPECT_FALSE(end.handled()); |
2095 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | 2044 EXPECT_EQ(NULL, GetGestureHandler(root_view)); |
2096 | 2045 |
2097 widget->Close(); | 2046 widget->Close(); |
2098 } | 2047 } |
2048 | |
2049 // Tests that gesture events which should not be processed (because | |
2050 // RootView::OnEventProcessingStarted() has marked them as handled) are not | |
2051 // dispatched to any views. | |
2052 TEST_F(WidgetTest, GestureEventsNotProcessed) { | |
2053 Widget* widget = CreateTopLevelNativeWidget(); | |
2054 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); | |
2055 | |
2056 // Define a hierarchy of four views (coordinates are in | |
2057 // their parent coordinate space). | |
2058 // v1 (0, 0, 300, 300) | |
2059 // v2 (0, 0, 100, 100) | |
2060 // v3 (0, 0, 50, 50) | |
2061 // v4(0, 0, 10, 10) | |
2062 EventCountView* v1 = new EventCountView(); | |
2063 v1->SetBounds(0, 0, 300, 300); | |
2064 EventCountView* v2 = new EventCountView(); | |
2065 v2->SetBounds(0, 0, 100, 100); | |
2066 EventCountView* v3 = new EventCountView(); | |
2067 v3->SetBounds(0, 0, 50, 50); | |
2068 EventCountView* v4 = new EventCountView(); | |
2069 v4->SetBounds(0, 0, 10, 10); | |
2070 internal::RootView* root_view = | |
2071 static_cast<internal::RootView*>(widget->GetRootView()); | |
2072 root_view->AddChildView(v1); | |
2073 v1->AddChildView(v2); | |
2074 v2->AddChildView(v3); | |
2075 v3->AddChildView(v4); | |
2076 | |
2077 widget->Show(); | |
2078 | |
2079 // ui::ET_GESTURE_BEGIN events should never be seen by any view, but | |
2080 // they should be marked as handled by OnEventProcessingStarted(). | |
2081 GestureEventForTest begin(ui::ET_GESTURE_BEGIN, 5, 5); | |
2082 widget->OnGestureEvent(&begin); | |
2083 EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_BEGIN)); | |
2084 EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_BEGIN)); | |
2085 EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_BEGIN)); | |
2086 EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_BEGIN)); | |
2087 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2088 EXPECT_TRUE(begin.handled()); | |
2089 v1->ResetCounts(); | |
2090 v2->ResetCounts(); | |
2091 v3->ResetCounts(); | |
2092 v4->ResetCounts(); | |
2093 | |
2094 // ui::ET_GESTURE_END events not corresponding to the release of the | |
2095 // final touch point should never be seen by any view, but they should | |
2096 // be marked as handled by OnEventProcessingStarted(). | |
2097 ui::GestureEventDetails details(ui::ET_GESTURE_END, 5, 5); | |
2098 details.set_touch_points(2); | |
2099 GestureEventForTest end_second_touch_point(details, 5, 5); | |
2100 widget->OnGestureEvent(&end_second_touch_point); | |
2101 EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_END)); | |
2102 EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_END)); | |
2103 EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_END)); | |
2104 EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_END)); | |
2105 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2106 EXPECT_TRUE(end_second_touch_point.handled()); | |
2107 v1->ResetCounts(); | |
2108 v2->ResetCounts(); | |
2109 v3->ResetCounts(); | |
2110 v4->ResetCounts(); | |
2111 | |
2112 // ui::ET_GESTURE_SCROLL_UPDATE events should never be seen by any view when | |
2113 // there is no default gesture handler set, but they should be marked as | |
2114 // handled by OnEventProcessingStarted(). | |
2115 GestureEventForTest scroll_update(ui::ET_GESTURE_SCROLL_UPDATE, 5, 5); | |
2116 widget->OnGestureEvent(&scroll_update); | |
2117 EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE)); | |
2118 EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE)); | |
2119 EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE)); | |
2120 EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE)); | |
2121 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2122 EXPECT_TRUE(scroll_update.handled()); | |
2123 v1->ResetCounts(); | |
2124 v2->ResetCounts(); | |
2125 v3->ResetCounts(); | |
2126 v4->ResetCounts(); | |
2127 | |
2128 // ui::ET_GESTURE_SCROLL_END events should never be seen by any view when | |
2129 // there is no default gesture handler set, but they should be marked as | |
2130 // handled by OnEventProcessingStarted(). | |
2131 GestureEventForTest scroll_end(ui::ET_GESTURE_SCROLL_END, 5, 5); | |
2132 widget->OnGestureEvent(&scroll_end); | |
2133 EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_SCROLL_END)); | |
2134 EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_SCROLL_END)); | |
2135 EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_SCROLL_END)); | |
2136 EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_SCROLL_END)); | |
2137 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2138 EXPECT_TRUE(scroll_end.handled()); | |
2139 v1->ResetCounts(); | |
2140 v2->ResetCounts(); | |
2141 v3->ResetCounts(); | |
2142 v4->ResetCounts(); | |
2143 | |
2144 // ui::ET_SCROLL_FLING_START events should never be seen by any view when | |
2145 // there is no default gesture handler set, but they should be marked as | |
2146 // handled by OnEventProcessingStarted(). | |
2147 GestureEventForTest scroll_fling_start(ui::ET_SCROLL_FLING_START, 5, 5); | |
2148 widget->OnGestureEvent(&scroll_fling_start); | |
2149 EXPECT_EQ(0, v1->GetEventCount(ui::ET_SCROLL_FLING_START)); | |
2150 EXPECT_EQ(0, v2->GetEventCount(ui::ET_SCROLL_FLING_START)); | |
2151 EXPECT_EQ(0, v3->GetEventCount(ui::ET_SCROLL_FLING_START)); | |
2152 EXPECT_EQ(0, v4->GetEventCount(ui::ET_SCROLL_FLING_START)); | |
2153 EXPECT_EQ(NULL, GetGestureHandler(root_view)); | |
2154 EXPECT_TRUE(scroll_fling_start.handled()); | |
2155 v1->ResetCounts(); | |
2156 v2->ResetCounts(); | |
2157 v3->ResetCounts(); | |
2158 v4->ResetCounts(); | |
2159 | |
2160 widget->Close(); | |
2161 } | |
2099 | 2162 |
2100 // Tests that a (non-scroll) gesture event is dispatched to the correct views | 2163 // Tests that a (non-scroll) gesture event is dispatched to the correct views |
2101 // in a view hierarchy and that the default gesture handler in RootView is set | 2164 // in a view hierarchy and that the default gesture handler in RootView is set |
2102 // correctly. | 2165 // correctly. |
2103 TEST_F(WidgetTest, GestureEventDispatch) { | 2166 TEST_F(WidgetTest, GestureEventDispatch) { |
2104 Widget* widget = CreateTopLevelNativeWidget(); | 2167 Widget* widget = CreateTopLevelNativeWidget(); |
2105 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); | 2168 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); |
2106 | 2169 |
2107 // Define a hierarchy of four views (coordinates are in | 2170 // Define a hierarchy of four views (coordinates are in |
2108 // their parent coordinate space). | 2171 // their parent coordinate space). |
(...skipping 1100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3209 | 3272 |
3210 EXPECT_EQ(test_rect, root_view->bounds()); | 3273 EXPECT_EQ(test_rect, root_view->bounds()); |
3211 widget->ReorderNativeViews(); | 3274 widget->ReorderNativeViews(); |
3212 EXPECT_EQ(test_rect, root_view->bounds()); | 3275 EXPECT_EQ(test_rect, root_view->bounds()); |
3213 | 3276 |
3214 widget->CloseNow(); | 3277 widget->CloseNow(); |
3215 } | 3278 } |
3216 | 3279 |
3217 } // namespace test | 3280 } // namespace test |
3218 } // namespace views | 3281 } // namespace views |
OLD | NEW |