Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "chrome/browser/cocoa/toolbar_controller.h" | |
| 6 | |
| 7 #include "base/scoped_nsobject.h" | |
| 8 #include "base/scoped_ptr.h" | |
| 9 #include "base/string_util.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "testing/platform_test.h" | |
| 12 | |
| 13 namespace { | |
| 14 int NumTypesOnPasteboard(NSPasteboard* pb) { | |
| 15 return [[pb types] count]; | |
| 16 } | |
| 17 | |
| 18 void ClearPasteboard(NSPasteboard* pb) { | |
| 19 [pb declareTypes:[NSArray array] owner:nil]; | |
| 20 } | |
| 21 | |
| 22 bool NoRichTextOnClipboard(NSPasteboard* pb) { | |
| 23 return ([pb dataForType:NSRTFPboardType] == nil) && | |
| 24 ([pb dataForType:NSRTFDPboardType] == nil) && | |
| 25 ([pb dataForType:NSHTMLPboardType] == nil); | |
| 26 } | |
| 27 | |
| 28 bool ClipboardContainsText(NSPasteboard* pb, NSString* cmp) { | |
| 29 NSString* clipboard_text = [pb stringForType:NSStringPboardType]; | |
| 30 return [clipboard_text isEqualToString:cmp]; | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 class LocationBarFieldEditorTest : public PlatformTest { | |
| 36 public: | |
| 37 virtual void SetUp() { | |
| 38 PlatformTest::SetUp(); | |
| 39 pb_ = [NSPasteboard pasteboardWithUniqueName]; | |
| 40 } | |
| 41 | |
| 42 virtual void TearDown() { | |
| 43 [pb_ releaseGlobally]; | |
| 44 pb_ = nil; | |
| 45 PlatformTest::TearDown(); | |
| 46 } | |
| 47 | |
| 48 NSPasteboard *clipboard() { | |
| 49 DCHECK(pb_); | |
| 50 return pb_; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 NSPasteboard *pb_; | |
| 55 }; | |
| 56 | |
| 57 TEST_F(LocationBarFieldEditorTest, CutCopyTest) { | |
| 58 // Make sure pasteboard is empty before we start. | |
| 59 ASSERT_EQ(NumTypesOnPasteboard(clipboard()), 0); | |
| 60 | |
| 61 NSString* test_string_1 = @"astring"; | |
| 62 NSString* test_string_2 = @"another string"; | |
| 63 | |
| 64 scoped_nsobject<LocationBarFieldEditor> field_editor( | |
| 65 [[LocationBarFieldEditor alloc] init]); | |
| 66 [field_editor.get() setRichText:YES]; | |
| 67 | |
| 68 // Put some text on the clipboard. | |
| 69 [field_editor.get() setString:test_string_1]; | |
| 70 [field_editor.get() selectAll:nil]; | |
| 71 [field_editor.get() alignRight:nil]; // Add a rich text attribute. | |
| 72 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); | |
| 73 | |
| 74 // Check that copying it works and we only get plain text. | |
| 75 NSPasteboard* pb = clipboard(); | |
| 76 [field_editor.get() performCopy:pb]; | |
| 77 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); | |
| 78 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_1)); | |
| 79 | |
| 80 // Check that cutting it works and we only get plain text. | |
| 81 [field_editor.get() setString:test_string_2]; | |
| 82 [field_editor.get() selectAll:nil]; | |
| 83 [field_editor.get() alignLeft:nil]; // Add a rich text attribute. | |
| 84 [field_editor.get() performCut:pb]; | |
| 85 ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); | |
| 86 ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_2)); | |
| 87 ASSERT_EQ([[field_editor.get() textStorage] length], (NSUInteger)0); | |
|
Scott Hess - ex-Googler
2009/06/15 18:22:29
I assume the cast is because of a type mismatch?
| |
| 88 } | |
| OLD | NEW |