Index: chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm |
diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm |
index d09eb8492084a5ef0e34260af922a98ad209fea0..6d4296431f80b6bdb01d4a9fbb1b48e2a9c822f6 100644 |
--- a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm |
+++ b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm |
@@ -7,18 +7,11 @@ |
#include "base/scoped_nsobject.h" |
#include "base/scoped_ptr.h" |
#include "base/string_util.h" |
+#import "chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h" |
#import "chrome/browser/cocoa/cocoa_test_helper.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "testing/platform_test.h" |
-@interface AutocompleteTextFieldEditorTestDelegate : NSObject { |
- BOOL textShouldPaste_; |
- BOOL receivedTextShouldPaste_; |
-} |
-- initWithTextShouldPaste:(BOOL)flag; |
-- (BOOL)receivedTextShouldPaste; |
-@end |
- |
namespace { |
int NumTypesOnPasteboard(NSPasteboard* pb) { |
@@ -45,12 +38,35 @@ class AutocompleteTextFieldEditorTest : public PlatformTest { |
AutocompleteTextFieldEditorTest() |
: pb_([NSPasteboard pasteboardWithUniqueName]) { |
NSRect frame = NSMakeRect(0, 0, 50, 30); |
- editor_.reset([[AutocompleteTextFieldEditor alloc] initWithFrame:frame]); |
- [editor_ setString:@"Testing"]; |
- [cocoa_helper_.contentView() addSubview:editor_.get()]; |
+ field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]); |
+ [field_ setStringValue:@"Testing"]; |
+ [field_ setObserver:&field_observer_]; |
+ [cocoa_helper_.contentView() addSubview:field_.get()]; |
+ |
+ // Arrange for |field_| to get the right field editor. |
+ window_delegate_.reset( |
+ [[AutocompleteTextFieldWindowTestDelegate alloc] init]); |
+ [cocoa_helper_.window() setDelegate:window_delegate_]; |
+ |
+ // Get the field editor setup. |
+ cocoa_helper_.makeFirstResponder(field_); |
+ id editor = [field_.get() currentEditor]; |
+ editor_.reset([static_cast<AutocompleteTextFieldEditor*>(editor) retain]); |
+ } |
+ |
+ virtual void SetUp() { |
+ EXPECT_TRUE(editor_.get() != nil); |
+ EXPECT_TRUE( |
+ [editor_.get() isKindOfClass:[AutocompleteTextFieldEditor class]]); |
} |
+ // The removeFromSuperview call is needed to prevent crashes in |
+ // later tests. |
+ // TODO(shess): -removeromSuperview should not be necessary. Fix |
+ // it. Also in autocomplete_text_field_unittest.mm. |
virtual ~AutocompleteTextFieldEditorTest() { |
+ [cocoa_helper_.window() setDelegate:nil]; |
+ [field_ removeFromSuperview]; |
[pb_ releaseGlobally]; |
} |
@@ -61,11 +77,22 @@ class AutocompleteTextFieldEditorTest : public PlatformTest { |
CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
scoped_nsobject<AutocompleteTextFieldEditor> editor_; |
+ scoped_nsobject<AutocompleteTextField> field_; |
+ AutocompleteTextFieldObserverMock field_observer_; |
+ scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; |
private: |
NSPasteboard *pb_; |
}; |
+// Test that the field editor is linked in correctly. |
+TEST_F(AutocompleteTextFieldEditorTest, FirstResponder) { |
+ EXPECT_EQ(editor_.get(), [field_ currentEditor]); |
+ EXPECT_TRUE([editor_.get() isDescendantOf:field_.get()]); |
+ EXPECT_EQ([editor_.get() delegate], field_.get()); |
+ EXPECT_EQ([editor_.get() observer], [field_.get() observer]); |
+} |
+ |
TEST_F(AutocompleteTextFieldEditorTest, CutCopyTest) { |
// Make sure pasteboard is empty before we start. |
ASSERT_EQ(NumTypesOnPasteboard(clipboard()), 0); |
@@ -99,58 +126,33 @@ TEST_F(AutocompleteTextFieldEditorTest, CutCopyTest) { |
// Test adding/removing from the view hierarchy, mostly to ensure nothing |
// leaks or crashes. |
TEST_F(AutocompleteTextFieldEditorTest, AddRemove) { |
- EXPECT_EQ(cocoa_helper_.contentView(), [editor_ superview]); |
- [editor_.get() removeFromSuperview]; |
- EXPECT_FALSE([editor_ superview]); |
+ EXPECT_EQ(cocoa_helper_.contentView(), [field_ superview]); |
+ |
+ // TODO(shess): For some reason, -removeFromSuperview while |field_| |
+ // is first-responder causes AutocompleteTextFieldWindowTestDelegate |
+ // -windowWillReturnFieldEditor:toObject: to be passed an object of |
+ // class AutocompleteTextFieldEditor. Which is weird. Changing |
+ // first responder will remove the field editor. |
+ cocoa_helper_.makeFirstResponder(nil); |
+ EXPECT_FALSE([field_.get() currentEditor]); |
+ EXPECT_FALSE([editor_.get() superview]); |
+ |
+ [field_.get() removeFromSuperview]; |
+ EXPECT_FALSE([field_.get() superview]); |
} |
// Test drawing, mostly to ensure nothing leaks or crashes. |
TEST_F(AutocompleteTextFieldEditorTest, Display) { |
+ [field_ display]; |
[editor_ display]; |
} |
-// Test that -shouldPaste properly queries the delegate. |
-TEST_F(AutocompleteTextFieldEditorTest, TextShouldPaste) { |
- EXPECT_TRUE(![editor_ delegate]); |
- EXPECT_TRUE([editor_ shouldPaste]); |
- |
- scoped_nsobject<AutocompleteTextFieldEditorTestDelegate> shouldPaste( |
- [[AutocompleteTextFieldEditorTestDelegate alloc] |
- initWithTextShouldPaste:YES]); |
- [editor_ setDelegate:shouldPaste]; |
- EXPECT_FALSE([shouldPaste receivedTextShouldPaste]); |
- EXPECT_TRUE([editor_ shouldPaste]); |
- EXPECT_TRUE([shouldPaste receivedTextShouldPaste]); |
- |
- scoped_nsobject<AutocompleteTextFieldEditorTestDelegate> shouldNotPaste( |
- [[AutocompleteTextFieldEditorTestDelegate alloc] |
- initWithTextShouldPaste:NO]); |
- [editor_ setDelegate:shouldNotPaste]; |
- EXPECT_FALSE([shouldNotPaste receivedTextShouldPaste]); |
- EXPECT_FALSE([editor_ shouldPaste]); |
- EXPECT_TRUE([shouldNotPaste receivedTextShouldPaste]); |
+// Test that -paste: is correctly delegated to the observer. |
+TEST_F(AutocompleteTextFieldEditorTest, Paste) { |
+ field_observer_.Reset(); |
+ EXPECT_FALSE(field_observer_.on_paste_called_); |
+ [editor_.get() paste:nil]; |
+ EXPECT_TRUE(field_observer_.on_paste_called_); |
} |
} // namespace |
- |
-@implementation AutocompleteTextFieldEditorTestDelegate |
- |
-- initWithTextShouldPaste:(BOOL)flag { |
- self = [super init]; |
- if (self) { |
- textShouldPaste_ = flag; |
- receivedTextShouldPaste_ = NO; |
- } |
- return self; |
-} |
- |
-- (BOOL)receivedTextShouldPaste { |
- return receivedTextShouldPaste_; |
-} |
- |
-- (BOOL)textShouldPaste:(NSText*)fieldEditor { |
- receivedTextShouldPaste_ = YES; |
- return textShouldPaste_; |
-} |
- |
-@end |