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 771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 782 ofView:field_]; | 782 ofView:field_]; |
| 783 EXPECT_FALSE([[field_ cell] showsFirstResponder]); | 783 EXPECT_FALSE([[field_ cell] showsFirstResponder]); |
| 784 EXPECT_FALSE([FieldEditor() shouldDrawInsertionPoint]); | 784 EXPECT_FALSE([FieldEditor() shouldDrawInsertionPoint]); |
| 785 | 785 |
| 786 [[field_ cell] setHideFocusState:NO | 786 [[field_ cell] setHideFocusState:NO |
| 787 ofView:field_]; | 787 ofView:field_]; |
| 788 EXPECT_TRUE([[field_ cell] showsFirstResponder]); | 788 EXPECT_TRUE([[field_ cell] showsFirstResponder]); |
| 789 EXPECT_TRUE([FieldEditor() shouldDrawInsertionPoint]); | 789 EXPECT_TRUE([FieldEditor() shouldDrawInsertionPoint]); |
| 790 } | 790 } |
| 791 | 791 |
| 792 // Verify that OnSetFocus for button decorations is only sent after the | 792 // Verify that clicking on a button decoration does not focus the omnibox. |
| 793 // decoration is picked as the target for the subsequent -mouseDown:. Otherwise | |
| 794 // hiding a ButtonDecoration in OnSetFocus will prevent a call to | |
| 795 // OnMousePressed, since it is already hidden at the time of mouseDown. | |
| 796 TEST_F(AutocompleteTextFieldObserverTest, ButtonDecorationFocus) { | 793 TEST_F(AutocompleteTextFieldObserverTest, ButtonDecorationFocus) { |
|
erikchen
2015/05/07 19:33:51
This test was written for Omnitheatre, and is no l
Scott Hess - ex-Googler
2015/05/07 20:07:42
Would it make sense to further revise this to not
erikchen
2015/05/08 01:51:46
Good suggestion. I updated the test, and discovere
| |
| 797 // Add the mock button. | 794 // Add the mock button. |
| 798 MockButtonDecoration mock_button; | 795 MockButtonDecoration mock_button; |
| 799 mock_button.SetVisible(true); | 796 mock_button.SetVisible(true); |
| 800 AutocompleteTextFieldCell* cell = [field_ cell]; | 797 AutocompleteTextFieldCell* cell = [field_ cell]; |
| 801 [cell addLeftDecoration:&mock_button]; | 798 [cell addLeftDecoration:&mock_button]; |
| 802 | 799 |
| 803 // Ensure button is hidden when OnSetFocus() is called. | |
| 804 EXPECT_CALL(field_observer_, OnSetFocus(false)).WillOnce( | |
| 805 testing::InvokeWithoutArgs(&mock_button, &MockButtonDecoration::Hide)); | |
| 806 | |
| 807 // Ignore incidental calls. | 800 // Ignore incidental calls. |
| 808 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(_)) | 801 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(_)) |
| 809 .WillRepeatedly(testing::Return(NSMakeRange(0, 0))); | 802 .WillRepeatedly(testing::Return(NSMakeRange(0, 0))); |
| 810 EXPECT_CALL(field_observer_, OnMouseDown(_)); | 803 EXPECT_CALL(field_observer_, OnMouseDown(_)); |
| 811 | 804 |
| 812 // Still expect an OnMousePressed on the button. | 805 // Expect an OnMousePressed on the button. |
| 813 EXPECT_CALL(mock_button, OnMousePressed(_, _)) | 806 EXPECT_CALL(mock_button, OnMousePressed(_, _)) |
| 814 .WillOnce(testing::Return(true)); | 807 .WillOnce(testing::Return(true)); |
| 815 | 808 |
| 816 // Get click point for button decoration. | 809 // Get click point for button decoration. |
| 817 NSRect button_rect = | 810 NSRect button_rect = |
| 818 [cell frameForDecoration:&mock_button inFrame:[field_ bounds]]; | 811 [cell frameForDecoration:&mock_button inFrame:[field_ bounds]]; |
| 819 EXPECT_FALSE(NSIsEmptyRect(button_rect)); | 812 EXPECT_FALSE(NSIsEmptyRect(button_rect)); |
| 820 NSPoint click_location = | 813 NSPoint click_location = |
| 821 NSMakePoint(NSMidX(button_rect), NSMidY(button_rect)); | 814 NSMakePoint(NSMidX(button_rect), NSMidY(button_rect)); |
| 822 | 815 |
| 823 // Ensure the field is currently not first responder. | 816 // Ensure the field is currently not first responder. |
| 824 [test_window() makePretendKeyWindowAndSetFirstResponder:nil]; | 817 [test_window() makePretendKeyWindowAndSetFirstResponder:nil]; |
| 825 EXPECT_NSNE([[field_ window] firstResponder], field_); | 818 EXPECT_FALSE([base::mac::ObjCCast<NSView>( |
| 819 [[field_ window] firstResponder]) isDescendantOf:field_]); | |
|
Scott Hess - ex-Googler
2015/05/07 20:07:42
At this point is there any first responder at all
erikchen
2015/05/08 01:51:46
The first responder is CocoaTestHelperWindow. I'd
| |
| 826 | 820 |
| 827 // Execute button click event sequence. | 821 // Execute button click event sequence. |
| 828 NSEvent* downEvent = Event(field_, click_location, NSLeftMouseDown); | 822 NSEvent* downEvent = Event(field_, click_location, NSLeftMouseDown); |
| 829 NSEvent* upEvent = Event(field_, click_location, NSLeftMouseUp); | 823 NSEvent* upEvent = Event(field_, click_location, NSLeftMouseUp); |
| 830 | 824 |
| 831 // Can't just use -sendEvent:, since that doesn't populate -currentEvent. | 825 // Can't just use -sendEvent:, since that doesn't populate -currentEvent. |
| 832 [NSApp postEvent:downEvent atStart:YES]; | 826 [NSApp postEvent:downEvent atStart:YES]; |
| 833 [NSApp postEvent:upEvent atStart:NO]; | 827 [NSApp postEvent:upEvent atStart:NO]; |
| 834 NSEvent* next_event = [NSApp nextEventMatchingMask:NSAnyEventMask | 828 NSEvent* next_event = [NSApp nextEventMatchingMask:NSAnyEventMask |
| 835 untilDate:nil | 829 untilDate:nil |
| 836 inMode:NSDefaultRunLoopMode | 830 inMode:NSDefaultRunLoopMode |
| 837 dequeue:YES]; | 831 dequeue:YES]; |
| 838 [NSApp sendEvent:next_event]; | 832 [NSApp sendEvent:next_event]; |
| 839 | 833 |
| 840 // Expectations check that both OnSetFocus and OnMouseDown were called. | 834 // Expectations check that OnMouseDown was called. Additionally, ensure |
| 841 // Additionally, ensure button is hidden and field is firstResponder. | 835 // button is visible and field is not firstResponder. |
| 842 EXPECT_FALSE(mock_button.IsVisible()); | 836 EXPECT_TRUE(mock_button.IsVisible()); |
| 843 EXPECT_TRUE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_ | 837 EXPECT_FALSE(NSIsEmptyRect([cell frameForDecoration:&mock_button |
| 844 inFrame:[field_ bounds]])); | 838 inFrame:[field_ bounds]])); |
|
Scott Hess - ex-Googler
2015/05/07 20:07:42
Alignment.
erikchen
2015/05/08 01:51:46
I clang-format-ed the entire CL.
Scott Hess - ex-Googler
2015/05/08 19:38:28
And the offending code is no longer present anyhow
erikchen
2015/05/08 20:02:20
The previous code was not. I should have been more
| |
| 845 EXPECT_TRUE([base::mac::ObjCCastStrict<NSView>( | 839 EXPECT_FALSE([base::mac::ObjCCast<NSView>( |
| 846 [[field_ window] firstResponder]) isDescendantOf:field_]); | 840 [[field_ window] firstResponder]) isDescendantOf:field_]); |
|
Scott Hess - ex-Googler
2015/05/07 20:07:42
Here also.
erikchen
2015/05/08 01:51:46
ditto.
| |
| 847 } | 841 } |
| 848 | 842 |
| 849 TEST_F(AutocompleteTextFieldObserverTest, SendsEditingMessages) { | 843 TEST_F(AutocompleteTextFieldObserverTest, SendsEditingMessages) { |
| 850 // Many of these methods try to change the selection. | 844 // Many of these methods try to change the selection. |
| 851 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(A<NSRange>())) | 845 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(A<NSRange>())) |
| 852 .WillRepeatedly(ReturnArg<0>()); | 846 .WillRepeatedly(ReturnArg<0>()); |
| 853 | 847 |
| 854 EXPECT_CALL(field_observer_, OnSetFocus(false)); | 848 EXPECT_CALL(field_observer_, OnSetFocus(false)); |
| 855 // Becoming first responder doesn't begin editing. | 849 // Becoming first responder doesn't begin editing. |
| 856 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; | 850 [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]); | 889 base::scoped_nsobject<AutocompleteTextField> pin([field_ retain]); |
| 896 [field_ removeFromSuperview]; | 890 [field_ removeFromSuperview]; |
| 897 [test_window() resignKeyWindow]; | 891 [test_window() resignKeyWindow]; |
| 898 | 892 |
| 899 [[test_window() contentView] addSubview:field_]; | 893 [[test_window() contentView] addSubview:field_]; |
| 900 EXPECT_CALL(field_observer_, ClosePopup()); | 894 EXPECT_CALL(field_observer_, ClosePopup()); |
| 901 [test_window() resignKeyWindow]; | 895 [test_window() resignKeyWindow]; |
| 902 } | 896 } |
| 903 | 897 |
| 904 } // namespace | 898 } // namespace |
| OLD | NEW |