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

Side by Side Diff: ui/views/view_unittest.cc

Issue 297963006: Introduce View::CanProcessEventsWithinSubtree() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: second test added Created 6 years, 6 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
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 <map> 5 #include <map>
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/rand_util.h" 8 #include "base/rand_util.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 196
197 } // namespace 197 } // namespace
198 198
199 namespace views { 199 namespace views {
200 200
201 typedef ViewsTestBase ViewTest; 201 typedef ViewsTestBase ViewTest;
202 202
203 // A derived class for testing purpose. 203 // A derived class for testing purpose.
204 class TestView : public View { 204 class TestView : public View {
205 public: 205 public:
206 TestView() : View(), delete_on_pressed_(false), native_theme_(NULL) {} 206 TestView()
207 : View(),
208 delete_on_pressed_(false),
209 native_theme_(NULL),
210 can_process_events_within_subtree_(true) {}
207 virtual ~TestView() {} 211 virtual ~TestView() {}
208 212
209 // Reset all test state 213 // Reset all test state
210 void Reset() { 214 void Reset() {
211 did_change_bounds_ = false; 215 did_change_bounds_ = false;
212 last_mouse_event_type_ = 0; 216 last_mouse_event_type_ = 0;
213 location_.SetPoint(0, 0); 217 location_.SetPoint(0, 0);
214 received_mouse_enter_ = false; 218 received_mouse_enter_ = false;
215 received_mouse_exit_ = false; 219 received_mouse_exit_ = false;
216 last_gesture_event_type_ = 0; 220 last_gesture_event_type_ = 0;
217 last_gesture_event_was_handled_ = false; 221 last_gesture_event_was_handled_ = false;
218 last_clip_.setEmpty(); 222 last_clip_.setEmpty();
219 accelerator_count_map_.clear(); 223 accelerator_count_map_.clear();
224 can_process_events_within_subtree_ = true;
220 } 225 }
221 226
222 // Exposed as public for testing. 227 // Exposed as public for testing.
223 void DoFocus() { 228 void DoFocus() {
224 views::View::Focus(); 229 views::View::Focus();
225 } 230 }
226 231
227 void DoBlur() { 232 void DoBlur() {
228 views::View::Blur(); 233 views::View::Blur();
229 } 234 }
230 235
231 bool focusable() const { return View::focusable(); } 236 bool focusable() const { return View::focusable(); }
232 237
238 void set_can_process_events_within_subtree(bool can_process) {
239 can_process_events_within_subtree_ = can_process;
240 }
241
242 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE {
243 return can_process_events_within_subtree_;
244 }
245
233 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; 246 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
234 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 247 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
235 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE; 248 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
236 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 249 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
237 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE; 250 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
238 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; 251 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
239 252
240 // Ignores GestureEvent by default. 253 // Ignores GestureEvent by default.
241 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 254 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
242 255
(...skipping 23 matching lines...) Expand all
266 bool last_gesture_event_was_handled_; 279 bool last_gesture_event_was_handled_;
267 280
268 // Painting. 281 // Painting.
269 SkRect last_clip_; 282 SkRect last_clip_;
270 283
271 // Accelerators. 284 // Accelerators.
272 std::map<ui::Accelerator, int> accelerator_count_map_; 285 std::map<ui::Accelerator, int> accelerator_count_map_;
273 286
274 // Native theme. 287 // Native theme.
275 const ui::NativeTheme* native_theme_; 288 const ui::NativeTheme* native_theme_;
289
290 // Value to return from CanProcessEventsWithinSubtree().
291 bool can_process_events_within_subtree_;
276 }; 292 };
277 293
278 // A view subclass that consumes all Gesture events for testing purposes. 294 // A view subclass that consumes all Gesture events for testing purposes.
279 class TestViewConsumeGesture : public TestView { 295 class TestViewConsumeGesture : public TestView {
280 public: 296 public:
281 TestViewConsumeGesture() : TestView() {} 297 TestViewConsumeGesture() : TestView() {}
282 virtual ~TestViewConsumeGesture() {} 298 virtual ~TestViewConsumeGesture() {}
283 299
284 protected: 300 protected:
285 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE { 301 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
(...skipping 919 matching lines...) Expand 10 before | Expand all | Expand 10 after
1205 // A mouse click on the center of |v5| and |v51| should target 1221 // A mouse click on the center of |v5| and |v51| should target
1206 // the child view. 1222 // the child view.
1207 touch_rect.SetRect(465, 215, 1, 1); 1223 touch_rect.SetRect(465, 215, 1, 1);
1208 result_view = root_view->GetEventHandlerForRect(touch_rect); 1224 result_view = root_view->GetEventHandlerForRect(touch_rect);
1209 EXPECT_EQ(v51, result_view); 1225 EXPECT_EQ(v51, result_view);
1210 result_view = NULL; 1226 result_view = NULL;
1211 1227
1212 widget->CloseNow(); 1228 widget->CloseNow();
1213 } 1229 }
1214 1230
1231 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
1232 // as expected when different views in the view hierarchy return false
1233 // when CanProcessEventsWithinSubtree() is called.
1234 TEST_F(ViewTest, CanProcessEventsWithinSubtree) {
1235 Widget* widget = new Widget;
1236 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1237 widget->Init(params);
1238 View* root_view = widget->GetRootView();
1239 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1240
1241 // Have this hierarchy of views (the coords here are in the coordinate
1242 // space of the root view):
1243 // v (0, 0, 100, 100)
1244 // - v_child (0, 0, 20, 30)
1245 // - v_grandchild (5, 5, 5, 15)
1246
1247 TestView* v = new TestView;
1248 v->SetBounds(0, 0, 100, 100);
1249 root_view->AddChildView(v);
1250 v->set_notify_enter_exit_on_child(true);
1251
1252 TestView* v_child = new TestView;
1253 v_child->SetBounds(0, 0, 20, 30);
1254 v->AddChildView(v_child);
1255
1256 TestView* v_grandchild = new TestView;
1257 v_grandchild->SetBounds(5, 5, 5, 15);
1258 v_child->AddChildView(v_grandchild);
1259
1260 v->Reset();
1261 v_child->Reset();
1262 v_grandchild->Reset();
1263
1264 // Define rects and points within the views in the hierarchy.
1265 gfx::Rect rect_in_v_grandchild(7, 7, 3, 3);
1266 gfx::Point point_in_v_grandchild(rect_in_v_grandchild.origin());
1267 gfx::Rect rect_in_v_child(12, 3, 5, 5);
1268 gfx::Point point_in_v_child(rect_in_v_child.origin());
1269 gfx::Rect rect_in_v(50, 50, 25, 30);
1270 gfx::Point point_in_v(rect_in_v.origin());
1271
1272 // When all three views return true when CanProcessEventsWithinSubtree()
1273 // is called, targeting should behave as expected.
1274
1275 View* result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1276 EXPECT_EQ(v_grandchild, result_view);
1277 result_view = NULL;
1278 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1279 EXPECT_EQ(v_grandchild, result_view);
1280 result_view = NULL;
1281
1282 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1283 EXPECT_EQ(v_child, result_view);
1284 result_view = NULL;
1285 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1286 EXPECT_EQ(v_child, result_view);
1287 result_view = NULL;
1288
1289 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1290 EXPECT_EQ(v, result_view);
1291 result_view = NULL;
1292 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1293 EXPECT_EQ(v, result_view);
1294 result_view = NULL;
1295
1296 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1297 // is called, then |v_grandchild| cannot be returned as a target.
1298
1299 v_grandchild->set_can_process_events_within_subtree(false);
1300
1301 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1302 EXPECT_EQ(v_child, result_view);
1303 result_view = NULL;
1304 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1305 EXPECT_EQ(v_child, result_view);
1306 result_view = NULL;
1307
1308 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1309 EXPECT_EQ(v_child, result_view);
1310 result_view = NULL;
1311 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1312 EXPECT_EQ(v_child, result_view);
1313 result_view = NULL;
1314
1315 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1316 EXPECT_EQ(v, result_view);
1317 result_view = NULL;
1318 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1319 EXPECT_EQ(v, result_view);
1320 result_view = NULL;
1321
1322 // When |v_child| returns false when CanProcessEventsWithinSubtree()
1323 // is called, then neither |v_child| nor |v_grandchild| can be returned
1324 // as a target (|v| should be returned as the target for each case).
1325
1326 v_grandchild->Reset();
1327 v_child->set_can_process_events_within_subtree(false);
1328
1329 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1330 EXPECT_EQ(v, result_view);
1331 result_view = NULL;
1332 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1333 EXPECT_EQ(v, result_view);
1334 result_view = NULL;
1335
1336 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1337 EXPECT_EQ(v, result_view);
1338 result_view = NULL;
1339 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1340 EXPECT_EQ(v, result_view);
1341 result_view = NULL;
1342
1343 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1344 EXPECT_EQ(v, result_view);
1345 result_view = NULL;
1346 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1347 EXPECT_EQ(v, result_view);
1348 result_view = NULL;
1349
1350 // When |v| returns false when CanProcessEventsWithinSubtree()
1351 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
1352 // as a target (|root_view| should be returned as the target for each case).
1353
1354 v_child->Reset();
1355 v->set_can_process_events_within_subtree(false);
1356
1357 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1358 EXPECT_EQ(root_view, result_view);
1359 result_view = NULL;
1360 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1361 EXPECT_EQ(root_view, result_view);
1362 result_view = NULL;
1363
1364 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1365 EXPECT_EQ(root_view, result_view);
1366 result_view = NULL;
1367 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1368 EXPECT_EQ(root_view, result_view);
1369 result_view = NULL;
1370
1371 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1372 EXPECT_EQ(root_view, result_view);
1373 result_view = NULL;
1374 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1375 EXPECT_EQ(root_view, result_view);
1376 }
1377
1215 TEST_F(ViewTest, NotifyEnterExitOnChild) { 1378 TEST_F(ViewTest, NotifyEnterExitOnChild) {
1216 Widget* widget = new Widget; 1379 Widget* widget = new Widget;
1217 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); 1380 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1218 widget->Init(params); 1381 widget->Init(params);
1219 View* root_view = widget->GetRootView(); 1382 View* root_view = widget->GetRootView();
1220 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500)); 1383 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1221 1384
1222 // Have this hierarchy of views (the coords here are in root coord): 1385 // Have this hierarchy of views (the coords here are in root coord):
1223 // v1 (0, 0, 100, 100) 1386 // v1 (0, 0, 100, 100)
1224 // - v11 (0, 0, 20, 30) 1387 // - v11 (0, 0, 20, 30)
(...skipping 2708 matching lines...) Expand 10 before | Expand all | Expand 10 after
3933 // notification. 4096 // notification.
3934 TestView* test_view_child_2 = new TestView(); 4097 TestView* test_view_child_2 = new TestView();
3935 test_view->AddChildView(test_view_child_2); 4098 test_view->AddChildView(test_view_child_2);
3936 EXPECT_TRUE(test_view_child_2->native_theme_); 4099 EXPECT_TRUE(test_view_child_2->native_theme_);
3937 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_); 4100 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_);
3938 4101
3939 widget->CloseNow(); 4102 widget->CloseNow();
3940 } 4103 }
3941 4104
3942 } // namespace views 4105 } // namespace views
OLDNEW
« ui/views/view_targeter_unittest.cc ('K') | « ui/views/view_targeter_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698