[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] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 5 | #include "base/memory/scoped_nsobject.h" |
[email protected] | 36ec017 | 2013-03-12 01:13:44 | [diff] [blame^] | 6 | #import "ui/base/cocoa/tracking_area.h" |
| 7 | #import "ui/base/test/ui_cocoa_test_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 |
| 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 | |
[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 | |
| 38 | scoped_nsobject<TestTrackingAreaOwner> owner_; |
| 39 | scoped_nsobject<CrTrackingArea> trackingArea_; |
| 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 | |
| 60 | TEST_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 | |
[email protected] | 0441a65 | 2011-03-04 21:45:27 | [diff] [blame] | 73 | TEST_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 | |
| 84 | TEST_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 | } |
[email protected] | 36ec017 | 2013-03-12 01:13:44 | [diff] [blame^] | 100 | |
| 101 | } // namespace ui |