| 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 18 matching lines...) Expand all Loading... |
| 29 using ::testing::_; | 29 using ::testing::_; |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 class MockDecoration : public LocationBarDecoration { | 33 class MockDecoration : public LocationBarDecoration { |
| 34 public: | 34 public: |
| 35 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } | 35 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } |
| 36 | 36 |
| 37 virtual void DrawInFrame(NSRect frame, NSView* control_view) { ; } | 37 virtual void DrawInFrame(NSRect frame, NSView* control_view) { ; } |
| 38 MOCK_METHOD0(AcceptsMousePress, bool()); | 38 MOCK_METHOD0(AcceptsMousePress, bool()); |
| 39 MOCK_METHOD1(OnMousePressed, bool(NSRect frame)); | 39 MOCK_METHOD2(OnMousePressed, bool(NSRect frame, NSPoint location)); |
| 40 MOCK_METHOD0(GetMenu, NSMenu*()); | 40 MOCK_METHOD0(GetMenu, NSMenu*()); |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 class MockButtonDecoration : public ButtonDecoration { | 43 class MockButtonDecoration : public ButtonDecoration { |
| 44 public: | 44 public: |
| 45 MockButtonDecoration() | 45 MockButtonDecoration() |
| 46 : ButtonDecoration(IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON), | 46 : ButtonDecoration(IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON), |
| 47 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE, | 47 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE, |
| 48 IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON_HOVER), | 48 IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON_HOVER), |
| 49 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE, | 49 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE, |
| 50 IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON_PRESSED), | 50 IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON_PRESSED), |
| 51 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE, | 51 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE, |
| 52 3) {} | 52 3) {} |
| 53 void Hide() { SetVisible(false); } | 53 void Hide() { SetVisible(false); } |
| 54 MOCK_METHOD1(OnMousePressed, bool(NSRect frame)); | 54 MOCK_METHOD2(OnMousePressed, bool(NSRect frame, NSPoint location)); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 // Mock up an incrementing event number. | 57 // Mock up an incrementing event number. |
| 58 NSUInteger eventNumber = 0; | 58 NSUInteger eventNumber = 0; |
| 59 | 59 |
| 60 // Create an event of the indicated |type| at |point| within |view|. | 60 // Create an event of the indicated |type| at |point| within |view|. |
| 61 // TODO(shess): Would be nice to have a MockApplication which provided | 61 // TODO(shess): Would be nice to have a MockApplication which provided |
| 62 // nifty accessors to create these things and inject them. It could | 62 // nifty accessors to create these things and inject them. It could |
| 63 // even provide functions for "Click and drag mouse from point A to | 63 // even provide functions for "Click and drag mouse from point A to |
| 64 // point B". | 64 // point B". |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 const NSRect iconFrame = | 568 const NSRect iconFrame = |
| 569 [cell frameForDecoration:&mock_left_decoration_ inFrame:[field_ bounds]]; | 569 [cell frameForDecoration:&mock_left_decoration_ inFrame:[field_ bounds]]; |
| 570 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); | 570 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); |
| 571 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1); | 571 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1); |
| 572 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1); | 572 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1); |
| 573 | 573 |
| 574 // Since decorations can be dragged, the mouse-press is sent on | 574 // Since decorations can be dragged, the mouse-press is sent on |
| 575 // mouse-up. | 575 // mouse-up. |
| 576 [NSApp postEvent:upEvent atStart:YES]; | 576 [NSApp postEvent:upEvent atStart:YES]; |
| 577 | 577 |
| 578 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_)) | 578 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_, _)) |
| 579 .WillOnce(Return(true)); | 579 .WillOnce(Return(true)); |
| 580 [field_ mouseDown:downEvent]; | 580 [field_ mouseDown:downEvent]; |
| 581 | 581 |
| 582 // Focus the field and test that handled clicks don't affect selection. | 582 // Focus the field and test that handled clicks don't affect selection. |
| 583 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; | 583 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 584 EXPECT_TRUE([field_ currentEditor]); | 584 EXPECT_TRUE([field_ currentEditor]); |
| 585 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]); | 585 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]); |
| 586 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); | 586 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); |
| 587 | 587 |
| 588 // Generate another click on the decoration. | 588 // Generate another click on the decoration. |
| 589 downEvent = Event(field_, location, NSLeftMouseDown, 1); | 589 downEvent = Event(field_, location, NSLeftMouseDown, 1); |
| 590 upEvent = Event(field_, location, NSLeftMouseUp, 1); | 590 upEvent = Event(field_, location, NSLeftMouseUp, 1); |
| 591 [NSApp postEvent:upEvent atStart:YES]; | 591 [NSApp postEvent:upEvent atStart:YES]; |
| 592 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_)) | 592 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_, _)) |
| 593 .WillOnce(Return(true)); | 593 .WillOnce(Return(true)); |
| 594 [field_ mouseDown:downEvent]; | 594 [field_ mouseDown:downEvent]; |
| 595 | 595 |
| 596 // The selection should not have changed. | 596 // The selection should not have changed. |
| 597 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); | 597 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); |
| 598 | 598 |
| 599 // TODO(shess): Test that mouse drags are initiated if the next | 599 // TODO(shess): Test that mouse drags are initiated if the next |
| 600 // event is a drag, or if the mouse-up takes too long to arrive. | 600 // event is a drag, or if the mouse-up takes too long to arrive. |
| 601 // IDEA: mock decoration to return a pasteboard which a mock | 601 // IDEA: mock decoration to return a pasteboard which a mock |
| 602 // AutocompleteTextField notes in -dragImage:*. | 602 // AutocompleteTextField notes in -dragImage:*. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 616 const NSRect iconFrame = | 616 const NSRect iconFrame = |
| 617 [cell frameForDecoration:&mock_right_decoration_ inFrame:bounds]; | 617 [cell frameForDecoration:&mock_right_decoration_ inFrame:bounds]; |
| 618 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); | 618 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); |
| 619 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1); | 619 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1); |
| 620 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1); | 620 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1); |
| 621 | 621 |
| 622 // Since decorations can be dragged, the mouse-press is sent on | 622 // Since decorations can be dragged, the mouse-press is sent on |
| 623 // mouse-up. | 623 // mouse-up. |
| 624 [NSApp postEvent:upEvent atStart:YES]; | 624 [NSApp postEvent:upEvent atStart:YES]; |
| 625 | 625 |
| 626 EXPECT_CALL(mock_right_decoration_, OnMousePressed(_)) | 626 EXPECT_CALL(mock_right_decoration_, OnMousePressed(_, _)) |
| 627 .WillOnce(Return(true)); | 627 .WillOnce(Return(true)); |
| 628 [field_ mouseDown:downEvent]; | 628 [field_ mouseDown:downEvent]; |
| 629 } | 629 } |
| 630 | 630 |
| 631 // Test that page action menus are properly returned. | 631 // Test that page action menus are properly returned. |
| 632 // TODO(shess): Really, this should test that things are forwarded to | 632 // TODO(shess): Really, this should test that things are forwarded to |
| 633 // the cell, and the cell tests should test that the right things are | 633 // the cell, and the cell tests should test that the right things are |
| 634 // selected. It's easier to mock the event here, though. This code's | 634 // selected. It's easier to mock the event here, though. This code's |
| 635 // event-mockers might be worth promoting to |cocoa_test_event_utils.h| or | 635 // event-mockers might be worth promoting to |cocoa_test_event_utils.h| or |
| 636 // |cocoa_test_helper.h|. | 636 // |cocoa_test_helper.h|. |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 // Ensure button is hidden when OnSetFocus() is called. | 802 // Ensure button is hidden when OnSetFocus() is called. |
| 803 EXPECT_CALL(field_observer_, OnSetFocus(false)).WillOnce( | 803 EXPECT_CALL(field_observer_, OnSetFocus(false)).WillOnce( |
| 804 testing::InvokeWithoutArgs(&mock_button, &MockButtonDecoration::Hide)); | 804 testing::InvokeWithoutArgs(&mock_button, &MockButtonDecoration::Hide)); |
| 805 | 805 |
| 806 // Ignore incidental calls. | 806 // Ignore incidental calls. |
| 807 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(_)) | 807 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(_)) |
| 808 .WillRepeatedly(testing::Return(NSMakeRange(0, 0))); | 808 .WillRepeatedly(testing::Return(NSMakeRange(0, 0))); |
| 809 EXPECT_CALL(field_observer_, OnMouseDown(_)); | 809 EXPECT_CALL(field_observer_, OnMouseDown(_)); |
| 810 | 810 |
| 811 // Still expect an OnMousePressed on the button. | 811 // Still expect an OnMousePressed on the button. |
| 812 EXPECT_CALL(mock_button, OnMousePressed(_)).WillOnce(testing::Return(true)); | 812 EXPECT_CALL(mock_button, OnMousePressed(_, _)) |
| 813 .WillOnce(testing::Return(true)); |
| 813 | 814 |
| 814 // Get click point for button decoration. | 815 // Get click point for button decoration. |
| 815 NSRect button_rect = | 816 NSRect button_rect = |
| 816 [cell frameForDecoration:&mock_button inFrame:[field_ bounds]]; | 817 [cell frameForDecoration:&mock_button inFrame:[field_ bounds]]; |
| 817 EXPECT_FALSE(NSIsEmptyRect(button_rect)); | 818 EXPECT_FALSE(NSIsEmptyRect(button_rect)); |
| 818 NSPoint click_location = | 819 NSPoint click_location = |
| 819 NSMakePoint(NSMidX(button_rect), NSMidY(button_rect)); | 820 NSMakePoint(NSMidX(button_rect), NSMidY(button_rect)); |
| 820 | 821 |
| 821 // Ensure the field is currently not first responder. | 822 // Ensure the field is currently not first responder. |
| 822 [test_window() makePretendKeyWindowAndSetFirstResponder:nil]; | 823 [test_window() makePretendKeyWindowAndSetFirstResponder:nil]; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 base::scoped_nsobject<AutocompleteTextField> pin([field_ retain]); | 894 base::scoped_nsobject<AutocompleteTextField> pin([field_ retain]); |
| 894 [field_ removeFromSuperview]; | 895 [field_ removeFromSuperview]; |
| 895 [test_window() resignKeyWindow]; | 896 [test_window() resignKeyWindow]; |
| 896 | 897 |
| 897 [[test_window() contentView] addSubview:field_]; | 898 [[test_window() contentView] addSubview:field_]; |
| 898 EXPECT_CALL(field_observer_, ClosePopup()); | 899 EXPECT_CALL(field_observer_, ClosePopup()); |
| 899 [test_window() resignKeyWindow]; | 900 [test_window() resignKeyWindow]; |
| 900 } | 901 } |
| 901 | 902 |
| 902 } // namespace | 903 } // namespace |
| OLD | NEW |