| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "base/memory/scoped_nsobject.h" | 7 #include "base/mac/scoped_nsobject.h" |
| 8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 9 #import "chrome/browser/ui/cocoa/find_bar/find_bar_view.h" | 9 #import "chrome/browser/ui/cocoa/find_bar/find_bar_view.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
| 12 #include "ui/base/test/cocoa_test_event_utils.h" | 12 #include "ui/base/test/cocoa_test_event_utils.h" |
| 13 | 13 |
| 14 @interface MouseDownViewPong : NSView { | 14 @interface MouseDownViewPong : NSView { |
| 15 BOOL pong_; | 15 BOOL pong_; |
| 16 } | 16 } |
| 17 @property (nonatomic, assign) BOOL pong; | 17 @property (nonatomic, assign) BOOL pong; |
| 18 @end | 18 @end |
| 19 | 19 |
| 20 @implementation MouseDownViewPong | 20 @implementation MouseDownViewPong |
| 21 @synthesize pong = pong_; | 21 @synthesize pong = pong_; |
| 22 - (void)mouseDown:(NSEvent*)event { | 22 - (void)mouseDown:(NSEvent*)event { |
| 23 pong_ = YES; | 23 pong_ = YES; |
| 24 } | 24 } |
| 25 @end | 25 @end |
| 26 | 26 |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 class FindBarViewTest : public CocoaTest { | 30 class FindBarViewTest : public CocoaTest { |
| 31 public: | 31 public: |
| 32 FindBarViewTest() { | 32 FindBarViewTest() { |
| 33 NSRect frame = NSMakeRect(0, 0, 100, 30); | 33 NSRect frame = NSMakeRect(0, 0, 100, 30); |
| 34 scoped_nsobject<FindBarView> view( | 34 base::scoped_nsobject<FindBarView> view( |
| 35 [[FindBarView alloc] initWithFrame:frame]); | 35 [[FindBarView alloc] initWithFrame:frame]); |
| 36 view_ = view.get(); | 36 view_ = view.get(); |
| 37 [[test_window() contentView] addSubview:view_]; | 37 [[test_window() contentView] addSubview:view_]; |
| 38 } | 38 } |
| 39 | 39 |
| 40 FindBarView* view_; | 40 FindBarView* view_; |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 TEST_VIEW(FindBarViewTest, view_) | 43 TEST_VIEW(FindBarViewTest, view_) |
| 44 | 44 |
| 45 TEST_F(FindBarViewTest, FindBarEatsMouseClicksInBackgroundArea) { | 45 TEST_F(FindBarViewTest, FindBarEatsMouseClicksInBackgroundArea) { |
| 46 scoped_nsobject<MouseDownViewPong> pongView( | 46 base::scoped_nsobject<MouseDownViewPong> pongView( |
| 47 [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]); | 47 [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]); |
| 48 | 48 |
| 49 // Remove all of the subviews of the findbar, to make sure we don't | 49 // Remove all of the subviews of the findbar, to make sure we don't |
| 50 // accidentally hit a subview when trying to simulate a click in the | 50 // accidentally hit a subview when trying to simulate a click in the |
| 51 // background area. | 51 // background area. |
| 52 [view_ setSubviews:[NSArray array]]; | 52 [view_ setSubviews:[NSArray array]]; |
| 53 [view_ setFrame:NSMakeRect(0, 0, 200, 200)]; | 53 [view_ setFrame:NSMakeRect(0, 0, 200, 200)]; |
| 54 | 54 |
| 55 // Add the pong view as a sibling of the findbar. | 55 // Add the pong view as a sibling of the findbar. |
| 56 [[test_window() contentView] addSubview:pongView.get() | 56 [[test_window() contentView] addSubview:pongView.get() |
| 57 positioned:NSWindowBelow | 57 positioned:NSWindowBelow |
| 58 relativeTo:view_]; | 58 relativeTo:view_]; |
| 59 | 59 |
| 60 // Synthesize a mousedown event and send it to the window. The event is | 60 // Synthesize a mousedown event and send it to the window. The event is |
| 61 // placed in the center of the find bar. | 61 // placed in the center of the find bar. |
| 62 NSPoint pointInCenterOfFindBar = NSMakePoint(100, 100); | 62 NSPoint pointInCenterOfFindBar = NSMakePoint(100, 100); |
| 63 [pongView setPong:NO]; | 63 [pongView setPong:NO]; |
| 64 [test_window() sendEvent: | 64 [test_window() sendEvent: |
| 65 cocoa_test_event_utils::LeftMouseDownAtPoint(pointInCenterOfFindBar)]; | 65 cocoa_test_event_utils::LeftMouseDownAtPoint(pointInCenterOfFindBar)]; |
| 66 // Click gets eaten by findbar, not passed through to underlying view. | 66 // Click gets eaten by findbar, not passed through to underlying view. |
| 67 EXPECT_FALSE([pongView pong]); | 67 EXPECT_FALSE([pongView pong]); |
| 68 } | 68 } |
| 69 | 69 |
| 70 TEST_F(FindBarViewTest, FindBarPassesThroughClicksInTransparentArea) { | 70 TEST_F(FindBarViewTest, FindBarPassesThroughClicksInTransparentArea) { |
| 71 scoped_nsobject<MouseDownViewPong> pongView( | 71 base::scoped_nsobject<MouseDownViewPong> pongView( |
| 72 [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]); | 72 [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]); |
| 73 [view_ setFrame:NSMakeRect(0, 0, 200, 200)]; | 73 [view_ setFrame:NSMakeRect(0, 0, 200, 200)]; |
| 74 | 74 |
| 75 // Add the pong view as a sibling of the findbar. | 75 // Add the pong view as a sibling of the findbar. |
| 76 [[test_window() contentView] addSubview:pongView.get() | 76 [[test_window() contentView] addSubview:pongView.get() |
| 77 positioned:NSWindowBelow | 77 positioned:NSWindowBelow |
| 78 relativeTo:view_]; | 78 relativeTo:view_]; |
| 79 | 79 |
| 80 // Synthesize a mousedown event and send it to the window. The event is inset | 80 // Synthesize a mousedown event and send it to the window. The event is inset |
| 81 // a few pixels from the lower left corner of the window, which places it in | 81 // a few pixels from the lower left corner of the window, which places it in |
| 82 // the transparent area surrounding the findbar. | 82 // the transparent area surrounding the findbar. |
| 83 NSPoint pointInTransparentArea = NSMakePoint(2, 2); | 83 NSPoint pointInTransparentArea = NSMakePoint(2, 2); |
| 84 [pongView setPong:NO]; | 84 [pongView setPong:NO]; |
| 85 [test_window() sendEvent: | 85 [test_window() sendEvent: |
| 86 cocoa_test_event_utils::LeftMouseDownAtPoint(pointInTransparentArea)]; | 86 cocoa_test_event_utils::LeftMouseDownAtPoint(pointInTransparentArea)]; |
| 87 // Click is ignored by findbar, passed through to underlying view. | 87 // Click is ignored by findbar, passed through to underlying view. |
| 88 EXPECT_TRUE([pongView pong]); | 88 EXPECT_TRUE([pongView pong]); |
| 89 } | 89 } |
| 90 } // namespace | 90 } // namespace |
| OLD | NEW |