1 // Copyright (c) 2011 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.
5 #import <Cocoa/Cocoa.h>
7 #include "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
11 #import "ui/base/cocoa/find_pasteboard.h"
13 // A subclass of FindPasteboard that doesn't write to the real find pasteboard.
14 @interface FindPasteboardTesting : FindPasteboard {
16 int notificationCount_;
18 NSPasteboard* pboard_;
20 - (NSPasteboard*)findPboard;
22 - (void)callback:(id)sender;
24 // These are for checking that pasteboard content is copied to/from the
25 // FindPasteboard correctly.
26 - (NSString*)findPboardText;
27 - (void)setFindPboardText:(NSString*)text;
30 @implementation FindPasteboardTesting
33 if ((self = [super init])) {
34 pboard_ = [NSPasteboard pasteboardWithUniqueName];
40 [pboard_ releaseGlobally];
44 - (NSPasteboard*)findPboard {
48 - (void)callback:(id)sender {
52 - (void)setFindPboardText:(NSString*)text {
53 [pboard_ declareTypes:[NSArray arrayWithObject:NSStringPboardType]
55 [pboard_ setString:text forType:NSStringPboardType];
58 - (NSString*)findPboardText {
59 return [pboard_ stringForType:NSStringPboardType];
65 class FindPasteboardTest : public CocoaTest {
67 FindPasteboardTest() {
68 pboard_.reset([[FindPasteboardTesting alloc] init]);
71 base::scoped_nsobject<FindPasteboardTesting> pboard_;
74 TEST_F(FindPasteboardTest, SettingTextUpdatesPboard) {
75 [pboard_.get() setFindText:@"text"];
78 [[pboard_.get() findPboardText] compare:@"text"]);
81 TEST_F(FindPasteboardTest, ReadingFromPboardUpdatesFindText) {
82 [pboard_.get() setFindPboardText:@"text"];
83 [pboard_.get() loadTextFromPasteboard:nil];
86 [[pboard_.get() findText] compare:@"text"]);
89 TEST_F(FindPasteboardTest, SendsNotificationWhenTextChanges) {
90 [[NSNotificationCenter defaultCenter]
91 addObserver:pboard_.get()
92 selector:@selector(callback:)
93 name:kFindPasteboardChangedNotification
94 object:pboard_.get()];
95 EXPECT_EQ(0, pboard_.get()->notificationCount_);
96 [pboard_.get() setFindText:@"text"];
97 EXPECT_EQ(1, pboard_.get()->notificationCount_);
98 [pboard_.get() setFindText:@"text"];
99 EXPECT_EQ(1, pboard_.get()->notificationCount_);
100 [pboard_.get() setFindText:@"other text"];
101 EXPECT_EQ(2, pboard_.get()->notificationCount_);
103 [pboard_.get() setFindPboardText:@"other text"];
104 [pboard_.get() loadTextFromPasteboard:nil];
105 EXPECT_EQ(2, pboard_.get()->notificationCount_);
107 [pboard_.get() setFindPboardText:@"otherer text"];
108 [pboard_.get() loadTextFromPasteboard:nil];
109 EXPECT_EQ(3, pboard_.get()->notificationCount_);
111 [[NSNotificationCenter defaultCenter] removeObserver:pboard_.get()];