blob: 942e650971a54dd73495413bfe2bb0f6712b5fab [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]7d791652010-12-01 16:34:492// 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
[email protected]a8522032013-06-24 22:51:467#include "base/mac/scoped_nsobject.h"
erikchend9b76f02016-04-01 21:48:528#include "base/memory/ref_counted.h"
lgrey92aad3c2016-12-10 01:22:189#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
[email protected]7d791652010-12-01 16:34:4910#include "testing/gtest/include/gtest/gtest.h"
11#include "testing/platform_test.h"
erikchend9b76f02016-04-01 21:48:5212#include "ui/base/clipboard/clipboard_util_mac.h"
[email protected]feb72832012-03-08 00:38:2413#import "ui/base/cocoa/find_pasteboard.h"
[email protected]7d791652010-12-01 16:34:4914
15// A subclass of FindPasteboard that doesn't write to the real find pasteboard.
16@interface FindPasteboardTesting : FindPasteboard {
17 @public
18 int notificationCount_;
19 @private
erikchend9b76f02016-04-01 21:48:5220 scoped_refptr<ui::UniquePasteboard> pboard_;
[email protected]7d791652010-12-01 16:34:4921}
22- (NSPasteboard*)findPboard;
23
24- (void)callback:(id)sender;
25
26// These are for checking that pasteboard content is copied to/from the
27// FindPasteboard correctly.
28- (NSString*)findPboardText;
29- (void)setFindPboardText:(NSString*)text;
30@end
31
32@implementation FindPasteboardTesting
33
[email protected]7d791652010-12-01 16:34:4934- (NSPasteboard*)findPboard {
erikchend9b76f02016-04-01 21:48:5235 // This method is called by the super class's -init, otherwise initialization
36 // would go into this class's -init.
37 if (!pboard_)
38 pboard_ = new ui::UniquePasteboard;
39 return pboard_->get();
[email protected]7d791652010-12-01 16:34:4940}
41
42- (void)callback:(id)sender {
43 ++notificationCount_;
44}
45
46- (void)setFindPboardText:(NSString*)text {
erikchend9b76f02016-04-01 21:48:5247 [pboard_->get() declareTypes:[NSArray arrayWithObject:NSStringPboardType]
48 owner:nil];
49 [pboard_->get() setString:text forType:NSStringPboardType];
[email protected]7d791652010-12-01 16:34:4950}
51
52- (NSString*)findPboardText {
erikchend9b76f02016-04-01 21:48:5253 return [pboard_->get() stringForType:NSStringPboardType];
[email protected]7d791652010-12-01 16:34:4954}
55@end
56
57namespace {
58
59class FindPasteboardTest : public CocoaTest {
60 public:
erikchend9b76f02016-04-01 21:48:5261 FindPasteboardTest() {}
62
63 void SetUp() override {
64 CocoaTest::SetUp();
[email protected]7d791652010-12-01 16:34:4965 pboard_.reset([[FindPasteboardTesting alloc] init]);
erikchend9b76f02016-04-01 21:48:5266 ASSERT_TRUE(pboard_.get());
[email protected]7d791652010-12-01 16:34:4967 }
erikchend9b76f02016-04-01 21:48:5268
69 void TearDown() override {
70 pboard_.reset();
71 CocoaTest::TearDown();
72 }
73
[email protected]7d791652010-12-01 16:34:4974 protected:
[email protected]a8522032013-06-24 22:51:4675 base::scoped_nsobject<FindPasteboardTesting> pboard_;
[email protected]7d791652010-12-01 16:34:4976};
77
78TEST_F(FindPasteboardTest, SettingTextUpdatesPboard) {
79 [pboard_.get() setFindText:@"text"];
80 EXPECT_EQ(
81 NSOrderedSame,
82 [[pboard_.get() findPboardText] compare:@"text"]);
83}
84
85TEST_F(FindPasteboardTest, ReadingFromPboardUpdatesFindText) {
86 [pboard_.get() setFindPboardText:@"text"];
87 [pboard_.get() loadTextFromPasteboard:nil];
88 EXPECT_EQ(
89 NSOrderedSame,
90 [[pboard_.get() findText] compare:@"text"]);
91}
92
93TEST_F(FindPasteboardTest, SendsNotificationWhenTextChanges) {
94 [[NSNotificationCenter defaultCenter]
95 addObserver:pboard_.get()
96 selector:@selector(callback:)
97 name:kFindPasteboardChangedNotification
98 object:pboard_.get()];
99 EXPECT_EQ(0, pboard_.get()->notificationCount_);
100 [pboard_.get() setFindText:@"text"];
101 EXPECT_EQ(1, pboard_.get()->notificationCount_);
102 [pboard_.get() setFindText:@"text"];
103 EXPECT_EQ(1, pboard_.get()->notificationCount_);
104 [pboard_.get() setFindText:@"other text"];
105 EXPECT_EQ(2, pboard_.get()->notificationCount_);
106
107 [pboard_.get() setFindPboardText:@"other text"];
108 [pboard_.get() loadTextFromPasteboard:nil];
109 EXPECT_EQ(2, pboard_.get()->notificationCount_);
110
111 [pboard_.get() setFindPboardText:@"otherer text"];
112 [pboard_.get() loadTextFromPasteboard:nil];
113 EXPECT_EQ(3, pboard_.get()->notificationCount_);
114
115 [[NSNotificationCenter defaultCenter] removeObserver:pboard_.get()];
116}
117
118
119} // namespace