| 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 #include "base/sys_string_conversions.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 |
| 13 namespace { | 14 namespace { |
| 15 |
| 14 int NumTypesOnPasteboard(NSPasteboard* pb) { | 16 int NumTypesOnPasteboard(NSPasteboard* pb) { |
| 15 return [[pb types] count]; | 17 return [[pb types] count]; |
| 16 } | 18 } |
| 17 | 19 |
| 18 void ClearPasteboard(NSPasteboard* pb) { | 20 void ClearPasteboard(NSPasteboard* pb) { |
| 19 [pb declareTypes:[NSArray array] owner:nil]; | 21 [pb declareTypes:[NSArray array] owner:nil]; |
| 20 } | 22 } |
| 21 | 23 |
| 22 bool NoRichTextOnClipboard(NSPasteboard* pb) { | 24 bool NoRichTextOnClipboard(NSPasteboard* pb) { |
| 23 return ([pb dataForType:NSRTFPboardType] == nil) && | 25 return ([pb dataForType:NSRTFPboardType] == nil) && |
| 24 ([pb dataForType:NSRTFDPboardType] == nil) && | 26 ([pb dataForType:NSRTFDPboardType] == nil) && |
| 25 ([pb dataForType:NSHTMLPboardType] == nil); | 27 ([pb dataForType:NSHTMLPboardType] == nil); |
| 26 } | 28 } |
| 27 | 29 |
| 28 bool ClipboardContainsText(NSPasteboard* pb, NSString* cmp) { | 30 bool ClipboardContainsText(NSPasteboard* pb, NSString* cmp) { |
| 29 NSString* clipboard_text = [pb stringForType:NSStringPboardType]; | 31 NSString* clipboard_text = [pb stringForType:NSStringPboardType]; |
| 30 return [clipboard_text isEqualToString:cmp]; | 32 return [clipboard_text isEqualToString:cmp]; |
| 31 } | 33 } |
| 32 | 34 |
| 35 bool PutStringOnPasteboard(NSPasteboard* pb, NSString* string) { |
| 36 return ([pb setString:string forType:NSStringPboardType] != NO); |
| 37 } |
| 38 |
| 33 } // namespace | 39 } // namespace |
| 34 | 40 |
| 35 class AutocompleteTextFieldEditorTest : public PlatformTest { | 41 class AutocompleteTextFieldEditorTest : public PlatformTest { |
| 36 public: | 42 public: |
| 37 virtual void SetUp() { | 43 virtual void SetUp() { |
| 38 PlatformTest::SetUp(); | 44 PlatformTest::SetUp(); |
| 39 pb_ = [NSPasteboard pasteboardWithUniqueName]; | 45 pb_ = [NSPasteboard pasteboardWithUniqueName]; |
| 40 } | 46 } |
| 41 | 47 |
| 42 virtual void TearDown() { | 48 virtual void TearDown() { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 85 |
| 80 // Check that cutting it works and we only get plain text. | 86 // Check that cutting it works and we only get plain text. |
| 81 [field_editor.get() setString:test_string_2]; | 87 [field_editor.get() setString:test_string_2]; |
| 82 [field_editor.get() selectAll:nil]; | 88 [field_editor.get() selectAll:nil]; |
| 83 [field_editor.get() alignLeft:nil]; // Add a rich text attribute. | 89 [field_editor.get() alignLeft:nil]; // Add a rich text attribute. |
| 84 [field_editor.get() performCut:pb]; | 90 [field_editor.get() performCut:pb]; |
| 85 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); | 91 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); |
| 86 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_2)); | 92 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_2)); |
| 87 ASSERT_EQ([[field_editor.get() textStorage] length], 0U); | 93 ASSERT_EQ([[field_editor.get() textStorage] length], 0U); |
| 88 } | 94 } |
| 95 |
| 96 TEST_F(AutocompleteTextFieldEditorTest, PasteTest) { |
| 97 const int num_tests = 5; |
| 98 NSString *test_string_input[num_tests] = { |
| 99 @"Has_no_spaces", |
| 100 @"Has spaces inside", |
| 101 @" \t Has leading whitespace", |
| 102 @"Has trailing whitespace \t ", |
| 103 @"\rHas s\n cat\rtered new \nlines and spaces\n\n\r" |
| 104 }; |
| 105 |
| 106 // Clear the pasteboard is empty and double-check. |
| 107 NSPasteboard* pb = clipboard(); |
| 108 ClearPasteboard(pb); |
| 109 ASSERT_EQ(NumTypesOnPasteboard(pb), 0); |
| 110 |
| 111 // Set up the pasteboard to take strings. |
| 112 [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
| 113 |
| 114 for(int i = 0; i < num_tests; i++) { |
| 115 // Make a field editor and make sure it's empty. |
| 116 scoped_nsobject<AutocompleteTextFieldEditor> field_editor( |
| 117 [[AutocompleteTextFieldEditor alloc] init]); |
| 118 ASSERT_EQ([[field_editor.get() textStorage] length], 0U); |
| 119 |
| 120 // Put test_string_input[i] on pasteboard and make sure it's there. |
| 121 ASSERT_TRUE(PutStringOnPasteboard(pb, test_string_input[i])); |
| 122 ASSERT_TRUE(ClipboardContainsText(pb, test_string_input[i])); |
| 123 |
| 124 // Paste and make sure we get the corresponding output from |
| 125 // CollapseWhitespace(...,true). CollapseWhitespace() should be tested |
| 126 // elsewhere and should yield the desired output. |
| 127 [field_editor.get() performPaste:pb]; |
| 128 EXPECT_EQ( |
| 129 CollapseWhitespace(base::SysNSStringToWide(test_string_input[i]),true), |
| 130 base::SysNSStringToWide([[field_editor.get() textStorage] string])); |
| 131 } |
| 132 } |
| OLD | NEW |