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