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.
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"
9 // A test object that counts the number of times a message is sent to it.
10 @interface TestTrackingAreaOwner : NSObject {
12 NSUInteger messageCount_;
14 @property(nonatomic, assign) NSUInteger messageCount;
15 - (void)performMessage;
18 @implementation TestTrackingAreaOwner
19 @synthesize messageCount = messageCount_;
20 - (void)performMessage {
27 class CrTrackingAreaTest : public CocoaTest {
30 : owner_([[TestTrackingAreaOwner alloc] init]),
31 trackingArea_([[CrTrackingArea alloc]
32 initWithRect:NSMakeRect(0, 0, 100, 100)
33 options:NSTrackingMouseMoved | NSTrackingActiveInKeyWindow
38 base::scoped_nsobject<TestTrackingAreaOwner> owner_;
39 base::scoped_nsobject<CrTrackingArea> trackingArea_;
42 TEST_F(CrTrackingAreaTest, OwnerForwards) {
43 [[trackingArea_ owner] performMessage];
44 EXPECT_EQ(1U, [owner_ messageCount]);
46 [[trackingArea_ owner] performMessage];
47 EXPECT_EQ(2U, [owner_ messageCount]);
50 TEST_F(CrTrackingAreaTest, OwnerStopsForwarding) {
51 [[trackingArea_ owner] performMessage];
52 EXPECT_EQ(1U, [owner_ messageCount]);
54 [trackingArea_ clearOwner];
56 [[trackingArea_ owner] performMessage];
57 EXPECT_EQ(1U, [owner_ messageCount]);
60 TEST_F(CrTrackingAreaTest, OwnerAutomaticallyStopsForwardingOnClose) {
61 [test_window() orderFront:nil];
62 [trackingArea_ clearOwnerWhenWindowWillClose:test_window()];
64 [[trackingArea_ owner] performMessage];
65 EXPECT_EQ(1U, [owner_ messageCount]);
67 [test_window() close];
69 [[trackingArea_ owner] performMessage];
70 EXPECT_EQ(1U, [owner_ messageCount]);
73 TEST_F(CrTrackingAreaTest, ScoperInit) {
75 ScopedCrTrackingArea scoper([trackingArea_ retain]);
76 [[scoper.get() owner] performMessage];
77 EXPECT_EQ(1U, [owner_ messageCount]);
80 [[trackingArea_ owner] performMessage];
81 EXPECT_EQ(1U, [owner_ messageCount]);
84 TEST_F(CrTrackingAreaTest, ScoperReset) {
86 ScopedCrTrackingArea scoper;
87 EXPECT_FALSE(scoper.get());
89 scoper.reset([trackingArea_ retain]);
90 [[scoper.get() owner] performMessage];
91 EXPECT_EQ(1U, [owner_ messageCount]);
93 [[scoper.get() owner] performMessage];
94 EXPECT_EQ(2U, [owner_ messageCount]);
97 [[trackingArea_ owner] performMessage];
98 EXPECT_EQ(2U, [owner_ messageCount]);