Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: ui/views/widget/widget_unittest.cc

Issue 11787042: views: Stop dispatching scroll-gesture events if the scroll-begin event wasn't handled. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/widget/root_view.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/native_widget_types.h" 10 #include "ui/gfx/native_widget_types.h"
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 virtual ~EventCountView() {} 129 virtual ~EventCountView() {}
130 130
131 int GetEventCount(ui::EventType type) { 131 int GetEventCount(ui::EventType type) {
132 return event_count_[type]; 132 return event_count_[type];
133 } 133 }
134 134
135 void ResetCounts() { 135 void ResetCounts() {
136 event_count_.clear(); 136 event_count_.clear();
137 } 137 }
138 138
139 private: 139 protected:
140 void RecordEvent(const ui::Event& event) {
141 ++event_count_[event.type()];
142 }
143
144 // Overridden from View: 140 // Overridden from View:
145 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE { 141 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE {
146 RecordEvent(event); 142 RecordEvent(event);
147 return false; 143 return false;
148 } 144 }
149 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE { 145 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE {
150 RecordEvent(event); 146 RecordEvent(event);
151 return false; 147 return false;
152 } 148 }
153 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE { 149 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE { 181 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE {
186 RecordEvent(*event); 182 RecordEvent(*event);
187 } 183 }
188 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE { 184 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
189 RecordEvent(*event); 185 RecordEvent(*event);
190 } 186 }
191 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE { 187 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
192 RecordEvent(*event); 188 RecordEvent(*event);
193 } 189 }
194 190
191 private:
192 void RecordEvent(const ui::Event& event) {
193 ++event_count_[event.type()];
194 }
195
195 std::map<ui::EventType, int> event_count_; 196 std::map<ui::EventType, int> event_count_;
196 197
197 DISALLOW_COPY_AND_ASSIGN(EventCountView); 198 DISALLOW_COPY_AND_ASSIGN(EventCountView);
198 }; 199 };
199 200
201 // A view that keeps track of the events it receives, and consumes all scroll
202 // gesture events.
203 class ScrollableEventCountView : public EventCountView {
204 public:
205 ScrollableEventCountView() {}
206 virtual ~ScrollableEventCountView() {}
207
208 private:
209 // Overridden from ui::EventHandler:
210 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
211 EventCountView::OnGestureEvent(event);
212 switch (event->type()) {
213 case ui::ET_GESTURE_SCROLL_BEGIN:
214 case ui::ET_GESTURE_SCROLL_UPDATE:
215 case ui::ET_GESTURE_SCROLL_END:
216 case ui::ET_SCROLL_FLING_START:
217 event->SetHandled();
218 break;
219 default:
220 break;
221 }
222 }
223
224 DISALLOW_COPY_AND_ASSIGN(ScrollableEventCountView);
225 };
226
200 // A view that does a capture on gesture-begin events. 227 // A view that does a capture on gesture-begin events.
201 class GestureCaptureView : public View { 228 class GestureCaptureView : public View {
202 public: 229 public:
203 GestureCaptureView() {} 230 GestureCaptureView() {}
204 virtual ~GestureCaptureView() {} 231 virtual ~GestureCaptureView() {}
205 232
206 private: 233 private:
207 // Overridden from View: 234 // Overridden from View:
208 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE { 235 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
209 if (event->type() == ui::ET_GESTURE_BEGIN) { 236 if (event->type() == ui::ET_GESTURE_BEGIN) {
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after
1275 focused_view->ResetCounts(); 1302 focused_view->ResetCounts();
1276 cursor_view->ResetCounts(); 1303 cursor_view->ResetCounts();
1277 1304
1278 ui::ScrollEvent scroll2(ui::ET_SCROLL, gfx::Point(5, 5), 0, 0, 20); 1305 ui::ScrollEvent scroll2(ui::ET_SCROLL, gfx::Point(5, 5), 0, 0, 20);
1279 widget->OnScrollEvent(&scroll2); 1306 widget->OnScrollEvent(&scroll2);
1280 EXPECT_EQ(1, focused_view->GetEventCount(ui::ET_SCROLL)); 1307 EXPECT_EQ(1, focused_view->GetEventCount(ui::ET_SCROLL));
1281 EXPECT_EQ(1, focused_view->GetEventCount(ui::ET_MOUSEWHEEL)); 1308 EXPECT_EQ(1, focused_view->GetEventCount(ui::ET_MOUSEWHEEL));
1282 1309
1283 EXPECT_EQ(0, cursor_view->GetEventCount(ui::ET_SCROLL)); 1310 EXPECT_EQ(0, cursor_view->GetEventCount(ui::ET_SCROLL));
1284 EXPECT_EQ(0, cursor_view->GetEventCount(ui::ET_MOUSEWHEEL)); 1311 EXPECT_EQ(0, cursor_view->GetEventCount(ui::ET_MOUSEWHEEL));
1312
1313 widget->CloseNow();
1285 } 1314 }
1286 1315
1287 #endif // defined(USE_AURA) 1316 #endif // defined(USE_AURA)
1288 1317
1318 // Tests that if a scroll-begin gesture is not handled, then subsequent scroll
1319 // events are not dispatched to any view.
1320 TEST_F(WidgetTest, GestureScrollEventDispatching) {
1321 EventCountView* noscroll_view = new EventCountView;
1322 EventCountView* scroll_view = new ScrollableEventCountView;
1323
1324 noscroll_view->SetBounds(0, 0, 50, 40);
1325 scroll_view->SetBounds(60, 0, 40, 40);
1326
1327 Widget* widget = CreateTopLevelPlatformWidget();
1328 widget->GetRootView()->AddChildView(noscroll_view);
1329 widget->GetRootView()->AddChildView(scroll_view);
1330
1331 {
1332 ui::GestureEvent begin(ui::ET_GESTURE_SCROLL_BEGIN,
1333 5, 5, 0, base::TimeDelta(),
1334 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN, 0, 0),
1335 1);
1336 widget->OnGestureEvent(&begin);
1337 ui::GestureEvent update(ui::ET_GESTURE_SCROLL_UPDATE,
1338 25, 15, 0, base::TimeDelta(),
1339 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, 20, 10),
1340 1);
1341 widget->OnGestureEvent(&update);
1342 ui::GestureEvent end(ui::ET_GESTURE_SCROLL_END,
1343 25, 15, 0, base::TimeDelta(),
1344 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_END, 0, 0),
1345 1);
1346 widget->OnGestureEvent(&end);
1347
1348 EXPECT_EQ(1, noscroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_BEGIN));
1349 EXPECT_EQ(0, noscroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
1350 EXPECT_EQ(0, noscroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_END));
1351 }
1352
1353 {
1354 ui::GestureEvent begin(ui::ET_GESTURE_SCROLL_BEGIN,
1355 65, 5, 0, base::TimeDelta(),
1356 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN, 0, 0),
1357 1);
1358 widget->OnGestureEvent(&begin);
1359 ui::GestureEvent update(ui::ET_GESTURE_SCROLL_UPDATE,
1360 85, 15, 0, base::TimeDelta(),
1361 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, 20, 10),
1362 1);
1363 widget->OnGestureEvent(&update);
1364 ui::GestureEvent end(ui::ET_GESTURE_SCROLL_END,
1365 85, 15, 0, base::TimeDelta(),
1366 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_END, 0, 0),
1367 1);
1368 widget->OnGestureEvent(&end);
1369
1370 EXPECT_EQ(1, scroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_BEGIN));
1371 EXPECT_EQ(1, scroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
1372 EXPECT_EQ(1, scroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_END));
1373 }
1374
1375 widget->CloseNow();
1376 }
1377
1289 } // namespace 1378 } // namespace
1290 } // namespace views 1379 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/widget/root_view.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698