diff options
author | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-16 15:35:38 +0000 |
---|---|---|
committer | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-16 15:35:38 +0000 |
commit | 3d4560f45d650a9421eff15cd63f3e4b796f01f5 (patch) | |
tree | 1bed2441c7e4b1071adb3d22146f0726f80510cd /chrome | |
parent | d49e4a51428d0234d0dcbb11343315e4f2aa8fd6 (diff) | |
download | chromium_src-3d4560f45d650a9421eff15cd63f3e4b796f01f5.zip chromium_src-3d4560f45d650a9421eff15cd63f3e4b796f01f5.tar.gz chromium_src-3d4560f45d650a9421eff15cd63f3e4b796f01f5.tar.bz2 |
[Mac] Eat mouse clicks in the findbar to prevent them from falling through to views below.
BUG=http://crbug.com/22854
TEST=See test case in bug.
Review URL: http://codereview.chromium.org/273041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29281 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/cocoa/find_bar_view.mm | 63 | ||||
-rw-r--r-- | chrome/browser/cocoa/find_bar_view_unittest.mm | 62 | ||||
-rw-r--r-- | chrome/browser/cocoa/test_event_utils.h | 3 | ||||
-rw-r--r-- | chrome/browser/cocoa/test_event_utils.mm | 12 |
4 files changed, 131 insertions, 9 deletions
diff --git a/chrome/browser/cocoa/find_bar_view.mm b/chrome/browser/cocoa/find_bar_view.mm index 259d59d..93b0c0c 100644 --- a/chrome/browser/cocoa/find_bar_view.mm +++ b/chrome/browser/cocoa/find_bar_view.mm @@ -4,10 +4,13 @@ #import "chrome/browser/cocoa/find_bar_view.h" +namespace { +CGFloat kCurveSize = 8; +} // end namespace + @implementation FindBarView - (void)drawRect:(NSRect)rect { - CGFloat curveSize = 8; // TODO(rohitrao): Make this prettier. rect = NSInsetRect([self bounds], 0.5, 0.5); rect = NSOffsetRect(rect, 0, 1.0); @@ -15,17 +18,17 @@ NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect)); NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect)); NSPoint midLeft1 = - NSMakePoint(NSMinX(rect) + curveSize, NSMaxY(rect) - curveSize); + NSMakePoint(NSMinX(rect) + kCurveSize, NSMaxY(rect) - kCurveSize); NSPoint midLeft2 = - NSMakePoint(NSMinX(rect) + curveSize, NSMinY(rect) + curveSize); + NSMakePoint(NSMinX(rect) + kCurveSize, NSMinY(rect) + kCurveSize); NSPoint midRight1 = - NSMakePoint(NSMaxX(rect) - curveSize, NSMinY(rect) + curveSize); + NSMakePoint(NSMaxX(rect) - kCurveSize, NSMinY(rect) + kCurveSize); NSPoint midRight2 = - NSMakePoint(NSMaxX(rect) - curveSize, NSMaxY(rect) - curveSize); + NSMakePoint(NSMaxX(rect) - kCurveSize, NSMaxY(rect) - kCurveSize); NSPoint bottomLeft = - NSMakePoint(NSMinX(rect) + (2 * curveSize), NSMinY(rect)); + NSMakePoint(NSMinX(rect) + (2 * kCurveSize), NSMinY(rect)); NSPoint bottomRight = - NSMakePoint(NSMaxX(rect) - (2 * curveSize), NSMinY(rect)); + NSMakePoint(NSMaxX(rect) - (2 * kCurveSize), NSMinY(rect)); NSBezierPath* path = [NSBezierPath bezierPath]; [path moveToPoint:topLeft]; @@ -60,4 +63,50 @@ [path stroke]; } +// The findbar is mostly opaque, but has an 8px transparent border on the left +// and right sides (see |kCurveSize|). This is an artifact of the way it is +// drawn. We override hitTest to return nil for points in this transparent +// area. +- (NSView*)hitTest:(NSPoint)point { + NSView* hitView = [super hitTest:point]; + if (hitView == self) { + // |rect| is approximately equivalent to the opaque area of the findbar. + NSRect rect = NSInsetRect([self bounds], kCurveSize, 0); + if (!NSMouseInRect(point, rect, [self isFlipped])) + return nil; + } + + return hitView; +} + +// Eat all mouse events, to prevent clicks from falling through to views below. +- (void)mouseDown:(NSEvent *)theEvent { +} + +- (void)rightMouseDown:(NSEvent *)theEvent { +} + +- (void)otherMouseDown:(NSEvent *)theEvent { +} + +- (void)mouseUp:(NSEvent *)theEvent { +} + +- (void)rightMouseUp:(NSEvent *)theEvent { +} + +- (void)otherMouseUp:(NSEvent *)theEvent { +} + +- (void)mouseMoved:(NSEvent *)theEvent { +} + +- (void)mouseDragged:(NSEvent *)theEvent { +} + +- (void)rightMouseDragged:(NSEvent *)theEvent { +} + +- (void)otherMouseDragged:(NSEvent *)theEvent { +} @end diff --git a/chrome/browser/cocoa/find_bar_view_unittest.mm b/chrome/browser/cocoa/find_bar_view_unittest.mm index 734dcd9..c1fc902 100644 --- a/chrome/browser/cocoa/find_bar_view_unittest.mm +++ b/chrome/browser/cocoa/find_bar_view_unittest.mm @@ -5,11 +5,26 @@ #import <Cocoa/Cocoa.h> #include "base/scoped_nsobject.h" -#import "chrome/browser/cocoa/find_bar_view.h" #import "chrome/browser/cocoa/cocoa_test_helper.h" +#import "chrome/browser/cocoa/find_bar_view.h" +#include "chrome/browser/cocoa/test_event_utils.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" +@interface MouseDownViewPong : NSView { + BOOL pong_; +} +@property(assign) BOOL pong; +@end + +@implementation MouseDownViewPong +@synthesize pong = pong_; +- (void)mouseDown:(NSEvent*)event { + pong_ = YES; +} +@end + + namespace { class FindBarViewTest : public PlatformTest { @@ -37,4 +52,49 @@ TEST_F(FindBarViewTest, Display) { [view_ display]; } +TEST_F(FindBarViewTest, FindBarEatsMouseClicksInBackgroundArea) { + MouseDownViewPong* pongView = + [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]; + + // Remove all of the subviews of the findbar, to make sure we don't + // accidentally hit a subview when trying to simulate a click in the + // background area. + [view_ setSubviews:[NSArray array]]; + [view_ setFrame:NSMakeRect(0, 0, 200, 200)]; + + // Add the pong view as a sibling of the findbar. + [cocoa_helper_.contentView() addSubview:pongView + positioned:NSWindowBelow + relativeTo:view_.get()]; + + // Synthesize a mousedown event and send it to the window. The event is + // placed in the center of the find bar. + NSPoint pointInCenterOfFindBar = NSMakePoint(100, 100); + [pongView setPong:NO]; + [cocoa_helper_.window() + sendEvent:test_event_utils::LeftMouseDownAtPoint(pointInCenterOfFindBar)]; + // Click gets eaten by findbar, not passed through to underlying view. + EXPECT_FALSE([pongView pong]); +} + +TEST_F(FindBarViewTest, FindBarPassesThroughClicksInTransparentArea) { + MouseDownViewPong* pongView = + [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]; + [view_ setFrame:NSMakeRect(0, 0, 200, 200)]; + + // Add the pong view as a sibling of the findbar. + [cocoa_helper_.contentView() addSubview:pongView + positioned:NSWindowBelow + relativeTo:view_.get()]; + + // Synthesize a mousedown event and send it to the window. The event is inset + // a few pixels from the lower left corner of the window, which places it in + // the transparent area surrounding the findbar. + NSPoint pointInTransparentArea = NSMakePoint(2, 2); + [pongView setPong:NO]; + [cocoa_helper_.window() + sendEvent:test_event_utils::LeftMouseDownAtPoint(pointInTransparentArea)]; + // Click is ignored by findbar, passed through to underlying view. + EXPECT_TRUE([pongView pong]); +} } // namespace diff --git a/chrome/browser/cocoa/test_event_utils.h b/chrome/browser/cocoa/test_event_utils.h index c6e8f0a..ac4b0f5 100644 --- a/chrome/browser/cocoa/test_event_utils.h +++ b/chrome/browser/cocoa/test_event_utils.h @@ -25,9 +25,10 @@ class ScopedClassSwizzler { namespace test_event_utils { -// Create a synthetic mouse event for testing. Currently this is very basic, +// Create synthetic mouse events for testing. Currently these are very basic, // flesh out as needed. NSEvent* MakeMouseEvent(NSEventType type, NSUInteger modifiers); +NSEvent* LeftMouseDownAtPoint(NSPoint point); } // namespace test_event_utils diff --git a/chrome/browser/cocoa/test_event_utils.mm b/chrome/browser/cocoa/test_event_utils.mm index 4f14d84..f0c4fc7 100644 --- a/chrome/browser/cocoa/test_event_utils.mm +++ b/chrome/browser/cocoa/test_event_utils.mm @@ -44,4 +44,16 @@ NSEvent* MakeMouseEvent(NSEventType type, NSUInteger modifiers) { pressure:1.0]; } +NSEvent* LeftMouseDownAtPoint(NSPoint point) { + return [NSEvent mouseEventWithType:NSLeftMouseDown + location:point + modifierFlags:0 + timestamp:0 + windowNumber:0 + context:nil + eventNumber:0 + clickCount:1 + pressure:1.0]; +} + } // namespace test_event_utils |