Home | History | Annotate | Download | only in cocoa
      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.
      4 
      5 #import <Cocoa/Cocoa.h>
      6 
      7 #include "base/memory/scoped_nsobject.h"
      8 #import "chrome/browser/ui/cocoa/find_pasteboard.h"
      9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "testing/platform_test.h"
     12 
     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_;
     19 }
     20 - (NSPasteboard*)findPboard;
     21 
     22 - (void)callback:(id)sender;
     23 
     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
     29 
     30 @implementation FindPasteboardTesting
     31 
     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 {
     45   return pboard_;
     46 }
     47 
     48 - (void)callback:(id)sender {
     49   ++notificationCount_;
     50 }
     51 
     52 - (void)setFindPboardText:(NSString*)text {
     53   [pboard_ declareTypes:[NSArray arrayWithObject:NSStringPboardType]
     54                   owner:nil];
     55   [pboard_ setString:text forType:NSStringPboardType];
     56 }
     57 
     58 - (NSString*)findPboardText {
     59   return [pboard_ stringForType:NSStringPboardType];
     60 }
     61 @end
     62 
     63 namespace {
     64 
     65 class FindPasteboardTest : public CocoaTest {
     66  public:
     67   FindPasteboardTest() {
     68     pboard_.reset([[FindPasteboardTesting alloc] init]);
     69   }
     70  protected:
     71   scoped_nsobject<FindPasteboardTesting> pboard_;
     72 };
     73 
     74 TEST_F(FindPasteboardTest, SettingTextUpdatesPboard) {
     75   [pboard_.get() setFindText:@"text"];
     76   EXPECT_EQ(
     77       NSOrderedSame,
     78       [[pboard_.get() findPboardText] compare:@"text"]);
     79 }
     80 
     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"]);
     87 }
     88 
     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_);
    102 
    103   [pboard_.get() setFindPboardText:@"other text"];
    104   [pboard_.get() loadTextFromPasteboard:nil];
    105   EXPECT_EQ(2, pboard_.get()->notificationCount_);
    106 
    107   [pboard_.get() setFindPboardText:@"otherer text"];
    108   [pboard_.get() loadTextFromPasteboard:nil];
    109   EXPECT_EQ(3, pboard_.get()->notificationCount_);
    110 
    111   [[NSNotificationCenter defaultCenter] removeObserver:pboard_.get()];
    112 }
    113 
    114 
    115 }  // namespace
    116