OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
8 #import "base/cocoa_protocols_mac.h" | 8 #import "base/cocoa_protocols_mac.h" |
9 #include "base/scoped_nsobject.h" | 9 #include "base/scoped_nsobject.h" |
10 #import "chrome/browser/cocoa/autocomplete_text_field.h" | 10 #import "chrome/browser/cocoa/autocomplete_text_field.h" |
11 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" | 11 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
12 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" | 12 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" |
13 #import "chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h" | 13 #import "chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h" |
14 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 14 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
15 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "testing/platform_test.h" | 18 #include "testing/platform_test.h" |
19 | 19 |
20 using ::testing::InSequence; | 20 using ::testing::InSequence; |
21 using ::testing::Return; | 21 using ::testing::Return; |
22 using ::testing::StrictMock; | 22 using ::testing::StrictMock; |
23 | 23 |
24 namespace { | 24 namespace { |
25 class MockLocationIconView : public LocationBarViewMac::LocationIconView { | 25 class MockLocationIconView : public LocationBarViewMac::LocationIconView { |
26 public: | 26 public: |
27 MockLocationIconView(LocationBarViewMac* owner) | 27 MockLocationIconView(LocationBarViewMac* owner) |
28 : LocationBarViewMac::LocationIconView(owner) {} | 28 : LocationBarViewMac::LocationIconView(owner) {} |
29 | 29 |
| 30 // |LocationBarViewMac::LocationIconView::GetDragPasteboard()| |
| 31 // expects things to be more initialized than they will be, and |
| 32 // will crash if it is called. |
| 33 virtual NSPasteboard* GetDragPasteboard() { |
| 34 return [NSPasteboard pasteboardWithUniqueName]; |
| 35 } |
| 36 |
30 // We can't use gmock's MOCK_METHOD macro, because it doesn't like the | 37 // We can't use gmock's MOCK_METHOD macro, because it doesn't like the |
31 // NSRect argument to OnMousePressed. | 38 // NSRect argument to OnMousePressed. |
32 virtual void OnMousePressed(NSRect bounds) { | 39 virtual void OnMousePressed(NSRect bounds) { |
33 mouse_was_pressed_ = true; | 40 mouse_was_pressed_ = true; |
34 } | 41 } |
35 bool mouse_was_pressed_; | 42 bool mouse_was_pressed_; |
36 }; | 43 }; |
37 | 44 |
38 class MockPageActionImageView : public LocationBarViewMac::PageActionImageView { | 45 class MockPageActionImageView : public LocationBarViewMac::PageActionImageView { |
39 public: | 46 public: |
(...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 | 594 |
588 MockLocationIconView location_icon_view(NULL); | 595 MockLocationIconView location_icon_view(NULL); |
589 [cell setLocationIconView:&location_icon_view]; | 596 [cell setLocationIconView:&location_icon_view]; |
590 location_icon_view.SetImage( | 597 location_icon_view.SetImage( |
591 ResourceBundle::GetSharedInstance().GetNSImageNamed( | 598 ResourceBundle::GetSharedInstance().GetNSImageNamed( |
592 IDR_OMNIBOX_HTTPS_VALID)); | 599 IDR_OMNIBOX_HTTPS_VALID)); |
593 location_icon_view.SetVisible(true); | 600 location_icon_view.SetVisible(true); |
594 | 601 |
595 NSRect iconFrame([cell locationIconFrameForFrame:[field_ bounds]]); | 602 NSRect iconFrame([cell locationIconFrameForFrame:[field_ bounds]]); |
596 NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame))); | 603 NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame))); |
597 NSEvent* event(Event(field_, location, NSLeftMouseDown, 1)); | 604 NSEvent* downEvent(Event(field_, location, NSLeftMouseDown, 1)); |
| 605 NSEvent* upEvent(Event(field_, location, NSLeftMouseUp, 1)); |
598 | 606 |
599 [field_ mouseDown:event]; | 607 // Since location icon can be dragged, the mouse-press is sent on |
| 608 // mouse-up. |
| 609 [NSApp postEvent:upEvent atStart:YES]; |
| 610 [field_ mouseDown:downEvent]; |
600 EXPECT_TRUE(location_icon_view.mouse_was_pressed_); | 611 EXPECT_TRUE(location_icon_view.mouse_was_pressed_); |
| 612 |
| 613 // TODO(shess): Test that mouse drags are initiated if the next |
| 614 // event is a drag, or if the mouse-up takes too long to arrive. |
601 } | 615 } |
602 | 616 |
603 // Clicking a Page Action icon should call its OnMousePressed. | 617 // Clicking a Page Action icon should call its OnMousePressed. |
604 TEST_F(AutocompleteTextFieldTest, PageActionMouseDown) { | 618 TEST_F(AutocompleteTextFieldTest, PageActionMouseDown) { |
605 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 619 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
606 | 620 |
607 MockLocationIconView location_icon_view(NULL); | |
608 location_icon_view.SetImage( | |
609 ResourceBundle::GetSharedInstance().GetNSImageNamed( | |
610 IDR_OMNIBOX_HTTPS_VALID)); | |
611 [cell setLocationIconView:&location_icon_view]; | |
612 | |
613 MockPageActionImageView page_action_view; | 621 MockPageActionImageView page_action_view; |
614 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | 622 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
615 page_action_view.SetImage(image); | 623 page_action_view.SetImage(image); |
616 | 624 |
617 MockPageActionImageView page_action_view2; | 625 MockPageActionImageView page_action_view2; |
618 page_action_view2.SetImage(image); | 626 page_action_view2.SetImage(image); |
619 | 627 |
620 TestPageActionViewList list; | 628 TestPageActionViewList list; |
621 list.Add(&page_action_view); | 629 list.Add(&page_action_view); |
622 list.Add(&page_action_view2); | 630 list.Add(&page_action_view2); |
623 [cell setPageActionViewList:&list]; | 631 [cell setPageActionViewList:&list]; |
624 | 632 |
625 // One page action, no lock. | 633 // One page action. |
626 location_icon_view.SetVisible(false); | |
627 page_action_view.SetVisible(true); | 634 page_action_view.SetVisible(true); |
628 page_action_view2.SetVisible(false); | 635 page_action_view2.SetVisible(false); |
629 NSRect iconFrame([cell pageActionFrameForIndex:0 inFrame:[field_ bounds]]); | 636 NSRect iconFrame([cell pageActionFrameForIndex:0 inFrame:[field_ bounds]]); |
630 NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame))); | 637 NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame))); |
631 NSEvent* event(Event(field_, location, NSLeftMouseDown, 1)); | 638 NSEvent* event(Event(field_, location, NSLeftMouseDown, 1)); |
632 | 639 |
633 [field_ mouseDown:event]; | 640 [field_ mouseDown:event]; |
634 EXPECT_TRUE(page_action_view.MouseWasPressed()); | 641 EXPECT_TRUE(page_action_view.MouseWasPressed()); |
635 | 642 |
636 // Two page actions, no lock. | 643 // Two page actions, no lock. |
637 page_action_view2.SetVisible(true); | 644 page_action_view2.SetVisible(true); |
638 iconFrame = [cell pageActionFrameForIndex:0 inFrame:[field_ bounds]]; | 645 iconFrame = [cell pageActionFrameForIndex:0 inFrame:[field_ bounds]]; |
639 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); | 646 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); |
640 event = Event(field_, location, NSLeftMouseDown, 1); | 647 event = Event(field_, location, NSLeftMouseDown, 1); |
641 | 648 |
642 [field_ mouseDown:event]; | 649 [field_ mouseDown:event]; |
643 EXPECT_TRUE(page_action_view.MouseWasPressed()); | 650 EXPECT_TRUE(page_action_view.MouseWasPressed()); |
644 | 651 |
645 iconFrame = [cell pageActionFrameForIndex:1 inFrame:[field_ bounds]]; | 652 iconFrame = [cell pageActionFrameForIndex:1 inFrame:[field_ bounds]]; |
646 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); | 653 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); |
647 event = Event(field_, location, NSLeftMouseDown, 1); | 654 event = Event(field_, location, NSLeftMouseDown, 1); |
648 | 655 |
649 [field_ mouseDown:event]; | 656 [field_ mouseDown:event]; |
650 EXPECT_TRUE(page_action_view.MouseWasPressed()); | 657 EXPECT_TRUE(page_action_view.MouseWasPressed()); |
651 | 658 |
652 // Two page actions plus lock. | 659 // Two page actions. |
653 location_icon_view.SetVisible(true); | |
654 iconFrame = [cell pageActionFrameForIndex:0 inFrame:[field_ bounds]]; | 660 iconFrame = [cell pageActionFrameForIndex:0 inFrame:[field_ bounds]]; |
655 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); | 661 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); |
656 event = Event(field_, location, NSLeftMouseDown, 1); | 662 event = Event(field_, location, NSLeftMouseDown, 1); |
657 | 663 |
658 [field_ mouseDown:event]; | 664 [field_ mouseDown:event]; |
659 EXPECT_TRUE(page_action_view.MouseWasPressed()); | 665 EXPECT_TRUE(page_action_view.MouseWasPressed()); |
660 | 666 |
661 iconFrame = [cell pageActionFrameForIndex:1 inFrame:[field_ bounds]]; | 667 iconFrame = [cell pageActionFrameForIndex:1 inFrame:[field_ bounds]]; |
662 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); | 668 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); |
663 event = Event(field_, location, NSLeftMouseDown, 1); | 669 event = Event(field_, location, NSLeftMouseDown, 1); |
664 | 670 |
665 [field_ mouseDown:event]; | 671 [field_ mouseDown:event]; |
666 EXPECT_TRUE(page_action_view.MouseWasPressed()); | 672 EXPECT_TRUE(page_action_view.MouseWasPressed()); |
667 | |
668 iconFrame = [cell locationIconFrameForFrame:[field_ bounds]]; | |
669 location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); | |
670 event = Event(field_, location, NSLeftMouseDown, 1); | |
671 | |
672 [field_ mouseDown:event]; | |
673 EXPECT_TRUE(location_icon_view.mouse_was_pressed_); | |
674 } | 673 } |
675 | 674 |
676 // Test that page action menus are properly returned. | 675 // Test that page action menus are properly returned. |
677 // TODO(shess): Really, this should test that things are forwarded to | 676 // TODO(shess): Really, this should test that things are forwarded to |
678 // the cell, and the cell tests should test that the right things are | 677 // the cell, and the cell tests should test that the right things are |
679 // selected. It's easier to mock the event here, though. This code's | 678 // selected. It's easier to mock the event here, though. This code's |
680 // event-mockers might be worth promoting to |test_event_utils.h| or | 679 // event-mockers might be worth promoting to |test_event_utils.h| or |
681 // |cocoa_test_helper.h|. | 680 // |cocoa_test_helper.h|. |
682 TEST_F(AutocompleteTextFieldTest, PageActionMenu) { | 681 TEST_F(AutocompleteTextFieldTest, PageActionMenu) { |
683 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 682 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
856 scoped_nsobject<AutocompleteTextField> pin([field_ retain]); | 855 scoped_nsobject<AutocompleteTextField> pin([field_ retain]); |
857 [field_ removeFromSuperview]; | 856 [field_ removeFromSuperview]; |
858 [test_window() resignKeyWindow]; | 857 [test_window() resignKeyWindow]; |
859 | 858 |
860 [[test_window() contentView] addSubview:field_]; | 859 [[test_window() contentView] addSubview:field_]; |
861 EXPECT_CALL(field_observer_, OnDidResignKey()); | 860 EXPECT_CALL(field_observer_, OnDidResignKey()); |
862 [test_window() resignKeyWindow]; | 861 [test_window() resignKeyWindow]; |
863 } | 862 } |
864 | 863 |
865 } // namespace | 864 } // namespace |
OLD | NEW |