[email protected] | f5558b97 | 2011-02-15 12:50:35 | [diff] [blame] | 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 | |
[email protected] | 36ec017 | 2013-03-12 01:13:44 | [diff] [blame] | 5 | #import "ui/base/cocoa/tracking_area.h" |
Sidney San MartÃn | d86c40f | 2017-12-21 20:23:47 | [diff] [blame] | 6 | #include "base/mac/scoped_nsobject.h" |
| 7 | #import "ui/base/test/cocoa_helper.h" |
[email protected] | f5558b97 | 2011-02-15 12:50:35 | [diff] [blame] | 8 | |
| 9 | // A test object that counts the number of times a message is sent to it. |
| 10 | @interface TestTrackingAreaOwner : NSObject { |
| 11 | @private |
Robert Liao | 60f733e7 | 2019-12-10 22:31:59 | [diff] [blame] | 12 | NSUInteger _messageCount; |
[email protected] | f5558b97 | 2011-02-15 12:50:35 | [diff] [blame] | 13 | } |
| 14 | @property(nonatomic, assign) NSUInteger messageCount; |
| 15 | - (void)performMessage; |
| 16 | @end |
| 17 | |
| 18 | @implementation TestTrackingAreaOwner |
Robert Liao | 60f733e7 | 2019-12-10 22:31:59 | [diff] [blame] | 19 | @synthesize messageCount = _messageCount; |
[email protected] | f5558b97 | 2011-02-15 12:50:35 | [diff] [blame] | 20 | - (void)performMessage { |
Robert Liao | 60f733e7 | 2019-12-10 22:31:59 | [diff] [blame] | 21 | ++_messageCount; |
[email protected] | f5558b97 | 2011-02-15 12:50:35 | [diff] [blame] | 22 | } |
| 23 | @end |
| 24 | |
[email protected] | 36ec017 | 2013-03-12 01:13:44 | [diff] [blame] | 25 | namespace ui { |
| 26 | |
[email protected] | f5558b97 | 2011-02-15 12:50:35 | [diff] [blame] | 27 | class 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 |
[email protected] | d3ac5632 | 2012-07-20 23:12:24 | [diff] [blame] | 34 | owner:owner_.get() |
[email protected] | f5558b97 | 2011-02-15 12:50:35 | [diff] [blame] | 35 | userInfo:nil]) { |
| 36 | } |
| 37 | |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 38 | base::scoped_nsobject<TestTrackingAreaOwner> owner_; |
| 39 | base::scoped_nsobject<CrTrackingArea> trackingArea_; |
[email protected] | f5558b97 | 2011-02-15 12:50:35 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | TEST_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 | |
| 50 | TEST_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 | |
[email protected] | 0441a65 | 2011-03-04 21:45:27 | [diff] [blame] | 60 | TEST_F(CrTrackingAreaTest, ScoperInit) { |
| 61 | { |
| 62 | ScopedCrTrackingArea scoper([trackingArea_ retain]); |
| 63 | [[scoper.get() owner] performMessage]; |
| 64 | EXPECT_EQ(1U, [owner_ messageCount]); |
| 65 | } |
| 66 | |
| 67 | [[trackingArea_ owner] performMessage]; |
| 68 | EXPECT_EQ(1U, [owner_ messageCount]); |
| 69 | } |
| 70 | |
| 71 | TEST_F(CrTrackingAreaTest, ScoperReset) { |
| 72 | { |
| 73 | ScopedCrTrackingArea scoper; |
| 74 | EXPECT_FALSE(scoper.get()); |
| 75 | |
| 76 | scoper.reset([trackingArea_ retain]); |
| 77 | [[scoper.get() owner] performMessage]; |
| 78 | EXPECT_EQ(1U, [owner_ messageCount]); |
| 79 | |
| 80 | [[scoper.get() owner] performMessage]; |
| 81 | EXPECT_EQ(2U, [owner_ messageCount]); |
| 82 | } |
| 83 | |
| 84 | [[trackingArea_ owner] performMessage]; |
| 85 | EXPECT_EQ(2U, [owner_ messageCount]); |
| 86 | } |
[email protected] | 36ec017 | 2013-03-12 01:13:44 | [diff] [blame] | 87 | |
| 88 | } // namespace ui |