| 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 <Cocoa/Cocoa.h> |
| 6 |
| 7 #include "base/scoped_nsobject.h" |
| 8 #import "chrome/browser/cocoa/find_pasteboard.h" |
| 9 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/platform_test.h" |
| 12 |
| 13 // A subclass of FindPasteboard that doesn't write to the real find pasteboard. |
| 14 @interface FindPasteboardTesting : FindPasteboard { |
| 15 @public |
| 16 int notificationCount_; |
| 17 @private |
| 18 scoped_nsobject<NSPasteboard> pboard_; |
| 19 } |
| 20 - (NSPasteboard*)findPboard; |
| 21 |
| 22 - (void)callback:(id)sender; |
| 23 |
| 24 // These are for checking that pasteboard content is copied to/from the |
| 25 // FindPasteboard correctly. |
| 26 - (NSString*)findPboardText; |
| 27 - (void)setFindPboardText:(NSString*)text; |
| 28 @end |
| 29 |
| 30 @implementation FindPasteboardTesting |
| 31 |
| 32 - (id)init { |
| 33 if ((self = [super init])) { |
| 34 pboard_.reset([[NSPasteboard pasteboardWithUniqueName] retain]); |
| 35 } |
| 36 return self; |
| 37 } |
| 38 |
| 39 - (NSPasteboard*)findPboard { |
| 40 return pboard_.get(); |
| 41 } |
| 42 |
| 43 - (void)callback:(id)sender { |
| 44 ++notificationCount_; |
| 45 } |
| 46 |
| 47 - (void)setFindPboardText:(NSString*)text { |
| 48 [pboard_.get() declareTypes:[NSArray arrayWithObject:NSStringPboardType] |
| 49 owner:nil]; |
| 50 [pboard_.get() setString:text forType:NSStringPboardType]; |
| 51 } |
| 52 |
| 53 - (NSString*)findPboardText { |
| 54 return [pboard_.get() stringForType:NSStringPboardType]; |
| 55 } |
| 56 @end |
| 57 |
| 58 namespace { |
| 59 |
| 60 class FindPasteboardTest : public PlatformTest { |
| 61 public: |
| 62 FindPasteboardTest() { |
| 63 pboard_.reset([[FindPasteboardTesting alloc] init]); |
| 64 } |
| 65 protected: |
| 66 scoped_nsobject<FindPasteboardTesting> pboard_; |
| 67 CocoaTestHelper helper_; |
| 68 }; |
| 69 |
| 70 TEST_F(FindPasteboardTest, SettingTextUpdatesPboard) { |
| 71 [pboard_.get() setFindText:@"text"]; |
| 72 EXPECT_EQ( |
| 73 NSOrderedSame, |
| 74 [[pboard_.get() findPboardText] compare:@"text"]); |
| 75 } |
| 76 |
| 77 TEST_F(FindPasteboardTest, ReadingFromPboardUpdatesFindText) { |
| 78 [pboard_.get() setFindPboardText:@"text"]; |
| 79 [pboard_.get() loadTextFromPasteboard:nil]; |
| 80 EXPECT_EQ( |
| 81 NSOrderedSame, |
| 82 [[pboard_.get() findText] compare:@"text"]); |
| 83 } |
| 84 |
| 85 TEST_F(FindPasteboardTest, SendsNotificationWhenTextChanges) { |
| 86 [[NSNotificationCenter defaultCenter] |
| 87 addObserver:pboard_.get() |
| 88 selector:@selector(callback:) |
| 89 name:kFindPasteboardChangedNotification |
| 90 object:pboard_.get()]; |
| 91 EXPECT_EQ(0, pboard_.get()->notificationCount_); |
| 92 [pboard_.get() setFindText:@"text"]; |
| 93 EXPECT_EQ(1, pboard_.get()->notificationCount_); |
| 94 [pboard_.get() setFindText:@"text"]; |
| 95 EXPECT_EQ(1, pboard_.get()->notificationCount_); |
| 96 [pboard_.get() setFindText:@"other text"]; |
| 97 EXPECT_EQ(2, pboard_.get()->notificationCount_); |
| 98 |
| 99 [pboard_.get() setFindPboardText:@"other text"]; |
| 100 [pboard_.get() loadTextFromPasteboard:nil]; |
| 101 EXPECT_EQ(2, pboard_.get()->notificationCount_); |
| 102 |
| 103 [pboard_.get() setFindPboardText:@"otherer text"]; |
| 104 [pboard_.get() loadTextFromPasteboard:nil]; |
| 105 EXPECT_EQ(3, pboard_.get()->notificationCount_); |
| 106 |
| 107 [[NSNotificationCenter defaultCenter] removeObserver:pboard_.get()]; |
| 108 } |
| 109 |
| 110 |
| 111 } // namespace |
| OLD | NEW |