Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #import <ApplicationServices/ApplicationServices.h> | 5 #import <ApplicationServices/ApplicationServices.h> |
| 6 #import <Cocoa/Cocoa.h> | 6 #import <Cocoa/Cocoa.h> |
| 7 | 7 |
| 8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
| 10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 class MockDecoration : public LocationBarDecoration { | 32 class MockDecoration : public LocationBarDecoration { |
| 33 public: | 33 public: |
| 34 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } | 34 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } |
| 35 | 35 |
| 36 virtual void DrawInFrame(NSRect frame, NSView* control_view) { ; } | 36 virtual void DrawInFrame(NSRect frame, NSView* control_view) { ; } |
| 37 MOCK_METHOD0(AcceptsMousePress, bool()); | 37 MOCK_METHOD0(AcceptsMousePress, bool()); |
| 38 MOCK_METHOD2(OnMousePressed, bool(NSRect frame, NSPoint location)); | 38 MOCK_METHOD2(OnMousePressed, bool(NSRect frame, NSPoint location)); |
| 39 MOCK_METHOD0(GetMenu, NSMenu*()); | 39 MOCK_METHOD0(GetMenu, NSMenu*()); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 class MockButtonDecoration : public ButtonDecoration { | |
| 43 public: | |
| 44 // Note: It does not matter which images are used here - but ButtonDecoration | |
| 45 // needs _some_ images to work properly. | |
| 46 MockButtonDecoration() | |
| 47 : ButtonDecoration(IMAGE_GRID(IDR_OMNIBOX_EV_BUBBLE), | |
| 48 IDR_OMNIBOX_EV_BUBBLE_CENTER, | |
| 49 IMAGE_GRID(IDR_OMNIBOX_EV_BUBBLE), | |
| 50 IDR_OMNIBOX_EV_BUBBLE_CENTER, | |
| 51 IMAGE_GRID(IDR_OMNIBOX_EV_BUBBLE), | |
| 52 IDR_OMNIBOX_EV_BUBBLE_CENTER, | |
| 53 3) {} | |
| 54 void Hide() { SetVisible(false); } | |
| 55 MOCK_METHOD2(OnMousePressed, bool(NSRect frame, NSPoint location)); | |
| 56 }; | |
| 57 | |
| 58 // Mock up an incrementing event number. | 42 // Mock up an incrementing event number. |
| 59 NSUInteger eventNumber = 0; | 43 NSUInteger eventNumber = 0; |
| 60 | 44 |
| 61 // Create an event of the indicated |type| at |point| within |view|. | 45 // Create an event of the indicated |type| at |point| within |view|. |
| 62 // TODO(shess): Would be nice to have a MockApplication which provided | 46 // TODO(shess): Would be nice to have a MockApplication which provided |
| 63 // nifty accessors to create these things and inject them. It could | 47 // nifty accessors to create these things and inject them. It could |
| 64 // even provide functions for "Click and drag mouse from point A to | 48 // even provide functions for "Click and drag mouse from point A to |
| 65 // point B". | 49 // point B". |
| 66 // TODO(groby): This is very similar to cocoa_testing_utils - unify. | 50 // TODO(groby): This is very similar to cocoa_testing_utils - unify. |
| 67 NSEvent* Event(NSView* view, const NSPoint point, const NSEventType type, | 51 NSEvent* Event(NSView* view, const NSPoint point, const NSEventType type, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 | 147 |
| 164 virtual void TearDown() { | 148 virtual void TearDown() { |
| 165 // Clear the observer so that we don't show output for | 149 // Clear the observer so that we don't show output for |
| 166 // uninteresting messages to the mock (for instance, if |field_| has | 150 // uninteresting messages to the mock (for instance, if |field_| has |
| 167 // focus at the end of the test). | 151 // focus at the end of the test). |
| 168 [field_ setObserver:NULL]; | 152 [field_ setObserver:NULL]; |
| 169 | 153 |
| 170 AutocompleteTextFieldTest::TearDown(); | 154 AutocompleteTextFieldTest::TearDown(); |
| 171 } | 155 } |
| 172 | 156 |
| 157 // Returns the center point of the decoration. | |
| 158 NSPoint ClickLocationForDecoration(LocationBarDecoration* decoration) { | |
| 159 AutocompleteTextFieldCell* cell = [field_ cell]; | |
| 160 NSRect decoration_rect = | |
| 161 [cell frameForDecoration:decoration inFrame:[field_ bounds]]; | |
| 162 EXPECT_FALSE(NSIsEmptyRect(decoration_rect)); | |
| 163 return NSMakePoint(NSMidX(decoration_rect), NSMidY(decoration_rect)); | |
| 164 } | |
| 165 | |
| 166 void SendMouseClickToDecoration(LocationBarDecoration* decoration) { | |
| 167 NSPoint point = ClickLocationForDecoration(decoration); | |
| 168 NSEvent* downEvent = Event(field_, point, NSLeftMouseDown); | |
| 169 NSEvent* upEvent = Event(field_, point, NSLeftMouseUp); | |
| 170 | |
| 171 // Can't just use -sendEvent:, since that doesn't populate -currentEvent. | |
| 172 [NSApp postEvent:downEvent atStart:YES]; | |
| 173 [NSApp postEvent:upEvent atStart:NO]; | |
| 174 | |
| 175 NSEvent* next_event = [NSApp nextEventMatchingMask:NSAnyEventMask | |
| 176 untilDate:nil | |
| 177 inMode:NSDefaultRunLoopMode | |
| 178 dequeue:YES]; | |
| 179 [NSApp sendEvent:next_event]; | |
| 180 } | |
| 181 | |
| 173 StrictMock<MockAutocompleteTextFieldObserver> field_observer_; | 182 StrictMock<MockAutocompleteTextFieldObserver> field_observer_; |
| 174 }; | 183 }; |
| 175 | 184 |
| 176 // Test that we have the right cell class. | 185 // Test that we have the right cell class. |
| 177 TEST_F(AutocompleteTextFieldTest, CellClass) { | 186 TEST_F(AutocompleteTextFieldTest, CellClass) { |
| 178 EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]); | 187 EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]); |
| 179 } | 188 } |
| 180 | 189 |
| 181 // Test that becoming first responder sets things up correctly. | 190 // Test that becoming first responder sets things up correctly. |
| 182 TEST_F(AutocompleteTextFieldTest, FirstResponder) { | 191 TEST_F(AutocompleteTextFieldTest, FirstResponder) { |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); | 596 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); |
| 588 | 597 |
| 589 // Generate another click on the decoration. | 598 // Generate another click on the decoration. |
| 590 downEvent = Event(field_, location, NSLeftMouseDown, 1); | 599 downEvent = Event(field_, location, NSLeftMouseDown, 1); |
| 591 upEvent = Event(field_, location, NSLeftMouseUp, 1); | 600 upEvent = Event(field_, location, NSLeftMouseUp, 1); |
| 592 [NSApp postEvent:upEvent atStart:YES]; | 601 [NSApp postEvent:upEvent atStart:YES]; |
| 593 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_, _)) | 602 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_, _)) |
| 594 .WillOnce(Return(true)); | 603 .WillOnce(Return(true)); |
| 595 [field_ mouseDown:downEvent]; | 604 [field_ mouseDown:downEvent]; |
| 596 | 605 |
| 597 // The selection should not have changed. | 606 // The text field should no longer be first responder. |
| 598 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); | 607 EXPECT_FALSE([base::mac::ObjCCast<NSView>([[field_ window] firstResponder]) |
| 608 isDescendantOf:field_]); | |
| 599 | 609 |
| 600 // TODO(shess): Test that mouse drags are initiated if the next | 610 // TODO(shess): Test that mouse drags are initiated if the next |
| 601 // event is a drag, or if the mouse-up takes too long to arrive. | 611 // event is a drag, or if the mouse-up takes too long to arrive. |
| 602 // IDEA: mock decoration to return a pasteboard which a mock | 612 // IDEA: mock decoration to return a pasteboard which a mock |
| 603 // AutocompleteTextField notes in -dragImage:*. | 613 // AutocompleteTextField notes in -dragImage:*. |
| 604 } | 614 } |
| 605 | 615 |
| 606 // Clicking a decoration should call decoration's OnMousePressed. | 616 // Clicking a decoration should call decoration's OnMousePressed. |
| 607 TEST_F(AutocompleteTextFieldTest, RightDecorationMouseDown) { | 617 TEST_F(AutocompleteTextFieldTest, RightDecorationMouseDown) { |
| 608 // At this point, not focussed. | 618 // At this point, not focussed. |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 782 ofView:field_]; | 792 ofView:field_]; |
| 783 EXPECT_FALSE([[field_ cell] showsFirstResponder]); | 793 EXPECT_FALSE([[field_ cell] showsFirstResponder]); |
| 784 EXPECT_FALSE([FieldEditor() shouldDrawInsertionPoint]); | 794 EXPECT_FALSE([FieldEditor() shouldDrawInsertionPoint]); |
| 785 | 795 |
| 786 [[field_ cell] setHideFocusState:NO | 796 [[field_ cell] setHideFocusState:NO |
| 787 ofView:field_]; | 797 ofView:field_]; |
| 788 EXPECT_TRUE([[field_ cell] showsFirstResponder]); | 798 EXPECT_TRUE([[field_ cell] showsFirstResponder]); |
| 789 EXPECT_TRUE([FieldEditor() shouldDrawInsertionPoint]); | 799 EXPECT_TRUE([FieldEditor() shouldDrawInsertionPoint]); |
| 790 } | 800 } |
| 791 | 801 |
| 792 // Verify that OnSetFocus for button decorations is only sent after the | 802 // Verify that clicking a decoration that accepts mouse clicks does not focus |
| 793 // decoration is picked as the target for the subsequent -mouseDown:. Otherwise | 803 // the Omnibox. |
| 794 // hiding a ButtonDecoration in OnSetFocus will prevent a call to | 804 TEST_F(AutocompleteTextFieldObserverTest, |
| 795 // OnMousePressed, since it is already hidden at the time of mouseDown. | 805 ClickingDecorationDoesNotFocusOmnibox) { |
| 796 TEST_F(AutocompleteTextFieldObserverTest, ButtonDecorationFocus) { | |
| 797 // Add the mock button. | |
| 798 MockButtonDecoration mock_button; | |
| 799 mock_button.SetVisible(true); | |
| 800 AutocompleteTextFieldCell* cell = [field_ cell]; | 806 AutocompleteTextFieldCell* cell = [field_ cell]; |
| 801 [cell addLeftDecoration:&mock_button]; | |
| 802 | 807 |
| 803 // Ensure button is hidden when OnSetFocus() is called. | 808 // Set up a non-interactive decoration. |
| 804 EXPECT_CALL(field_observer_, OnSetFocus(false)).WillOnce( | 809 MockDecoration noninteractive_decoration; |
| 805 testing::InvokeWithoutArgs(&mock_button, &MockButtonDecoration::Hide)); | 810 noninteractive_decoration.SetVisible(true); |
| 811 EXPECT_CALL(noninteractive_decoration, AcceptsMousePress()) | |
| 812 .WillRepeatedly(testing::Return(false)); | |
| 813 [cell addLeftDecoration:&noninteractive_decoration]; | |
| 806 | 814 |
| 807 // Ignore incidental calls. | 815 // Set up an interactive decoration. |
| 816 MockDecoration interactive_decoration; | |
| 817 EXPECT_CALL(interactive_decoration, AcceptsMousePress()) | |
| 818 .WillRepeatedly(testing::Return(true)); | |
| 819 interactive_decoration.SetVisible(true); | |
| 820 [cell addLeftDecoration:&interactive_decoration]; | |
| 821 EXPECT_CALL(interactive_decoration, OnMousePressed(_, _)) | |
| 822 .WillRepeatedly(testing::Return(true)); | |
| 823 | |
| 824 // Ignore incidental calls. The exact frequency of these calls doesn't matter | |
| 825 // as they are auxiliary. | |
| 808 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(_)) | 826 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(_)) |
| 809 .WillRepeatedly(testing::Return(NSMakeRange(0, 0))); | 827 .WillRepeatedly(testing::Return(NSMakeRange(0, 0))); |
| 810 EXPECT_CALL(field_observer_, OnMouseDown(_)); | 828 EXPECT_CALL(field_observer_, OnMouseDown(_)).Times(testing::AnyNumber()); |
| 811 | 829 EXPECT_CALL(field_observer_, OnSetFocus(false)).Times(testing::AnyNumber()); |
| 812 // Still expect an OnMousePressed on the button. | 830 EXPECT_CALL(field_observer_, OnKillFocus()).Times(testing::AnyNumber()); |
| 813 EXPECT_CALL(mock_button, OnMousePressed(_, _)) | 831 EXPECT_CALL(field_observer_, OnDidEndEditing()).Times(testing::AnyNumber()); |
| 814 .WillOnce(testing::Return(true)); | |
| 815 | |
| 816 // Get click point for button decoration. | |
| 817 NSRect button_rect = | |
| 818 [cell frameForDecoration:&mock_button inFrame:[field_ bounds]]; | |
| 819 EXPECT_FALSE(NSIsEmptyRect(button_rect)); | |
| 820 NSPoint click_location = | |
| 821 NSMakePoint(NSMidX(button_rect), NSMidY(button_rect)); | |
| 822 | 832 |
| 823 // Ensure the field is currently not first responder. | 833 // Ensure the field is currently not first responder. |
| 824 [test_window() makePretendKeyWindowAndSetFirstResponder:nil]; | 834 [test_window() makePretendKeyWindowAndSetFirstResponder:nil]; |
| 825 EXPECT_NSNE([[field_ window] firstResponder], field_); | 835 NSResponder* firstResponder = [[field_ window] firstResponder]; |
|
Scott Hess - ex-Googler
2015/05/11 22:40:43
This approach makes sense.
| |
| 836 EXPECT_FALSE( | |
| 837 [base::mac::ObjCCast<NSView>(firstResponder) isDescendantOf:field_]); | |
| 826 | 838 |
| 827 // Execute button click event sequence. | 839 // Clicking an interactive decoration doesn't change the first responder. |
| 828 NSEvent* downEvent = Event(field_, click_location, NSLeftMouseDown); | 840 SendMouseClickToDecoration(&interactive_decoration); |
| 829 NSEvent* upEvent = Event(field_, click_location, NSLeftMouseUp); | 841 EXPECT_NSEQ(firstResponder, [[field_ window] firstResponder]); |
| 830 | 842 |
| 831 // Can't just use -sendEvent:, since that doesn't populate -currentEvent. | 843 // Clicking a non-interactive decoration focuses the Omnibox. |
| 832 [NSApp postEvent:downEvent atStart:YES]; | 844 SendMouseClickToDecoration(&noninteractive_decoration); |
| 833 [NSApp postEvent:upEvent atStart:NO]; | 845 firstResponder = [[field_ window] firstResponder]; |
| 834 NSEvent* next_event = [NSApp nextEventMatchingMask:NSAnyEventMask | 846 EXPECT_TRUE( |
| 835 untilDate:nil | 847 [base::mac::ObjCCast<NSView>(firstResponder) isDescendantOf:field_]); |
| 836 inMode:NSDefaultRunLoopMode | |
| 837 dequeue:YES]; | |
| 838 [NSApp sendEvent:next_event]; | |
| 839 | 848 |
| 840 // Expectations check that both OnSetFocus and OnMouseDown were called. | 849 // Clicking an interactive decoration doesn't change the first responder. |
| 841 // Additionally, ensure button is hidden and field is firstResponder. | 850 SendMouseClickToDecoration(&interactive_decoration); |
| 842 EXPECT_FALSE(mock_button.IsVisible()); | 851 EXPECT_NSEQ(firstResponder, [[field_ window] firstResponder]); |
| 843 EXPECT_TRUE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_ | |
| 844 inFrame:[field_ bounds]])); | |
| 845 EXPECT_TRUE([base::mac::ObjCCastStrict<NSView>( | |
| 846 [[field_ window] firstResponder]) isDescendantOf:field_]); | |
| 847 } | 852 } |
| 848 | 853 |
| 849 TEST_F(AutocompleteTextFieldObserverTest, SendsEditingMessages) { | 854 TEST_F(AutocompleteTextFieldObserverTest, SendsEditingMessages) { |
| 850 // Many of these methods try to change the selection. | 855 // Many of these methods try to change the selection. |
| 851 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(A<NSRange>())) | 856 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(A<NSRange>())) |
| 852 .WillRepeatedly(ReturnArg<0>()); | 857 .WillRepeatedly(ReturnArg<0>()); |
| 853 | 858 |
| 854 EXPECT_CALL(field_observer_, OnSetFocus(false)); | 859 EXPECT_CALL(field_observer_, OnSetFocus(false)); |
| 855 // Becoming first responder doesn't begin editing. | 860 // Becoming first responder doesn't begin editing. |
| 856 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; | 861 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 895 base::scoped_nsobject<AutocompleteTextField> pin([field_ retain]); | 900 base::scoped_nsobject<AutocompleteTextField> pin([field_ retain]); |
| 896 [field_ removeFromSuperview]; | 901 [field_ removeFromSuperview]; |
| 897 [test_window() resignKeyWindow]; | 902 [test_window() resignKeyWindow]; |
| 898 | 903 |
| 899 [[test_window() contentView] addSubview:field_]; | 904 [[test_window() contentView] addSubview:field_]; |
| 900 EXPECT_CALL(field_observer_, ClosePopup()); | 905 EXPECT_CALL(field_observer_, ClosePopup()); |
| 901 [test_window() resignKeyWindow]; | 906 [test_window() resignKeyWindow]; |
| 902 } | 907 } |
| 903 | 908 |
| 904 } // namespace | 909 } // namespace |
| OLD | NEW |