| 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 <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
| 8 #include "base/memory/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 9 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" | 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" |
| 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" | 11 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" |
| 11 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h" | 12 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h" |
| 12 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_unittest_h
elper.h" | 13 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_unittest_h
elper.h" |
| 13 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" | 14 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" |
| 14 #import "chrome/browser/ui/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 #import "testing/gtest_mac.h" | 18 #import "testing/gtest_mac.h" |
| 19 #include "testing/platform_test.h" | 19 #include "testing/platform_test.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 | 21 |
| 22 using ::testing::A; | 22 using ::testing::A; |
| 23 using ::testing::InSequence; | 23 using ::testing::InSequence; |
| 24 using ::testing::Return; | 24 using ::testing::Return; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // Width of the field so that we don't have to ask |field_| for it all | 68 // Width of the field so that we don't have to ask |field_| for it all |
| 69 // the time. | 69 // the time. |
| 70 static const CGFloat kWidth(300.0); | 70 static const CGFloat kWidth(300.0); |
| 71 | 71 |
| 72 class AutocompleteTextFieldTest : public CocoaTest { | 72 class AutocompleteTextFieldTest : public CocoaTest { |
| 73 public: | 73 public: |
| 74 AutocompleteTextFieldTest() { | 74 AutocompleteTextFieldTest() { |
| 75 // Make sure this is wide enough to play games with the cell | 75 // Make sure this is wide enough to play games with the cell |
| 76 // decorations. | 76 // decorations. |
| 77 NSRect frame = NSMakeRect(0, 0, kWidth, 30); | 77 NSRect frame = NSMakeRect(0, 0, kWidth, 30); |
| 78 scoped_nsobject<AutocompleteTextField> field( | 78 base::scoped_nsobject<AutocompleteTextField> field( |
| 79 [[AutocompleteTextField alloc] initWithFrame:frame]); | 79 [[AutocompleteTextField alloc] initWithFrame:frame]); |
| 80 field_ = field.get(); | 80 field_ = field.get(); |
| 81 [field_ setStringValue:@"Test test"]; | 81 [field_ setStringValue:@"Test test"]; |
| 82 [[test_window() contentView] addSubview:field_]; | 82 [[test_window() contentView] addSubview:field_]; |
| 83 | 83 |
| 84 AutocompleteTextFieldCell* cell = [field_ cell]; | 84 AutocompleteTextFieldCell* cell = [field_ cell]; |
| 85 [cell clearDecorations]; | 85 [cell clearDecorations]; |
| 86 | 86 |
| 87 mock_left_decoration_.SetVisible(false); | 87 mock_left_decoration_.SetVisible(false); |
| 88 [cell addLeftDecoration:&mock_left_decoration_]; | 88 [cell addLeftDecoration:&mock_left_decoration_]; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 } | 122 } |
| 123 | 123 |
| 124 AutocompleteTextFieldEditor* FieldEditor() { | 124 AutocompleteTextFieldEditor* FieldEditor() { |
| 125 return base::mac::ObjCCastStrict<AutocompleteTextFieldEditor>( | 125 return base::mac::ObjCCastStrict<AutocompleteTextFieldEditor>( |
| 126 [field_ currentEditor]); | 126 [field_ currentEditor]); |
| 127 } | 127 } |
| 128 | 128 |
| 129 AutocompleteTextField* field_; | 129 AutocompleteTextField* field_; |
| 130 MockDecoration mock_left_decoration_; | 130 MockDecoration mock_left_decoration_; |
| 131 MockDecoration mock_right_decoration_; | 131 MockDecoration mock_right_decoration_; |
| 132 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; | 132 base::scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> |
| 133 window_delegate_; |
| 133 }; | 134 }; |
| 134 | 135 |
| 135 TEST_VIEW(AutocompleteTextFieldTest, field_); | 136 TEST_VIEW(AutocompleteTextFieldTest, field_); |
| 136 | 137 |
| 137 // Base class for testing AutocompleteTextFieldObserver messages. | 138 // Base class for testing AutocompleteTextFieldObserver messages. |
| 138 class AutocompleteTextFieldObserverTest : public AutocompleteTextFieldTest { | 139 class AutocompleteTextFieldObserverTest : public AutocompleteTextFieldTest { |
| 139 public: | 140 public: |
| 140 virtual void SetUp() { | 141 virtual void SetUp() { |
| 141 AutocompleteTextFieldTest::SetUp(); | 142 AutocompleteTextFieldTest::SetUp(); |
| 142 [field_ setObserver:&field_observer_]; | 143 [field_ setObserver:&field_observer_]; |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 // the cell, and the cell tests should test that the right things are | 616 // the cell, and the cell tests should test that the right things are |
| 616 // selected. It's easier to mock the event here, though. This code's | 617 // selected. It's easier to mock the event here, though. This code's |
| 617 // event-mockers might be worth promoting to |cocoa_test_event_utils.h| or | 618 // event-mockers might be worth promoting to |cocoa_test_event_utils.h| or |
| 618 // |cocoa_test_helper.h|. | 619 // |cocoa_test_helper.h|. |
| 619 TEST_F(AutocompleteTextFieldTest, DecorationMenu) { | 620 TEST_F(AutocompleteTextFieldTest, DecorationMenu) { |
| 620 AutocompleteTextFieldCell* cell = [field_ cell]; | 621 AutocompleteTextFieldCell* cell = [field_ cell]; |
| 621 const NSRect bounds([field_ bounds]); | 622 const NSRect bounds([field_ bounds]); |
| 622 | 623 |
| 623 const CGFloat edge = NSHeight(bounds) - 4.0; | 624 const CGFloat edge = NSHeight(bounds) - 4.0; |
| 624 const NSSize size = NSMakeSize(edge, edge); | 625 const NSSize size = NSMakeSize(edge, edge); |
| 625 scoped_nsobject<NSImage> image([[NSImage alloc] initWithSize:size]); | 626 base::scoped_nsobject<NSImage> image([[NSImage alloc] initWithSize:size]); |
| 626 | 627 |
| 627 scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Menu"]); | 628 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Menu"]); |
| 628 | 629 |
| 629 mock_left_decoration_.SetVisible(true); | 630 mock_left_decoration_.SetVisible(true); |
| 630 mock_right_decoration_.SetVisible(true); | 631 mock_right_decoration_.SetVisible(true); |
| 631 | 632 |
| 632 // The item with a menu returns it. | 633 // The item with a menu returns it. |
| 633 NSRect actionFrame = [cell frameForDecoration:&mock_right_decoration_ | 634 NSRect actionFrame = [cell frameForDecoration:&mock_right_decoration_ |
| 634 inFrame:bounds]; | 635 inFrame:bounds]; |
| 635 NSPoint location = NSMakePoint(NSMidX(actionFrame), NSMidY(actionFrame)); | 636 NSPoint location = NSMakePoint(NSMidX(actionFrame), NSMidY(actionFrame)); |
| 636 NSEvent* event = Event(field_, location, NSRightMouseDown, 1); | 637 NSEvent* event = Event(field_, location, NSRightMouseDown, 1); |
| 637 | 638 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 667 [field_ setAllowsEditingTextAttributes:YES]; | 668 [field_ setAllowsEditingTextAttributes:YES]; |
| 668 | 669 |
| 669 // Set an attribute different from the field's default so we can | 670 // Set an attribute different from the field's default so we can |
| 670 // tell we got the same string out as we put in. | 671 // tell we got the same string out as we put in. |
| 671 NSFont* font = [NSFont fontWithDescriptor:[[field_ font] fontDescriptor] | 672 NSFont* font = [NSFont fontWithDescriptor:[[field_ font] fontDescriptor] |
| 672 size:[[field_ font] pointSize] + 2]; | 673 size:[[field_ font] pointSize] + 2]; |
| 673 NSDictionary* attributes = | 674 NSDictionary* attributes = |
| 674 [NSDictionary dictionaryWithObject:font | 675 [NSDictionary dictionaryWithObject:font |
| 675 forKey:NSFontAttributeName]; | 676 forKey:NSFontAttributeName]; |
| 676 NSString* const kString = @"This is a test"; | 677 NSString* const kString = @"This is a test"; |
| 677 scoped_nsobject<NSAttributedString> attributedString( | 678 base::scoped_nsobject<NSAttributedString> attributedString( |
| 678 [[NSAttributedString alloc] initWithString:kString | 679 [[NSAttributedString alloc] initWithString:kString |
| 679 attributes:attributes]); | 680 attributes:attributes]); |
| 680 | 681 |
| 681 // Check that what we get back looks like what we put in. | 682 // Check that what we get back looks like what we put in. |
| 682 EXPECT_NSNE(kString, [field_ stringValue]); | 683 EXPECT_NSNE(kString, [field_ stringValue]); |
| 683 [field_ setAttributedStringValue:attributedString]; | 684 [field_ setAttributedStringValue:attributedString]; |
| 684 EXPECT_TRUE([[field_ attributedStringValue] | 685 EXPECT_TRUE([[field_ attributedStringValue] |
| 685 isEqualToAttributedString:attributedString]); | 686 isEqualToAttributedString:attributedString]); |
| 686 EXPECT_NSEQ(kString, [field_ stringValue]); | 687 EXPECT_NSEQ(kString, [field_ stringValue]); |
| 687 | 688 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 700 } | 701 } |
| 701 | 702 |
| 702 // -setAttributedStringValue: shouldn't reset the undo state if things | 703 // -setAttributedStringValue: shouldn't reset the undo state if things |
| 703 // are being editted. | 704 // are being editted. |
| 704 TEST_F(AutocompleteTextFieldTest, SetAttributedStringUndo) { | 705 TEST_F(AutocompleteTextFieldTest, SetAttributedStringUndo) { |
| 705 NSColor* redColor = [NSColor redColor]; | 706 NSColor* redColor = [NSColor redColor]; |
| 706 NSDictionary* attributes = | 707 NSDictionary* attributes = |
| 707 [NSDictionary dictionaryWithObject:redColor | 708 [NSDictionary dictionaryWithObject:redColor |
| 708 forKey:NSForegroundColorAttributeName]; | 709 forKey:NSForegroundColorAttributeName]; |
| 709 NSString* const kString = @"This is a test"; | 710 NSString* const kString = @"This is a test"; |
| 710 scoped_nsobject<NSAttributedString> attributedString( | 711 base::scoped_nsobject<NSAttributedString> attributedString( |
| 711 [[NSAttributedString alloc] initWithString:kString | 712 [[NSAttributedString alloc] initWithString:kString |
| 712 attributes:attributes]); | 713 attributes:attributes]); |
| 713 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; | 714 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 714 EXPECT_TRUE([field_ currentEditor]); | 715 EXPECT_TRUE([field_ currentEditor]); |
| 715 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); | 716 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); |
| 716 NSUndoManager* undoManager = [editor undoManager]; | 717 NSUndoManager* undoManager = [editor undoManager]; |
| 717 EXPECT_TRUE(undoManager); | 718 EXPECT_TRUE(undoManager); |
| 718 | 719 |
| 719 // Nothing to undo, yet. | 720 // Nothing to undo, yet. |
| 720 EXPECT_FALSE([undoManager canUndo]); | 721 EXPECT_FALSE([undoManager canUndo]); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 | 810 |
| 810 // Test that the resign-key notification is forwarded right, and that | 811 // Test that the resign-key notification is forwarded right, and that |
| 811 // the notification is registered and unregistered when the view moves | 812 // the notification is registered and unregistered when the view moves |
| 812 // in and out of the window. | 813 // in and out of the window. |
| 813 // TODO(shess): Should this test the key window for realz? That would | 814 // TODO(shess): Should this test the key window for realz? That would |
| 814 // be really annoying to whoever is running the tests. | 815 // be really annoying to whoever is running the tests. |
| 815 TEST_F(AutocompleteTextFieldObserverTest, ClosePopupOnResignKey) { | 816 TEST_F(AutocompleteTextFieldObserverTest, ClosePopupOnResignKey) { |
| 816 EXPECT_CALL(field_observer_, ClosePopup()); | 817 EXPECT_CALL(field_observer_, ClosePopup()); |
| 817 [test_window() resignKeyWindow]; | 818 [test_window() resignKeyWindow]; |
| 818 | 819 |
| 819 scoped_nsobject<AutocompleteTextField> pin([field_ retain]); | 820 base::scoped_nsobject<AutocompleteTextField> pin([field_ retain]); |
| 820 [field_ removeFromSuperview]; | 821 [field_ removeFromSuperview]; |
| 821 [test_window() resignKeyWindow]; | 822 [test_window() resignKeyWindow]; |
| 822 | 823 |
| 823 [[test_window() contentView] addSubview:field_]; | 824 [[test_window() contentView] addSubview:field_]; |
| 824 EXPECT_CALL(field_observer_, ClosePopup()); | 825 EXPECT_CALL(field_observer_, ClosePopup()); |
| 825 [test_window() resignKeyWindow]; | 826 [test_window() resignKeyWindow]; |
| 826 } | 827 } |
| 827 | 828 |
| 828 } // namespace | 829 } // namespace |
| OLD | NEW |