[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 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 | |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 7 | #include "base/mac/scoped_nsobject.h" |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 8 | #include "base/memory/ref_counted.h" |
lgrey | 92aad3c | 2016-12-10 01:22:18 | [diff] [blame] | 9 | #import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | #include "testing/platform_test.h" |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 12 | #include "ui/base/clipboard/clipboard_util_mac.h" |
[email protected] | feb7283 | 2012-03-08 00:38:24 | [diff] [blame] | 13 | #import "ui/base/cocoa/find_pasteboard.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 14 | |
| 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 |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 20 | scoped_refptr<ui::UniquePasteboard> pboard_; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 21 | } |
| 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] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 34 | - (NSPasteboard*)findPboard { |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 35 | // 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] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | - (void)callback:(id)sender { |
| 43 | ++notificationCount_; |
| 44 | } |
| 45 | |
| 46 | - (void)setFindPboardText:(NSString*)text { |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 47 | [pboard_->get() declareTypes:[NSArray arrayWithObject:NSStringPboardType] |
| 48 | owner:nil]; |
| 49 | [pboard_->get() setString:text forType:NSStringPboardType]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | - (NSString*)findPboardText { |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 53 | return [pboard_->get() stringForType:NSStringPboardType]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 54 | } |
| 55 | @end |
| 56 | |
| 57 | namespace { |
| 58 | |
| 59 | class FindPasteboardTest : public CocoaTest { |
| 60 | public: |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 61 | FindPasteboardTest() {} |
| 62 | |
| 63 | void SetUp() override { |
| 64 | CocoaTest::SetUp(); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 65 | pboard_.reset([[FindPasteboardTesting alloc] init]); |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 66 | ASSERT_TRUE(pboard_.get()); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 67 | } |
erikchen | d9b76f0 | 2016-04-01 21:48:52 | [diff] [blame] | 68 | |
| 69 | void TearDown() override { |
| 70 | pboard_.reset(); |
| 71 | CocoaTest::TearDown(); |
| 72 | } |
| 73 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 74 | protected: |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 75 | base::scoped_nsobject<FindPasteboardTesting> pboard_; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | TEST_F(FindPasteboardTest, SettingTextUpdatesPboard) { |
| 79 | [pboard_.get() setFindText:@"text"]; |
| 80 | EXPECT_EQ( |
| 81 | NSOrderedSame, |
| 82 | [[pboard_.get() findPboardText] compare:@"text"]); |
| 83 | } |
| 84 | |
| 85 | TEST_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 | |
| 93 | TEST_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 |