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 #include "base/memory/scoped_nsobject.h" 6 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" 7 #include "chrome/browser/ui/cocoa/objc_zombie.h" 8 #import "chrome/browser/ui/cocoa/tracking_area.h" 9 10 // A test object that counts the number of times a message is sent to it. 11 @interface TestTrackingAreaOwner : NSObject { 12 @private 13 NSUInteger messageCount_; 14 } 15 @property(nonatomic, assign) NSUInteger messageCount; 16 - (void)performMessage; 17 @end 18 19 @implementation TestTrackingAreaOwner 20 @synthesize messageCount = messageCount_; 21 - (void)performMessage { 22 ++messageCount_; 23 } 24 @end 25 26 class CrTrackingAreaTest : public CocoaTest { 27 public: 28 CrTrackingAreaTest() 29 : owner_([[TestTrackingAreaOwner alloc] init]), 30 trackingArea_([[CrTrackingArea alloc] 31 initWithRect:NSMakeRect(0, 0, 100, 100) 32 options:NSTrackingMouseMoved | NSTrackingActiveInKeyWindow 33 proxiedOwner:owner_.get() 34 userInfo:nil]) { 35 } 36 37 scoped_nsobject<TestTrackingAreaOwner> owner_; 38 scoped_nsobject<CrTrackingArea> trackingArea_; 39 }; 40 41 TEST_F(CrTrackingAreaTest, OwnerForwards) { 42 [[trackingArea_ owner] performMessage]; 43 EXPECT_EQ(1U, [owner_ messageCount]); 44 45 [[trackingArea_ owner] performMessage]; 46 EXPECT_EQ(2U, [owner_ messageCount]); 47 } 48 49 TEST_F(CrTrackingAreaTest, OwnerStopsForwarding) { 50 [[trackingArea_ owner] performMessage]; 51 EXPECT_EQ(1U, [owner_ messageCount]); 52 53 [trackingArea_ clearOwner]; 54 55 [[trackingArea_ owner] performMessage]; 56 EXPECT_EQ(1U, [owner_ messageCount]); 57 } 58 59 TEST_F(CrTrackingAreaTest, OwnerAutomaticallyStopsForwardingOnClose) { 60 [test_window() orderFront:nil]; 61 [trackingArea_ clearOwnerWhenWindowWillClose:test_window()]; 62 63 [[trackingArea_ owner] performMessage]; 64 EXPECT_EQ(1U, [owner_ messageCount]); 65 66 [test_window() close]; 67 68 [[trackingArea_ owner] performMessage]; 69 EXPECT_EQ(1U, [owner_ messageCount]); 70 } 71 72 TEST_F(CrTrackingAreaTest, ZombieOwner) { 73 EXPECT_TRUE(ObjcEvilDoers::ZombieEnable(NO, 20)); 74 75 [[trackingArea_ owner] performMessage]; 76 EXPECT_EQ(1U, [owner_ messageCount]); 77 78 [owner_ shouldBecomeCrZombie]; 79 owner_.reset(); 80 [trackingArea_ clearOwner]; 81 82 [[trackingArea_ owner] performMessage]; 83 // Don't crash! 84 85 ObjcEvilDoers::ZombieDisable(); 86 } 87 88 TEST_F(CrTrackingAreaTest, ScoperInit) { 89 { 90 ScopedCrTrackingArea scoper([trackingArea_ retain]); 91 [[scoper.get() owner] performMessage]; 92 EXPECT_EQ(1U, [owner_ messageCount]); 93 } 94 95 [[trackingArea_ owner] performMessage]; 96 EXPECT_EQ(1U, [owner_ messageCount]); 97 } 98 99 TEST_F(CrTrackingAreaTest, ScoperReset) { 100 { 101 ScopedCrTrackingArea scoper; 102 EXPECT_FALSE(scoper.get()); 103 104 scoper.reset([trackingArea_ retain]); 105 [[scoper.get() owner] performMessage]; 106 EXPECT_EQ(1U, [owner_ messageCount]); 107 108 [[scoper.get() owner] performMessage]; 109 EXPECT_EQ(2U, [owner_ messageCount]); 110 } 111 112 [[trackingArea_ owner] performMessage]; 113 EXPECT_EQ(2U, [owner_ messageCount]); 114 } 115