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

Side by Side Diff: ui/views/cocoa/bridged_native_widget_interactive_uitest.mm

Issue 2475173002: MacViews: Fix window dragging on Sierra. (Closed)
Patch Set: Rebase. Update comment. #include=>#import. Created 4 years, 1 month 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
« no previous file with comments | « ui/views/cocoa/bridged_native_widget.mm ('k') | ui/views/cocoa/native_widget_mac_nswindow.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #import "ui/views/cocoa/bridged_native_widget.h" 5 #import "ui/views/cocoa/bridged_native_widget.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #import "base/mac/mac_util.h" 9 #import "base/mac/mac_util.h"
10 #import "base/mac/sdk_forward_declarations.h" 10 #import "base/mac/sdk_forward_declarations.h"
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 namespace { 191 namespace {
192 192
193 // This is used to wait for reposted events to be seen. We can't just use 193 // This is used to wait for reposted events to be seen. We can't just use
194 // RunPendingMessages() because CGEventPost might not be synchronous. 194 // RunPendingMessages() because CGEventPost might not be synchronous.
195 class HitTestBridgedNativeWidget : public BridgedNativeWidget { 195 class HitTestBridgedNativeWidget : public BridgedNativeWidget {
196 public: 196 public:
197 explicit HitTestBridgedNativeWidget(NativeWidgetMac* widget) 197 explicit HitTestBridgedNativeWidget(NativeWidgetMac* widget)
198 : BridgedNativeWidget(widget) {} 198 : BridgedNativeWidget(widget) {}
199 199
200 // BridgedNativeWidget: 200 // BridgedNativeWidget:
201 bool ShouldRepostPendingLeftMouseDown(NSPoint location_in_window) override { 201 bool ShouldRepostPendingLeftMouseDown(NSEvent* event) override {
202 did_repost_ = BridgedNativeWidget::ShouldRepostPendingLeftMouseDown( 202 did_repost_ = BridgedNativeWidget::ShouldRepostPendingLeftMouseDown(event);
203 location_in_window);
204 203
205 if (run_loop_) 204 if (run_loop_)
206 run_loop_->Quit(); 205 run_loop_->Quit();
207 206
208 return did_repost_; 207 return did_repost_;
209 } 208 }
210 209
211 void WaitForShouldRepost() { 210 void WaitForShouldRepost() {
212 base::RunLoop run_loop; 211 base::RunLoop run_loop;
213 run_loop_ = &run_loop; 212 run_loop_ = &run_loop;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 test::HitTestNativeWidgetMac* native_widget = 315 test::HitTestNativeWidgetMac* native_widget =
317 new test::HitTestNativeWidgetMac(&widget, frame_view); 316 new test::HitTestNativeWidgetMac(&widget, frame_view);
318 HitTestWidgetDelegate* widget_delegate = new HitTestWidgetDelegate(&widget); 317 HitTestWidgetDelegate* widget_delegate = new HitTestWidgetDelegate(&widget);
319 Widget::InitParams init_params = 318 Widget::InitParams init_params =
320 CreateParams(Widget::InitParams::TYPE_WINDOW); 319 CreateParams(Widget::InitParams::TYPE_WINDOW);
321 init_params.native_widget = native_widget; 320 init_params.native_widget = native_widget;
322 init_params.delegate = widget_delegate; 321 init_params.delegate = widget_delegate;
323 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 322 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
324 init_params.bounds = gfx::Rect(100, 200, 400, 300); 323 init_params.bounds = gfx::Rect(100, 200, 400, 300);
325 widget.Init(init_params); 324 widget.Init(init_params);
325
326 WidgetActivationWaiter activation_waiter(&widget, true);
326 widget.Show(); 327 widget.Show();
328 activation_waiter.Wait();
327 329
328 // Points inside the resize area. 330 // Points inside the resize area.
329 const NSPoint bottom_right_point = {398, 2}; 331 const NSPoint bottom_right_point = {398, 2};
330 const NSPoint right_of_bottom_right = {398 + 10, 2}; 332 const NSPoint right_of_bottom_right = {398 + 10, 2};
331 333
332 NSWindow* window = widget.GetNativeWindow(); 334 NSWindow* window = widget.GetNativeWindow();
335
336 EXPECT_FALSE([window ignoresMouseEvents]);
337 // OSX uses both the alpha value of the window and the underlying CALayer to
338 // decide whether to send mouse events to window, in case [window
339 // ignoresMouseEvent] is not explicitly initialized. Since, no frames are
340 // drawn during tests and the underlying CALayer has a transparent background,
341 // explicitly call setIgnoresMouseEvents: to ensure the window receives the
342 // mouse events.
343 [window setIgnoresMouseEvents:NO];
344
333 HitTestBridgedNativeWidget* bridge = native_widget->bridge(); 345 HitTestBridgedNativeWidget* bridge = native_widget->bridge();
334 346
347 const bool using_drag_event_monitor =
348 BridgedNativeWidget::ShouldUseDragEventMonitor();
349
335 // Dragging the window should work. 350 // Dragging the window should work.
336 frame_view->set_hit_test_result(HTCAPTION); 351 frame_view->set_hit_test_result(HTCAPTION);
337 { 352 {
338 EXPECT_EQ(100, [window frame].origin.x); 353 EXPECT_EQ(100, [window frame].origin.x);
339 354
355 base::scoped_nsobject<WindowedNSNotificationObserver> will_move_observer(
356 [[WindowedNSNotificationObserver alloc]
357 initForNotification:NSWindowWillMoveNotification]);
340 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow( 358 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
341 NSMakePoint(20, 20), window); 359 NSMakePoint(20, 20), window);
342 EXPECT_FALSE(bridge->IsDraggable()); 360 EXPECT_FALSE(bridge->IsDraggable());
343 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]); 361 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]);
344 bridge->WaitForShouldRepost(); 362 if (using_drag_event_monitor) {
345 EXPECT_TRUE(bridge->did_repost()); 363 bridge->WaitForShouldRepost();
346 EXPECT_TRUE(bridge->IsDraggable()); 364 EXPECT_TRUE(bridge->did_repost());
347 bridge->WaitForShouldRepost(); 365 EXPECT_TRUE(bridge->IsDraggable());
348 EXPECT_FALSE(bridge->did_repost()); 366 bridge->WaitForShouldRepost();
349 EXPECT_FALSE(bridge->IsDraggable()); 367 EXPECT_FALSE(bridge->did_repost());
368 EXPECT_FALSE(bridge->IsDraggable());
369 } else {
370 WaitForEvent(NSLeftMouseDownMask);
371 }
350 372
351 base::scoped_nsobject<WindowedNSNotificationObserver> ns_observer( 373 base::scoped_nsobject<WindowedNSNotificationObserver> did_move_observer(
352 [[WindowedNSNotificationObserver alloc] 374 [[WindowedNSNotificationObserver alloc]
353 initForNotification:NSWindowDidMoveNotification]); 375 initForNotification:NSWindowDidMoveNotification]);
354 NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPointInWindow( 376 NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPointInWindow(
355 NSMakePoint(30, 30), NSLeftMouseDragged, window, 0); 377 NSMakePoint(30, 30), NSLeftMouseDragged, window, 0);
356 CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]); 378 CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]);
357 [ns_observer wait]; 379 WaitForEvent(NSLeftMouseDraggedMask);
380 // NSWindowWillMoveNotification should have been observed by the time the
381 // mouse drag event is received.
382 EXPECT_EQ(1, [will_move_observer notificationCount]);
383 EXPECT_TRUE([did_move_observer wait]);
358 EXPECT_EQ(110, [window frame].origin.x); 384 EXPECT_EQ(110, [window frame].origin.x);
359 385
360 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow( 386 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow(
361 NSMakePoint(20, 20), NSLeftMouseUp, window, 0); 387 NSMakePoint(20, 20), NSLeftMouseUp, window, 0);
362 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]); 388 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]);
363 WaitForEvent(NSLeftMouseUpMask); 389 WaitForEvent(NSLeftMouseUpMask);
364 EXPECT_EQ(110, [window frame].origin.x); 390 EXPECT_EQ(110, [window frame].origin.x);
365 } 391 }
366 392
367 // Dragging in the resize area works since the widget is not resizable. 393 // Dragging in the resize area works since the widget is not resizable.
368 { 394 {
369 EXPECT_EQ(110, [window frame].origin.x); 395 EXPECT_EQ(110, [window frame].origin.x);
370 396
397 base::scoped_nsobject<WindowedNSNotificationObserver> will_move_observer(
398 [[WindowedNSNotificationObserver alloc]
399 initForNotification:NSWindowWillMoveNotification]);
371 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow( 400 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
372 bottom_right_point, window); 401 bottom_right_point, window);
373 EXPECT_FALSE(bridge->IsDraggable()); 402 EXPECT_FALSE(bridge->IsDraggable());
374 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]); 403 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]);
375 bridge->WaitForShouldRepost(); 404 if (using_drag_event_monitor) {
376 EXPECT_TRUE(bridge->did_repost()); 405 bridge->WaitForShouldRepost();
377 EXPECT_TRUE(bridge->IsDraggable()); 406 EXPECT_TRUE(bridge->did_repost());
378 bridge->WaitForShouldRepost(); 407 EXPECT_TRUE(bridge->IsDraggable());
379 EXPECT_FALSE(bridge->did_repost()); 408 bridge->WaitForShouldRepost();
380 EXPECT_FALSE(bridge->IsDraggable()); 409 EXPECT_FALSE(bridge->did_repost());
410 EXPECT_FALSE(bridge->IsDraggable());
411 } else {
412 WaitForEvent(NSLeftMouseDownMask);
413 }
381 414
382 base::scoped_nsobject<WindowedNSNotificationObserver> ns_observer( 415 base::scoped_nsobject<WindowedNSNotificationObserver> did_move_observer(
383 [[WindowedNSNotificationObserver alloc] 416 [[WindowedNSNotificationObserver alloc]
384 initForNotification:NSWindowDidMoveNotification]); 417 initForNotification:NSWindowDidMoveNotification]);
385 NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPointInWindow( 418 NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPointInWindow(
386 right_of_bottom_right, NSLeftMouseDragged, window, 0); 419 right_of_bottom_right, NSLeftMouseDragged, window, 0);
387 CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]); 420 CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]);
388 [ns_observer wait]; 421 WaitForEvent(NSLeftMouseDraggedMask);
422 EXPECT_EQ(1, [will_move_observer notificationCount]);
423 EXPECT_TRUE([did_move_observer wait]);
389 EXPECT_EQ(120, [window frame].origin.x); 424 EXPECT_EQ(120, [window frame].origin.x);
390 425
391 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow( 426 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow(
392 bottom_right_point, NSLeftMouseUp, window, 0); 427 bottom_right_point, NSLeftMouseUp, window, 0);
393 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]); 428 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]);
394 WaitForEvent(NSLeftMouseUpMask); 429 WaitForEvent(NSLeftMouseUpMask);
395 EXPECT_EQ(120, [window frame].origin.x); 430 EXPECT_EQ(120, [window frame].origin.x);
396 } 431 }
397 432
398 // If the widget is resizable, dragging in the resize area should not repost 433 // If the widget is resizable, dragging in the resize area should not repost
399 // (and should resize). 434 // (and should resize).
400 widget_delegate->set_can_resize(true); 435 widget_delegate->set_can_resize(true);
401 { 436 {
402 EXPECT_EQ(400, [window frame].size.width); 437 EXPECT_EQ(400, [window frame].size.width);
403 438
404 NSUInteger x = [window frame].origin.x; 439 NSUInteger x = [window frame].origin.x;
405 NSUInteger y = [window frame].origin.y; 440 NSUInteger y = [window frame].origin.y;
406 441
407 // Enqueue all mouse events first because AppKit will run its own loop to 442 // Enqueue all mouse events first because AppKit will run its own loop to
408 // consume them. 443 // consume them.
409 444 base::scoped_nsobject<WindowedNSNotificationObserver> will_move_observer(
445 [[WindowedNSNotificationObserver alloc]
446 initForNotification:NSWindowWillMoveNotification]);
410 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow( 447 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
411 bottom_right_point, window); 448 bottom_right_point, window);
412 EXPECT_FALSE(bridge->IsDraggable()); 449 EXPECT_FALSE(bridge->IsDraggable());
413 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]); 450 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]);
414 451
452 base::scoped_nsobject<WindowedNSNotificationObserver> did_resize_observer(
453 [[WindowedNSNotificationObserver alloc]
454 initForNotification:NSWindowDidResizeNotification]);
415 NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPoint( 455 NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPoint(
416 NSMakePoint(x + 408, y + 2), NSLeftMouseDragged, 0); 456 NSMakePoint(x + 408, y + 2), NSLeftMouseDragged, 0);
417 CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]); 457 CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]);
418 458
419 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPoint( 459 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPoint(
420 NSMakePoint(x + 408, y + 2), NSLeftMouseUp, 0); 460 NSMakePoint(x + 408, y + 2), NSLeftMouseUp, 0);
421 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]); 461 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]);
422 462
423 // The only event observed by us is the original mouse-down. It should not 463 if (using_drag_event_monitor) {
424 // be reposted. 464 // The only event observed by us is the original mouse-down. It should not
425 bridge->WaitForShouldRepost(); 465 // be reposted.
426 EXPECT_FALSE(bridge->did_repost()); 466 bridge->WaitForShouldRepost();
427 EXPECT_FALSE(bridge->IsDraggable()); 467 EXPECT_FALSE(bridge->did_repost());
468 EXPECT_FALSE(bridge->IsDraggable());
469 }
470
471 EXPECT_TRUE([did_resize_observer wait]);
472 EXPECT_EQ(0, [will_move_observer notificationCount]);
428 EXPECT_EQ(410, [window frame].size.width); 473 EXPECT_EQ(410, [window frame].size.width);
429 474
430 // Origin is unchanged. 475 // Origin is unchanged.
431 EXPECT_EQ(x, [window frame].origin.x); 476 EXPECT_EQ(x, [window frame].origin.x);
432 EXPECT_EQ(y, [window frame].origin.y); 477 EXPECT_EQ(y, [window frame].origin.y);
433 } 478 }
434 479
435 // Mouse-downs on the window controls should not be intercepted. 480 // Mouse-downs on the window controls should not be intercepted.
436 { 481 {
437 EXPECT_EQ(120, [window frame].origin.x); 482 EXPECT_EQ(120, [window frame].origin.x);
438 483
439 base::scoped_nsobject<WindowedNSNotificationObserver> ns_observer( 484 base::scoped_nsobject<WindowedNSNotificationObserver> will_move_observer(
440 [[WindowedNSNotificationObserver alloc] 485 [[WindowedNSNotificationObserver alloc]
486 initForNotification:NSWindowWillMoveNotification]);
487 base::scoped_nsobject<WindowedNSNotificationObserver>
488 did_miniaturize_observer([[WindowedNSNotificationObserver alloc]
441 initForNotification:NSWindowDidMiniaturizeNotification]); 489 initForNotification:NSWindowDidMiniaturizeNotification]);
442 490
443 // Position this on the minimize button. 491 // Position this on the minimize button.
444 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow( 492 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
445 NSMakePoint(30, 290), window); 493 NSMakePoint(30, 290), window);
446 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]); 494 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]);
447 495
448 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow( 496 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow(
449 NSMakePoint(30, 290), NSLeftMouseUp, window, 0); 497 NSMakePoint(30, 290), NSLeftMouseUp, window, 0);
450 EXPECT_FALSE([window isMiniaturized]); 498 EXPECT_FALSE([window isMiniaturized]);
451 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]); 499 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]);
452 [ns_observer wait]; 500 [did_miniaturize_observer wait];
501 EXPECT_EQ(0, [will_move_observer notificationCount]);
453 EXPECT_TRUE([window isMiniaturized]); 502 EXPECT_TRUE([window isMiniaturized]);
454 [window deminiaturize:nil]; 503 [window deminiaturize:nil];
455 504
456 // Position unchanged. 505 // Position unchanged.
457 EXPECT_EQ(120, [window frame].origin.x); 506 EXPECT_EQ(120, [window frame].origin.x);
458 } 507 }
459 508
460 // Non-draggable areas should do nothing. 509 // Non-draggable areas should do nothing.
461 frame_view->set_hit_test_result(HTCLIENT); 510 frame_view->set_hit_test_result(HTCLIENT);
462 { 511 {
463 EXPECT_EQ(120, [window frame].origin.x); 512 EXPECT_EQ(120, [window frame].origin.x);
464 513
514 base::scoped_nsobject<WindowedNSNotificationObserver> will_move_observer(
515 [[WindowedNSNotificationObserver alloc]
516 initForNotification:NSWindowWillMoveNotification]);
465 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow( 517 NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
466 NSMakePoint(20, 20), window); 518 NSMakePoint(20, 20), window);
467 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]); 519 CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]);
468 WaitForEvent(NSLeftMouseDownMask); 520 WaitForEvent(NSLeftMouseDownMask);
469 521
470 NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPointInWindow( 522 NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPointInWindow(
471 NSMakePoint(30, 30), NSLeftMouseDragged, window, 0); 523 NSMakePoint(30, 30), NSLeftMouseDragged, window, 0);
472 CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]); 524 CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]);
473 WaitForEvent(NSLeftMouseDraggedMask); 525 WaitForEvent(NSLeftMouseDraggedMask);
526 EXPECT_EQ(0, [will_move_observer notificationCount]);
474 EXPECT_EQ(120, [window frame].origin.x); 527 EXPECT_EQ(120, [window frame].origin.x);
475 528
476 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow( 529 NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow(
477 NSMakePoint(30, 30), NSLeftMouseUp, window, 0); 530 NSMakePoint(30, 30), NSLeftMouseUp, window, 0);
478 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]); 531 CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]);
479 WaitForEvent(NSLeftMouseUpMask); 532 WaitForEvent(NSLeftMouseUpMask);
480 EXPECT_EQ(120, [window frame].origin.x); 533 EXPECT_EQ(120, [window frame].origin.x);
481 } 534 }
482 } 535 }
483 536
484 } // namespace test 537 } // namespace test
485 } // namespace views 538 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/cocoa/bridged_native_widget.mm ('k') | ui/views/cocoa/native_widget_mac_nswindow.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698