| OLD | NEW |
| 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/widget/native_widget_mac.h" | 5 #import "ui/views/widget/native_widget_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #import "base/mac/foundation_util.h" | 9 #import "base/mac/foundation_util.h" |
| 10 #import "base/mac/scoped_nsautorelease_pool.h" | 10 #import "base/mac/scoped_nsautorelease_pool.h" |
| 11 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
| 12 #import "base/mac/scoped_objc_class_swizzler.h" | 12 #import "base/mac/scoped_objc_class_swizzler.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/test/test_timeouts.h" | 17 #include "base/test/test_timeouts.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #import "testing/gtest_mac.h" | 19 #import "testing/gtest_mac.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "third_party/skia/include/core/SkCanvas.h" | 21 #include "third_party/skia/include/core/SkCanvas.h" |
| 22 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h" | 22 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h" |
| 23 #import "ui/base/cocoa/window_size_constants.h" | 23 #import "ui/base/cocoa/window_size_constants.h" |
| 24 #import "ui/base/test/scoped_fake_full_keyboard_access.h" | 24 #import "ui/base/test/scoped_fake_full_keyboard_access.h" |
| 25 #import "ui/events/cocoa/cocoa_event_utils.h" |
| 25 #import "ui/events/test/cocoa_test_event_utils.h" | 26 #import "ui/events/test/cocoa_test_event_utils.h" |
| 26 #include "ui/events/test/event_generator.h" | 27 #include "ui/events/test/event_generator.h" |
| 27 #import "ui/gfx/mac/coordinate_conversion.h" | 28 #import "ui/gfx/mac/coordinate_conversion.h" |
| 28 #include "ui/views/bubble/bubble_dialog_delegate.h" | 29 #include "ui/views/bubble/bubble_dialog_delegate.h" |
| 29 #import "ui/views/cocoa/bridged_content_view.h" | 30 #import "ui/views/cocoa/bridged_content_view.h" |
| 30 #import "ui/views/cocoa/bridged_native_widget.h" | 31 #import "ui/views/cocoa/bridged_native_widget.h" |
| 31 #import "ui/views/cocoa/native_widget_mac_nswindow.h" | 32 #import "ui/views/cocoa/native_widget_mac_nswindow.h" |
| 32 #include "ui/views/controls/button/label_button.h" | 33 #include "ui/views/controls/button/label_button.h" |
| 33 #include "ui/views/controls/label.h" | 34 #include "ui/views/controls/label.h" |
| 34 #include "ui/views/controls/native/native_view_host.h" | 35 #include "ui/views/controls/native/native_view_host.h" |
| 35 #include "ui/views/native_cursor.h" | 36 #include "ui/views/native_cursor.h" |
| 36 #include "ui/views/test/native_widget_factory.h" | 37 #include "ui/views/test/native_widget_factory.h" |
| 38 #include "ui/views/test/test_views.h" |
| 37 #include "ui/views/test/test_widget_observer.h" | 39 #include "ui/views/test/test_widget_observer.h" |
| 38 #include "ui/views/test/widget_test.h" | 40 #include "ui/views/test/widget_test.h" |
| 39 #include "ui/views/widget/native_widget_mac.h" | 41 #include "ui/views/widget/native_widget_mac.h" |
| 40 #include "ui/views/widget/native_widget_private.h" | 42 #include "ui/views/widget/native_widget_private.h" |
| 41 #include "ui/views/window/dialog_delegate.h" | 43 #include "ui/views/window/dialog_delegate.h" |
| 42 | 44 |
| 43 // Donates an implementation of -[NSAnimation stopAnimation] which calls the | 45 // Donates an implementation of -[NSAnimation stopAnimation] which calls the |
| 44 // original implementation, then quits a nested run loop. | 46 // original implementation, then quits a nested run loop. |
| 45 @interface TestStopAnimationWaiter : NSObject | 47 @interface TestStopAnimationWaiter : NSObject |
| 46 @end | 48 @end |
| (...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 | 637 |
| 636 // Moving to the third view (but remaining in the content area) should also | 638 // Moving to the third view (but remaining in the content area) should also |
| 637 // forward to the native NSWindow implementation. | 639 // forward to the native NSWindow implementation. |
| 638 event_generator.MoveMouseTo(gfx::Point(250, 50)); | 640 event_generator.MoveMouseTo(gfx::Point(250, 50)); |
| 639 [widget->GetNativeWindow() cursorUpdate:event_in_content]; | 641 [widget->GetNativeWindow() cursorUpdate:event_in_content]; |
| 640 EXPECT_EQ(arrow, [NSCursor currentCursor]); | 642 EXPECT_EQ(arrow, [NSCursor currentCursor]); |
| 641 | 643 |
| 642 widget->CloseNow(); | 644 widget->CloseNow(); |
| 643 } | 645 } |
| 644 | 646 |
| 647 // Tests that when no mouse drag is in progress the Entered / Moved / Exited |
| 648 // notifications are sent normally. |
| 649 TEST_F(NativeWidgetMacTest, MouseEnterExit) { |
| 650 Widget* widget = CreateTopLevelPlatformWidget(); |
| 651 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); |
| 652 EventCountView* mock_view = new EventCountView(); |
| 653 widget->GetContentsView()->AddChildView(mock_view); |
| 654 mock_view->SetBounds(0, 0, 300, 300); |
| 655 widget->Show(); |
| 656 |
| 657 // Use an event generator to ask views code to set the cursor. However, note |
| 658 // that this does not cause Cocoa to generate tracking rectangle updates. |
| 659 ui::test::EventGenerator event_generator(GetContext(), |
| 660 widget->GetNativeWindow()); |
| 661 |
| 662 |
| 663 event_generator.MoveMouseTo(gfx::Point(50, 50)); |
| 664 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_ENTERED)); |
| 665 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_MOVED)); |
| 666 mock_view->ResetCounts(); |
| 667 |
| 668 event_generator.MoveMouseTo(gfx::Point(-50, -50)); |
| 669 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_EXITED)); |
| 670 mock_view->ResetCounts(); |
| 671 |
| 672 widget->CloseNow(); |
| 673 } |
| 674 |
| 675 // Tests that we don't get Exited notification when we're handling the dragging |
| 676 // operation. When we stop the drag outside of Widget's bounds we should get the |
| 677 // Exited notification when the mouse capture will be released. |
| 678 TEST_F(NativeWidgetMacTest, MouseEnterExitWithCapture) { |
| 679 Widget* widget = CreateTopLevelNativeWidget(); |
| 680 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); |
| 681 EventCountView* mock_view = new EventCountView(); |
| 682 widget->GetContentsView()->AddChildView(mock_view); |
| 683 mock_view->SetBounds(0, 0, 300, 300); |
| 684 widget->Show(); |
| 685 |
| 686 // Use an event generator to ask views code to set the cursor. However, note |
| 687 // that this does not cause Cocoa to generate tracking rectangle updates. |
| 688 ui::test::EventGenerator event_generator(GetContext(), |
| 689 widget->GetNativeWindow()); |
| 690 |
| 691 event_generator.MoveMouseTo(gfx::Point(50, 50)); |
| 692 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_ENTERED)); |
| 693 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_MOVED)); |
| 694 mock_view->ResetCounts(); |
| 695 |
| 696 |
| 697 mock_view->set_handle_mode(EventCountView::CONSUME_EVENTS); |
| 698 event_generator.PressLeftButton(); |
| 699 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_PRESSED)); |
| 700 mock_view->set_handle_mode(EventCountView::PROPAGATE_EVENTS); |
| 701 mock_view->ResetCounts(); |
| 702 |
| 703 EXPECT_TRUE(widget->HasCapture()); |
| 704 |
| 705 mock_view->set_handle_mode(EventCountView::CONSUME_EVENTS); |
| 706 event_generator.MoveMouseTo(gfx::Point(-50, -50)); |
| 707 // The mouse exit event should not be received, if we have capture. |
| 708 EXPECT_EQ(0, mock_view->GetEventCount(ui::ET_MOUSE_EXITED)); |
| 709 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_DRAGGED)); |
| 710 mock_view->set_handle_mode(EventCountView::PROPAGATE_EVENTS); |
| 711 mock_view->ResetCounts(); |
| 712 |
| 713 event_generator.ReleaseLeftButton(); |
| 714 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_RELEASED)); |
| 715 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_EXITED)); |
| 716 mock_view->ResetCounts(); |
| 717 |
| 718 EXPECT_FALSE(widget->HasCapture()); |
| 719 |
| 720 widget->CloseNow(); |
| 721 } |
| 722 |
| 723 // Tests that when exited event have location inside Widget, it will be |
| 724 // processed. |
| 725 // TODO(art-snake): Fix ui::test::EventGenerator::SendMouseExit method. |
| 726 TEST_F(NativeWidgetMacTest, MouseExitInsideWidget) { |
| 727 Widget* widget = CreateTopLevelPlatformWidget(); |
| 728 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); |
| 729 EventCountView* mock_view = new EventCountView(); |
| 730 widget->GetContentsView()->AddChildView(mock_view); |
| 731 mock_view->SetBounds(0, 0, 300, 300); |
| 732 widget->Show(); |
| 733 |
| 734 ui::test::EventGenerator event_generator(GetContext(), |
| 735 widget->GetNativeWindow()); |
| 736 |
| 737 event_generator.MoveMouseTo(gfx::Point(50, 50)); |
| 738 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_ENTERED)); |
| 739 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_MOVED)); |
| 740 mock_view->ResetCounts(); |
| 741 |
| 742 event_generator.SendMouseExit(); |
| 743 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSE_EXITED)); |
| 744 mock_view->ResetCounts(); |
| 745 |
| 746 widget->CloseNow(); |
| 747 } |
| 748 |
| 749 TEST_F(NativeWidgetMacTest, MouseWheelEvent) { |
| 750 Widget* widget = CreateTopLevelPlatformWidget(); |
| 751 widget->SetBounds(gfx::Rect(0, 0, 600, 600)); |
| 752 EventCountView* mock_view = new EventCountView(); |
| 753 widget->GetContentsView()->AddChildView(mock_view); |
| 754 mock_view->SetBounds(0, 0, 600, 600); |
| 755 widget->Show(); |
| 756 |
| 757 ui::test::EventGenerator event_generator(GetContext(), |
| 758 widget->GetNativeWindow()); |
| 759 |
| 760 event_generator.MoveMouseWheel(1, 1); |
| 761 EXPECT_EQ(1, mock_view->GetEventCount(ui::ET_MOUSEWHEEL)); |
| 762 mock_view->ResetCounts(); |
| 763 |
| 764 widget->CloseNow(); |
| 765 } |
| 766 |
| 645 // Tests that an accessibility request from the system makes its way through to | 767 // Tests that an accessibility request from the system makes its way through to |
| 646 // a views::Label filling the window. | 768 // a views::Label filling the window. |
| 647 TEST_F(NativeWidgetMacTest, AccessibilityIntegration) { | 769 TEST_F(NativeWidgetMacTest, AccessibilityIntegration) { |
| 648 Widget* widget = CreateTopLevelPlatformWidget(); | 770 Widget* widget = CreateTopLevelPlatformWidget(); |
| 649 gfx::Rect screen_rect(50, 50, 100, 100); | 771 gfx::Rect screen_rect(50, 50, 100, 100); |
| 650 widget->SetBounds(screen_rect); | 772 widget->SetBounds(screen_rect); |
| 651 | 773 |
| 652 const base::string16 test_string = base::ASCIIToUTF16("Green"); | 774 const base::string16 test_string = base::ASCIIToUTF16("Green"); |
| 653 views::Label* label = new views::Label(test_string); | 775 views::Label* label = new views::Label(test_string); |
| 654 label->SetBounds(0, 0, 100, 100); | 776 label->SetBounds(0, 0, 100, 100); |
| (...skipping 1138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1793 | 1915 |
| 1794 - (void)dealloc { | 1916 - (void)dealloc { |
| 1795 if (deallocFlag_) { | 1917 if (deallocFlag_) { |
| 1796 DCHECK(!*deallocFlag_); | 1918 DCHECK(!*deallocFlag_); |
| 1797 *deallocFlag_ = true; | 1919 *deallocFlag_ = true; |
| 1798 } | 1920 } |
| 1799 [super dealloc]; | 1921 [super dealloc]; |
| 1800 } | 1922 } |
| 1801 | 1923 |
| 1802 @end | 1924 @end |
| OLD | NEW |