Chromium Code Reviews| 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 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #import "testing/gtest_mac.h" | 20 #import "testing/gtest_mac.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "third_party/skia/include/core/SkCanvas.h" | 22 #include "third_party/skia/include/core/SkCanvas.h" |
| 22 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h" | 23 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h" |
| 23 #import "ui/base/cocoa/window_size_constants.h" | 24 #import "ui/base/cocoa/window_size_constants.h" |
| 24 #import "ui/base/test/scoped_fake_full_keyboard_access.h" | 25 #import "ui/base/test/scoped_fake_full_keyboard_access.h" |
| 26 #import "ui/events/cocoa/cocoa_event_utils.h" | |
| 25 #import "ui/events/test/cocoa_test_event_utils.h" | 27 #import "ui/events/test/cocoa_test_event_utils.h" |
| 26 #include "ui/events/test/event_generator.h" | 28 #include "ui/events/test/event_generator.h" |
| 27 #import "ui/gfx/mac/coordinate_conversion.h" | 29 #import "ui/gfx/mac/coordinate_conversion.h" |
| 28 #include "ui/views/bubble/bubble_dialog_delegate.h" | 30 #include "ui/views/bubble/bubble_dialog_delegate.h" |
| 29 #import "ui/views/cocoa/bridged_content_view.h" | 31 #import "ui/views/cocoa/bridged_content_view.h" |
| 30 #import "ui/views/cocoa/bridged_native_widget.h" | 32 #import "ui/views/cocoa/bridged_native_widget.h" |
| 31 #import "ui/views/cocoa/native_widget_mac_nswindow.h" | 33 #import "ui/views/cocoa/native_widget_mac_nswindow.h" |
| 32 #include "ui/views/controls/button/label_button.h" | 34 #include "ui/views/controls/button/label_button.h" |
| 33 #include "ui/views/controls/label.h" | 35 #include "ui/views/controls/label.h" |
| 34 #include "ui/views/controls/native/native_view_host.h" | 36 #include "ui/views/controls/native/native_view_host.h" |
| (...skipping 600 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 class MockView : public View { | |
|
tapted
2016/10/26 00:57:00
gmock isn't allowed here. You'll need to use a dif
snake
2016/10/26 12:44:56
Done.
| |
| 648 public: | |
| 649 MockView() {} | |
| 650 | |
| 651 MOCK_METHOD1(OnMousePressed, bool(const ui::MouseEvent& event)); | |
| 652 MOCK_METHOD1(OnMouseReleased, void(const ui::MouseEvent& event)); | |
| 653 MOCK_METHOD1(OnMouseDragged, bool(const ui::MouseEvent& event)); | |
| 654 MOCK_METHOD1(OnMouseMoved, void(const ui::MouseEvent& event)); | |
| 655 MOCK_METHOD1(OnMouseEntered, void(const ui::MouseEvent& event)); | |
| 656 MOCK_METHOD1(OnMouseExited, void(const ui::MouseEvent& event)); | |
| 657 MOCK_METHOD1(OnMouseWheel, bool(const ui::MouseWheelEvent& event)); | |
| 658 private: | |
| 659 DISALLOW_COPY_AND_ASSIGN(MockView); | |
| 660 }; | |
| 661 | |
| 662 // Tests that when no mouse drag is in progress the Entered / Moved / Exited | |
| 663 // notifications are sent normally. | |
| 664 TEST_F(NativeWidgetMacTest, MouseEnterExit) { | |
| 665 Widget* widget = CreateTopLevelPlatformWidget(); | |
| 666 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); | |
| 667 MockView* mock_view = new MockView(); | |
| 668 widget->GetContentsView()->AddChildView(mock_view); | |
| 669 mock_view->SetBounds(0, 0, 300, 300); | |
| 670 widget->Show(); | |
| 671 | |
| 672 // Use an event generator to ask views code to set the cursor. However, note | |
| 673 // that this does not cause Cocoa to generate tracking rectangle updates. | |
| 674 ui::test::EventGenerator event_generator(GetContext(), | |
| 675 widget->GetNativeWindow()); | |
| 676 | |
| 677 EXPECT_CALL(*mock_view, OnMouseEntered(::testing::_)).Times(1); | |
| 678 EXPECT_CALL(*mock_view, OnMouseMoved(::testing::_)).Times(1); | |
| 679 event_generator.MoveMouseTo(gfx::Point(50, 50)); | |
| 680 ::testing::Mock::VerifyAndClear(mock_view); | |
| 681 | |
| 682 EXPECT_CALL(*mock_view, OnMouseExited(::testing::_)).Times(1); | |
| 683 event_generator.MoveMouseTo(gfx::Point(-50, -50)); | |
| 684 ::testing::Mock::VerifyAndClear(mock_view); | |
| 685 | |
| 686 widget->CloseNow(); | |
| 687 } | |
| 688 | |
| 689 // Tests that we don't get Exited notification when we're handling the dragging | |
| 690 // operation. When we stop the drag outside of Widget's bounds we should get the | |
| 691 // Exited notification when the mouse capture will be released. | |
| 692 TEST_F(NativeWidgetMacTest, MouseEnterExitWithCapture) { | |
| 693 Widget* widget = CreateTopLevelNativeWidget(); | |
| 694 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); | |
| 695 MockView* mock_view = new MockView(); | |
| 696 widget->GetContentsView()->AddChildView(mock_view); | |
| 697 mock_view->SetBounds(0, 0, 300, 300); | |
| 698 widget->Show(); | |
| 699 | |
| 700 // Use an event generator to ask views code to set the cursor. However, note | |
| 701 // that this does not cause Cocoa to generate tracking rectangle updates. | |
| 702 ui::test::EventGenerator event_generator(GetContext(), | |
| 703 widget->GetNativeWindow()); | |
| 704 | |
| 705 EXPECT_CALL(*mock_view, OnMouseEntered(::testing::_)).Times(1); | |
| 706 EXPECT_CALL(*mock_view, OnMouseMoved(::testing::_)).Times(1); | |
| 707 event_generator.MoveMouseTo(gfx::Point(50, 50)); | |
| 708 ::testing::Mock::VerifyAndClear(mock_view); | |
| 709 | |
| 710 EXPECT_CALL(*mock_view, OnMousePressed(::testing::_)).Times(1); | |
| 711 ON_CALL(*mock_view, OnMousePressed(::testing::_)) | |
| 712 .WillByDefault(::testing::Return(true)); | |
| 713 | |
| 714 event_generator.PressLeftButton(); | |
| 715 ::testing::Mock::VerifyAndClear(mock_view); | |
| 716 | |
| 717 EXPECT_TRUE(widget->HasCapture()); | |
| 718 | |
| 719 // The mouse exit event should not be received, if we have capture. | |
| 720 EXPECT_CALL(*mock_view, OnMouseExited(::testing::_)).Times(0); | |
| 721 EXPECT_CALL(*mock_view, OnMouseDragged(::testing::_)).Times(1); | |
| 722 ON_CALL(*mock_view, OnMouseDragged(::testing::_)) | |
| 723 .WillByDefault(::testing::Return(true)); | |
| 724 event_generator.MoveMouseTo(gfx::Point(-50, -50)); | |
| 725 ::testing::Mock::VerifyAndClear(mock_view); | |
| 726 | |
| 727 EXPECT_CALL(*mock_view, OnMouseReleased(::testing::_)).Times(1); | |
| 728 EXPECT_CALL(*mock_view, OnMouseExited(::testing::_)).Times(1); | |
| 729 event_generator.ReleaseLeftButton(); | |
| 730 ::testing::Mock::VerifyAndClear(mock_view); | |
| 731 | |
| 732 EXPECT_FALSE(widget->HasCapture()); | |
| 733 | |
| 734 widget->CloseNow(); | |
| 735 } | |
| 736 | |
| 737 // Tests that when exited event have location inside Widget, it will be | |
| 738 // processed. | |
| 739 // TODO(art-snake): Fix ui::test::EventGenerator::SendMouseExit method. | |
| 740 TEST_F(NativeWidgetMacTest, MouseExitInsideWidget) { | |
| 741 Widget* widget = CreateTopLevelPlatformWidget(); | |
| 742 widget->SetBounds(gfx::Rect(0, 0, 300, 300)); | |
| 743 MockView* mock_view = new MockView(); | |
| 744 widget->GetContentsView()->AddChildView(mock_view); | |
| 745 mock_view->SetBounds(0, 0, 300, 300); | |
| 746 widget->Show(); | |
| 747 | |
| 748 ui::test::EventGenerator event_generator(GetContext(), | |
| 749 widget->GetNativeWindow()); | |
| 750 | |
| 751 EXPECT_CALL(*mock_view, OnMouseEntered(::testing::_)).Times(1); | |
| 752 EXPECT_CALL(*mock_view, OnMouseMoved(::testing::_)).Times(1); | |
| 753 event_generator.MoveMouseTo(gfx::Point(50, 50)); | |
| 754 ::testing::Mock::VerifyAndClear(mock_view); | |
| 755 | |
| 756 EXPECT_CALL(*mock_view, OnMouseExited(::testing::_)).Times(1); | |
| 757 event_generator.SendMouseExit(); | |
| 758 ::testing::Mock::VerifyAndClear(mock_view); | |
| 759 | |
| 760 widget->CloseNow(); | |
| 761 } | |
| 762 | |
| 763 TEST_F(NativeWidgetMacTest, MouseWheelEvent) { | |
| 764 Widget* widget = CreateTopLevelPlatformWidget(); | |
| 765 widget->SetBounds(gfx::Rect(0, 0, 600, 600)); | |
| 766 MockView* mock_view = new MockView(); | |
| 767 widget->GetContentsView()->AddChildView(mock_view); | |
| 768 mock_view->SetBounds(0, 0, 600, 600); | |
| 769 widget->Show(); | |
| 770 | |
| 771 ui::test::EventGenerator event_generator(GetContext(), | |
| 772 widget->GetNativeWindow()); | |
| 773 EXPECT_CALL(*mock_view, OnMouseWheel(::testing::_)).Times(1); | |
| 774 event_generator.MoveMouseWheel(1, 1); | |
| 775 ::testing::Mock::VerifyAndClear(mock_view); | |
| 776 | |
| 777 widget->CloseNow(); | |
| 778 } | |
| 779 | |
| 645 // Tests that an accessibility request from the system makes its way through to | 780 // Tests that an accessibility request from the system makes its way through to |
| 646 // a views::Label filling the window. | 781 // a views::Label filling the window. |
| 647 TEST_F(NativeWidgetMacTest, AccessibilityIntegration) { | 782 TEST_F(NativeWidgetMacTest, AccessibilityIntegration) { |
| 648 Widget* widget = CreateTopLevelPlatformWidget(); | 783 Widget* widget = CreateTopLevelPlatformWidget(); |
| 649 gfx::Rect screen_rect(50, 50, 100, 100); | 784 gfx::Rect screen_rect(50, 50, 100, 100); |
| 650 widget->SetBounds(screen_rect); | 785 widget->SetBounds(screen_rect); |
| 651 | 786 |
| 652 const base::string16 test_string = base::ASCIIToUTF16("Green"); | 787 const base::string16 test_string = base::ASCIIToUTF16("Green"); |
| 653 views::Label* label = new views::Label(test_string); | 788 views::Label* label = new views::Label(test_string); |
| 654 label->SetBounds(0, 0, 100, 100); | 789 label->SetBounds(0, 0, 100, 100); |
| (...skipping 1138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1793 | 1928 |
| 1794 - (void)dealloc { | 1929 - (void)dealloc { |
| 1795 if (deallocFlag_) { | 1930 if (deallocFlag_) { |
| 1796 DCHECK(!*deallocFlag_); | 1931 DCHECK(!*deallocFlag_); |
| 1797 *deallocFlag_ = true; | 1932 *deallocFlag_ = true; |
| 1798 } | 1933 } |
| 1799 [super dealloc]; | 1934 [super dealloc]; |
| 1800 } | 1935 } |
| 1801 | 1936 |
| 1802 @end | 1937 @end |
| OLD | NEW |