Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: chrome/browser/ui/cocoa/find_pasteboard_unittest.mm

Issue 1852433004: Fix NSPasteboard leaks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compile error. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "base/mac/scoped_nsobject.h" 7 #include "base/mac/scoped_nsobject.h"
8 #include "base/memory/ref_counted.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h" 11 #include "testing/platform_test.h"
12 #include "ui/base/clipboard/clipboard_util_mac.h"
11 #import "ui/base/cocoa/find_pasteboard.h" 13 #import "ui/base/cocoa/find_pasteboard.h"
12 14
13 // A subclass of FindPasteboard that doesn't write to the real find pasteboard. 15 // A subclass of FindPasteboard that doesn't write to the real find pasteboard.
14 @interface FindPasteboardTesting : FindPasteboard { 16 @interface FindPasteboardTesting : FindPasteboard {
15 @public 17 @public
16 int notificationCount_; 18 int notificationCount_;
17 @private 19 @private
18 NSPasteboard* pboard_; 20 scoped_refptr<ui::UniquePasteboard> pboard_;
19 } 21 }
20 - (NSPasteboard*)findPboard; 22 - (NSPasteboard*)findPboard;
21 23
22 - (void)callback:(id)sender; 24 - (void)callback:(id)sender;
23 25
24 // These are for checking that pasteboard content is copied to/from the 26 // These are for checking that pasteboard content is copied to/from the
25 // FindPasteboard correctly. 27 // FindPasteboard correctly.
26 - (NSString*)findPboardText; 28 - (NSString*)findPboardText;
27 - (void)setFindPboardText:(NSString*)text; 29 - (void)setFindPboardText:(NSString*)text;
28 @end 30 @end
29 31
30 @implementation FindPasteboardTesting 32 @implementation FindPasteboardTesting
31 33
32 - (id)init {
33 if ((self = [super init])) {
34 pboard_ = [NSPasteboard pasteboardWithUniqueName];
35 }
36 return self;
37 }
38
39 - (void)dealloc {
40 [pboard_ releaseGlobally];
41 [super dealloc];
42 }
43
44 - (NSPasteboard*)findPboard { 34 - (NSPasteboard*)findPboard {
45 return pboard_; 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();
46 } 40 }
47 41
48 - (void)callback:(id)sender { 42 - (void)callback:(id)sender {
49 ++notificationCount_; 43 ++notificationCount_;
50 } 44 }
51 45
52 - (void)setFindPboardText:(NSString*)text { 46 - (void)setFindPboardText:(NSString*)text {
53 [pboard_ declareTypes:[NSArray arrayWithObject:NSStringPboardType] 47 [pboard_->get() declareTypes:[NSArray arrayWithObject:NSStringPboardType]
54 owner:nil]; 48 owner:nil];
55 [pboard_ setString:text forType:NSStringPboardType]; 49 [pboard_->get() setString:text forType:NSStringPboardType];
56 } 50 }
57 51
58 - (NSString*)findPboardText { 52 - (NSString*)findPboardText {
59 return [pboard_ stringForType:NSStringPboardType]; 53 return [pboard_->get() stringForType:NSStringPboardType];
60 } 54 }
61 @end 55 @end
62 56
63 namespace { 57 namespace {
64 58
65 class FindPasteboardTest : public CocoaTest { 59 class FindPasteboardTest : public CocoaTest {
66 public: 60 public:
67 FindPasteboardTest() { 61 FindPasteboardTest() {}
62
63 void SetUp() override {
64 CocoaTest::SetUp();
68 pboard_.reset([[FindPasteboardTesting alloc] init]); 65 pboard_.reset([[FindPasteboardTesting alloc] init]);
66 ASSERT_TRUE(pboard_.get());
69 } 67 }
68
69 void TearDown() override {
70 pboard_.reset();
71 CocoaTest::TearDown();
72 }
73
70 protected: 74 protected:
71 base::scoped_nsobject<FindPasteboardTesting> pboard_; 75 base::scoped_nsobject<FindPasteboardTesting> pboard_;
72 }; 76 };
73 77
74 TEST_F(FindPasteboardTest, SettingTextUpdatesPboard) { 78 TEST_F(FindPasteboardTest, SettingTextUpdatesPboard) {
75 [pboard_.get() setFindText:@"text"]; 79 [pboard_.get() setFindText:@"text"];
76 EXPECT_EQ( 80 EXPECT_EQ(
77 NSOrderedSame, 81 NSOrderedSame,
78 [[pboard_.get() findPboardText] compare:@"text"]); 82 [[pboard_.get() findPboardText] compare:@"text"]);
79 } 83 }
(...skipping 26 matching lines...) Expand all
106 110
107 [pboard_.get() setFindPboardText:@"otherer text"]; 111 [pboard_.get() setFindPboardText:@"otherer text"];
108 [pboard_.get() loadTextFromPasteboard:nil]; 112 [pboard_.get() loadTextFromPasteboard:nil];
109 EXPECT_EQ(3, pboard_.get()->notificationCount_); 113 EXPECT_EQ(3, pboard_.get()->notificationCount_);
110 114
111 [[NSNotificationCenter defaultCenter] removeObserver:pboard_.get()]; 115 [[NSNotificationCenter defaultCenter] removeObserver:pboard_.get()];
112 } 116 }
113 117
114 118
115 } // namespace 119 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698