| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/cocoa/autocomplete_text_field_editor.h" | 5 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" |
| 6 | 6 |
| 7 #include "base/scoped_nsobject.h" | 7 #include "base/scoped_nsobject.h" |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #import "chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h" |
| 10 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
| 13 | 14 |
| 14 @interface AutocompleteTextFieldEditorTestDelegate : NSObject { | |
| 15 BOOL textShouldPaste_; | |
| 16 BOOL receivedTextShouldPaste_; | |
| 17 } | |
| 18 - initWithTextShouldPaste:(BOOL)flag; | |
| 19 - (BOOL)receivedTextShouldPaste; | |
| 20 @end | |
| 21 | |
| 22 namespace { | 15 namespace { |
| 23 | 16 |
| 24 int NumTypesOnPasteboard(NSPasteboard* pb) { | 17 int NumTypesOnPasteboard(NSPasteboard* pb) { |
| 25 return [[pb types] count]; | 18 return [[pb types] count]; |
| 26 } | 19 } |
| 27 | 20 |
| 28 void ClearPasteboard(NSPasteboard* pb) { | 21 void ClearPasteboard(NSPasteboard* pb) { |
| 29 [pb declareTypes:[NSArray array] owner:nil]; | 22 [pb declareTypes:[NSArray array] owner:nil]; |
| 30 } | 23 } |
| 31 | 24 |
| 32 bool NoRichTextOnClipboard(NSPasteboard* pb) { | 25 bool NoRichTextOnClipboard(NSPasteboard* pb) { |
| 33 return ([pb dataForType:NSRTFPboardType] == nil) && | 26 return ([pb dataForType:NSRTFPboardType] == nil) && |
| 34 ([pb dataForType:NSRTFDPboardType] == nil) && | 27 ([pb dataForType:NSRTFDPboardType] == nil) && |
| 35 ([pb dataForType:NSHTMLPboardType] == nil); | 28 ([pb dataForType:NSHTMLPboardType] == nil); |
| 36 } | 29 } |
| 37 | 30 |
| 38 bool ClipboardContainsText(NSPasteboard* pb, NSString* cmp) { | 31 bool ClipboardContainsText(NSPasteboard* pb, NSString* cmp) { |
| 39 NSString* clipboard_text = [pb stringForType:NSStringPboardType]; | 32 NSString* clipboard_text = [pb stringForType:NSStringPboardType]; |
| 40 return [clipboard_text isEqualToString:cmp]; | 33 return [clipboard_text isEqualToString:cmp]; |
| 41 } | 34 } |
| 42 | 35 |
| 43 class AutocompleteTextFieldEditorTest : public PlatformTest { | 36 class AutocompleteTextFieldEditorTest : public PlatformTest { |
| 44 public: | 37 public: |
| 45 AutocompleteTextFieldEditorTest() | 38 AutocompleteTextFieldEditorTest() |
| 46 : pb_([NSPasteboard pasteboardWithUniqueName]) { | 39 : pb_([NSPasteboard pasteboardWithUniqueName]) { |
| 47 NSRect frame = NSMakeRect(0, 0, 50, 30); | 40 NSRect frame = NSMakeRect(0, 0, 50, 30); |
| 48 editor_.reset([[AutocompleteTextFieldEditor alloc] initWithFrame:frame]); | 41 field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]); |
| 49 [editor_ setString:@"Testing"]; | 42 [field_ setStringValue:@"Testing"]; |
| 50 [cocoa_helper_.contentView() addSubview:editor_.get()]; | 43 [field_ setObserver:&field_observer_]; |
| 44 [cocoa_helper_.contentView() addSubview:field_.get()]; |
| 45 |
| 46 // Arrange for |field_| to get the right field editor. |
| 47 window_delegate_.reset( |
| 48 [[AutocompleteTextFieldWindowTestDelegate alloc] init]); |
| 49 [cocoa_helper_.window() setDelegate:window_delegate_]; |
| 50 |
| 51 // Get the field editor setup. |
| 52 cocoa_helper_.makeFirstResponder(field_); |
| 53 id editor = [field_.get() currentEditor]; |
| 54 editor_.reset([static_cast<AutocompleteTextFieldEditor*>(editor) retain]); |
| 51 } | 55 } |
| 52 | 56 |
| 57 virtual void SetUp() { |
| 58 EXPECT_TRUE(editor_.get() != nil); |
| 59 EXPECT_TRUE( |
| 60 [editor_.get() isKindOfClass:[AutocompleteTextFieldEditor class]]); |
| 61 } |
| 62 |
| 63 // The removeFromSuperview call is needed to prevent crashes in |
| 64 // later tests. |
| 65 // TODO(shess): -removeromSuperview should not be necessary. Fix |
| 66 // it. Also in autocomplete_text_field_unittest.mm. |
| 53 virtual ~AutocompleteTextFieldEditorTest() { | 67 virtual ~AutocompleteTextFieldEditorTest() { |
| 68 [cocoa_helper_.window() setDelegate:nil]; |
| 69 [field_ removeFromSuperview]; |
| 54 [pb_ releaseGlobally]; | 70 [pb_ releaseGlobally]; |
| 55 } | 71 } |
| 56 | 72 |
| 57 NSPasteboard *clipboard() { | 73 NSPasteboard *clipboard() { |
| 58 DCHECK(pb_); | 74 DCHECK(pb_); |
| 59 return pb_; | 75 return pb_; |
| 60 } | 76 } |
| 61 | 77 |
| 62 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 78 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
| 63 scoped_nsobject<AutocompleteTextFieldEditor> editor_; | 79 scoped_nsobject<AutocompleteTextFieldEditor> editor_; |
| 80 scoped_nsobject<AutocompleteTextField> field_; |
| 81 AutocompleteTextFieldObserverMock field_observer_; |
| 82 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; |
| 64 | 83 |
| 65 private: | 84 private: |
| 66 NSPasteboard *pb_; | 85 NSPasteboard *pb_; |
| 67 }; | 86 }; |
| 68 | 87 |
| 88 // Test that the field editor is linked in correctly. |
| 89 TEST_F(AutocompleteTextFieldEditorTest, FirstResponder) { |
| 90 EXPECT_EQ(editor_.get(), [field_ currentEditor]); |
| 91 EXPECT_TRUE([editor_.get() isDescendantOf:field_.get()]); |
| 92 EXPECT_EQ([editor_.get() delegate], field_.get()); |
| 93 EXPECT_EQ([editor_.get() observer], [field_.get() observer]); |
| 94 } |
| 95 |
| 69 TEST_F(AutocompleteTextFieldEditorTest, CutCopyTest) { | 96 TEST_F(AutocompleteTextFieldEditorTest, CutCopyTest) { |
| 70 // Make sure pasteboard is empty before we start. | 97 // Make sure pasteboard is empty before we start. |
| 71 ASSERT_EQ(NumTypesOnPasteboard(clipboard()), 0); | 98 ASSERT_EQ(NumTypesOnPasteboard(clipboard()), 0); |
| 72 | 99 |
| 73 NSString* test_string_1 = @"astring"; | 100 NSString* test_string_1 = @"astring"; |
| 74 NSString* test_string_2 = @"another string"; | 101 NSString* test_string_2 = @"another string"; |
| 75 | 102 |
| 76 [editor_.get() setRichText:YES]; | 103 [editor_.get() setRichText:YES]; |
| 77 | 104 |
| 78 // Put some text on the clipboard. | 105 // Put some text on the clipboard. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 92 [editor_.get() alignLeft:nil]; // Add a rich text attribute. | 119 [editor_.get() alignLeft:nil]; // Add a rich text attribute. |
| 93 [editor_.get() performCut:clipboard()]; | 120 [editor_.get() performCut:clipboard()]; |
| 94 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); | 121 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); |
| 95 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_2)); | 122 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_2)); |
| 96 ASSERT_EQ([[editor_.get() textStorage] length], 0U); | 123 ASSERT_EQ([[editor_.get() textStorage] length], 0U); |
| 97 } | 124 } |
| 98 | 125 |
| 99 // Test adding/removing from the view hierarchy, mostly to ensure nothing | 126 // Test adding/removing from the view hierarchy, mostly to ensure nothing |
| 100 // leaks or crashes. | 127 // leaks or crashes. |
| 101 TEST_F(AutocompleteTextFieldEditorTest, AddRemove) { | 128 TEST_F(AutocompleteTextFieldEditorTest, AddRemove) { |
| 102 EXPECT_EQ(cocoa_helper_.contentView(), [editor_ superview]); | 129 EXPECT_EQ(cocoa_helper_.contentView(), [field_ superview]); |
| 103 [editor_.get() removeFromSuperview]; | 130 |
| 104 EXPECT_FALSE([editor_ superview]); | 131 // TODO(shess): For some reason, -removeFromSuperview while |field_| |
| 132 // is first-responder causes AutocompleteTextFieldWindowTestDelegate |
| 133 // -windowWillReturnFieldEditor:toObject: to be passed an object of |
| 134 // class AutocompleteTextFieldEditor. Which is weird. Changing |
| 135 // first responder will remove the field editor. |
| 136 cocoa_helper_.makeFirstResponder(nil); |
| 137 EXPECT_FALSE([field_.get() currentEditor]); |
| 138 EXPECT_FALSE([editor_.get() superview]); |
| 139 |
| 140 [field_.get() removeFromSuperview]; |
| 141 EXPECT_FALSE([field_.get() superview]); |
| 105 } | 142 } |
| 106 | 143 |
| 107 // Test drawing, mostly to ensure nothing leaks or crashes. | 144 // Test drawing, mostly to ensure nothing leaks or crashes. |
| 108 TEST_F(AutocompleteTextFieldEditorTest, Display) { | 145 TEST_F(AutocompleteTextFieldEditorTest, Display) { |
| 146 [field_ display]; |
| 109 [editor_ display]; | 147 [editor_ display]; |
| 110 } | 148 } |
| 111 | 149 |
| 112 // Test that -shouldPaste properly queries the delegate. | 150 // Test that -paste: is correctly delegated to the observer. |
| 113 TEST_F(AutocompleteTextFieldEditorTest, TextShouldPaste) { | 151 TEST_F(AutocompleteTextFieldEditorTest, Paste) { |
| 114 EXPECT_TRUE(![editor_ delegate]); | 152 field_observer_.Reset(); |
| 115 EXPECT_TRUE([editor_ shouldPaste]); | 153 EXPECT_FALSE(field_observer_.on_paste_called_); |
| 116 | 154 [editor_.get() paste:nil]; |
| 117 scoped_nsobject<AutocompleteTextFieldEditorTestDelegate> shouldPaste( | 155 EXPECT_TRUE(field_observer_.on_paste_called_); |
| 118 [[AutocompleteTextFieldEditorTestDelegate alloc] | |
| 119 initWithTextShouldPaste:YES]); | |
| 120 [editor_ setDelegate:shouldPaste]; | |
| 121 EXPECT_FALSE([shouldPaste receivedTextShouldPaste]); | |
| 122 EXPECT_TRUE([editor_ shouldPaste]); | |
| 123 EXPECT_TRUE([shouldPaste receivedTextShouldPaste]); | |
| 124 | |
| 125 scoped_nsobject<AutocompleteTextFieldEditorTestDelegate> shouldNotPaste( | |
| 126 [[AutocompleteTextFieldEditorTestDelegate alloc] | |
| 127 initWithTextShouldPaste:NO]); | |
| 128 [editor_ setDelegate:shouldNotPaste]; | |
| 129 EXPECT_FALSE([shouldNotPaste receivedTextShouldPaste]); | |
| 130 EXPECT_FALSE([editor_ shouldPaste]); | |
| 131 EXPECT_TRUE([shouldNotPaste receivedTextShouldPaste]); | |
| 132 } | 156 } |
| 133 | 157 |
| 134 } // namespace | 158 } // namespace |
| 135 | |
| 136 @implementation AutocompleteTextFieldEditorTestDelegate | |
| 137 | |
| 138 - initWithTextShouldPaste:(BOOL)flag { | |
| 139 self = [super init]; | |
| 140 if (self) { | |
| 141 textShouldPaste_ = flag; | |
| 142 receivedTextShouldPaste_ = NO; | |
| 143 } | |
| 144 return self; | |
| 145 } | |
| 146 | |
| 147 - (BOOL)receivedTextShouldPaste { | |
| 148 return receivedTextShouldPaste_; | |
| 149 } | |
| 150 | |
| 151 - (BOOL)textShouldPaste:(NSText*)fieldEditor { | |
| 152 receivedTextShouldPaste_ = YES; | |
| 153 return textShouldPaste_; | |
| 154 } | |
| 155 | |
| 156 @end | |
| OLD | NEW |