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/cocoa_test_helper.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "testing/platform_test.h" | 12 #include "testing/platform_test.h" |
12 | 13 |
| 14 @interface AutocompleteTextFieldEditorTestDelegate : NSObject { |
| 15 BOOL textShouldPaste_; |
| 16 BOOL receivedTextShouldPaste_; |
| 17 } |
| 18 - initWithTextShouldPaste:(BOOL)flag; |
| 19 - (BOOL)receivedTextShouldPaste; |
| 20 @end |
| 21 |
13 namespace { | 22 namespace { |
14 int NumTypesOnPasteboard(NSPasteboard* pb) { | 23 int NumTypesOnPasteboard(NSPasteboard* pb) { |
15 return [[pb types] count]; | 24 return [[pb types] count]; |
16 } | 25 } |
17 | 26 |
18 void ClearPasteboard(NSPasteboard* pb) { | 27 void ClearPasteboard(NSPasteboard* pb) { |
19 [pb declareTypes:[NSArray array] owner:nil]; | 28 [pb declareTypes:[NSArray array] owner:nil]; |
20 } | 29 } |
21 | 30 |
22 bool NoRichTextOnClipboard(NSPasteboard* pb) { | 31 bool NoRichTextOnClipboard(NSPasteboard* pb) { |
23 return ([pb dataForType:NSRTFPboardType] == nil) && | 32 return ([pb dataForType:NSRTFPboardType] == nil) && |
24 ([pb dataForType:NSRTFDPboardType] == nil) && | 33 ([pb dataForType:NSRTFDPboardType] == nil) && |
25 ([pb dataForType:NSHTMLPboardType] == nil); | 34 ([pb dataForType:NSHTMLPboardType] == nil); |
26 } | 35 } |
27 | 36 |
28 bool ClipboardContainsText(NSPasteboard* pb, NSString* cmp) { | 37 bool ClipboardContainsText(NSPasteboard* pb, NSString* cmp) { |
29 NSString* clipboard_text = [pb stringForType:NSStringPboardType]; | 38 NSString* clipboard_text = [pb stringForType:NSStringPboardType]; |
30 return [clipboard_text isEqualToString:cmp]; | 39 return [clipboard_text isEqualToString:cmp]; |
31 } | 40 } |
32 | 41 |
33 } // namespace | |
34 | |
35 class AutocompleteTextFieldEditorTest : public PlatformTest { | 42 class AutocompleteTextFieldEditorTest : public PlatformTest { |
36 public: | 43 public: |
| 44 AutocompleteTextFieldEditorTest() { |
| 45 NSRect frame = NSMakeRect(0, 0, 50, 30); |
| 46 editor_.reset([[AutocompleteTextFieldEditor alloc] initWithFrame:frame]); |
| 47 [editor_ setString:@"Testing"]; |
| 48 [cocoa_helper_.contentView() addSubview:editor_.get()]; |
| 49 } |
| 50 |
37 virtual void SetUp() { | 51 virtual void SetUp() { |
38 PlatformTest::SetUp(); | 52 PlatformTest::SetUp(); |
39 pb_ = [NSPasteboard pasteboardWithUniqueName]; | 53 pb_ = [NSPasteboard pasteboardWithUniqueName]; |
40 } | 54 } |
41 | 55 |
42 virtual void TearDown() { | 56 virtual void TearDown() { |
43 [pb_ releaseGlobally]; | 57 [pb_ releaseGlobally]; |
44 pb_ = nil; | 58 pb_ = nil; |
45 PlatformTest::TearDown(); | 59 PlatformTest::TearDown(); |
46 } | 60 } |
47 | 61 |
48 NSPasteboard *clipboard() { | 62 NSPasteboard *clipboard() { |
49 DCHECK(pb_); | 63 DCHECK(pb_); |
50 return pb_; | 64 return pb_; |
51 } | 65 } |
52 | 66 |
53 private: | 67 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
54 NSPasteboard *pb_; | 68 scoped_nsobject<AutocompleteTextFieldEditor> editor_; |
| 69 |
| 70 private: |
| 71 NSPasteboard *pb_; |
55 }; | 72 }; |
56 | 73 |
57 TEST_F(AutocompleteTextFieldEditorTest, CutCopyTest) { | 74 TEST_F(AutocompleteTextFieldEditorTest, CutCopyTest) { |
58 // Make sure pasteboard is empty before we start. | 75 // Make sure pasteboard is empty before we start. |
59 ASSERT_EQ(NumTypesOnPasteboard(clipboard()), 0); | 76 ASSERT_EQ(NumTypesOnPasteboard(clipboard()), 0); |
60 | 77 |
61 NSString* test_string_1 = @"astring"; | 78 NSString* test_string_1 = @"astring"; |
62 NSString* test_string_2 = @"another string"; | 79 NSString* test_string_2 = @"another string"; |
63 | 80 |
64 scoped_nsobject<AutocompleteTextFieldEditor> field_editor( | 81 [editor_.get() setRichText:YES]; |
65 [[AutocompleteTextFieldEditor alloc] init]); | |
66 [field_editor.get() setRichText:YES]; | |
67 | 82 |
68 // Put some text on the clipboard. | 83 // Put some text on the clipboard. |
69 [field_editor.get() setString:test_string_1]; | 84 [editor_.get() setString:test_string_1]; |
70 [field_editor.get() selectAll:nil]; | 85 [editor_.get() selectAll:nil]; |
71 [field_editor.get() alignRight:nil]; // Add a rich text attribute. | 86 [editor_.get() alignRight:nil]; // Add a rich text attribute. |
72 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); | 87 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); |
73 | 88 |
74 // Check that copying it works and we only get plain text. | 89 // Check that copying it works and we only get plain text. |
75 NSPasteboard* pb = clipboard(); | 90 NSPasteboard* pb = clipboard(); |
76 [field_editor.get() performCopy:pb]; | 91 [editor_.get() performCopy:pb]; |
77 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); | 92 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); |
78 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_1)); | 93 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_1)); |
79 | 94 |
80 // Check that cutting it works and we only get plain text. | 95 // Check that cutting it works and we only get plain text. |
81 [field_editor.get() setString:test_string_2]; | 96 [editor_.get() setString:test_string_2]; |
82 [field_editor.get() selectAll:nil]; | 97 [editor_.get() selectAll:nil]; |
83 [field_editor.get() alignLeft:nil]; // Add a rich text attribute. | 98 [editor_.get() alignLeft:nil]; // Add a rich text attribute. |
84 [field_editor.get() performCut:pb]; | 99 [editor_.get() performCut:pb]; |
85 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); | 100 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); |
86 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_2)); | 101 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_2)); |
87 ASSERT_EQ([[field_editor.get() textStorage] length], 0U); | 102 ASSERT_EQ([[editor_.get() textStorage] length], 0U); |
88 } | 103 } |
| 104 |
| 105 // Test adding/removing from the view hierarchy, mostly to ensure nothing |
| 106 // leaks or crashes. |
| 107 TEST_F(AutocompleteTextFieldEditorTest, AddRemove) { |
| 108 EXPECT_EQ(cocoa_helper_.contentView(), [editor_ superview]); |
| 109 [editor_.get() removeFromSuperview]; |
| 110 EXPECT_FALSE([editor_ superview]); |
| 111 } |
| 112 |
| 113 // Test drawing, mostly to ensure nothing leaks or crashes. |
| 114 TEST_F(AutocompleteTextFieldEditorTest, Display) { |
| 115 [editor_ display]; |
| 116 } |
| 117 |
| 118 // Test that -shouldPaste properly queries the delegate. |
| 119 TEST_F(AutocompleteTextFieldEditorTest, TextShouldPaste) { |
| 120 EXPECT_TRUE(![editor_ delegate]); |
| 121 EXPECT_TRUE([editor_ shouldPaste]); |
| 122 |
| 123 scoped_nsobject<AutocompleteTextFieldEditorTestDelegate> shouldPaste( |
| 124 [[AutocompleteTextFieldEditorTestDelegate alloc] |
| 125 initWithTextShouldPaste:YES]); |
| 126 [editor_ setDelegate:shouldPaste]; |
| 127 EXPECT_FALSE([shouldPaste receivedTextShouldPaste]); |
| 128 EXPECT_TRUE([editor_ shouldPaste]); |
| 129 EXPECT_TRUE([shouldPaste receivedTextShouldPaste]); |
| 130 |
| 131 scoped_nsobject<AutocompleteTextFieldEditorTestDelegate> shouldNotPaste( |
| 132 [[AutocompleteTextFieldEditorTestDelegate alloc] |
| 133 initWithTextShouldPaste:NO]); |
| 134 [editor_ setDelegate:shouldNotPaste]; |
| 135 EXPECT_FALSE([shouldNotPaste receivedTextShouldPaste]); |
| 136 EXPECT_FALSE([editor_ shouldPaste]); |
| 137 EXPECT_TRUE([shouldNotPaste receivedTextShouldPaste]); |
| 138 } |
| 139 |
| 140 } // namespace |
| 141 |
| 142 @implementation AutocompleteTextFieldEditorTestDelegate |
| 143 |
| 144 - initWithTextShouldPaste:(BOOL)flag { |
| 145 self = [super init]; |
| 146 if (self) { |
| 147 textShouldPaste_ = flag; |
| 148 receivedTextShouldPaste_ = NO; |
| 149 } |
| 150 return self; |
| 151 } |
| 152 |
| 153 - (BOOL)receivedTextShouldPaste { |
| 154 return receivedTextShouldPaste_; |
| 155 } |
| 156 |
| 157 - (BOOL)textShouldPaste:(NSText*)fieldEditor { |
| 158 receivedTextShouldPaste_ = YES; |
| 159 return textShouldPaste_; |
| 160 } |
| 161 |
| 162 @end |
OLD | NEW |