BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / find_pasteboard_unittest.mm
blob374abc6851220f80a9567e270f029f66f6ba4d8b
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 {
15  @public
16   int notificationCount_;
17  @private
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;
28 @end
30 @implementation FindPasteboardTesting
32 - (id)init {
33   if ((self = [super init])) {
34     pboard_ = [NSPasteboard pasteboardWithUniqueName];
35   }
36   return self;
39 - (void)dealloc {
40   [pboard_ releaseGlobally];
41   [super dealloc];
44 - (NSPasteboard*)findPboard {
45   return pboard_;
48 - (void)callback:(id)sender {
49   ++notificationCount_;
52 - (void)setFindPboardText:(NSString*)text {
53   [pboard_ declareTypes:[NSArray arrayWithObject:NSStringPboardType]
54                   owner:nil];
55   [pboard_ setString:text forType:NSStringPboardType];
58 - (NSString*)findPboardText {
59   return [pboard_ stringForType:NSStringPboardType];
61 @end
63 namespace {
65 class FindPasteboardTest : public CocoaTest {
66  public:
67   FindPasteboardTest() {
68     pboard_.reset([[FindPasteboardTesting alloc] init]);
69   }
70  protected:
71   base::scoped_nsobject<FindPasteboardTesting> pboard_;
74 TEST_F(FindPasteboardTest, SettingTextUpdatesPboard) {
75   [pboard_.get() setFindText:@"text"];
76   EXPECT_EQ(
77       NSOrderedSame,
78       [[pboard_.get() findPboardText] compare:@"text"]);
81 TEST_F(FindPasteboardTest, ReadingFromPboardUpdatesFindText) {
82   [pboard_.get() setFindPboardText:@"text"];
83   [pboard_.get() loadTextFromPasteboard:nil];
84   EXPECT_EQ(
85       NSOrderedSame,
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()];
115 }  // namespace